Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Installing Xen On An Ubuntu 8.04 (Hardy Heron) Server From The Ubuntu Repositories


This tutorial provides step-by-step instructions on how to install Xen on an Ubuntu Hardy Heron (Ubuntu 8.04) server system (i386). You can find all the software used here in the Ubuntu repositories, so no external files (apart from a fixed Ubuntu Xen kernel to enable networking for the virtual machines) or compilation are needed.

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 will use Ubuntu Hardy Heron (i386) for the host OS (dom0) and Ubuntu Hardy Heron and Ubuntu Gutsy Gibbon (7.10) for the guest operating systems (domU). Other Debian-based distributions that can be installed using xen-tools are Debian Etch, Debian Lenny, Debian Sid, and Debian Sarge, as well as Ubuntu Dapper Drake, Ubuntu Edgy Eft, and Ubuntu Feisty Fawn.

This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.

This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

 

1 Install The Ubuntu Hardy Heron Host System (dom0)

You can install the host system (dom0) as shown in the chapters one to ten of this tutorial: http://www.Kreationnext.com/perfect-server-ubuntu8.04-lts (of course, you don’t have to do this if you already have an Ubuntu 8.04 host system that you want to use).

Make sure that you are logged in as root (type in

sudo su

to become root), because we must run all the steps from this tutorial as root user. Also, if you want to use vi as your text editor (as suggested by this tutorial), you should run

apt-get install vim-full

The vim-full package makes sure that the vi text editor behaves as expected (without vim-full, you might experience some strange behaviour in the vi text editor).

It’s a good idea to disable AppArmor:

/etc/init.d/apparmor stop
update-rc.d -f apparmor remove

dom0‘s FQDN in this example will be server1.example.com. server1.example.com‘s IP address will be 192.168.0.100 in this tutorial, and the gateway I use is 192.168.0.1, so the virtual machines will have to use that one, too.

 

2 Install Xen

To install Xen and all needed dependencies, all we have to do is run the following command:

apt-get install ubuntu-xen-server

This will also install the xen-tools package which we use later on to create virtual machines.

