#!/bin/ksh

# sigbuildmini
#
# This script will automatically
# duplicate the operational boot (/boot)  and root (/)
# partitions to provide another linux installation called
# miniroot.

MINI_BOOT="/dev/hda3"
MINI_BOOT_MNT="/mnt/hda3"
MINI_ROOT="/dev/hda2"
MINI_ROOT_MNT="/mnt/hda2"
LILOCONF="/etc/lilo.conf"

echo "Unmounting ${MINI_BOOT} and ${MINI_ROOT}..."
umount ${MINI_BOOT} 2>&-
umount ${MINI_ROOT} 2>&-

echo "Making empty file systems on ${MINI_BOOT} and ${MINI_ROOT}..."
mkfs -t ext3 ${MINI_BOOT}
mkfs -t ext3 ${MINI_ROOT}

echo "Creating directories as mount points if they have not already been created..."
mkdir ${MINI_BOOT_MNT} 2>&-
mkdir ${MINI_ROOT_MNT} 2>&-

echo "Mounting ${MINI_BOOT} and ${MINI_ROOT}..."
mount ${MINI_BOOT} ${MINI_BOOT_MNT}
mount ${MINI_ROOT} ${MINI_ROOT_MNT}

echo "Copying /boot to ${MINI_BOOT}..."
( cd /boot ; tar -cf - . ) | \
  ( cd ${MINI_BOOT_MNT} ; tar -xpf - )

echo "Copying / to ${MINI_ROOT}..."
( cd / ; tar -cf - --exclude proc --exclude mnt --exclude nfs --exclude tmp . ) | \
  ( cd ${MINI_ROOT_MNT} ; tar -xpf - )

echo "Creating system files and mount points..."
mkdir -p ${MINI_ROOT_MNT}/mnt/floppy
mkdir -p ${MINI_ROOT_MNT}/mnt/cdrom
mkdir    ${MINI_ROOT_MNT}/proc
mkdir    ${MINI_ROOT_MNT}/tmp
mv       ${MINI_ROOT_MNT}${LILOCONF} ${MINI_ROOT_MNT}${LILOCONF}-old

mv ${MINI_ROOT_MNT}/etc/fstab ${MINI_ROOT_MNT}/etc/fstab.orig

cat - <<EOF > ${MINI_ROOT_MNT}/etc/fstab
${MINI_ROOT}            /                       ext3    defaults        1 1
${MINI_BOOT}            /boot                   ext3    defaults        1 2
EOF

cat ${MINI_ROOT_MNT}/etc/fstab.orig | \
  egrep -v "/[     ]*ext|/boot[     ]*ext" >> ${MINI_ROOT_MNT}/etc/fstab

# Check for an original version of lilo.conf, and create it now if it
# does not exist already.  Then delete the current lilo.conf and
# rebuild it using the original version plus our miniroot additions.
#
echo "Modifying lilo.conf and running lilo..."

if [ ! -r ${LILOCONF}-orig ] ; then mv ${LILOCONF} ${LILOCONF}-orig ; fi
rm -f ${LILOCONF} ; cp ${LILOCONF}-orig ${LILOCONF}

cat ${LILOCONF} | sed '/read-only/{x;s/.*/	vga=773/;x;G;}'

KERNELS="`cat ${LILOCONF} | grep 'image=/boot/' | sed 's+^.*boot/++'`"
 APPEND="`cat ${LILOCONF} | grep -m 1 "append=" | sed 's+ *root=LABEL=\/++'`"

for KERNEL in ${KERNELS} ; do
  echo "\nimage=${MINI_BOOT_MNT}/${KERNEL}\n" \
       "	label=mini-${KERNEL##*.}\n" \
       "	initrd=${MINI_BOOT_MNT}/initrd-${KERNEL#*-}.img\n" \
       "	read-only\n" \
       "	vga=773\n" \
       "	root=${MINI_ROOT}\n" \
       "${APPEND}" >> ${LILOCONF}
done

lilo -v

# Unmount the miniroot partitions so that users do not accidently
# wander into them.
#
umount ${MINI_BOOT}  2>&-
umount ${MINI_ROOT}  2>&-

echo "The paritions and mount points for your miniroot will be:"
echo "******"
echo "${MINI_BOOT} for miniroot's boot partition"
echo "${MINI_ROOT} for miniroot's root partition"
echo "${MINI_ROOT_MNT} will be the mount point for the root partition"
echo "${MINI_BOOT_MNT} will be the mount point for the boot partition"
echo "******"
