CentOS Stream 9
Sponsored Link

KVM : Install VM Management Tools2022/01/20

 
Install useful tools for virtual machine management.
[1] Install required packages.
[root@dlp ~]#
dnf -y install guestfs-tools virt-top
[2] Create a Virtual machine from officially provided images.
# display available OS template

[root@dlp ~]#
virt-builder -l

.....
.....
centos-7.8               x86_64     CentOS 7.8
centos-8.0               x86_64     CentOS 8.0
centos-8.2               x86_64     CentOS 8.2
cirros-0.3.1             x86_64     CirrOS 0.3.1
cirros-0.3.5             x86_64     CirrOS 0.3.5
debian-6                 x86_64     Debian 6 (Squeeze)
debian-7                 sparc64    Debian 7 (Wheezy) (sparc64)
debian-7                 x86_64     Debian 7 (wheezy)
debian-8                 x86_64     Debian 8 (jessie)
debian-9                 x86_64     Debian 9 (stretch)
debian-10                x86_64     Debian 10 (buster)
debian-11                x86_64     Debian 11 (bullseye)
.....
.....

# create an image of [debian-11]
# replace the [myrootpassword] to the root password you'd like to set

[root@dlp ~]#
virt-builder debian-11 --format qcow2 --size 10G -o /var/kvm/images/debian-11.qcow2 --root-password password:myrootpassword

[   6.7] Downloading: http://builder.libguestfs.org/debian-11.xz
[ 144.1] Planning how to build this image
[ 144.1] Uncompressing
[ 148.4] Resizing (using virt-resize) to expand the disk to 10.0G
[ 173.6] Opening the new disk
[ 178.6] Setting a random seed
virt-builder: warning: random seed could not be set for this type of guest
[ 178.7] Setting passwords
[ 179.6] Finishing off
                   Output file: /var/kvm/images/debian-11.qcow2
                   Output size: 10.0G
                 Output format: qcow2
            Total usable space: 9.8G
                    Free space: 8.8G (89%)

# to create a VM with the image above, run [virt-install]

[root@dlp ~]#
virt-install \
--name debian-11 \
--ram 4096 \
--disk path=/var/kvm/images/debian-11.qcow2 \
--vcpus 2 \
--os-variant debian11 \
--network bridge=br0 \
--graphics none \
--serial pty \
--console pty \
--boot hd \
--import
[3] [ls] a directory in a virtual machine.
[root@dlp ~]#
virt-ls -l -d centos-st9 /root

total 28
dr-xr-x---.  2 root root  135 Jan 19 04:53 .
dr-xr-xr-x. 18 root root  235 Jan 19 04:46 ..
-rw-------.  1 root root   16 Jan 19 04:53 .bash_history
-rw-r--r--.  1 root root   18 Aug 10 04:16 .bash_logout
-rw-r--r--.  1 root root  141 Aug 10 04:16 .bash_profile
-rw-r--r--.  1 root root  429 Aug 10 04:16 .bashrc
-rw-r--r--.  1 root root  100 Aug 10 04:16 .cshrc
-rw-r--r--.  1 root root  129 Aug 10 04:16 .tcshrc
-rw-------.  1 root root 1100 Jan 19 04:50 anaconda-ks.cfg
[4] [cat] a file in a virtual machine.
[root@dlp ~]#
virt-cat -d centos-st9 /etc/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
.....
.....
[5] Edit a file in a virtual machine.
[root@dlp ~]#
virt-edit -d centos-st9 /etc/fstab


#
# /etc/fstab
# Created by anaconda on Wed Jan 19 04:45:38 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cs-root     /                       xfs     defaults        0 0
UUID=0b07140d-5c31-4323-8097-ce20fe13908d /boot xfs     defaults        0 0
/dev/mapper/cs-swap     none                    swap    defaults        0 0
[6] Display disk usage in a virtual machine.
[root@dlp ~]#
virt-df -d centos-st9

Filesystem                           1K-blocks       Used  Available  Use%
centos-st9:/dev/sda1                   1038336     185388     852948   18%
centos-st9:/dev/cs/root               17811456    1407800   16403656    8%
[7] Mount a disk for a virtual machine.
[root@dlp ~]#
guestmount -d centos-st9 -i /media

[root@dlp ~]#
ll /media

total 20
dr-xr-xr-x.  2 root root    6 Aug  9 15:40 afs
lrwxrwxrwx.  1 root root    7 Aug  9 15:40 bin -> usr/bin
dr-xr-xr-x.  5 root root 4096 Jan 18 22:52 boot
drwxr-xr-x.  2 root root    6 Jan 18 22:45 dev
drwxr-xr-x. 99 root root 8192 Jan 18 22:52 etc
.....
.....
[8] Display the status of virtual machines.
[root@dlp ~]#
virt-top

virt-top 00:00:48 - x86_64 8/8CPU 2593MHz 15735MB
3 domains, 0 active, 0 running, 0 sleeping, 0 paused, 3 inactive D:0 O:0 X:0
CPU: 0.0%  Mem: 0 MB (0 MB by guests)
Matched Content