Building a Xen Virtual Guest Filesystem on a Disk Image (Cloning Host System)

From Virtuatopia
Jump to: navigation, search
PreviousTable of ContentsNext
Adding USB Devices to a Xen HVM domainU GuestBuilding a Xen Virtual Guest Filesystem on a Physical Disk Partition (Cloning Host System)


Purchase and download the full PDF and ePub versions of this Xen eBook for only $8.99 Add to Cart


In this chapter of Xen Virtualization Essentials we will look at the steps involved in configuring Xen to boot a paravirtualized guest operating system from a disk image file. The approach taken in this chapter involves cloning the root filesystem of the host operating system. This is just one of a number of ways of building a root filesystem for a Xen guest. Other methods are covered in later chapters.




Xen Requirements

In order to boot a Xen guest domain a number of criteria need to be met. Firstly, a Xen enabled host operating system must be installed on the physical hardware and the Xen daemon (xend) must be running. Secondly, a suitable Xen aware kernel and initial RAM Disk (initrd) for the guest domain must be available and referenced in the Xen configuration file. Thirdly a root filesystem containing the pertinent Linux directories, files libraries and tools necessary for the operating system to boot and function must be available. Finally the guest operating system will need access to some swap space.

In this chapter we assume that the first of the above criteria has been met (in as much as you have the Xen environment for your chosen Linux distribution installed and functioning on your hardware platform). Given this assumption, this chapter will cover in detail the steps necessary to create disk images to store the root filesystem and swap space, populate the root filesystem with the necessary elements of a Linux distribution. Once these steps are complete we will then work through the creation of a Xen configuration file with references to the root and swap disk images, and also appropriate kernel and initial RAM Disk images such that the guest operating system will boot as a Xen guest domain.


Creating a Disk Image for the Root Filesystem

The first step in configuring our Xen guest operating system is to create a disk image which will contain the guest's root filesystem. This is achieved using the dd command. The following example uses dd to create a 6Gb disk image called XenGuest1.img:

dd if=/dev/zero of=XenGuest1.img bs=1024k seek=6144 count=0

Having generated a blank disk image the next task is to create a filesystem on it so that it able to store the files and directories that comprise the root filesystem. The following example creates an ext3 type filesystem on our XenGuest1.img disk image. Note that mkfs identifies the disk as an image file rather than a block device and seeks confirmation that a filesystem is to be created. When prompted simply enter y:

mkfs -t ext3 XenGuest1.img

mke2fs 1.39 (29-May-2006)
XenGuest1.img is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
786432 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

At this point we have a disk image to use for the root filesystem of our guest operating system. The next step is to create a disk image to act as swap space for the system.

Creating a Swap Space Disk Image

In order to create a disk image to act as swap for our Xen guest operating system we once again use the dd command. In this instance we will create a 1Gb swap disk image named XenGuest1.swap:

dd if=/dev/zero of=XenGuest1.swap bs=1024k seek=1024 count=0

Having created an image file to act as the swap device we need to format this image as swap space using the mkswap command as follows:

 mkswap XenGuest1.swap
Setting up swapspace version 1, size = 1073737 kB

With the root filesystem and swap space images created it is now necessary to install the mandatory files on the root filesystem in order to make the system functional when it boots.

Cloning the Host OS on the Guest Domain

One of the easiest ways to install the necessary files on the guest OS root filesystem is to simply copy the files from the host operating system. Before doing so, however, it is important to keep in mind a few points. Firstly, the following steps will copy everything from the host operating system to the guest root filesystem. This may well be much more than is actually necessary. For example, you may need a minimal Linux installation for your guest OS. If the host system has graphical desktop environments, office productivity applications and other packages not needed by the guest OS then cloning the host environment may not be the best approach. If, on the other hand, you are confident that everything on your host operating system will be of use in the guest system, or are unconcerned about disk space then this approach will work well for you.

Before any files can be copied onto the root filesystem disk image it must first be mounted. Disk images are mounted using the Linux loopback interface and require a pre-existing mount point. In the following example a mount point is created at /tmp/loop:

mkdir /tmp/loop
mount -o loop XenGuest1.img /tmp/loop

Having created the mount point and mounted our root filesystem image the next step is to copy the relevant files from the host operating system to guest filesystem image. Execute the following command to copy the required directories and files to the root filesystem (be very careful to substitute the correct destination path if you mounted the filesystem somewhere other than /tmp/loop):

cp -ax /{bin,dev,etc,lib,root,sbin,usr,var} /tmp/loop

The amount of time the copy process takes to complete will depend to a large extent on the speed of physical disk drive and the number of additional applications and packages installed in the root partition of the host operating system.

Once the copy process is completed a number of additional directories need to be created (once again the following example assumes the image has been mounted at /tmp/loop):

mkdir /tmp/loop/{home,proc,opt,sys,tmp}

The tmp directory created above must have read/write access for all users. To ensure this the chmod command needs to be run:

chmod 777 /tmp/loop/tmp

Creating a Xen Configuration File

Now that we have disk images for the root filesystem and the swap space, and have populated the root filesystem with the necessary system files the next step is to create a Xen configuration file. In this file we need to specify the kernel and initial RAM Disk image files together with directives to map the disk images to devices within the guest OS.

