Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Virtualization With Xen 3.3.1 On Debian Etch


This tutorial provides step-by-step instructions on how to install Xen on a Debian Etch (4.0) system.

Xen lets you create guest operating systems (*nix operating systems like Linux and FreeBSD), so called “virtual machines” or domUs, under a host operating system (dom0). Using Xen you can separate your applications into different virtual machines that are totally independent from each other (e.g. a virtual machine for a mail server, a virtual machine for a high-traffic web site, another virtual machine that serves your customers’ web sites, a virtual machine for DNS, etc.), but still use the same hardware. This saves money, and what is even more important, it’s more secure. If the virtual machine of your DNS server gets hacked, it has no effect on your other virtual machines. Plus, you can move virtual machines from one Xen server to the next one.

I do not issue any guarantee that this will work for you!
1 Preliminary Note

I’m using a Debian Etch system with the hostname server1.example.com and the IP address 192.168.0.100 as the host system (dom0). I will use Debian Etch for the virtual machines (domU) as well.

This guide will explain how to set up image-based virtual machines and also LVM-based virtual machines.

Please note: Xen 3.3.1 comes with the kernel 2.6.18 which is pretty old. If you are using new hardware, the system might not boot anymore due to missing drivers!
2 Installing Xen

Before we install Xen, we must install some prerequisites:

apt-get install make gcc libc6-dev zlib1g-dev python python-dev python-twisted bridge-utils iproute libcurl3 libcurl3-dev bzip2 module-init-tools transfig tgif libncurses5-dev patch libvncserver-dev libsdl-dev libjpeg62-dev bcc bin86 gawk pciutils-dev mercurial build-essential

Next we download Xen 3.3.1…

cd /usr/src
wget http://bits.xensource.com/oss-xen/release/3.3.1/xen-3.3.1.tar.gz

… and uncompress it:

tar -xvzf xen-3.3.1.tar.gz
cd xen-3.3.1

Now we build it as follows:

make world && make dist

This can take some time, so please be patient. Afterwards, we install Xen:

./install.sh

The output should look as follows:

server1:/usr/src/xen-3.3.1# ./install.sh
Installing Xen from ‘./dist/install’ to ‘/’…
– installing for udev-based system
– modifying permissions
All done.
Checking to see whether prerequisite tools are installed…
Xen CHECK-INSTALL Sun Feb 8 21:22:39 CET 2009
Checking check_brctl: OK
Checking check_crypto_lib: OK
Checking check_curl: unused, OK
Checking check_iproute: OK
Checking check_python: OK
Checking check_python_xml: OK
Checking check_udev: OK
Checking check_xml2: unused, OK
Checking check_zlib_lib: OK
All done.
server1:/usr/src/xen-3.3.1#

Run

mv /lib/tls /lib/tls.disabled

… and create the system startup links for Xen:

update-rc.d xend defaults 20 21
update-rc.d xendomains defaults 21 20

Afterwards we open /etc/modules and make sure that we have the line loop max_loop=64 in it (this step is needed only if you want to create image-based virtual machines – you can skip it if you want to create LVM-based virtual machines):

vi /etc/modules

[…]
loop max_loop=64

Now take a look at the /boot directory:

ls -l /boot/

server1:/usr/src/xen-3.3.1# ls -l /boot/
total 15546
-rw-r–r– 1 root root 71818 2007-03-26 18:32 config-2.6.18-4-486
-rw-r–r– 1 root root 66264 2009-02-08 21:21 config-2.6.18.8-xen
drwxr-xr-x 2 root root 1024 2008-12-14 00:07 grub
-rw-r–r– 1 root root 4845777 2008-12-14 00:04 initrd.img-2.6.18-4-486
drwx—— 2 root root 12288 2008-12-13 23:59 lost+found
-rw-r–r– 1 root root 716728 2007-03-27 00:39 System.map-2.6.18-4-486
-rw-r–r– 1 root root 752541 2009-02-08 21:21 System.map-2.6.18.8-xen
-rw-r–r– 1 root root 1220128 2007-03-27 00:39 vmlinuz-2.6.18-4-486
-rw-r–r– 1 root root 1541626 2009-02-08 21:21 vmlinuz-2.6.18.8-xen
-rw-r–r– 1 root root 433689 2009-02-08 21:14 xen-3.3.1.gz
lrwxrwxrwx 1 root root 12 2009-02-08 21:22 xen-3.3.gz -> xen-3.3.1.gz
lrwxrwxrwx 1 root root 12 2009-02-08 21:22 xen-3.gz -> xen-3.3.1.gz
lrwxrwxrwx 1 root root 12 2009-02-08 21:22 xen.gz -> xen-3.3.1.gz
-rw-r–r– 1 root root 6180789 2009-02-08 21:14 xen-syms-3.3.1
server1:/usr/src/xen-3.3.1#

