CentOS 5 Xen images – part 2: installing the Xen guest

OK, so you downloaded an image from jailtime.org or you made your own image as described in my previous post, and now you want to install a Xen guest using it. Here’s how to do it :

Setup the dom0 (host OS)

Of course, Xen should be installed on the host OS. I use CentOS 5, so I just selected Xen during the installation. It will install a xen kernel that you should use to boot the host OS (dom0). I won’t go into details here, because that’s really easy to do with CentOS or Redhat Enterprise Linux. With other distributions, you could have to install distribution-specific packages, or use the official Xen package from xen.org.

Setup the target partition or logical volume

You should create a filesystem for the “root” partition and the swap. You could use simple files, but you will have better performance using real partitions or LVM volumes. LVM volumes also has other advantages, like the ability to create snapshots for backing up data, and easy resizing.

The following commands will create a 5GB root logical volume (LV) and 1GB swap in the /dev/vg0 volume group (VG). For more information about LVM, search for LVM howto in a search engine.

# root
lvcreate -L 5000M -n mailroot /dev/vg0
mkfs.ext3 /dev/vg0/mailroot
# swap
lvcreate -L 1000M -n mailswap /dev/vg0
mkswap /dev/vg0/mailswap

You can then mount the root partition and copy the base system (either an image from jailtime or an image you made yourself) on it.

