#!/bin/sh
#
#  This script sets up the kernel and fstab for CentOS 5.
#

prefix=$1

#
#  Source our common functions
#
if [ -e /usr/share/xen-tools/common.sh ]; then
    . /usr/share/xen-tools/common.sh
else
    . ./hooks/common.sh
fi

#
# Log our start
#

logMessage Script $0 starting

# Make the console work
sed -i "/Cancelled/ {G;s/$/co:2345:respawn:\/sbin\/mingetty console/;}" ${prefix}/etc/inittab
sed -i "s/^1:2345/#1:2345/" ${prefix}/etc/inittab

# MAKEDEV is needed at this point
chroot ${prefix} ln -s /sbin/MAKEDEV /dev/MAKEDEV
chroot ${prefix} /sbin/MAKEDEV sda sdb sdc sdd

# Create fstab
logMessage Create /etc/fstab
cat > ${prefix}/etc/fstab << EOF
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
none            /dev/pts        devpts  mode=0620       0       0
EOF

for i in `seq 1 $NUMPARTITIONS`; do
  echo -n "/dev/xvde$i " >> ${prefix}/etc/fstab
  eval part=\$PARTITION$i
  if [ -n "`echo $part | grep swap`" ]; then
     echo "none swap ws 0 0" >> ${prefix}/etc/fstab
  else
     echo $part | awk -F: '{print $4,$3,$5,0,1}' >> ${prefix}/etc/fstab
  fi
done

# Install the kernel, grub and perl
chroot ${prefix} yum clean expire-cache
chroot ${prefix} yum -y install kernel-xen grub.x86_64 perl.x86_64

KERNELVERSION=`ls ${prefix}/boot/vmlinuz-* | sed "s#$prefix/boot/vmlinuz-##"`
logMessage "Kernel $KERNELVERSION found"

# Create grub's menu.list
logMessage "Creating /boot/grub/menu.lst"
mkdir -p ${prefix}/boot/grub
cat > ${prefix}/boot/grub/menu.lst << EOF
# WARNING : Don't forget to update this when you upgrade kernel !
# You can also exclude kernel-xen from updates by putting
# exclude=kernel-xen in in [main] in yum.conf

default=0
timeout=5
title CentOS ($KERNELVERSION)
kernel        /boot/vmlinuz-$KERNELVERSION xen_pv_hvm=enable elevator=noop
initrd        /boot/initrd-$KERNELVERSION.img
EOF

#
#  Log our finish
#
logMessage Script $0 finished
