kvm HOWTO

kvm_howto.sdf : 0.1 (DRAFT)
Richard W. Brown
3 July 2008

There should be a blue line here

Table of Contents

There should be a blue line here

1. Introduction

KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko.

1.1. Copyright

Copyright (c) 2008 by Richard W. Brown

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is available from http://www.fsf.org/licenses/fdl.html.

1.2. Disclaimer

No liability for the contents of this document can be accepted. Use the concepts, examples and information at your own risk. There may be errors and inaccuracies which could be damaging to your system. Proceed with caution, and although this is highly unlikely, the author(s) do not take any responsibility.

Owners hold all copyrights, unless specifically noted otherwise. Use of a term in this document should not be regarded as affecting the validity of any trademark or service mark. Naming of particular products or brands should not be seen as endorsements.

This document is how I configure and use the various software tools related to my own system. I am not, nor do I pretend to be a Linux expert. I am just some guy who has benefited from the expertise of others and wish to add my contribution back to the Linux community.


Note: You are strongly recommended to take backup copies of all files before they are modified.

1.3. Feedback.

Feedback is most certainly welcome for this document. Send your additions, comments and criticisms to the following email address: howtos at Mythical Beast dot co dot uk Errors and omission will be added to the next version of this document.

There should be a blue line here

2. Installation.

2.1. Install Using The 'apt-get' Software

Install command
sudo apt-get install kvm libvirt-bin virt-manager virt-viewer ubuntu-vm-builder 


Note: You need to install ubuntu-vm-builder as it includes "something" that virt-install uses. Virt-install will not run correctly without it.

2.2. So your user can use the tools

Then, add yourself to the libvirtd and kvm groups. This mans that your own user can create and run kvm guests and the guests are reasonable secure as the processes are run by a user with few permissions. You can also take advantage of the system networking and not have to use the really slow and CPU intensive userbase networking.

The id -un command will print the users identity, useful command when writing HOWTOs. Try it out on its own.

Code:
sudo adduser `id -un` libvirtd 
sudo adduser `id -un` kvm 


Note: You need to log out and log back in for the new group membership to take effect.

You can test if your install has been successful with the following command:

Code:
virsh -c qemu:///system list 
 Id Name                 State 
---------------------------------- 

If you get an error message then you still have a problem. You did logout and back in again didn't you?

Code:
virsh -c qemu:///system list 
libvir: Remote error : Permission denied 
error: failed to connect to the hypervisor 

The usual problem is that you do not have write access to /var/run/libvirt/libvirt-sock. The sock file should have permissions similar to

Code:
ls -l 
srwxrwx--- 1 root libvirtd 0 2008-05-11 12:08 /var/run/libvirt/libvirt-sock= 

And you should see the libvirtd when you run the groups command as your user. If both of these are correct and you reinstalled kvm after trying this before there can also be a problem with ssh known-hosts keys. Move the known-hosts file to something new and create a blank one then try again. If it was a problem with known-host keys you should see get a question. Answer "yes" and press return.

2.3. What IP address will the virtual manager being running with.

To open a console of graphical view of the virtual machines you need to know the IP address of where the master can be connect to. If you look in the file /etc/libvirt/qemu/networks/default.xml you will see that there is a xml tag <ip address= you need to remember the IP address.

Code:
<ip address="192.168.122.1" netmask="255.255.255.0"> 

2.4. Configuring the network.

We are going to be using a virtual bridge to access the network that the kvm guests will be using. This is faster than using the usermode networking that is default. Your guests will also have a full range of access to the local network and to the internet.

2.4.1. Make backup copies of configuration files.

Start by making a copy of the files we will be changing so you can always go back to a know starting point.

Copy original configuration files:
cd /etc 
cp network/interfaces  ~/mybackups 

2.4.2. Setup /etc/network/interfaces

The example file below was taken from a machine that uses a static IP address and the network card is eth0. Before we do anything the file /etc/network/interfaces looked like this. (I missed off the comments at the top). Also before going on have a look at the output of the command ifconfig. If you have installed the kvm packages you should see output for three devices, eth0, lo and vnet0.

Example /etc/network/interfaces (before):
# The loopback network interface 
auto lo 
iface lo inet loopback 
 
# The primary network interface 
auto eth0 
#iface eth0 inet dhcp 
 
iface eth0 inet static 
   address 192.168.0.7 
   netmask 255.255.255.0 
   gateway 192.168.0.1 

After the changes it will look like this:

Example /etc/network/interfaces (after):
auto lo 
iface lo inet loopback 
 
auto br0 
iface br0 inet static 
   address 192.168.0.7 
   network 192.168.0.0 
   netmask 255.255.255.0 
   broadcast 192.168.0.255 
   gateway 192.168.0.1 
   bridge_ports eth0 
   bridge_fd 9 
   bridge_hello 2 
   bridge_maxage 12 
   bridge_stp off 
 