As you see, there’s a new kernel, 2.6.18.8-xen, but no ramdisk for it; therefore we build one…

depmod 2.6.18.8-xen
update-initramfs -c -k 2.6.18.8-xen

… and update our bootloader:

update-grub

Then reboot the system:

reboot

Run

uname -r

and your new Xen kernel should show up:

server1:~# uname -r
2.6.18.8-xen
server1:~#
3 Installing xen-tools

We will use xen-tools to create virtual machines. xen-tools make it very easy to create virtual machines – please read this tutorial to learn more: http://www.Kreationnext.com/xen_tools_xen_shell_argo. We don’t install the xen-tools package from the Debian Etch repository (because it is quite old), but directly from the repository of the developer – that way we get the newest version. Open /etc/apt/sources.list…

vi /etc/apt/sources.list

… and add the following line:

[…]
deb http://apt.steve.org.uk/etch etch main non-free contrib
[…]

Then run…

wget -q http://apt.steve.org.uk/apt-key.gpg -O- | apt-key add – && apt-get update

… and install xen-tools:

apt-get install xen-tools

4 Creating Image-Based Virtual Machines

Now we edit /etc/xen-tools/xen-tools.conf. This file contains the default values that are used by the xen-create-image script unless you specify other values on the command line. I changed the following values and left the rest untouched:

vi /etc/xen-tools/xen-tools.conf

[...]
dir = /home/xen
[...]
dist   = etch     # Default distribution to install.
[...]
gateway   = 192.168.0.1
netmask   = 255.255.255.0
broadcast = 192.168.0.255
[...]
passwd = 1
[...]
kernel      = /boot/vmlinuz-`uname -r`
initrd      = /boot/initrd.img-`uname -r`
[...]
mirror = http://ftp.de.debian.org/debian/
[...]
serial_device = hvc0 #default
[...]
disk_device = xvda #default
[...]

The dir line specifies where the virtual machine images will be stored. dist specifies the distribution to be installed in the virtual machines (Debian Etch) (there’s a comment in the file that explains what distributions are currently supported).

The passwd = 1 line makes that you can specify a root password when you create a new guest domain. In the mirror line specify a Debian mirror close to you.

Make sure you specify a gateway, netmask, and broadcast address. If you don’t, and you don’t specify a gateway and netmask on the command line when using xen-create-image, your guest domains won’t have networking even if you specified an IP address!

It is very important that you add the line serial_device = hvc0 (or serial_device = xvc0) because otherwise your virtual machines might not boot properly!

Before we go on, we must create the directory where the virtual machine images should be stored:

mkdir /home/xen

Now let’s create our first guest domain, xen1.example.com, with the IP address 192.168.0.101:

xen-create-image –hostname=xen1.example.com –size=4Gb –swap=256Mb –ip=192.168.0.101 –memory=128Mb –arch=i386 –role=udev

Options that you specify on the command line override the settings in /etc/xen-tools/xen-tools.conf. Options that are not specified on the command line are taken from /etc/xen-tools/xen-tools.conf. Please make sure that you add –role=udev, or your virtual machine might not boot properly!

(To learn more about the available options, take a look at the xen-create-image man page:

man xen-create-image

)

The xen-create-image command will now create the xen1.example.com virtual machine for us. This can take a few minutes. The output should be similar to this one:

