Using QEMU/KVM
Overview
QEMU + KVM is a popular virtualization option for Linux distrobutions which has the promise of better hardware optimization and performance than other platforms such as VirtualBox. It also gives a better platform for doing direct PCI passthrough of physical devices attached to the Host OS.
Creating Virtual Machines
Creating a virtual machine is done through virt install by passing a list of desired options.
--name take the desired logical name for this machine
--description is a description
--ram is the amount of RAM to allocate to the VM
--vcpus is the number of VCPUs you want to allocate to the VM
--os-variant tells the hypervisor what kind of underlying OS the machine might be running. This allows the QEMU/KVM system to provide certain unique tweaks that would optimize the performance of the guest OS
--disk path is the path to each disk you wish to attach to the VM. Providing the path and file name, KVM will create the disks as part of the virt-install procedure
--graphics is the desired graphics module
--cdrom takes a path to the installation ISO
--network tells KVM to use the desired network for the guest
--boot uefi tells KVM to boot the VM in UEFI mode
Example total configuration:
virt-install \
--name huginn \
--description "Pirate Platform" \
--ram=8192 \
--vcpus=4 \
--os-variant=archlinux \
--disk path=/srv/virtual/kvm/vm-drives/huginn.qcow2,bus=virtio,size=50 \
--disk path=/opt/storage/VM-HDD/kvm_machines/huginn-data.qcow2,bus=virtio,size=250 \
--graphics spice \
--cdrom=/opt/storage/ISO/mabox-linux-23.01-Istredd-230126-linux54.iso \
--network network=bridged-network \
--boot uefiRunning Virtual Machines
After installation is complete, there are various commands for managing the state of the VM.
virsh start <vm name> will start the VM
virsh destroy <vm name> will hard stop the VM
virsh list --all will show all VMs registered to the system
virsh list will show all running VMs
Removing Virtual Machines
When removing a VM, you will need to both ensure the machine is stopped as well as undefine it. Using the following script will make it easier:
Example:
#!/bin/bash
virsh destroy huginn
virsh undefine --nvram huginn --remove-all-storage
virsh pool-refresh vm-drivesThis will stop the VM, undefine the VM and remove all storage, and then refresh the storage pool.
Virtual Machine Snapshots
It may be desirable to take snapshots, or use snapshots of virtual machines from time to time. KVM provides this functionality.
Creating a Snapshot
To create a snapshot, you can run this command:
virsh snapshot-create-as --domain <vm name> --name "<snapshot name>"You can then confirm the snapshot was created with:
virsh snapshot-list --domain <vm name>Reverting to Snapshot
If you need to revert to a snapshot, run this command:
virsh shutdown --domain <vm name>
virsh snapshot-revert --domain <vm name> --snapshotname <name of snapshot>If the snapshot was taken in a running state, you can add --running to end of the above command.
Removing Snapshot
If you wish to delete a snapshot, you can run:
virsh snapshot-delete --domain <vm name> --snapshotname <name of snapshot>Further Reading
There is more complicated things that can be done with snapshots, for further reading Check out This Article