Managing KVM Deployments with Virt-Manager
Before diving into creating virtual machines, it's important to take a look at the related tools and files. Virtual Machine Manager is actually a suite of tools for working with virtual machines:
virt-install: command-line tool used to install software into a VM image.
virt-clone: command-line tool used to clone a VM image.
virt-manager: GUI for starting, stopping and using VM images.
virt-image: command-line tool used to create a VM-based on an XML description.
The first three of these will be used to create, clone and use virtual machines. The latter is useful for advanced users but is beyond the scope of this article.
The virt-manager tools are Python programs that depend on the libvirt library to manage virtual machines and QEMU to run the virtual machines. Both libvirt and QEMU offer sophisticated features for a wide variety of tasks. Fortunately, you don't need to know anything about QEMU to get started using a VM, and you need to know only very basic information about libvirt.
The virt-manager application uses VNC to connect to remote libvirt dæmons and view remote virtual machines on a local display. This means you can launch virt-manager on your system and connect to a VM running on another system across the network. Such connections will require password authentication in most cases, depending on how libvirt is configured. This article takes the simple (and highly insecure) path of using root SSH access to access remote hosts. This works for my company, because the virtual machines are on an insulated networks. This also works if you're behind a well-configured firewall at home, but to be safe, consider further research into the secure (TLS) transport options of libvirt.
Note that virt-manager provides a wizard for creating new virtual machines on the localhost using local or remote media installations (Figure 1). However, this article focuses on the underlying tools virt-install and virt-clone. These tools offer far more power and flexibility when creating virtual machines.
With the software installed and the host network configured, it's time to create a base image. The base image is an installation of an operating system into a VM image file. These files can take multiple formats, but the recommended format is qcow2:
sudo virt-install --connect qemu:///system \ -n <vm-name> \ -r 512 \ --vcpus=1 \ -f ~/<vm-name>.qcow2 \ -s 12 \ -c <distro-install-image>.iso \ --vnc \ --noautoconsole \ --accelerate \ --os-type linux \ --os-variant generic26 \ --network=bridge:br0
Replace <vm-name> with a symbolic name for the virtual machine. At work, we use the distribution name, version and CPU size, such as “fedora11-64”. The <distro-install-image> is the name of the ISO image file used to install a Linux distribution.
The man page for virt-install covers the available options in detail. This particular command attaches to the local hypervisor as root (--connect) and sets up a guest virtual machine with 512MB of memory and the maximum number of CPUs it can use (-r, --vcpus). It will create a virtual machine image in the file ~/<vm-name>.qcow2 that is 12GB (-f, -s) and boot the installation media <distro-install-image>.iso. virt-install will start a VNC console on the guest and make it available for use via the host (--vnc), but no connection to it is started by default (--noautoconsole). A connection to it will be made later using virt-manager. The guest machine will run using kernel acceleration if available (--accelerate).
The guest will be created using optimized ACPI, APIC, mouse and other hardware configurations (--os-type, --os-variant) and use the bridged network device (--network). Note that the os_type has specific valid values. Check the man page for details.
This command will exit immediately while it starts up the VM in the background. To interact with the operating system installation, start the virt-manager application. This program will recognize VNC servers on the local network and list them (Figure 2). Double-clicking on one will open a connection to that host and list the available guest systems. Double-clicking on a guest will open a window to the guest (Figure 3).
With the VNC window open, the installation proceeds just as it would when installing on bare metal. Click inside the VM window to activate the guest VM mouse, then hold down Ctrl-Alt at the same time to return the mouse to the host desktop. In many cases, you won't need to capture the mouse pointer this way. Instead, just type with the host mouse pointer over the VM console window and keystrokes are passed to the guest VM.
Once the installation completes, a reboot typically is required. A reboot of the VM, that is—a reboot in this VM instance shuts down only the virtual machine, not the host. You must use the Run button in the menu bar of the VNC window to start the VM again manually. After rebooting, be sure to install any updates for the Linux distribution.