server1:~# xen-create-image –hostname=xen1.example.com –size=4Gb –swap=256Mb –ip=192.168.0.101 –memory=128Mb –arch=i386 –role=udev

General Information
——————–
Hostname : xen1.example.com
Distribution : etch
Partitions : swap 256Mb (swap)
/ 4Gb (ext3)
Image type : sparse
Memory size : 128Mb
Kernel path : /boot/vmlinuz-2.6.18.8-xen
Initrd path : /boot/initrd.img-2.6.18.8-xen

Networking Information
———————-
IP Address 1 : 192.168.0.101 [MAC: 00:16:3E:85:BC:FC]
Netmask : 255.255.255.0
Broadcast : 192.168.0.255
Gateway : 192.168.0.1

WARNING
——-
Loopback module not loaded and you’re using loopback images
Run the following to load the module:

modprobe loop loop_max=255
Creating partition image: /home/xen/domains/xen1.example.com/swap.img
Done

Creating swap on /home/xen/domains/xen1.example.com/swap.img
Done

Creating partition image: /home/xen/domains/xen1.example.com/disk.img
Done

Creating ext3 filesystem on /home/xen/domains/xen1.example.com/disk.img
Done
Installation method: debootstrap
Done

Running hooks
Done

Role: udev
File: /etc/xen-tools/role.d/udev
Role script completed.

Creating Xen configuration file
Done
Setting up root password
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
All done
Logfile produced at:
/var/log/xen-tools/xen1.example.com.log
server1:~#

There should now be a xen1.example.com configuration file – /etc/xen/xen1.example.com.cfg. Open it and add the line extra = ‘xencons=hvc0 console=hvc0’ (or extra = ‘xencons=xvc0 console=xvc0’ if you use serial_device = xvc0 in /etc/xen-tools/xen-tools.conf) at the end so that the file looks as follows:

vi /etc/xen/xen1.example.com.cfg

#
# Configuration file for the Xen instance xen1.example.com, created
# by xen-tools 4.1 on Sun Feb 8 21:42:15 2009.
#

#
# Kernel + memory size
#
kernel = ‘/boot/vmlinuz-2.6.18.8-xen’
ramdisk = ‘/boot/initrd.img-2.6.18.8-xen’

memory = ‘128’

#
# Disk device(s).
#
root = ‘/dev/xvda2 ro’
disk = [
‘file:/home/xen/domains/xen1.example.com/disk.img,xvda2,w’,
‘file:/home/xen/domains/xen1.example.com/swap.img,xvda1,w’,
]
#
# Physical volumes
#

#
# Hostname
#
name = ‘xen1.example.com’

#
# Networking
#
vif = [ ‘ip=192.168.0.101,mac=00:16:3E:85:BC:FC’ ]

#
# Behaviour
#
on_poweroff = ‘destroy’
on_reboot = ‘restart’
on_crash = ‘restart’
extra = ‘xencons=hvc0 console=hvc0’

If you don’t add that line and you go to the console of the vm using the xm console command, the boot process seems to hang forever (while in fact, the vm is running perfectly fine – you can connect to it using SSH).

(Please note: if you have a dual-core or quad-core CPU and want the virtual machine to use all CPU cores, please add the line vcpus = ‘2’ or vcpus = ‘4’ to the configuration file.)

To start the virtual machine, run

xm create /etc/xen/xen1.example.com.cfg

Run

xm console xen1.example.com

to log in on that virtual machine (type CTRL+] if you are at the console, or CTRL+5 if you’re using PuTTY to go back to dom0), or use an SSH client to connect to it (192.168.0.101).

(If you see something like this during the boot process of the vm…

EXT3-fs: mounted filesystem with ordered data mode.

***************************************************************
***************************************************************
** WARNING: Currently emulating unsupported memory accesses **
** in /lib/tls glibc libraries. The emulation is **
** slow. To ensure full performance you should **
** install a ‘xen-friendly’ (nosegneg) version of **
** the library, or disable tls support by executing **
** the following as root: **
** mv /lib/tls /lib/tls.disabled **
** Offending process: modprobe (pid=1757) **
***************************************************************
***************************************************************

Continuing…

… run the following command in the vm:

mv /lib/tls /lib/tls.disabled

)

