Rev 15 | 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 filesFILES_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 copiedDIR_NOT_COPY="/proc \/dev \/livecd \/boot \/afs \/sys \/mnt \/media"# Mount point of the new SL systemNEW=/mnt/harddiskSCRIPTNAME=$( basename $0 )################################################################ Functions###############################################################function usage() {## Usage# ----------------------------------------------------------cat <<EOF$SCRIPTNAME [OPTIONS] -mbr [DEVICE] [PARTITION]Installes LiveCD on PARTITION and the boot loader grubinto the Master Boot Record (MBR) of DEVICE.OPTIONS:-h print this screen-swap [partition] use [partition] as swap-nogrub do not install grub-y answer all questions with yesEOF}################################################################ Main################################################################## are we root?### -----------------------------------------------------------if [ "$( whoami )" != "root" ]; thenecho "Please run this script as root."# exit 1fi### read options from command-line### -----------------------------------------------------------while [ $# -gt 0 ]; docase "$1" in-h)usage; exit;;-swap)shift; SWAP_PART=$1; shift; continue;;-mbr)shift; MBR_DEV=$1; shift; continue;;-nogrub)NOGRUB=$1; shift; continue;;-y)YES=$1; shift; continue;;*)INSTALL_PART=$1; break;;esacdoneecho### test if MBR_DEV is given### -----------------------------------------------------------if [ ! $NOGRUB ]; thenif [ ! $MBR_DEV ]; thenecho "No MBR device defined - where should grub be installed?"echo "Please see '$SCRIPTNAME -h'."; echoexit 1fifi### test if $INSTALL_PART defined and exists### -----------------------------------------------------------if [ ! $INSTALL_PART ]; thenecho "No partition defined for installation"echo "Please see '$SCRIPTNAME -h'."; echoexit 1fi### test if $INSTALL_PART exists### -----------------------------------------------------------fdisk -l | cut -d" " -f1 | grep -q "^${INSTALL_PART}$"if [ "$?" != "0" ]; thenecho "Partition $INSTALL_PART not found! (see 'disk -l')"; echoexit 1fi### 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 ]; thenfdisk -l | cut -d" " -f1 | grep -q "^${SWAP_PART}$"if [ "$?" != "0" ]; thenecho "Swap partition $SWAP_PART not found! (see 'disk -l')"; echoexit 1fifdisk -l | grep "Linux swap" | cut -d" " -f1 | grep -q "^${SWAP_PART}$"if [ "$?" != "0" ]; thenecho "Partition $SWAP_PART is not a Linux swap partition! (see 'disk -l')"; echoexit 1fifi### 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 ]; thenecho -n "Continue (y/N)? "read -n 1 keyecho[ "$key" != "y" ] && exit 0fiecho### format $INSTALL_PART### -----------------------------------------------------------echo -n "Format $INSTALL_PART, please wait ... "mkfs.ext3 -q $INSTALL_PART || exit 1echo "done."; echo### mount $INSTALL_PART### -----------------------------------------------------------echo -n "Try to mount $INSTALL_PART to $NEW ... "mkdir -p $NEWmount $INSTALL_PART $NEW || exit 1echo "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_COPYdo_not_copy=""for not_dir in $DIR_NOT_COPY; doif [ "$not_dir" = "/$dir" ]; thendo_not_copy="yes"breakfidone# do not copy links[ -L /$dir ] && do_not_copy="yes"if [ ! $do_not_copy ]; thenecho -n " * Copy /$dir ... "cp -a /$dir $NEWecho "done."fidoneecho### move /usr/opt back to /opt### -----------------------------------------------------------if [ -d $NEW/usr/opt ]; thenecho -n "Move /opt back ... "mv $NEW/usr/opt $NEW/echo "done."; echofi### create dirs which were not copied### -----------------------------------------------------------for dir in $DIR_NOT_COPY; domkdir $NEW/$dirdonermdir $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}doneecho "done."; echo### do some mounts for chroot $NEW### -----------------------------------------------------------mount --bind /dev $NEW/devmount --bind /sys $NEW/sysmount -t proc proc $NEW/proc### install grub### -----------------------------------------------------------echo "Run grub-install: "; echomkdir -p $NEW/boot/grubgrub-install --root-directory=$NEW $MBR_DEVecho "done."; echo### define kernel version### -----------------------------------------------------------rpm --quiet -q kernel && UP_installed=truerpm --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 ]; thenecho "ERROR: Kernel version could not be determined - installation failed"; echoexit 1fi### convert dev syntax to grub syntax### -----------------------------------------------------------device_map=$NEW/boot/grub/device.mapGRUB_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=trueGRUB_WIN_PART=(hd0,0)### create grub.conf file### -----------------------------------------------------------echo "Create grub.conf"cat > $NEW/boot/grub/grub.conf <<EOF# grub.conf generated by $SCRIPTNAMEdefault=0timeout=5splashimage=$GRUB_ROOT_PART/boot/grub/splash.xpm.gz#hiddenmenuEOFif [ $UP_installed ]; thencat >> $NEW/boot/grub/grub.conf <<EOFtitle Scientific Linux (${KERNEL_VERSION})root $GRUB_ROOT_PARTkernel /boot/vmlinuz-$KERNEL_VERSION ro root=$INSTALL_PARTinitrd /boot/initrd-$KERNEL_VERSION.imgEOFfiif [ $SMP_installed ]; thencat >> $NEW/boot/grub/grub.conf <<EOFtitle Scientific Linux (${KERNEL_VERSION}smp)root $GRUB_ROOT_PARTkernel /boot/vmlinuz-${KERNEL_VERSION}smp ro root=$INSTALL_PARTinitrd /boot/initrd-${KERNEL_VERSION}smp.imgEOFfiif [ $WIN_installed ]; thencat >> $NEW/boot/grub/grub.conf <<EOFtitle Windowsrootnoverify $GRUB_WIN_PARTchainloader +1EOFfichmod 600 $NEW/boot/grub/grub.confln -s ../boot/grub/grub.conf $NEW/etc/grub.confln -s ./grub.conf $NEW/boot/grub/menu.lstecho "done."; echo### install kernel into /boot### -----------------------------------------------------------echo "Install kernel(s)"[ -e /boot/vmlinuz ] && BOOT_DIR=/boot[ -e /boot/boot/vmlinuz ] && BOOT_DIR=/boot/bootif [ ! $BOOT_DIR ]; thenecho "ERROR: No kernel found - installation failed"; echoexit 1ficp -a $BOOT_DIR/vmlinuz $NEW/boot/vmlinuz-${KERNEL_VERSION}cp -a $BOOT_DIR/vmlinuzs $NEW/boot/vmlinuz-${KERNEL_VERSION}smp 2>/dev/nullcp -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 ]; thenchroot $NEW \/sbin/new-kernel-pkg --package kernel --mkinitrd --depmod --install ${KERNEL_VERSION}fiif [ $SMP_installed ]; thenchroot $NEW \/sbin/new-kernel-pkg --package kernel --mkinitrd --depmod --install ${KERNEL_VERSION}smpfiecho "done."; echo### create /etc/fstab### -----------------------------------------------------------cat > $NEW/etc/fstab <<EOF$INSTALL_PART / ext3 defaults 1 1devpts /dev/pts devpts gid=5,mode=620 0 0tmpfs /dev/shm tmpfs defaults 0 0proc /proc proc defaults 0 0sysfs /sys sysfs defaults 0 0EOFif [ $SWAP_PART ]; thenecho "$SWAP_PART swap swap defaults 0 0" >> $NEW/etc/fstabfi### remove LiveCD init.d scripts### -----------------------------------------------------------for file in $LIVECD_INIT_SCRIPTS; dorm -f $NEW/etc/init.d/$filefor n in 0 1 2 3 4 5 6; dorm -f $NEW/etc/rc.d/rc${n}.d/*$filedonedone### restore cronjobs### -----------------------------------------------------------mv /etc/cron_backup/sysstat /etc/cron.d/ 2>/dev/nullmv /etc/cron_backup/00-makewhatis.cron.weekly /etc/cron.weekly/00-makewhatis.cron 2>/dev/nullmv /etc/cron_backup/* /etc/cron.daily/### umount $INSTALL_PART### -----------------------------------------------------------umount $NEW/devumount $NEW/sysumount $NEW/procumount $INSTALL_PART