mkdir /mnt/mailroot
mount /dev/vg0/mailroot /mnt/mailroot
# if you image contents is located in /centos...
cp -R /centos/* /mnt/mailroot/

Don’t forget to unmount the root partition when you’re done! Xen will not boot the domain if the partition is already mounted.

Download a kernel for the domU

The kernel that we will need to boot the domU has be to located in the dom0.

You can use the standard xen kernel that comes with CentOS to do that (e.g. vmlinuz-2.6.18-53.1.13.el5xen) , but you’ll also need an initrd, or the kernel won’t boot. To make the initrd, use the following command :

/sbin/mkinitrd --with=xennet --preload=xenblk /boot/initrd-centos5-xen.img 2.6.18-53.1.13.el5xen

This makes an initrd image with the required modules to boot a domU. The last parameter is the version of your kernel (the one you will use to boot the domU). You can get this number by typing “uname -r” on the command line. This will result in a /boot/initrd-centos5-xen.img image file.

Note (2008-02-14) : in a previous version of this blog post, I recommended to use a kernel from the official Xen distribution at xen.org. It worked, but it doesn’t seem to work anymore.

Create the configuration file

The configuration of the Xen guest is controlled by a simple text file. Create it as /etc/xen/yourdomUname, and move (or symlink) it in /etc/xen/auto if you want to start it automatically on boot.

Most basic parameters in this file are easy to understand. You should make sure “kernel” points to the kernel you copied from the xen tarball. “memory” is the amount of RAM allocated to the guest. “name” will be the name of the guest that you will use when connecting to it or shutting it down using the “xm” command.

“vif” contains information about network interfaces. One important thing in that line is the MAC address. If you don’t specify it here, a random MAC will be assigned at each boot, and that may not give good results. Edit the last 3 numbers (put anything, it just has to be unique across your network).

Finally, ‘disk’ is the parameter that tells Xen what partitions to use and what device name it will assign them. The last line, ‘root’, will tell the kernel what is the root device.

kernel = "/boot/vmlinuz-2.6.18-53.1.13.el5xen"
ramdisk = "/boot/initrd-centos5-xen.img"
memory = 512
name = "mail"
vif = [ 'mac=00:16:3e:21:f1:31,bridge=xenbr0' ]
dhcp = "dhcp"
disk = ['phy:/dev/vg0/mailroot,sda1,w', 'phy:/dev/vg0/mailswap,sda2,w' ]
# The next line would be useful if you want to use an simple file instead of a partition/LV
#disk = ['file:/root/test.img,sda1,w', 'file:/root/centos.swap,sda2,w' ]
# We don't use pygrub, we boot the kernel directly from dom0
#bootloader="/usr/bin/pygrub"
root = "/dev/sda1 ro"

Boot the domain!

OK, you’re ready to boot the guest domain! Just issue the following command to “create” (which means boot, really) the domU.

xm create /etc/xen/YOUR_CONFIG_FILE -c

The -c parameter tells xm to connect to the domain’s console. You can disconnect from it by pressing CTRL+], and connect to it again with “xm connect NAME”.

If everything works right, you should see the login prompt appearing, and you will be ready to use the new guest domain!

Fix SSH

If you made the guest image yourself as I explained in my previous post (part 1), you need to create the random device to fix SSH (and probably other services that requires generating keys). Issue the following commands on the guest’s console :

/sbin/MAKEDEV generic
/etc/init.d/sshd start

Tags: , , , ,

7 Responses to “CentOS 5 Xen images – part 2: installing the Xen guest”

  1. Pgsousa Says:

    I followed your configuration but it gives me this error when I boot guest domain:

    Warning: unable to open an initial console.
    Restarting system.

    Maybe it needs a initrd?

    PGSousa

  2. jphoude Says:

    Damn, I just tried creating a new guest, after few months while I was too busy to work on that, and I realized that I also get this error!
    I don’t know why it’s happening now, and not before… Perhaps it’s because of the centos 5.1 update, or the XenSource kernel changed (or I just made a mistake somewhere I can’t seem to find it) ?

    Anyway, I was able to boot a domain correctly by using the (xen) kernel that comes with CentOS 5.1 with a custom initrd. I’m using the vmlinuz-2.6.18-53.1.6.el5xen kernel.
    The command I used to create the initrd file was :
    /sbin/mkinitrd –with=xennet –preload=xenblk /boot/initrd-centos5-xen.img 2.6.18-53.1.6.el5xen
    (watch out the double “- -” what copy-pasting this line)

    So now, my config looks like this :
    kernel = “/boot/vmlinuz-2.6.18-53.1.6.el5xen”
    ramdisk = “/boot/initrd-centos5-xen.img”

    I updated the post to reflect this.

  3. Xen troubleshooting — Jean-Pascal Houde's weblog Says:

    [...] I had this problem when using the kernels from XenSource to boot a CentOS 5 image (this worked previously, but now fails). I fixed the problem by using the kernel that comes with CentOS 5.1 instead, and building an initrd for the Xen guest. For more information, see this comment. [...]

  4. Maisiba Peter Says:

    I have managed to install Windows XP Professional on Centos 5 Xen. However I am unable to do the following:

    1. Connect to the network(Lan and Internet). I have configured the LAN but it is unable to communicate with the default gateway.

    2. Cannot be able to see a usb disk.

    Please give an idea on how I can be able to overcome the above.

    Thanks in advance.

  5. jeff Says:

    Hi great article. Using this I have been able to get really close to what i’m trying to do. for some reason I can not log into my instance after I boot it. I also can’t seem to issue the command for ssh. Perhaps the two are related. However this i get when I try to login

    Last login: Sun Jul 13 12:21:00 on xvc0
    audit(1215966112.122:5): avc: denied { entrypoint } for pid=907 comm=”login” path=”/bin/bash” dev=sda1 ino=16167 scontext=root:system_r:hotplug_t:s0-s0:c0.c1023 tcontext=system_u:object_r:file_t:s0 tclass=file
    login: no shell: Permission denied.

    Also I tried to get the pre-made images running and had little to no success. I’d really be interested in how to run debian on a xen system. If you could post something on that it would be great. It seems it’s one thing to run the same virtual environment as the host, but to do a different one does not seem trivial.

    thanks in advance for any help

  6. pk stuff » CentOS 5 Xen images - part 2: installing the Xen guest Says:

    [...] Copied from JP Houde’s blog: [...]

  7. Steve K Says:

    Thank you, very helpful.