To get a list of running virtual machines, type

xm list

The output should look like this:

server1:~# xm list
Name ID Mem VCPUs State Time(s)
Domain-0 0 874 1 r—– 714.2
xen1.example.com 1 128 1 -b—- 63.8
server1:~#

To shut down xen1.example.com, do this:

xm shutdown xen1.example.com

If you want xen1.example.com to start automatically at the next boot of the system, then do this:

ln -s /etc/xen/xen1.example.com.cfg /etc/xen/auto

Here are the most important Xen commands:

xm create -c /path/to/config – Start a virtual machine.
xm shutdown <name> – Stop a virtual machine.
xm destroy <name> – Stop a virtual machine immediately without shutting it down. It’s as if you switch off the power button.
xm list – List all running systems.
xm console <name> – Log in on a virtual machine.
xm help – List of all commands.

A list of all virtual machines that were created with the xen-create-image command is available under

xen-list-images

server1:~# xen-list-images
Name: xen1.example.com
Memory: 128
IP: 192.168.0.101
server1:~#

To learn more about what you can do with xen-tools, take a look at this tutorial: http://www..com/xen_tools_xen_shell_argo

5 Creating LVM-Based Virtual Machines

This chapter explains how you can set up LVM-based virtual machines instead of virtual machines that use disk images. Virtual machines that use disk images are very slow and heavy on disk IO.

In this example I’m using a Debian Etch host with the LVM volume group /dev/vg0 that has about 50GB of space. /dev/vg0 contains two logical volumes, /dev/vg0/root and /dev/vg0/swap_1 that consume about 12GB of space – the rest is not allocated and can be used to create logical volumes for our virtual machines:

vgdisplay

server1:~# vgdisplay
— Volume group —
VG Name               vg0
System ID
Format                lvm2
Metadata Areas        1
Metadata Sequence No  3
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                2
Open LV               2
Max PV                0
Cur PV                1
Act PV                1
VG Size               49.52 GB
PE Size               4.00 MB
Total PE              12678
Alloc PE / Size       3072 / 12.00 GB
Free  PE / Size       9606 / 37.52 GB
VG UUID               iXaW0I-RkGD-WQlQ-dMaD-qfM5-xkvq-2w5uP5

server1:~#

lvdisplay

server1:~# lvdisplay
— Logical volume —
LV Name /dev/vg0/root
VG Name vg0
LV UUID 17thCh-CgS8-ZvrJ-FOai-B1AX-89sv-xxq12a
LV Write Access read/write
LV Status available
# open 1
LV Size 10.00 GB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:0

— Logical volume —
LV Name /dev/vg0/swap_1
VG Name vg0
LV UUID u2EZyP-3Vp5-Lr32-nquG-ub5a-f0U5-VI3WRN
LV Write Access read/write
LV Status available
# open 1
LV Size 2.00 GB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1

server1:~#

Now we edit /etc/xen-tools/xen-tools.conf. This file contains the default values that are used by the xen-create-image script unless you specify other values on the command line. I changed the following values and left the rest untouched:

vi /etc/xen-tools/xen-tools.conf

[…]
lvm = vg0
[…]
dist = etch # Default distribution to install.
[…]
gateway = 192.168.0.1
netmask = 255.255.255.0
broadcast = 192.168.0.255
[…]
passwd = 1
[…]
kernel = /boot/vmlinuz-`uname -r`
initrd = /boot/initrd.img-`uname -r`
[…]
mirror = http://ftp.de.debian.org/debian/
[…]
serial_device = hvc0 #default
[…]
disk_device = xvda #default
[…]

Make sure that you uncomment the lvm line and fill in the name of your volume group (vg0 in my case). At the same time make sure that the dir line is commented out!

dist specifies the distribution to be installed in the virtual machines (Debian Etch) (there’s a comment in the file that explains what distributions are currently supported).

The passwd = 1 line makes that you can specify a root password when you create a new guest domain.

