http:// www.jms1.net / xen / xenU-centos.shtml

Xen - Set up CentOS as a xenU client

Because the "host" OS is also running CentOS, installing a child CentOS session is fairly simple. The yum program has an option which makes it do its work totally within a given directory- similar to how the chroot command works, but without actually using chroot. The CentOS yum repository also has a pre-defined meta-package which includes 95% of what needs to be installed on a new system.


Creating the LVM disks

# lvcreate -L 128M -n centos-swap disks
# lvcreate -L 2G -n centos-root disks
# mkswap /dev/disks/centos-swap
# mke2fs -j /dev/disks/centos-root


Configuring yum

We need to use yum to create the files to be stored on the new system, but we need to do it in such a way that the existing system's packages don't affect what gets installed on the new client, and the packages on the new client don't affect what's on the host system.

Yum normally works by reading the /etc/yum.conf file and combining it with any files it finds in the /etc/yum.repos.d directory. When I first tried this, I discovered that no matter what I did, the "other" repositories (extras, contrib, etc.) were being consulted as well, even though they weren't listed in the custom yum.conf file I was using. Eventually I added a line to the yum.conf file which prevented it from looking in the /etc/yum.repos.d directory.

After a bit of trial and error, I finally found a yum.conf which works correctly. Download a copy of the file and save it in root's home directory.

[main]
cachedir=/mnt/var/cache/yum
reposdir=/dev/null
debuglevel=2
logfile=/mnt/var/log/yum.log
pkgpolicy=newest
distroverpkg=centos-release
tolerant=1
exactarch=1
retries=20
obsoletes=1

[base]
name=CentOS-4.2 - Base
baseurl=http://mirror.centos.org/centos/4.2/os/$basearch/

[update]
name=CentOS-4.2 - Updates
baseurl=http://mirror.centos.org/centos/4.2/updates/$basearch/

You will notice the path /mnt at the beginning of two of the lines. This is because when we install the client (below) the client's root filesystem will be mounted as /mnt on the host system.


Installing CentOS

The first step is to mount the new partition and create a few "basic basics" which have to be there in order for pretty much any Linux system to exist.

# mount /dev/disks/centos-root /mnt
# cd /mnt
# mkdir -m 755 dev etc proc
# mknod -m 600 dev/console c 5 1
# mknod -m 666 dev/null c 1 3
# mknod -m 666 dev/zero c 1 5
# nano etc/fstab
The file should contain...
/dev/hda1 / ext3 defaults 1 1 /dev/hda2 swap swap defaults 0 0 none /dev/pts devpts gid=6,mode=620 0 0 none /dev/shm tmpfs defaults 0 0 none /proc proc defaults 0 0 none /sys sysfs defaults 0 0
# chmod 644 etc/fstab
# mount -t proc none /mnt/proc

Once this is done, we can use yum to install a minimal CentOS system on the partition, along with a few individual packages which are needed but are not included in the "Core" meta-package.

# yum -c ~/yum.c42.conf --installroot=/mnt -y groupinstall Core
Go get some coffee, this will take a while.
# yum -c ~/yum.c42.conf --installroot=/mnt -y install yum openssh-clients openssh-server
This one will take a little while to run as well, but not nearly as long as the first one did.

At this point we are about 90% done. All that remains is a bit of manual tweaking.

SELinux tends to complicate things. Disable it.
# nano /mnt/etc/selinux/config
SELINUX=disabled

Configure the network - the client will need an IP address.
# nano /mnt/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.69.255
IPADDR=192.168.69.41
NETMASK=255.255.255.0
NETWORK=192.168.69.0
TYPE=Ethernet
ONBOOT=yes
# chmod 644 /mnt/etc/sysconfig/network-scripts/ifcfg-eth0

# nano /mnt/etc/sysconfig/network
NETWORKING=yes
HOSTNAME=centos.xen.internal
GATEWAY=192.168.69.1
# chmod 644 /mnt/etc/sysconf/network

Make sure DNS resolution will work.
# cp -a /etc/resolv.conf /mnt/etc/

Make sure /etc/hosts resolution works as well.
# nano /mnt/etc/hosts
127.0.0.1 localhost 192.168.69.41 centos.xen.internal centos
# chmod 644 /mnt/etc/hosts

Set up the /etc/inittab file.
# nano /mnt/etc/inittab
Comment out all "getty" lines except tty1
Comment out xdm line
Make sure the "id:" line says 3, not 5, for the default runlevel

The "kudzu" service is a waste of time for a virtual system.
# rm /mnt/etc/rc?.d/*kudzu

Make sure the virtual system doesn't use the TLS libraries.
# mv /mnt/lib/tls /mnt/lib/tls.disabled

And finally, we can un-mount the disk from the host system.

# cd
# umount /mnt/proc
# umount /mnt


Starting the new session

One thing which changed from Xen 2.0.7 to Xen 3 is that the non-privileged "xenU" sessions now use the same kernels as the privileged "xen0" sessions. The RPM installation only installed the one kernel, so we're going to use it for the xenU sessions as well as the xen0 session.

In order to start a xenU session, we need to create a text file which defines the parameters of that session- what kernel it will run, how much memory it will have, the network interface(s), the virtual disks, and so forth.

# cd /etc/xen
# nano centos
kernel = "/boot/vmlinuz-2.6-xen"
ramdisk = "/boot/initrd-2.6-xen.img"
memory = 128
name = "centos"
nics = 1
vif = [ 'mac=c0:ff:ee:00:01:00, bridge=xenbr0' ]
disk = [ 'phy:/dev/disks/centos-root,hda1,w'
, 'phy:/dev/disks/centos-swap,hda2,w' ]
root = "/dev/hda1 ro"

Once you have created the text file (it's actually python code, but who's keeping track) it's time to start the session and let it boot. There are two ways to do this...

If you want to watch the xenU session boot...
# xm create centos -c

If you're not interested in watching and just want it to start up on its own...
# xm create centos

If a session is already started and you need to attach to its console, you can use this command:

# xm console centos

Once you're attached to a session (whether by using "xm console" or by using the "-c" option of "xm create") you can "detach" from that virtual console by pressing CONTROL and ] (the right square bracket key) at the same time. This will not stop the session from running, it simply returns the keyboard and screen to the host machine.

If you do need to shut a session down, the safest way is to go into the session (using xm console), logging in as root, and issuing the standard shutdown command...

# shutdown -h now

Other options include the following commands, whose functions should be fairly obvious...

# xm shutdown centos
# xm reboot centos