Anda di halaman 1dari 8

VIRTUALIZATION WITH KVM ON UBUNTU 9.

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

Installing KVM and vmbuilder


Check virtualization support

First is necessary to check if the CPU supports hardware virtualization - if this is the case, the
command:

# egrep '(vmx|svm)' --color=always /proc/cpuinfo

...should display something, for example, like this:

root@ubuntu:~# egrep '(vmx|svm)' --color=always /proc/cpuinfo

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.

Install KVM and vmbuilder

To install KVM and vmbuilder (a script to create Ubuntu-based virtual machines), must be used the
terminal instruction:

# aptitude install ubuntu-virt-server python-vm-builder

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:

# adduser 'whoami' 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:

# virsh -c qemu:///system list

It should display something like this:

root@ubuntu:~# virsh -c qemu:///system list


Connecting to uri: qemu:///system
Id Name State
----------------------------------

If it displays an error instead, then something went wrong.

Setup a network bridge

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).

# aptitude install bridge-utils

Edit /etc/network/interfaces:

# nano /etc/network/interfaces

Before the modification, the file looks similar as the following:

# The loopback network interface


auto lo
iface lo inet loopback

# The primary network interface


auto eth0
iface eth0 inet dhcp

saulparada 2
It should be edited and changed for something similar to:

# This file describes the network interfaces available on your system


# and how to activate them. For more information, see interfaces(5).

# The loopback network interface


auto lo
iface lo inet loopback

# The primary network interface


auto eth0
iface eth0 inet manual

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.

Restarting the network:

# /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)

eth0 Link encap:Ethernet HWaddr 00:1b:24:b4:a0:9e


inet6 addr: fe80::21b:24ff:feb4:a09e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

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

lo Link encap:Local Loopback


inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:19947 errors:0 dropped:0 overruns:0 frame:0
TX packets:19947 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:193070032 (193.0 MB) TX bytes:193070032 (193.0 MB)

virbr0 Link encap:Ethernet HWaddr 7e:83:6d:62:f4:f6


inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
inet6 addr: fe80::7c83:6dff:fe62:f4f6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:33 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:5045 (5.0 KB)

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.

First Boot Options

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:

# aptitude install apt-proxy

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:

# vmbuilder kvm ubuntu --help

saulparada 5
To create the first VM, vm1, it is necessary go to the respective VM directory:

# cd ~/vm1/

...and run vmbuilder, for e.g., as follows:

vmbuilder kvm ubuntu --suite=intrepid --flavour=virtual --arch=i386 --


mirror=http://158.196.53.158:9999/ubuntu -o --libvirt=qemu:///system --tmpfs=- --ip=158.196.53.157 --
mask=255.255.255.0 --net=158.196.53.0 --bcast=158.196.53.255 --gw=158.196.53.1 --
part=vmbuilder.partition --templates=mytemplates --user=saulparada --name=saulparada --pass=smsp -
-addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --firstboot=boot.sh --mem=256 --
hostname=vm1 –bridge=br0

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.

Afterwards, can be found an XML configuration file for the VM in /etc/libvirt/qemu/


(/etc/libvirt/qemu/vm1.xml):

# 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 --connect qemu:///system

This is how the virtual shell looks:

root@ubuntu:~# virsh --connect qemu:///system


Connecting to uri: qemu:///system
Welcome to virsh, the virtualization interactive terminal.

Type: 'help' for help with commands


'quit' to quit

virsh #

To show all running VMs must be performed the instruction:

# list

To show all existent VMs should be performed the command:

list --all

virsh # list --all


Id Name State
----------------------------------
- vm1 shut off
- vm2 shut off
NOTE: For that example was taken into account two created VMs (vm1 and vm2).

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.

Now can be started the VM:

# 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

virsh # list --all


Id Name State
----------------------------------
- vm1 shut off
- vm2 shut off

virsh # start vm1


Domain vm1 started

virsh # list --all


Id Name State
----------------------------------
2 vm1 running
- vm2 shut off

Commands

To stop a VM, run:

# shutdown vm1

To immediately stop it (i.e., pull the power plug), run:

# destroy vm1

Suspend a VM:

# suspend vm1

Resume a VM:

# resume vm1

To leave the virtual shell type:

# 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

Anda mungkin juga menyukai