In the mirror line specify a Debian mirror close to you.

Make sure you specify a gateway, netmask, and broadcast address. If you don’t, and you don’t specify a gateway and netmask on the command line when using xen-create-image, your guest domains won’t have networking even if you specified an IP address!

It is very important that you add the line serial_device = hvc0 (or serial_device = xvc0) because otherwise your virtual machines might not boot properly!

Now let’s create our first guest domain, xen1.example.com, with the IP address 192.168.0.101:

xen-create-image –hostname=xen1.example.com –size=4Gb –swap=256Mb –ip=192.168.0.101 –memory=128Mb –arch=i386 –role=udev

Options that you specify on the command line override the settings in /etc/xen-tools/xen-tools.conf. Options that are not specified on the command line are taken from /etc/xen-tools/xen-tools.conf. Please make sure that you add –role=udev, or your virtual machine might not boot properly!

(To learn more about the available options, take a look at the xen-create-image man page:

man xen-create-image

)

The xen-create-image command will now create the xen1.example.com virtual machine for us. This can take a few minutes. The output should be similar to this one:

server1:~# xen-create-image –hostname=xen1.example.com –size=4Gb –swap=256Mb –ip=192.168.0.101 –memory=128Mb –arch=i386 –role=udev

General Information
——————–
Hostname : xen1.example.com
Distribution : etch
Partitions : swap 256Mb (swap)
/ 4Gb (ext3)
Image type : full
Memory size : 128Mb
Kernel path : /boot/vmlinuz-2.6.18.8-xen
Initrd path : /boot/initrd.img-2.6.18.8-xen

Networking Information
———————-
IP Address 1 : 192.168.0.101 [MAC: 00:16:3E:1C:B9:F3]
Netmask : 255.255.255.0
Broadcast : 192.168.0.255
Gateway : 192.168.0.1
Creating swap on /dev/vg0/xen1.example.com-swap
Done

Creating ext3 filesystem on /dev/vg0/xen1.example.com-disk
Done
Installation method: debootstrap
Done

Running hooks
Done

Role: udev
File: /etc/xen-tools/role.d/udev
Role script completed.

Creating Xen configuration file
Done
Setting up root password
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
All done
Logfile produced at:
/var/log/xen-tools/xen1.example.com.log
server1:~#

As you see from the output, xen-create-image has created a new logical volume for our VM in the vg0 volume group, /dev/vg0/xen1.example.com-disk, for the VM’s root filesystem. Take a look at

lvdisplay

and you will see that it has also created a second logical volume, /dev/vg0/xen1.example.com-swap, for the VM’s swap:

server1:~# lvdisplay
— Logical volume —
LV Name /dev/vg0/root
VG Name vg0
LV UUID 17thCh-CgS8-ZvrJ-FOai-B1AX-89sv-xxq12a
LV Write Access read/write
LV Status available
# open 1
LV Size 10.00 GB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:0

— Logical volume —
LV Name /dev/vg0/swap_1
VG Name vg0
LV UUID u2EZyP-3Vp5-Lr32-nquG-ub5a-f0U5-VI3WRN
LV Write Access read/write
LV Status available
# open 1
LV Size 2.00 GB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1

— Logical volume —
LV Name /dev/vg0/xen1.example.com-swap
VG Name vg0
LV UUID No03G8-PzA3-67JH-m936-rUQY-rZFi-djIu21
LV Write Access read/write
LV Status available
# open 0
LV Size 256.00 MB
Current LE 64
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:2

— Logical volume —
LV Name /dev/vg0/xen1.example.com-disk
VG Name vg0
LV UUID xDwpFO-XALQ-7I1N-TA3k-paYc-biG7-TjrNIM
LV Write Access read/write
LV Status available
# open 0
LV Size 4.00 GB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:3

server1:~#

There should now be a xen1.example.com configuration file – /etc/xen/xen1.example.com.cfg. Open it and add the line extra = ‘xencons=hvc0 console=hvc0’ (or extra = ‘xencons=xvc0 console=xvc0’ if you use serial_device = xvc0 in /etc/xen-tools/xen-tools.conf) at the end so that the file looks as follows:

