In part 3 of this series about installing KVM on Ubuntu 14.04 I installed and configured KVM. In this part I’ll be installing a guest.
Installing a Guest in KVM
Important: These commands should be run under the account that will own the virtual machine. If you run them as root (e.g. after “sudo su -“) you’ll have problems. In particular you’ll probably find that you can’t start or shell into the guest.
At it’s simplest installing a guest is jsut a matter of running the following command:
sudo ubuntu-vm-builder kvm trusty
The vm builder will download the required image and install it for you. I was slightly disappointed to find that this simple command actually failed on my system, I’m not sure why it failed but I believe it was something to do with installing the kernel. I suspect it selected i386 architecture for the VM and an amd64 kernel or something like that. Either way it wasn’t really an issue as wanted to do a bit more configuration before installation so this is the command I ran:
sudo ubuntu-vm-builder kvm trusty \ --destdir demo2 \ --rootsize 10000 \ --domain wobblycogs.co.uk \ --user username --name user --pass password \ --addpkg acpid --addpkg openssh-server --addpkg linux-image-generic \ --mirror http://gb.archive.ubuntu.com/ubuntu/ --components main,universe,restricted \ --arch amd64 --hostname demo2 \ --libvirt qemu:///system --bridge br0 \ --mem 1024 --cpus 1 ;
Everywhere you look about installing a VM will tell you there are tons of argument for ubuntu-vm-builder (or just vmbuilder if you are going for the plain version). What no one tells you is now to get a list of those options. If you just run “ubuntu-vm-builder –help” you get some options but not all. To get the full list you need to specify the hypervisor and suite as well like this:
ubuntu-vm-builder kvm trusty --help
Anyway, the installation command will take a while to run as it has to download the installation image so here’s a run down of the options I’ve used while you are waiting, I’ve tried to organise them into sensible groups per line.
- destdir – just specifies where you want the image to be created.
- rootsize – specifies the size of the root disk. The default is a bit small at 4GB
- domain – gives the domain that the new guest will be in
- user, name and pass – give the username, real name and password of the first account on the guest. Obviously you’ll want to change these.
- addpkg – adds packages to the new guest machine. You’ll certainly want to install SSH or the machine will be difficult to access. The acpid package allows the host to power off the machine and the linux-image-generic package is to stop the error “This kernel does not support a non-PAE CPU”.
- mirror, components – tell the guest where to download packages from (can speed up the install) and which components to set up in apt.
- arch, hostname – the architecture of the machine to install. In theory you could use i386 for some of the older versions of Ubuntu but I don’t know why you would. Hostname just sets the host name.
- libvirt, bridge – the libvirt option tells the installer which dom0 or host to install the machine into. Presumably you can specify a machine other than the one the command is running on (how very cloud). The bridge option tells the install which bridge to place the network adaptor into.
- mem, cpus – how much memory and how many cpus to give the guest.
Notice that I didn’t specify any networking options other than to indicate which bridge I wanted the interface placed in. As I mentioned back in part 2 the configuration will all be handled by DHCP but this does mean I have a small additional step once the installation is complete. The problem, you see, is that the MAC address is assigned randomly during the install of the guest before I start the guest I need to look up the MAC address and make an entry in the DHCP server. That way when the guest starts I’ll know what IP address it’s been assigned. If you DHCP is good enough to do MAC to IP lookups that’s also an option.
Once the install process is complete check that your machine is at least recognized by KVM by starting a virsh session and entering (ignore demo1 it was another machine I was playing with):
virsh # list --all Id Name State ---------------------------------------------------- - demo1 shut off - demo2 shut off
When a machine is first installed it is left in the shut off state. Now open the configuration file for the new guest and note the MAC address:
virsh # edit demo2 ...other settings... <interface type='bridge'> <mac address='52:54:00:2e:d1:bf'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> ...other settings...
Make an entry in your DHCP server for this MAC address and then start the machine with:
virsh # start demo2 Domain demo2 started
If all has gone to plan you should now be able to SSH into the new guest using the username and password specified in the install script.
While this leaves you with a functional virtual machine there are some nice to have additional bits of configuration still to do. In particular I wan’t to add a console that can be accessed though virsh in an emergency (such as when you screwed up the network settings) and I want to make the machine start at boot time, that’ll be coming up in part 5.
Creating and Running VMs as root
Running VM’s as root is probably a bad idea but these notes should help. Essentially the problem I ran into when I tried it was getting permission denied on the folder containing the image. The virtual machine wouldn’t start and gave this error message:
# virsh start demo1 error: Failed to start domain demo1 error: internal error: process exited while connecting to monitor: qemu-system-x86_64: -drive file=/root/demo1/tmph1ucF7.qcow2,if=none,id=drive-ide0-0-0,format=qcow2: could not open disk image /root/demo1/tmph1ucF7.qcow2: Could not open '/root/demo1/tmph1ucF7.qcow2': Permission denied
The quick solution was to edit the /etc/libvirt/qemu.conf file and uncomment the lines although I don’t currently know what the security implications of this change are:
user = "root" group = "root"
Now restart libvirtd:
Rerun the start command virsh start demo1 and you should now have a working virtual machine.
- KVM – Main KVM site. Useful for lower level information but most of your interaction with the system will be through libvirt.
- libvirt – If you are going to be doing Linux based virtualization you pretty much have to know how to drive libvrit. Fortunately it has shockingly good documentation.
- VMBuilder – The scripts that actually do the VM install.