Posts Tagged ‘xen’

Setting up Xen on Debian etch (64bit)

Posted by rich on August 8th, 2008 categorised as How-to | 7 Comments »

Today I decided to setup Xen (not the Half-Life world) on a clean Debian installation so I could have a few VPSes to play around with load balancing mySQL, FastCGI and Mongrel. I thought I’d break down the steps I went though to get the basic Xen setup working.

Note: This is by no means a perfect setup, it’s more of a quick way to play around with Xen using a spare PC in the home. If you want more of a production level setup I suggest checking out the Xen website, or Google.

Step 1. Install Debian (Etch)

Here you just need to setup a standard Debian system, I’m using the 64bit edition of Etch but the process is the same for most Debian releases. I won’t go into detail on how to setup Debian as there are plenty of how-tos out there, for me the normal setup was fine. During the installation I gave the machine judgement as the hostname and gnet.foo as the domain (you can use whatever) I also unchecked desktop from the selectable software menu as I didn’t need/or want a GUI.

Step 2. Configure Debian ready for Xen

Once you’ve booted and logged into your system you need to install a few applications, but before we do that I tend to remove the CD source from apt-get’s sources.list:

vim /etc/apt/sources.list
#comment out the CD line
#deb cdrom:[Debian...........
apt-get update

Now install the packages (remove vim-full if you're not using vim):

apt-get install ssh build-essential vim-full

Next we need to configure the server to use a static IP (if you didn't during the installation). I gave my box (judgement) a static IP of 192.168.1.10. Open up the interfaces file:

vim /etc/network/interfaces

Now if you selected DHCP during the installation your file should look similar to this by default (eth2 was my interface, yours might be different):

allow-hotplug eth2
iface eth2 inet dhcp

Change yours to match this (of course change eth2 to match yours and whatever IP you wish to use):

auto eth2
address 192.168.1.10
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

Once you've done that you need to restart the networking:

/etc/init.d/networking restart

If all is working then you should be able to ping other computers on your network and they should be able to ping your machine.

If you wish you can continue the rest of the setup via SSH (on windows you can use Putty).

Finally just make sure your system is up-to-date before giving it a reboot:

apt-get upgrade
shutdown -r now

Step 3. Installing Xen

There are two main options when it comes to installing Xen. You can compile it from the source code or simply use apt-get, the latter worked fine for me and is dead simple...to install via apt-get use the following command (bridge-utils is used to setup the network for the virtual machines):

apt-get install xen-linux-system-2.6.18-6-xen-amd64 bridge-utils

The version of Xen kernel might of changed since writing this, try this command to see what's there:

apt-cache search xen-linux

When that has finished installing give the machine a reboot and select Xen kernel from the GRUB menu (for me it became the default option):

shudown -r now

Once the system is back up and logged in (your previous credentials should work) check that you've booted the correct kernel:

uname -r

It should read something like 2.6.18-6-xen-amd64 if not then you've most likely booted the wrong kernel from GRUB.

Step 4. Creating virtual images or DomU's (the VPSes)

To help with creating, starting, stopping, etc... you need to install xen-tools:

apt-get install xen-tools

Next we need to create the place we want to store the domains (virtual servers), I put mine in /home/xen (xen requires a folder called domains within this folder) so run this command:

mkdir -p /home/xen/domains

Once you've created the location to store the domains you need to edit a few options in Xen's configuration. Start by opening up xend-config.sxp:

vim /etc/xen/xend-config.sxp

and look this for line (around line 70 for me):

##
# To bridge network traffic, like this:
#
# dom0: fake eth0 -> vif0.0 -+
#                            |
#                          bridge -> real eth0 -> the network
#                            |
# domU: fake eth0 -> vifN.0 -+
#
# use
#
# (network-script network-bridge)

and uncomment the network-script network-bridge part so it looks like this:

##
# To bridge network traffic, like this:
#
# dom0: fake eth0 -> vif0.0 -+
#                            |
#                          bridge -> real eth0 -> the network
#                            |
# domU: fake eth0 ->; vifN.0 -+
#
# use
#
(network-script network-bridge)

Now we need to setup some defaults as well as the location to store the domains by opening up xen-tools.conf:

vim /etc/xen-tools/xen-tools.conf

There are several things to comment out and change here so I'll just list the values I changed in mine:

...
dir = /home/xen
...
debootstrap = 1
...
dist = etch
#If you want to give your domains static IP's rather than use DHCP (which I did then you can setup the default gateway/netmask)
gateway = 192.168.1.1
netmask = 255.255.255.0
...
#This bit is important, as mine differed to the default you need to change kernel and initrd to point to the right ones (ls /boot/* - to find out)
kernel = /boot/vmlinuz-2.6.18-6-xen-amd64
initrd = /boot/initrd.img-2.6.18-6-xen-amd64
...
#Change this to your prefered mirror (for me that's the UK):
mirror = http://ftp.uk.debian.org/debian/

Restart Xen to make sure the configuration loads correctly:

/etc/init.d/xend restart

You should now be ready to create your first domain. If you're happy with the defaults in the xen-tools.conf file then you can leave a lot of the options out or you can of course put them as arguments of the command, check the man pages for xen-create-image for more information:

xen-create-image --hostname=i-am-webial --ip=192.168.1.11 --netmask=255.255.255.0 --gateway=192.168.1.1 --passwd

You should see output of something like this:

General Infomation
--------------------
Hostname       :  i-am-webial
Distribution   :  etch
Fileystem Type :  ext3

Size Information
----------------
Image size     :  4Gb
Swap size      :  128Mb
Image type     :  sparse
Memory size    :  128Mb
Kernel path    :  /boot/vmlinuz-2.6.18-6-xen-amd64
Initrd path    :  /boot/initrd.img-2.6.18-6-xen-amd64

Networking Information
----------------------
IP Address 1   : 192.168.1.11
Netmask        : 255.255.255.0
Gateway        : 192.168.1.1

Creating swap image: /home/xen/domains/i-am-webial/swap.img
Done

Creating disk image: /home/xen/domains/i-am-webial/disk.img
Done

Creating ext3 filesystem on /home/xen/domains/i-am-webial/disk.img
Done

Installing your system with debootstrap mirror http://ftp.uk.debian.org/debian/
....
....
....

Once it's finished you can start your domain by using the following command:

xm create /etc/xen/i-am-webial.cfg

Extra Stuff

Open a console for the domain:

xm console i-am-webial

you can exit the console by doing Ctrl + ].

To shutdown a domain:

xm shutdown i-am-webial

Increase the RAM of the domain:

vim /etc/xen/i-am-webial.cfg

find and edit memory

memory  = '512'

Resize the swap to 1GB (Resize Xen DomU swap):

cd /home/xen/domains/i-am-webial
dd if=/dev/zero of=swap.img bs=1024k count=1024
mkswap swap.img

Expand the file system by 4 GB (Resize Xen DomU disk)

dd if=/dev/zero bs=1GB count=4 >> disk.img
resize2fs -f disk.img

Hopefully you should now all be setup with basic VPSes, if you need any help with problems please feel free to contact me. You may also leave any feedback/comments you wish below.