Rev 17 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed
#!/bin/bash
#
###############################################################
#
# Installes the LiveCD on local harddisk
#
# Boot LiveCD and run this script as root
#
# Urs Beyerle, PSI
#
###############################################################
###############################################################
# Definitions
###############################################################
# files which should be restored from .ori files
FILES_RESTORE="/etc/init.d/netfs \
/etc/init.d/autofs \
/etc/init.d/halt \
/etc/init.d/network
/etc/init.d/functions \
/etc/rc.d/rc.sysinit \
/etc/sysconfig/afs \
/etc/motd \
/etc/redhat-release \
/etc/rc.d/rc.local \
/etc/rc.d/rc.sysinit"
LIVECD_INIT_SCRIPTS="runfirst \
runveryfirst \
runlast \
kudzu-auto"
# Main directories which should be not be copied
DIR_NOT_COPY="/proc \
/dev \
/livecd \
/boot \
/afs \
/sys \
/mnt \
/media"
# Mount point of the new SL system
NEW=/mnt/harddisk
SCRIPTNAME=$( basename $0 )
###############################################################
# Functions
###############################################################
function usage() {
## Usage
# ----------------------------------------------------------
cat <<EOF
$SCRIPTNAME [OPTIONS] -mbr [DEVICE] [PARTITION]
Installes LiveCD on PARTITION and the boot loader grub
into the Master Boot Record (MBR) of DEVICE.
OPTIONS:
-h print this screen
-swap [partition] use [partition] as swap
-nogrub do not install grub (leave MBR untouched)
-floppy need, if grub should be isntalled on a floopy
-y answer all questions with yes
EOF
}
###############################################################
# Main
###############################################################
### are we root?
### -----------------------------------------------------------
if [ "$( whoami )" != "root" ]; then
echo "Please run this script as root."
# exit 1
fi
### read options from command-line
### -----------------------------------------------------------
while [ $# -gt 0 ]; do
case "$1" in
-h)
usage; exit;;
-swap)
shift; SWAP_PART=$1; shift; continue;;
-mbr)
shift; MBR_DEV=$1; shift; continue;;
-nogrub)
NOGRUB=$1; shift; continue;;
-floppy)
FLOPPY=$1; shift; continue;;
-y)
YES=$1; shift; continue;;
*)
INSTALL_PART=$1; break;;
esac
done
echo
### test if MBR_DEV is given
### -----------------------------------------------------------
if [ ! $NOGRUB ]; then
if [ ! $MBR_DEV ]; then
echo "No MBR device defined."
echo "Please see '$SCRIPTNAME -h'."; echo
exit 1
fi
fi
### test if $INSTALL_PART defined and exists
### -----------------------------------------------------------
if [ ! $INSTALL_PART ]; then
echo "No partition defined for installation"
echo "Please see '$SCRIPTNAME -h'."; echo
exit 1
fi
### test if $INSTALL_PART exists
### -----------------------------------------------------------
fdisk -l | cut -d" " -f1 | grep -q "^${INSTALL_PART}$"
if [ "$?" != "0" ]; then
echo "Partition $INSTALL_PART not found! (see 'disk -l')"; echo
exit 1
fi
### set $INSTALL_DEV (eg. /dev/sda)
### -----------------------------------------------------------
INSTALL_DEV=$( echo "$INSTALL_PART" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' )
INSTALL_PART_NR=$( echo "$INSTALL_PART" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' )
### test if $SWAP_PART exists
### -----------------------------------------------------------
if [ $SWAP_PART ]; then
fdisk -l | cut -d" " -f1 | grep -q "^${SWAP_PART}$"
if [ "$?" != "0" ]; then
echo "Swap partition $SWAP_PART not found! (see 'disk -l')"; echo
exit 1
fi
fdisk -l | grep "Linux swap" | cut -d" " -f1 | grep -q "^${SWAP_PART}$"
if [ "$?" != "0" ]; then
echo "Partition $SWAP_PART is not a Linux swap partition! (see 'disk -l')"; echo
exit 1
fi
fi
### print warning
### -----------------------------------------------------------
echo "-----------------------------------------------------------"
echo " LiveCD will be installed on partition $INSTALL_PART."
[ "$SWAP_PART" ] && echo " Partition $SWAP_PART will be used as swap partition."
echo
[ ! $NOGRUB ] && echo " !! Master Boot Record of $MBR_DEV will be overwritten !!"
echo " !! All data on $INSTALL_PART will be lost !!"
echo "-----------------------------------------------------------"
echo
### continue ?
### -----------------------------------------------------------
if [ ! $YES ]; then
echo -n "Continue (y/N)? "
read key
echo
[ "$key" != "y" ] && exit 0
fi
echo
### format $INSTALL_PART
### -----------------------------------------------------------
echo -n "Format $INSTALL_PART, please wait ... "
mkfs.ext3 -q $INSTALL_PART || exit 1
echo "done."; echo
### mount $INSTALL_PART
### -----------------------------------------------------------
echo -n "Try to mount $INSTALL_PART to $NEW ... "
mkdir -p $NEW
mount $INSTALL_PART $NEW || exit 1
echo "done."; echo
### copy root dirs
### -----------------------------------------------------------
echo "Copy Live System to $INSTALL_PART:"
root_dirs=$( ls / )
for dir in $root_dirs; do
# check if dir is not in $DIR_NOT_COPY
do_not_copy=""
for not_dir in $DIR_NOT_COPY; do
if [ "$not_dir" = "/$dir" ]; then
do_not_copy="yes"
break
fi
done
# do not copy links
[ -L /$dir ] && do_not_copy="yes"
if [ ! $do_not_copy ]; then
echo -n " * Copy /$dir ... "
cp -a /$dir $NEW
echo "done."
fi
done
echo
### move /usr/opt back to /opt
### -----------------------------------------------------------
if [ -d $NEW/usr/opt ]; then
echo -n "Move /opt back ... "
mv $NEW/usr/opt $NEW/
echo "done."; echo
fi
### create dirs which were not copied
### -----------------------------------------------------------
for dir in $DIR_NOT_COPY; do
mkdir $NEW/$dir
done
rmdir $NEW/livecd $NEW/mnt
### copy back original files
### -----------------------------------------------------------
echo -n "Restore original files ... "
for file in $FILES_RESTORE; do
[ -r $NEW/${file}.ori ] && cp -a $NEW/${file}.ori $NEW/${file}
done
echo "done."; echo
### do some mounts for chroot $NEW
### -----------------------------------------------------------
mount --bind /dev $NEW/dev
mount --bind /sys $NEW/sys
mount -t proc proc $NEW/proc
### install grub
### -----------------------------------------------------------
echo "Run grub-install: "; echo
mkdir -p $NEW/boot/grub
if [ $FLOPPY ]; then
grub-install --root-directory=$NEW $MBR_DEV
else
grub-install --no-floppy --root-directory=$NEW $MBR_DEV
fi
echo "done."; echo
### define kernel version
### -----------------------------------------------------------
rpm --quiet -q kernel && UP_installed=true
rpm --quiet -q kernel-smp && SMP_installed=true
[ $UP_installed ] && KERNEL_VERSION=$( rpm -q --qf "%{V}-%{R}" kernel 2>/dev/null )
[ $SMP_installed ] && KERNEL_VERSION=$( rpm -q --qf "%{V}-%{R}" kernel-smp 2>/dev/null )
if [ ! $KERNEL_VERSION ]; then
echo "ERROR: Kernel version could not be determined - installation failed"; echo
exit 1
fi
[ $UP_installed ] && echo "UP kernel found."
[ $SMP_installed ] && echo "SMP kernel found."
echo
### convert dev syntax to grub syntax
### -----------------------------------------------------------
device_map=$NEW/boot/grub/device.map
GRUB_INSTALL_DEV=$( grep $INSTALL_DEV $device_map | awk '{ print $1 }' )
GRUB_ROOT_PART=$( echo "$GRUB_INSTALL_DEV" | sed "s%)$%,`expr $INSTALL_PART_NR - 1`)%" )
### define active Windows partition - to be fixed !!!
### -----------------------------------------------------------
WIN_installed=true
GRUB_WIN_PART=(hd0,0)
### create grub.conf file
### -----------------------------------------------------------
echo "Create grub.conf"
cat > $NEW/boot/grub/grub.conf <<EOF
# grub.conf generated by $SCRIPTNAME
default=0
timeout=5
splashimage=$GRUB_ROOT_PART/boot/grub/splash.xpm.gz
#hiddenmenu
EOF
if [ $UP_installed ]; then
echo "Add entry for UP kernel into grub.conf"
cat >> $NEW/boot/grub/grub.conf <<EOF
title Scientific Linux (${KERNEL_VERSION})
root $GRUB_ROOT_PART
kernel /boot/vmlinuz-$KERNEL_VERSION ro root=$INSTALL_PART
initrd /boot/initrd-$KERNEL_VERSION.img
EOF
fi
if [ $SMP_installed ]; then
echo "Add entry for SMP kernel into grub.conf"
cat >> $NEW/boot/grub/grub.conf <<EOF
title Scientific Linux (${KERNEL_VERSION}smp)
root $GRUB_ROOT_PART
kernel /boot/vmlinuz-${KERNEL_VERSION}smp ro root=$INSTALL_PART
initrd /boot/initrd-${KERNEL_VERSION}smp.img
EOF
fi
if [ $WIN_installed ]; then
echo "Add entry for Windows into grub.conf"
cat >> $NEW/boot/grub/grub.conf <<EOF
title Windows
rootnoverify $GRUB_WIN_PART
chainloader +1
EOF
fi
chmod 600 $NEW/boot/grub/grub.conf
ln -s ../boot/grub/grub.conf $NEW/etc/grub.conf
ln -s ./grub.conf $NEW/boot/grub/menu.lst
echo "done."; echo
### install kernel into /boot
### -----------------------------------------------------------
echo "Install kernel(s)"
[ -e /boot/vmlinuz ] && BOOT_DIR=/boot
[ -e /boot/boot/vmlinuz ] && BOOT_DIR=/boot/boot
if [ ! $BOOT_DIR ]; then
echo "ERROR: No kernel found - installation failed"; echo
exit 1
fi
cp -a $BOOT_DIR/vmlinuz $NEW/boot/vmlinuz-${KERNEL_VERSION}
cp -a $BOOT_DIR/vmlinuzs $NEW/boot/vmlinuz-${KERNEL_VERSION}smp 2>/dev/null
cp -a $BOOT_DIR/System.map-* $NEW/boot/
cp -a $BOOT_DIR/config-* $NEW/boot/
cp -a $BOOT_DIR/grub/splash.xpm.gz $NEW/boot/grub/
echo "done."; echo
### make initrd
### -----------------------------------------------------------
echo "Create initrd(s)"
if [ $UP_installed ]; then
chroot $NEW depmod -a
chroot $NEW \
livecd-mkinitrd $NEW/boot/initrd-${KERNEL_VERSION}.img ${KERNEL_VERSION}
if [ ! -e $NEW/boot/initrd-${KERNEL_VERSION}.img ]; then
echo "ERROR: Failed to create $NEW/boot/initrd-${KERNEL_VERSION}.img"
exit 1
fi
fi
if [ $SMP_installed ]; then
chroot $NEW depmod -a
chroot $NEW \
livecd-mkinitrd $NEW/boot/initrd-${KERNEL_VERSION}smp.img ${KERNEL_VERSION}smp
if [ ! -e $NEW/boot/initrd-${KERNEL_VERSION}smp.img ]; then
echo "ERROR: Failed to create $NEW/boot/initrd-${KERNEL_VERSION}smp.img"
exit 1
fi
fi
echo "done."; echo
### create /etc/fstab
### -----------------------------------------------------------
cat > $NEW/etc/fstab <<EOF
$INSTALL_PART / ext3 defaults 1 1
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
EOF
if [ $SWAP_PART ]; then
echo "$SWAP_PART swap swap defaults 0 0" >> $NEW/etc/fstab
fi
### remove LiveCD init.d scripts
### -----------------------------------------------------------
for file in $LIVECD_INIT_SCRIPTS; do
rm -f $NEW/etc/init.d/$file
for n in 0 1 2 3 4 5 6; do
rm -f $NEW/etc/rc.d/rc${n}.d/*$file
done
done
### restore cronjobs
### -----------------------------------------------------------
mv /etc/cron_backup/sysstat /etc/cron.d/ 2>/dev/null
mv /etc/cron_backup/00-makewhatis.cron.weekly /etc/cron.weekly/00-makewhatis.cron 2>/dev/null
mv /etc/cron_backup/* /etc/cron.daily/
### turn on kudzu again
### -----------------------------------------------------------
chkconfig kudzu on
### umount $INSTALL_PART
### -----------------------------------------------------------
umount $NEW/dev
umount $NEW/sys
umount $NEW/proc
umount $INSTALL_PART