Creating a CentOS Xen virtual machine image

xen
In this article we will create a CentOS 5.2 virtual machine image that can be used by Xen. We need to bring three pieces together:

  1. The kernel (vmlinuz)
  2. The ramdisk (initrd)
  3. The filesystem

Kernel

The easiest way to create a suitable kernel, ramdisk and filesystem is to use a system with CentOS 5.2 on it with xen enabled:

yum install xen

Change the default kernel to the new kernel with xen support by editing /boot/grub/menu.lst:

default=0

and reboot

Ramdisk

Create the ramdisk by running mkinitrd:

mkinitrd --omit-scsi-modules --with=xennet --with=xenblk --preload=xenblk /root/xen-image/centos-ramdisk.img

Filesystem

The hardest job is getting a filesystem. Create a directory where we will hold the filesystem image:

mkdir /root/xen-image
cd /root/xen-image

Create an image file and make a filesystem on this file:

dd if=/dev/zero of=centos-root.img bs=1M count=1999
mkfs.ext3 centos-root.img

Create a directory where we can mount the newly created image:

mkdir rootdisk
mount -o loop centos-root.img /root/xen-image/rootdisk/
cd rootdisk

Create the /etc directory:

mkdir etc

and edit the file /etc/fstab:

/dev/sda1   /          ext3     defaults         1 1
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

Create some necessary device files:

mkdir dev
for i in console null zero; do /sbin/MAKEDEV -d /root/xen-image/rootdisk/dev -x $i; done

Copy the kernel modules:

mkdir -p lib/modules
cp -a /lib/modules/2.6.18-128.1.6.el5xen/ /root/xen-image/rootdisk/lib/modules/

Create the directory for network scripts:

mkdir -p etc/sysconfig/network-scripts

Edit the file that describes the first network interface, /etc/sysconfig/network-scripts/ifcfg-eth0:

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

Do the same for the second network interface, /etc/sysconfig/network-scripts/ifcfg-eth1:

DEVICE=eth1
BOOTPROTO=dhcp
ONBOOT=yes

And the last network script, etc/sysconfig/network:

NETWORKING=yes
HOSTNAME=centos52
GATEWAY=x.x.x.x

Create the RPM lock directory:

mkdir -p var/lock/rpm

We need to create a specific configuration file for yum, /root/xen-image/yum-xen.conf:

[main]
cachedir=/var/cache/yum
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
distroverpkg=redhat-release
tolerant=1
exactarch=1
obsoletes=1
gpgcheck=0
plugins=1
metadata_expire=1h

[base]
name=CentOS-5.2 - Base
baseurl=http://mirror.centos.org/centos/5.2/os/i386/
enabled=1

[extras]
name=CentOS-5.2 - Extras
baseurl=http://mirror.centos.org/centos/5.2/extras/i386/
enabled=1

[updates]
name=CentOS-5.2 - Updates
baseurl=http://mirror.centos.org/centos/5.2/updates/i386/
enabled=1

[addons]
name=CentOS-5.2 - Addons
baseurl=http://mirror.centos.org/centos/5.2/addons/i386/
enabled=1

Use the configuration file we just edited to install the base system and add the openssh server:

yum -c /root/xen-image/yum-xen.conf --installroot=/root/xen-image/rootdisk -y groupinstall base
yum -c /root/xen-image/yum-xen.conf --installroot=/root/xen-image/rootdisk -y install openssh openssh-server

To set an initial password for root, we chroot into the rootdisk we created:

chroot /root/xen-image/rootdisk

Edit the /etc/passwd file inside the chroot and change the ‘*’ on the first line with an ‘x’:

root:x:0:0:root:/root:/bin/bash

Run pwconv to enable shadow passwords and set the root password:

pwconv
passwd root

Disable TLS:

mv /lib/tls /lib/tls.disabled

Exit from the chroot:

exit

Unmount the rootdisk:

cd /root
umount /root/xen-image/rootdisk

Putting it all together

Create a new configuration file for Xen specific to this image, /etc/xen/centos52:

kernel = "/boot/vmlinuz-2.6.18-128.1.6.el5xen"
ramdisk = "/root/xen-image/centos-ramdisk.img"
name = "centos52"
memory = "256"
disk = [ 'file:/root/xen-image/centos-root.img,sda1,w' ]
root = '/dev/sda1 ro'
vif = [ 'bridge=xenbr0', '']
vcpus=1
on_reboot = 'destroy'
on_crash = 'destroy'

And finally, starting the Xen VM image:

/usr/sbin/xm create -c centos52

6 thoughts on “Creating a CentOS Xen virtual machine image

  1. Permalink  ⋅ Reply

    Arwin Tugade

    March 26, 2010 at 6:02pm

    Oh nice, just checking out the link you posted on the thread that I had created on the Open Eucalyptus forum. I see you improved the doc.

    Arwin

  2. Permalink  ⋅ Reply

    Sohail Aslam

    October 5, 2010 at 3:28pm

    The mkinitrd –omit-scsi-modules… command in the RAMDISK section is missing the kernel version at the end. I believe the last argument on the command line needs to be “2.6.18-128.1.6.el5xen”. Without it, mkinitrd complains that it can not find the kernel modules.

  3. Permalink  ⋅ Reply

    janson

    January 12, 2011 at 10:03am

    thank you for your share! with your help,we complete our job successfully!
    Thank you~

  4. Permalink  ⋅ Reply

    janson

    January 12, 2011 at 10:32am

    but this comond “mkinitrd –omit-scsi-modules –with=xennet –with=xenblk –preload=xenblk /root/xen-image/centos-ramdisk.img “is not useful;
    you should add such as “2.6.18-194.el5xen” to the end of this command.

  5. Permalink  ⋅ Reply

    sangita

    May 18, 2011 at 1:17pm

    Hi,

    Thank you for your tutorial.

    I am trying to create an image for centos 5.4 and I’ve followed your tutorial.

    but I am running in to problems after the creation of rpm lock directory. ie. yum install base and yum install open ssh is unsuccessful . I ignored and proceeded with the next commands and found its not working. As I new to these stuff, I am not able to figure out the issues.

    Also, Is the above procedure creates an entire image of the CentOS? Actually, I am trying to run applications in cloud by creating an image which includes everything(the application, application server and other databases) and trying to run an instance for the image.

    So kindly help me in fixing the issue. Do I need to change in the /etc/fstab or etc/fstab under /root/xen-image/rootdisk.. kindly help me…

  6. Permalink  ⋅ Reply

    amitha

    June 12, 2013 at 12:14pm

    am not able to create virtual machine(DomU) of centos on Dom0(centos). am gettin error in chroot. erros is chroot: cannot run command ‘/bin/bash’ :no such files or directory

Leave a Reply

Your email will not be published. Name and Email fields are required.