iface eth0 inet static 
   address 192.168.0.0 
   netmask 255.255.255.0 

The lo device is required and is not changed. The virtual bridge will effectively become the network interface replacing eth0 for the host. That is why the old settings for eth0 are now in br0 with some extra lines for setting a bridge. The lines that generally cause the confusion come with the new settings for eth0. The address is wrong, I hear you say. Actually you can put just about anything in there as it will get over written when the bridge is created. You have to have "something" or you get an error.

Once you have made the changes restart networking. If you have a local NTP server running you will see that gets restarted too. The terminal may also scroll very fast and give you lots of prompts. It will finally return to normal. Well mine did.

Code:
sudo /etc/init.d/networking restart 

Again have a look at the new output from ifconfig. You will now see that you have an extra device called br0, your virtual bridge. It should have the ip address that was originally on eth0. (192.168.0.7) The other change is that the line for inet under eth0 is no longer there. It has been over written as expected. At this point you should still be able to do everything as before. Connect to your local hosts for example ping them. Connections to the outside or the internet are also working as expected. In other words as far as the host is concerned its networking functionality has not changed.

There should be a blue line here

3. Creating Virtual machines.

3.1. Creating a Virtual Ubuntu Hardy machine

Make sure you have access to the installation iso file and enough free disk space for a 5 gig hard drive file.

Code:
sudo virt-install -n kvmubuntu -r 512 -f /data/Virtual_machines/ubuntu/hardy1.img \ 
    -s 5  -c /data/ubuntu_iso/ubuntu-8.04-alternate-i386.iso --accelerate --vnc    \ 
    --noautoconsole --os-type=linux 

The above command GUI create a new virtual machine.

Run the above command and follow your usual installation process. Once the install process has finished it is worth making a copy of the kvmubuntu.xml file that was generated incase you want to move the virtual machine somewhere else. I keep my copy in with the disk file. If you do move it simple copy the file back to the directory /etc/libvirt/qemu/ on the new machine and restart libvirt-bin.

Code:
sudo cp  /etc/libvirt/qemu/kvmubuntu.xml /data/Virtual_machines/ubuntu 

Now the virtual machine has been created, but it has not yet had an OS installed to it although the install program is running you cannot see or interact with it yet.

Code:
virt-viewer -c qemu+ssh://192.168.122.1/system kvmubuntu 

There is also a graphical interface virt-manager when it is installed it adds an icon to the menu, Applications->System Tools->Virtual Machine Manager.

3.2. Creating machines using raw disk space.

Just pass virt-install a -f parameter with a LVM logical volume or

There should be a blue line here

4. Making Copies or Cloning Existing Machines.

This is simplicity itself . You should already have the little utility script called virt-clone. We already have a kvm machine called kvmubuntu which we will now clone. Calling the new machine, kvmhardy and storing it in /data/Virtual_machines/kvmhardy. The machine you are cloning must be closed down.

Clone command line
cd /data/Virtual_machines/kvmhardy 
sudo virt-clone -o kvmubuntu -n kvmhardy -f /data/Virtual_machines/kvmhardy/kvmhardy.img 
Cloning domain...         100% |=========================| 5.0 GB    01:31 


Note: Make sure you give the full path to the new image!

Startup the Virtual Machine Manager and it will listed there for you. Boot the new machine. Now to change a couple of things that are easily forgotten and will prevent some weird stuff from happening. Don't forget to rename the new guest machine or your network may get a little strange and change the line in the /etc/hosts file to be the correct name as well. Then reboot.

Debian based systems use the file /etc/hostname to read the hostname of the system at boot time and set it up using the init script /etc/init.d/hostname.sh. So load up the file and change the home name to whatever you want it to be.

Change the hostname
sudo gedit /etc/hostname /etc/hosts 
kvmhardy                         # <<---- /etc/hostname 
 
127.0.1.1    kvmhardy            # <<---- /etc/hosts 
 
/etc/init.d/hostname.sh start 


Note: That the command to start and restart, i.e. change the hostname uses the subcommand start and not restart as there is no need to stop it first.

If you have also followed my HOWTO for dnsmasq then you also need to change the hostname the sendto line of /etc/dhcp3/dhclient.conf

Change the hostname
send host-name "kvmhardy.example.com"; 

The line will tell your dhcp server the name of your new kvmguest. Now when you reboot your kvmguest will get an IP address from the DHCP while registering its hostname so all your networked machines can use the name of the new kvmguest.

Told you it was easy!

There should be a blue line here

5. Stopping And Starting The libvirt-bin Daemon.

This is really simple just run the libvirt-bin startup script in /etc/init.d with stop, start or restart. That was simple

Code:
sudo /etc/init.d/libvirt-bin stop 
sudo /etc/init.d/libvirt-bin start 
sudo /etc/init.d/libvirt-bin restart 

There should be a blue line here

6. Further Information.

6.1. Links I Found Useful