Virtualization
The typical virtualization stack on Linux is referred to as QEMU/KVM; both of these are separate technologies.
Tasks
Check CPU for virtualization support
-
grep -E 'svm|vmx' /proc/cpuinfo # (1)
- AMD CPUs will have svm in the flags section, whereas Intel CPUs will have vmx.
Virtual machine
-
The easiest way to create a VM is with the Boxes GNOME Desktop Environment application or virt-manager.
virt-install \ --cdrom=/tmp/debian-9.0.0-amd64-netinst.iso \ --vcpus=1 --memory=1024 --disk size=5 \ --os-variant=debian8 --name=linuxconfig-vm \
Virtual networking
- Using Boxes, a new interface will be created (apparently named tap0, etc) and slaved to a bridge, or a bridge will be created if none exist.
Console access
-
In order to enable console access to a domain, the serial-gettty@.service service must be started.
Now console access is available from the hostsystemctl enable --now serial-getty@ttyS0.service
virsh console $DOMAIN
Note that the console will remain at 24 lines by 80 columns no matter if the terminal window is resized.
Virtual disk
-
CoW filesystems
If the disk image is created on a filesystem that does not support O_DIRECT (ref.
man 5 open
), i.e. COW filesystem like btrfs and ZFS, the cache must be disabled. This appears to be impossible on btrfs, so disk images must be created on partitions with non-CoW alternatives, like ext4 and xfs.A virtual disk can be created in various ways and in various formats (the sparse QCOW2 disk image format, associated with QEMU, is preferred).
One way is to create the image using a utility like qemu-img.
Create qcow2 disk imageqemu-img create -f qcow2 disk0.qcow2 5G # (1)
- By omitting -f, qemu-img will create a RAW format file.
Alternatively, and more circuitously, a volume can be created within a storage pool.
virsh pool-define-as --name Disks --type dir --target /disk/Disks # (1) virsh pool-start Disks # (2) virsh vol-create-as Disks disk0.qcow2 10G --format qcow2
- A pool is deleted by first making sure its contents are deleted (but not the containing folder, in the case of a directory-based storage pool)
virsh pool-destroy Disks # Destroy the contents virsh pool-delete Disks # Delete directory virsh pool-undefine Disks # Delete resource
- Alternatively, activate pool on boot
virsh pool-autostart Disks
Regardless of the method, the disk image is then attached to the domain, whether or not it is running.
virsh attach-disk rhel \ --source /tmp/Disks/disk0.qcow2 \ --target vdb --cache none \ --driver qemu --subdriver qcow2 \ --persistent # (1)
- --persistent is necessary for the disk to remain attached after a shutdown.
If the domain is not running, --config is necessary.
This can be reversed with the following command.
virsh attach-disk rhel \ --source /tmp/Disks/disk0.qcow2 \ --target vdb --cache none \ --driver qemu --subdriver qcow2 \ --config
virsh detach-disk rhel /tmp/Disks/disk0.qcow2 virhs detach-disk rhel vdb # Specifying target instead
Snapshots
-
Snapshots of VMs can be taken from within Boxes or via the command-line:
virsh snapshot-create-as rhel --name "Disks added"
These produce XML records that can then be viewed: note that the field that Boxes uses to identify each snapshot is actually "description".
virsh snapshot-dumpxml rhel "Disks added"
Snapshots can be renamed by changing the value of the name element in a text editor.
virsh snapshot-edit rhel --snapshotname "Disks added" --rename
Revert to a snapshot
virsh snapshot-revert rhel --snapshotname
Custom resolution
-
Specify a custom resolution in a VM using KVM
cvt 2560 1440 xrandr --newmode "2560x1440_60.00" 312.25 2560 2752 3024 3488 1440 1443 1448 1493 -hsync +vsync xrandr --addmode Virtual-1 2560x1440_60.00 xrandr --output Virtual-1 --mode 2560x1440_60.0
On a Hyper-V VM, this method will not work, but a simple change to a line in /etc/default/grub will do the trick after running update-grub and restarting
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=hyperv_fb:1920x1080"
Commands
virt-install
virsh
-
virsh is the main interface for managing libvirt guest domains. In virsh terminology, the word domain refers to a VM.
virsh commands can be entered from the virsh shell or from the command-line.
virsh list --all virsh start rhel virsh shutdown rhel virsh destroy rhel # Forcefully virsh undefine rhel virsh autostart rhel # (1)
- Start domain at boot
Inspect a running domain
virsh dominfo $DOMAIN virsh domiflist $DOMAIN virsh domblklist $DOMAIN virsh domrename $DOMAIN $NEWNAME`
Virsh uses XML documents to define domain resources.
virsh dumpxml $DOMAIN virsh edit $DOMAIN
Several virsh commands come in two varieties, one to handle XML documents that define resources and another to define them inline from the command-line.
XML CLI Description pool-define pool-define-as Define a persistent storage pool vol-create vol-create-as Create a volume The commands ending in -as also support use of the --print-xml option, which will output the equivalent XML document to stdout.
Glossary
Boxes
- Boxes is a GNOME Desktop Environment application intended as a more user-friendly alternative to virt-manager to make virtualization easier for end-users.
domain
- In libvirt, a domain is a guest VM.
KVM
-
KVM is a FreeBSD and Linux kernel module that allows the kernel to function as a hypervisor.
KVM was first merged into kernel 2.6.20.
libvirt
- libvirt is an open-source API, daemon, and management tool for managing virtualization. It is a C toolkit to interact with the virtualization capabilities of the Linux KVM module, but it can also be used with KVM along with other virtualization technologies like QEMU.
QEMU
- QEMU is an open-source emulator that interoperates with KVM to run VMs at near-native speed when the guest architecture is the same as that of the host.
storage pool
- In libvirt, a storage pool is a file, directory, or storage device managed by libvirt to provide storage for domains.