4 minutes
KVM setup on Ubuntu 16.04
I decided it was time to upgrade my dev systems to Ubuntu 16.04.1 LTS Xenial Xerus. This was actually predicated by an issue I had with my home dhcp/dns server. Specifically I pooched it, so I was going to start all over with a new vm. I started by moving the home network over to using the router as a dhcp server just to buy some time to complete this project. Once I did this, I realized nothing in my home network needed the server anymore. This seemed like a really good time to start all my servers over again, but on 16.04. So I did. The first server I would need to upgrade is my virtual machine host. This computer I went ahead and formatted the hard drive and put a base install of 16.04 on simply setting up ssh so I could get into it. Next I went ahead and started setting up the headless KVM environment.
Pre-Req’s
As I had previously used this machine for virtualization (and a home server), I have already setup bios the way I need it. Specifically you may have to turn on hardware virtualization on the processor. If you are running a processor that doesn’t support that, these instructions will not work.
As part of my server setup, I set this machine to be a static member of my network with hardcoded nameservers. This may not be as important to you, but if you remember I like to use a virtual dns/dhcp server - I don’t want the host to rely on the guest for the network. Here is a look at my /etc/network/interfaces file that I simply use nano to edit.
# The primary network interface
auto enp5s0
iface enp5s0 inet static
address 192.168.80.3
netmask 255.255.255.0
network 192.168.80.0
broadcast 192.168.80.255
gateway 192.168.80.1
dns-nameservers 151.197.0.37 198.6.1.3 71.242.0.12
Setup the Bridge network
To make any of this work, the host machine will need to have a bridge network to connect the network that the guests are using with the actual network the host is physically connected to. Therefore my first step is to setup a bridge network on the host.
Installing the bridge utility:
sudo apt-get install bridge-utils
Run ifconfig to validate network interface name - mine is enp5s0
ifconfig
Now I am going to nano /etc/network/interfaces again and edit it to the below. If you read through this, all I am doing is changing the primary interface over to manual and then moving all the settings over to a new interface called br0. Lastly I am adding a few bridge specific settings like which physical interface the bridge should use (in my case it is enp5s0).
# The primary network interface
auto enp5s0
iface enp5s0 inet manual
auto br0 inet static
iface br0 inet static
address 192.168.80.3
netmask 255.255.255.0
network 192.168.80.0
broadcast 192.168.80.255
gateway 192.168.80.1
dns-nameservers 151.197.0.37 198.6.1.3 71.242.0.12
bridge_ports enp5s0
bridge_stp off
bridge_maxwait 0
To actually get this working I now need to restart a number of services. I am sure there is a more nuanced way to do this, but I find it easiest to simply reboot.
sudo reboot now
Install KVM
My next step is to install kvm itself as well as the management tools I will be using.
sudo apt-get install qemu-kvm libvirt-bin virtinst ubuntu-vm-builder
This was a long install as there are a lot of dependencies I didn’t already have on this fresh server install. Once finished, I had to log out and log back in to my ssh session to be able to run virsh without sudo. This is because the installation process added my account to the libvertd group, but it isn’t effective until you relogin.
Validate Install
Firstly I just wanted to make sure it was running, so I ran a basic listing of virtual machines and successfully returned nothing. Woot!
$ virsh list
Id Name State
----------------------------------------------------
Next I wanted to spin up a guest just to see if everything was working. I downloaded an ubuntu iso into the default images folder and then spun up a test machine using Virtual Machine Manager connected in from a client machine.
wget http://releases.ubuntu.com/16.04/ubuntu-16.04.1-server-amd64.iso /var/lib/libvirt/images/
I then simply ran updates on the new guest machine just to make sure network connectivity was working as expected.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get autoremove
It all looked good at this point, so I went ahead and shutdown my test guest to start rebuilding my home dns/dhcp server.
Credits:
Narrow Escape has a great write up on Bridged Networking options for Ubuntu 16.04. nixCraft has a great write up on the entire process and has a number of useful references for using virt-install. Narrow Escape also has a great guide, but slightly aimed at a more gui based user. Ubunut KVM Installation this has a lot of useful info - definitely worth keeping this page up during your installation.