Booting a KVM VM from a Block Device

For MAAS development, I need to maintain separate development environments for both Ubuntu 14.04 (Trusty) and Ubuntu 16.04 (Xenial), the two LTS releases we support MAAS on. When Xenial development opened, I needed to have my old Trusty environment available (just in case), while at the same time moving my system to Xenial.

To get the job done, I decided to add a second SSD to my system to use for my primary environment, and boot my old system inside a virtual machine in case I needed it again. This post explains how I got it done.


Everything from here on was done on an Ubuntu 16.04 (Xenial) Desktop

First you'll need to install a few software packages to create and manage your virtual machines (if you haven't got these installed already):

sudo apt-get install libvirt-bin qemu-kvm virtinst virt-manager

If you haven't installed any of these packages yet, you may need to log out and log back in so that the system recognizes your permissions to create and manage virtual machines.

The libvirt-bin package contains the core libraries needed to support virtual machines. The qemu-kvm package allows for kernel virtual machines, which perform much better than a VM running in userspace. The virtinst package contains the virt-install utility, which will be used to create the configuration file for your new VM. The virt-manager package contains a nice graphical tool you can use to manage your virtual machines.

If you're just installing libvirt-bin for the first time, you'll need to log out and log back in, so that you will have permission to create a virtual machine.


After you have installed the dependencies, you'll need to create a barebones configuration file for your virutal machine:

virt-install --print-xml --name trusty --boot hd --nodisks --memory 4096 > template.xml

You can adjust the --memory and --name parameters as you see fit.

Next, find which block device you want the virtual machine to boot from. (I used ls -l /dev/disk/by-id to find the path, and used the one with the name /dev/disk/by-id/wwn-<wwn>, so that if I add another disk, the virtual machine configuration won't need to change.) Ubuntu comes with a graphical Disks utility, which you can use if you need help finding the correct disk.

To create the VM, use the virsh tool to define it using the configuration file you created in the first step. This will make it available to libvirt (and virt-manager). When you use the attach-disk command, be sure to use the same name you specified in --name above, which will become the name for your new VM. For example:

$ virsh
virsh # define template.xml
virsh # attach-disk trusty /dev/disk/by-id/wwn-0x5002537733492c30 vda --config

You can then use virt-manager to boot the virtual machine!


If you were running Ubuntu Desktop before, the hypervisor's display might become corrupt when the system boots, leading to an unusable system. In order to fix this, after you boot the VM, press the shift key to enter the grub menu. Edit the kernel command line to remove quiet splash.

When the machine boots, you can remove the splash screen as follows:

sudo apt-get remove plymouth-theme-ubuntu-logo plymouth-theme-ubuntu-text

Also, you can edit /etc/default/grub and remove quiet splash from GRUB_CMDLINE_LINUX_DEFAULT, then run sudo update-grub.

You should now have a usable virtual machine that can boot directly from your old disk!