Professional Documents
Culture Documents
04
INSTALLATION AND CONFIGURATION TUTORIAL
This tutorial explains how to install and use KVM for creating and running virtual machines on an
Ubuntu 9.04 Server OS.
Preliminary Note
Throughout this tutorial is used a machine with hostname server.saulparada.com and IP address
158.196.53. 158 as KVM host.
Because almost all steps from this tutorial require root privileges, can be either precede all
commands in this tutorial with the string sudo, or become root right at the beginning by typing:
$ sudo -i
First is necessary to check if the CPU supports hardware virtualization - if this is the case, the
command:
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush
dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts pni dtes64 monitor
ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm ida tpr_shadow vnmi flexpriority
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush
dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts pni dtes64 monitor
ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm ida tpr_shadow vnmi flexpriority
If nothing is displayed, then the processor does not support hardware virtualization and the
virtualization process must stop here.
To install KVM and vmbuilder (a script to create Ubuntu-based virtual machines), must be used the
terminal instruction:
saulparada 1
General type of mail configuration: <-- Internet Site
System mail name: <-- server.saulparada.com
Afterwards must be added the user as which is currently logged in (root) to the group libvirtd:
It is necessary to log out and log back in for the new group membership to take effect.
To check if KVM has successfully been installed, can be performed the following terminal
instruction:
Next is necessary to set up a network bridge on the server so that the virtual machines can be
accessed from other hosts as if they were physical systems in the network.
To do this, is installed the package bridge-utils and configured a bridge (/etc/network/interfaces).
Edit /etc/network/interfaces:
# nano /etc/network/interfaces
saulparada 2
It should be edited and changed for something similar to:
auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off
Make sure it is used the correct settings for the network. Just was created a new iface called br0
that is configured just like eth0 was before (dhcp or static, etc). Must be change iface eth0 inet to
manual and append the bridge_xxx stuff to the br0 definition.
# /etc/init.d/networking restart
In order to see the new created interface, br0, and check the applied configurations can be used the
following instruction:
# ifconfig
root@ubuntu:~# ifconfig
br0 Link encap:Ethernet HWaddr 00:1b:24:b4:a0:9e
inet addr:158.196.53.158 Bcast:158.196.53.255 Mask:255.255.255.0
inet6 addr: fe80::21b:24ff:feb4:a09e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:14866 errors:0 dropped:0 overruns:0 frame:0
TX packets:8783 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5720077 (5.7 MB) TX bytes:1413432 (1.4 MB)
saulparada 3
RX packets:21661 errors:0 dropped:0 overruns:0 frame:0
TX packets:12525 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8932081 (8.9 MB) TX bytes:2198281 (2.1 MB)
Interrupt:17
Creating an Image-Based VM
Can now be created the first VM - an image-based VM.
It will be now create a new directory for each VM that is going to be created, for e.g. ~/vm1, ~/vm2,
~/vm3, and so on; because each VM will have a subdirectory called ubuntu-kvm, and obviously there
can be just one such directory in ~/vm1, for example.
It will be use the vmbuilder tool to create the VMs.
The following process to create an image-based VM is equal for each VM, only, apart from some
specific fields in vmbuilder options, must be changed the directory ~/vmx, according to the x pretended
VM.
vmbuilder uses a template to create virtual machines - this template is located in
/etc/vmbuilder/libvirt/ directory. First is created a copy:
# mkdir -p ~/vm1/mytemplates/libvirt
# cp /etc/vmbuilder/libvirt/* ~/vm1/mytemplates/libvirt/
Partitioning
Now must be partitioned the pretended VM. For that purpose is created a file called
vmbuilder.partition:
# nano ~/vm1/vmbuilder.partition
saulparada 4
The desired partitions can be defined as follows:
root 1500
swap 500
---
/var 1000
This defines a root partition (/) with a size of 1500MB, a swap partition of 500MB, and a /var
partition of 1000MB. The --- line makes that the following partition (/var in this example) is on a
separate disk image (i.e., this would create two disk images, one for root and swap and one for /var). Of
course, can be define whatever partitions are needed (as long as is also defined root and swap), and
certainly, they can be in just one disk image - this is just an example.
Must be installed openssh-server in the VM. To make sure that each VM gets a unique OpenSSH key,
cannot be installed openssh-server when is created the VM. Therefore is created a script called boot.sh
that will be executed when the VM is booted for the first time. It will install openssh-server with an
unique key:
# nano ~/vm1/boot.sh
# This script will run the first time the virtual machine boots
# It is ran as root.
# Install openssh-server
apt-get update
apt-get install -qqy --force-yes openssh-server
Build a VM
Whenever vmbuilder builds a new VM, it has to download all packages from an Ubuntu mirror which
can take quite some time. To speed this up, we install apt-proxy to cache the downloaded packages so
that subsequent VM installations will be a lot faster:
apt-proxy listens on port 9999, so can be passed the local apt-proxy "mirror" as an argument to
vmbuilder script.
Now the user, in order to learn about the available options, must take a look at:
saulparada 5
To create the first VM, vm1, it is necessary go to the respective VM directory:
# cd ~/vm1/
Most of the options are self-explanatory. --part specifies the file with the partitioning details,
relative to the working directory (that is why is necessary go to the respective VM directory before
running vmbuilder), --templates specifies the directory that holds the template file (again relative to the
working directory), and --firstboot specifies the firstboot script. --libvirt=qemu:///system tells KVM to
add this VM to the list of available virtual machines. --addpkg allows the user to specify the Ubuntu
packages that he wants to have installed during the VM creation. --bridge sets up a bridged network; as
were created the bridge br0, now is specified that bridge here.
In the --mirror line must be specified the local apt-proxy mirror
(http://158.196.53.158:9999/ubuntu) – In this case was used the publically accessible IP address instead
of localhost or 127.0.0.1 because this mirror will be used in the VM's /etc/apt/sources.list file as well,
and of course, the VM will not be able to connect to 127.0.0.1 on the host. Of course, can be as well
specified an official Ubuntu repository in --mirror, for example http://cz.archive.ubuntu.com/ubuntu. If
the field --mirror is leave out, then the default Ubuntu repository (http://archive.ubuntu.com/ubuntu)
will be used.
The build process can take a few minutes.
# ls -l /etc/libvirt/qemu/
root@ubuntu:~# ls -l /etc/libvirt/qemu/
total 12
drwxr-xr-x 3 root root 4096 2009-06-13 15:55 networks
-rw------- 1 root root 1028 2009-06-13 17:25 vm1.xml
The disk images are located in the ubuntu-kvm/ subdirectory of the VM directory:
# ls -l ~/vm1/ubuntu-kvm/
root@ubuntu:~# ls -l ~/vm1/ubuntu-kvm/
total 377972
-rw-r--r-- 1 saulparada saulparada 226938880 2009-06-13 17:24 disk0.qcow2
-rw-r--r-- 1 saulparada saulparada 159711232 2009-06-13 17:25 disk1.qcow2
saulparada 6
Managing a VM
VMs can be managed through virsh, the "virtual shell". To connect to the virtual shell, run:
virsh #
# list
list --all
Before start a new VM for the first time, must be defined it from its xml file (located in the
/etc/libvirt/qemu/ directory) as follows:
# define /etc/libvirt/qemu/vm1.xml
Please note that whenever is modified the VM's xml file in /etc/libvirt/qemu/, must be ran the
define command again.
# start vm1
After a few moments, should be able to connect the VM with an SSH client such as PuTTY; log in
with the default username (saulparada) and password (smsp).
saulparada 7
Checking the running VMs
Commands
# shutdown vm1
# destroy vm1
Suspend a VM:
# suspend vm1
Resume a VM:
# resume vm1
# quit
Sources:
http://www.howtoforge.com/virtualization-with-kvm-on-ubuntu-9.04
http://ubuntuforums.org/showthread.php?t=1163175
http://samiux.wordpress.com/2008/07/29/kmv-kernel-based-virtual-machine-on-ubuntu-
8041-desktop/
saulparada 8