Install and Remove KVM Guests With virt-install

virt-install is a command-line tool used to provision new virtual machines (VMs) using the libvirt hypervisor management library. It supports creating KVM, Xen, or Linux container guests and can configure various aspects such as virtual disks, network interfaces, audio devices, and physical USB or PCI devices. The installation media can be held locally or remotely on NFS, HTTP, or FTP servers.

virt-install can run completely unattended, allowing for easy automation of guest installs, and also supports graphical installations using VNC or SDL graphics, as well as text mode installs over serial console.

Install a guest with using virt-viewer and spice

To install virtual machines with virt-install and connect to a spice console with virt-viewer you have to run a similar command to the followings:

virt-install --virt-type kvm \
--name debian-virtinstall \
--location https://deb.debian.org/debian/dists/bookworm/main/installer-amd64/ \
--os-variant debian11 \
--network bridge=bridge0 \
--graphics spice \
--disk path=/home/tmolnar/Documents/kvm-vms/debian-virtinstall.qcow2,size=40 \
--memory 4096 \
--vcpus 6 \
--metadata title='My New Debian VM',description='This is a test install'

It will create a new virtual machine using a HTTPS location for the installation media. The disk path is on a custom image pool. The network uses the host bridge adapter for a bridged network. The spice graphics help us to reach the console with virt-viewer.

Install with using a serial console

If you want to install your virtual machine on a server, or simply without GUI then you can connect to the serial console and do it in the command line with the following command options of the virt-install:

--graphics none \
--console pty,target_type=serial \
--extra-args "console=ttyS0"

Delete a guest with its storage

Removing a virtual machine can happen if the virtual machine is shut off.

The --remove-all-storage option will erase the virtual disk image too.

The virsh destroy forces to shut down the machine.

virsh destroy --domain debian-virtinstall

The virsh undefine removes the guest.

virsh undefine --remove-all-storage debian-virtinstall

Be careful with the --remove-all-storage option, as it deletes the storage of the virtual machine!

If you want to discuss the topic with other technology-minded people, join my Discord: https://discord.gg/YbSYGsQYES

Now we have an IRC channel as well: irc.libera.chat / #tomsitcafe

Leave a comment