Ubuntu 22.04
Sponsored Link

KVM : Management tools for VM2022/04/27

 
Install useful tools for virtual machine management.
[1] Install required packages.
root@dlp:~#
apt -y install libguestfs-tools virt-top
[2] Get official OS image and Create a Virtual Machine.
( If you'd like to create VM from OS installation, refer to here of [1] )
# display available OS template

root@dlp:~#
virt-builder -l

.....
.....
ubuntu-10.04             x86_64     Ubuntu 10.04 (Lucid)
ubuntu-12.04             x86_64     Ubuntu 12.04 (Precise)
ubuntu-14.04             x86_64     Ubuntu 14.04 (Trusty)
ubuntu-16.04             x86_64     Ubuntu 16.04 (Xenial)
ubuntu-18.04             x86_64     Ubuntu 18.04 (bionic)
ubuntu-20.04             x86_64     Ubuntu 20.04 (focal)
.....
.....

# create an image of CentOS Stream 9

root@dlp:~#
virt-builder centosstream-9 --format qcow2 --size 10G -o /var/kvm/images/centosstream-9.qcow2 --root-password password:myrootpassword

[   5.9] Downloading: http://builder.libguestfs.org/centosstream-9.xz
[ 142.3] Planning how to build this image
[ 142.3] Uncompressing
[ 147.3] Resizing (using virt-resize) to expand the disk to 10.0G
[ 185.2] Opening the new disk
[ 189.2] Setting a random seed
[ 189.2] Setting passwords
[ 190.5] Finishing off
                   Output file: /var/kvm/images/centosstream-9.qcow2
                   Output size: 10.0G
                 Output format: qcow2
            Total usable space: 9.4G
                    Free space: 8.3G (88%)

# to configure VM with the image above, run virt-install
root@dlp:~# virt-install \
--name centosstream-9 \
--ram 4096 \
--disk path=/var/kvm/images/centosstream-9.qcow2 \
--vcpus 2 \
--os-variant centos-stream9 \
--network bridge=br0 \
--graphics none \
--noautoconsole \
--boot hd \
--noreboot \
--import 

Starting install...
Creating domain...
Domain creation completed.
You can restart your domain by running:
  virsh --connect qemu:///system start centosstream-9
[3] [ls] a directory in a virtual machine.
root@dlp:~#
virt-ls -l -d ubuntu2204 /root

total 36
drwx------  5 0 0 4096 Apr 27 06:14 .
drwxr-xr-x 19 0 0 4096 Apr 27 04:51 ..
-rw-------  1 0 0  120 Apr 27 06:14 .bash_history
-rw-r--r--  1 0 0 3106 Oct 15  2021 .bashrc
drwx------  2 0 0 4096 Apr 27 06:11 .cache
-rw-r--r--  1 0 0  161 Jul  9  2019 .profile
drwx------  2 0 0 4096 Apr 27 05:01 .ssh
-rw-------  1 0 0  905 Apr 27 06:03 .viminfo
drwx------  4 0 0 4096 Apr 27 06:06 snap
[4] [cat] a file in a virtual machine.
root@dlp:~#
virt-cat -d ubuntu2204 /etc/passwd

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
.....
.....
[5] Edit a file in a virtual machine.
root@dlp:~#
virt-edit -d ubuntu2204 /etc/fstab


# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/ubuntu-vg/ubuntu-lv during curtin installation
/dev/disk/by-id/dm-uuid-LVM-8w2V0osTIFFadiaQY9ApdYmVWdgeYnSNBbmIT3BMb6fUdMHO2NL33
sqNuok9ztajH / ext4 defaults 0 1
# /boot was on /dev/vda2 during curtin installation
/dev/disk/by-uuid/904d6e39-6212-4600-8e94-303b8db302ed /boot ext4 defaults 0 1
[6] Display disk usage in a virtual machine.
root@dlp:~#
virt-df -d ubuntu2204

Filesystem                           1K-blocks       Used  Available  Use%
ubuntu2204:/dev/sda2                   1790136     133440    1547436    8%
ubuntu2204:/dev/ubuntu-vg/ubuntu-lv   10218772    6684024    2994076   66%
[7] Mount disk image of a virtual machine.
root@dlp:~#
guestmount -d ubuntu2204 -i /mnt

root@dlp:~#
ll /mnt

total 96
drwxr-xr-x  19 root root  4096 Apr 27 04:51 ./
drwxr-xr-x  19 root root  4096 Apr 27 02:35 ../
lrwxrwxrwx   1 root root     7 Apr 21 00:57 bin -> usr/bin/
drwxr-xr-x   4 root root  4096 Apr 27 06:13 boot/
drwxr-xr-x   4 root root  4096 Apr 21 01:01 dev/
drwxr-xr-x 145 root root 12288 Apr 27 06:13 etc/
drwxr-xr-x   3 root root  4096 Apr 27 05:01 home/
.....
.....
[8] Display the status of virtual machines.
root@dlp:~#
virt-top

virt-top 06:42:30 - x86_64 8/8CPU 2593MHz 16002MB
1 domains, 0 active, 0 running, 0 sleeping, 0 paused, 1 inactive D:0 O:0 X:0
CPU: 0.0%  Mem: 0 MB (0 MB by guests)

   ID S RDRQ WRRQ RXBY TXBY %CPU %MEM    TIME   NAME
    -                                           (ubuntu2204)
Matched Content