This is an installation guide to installing a CentOS5 paravirtualized xen guest from a CentOS5 Dom0. This particular case is a 64bit guest on a 64bit host; steps to installing a 32bit guest on a 64bit host or a 32bit guest on a 32bit host may vary slightly.
You are welcome to “copy and paste” the commands in each section directly but I advise you to read it before you paste it. You may want to change directory paths, names of items, or values on command options.
Also a matter of note, there is the “virt-install” command on CentOS5 which is a _lot_ easier to use. This document is for the semi-advanced user who likes to tinker with stuff like I do. If anything, this document outlines what is required to get a minimal system to boot and operate correctly, and maybe introduce you to a few new commands and concepts.
The one advantage this has over the “virt-install” command is that you get _exactly_ what you want, nothing more, nothing less. Anaconda likes to install other misc things that you may not want and end up having to remove. The “yum” utility only grabs the packages you specify, and their dependencies. You basically get as close to a bare-minimum system that you can possibly have with an RPM distribution with this method of installation.
Enjoy!
==
Start off by making the disk image. In this example, it’ll be 1.5GB
dd if=/dev/zero of=/tmp/CentOS5.x86_64.img bs=1k count=1500k
You can opt to grow disk as you use it rather then create it all at once by replacing the “count=1500k” with “seek=1500k count=1″ but do so at your own performance decrease.
Now to partition the image:
losetup /dev/loop5 /tmp/CentOS5.x86_64.img cat << EOF | fdisk /dev/loop5 n p 1 +100M n p 2 +512M n p 3
w EOF
This gives 100MB to /boot, 512MB to swap, and the rest to /. If you run fdisk on that loop device and print the table, it should look like this:
Device Boot Start End Blocks Id System /dev/loop5p1 1 13 104391 83 Linux /dev/loop5p2 14 76 506047+ 83 Linux /dev/loop5p3 77 191 923737+ 83 Linux
Next is to format the partitions:
kpartx -a /dev/loop5 mkfs.ext3 /dev/mapper/loop5p1 mkswap /dev/mapper/loop5p2 mkfs.ext3 /dev/mapper/loop5p3
Now to mount them into a directory:
mkdir -p /var/distro/CentOS5.x86_64 mount /dev/mapper/loop5p3 /var/distro/CentOS5.x86_64 mkdir -p /var/distro/CentOS5.x86_64/boot mount /dev/mapper/loop5p1 /var/distro/CentOS5.x86_64/boot
Now that it’s mounted, we can start the install process. First we create all the necessary base directories:
mkdir -p /var/distro/CentOS5.x86_64/dev mkdir -p /var/distro/CentOS5.x86_64/etc mkdir -p /var/distro/CentOS5.x86_64/sys mkdir -p /var/distro/CentOS5.x86_64/proc mkdir -p /var/distro/CentOS5.x86_64/var/lib/rpm mkdir -p /var/distro/CentOS5.x86_64/var/log mkdir -p /var/distro/CentOS5.x86_64/var/lib/yum
We need some initial stuff to go into /etc and /dev:
mknod /var/distro/CentOS5.x86_64/dev/null c 1 3 cp /etc/resolv.conf /var/distro/CentOS5.x86_64/etc cat << EOF > /var/distro/CentOS5.x86_64/etc/fstab /dev/xvda3 / ext3 defaults 1 1 /dev/xvda1 /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/xvda2 swap swap defaults 0 0 EOF
Now to mount kernel filesystems:
mount -t sysfs none /var/distro/CentOS5.x86_64/sys mount -t proc none /var/distro/CentOS5.x86_64/proc
Next up is to prepare the environment for a yum install:
wget http://mirror.centos.org/centos/5/os/x86_64/CentOS/centos-release-5-2.el5.centos.x86_64.rpm rpm --root /var/distro/CentOS5.x86_64 --initdb rpm --root /var/distro/CentOS5.x86_64 --nodeps -ivh centos-release-5-2.el5.centos.x86_64.rpm rpm --root /var/distro/CentOS5.x86_64 --import http://mirror.centos.org/centos/5/os/x86_64/RPM-GPG-KEY-CentOS-5
Now the part where you should take a coffee break or something, because it’ll take a little while:
yum --installroot=/var/distro/CentOS5.x86_64 -y install wget bash rootfiles \ yum pam postfix vim-minimal passwd grub kernel-xen openssh \ openssh-server openssh-clients
After that finishes, there is some post configuration stuff that needs doing. In particular, we need to rebuild the initrd because the kernel-xen package doesn’t automatically know to include certain xen front drivers:
rm -f /var/distro/CentOS5.x86_64/boot/initrd-2.6.18-92.1.18.el5xen.img chroot /var/distro/CentOS5.x86_64 mkinitrd --with xennet --with xenblk \ /boot/initrd-2.6.18-92.1.18.el5xen.img 2.6.18-92.1.18.el5xen
Now to install grub, give it a grub.conf, and make appropriate symlinks:
chroot /var/distro/CentOS5.x86_64 grub-install /dev/xvda
cat << EOF > /var/distro/CentOS5.x86_64/boot/grub/grub.conf
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/xvda3
# initrd /initrd-version.img
#boot=/dev/xvda1
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-92.1.18.el5xen)
root (hd0,0)
kernel /vmlinuz-2.6.18-92.1.18.el5xen ro root=/dev/xvda3 console=xvc0
initrd /initrd-2.6.18-92.1.18.el5xen.img
EOF
chroot /var/distro/CentOS5.x86_64 ln -s grub.conf /boot/grub/menu.lst
chroot /var/distro/CentOS5.x86_64 ln -s ../boot/grub/grub.conf /etc/grub.conf
Now to get the console working:
cat /var/distro/CentOS5.x86_64/etc/inittab \ | sed "s|1:2345:respawn:/sbin/mingetty tty1|1:2345:respawn:/sbin/agetty -L 9600 xvc0|" \ > /var/distro/CentOS5.x86_64/etc/inittab.new mv -f /var/distro/CentOS5.x86_64/etc/inittab.new /var/distro/CentOS5.x86_64/etc/inittab echo xvc0 >> /var/distro/CentOS5.x86_64/etc/securetty
Now to setup networking; be sure to change the values here to match your local network setup! Note, that if you need to use dhcp instead of a static address, you’ll need to install the “dhclient” package in the above yum installation:
cat << EOF > /var/distro/CentOS5.x86_64/etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 localhost.localdomain localhost EOF cat << EOF > /var/distro/CentOS5.x86_64/etc/sysconfig/network NETWORKING=yes NETWORKING_IPV6=no HOSTNAME=localhost.localdomain GATEWAY=192.168.1.1 EOF cat << EOF > /var/distro/CentOS5.x86_64/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.1.205 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 EOF
And finally, set a root password:
chroot /var/distro/CentOS5.x86_64 pwconv chroot /var/distro/CentOS5.x86_64 passwd
You’re all setup now. Unmount everything:
umount /var/distro/CentOS5.x86_64/sys umount /var/distro/CentOS5.x86_64/proc umount /var/distro/CentOS5.x86_64/boot umount /var/distro/CentOS5.x86_64 kpartx -d /dev/loop5 losetup -d /dev/loop5
And start the guest (note that this portion will only work if the server you’re doing this on is running the kernel-xen package as a dom0):
xm create /dev/null name=CentOS5.x86_64 bootloader=/usr/bin/pygrub \
vcpus=1 memory=128 vif="" \
disk="tap:aio:/tmp/CentOS5.x86_64.img,xvda,w" -c
You’ll more then likely want to create a configuration file for the domain in /etc/xen, but this will get you going. More information on the xen configuration file (and also installing via the “virt-install” command) can be found at the CentOS Wiki article Creating and installing a CentOS 5 domU instance.