Tuesday, November 25, 2025

KVM : Create Virtual Machine

 

KVM : Create Virtual Machine


 

Install GuestOS and Create a Virtual Machine.
This example shows to create VM with installing Debian 13.

[1]On this example, Download an ISO file of Debian 13 on a directory first and Install GuestOS from the ISO on the text mode.
It's OK on the console or remote connection via SSH and so on.
Furthermore, Virtual Machine's images are placed at [/var/lib/libvirt/images] by default as a Storage Pool, however this example shows to create and use a new Storage Pool. (OK to use any place you like)
# create a Storage Pool directory

root@dlp:~# 
mkdir -p /var/kvm/images

root@dlp:~# virt-install \
--name debian13 \
--ram 4096 \
--disk path=/var/kvm/images/debian13.img,size=20 \
--vcpus 2 \
--os-variant debian13 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location /home/debian-13.0.0-amd64-DVD-1.iso \
--extra-args 'console=ttyS0,115200n8 serial' 

# installation starts
# after this, installation proceeds with the common procedure
Starting install...
Retrieving file vmlinuz...                                  | 6.5 MB  00:00
Retrieving file initrd.gz...                                |  17 MB  00:00
Allocating 'debian13.img'                                   |  20 GB  00:00
.....
.....

# once the installation is complete, return to the host and shut down the guest

root@dlp:~# 
virsh shutdown debian13
# mount the guest area and enable the service

root@dlp:~# 
guestmount -d debian13 -i /mnt

root@dlp:~# 
ln -s /mnt/lib/systemd/system/getty@.service /mnt/etc/systemd/system/getty.target.wants/getty@ttyS0.service

root@dlp:~# 
umount /mnt
# restart the VM and if you can connect to the console, the installation is complete

root@dlp:~# 
virsh start debian13 --console

Debian GNU/Linux 13 debian ttyS0

debian login:
The example of options above means like follows. There are many options for others, make sure with [man virt-install].
--namespecify the name of Virtual Machine
--ramspecify the amount of memory of Virtual Machine
--disk path=xxx,size=xxx[path=xxx] : specify the location of disks of Virtual Machine (default is [/var/lib/libvirt/images])
[size=xxx] : specify the amount of disk of Virtual Machine
--vcpusspecify the virtual CPUs
--os-variantspecify the kind of GuestOS
possible to show the list of available OS with the command below
[# osinfo-query os]
--networkspecify network type of Virtual Machine
--graphicsspecify the kind of graphics
possible to specify spice, vnc, none and so on
--consolespecify the console type
--locationspecify the location of installation source where from
--extra-argsspecify parameters that are set in Kernel

[2]For installing on text mode, it's the same with common installation procedure.
After finishing installation, restart computer and then login prompt is shown like follows.
Debian GNU/Linux 13 debian ttyS0

debian login: root
Password:
Linux debian 6.12.38+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.38-1 (2025-07-16) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
root@debian:~#
[3]Move to GuestOS to HostOS with Ctrl + ] key.
Move to HostOS to GuestOS with a command [virsh console (name of virtual machine)].
root@debian:~#     # Ctrl + ] key
root@dlp:~#        # Host's console

root@dlp:~# virsh console debian13    # switch to Guest's console
Connected to domain 'debian13'
Escape character is ^]     # Enter key

root@debian:~#     # Guest's console
[4]It's easy to replicate a copy VM from current VM with a command below.
root@dlp:~# 
virt-clone --original debian13 --name template --file /var/kvm/images/template.img

Allocating 'template.img'                    |  20 GB  00:00:05

Clone 'template' created successfully.

# disk image

root@dlp:~# 
ll /var/kvm/images/template.img

-rw------- 1 root root 5321195520 Aug 21 09:24 /var/kvm/images/template.img
# configuration file

root@dlp:~# 
ll /etc/libvirt/qemu/template.xml

-rw------- 1 root root 6730 Aug 21 09:24 /etc/libvirt/qemu/template.xml

No comments:

Post a Comment