Centos-4 on Xen

From ConShell
Jump to: navigation, search

Concise instructions for installing a CentOS VM on Xen

Before you begin, please read this entire guide and visit the other links to make sure this method (using rpmstrap) is what you prefer. The other way involves the use of yum's --installroot option and rpm's --root option.

Prepare your storage. You can use partitions, LVM volumes or simple file-based images. I choose file-based for the flexibility.

Choosing a suitably large partition (/usr) I linked it into /opt/xen

 mkdir /usr/xen
 ln -s /usr/xen /opt/xen

Prepare file-based image (note: this is a meager setup of 1GB root / and 128MB swap, you'll likely want much more for a real-world VM)

 mkdir -p /opt/xen/domains/vm02
 cd /opt/xen/domains/vm02
 dd if=/dev/zero of=diskimage bs=1024k count=1024
 dd if=/dev/zero of=swapimage bs=1024k count=128
 mkfs.ext3 diskimage
 mkswap swapimage
 mkdir /mnt/disk
 mount -o loop diskimage /mnt/disk

Alternatively, you may want to use LVM to more flexibly manage your virtual disks. In this example /dev/sdb1 is an available (empty) SCSI disk device partition.

 pvcreate /dev/sdb1
 vgcreate vg01 /dev/sdb1
 # Create an 8GB partition for /
 lvcreate -L 8192 -nvol01 vg01 
 # Create a 512M partition for swap
 lvcreate -L 512 -nvol02 vg01
 mkfs.ext3 /dev/vg01/vol01
 mkswap /dev/vg01/vol02
 mkdir /mnt/disk
 mount /dev/vg01/vol01 /mnt/disk

Now setup prepare a few things, an empty fstab to appease some RPMs, a resolv.conf and mounted proc filesystem.

 cd /mnt/disk
 mkdir -p etc
 touch etc/fstab
 cp /etc/resolv.conf etc/
 mkdir proc
 mount -t proc proc ./proc

You will need the rpmstrap program. See http://hackers.progeny.com/~sam/rpmstrap/releases/ The latest release support centos3, centos4 and quite a few other distros such as fedora!

 cd /tmp/
 wget http://rpmstrap.pimpscript.net/releases/rpmstrap-0.5.2.tar.bz2
 tar xjvf rpmstrap-0.5.2.tar.bz2
 cd rpmstrap-0.5.2

In this example, we bootstrap a centos4.3 system into place. It is easy enough to replace centos4.3 with centos4 or even centos3.

important.gif /tmp/centos4-rpms should NOT exist or your downloaded rpms will go into an unexpected subfolder.

info.gif You will need the latest Centos 4.3 rpmstrap file http://svn.samhart.net/rpmstrap/trunk/lib/scripts/. Download the script that do you need.

wget 'http://svn.samhart.net/rpmstrap/trunk/lib/scripts/centos4'
mv centos4 /usr/lib/rpmstrap/scripts/centos43

Now proceed with the rpmstrap process.

 cd /
 mkdir /tmp/centos4-rpms 
 mkdir /var/lib/rpm
 rpmstrap --verbose --download-only centos43 /tmp/centos4-rpms 
 rpm --root /mnt/disk --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-4
 rpmstrap --verbose --local-source /tmp/centos4-rpms/rpmstrap*/ centos43 /mnt/disk

important.gif I get a nasty error here...

 rpmstrap: debug: Installing glibc-common-2.3.4-2.19.i386.rpm to /mnt/disk...
 error: %post(glibc-common-2.3.4-2.19.i386) scriptlet failed, exit status 255

To fix, I disabled selinux as per http://trac.samhart.net/trac/ticket/24 and also put selinux=0 in the grub.conf kernel line ... rebooted then made sure that proc was mounted.

important.gif I get this nasty error here...

 error: can't create transaction lock on /mnt/disk/var/lock/rpm/transaction

To fix, I created the /mnt/disk/var/lock/rpm directory and continued installation with:

 mkdir -p /mnt/disk/var/lock/rpm
 rpm --root /mnt/disk --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-4
 rpmstrap --verbose --local-source /tmp/centos4-rpms/rpmstrap*/ centos43 /mnt/disk

Continue install:

 chroot /mnt/disk /bin/bash
 cd /etc
 nano fstab

Create an fstab file. You'll have to use cat or nano because the rpmstrap centos4 suite does not install vi.

 /dev/sdb1     /     ext3     errors=remount-ro     0     1
 /dev/sdb2     none  swap     sw                    0     0
 proc          /proc proc     defaults              0     0
 none          /dev/pts       devpts gid=5,mode=620 0     0
 none          /dev/shm       tmpfs  defaults       0     0 

Make some necessary devices

 cd /dev
 MAKEDEV null console zero random hda hdb sda sdb

Logout and unmount /mnt/disk

 cd /mnt/disk
 umount proc
 umount /mnt/disk

Now you can setup the VM configuration in /etc/xen/vm02

 kernel = "/boot/vmlinuz-2.6.11-xenU"
 memory = 96
 name = "vm02"
 dhcp = "dhcp"
 disk = [ 'file:/opt/xen/domains/vm02/diskimage,sdb1,w','file:/opt/xen/domains/vm02/swapimage,sdb2,w' ]
 # or if you use LVM
 # disk = [ 'phy:/dev/vg01/vol01,sdb1,w','phy:/dev/vg01/vol02,sdb2,w' ]
 root = "/dev/sdb1 ro"

Start it (-c also puts you into the console so you can watch it boot)

 xm create -c vm02
 > Using config file "/etc/xen/vm02".
 > Started domain vm02, console on port 9602

You can use CTL-] to break out of the console, just like telnet.

Login as root, update the system and set the root password.

 mv /lib/tls /lib/tls.disabled
 rpm --import /usr/share/doc/centos-release-4/RPM-GPG-KEY
 yum update
 yum install passwd
 passwd root