The ubuntu-xen-server package installs the Ubuntu kernel 2.6.24-16-xen; unfortunately there’s a nasty bug in this kernel which causes networking to not work for virtual machines (see https://bugs.launchpad.net/ubuntu/+source/xen-3.2/+bug/204010 and https://bugs.launchpad.net/ubuntu/+source/linux/+bug/218126). Fortunately Hirano Takahito has patched this kernel and made the patched versions (for i386 and amd64) available on http://www.il.is.s.u-tokyo.ac.jp/~hiranotaka/ (I have created a mirror of these packages at http://downloads.Kreationnext.com/ubuntu_8.04_server_xen_from_repositories/ in case the original link doesn’t work anymore one day). Download the correct linux-image package for your architecture (i386 or amd64), e.g.:

wget http://www.il.is.s.u-tokyo.ac.jp/~hiranotaka/linux-image-2.6.24-16-xen_2.6.24-16.30zng1_i386.deb

Then replace the Ubuntu 2.6.24-16-xen kernel with it like this:

dpkg -i linux-image-2.6.24-16-xen_2.6.24-16.30zng1_i386.deb

Next do this:

mv /lib/tls /lib/tls.disabled

We also need to add the loop module to the kernel every time we boot our system, so we edit /etc/modules. If you already have a loop line in there, make it look like this, otherwise add it:

vi /etc/modules

[...]
loop max_loop=64
[...]

Now take a look at the /boot directory to see which kernels and ramdisks are installed:

ls -l /boot/

root@server1:~# ls -l /boot/
total 34380
-rw-r–r– 1 root root  426444 2008-04-10 18:55 abi-2.6.24-16-server
-rw-r–r– 1 root root   80203 2008-04-10 18:55 config-2.6.24-16-server
-rw-r–r– 1 root root   85149 2008-04-19 23:17 config-2.6.24-16-xen
drwxr-xr-x 2 root root    4096 2008-05-06 15:19 grub
-rw-r–r– 1 root root 7323503 2008-04-24 11:47 initrd.img-2.6.24-16-server
-rw-r–r– 1 root root 7047843 2008-04-24 11:45 initrd.img-2.6.24-16-server.bak
-rw-r–r– 1 root root 7056979 2008-05-06 15:19 initrd.img-2.6.24-16-xen
-rw-r–r– 1 root root 7062168 2008-05-06 15:09 initrd.img-2.6.24-16-xen.bak
-rw-r–r– 1 root root  103204 2007-09-28 12:06 memtest86+.bin
-rw-r–r– 1 root root  933296 2008-04-10 18:55 System.map-2.6.24-16-server
-rw-r–r– 1 root root  883544 2008-04-19 23:17 System.map-2.6.24-16-xen
-rw-r–r– 1 root root 1987288 2008-04-10 18:55 vmlinuz-2.6.24-16-server
-rw-r–r– 1 root root 1730344 2008-04-19 23:17 vmlinuz-2.6.24-16-xen
-rw-r–r– 1 root root  370610 2008-04-11 03:16 xen-3.2.gz
root@server1:~#

The /boot/vmlinuz-2.6.24-16-xen kernel is the new, patched Xen kernel, and /boot/initrd.img-2.6.24-16-xen is its ramdisk. We will need these soon.

You can now also take a look at the /usr/lib/xen-tools directory because it reveals which distributions can be installed in a virtual machine with xen-tools:

ls -l /usr/lib/xen-tools

root@server1:~# ls -l /usr/lib/xen-tools
total 36
drwxr-xr-x 2 root root 4096 2008-05-06 15:08 centos-4.d
drwxr-xr-x 2 root root 4096 2008-05-06 15:08 centos-5.d
-rwxr-xr-x 1 root root 4409 2008-04-06 03:12 common.sh
drwxr-xr-x 2 root root 4096 2008-05-06 15:08 dapper.d
drwxr-xr-x 2 root root 4096 2008-05-06 15:08 debian.d
drwxr-xr-x 2 root root 4096 2008-05-06 15:08 edgy.d
lrwxrwxrwx 1 root root    8 2008-05-06 15:08 etch.d -> debian.d
lrwxrwxrwx 1 root root   15 2008-05-06 15:08 fedora-core-4.d -> fedora-core-6.d
lrwxrwxrwx 1 root root   15 2008-05-06 15:08 fedora-core-5.d -> fedora-core-6.d
drwxr-xr-x 2 root root 4096 2008-05-06 15:08 fedora-core-6.d
lrwxrwxrwx 1 root root   15 2008-05-06 15:08 fedora-core-7.d -> fedora-core-6.d
lrwxrwxrwx 1 root root    6 2008-05-06 15:08 feisty.d -> edgy.d
drwxr-xr-x 2 root root 4096 2008-05-06 15:08 gentoo.d
lrwxrwxrwx 1 root root    6 2008-05-06 15:08 gutsy.d -> edgy.d
lrwxrwxrwx 1 root root    6 2008-05-06 15:08 hardy.d -> edgy.d
lrwxrwxrwx 1 root root    8 2008-05-06 15:08 lenny.d -> debian.d
lrwxrwxrwx 1 root root    8 2008-05-06 15:08 sarge.d -> debian.d
lrwxrwxrwx 1 root root    8 2008-05-06 15:08 sid.d -> debian.d
root@server1:~#

As you see we can install the following distributions:

  • Debian: Etch, Lenny (testing), Sid (unstable), Sarge
  • Ubuntu: Dapper Drake, Edgy Eft, Feisty Fawn, Gutsy Gibbon, Hardy Heron
  • CentOS 4, CentOS 5
  • Fedora Core 4, 5, 6, Fedora 7
  • Gentoo

(I haven’t tried to install CentOS, Fedora, and Gentoo with xen-tools yet, so I can’t say how good it works.)

I want to store my virtual machines in the /home/xen directory, therefore I create it now:

mkdir /home/xen

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. As mentioned before, the xen-tools package got installed together with the ubuntu-xen-server package.

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   = hardy     # Default distribution to install.
[...]
gateway   = 192.168.0.1
netmask   = 255.255.255.0
broadcast = 192.168.0.255
[...]
passwd = 1
[...]
mirror = http://archive.ubuntu.com/ubuntu/
[...]

The dist line holds the default distribution that you want to install in a virtual machine. The contents of the /usr/lib/xen-tools directory reveals which distributions are available (see above).

The kernel line must contain our Xen kernel, and the initrd line its ramdisk. The default /etc/xen-tools/xen-tools.conf file has the values kernel = /boot/vmlinuz-`uname -r` and initrd = /boot/initrd.img-`uname -r` which automatically translate to the correct kernel and ramdisk, so we don’t have to modify these lines.

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

Make sure you specify a gateway and netmask. 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!

Now reboot the system:

reboot

If your system reboots without problems, then everything is fine!

Run

uname -r

and your new Xen kernel should show up:

root@server1:~# uname -r
2.6.24-16-xen
root@server1:~#

3 Creating Virtual Machines (domU)

Now let’s create our first guest domain, xen1.example.com, running Hardy Heron (hardy) with the IP address 192.168.0.101:

xen-create-image –hostname=xen1.example.com –size=2Gb –swap=256Mb –ide \
–ip=192.168.0.101 –netmask=255.255.255.0 –gateway=192.168.0.1 –force \
–dir=/home/xen –memory=64Mb –arch=i386 –kernel=/boot/vmlinuz-2.6.24-16-xen \
–initrd=/boot/initrd.img-2.6.24-16-xen –install-method=debootstrap –dist=hardy \
–mirror=http://archive.ubuntu.com/ubuntu/ –passwd

A lot of switches are unnecessary here because we specified the same details in /etc/xen-tools/xen-tools.conf but it shows that you can specify the desired settings either on the command line or in /etc/xen-tools/xen-tools.conf. Please make sure that you specify –ide, otherwise your virtual machine might not boot!

(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:

root@server1:~# xen-create-image –hostname=xen1.example.com –size=2Gb –swap=256Mb –ide \
> –ip=192.168.0.101 –netmask=255.255.255.0 –gateway=192.168.0.1 –force \
> –dir=/home/xen –memory=64Mb –arch=i386 –kernel=/boot/vmlinuz-2.6.24-16-xen \
> –initrd=/boot/initrd.img-2.6.24-16-xen –install-method=debootstrap –dist=hardy \
> –mirror=http://archive.ubuntu.com/ubuntu/ –passwd

General Information
——————–
Hostname       :  xen1.example.com
Distribution   :  hardy
Partitions     :  swap            256Mb (swap)
/               2Gb   (ext3)
Image type     :  sparse
Memory size    :  64Mb
Kernel path    :  /boot/vmlinuz-2.6.24-16-xen
Initrd path    :  /boot/initrd.img-2.6.24-16-xen

Networking Information
———————-
IP Address 1   : 192.168.0.101 [MAC: 00:16:3E:62:DA:BB]
Netmask        : 255.255.255.0
Broadcast      : 192.168.0.255
Gateway        : 192.168.0.1

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

No role scripts were specified.  Skipping

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
root@server1:~#

There should now be a xen1.example.com configuration file – /etc/xen/xen1.example.com.cfg. Take a look at it to become familiar with virtual machine configuration files:

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

# Configuration file for the Xen instance xen1.example.com, created
# by xen-tools 3.8 on Tue May  6 16:01:38 2008.
#
#
#  Kernel + memory size
#
kernel      = '/boot/vmlinuz-2.6.24-16-xen'
ramdisk     = '/boot/initrd.img-2.6.24-16-xen'
memory      = '64'
#
#  Disk device(s).
#
root        = '/dev/hda2 ro'
disk        = [
                  'file:/home/xen/domains/xen1.example.com/swap.img,hda1,w',
                  'file:/home/xen/domains/xen1.example.com/disk.img,hda2,w',
              ]

#
#  Hostname
#
name        = 'xen1.example.com'
#
#  Networking
#
vif         = [ 'ip=192.168.0.101,mac=00:16:3E:62:DA:BB' ]
#
#  Behaviour
#
on_poweroff = 'destroy'
on_reboot   = 'restart'
on_crash    = 'restart'
extra = '2 console=xvc0'

The file: parameter is now deprecated in Xen 3.2, therefore we must edit /etc/xen/xen1.example.com.cfg now and replace file: with tap:aio: (otherwise you’ll get an error similar to this one:

Error: Device 769 (vbd) could not be connected. losetup /dev/loop0 /home/xen/domains/xen1.example.com/swap.img failed

when you try to start a vm – see http://ubuntuforums.org/showthread.php?t=770902 as well):

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

[...]
disk        = [
                  'tap:aio:/home/xen/domains/xen1.example.com/swap.img,hda1,w',
                  'tap:aio:/home/xen/domains/xen1.example.com/disk.img,hda2,w',
              ]
[...]

(Please note: if you have a dual-core CPU and want the virtual machine to use both CPU cores, please add the line vcpus = ‘2’ to the configuration file, like this:

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

# Configuration file for the Xen instance xen1.example.com, created
# by xen-tools 3.8 on Mon May  5 16:49:32 2008.
#
#
#  Kernel + memory size
#
kernel      = '/boot/vmlinuz-2.6.24-16-xen'
ramdisk     = '/boot/initrd.img-2.6.24-16-xen'
memory      = '64'
vcpus       = '2'
[...]

Afterwards, in the virtual machine, you can run the command

cat /proc/cpuinfo

to check that both CPU cores are being used by the virtual machine. The output could look like this if your vm is using both cores:

xen1:~# cat /proc/cpuinfo
processor       : 0
vendor_id       : AuthenticAMD
cpu family      : 15
model           : 75
model name      : AMD Athlon(tm) 64 X2 Dual Core Processor 4200+
stepping        : 2
cpu MHz         : 2210.054
cache size      : 512 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 1
wp              : yes
flags           : fpu tsc msr pae mce cx8 apic mca cmov pat pse36 clflush mmx fxsr
sse sse2 ht syscall nx mmxext fxsr_opt lm 3dnowext 3dnow pni cx16 lahf_lm cmp_legacy svm
cr8legacy ts fid vid ttp tm stc
bogomips        : 5526.81

processor       : 1
vendor_id       : AuthenticAMD
cpu family      : 15
model           : 75
model name      : AMD Athlon(tm) 64 X2 Dual Core Processor 4200+
stepping        : 2
cpu MHz         : 2210.054
cache size      : 512 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 1
wp              : yes
flags           : fpu tsc msr pae mce cx8 apic mca cmov pat pse36 clflush mmx fxsr
sse sse2 ht syscall nx mmxext fxsr_opt lm 3dnowext 3dnow up pni cx16 lahf_lm cmp_legacy svm
cr8legacy ts fid vid ttp tm stc
bogomips        : 5526.81

xen1:~#

)

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

In the virtual machine (when you use it for the first time), run

mv /lib/tls /lib/tls.disabled

Now back to the host system (dom0). To get a list of running virtual machines, type

xm list

The output should look like this:

root@server1:~# xm list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0   569     1     r—–    103.6
xen1.example.com                             1    64     1     -b—-     60.8
root@server1:~#

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

xm shutdown xen1.example.com

If you want vm01 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.

Let’s create a second vm, xen2.example.com with the IP address 192.168.0.102:

xen-create-image –hostname=xen2.example.com –size=2Gb –swap=256Mb –ide \
–ip=192.168.0.102 –netmask=255.255.255.0 –gateway=192.168.0.1 –force \
–dir=/home/xen –memory=64Mb –arch=i386 –kernel=/boot/vmlinuz-2.6.24-16-xen \
–initrd=/boot/initrd.img-2.6.24-16-xen –install-method=debootstrap –dist=hardy \
–mirror=http://archive.ubuntu.com/ubuntu/ –passwd

Then open the configuration file of the vm, /etc/xen/xen2.example.com.cfg, and replace file: with tap:aio: again:

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

[...]
disk        = [
                  'tap:aio:/home/xen/domains/xen2.example.com/swap.img,hda1,w',
                  'tap:aio:/home/xen/domains/xen2.example.com/disk.img,hda2,w',
              ]
[...]

Afterwards, you can start xen2.example.com like this:

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

(Don’t forget to run

xm console xen2.example.com

and

mv /lib/tls /lib/tls.disabled

when you’re in the vm for the first time!)

… and shut it down like this:

xm shutdown xen2.example.com

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

xen-list-images

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

Name: xen2.example.com
Memory: 64
IP: 192.168.0.102
root@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

 

  • Xen: http://www.xensource.com/xen/
  • xen-tools: http://xen-tools.org/software/xen-tools
  • Ubuntu: http://www.ubuntu.com/

Comments

comments