vi /etc/xen/xen1.example.com.cfg

#
# Configuration file for the Xen instance xen1.example.com, created
# by xen-tools 4.1 on Sun Feb 8 22:01:30 2009.
#

#
# Kernel + memory size
#
kernel = ‘/boot/vmlinuz-2.6.18.8-xen’
ramdisk = ‘/boot/initrd.img-2.6.18.8-xen’

memory = ‘128’

#
# Disk device(s).
#
root = ‘/dev/xvda2 ro’
disk = [
‘phy:/dev/vg0/xen1.example.com-disk,xvda2,w’,
‘phy:/dev/vg0/xen1.example.com-swap,xvda1,w’,
]
#
# Physical volumes
#

#
# Hostname
#
name = ‘xen1.example.com’

#
# Networking
#
vif = [ ‘ip=192.168.0.101,mac=00:16:3E:1C:B9:F3’ ]

#
# Behaviour
#
on_poweroff = ‘destroy’
on_reboot = ‘restart’
on_crash = ‘restart’

extra = ‘xencons=hvc0 console=hvc0’

If you don’t add that line and you go to the console of the vm using the xm console command, the boot process seems to hang forever (while in fact, the vm is running perfectly fine – you can connect to it using SSH).

(If we had used disk images instead of logical volumes, the disk line would look similar to this one:

disk = [ ‘file:/path/to/xen1.example.com/disk.img,xvda1,w’, ‘file:/path/to/xen1.example.com/swap.img,xvda2,w’ ]

)

(Please note: if you have a dual-core or quad-core CPU and want the virtual machine to use all CPU cores, please add the line vcpus = ‘2’ or vcpus = ‘4’ to the configuration file.)

To start the virtual machine, run

xm create /etc/xen/xen1.example.com.cfg

Run

xm console xen1.example.com

to log in on that virtual machine (type CTRL+] if you are at the console, or CTRL+5 if you’re using PuTTY to go back to dom0), or use an SSH client to connect to it (192.168.0.101).

(If you see something like this during the boot process of the vm…

EXT3-fs: mounted filesystem with ordered data mode.

***************************************************************
***************************************************************
** WARNING: Currently emulating unsupported memory accesses **
** in /lib/tls glibc libraries. The emulation is **
** slow. To ensure full performance you should **
** install a ‘xen-friendly’ (nosegneg) version of **
** the library, or disable tls support by executing **
** the following as root: **
** mv /lib/tls /lib/tls.disabled **
** Offending process: modprobe (pid=1757) **
***************************************************************
***************************************************************

Continuing…

… run the following command in the vm:

mv /lib/tls /lib/tls.disabled

)

To get a list of running virtual machines, type

xm list

The output should look like this:

server1:~# xm list
Name ID Mem VCPUs State Time(s)
Domain-0 0 874 1 r—– 1367.1
xen1.example.com 2 128 1 —— 33.3
server1:~#

To shut down xen1.example.com, do this:

xm shutdown xen1.example.com

If you want xen1.example.com to start automatically at the next boot of the system, then do this:

ln -s /etc/xen/xen1.example.com.cfg /etc/xen/auto

Here are the most important Xen commands:

xm create -c /path/to/config – Start a virtual machine.
xm shutdown <name> – Stop a virtual machine.
xm destroy <name> – Stop a virtual machine immediately without shutting it down. It’s as if you switch off the power button.
xm list – List all running systems.
xm console <name> – Log in on a virtual machine.
xm help – List of all commands.

A list of all virtual machines that were created with the xen-create-image command is available under

xen-list-images

server1:~# xen-list-images
Name: xen1.example.com
Memory: 128
IP: 192.168.0.101
server1:~#

To learn more about what you can do with xen-tools, take a look at this tutorial: http://www.Kreationnext.com/xen_tools_xen_shell_argo
6 Links

Xen: http://www.xensource.com/xen/
xen-tools: http://xen-tools.org/software/xen-tools
Debian: http://www.debian.org/

Comments

comments