For the purposes of this example we will load the initial RAM disk and kernel images from the host operating system. These are typically located in the /boot directory. The kernel file typically begins with the name vmlinuz and the initial RAM disk begins with initrd. These two images are defined in the Xen configuration file using the kernel = and ramdisk = directives. For example:

kernel = "/boot/vmlinuz-2.6.18-53.1.14.el5xen"
ramdisk = "/boot/initrd-2.6.18-53.1.14.el5xen.img"

Note that using the default host system initial RAM disk can cause problems on some Linux distributions. If the system fails to boot it may be necessary to generate an init RAM disk without SCSI modules. Details on this and solutions to other Xen problems can be found at Linuxtopia.org

Next, the amount of memory to be allocated to the guest system is defined using the memory = directive. The following assigns 512Mb to the guest:

memory = 512

Each guest domain needs to have a name by which it can be referenced when using the Xen management tools. This is assigned using name = :

name = "XenGuest1"

Now we specify network configuration options. The following lines indicate to Xen that the IP address of the system will be assigned using DHCP and the empty vif line instructs Xen to use the default values for the network interface configuration:

vif = [ '' ]
dhcp = "dhcp"

Finally the disk = directive is used to map the disk images we have created to guest operating system devices. The following lines map the root and swap images to /dev/xvda1 and /dev/xvda2 respectively and define the root filesystem as /dev/xvda:

disk = ['tap:aio:/xen/XenGuest1.img,xvda1,w', 'tap:aio:/xen/XenGuest1.swap,xvda2,w']
root = "/dev/xvda1 ro"

Note that these device mappings will be needed when the fstab file is configured later in this chapter.

Bringing all these configuration lines together should result in the following configuration file which we will name XenGuest1.cfg:

kernel = "/boot/vmlinuz-2.6.18-53.1.14.el5xen"
ramdisk = "/boot/initrd-2.6.18-53.1.14.el5xen.img"
memory = 512
name = "XenGuest1"
vif = [ '' ]
dhcp = "dhcp"
disk = ['tap:aio:/xen/XenGuest1.img,xvda1,w', 'tap:aio:/xen/XenGuest1.swap,xvda2,w']
root = "/dev/xvda1 ro"

Configuring System Files for the Guest Operating System

It is important to keep in mind that at this point in the process we have copied all of the system files from our host operating system onto the disk image for our guest operating system. It is vital at this point, therefore, that we change any system configuration files to meet the requirements of our guest domain. Typical settings that will need to be changed are:

  • /etc/fstab - This file contains the mappings between physical and network devices and filesystems. This will need to be changed to reflect the disk configuration used by the guest system.
  • /etc/passwd - Contains password information for all user and application accounts on the host system. It is important to remove the entries for any accounts which will not be required on the guest system.
  • /etc/group - This file contains information about user and application groups and, as with the passwd file, should be modified to remove any groups not required in the guest system.
  • /etc/hosts - Contains information about the local host and other hosts on the network. Be sure to change the name of the local host to match the name to be used by the guest operating system.
  • /etc/sysconfig/network or /etc/network/interfaces - Depending on which Linux distribution you are using, one of these files will contain networking information such as the host name and possibly a static IP address. Be sure to modify this file to remove any conflicts between the host and guest operating systems.
  • /etc/exports - Contains information about any filesystems local to the host operating system which are exported for NFS access to remote systems. This should be modified to reflect any exports needed for the guest OS.

Modifying /etc/fstab for the Guest System

In the preceding section we talked about the system configuration files which may need to be modified in the root filesystem of the guest operating system. One file which needs particular attention is the /etc/fstab file. The file currently present in the guest root filesystem is a direct replica of the file used by the host operating system. It is almost certain that this file will not be configured to work with the disk represented by our root filesystem and swap images. The following shows a typical file from a CentOS, Red Hat Enterprise Linux or Fedora host operating system:

/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0

Clearly, this doesn't match the disk configuration we have set up using disk images for the root filesystem and the swap device. The guest OS copy of the fstab file needs to be modified to reflect the device mappings in the Xen configuration file created above (recall that the root filesystem was mapped to /dev/xvda1 and swap to /dev/xvda2). For example:

/dev/xvda1               /                       ext3    defaults 1 1
/dev/xvda2               none                    swap    sw       0 0
none                    /dev/pts                devpts  gid=5,mode=620 0 0
none                    /dev/shm                tmpfs   defaults 0 0
none                    /proc                   proc    defaults 0 0
none                    /sys                    sysfs   defaults 0 0

Booting the Guest OS

Now all that remains is to boot the guest operating system. before issuing the command to do so it is important to note that the system may not boot for a number of reasons.

If the system fails to boot be sure to read the Xen Guest Boot Problems section at the end of this chapter.

With the set up and configuration complete we are now ready to start up the guest OS. This is achieved using the xm create command:

xm create XenGuest1.cfg -c

The -c flag above instructs Xen to attach a console to the guest system so that we see output as the system boots.

Xen Guest Boot Problems

A number of different problems can occur during the boot process of the Xen guest system. Solutions to a number of Xen problems are available on Virtuatopia:


Purchase and download the full PDF and ePub versions of this Xen eBook for only $8.99 Add to Cart



PreviousTable of ContentsNext
Adding USB Devices to a Xen HVM domainU GuestBuilding a Xen Virtual Guest Filesystem on a Physical Disk Partition (Cloning Host System)