Tuesday, November 25, 2025

KVM : Management tools for VM

 

KVM : Management tools for VM


 

Install useful tools for virtual machine management.

[1]Install required packages.
root@dlp:~# 
apt -y install libguestfs-tools
[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

.....
.....
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)
debian-12                x86_64     Debian 12 (bookworm)
.....
.....

# create an image of Debian 12

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

[   7.4] Downloading: http://builder.libguestfs.org/debian-12.xz
[  82.7] Planning how to build this image
[  82.7] Uncompressing
[  85.2] Resizing (using virt-resize) to expand the disk to 10.0G
[ 100.4] Opening the new disk
[ 107.9] Setting a random seed
[ 107.9] Setting passwords
[ 108.5] SELinux relabelling
[ 108.6] Finishing off
                   Output file: /var/kvm/images/debian-12.qcow2
                   Output size: 10.0G
                 Output format: qcow2
            Total usable space: 9.8G
                    Free space: 8.6G (87%)

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

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

total 32
drwx------  4 0 0 4096 Aug 21 00:07 .
drwxr-xr-x 19 0 0 4096 Aug 20 23:53 ..
-rw-------  1 0 0   58 Aug 21 01:08 .bash_history
-rw-r--r--  1 0 0  607 May 12 19:25 .bashrc
drwx------  2 0 0 4096 Aug 20 23:56 .cache
-rw-------  1 0 0   20 Aug 21 00:07 .lesshst
-rw-r--r--  1 0 0  132 May 12 19:25 .profile
drwx------  2 0 0 4096 Aug 20 23:49 .ssh
[4][cat] a file in a virtual machine.
root@dlp:~# 
virt-cat -d debian13 /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
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
.....
.....
[5]Edit a file in a virtual machine.
root@dlp:~# 
virt-edit -d debian13 /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).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/debian--vg-root /               ext4    errors=remount-ro 0       1
# /boot was on /dev/vda1 during installation
UUID=7fd24747-f3ac-4c8e-9c37-92cd30cc4220 /boot           ext4    defaults        0       2
/dev/mapper/debian--vg-swap_1 none            swap    sw              0       0
/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto     0       0
[6]Display disk usage in a virtual machine.
root@dlp:~# 
virt-df -d debian13

Filesystem                           1K-blocks       Used  Available  Use%
debian13:/dev/sda1                      954840      93308     795692   10%
debian13:/dev/debian-vg/root          18421584    4445036   13015424   25%
[7]Mount disk image of a virtual machine.
root@dlp:~# 
guestmount -d debian13 -i /mnt

root@dlp:~# 
ll /mnt

total 84
lrwxrwxrwx   1 root root     7 Aug 21 08:49 bin -> usr/bin
drwxr-xr-x   4 root root  4096 Aug 21 08:55 boot
drwxr-xr-x   4 root root  4096 Aug 21 08:49 dev
drwxr-xr-x 125 root root 12288 Aug 21 09:59 etc
drwxr-xr-x   3 root root  4096 Aug 21 08:55 home
.....
.....

No comments:

Post a Comment