Thursday, July 17, 2025

Debian 12: KVM Virtualization - creating guest (part 3)

To show available os for guest

$ virt-install --osinfo list | grep arch
archlinux

To create guest with name=guest01, disk size 10GB, Ram 2Gb (2048), virtual processor 2, os variant debian 12 (not available, we use debian11), boot from iso file: 

$ virt-install \
  --name guest01 \
  --memory 2048 \
  --vcpus 2 \
  --disk path=/home/dedetok/guests/guest01.qcoe2,size=10 \
  --cdrom /home/dedetok/Downloads/debian-12.11.0-amd64-netinst.iso \
  --nonetworks \
  --os-variant debian11 \
  --virt-type kvm

Parameters:

  • --name: name to identify guest
  • --ram: guest memory in megabytes
  • --vcpus: number of cpu for guest
  • --disk: path=<path_to_disk_image> , size=<disk_size_in_gb>
  • --cdrom: install from iso file or CD/DVD/USB
  • --nonetworks: no update or install from internet 
    or
    --network bridge=br0 to use bridge network, see part 2

Options:

  1. Graphics option
    • --graphics vnc: Enables VNC for graphical access. If virt-viewer is installed, it will automatically launch. If not, you'll need to manually connect using a VNC client like vinagre or remmina.
    • --graphics spice: Enables SPICE for graphical access. SPICE is generally considered more modern and efficient than VNC.
    • --graphics none: Disables graphical access and forces a text-mode installation using the serial console.
  2. Disk option
    • Default folder for virtual disk /var/lib/libvirt/images/
      qcow2 offers features that raw (or img) doesn't:
      1. Snapshots: qcow2 allows you to create snapshots of your virtual machine's disk, enabling easy rollback to previous states.
      2. Compression: It can compress the disk image, potentially saving storage space.
      3. Sparse files: qcow2 supports sparse files, meaning it only allocates disk space for used portions of the image, which can be more efficient.
    • raw format (or just img when using virt-install) has no special features:
      It simply represents the raw data of the disk, which can be less flexible and potentially wasteful of disk space.

list guest
$ virsh list --all

To start vm
$ virsh start guest01

to restart vm
$ virsh reboot guest01 --mode initctl

to force stoping vm
$ virsh destroy guest01

to force shutdown vm
$ virsh shutdown guest01 --mode acpi

to suspend vm
$ virsh suspend guest01

to resume vm after suspend
$ virsh resume guest01

to reset vm (similiar to pressing reset button on physical PC)
$ virsh reset guest01

Restarting KVM Daemon
# systemctl restart libvirtd

To show version
$ virsh version
Compiled against library: libvirt 9.0.0
Using library: libvirt 9.0.0
Using API: QEMU 9.0.0
Running hypervisor: QEMU 7.2.17

to remove vm and its storage permanently
$ virsh undefine --managed-save --remove-all-storage guest01

To make vm auto run after host restart (run once)
$ virsh autostart guest01

References:

  • wiki.debian.org/KVM
  • wiki.debian.org/DebianInstaller/Preseed