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.sysinit"
LIVECD_INIT_SCRIPS="/etc/init.d/runfirst \
/etc/init.d/runveryfirst \
/etc/init.d/runlast \
/etc/init.d/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] [PARTITION]
Installes LiveCD on PARTITION.
OPTIONS:
-h print this screen
-mbr [device] install grub on MBR of [device]
-swap [partition] use [partition] as swap
-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;;
-y)
YES=$1; shift; continue;;
*)
INSTALL_PART=$1; break;;
esac
done
echo
### test if MBR_DEV is given
if [ ! $MBR_DEV ]; then
echo "No MBR device defined - where should grub be installed?"
echo "Please see '$SCRIPTNAME -h'."; echo
exit 1
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
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 -n 1 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 /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
### some mounts for $NEW
# mount --bind /dev $NEW/dev
# mount null -t proc $NEW/proc
# mount --bind /sys $NEW/sys
# mount -t proc proc $NEW/proc
### re-install grub
echo "Re-install grub:"; echo
rpm --root $NEW -e --nodeps grub
yum -y --installroot=$NEW install grub
grub-install --root-directory=$NEW $MBR_DEV
echo "done."; echo
### re-install kernel
echo "Re-install kenel(s):"; echo
rpm --quiet --root $NEW -q kernel-smp && smp_installed="yes"
rpm --root $NEW -e --nodeps kernel
yum -y --installroot=$NEW install kernel
if [ $smp_installed ]; then
rpm --root $NEW -e --nodeps kernel-smp
yum -y --installroot=$NEW install kernel kernel-smp
fi
echo "done."; echo
### create grub.conf file
echo "Create grub.conf"
KERNEL_VERSION=$( rpm -q --qf "%{V}-%{R}" kernel )
TITLE="Scientific Linux (${KERNEL_VERSION})"
# 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`)%" )
GRUB_WIN_PART=(hd0,0)
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
title $TITLE
root $GRUB_ROOT_PART
kernel /boot/vmlinuz-$KERNEL_VERSION ro root=$INSTALL_PART
initrd /boot/initrd-$KERNEL_VERSION.img
title Windows
rootnoverify $GRUB_WIN_PART
chainloader +1
EOF
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
grub-install --root-directory=$NEW $MBR_DEV
### 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_SCRIPS; do
rm -f $NEW/$file
done
### umount $INSTALL_PART
# umount $INSTALL_PART