| 15 | beyerle@PS | 1 | #!/bin/bash
 | 
        
           |  |  | 2 | #
 | 
        
           |  |  | 3 | ###############################################################
 | 
        
           |  |  | 4 | #
 | 
        
           |  |  | 5 | # Installes the LiveCD on local harddisk
 | 
        
           |  |  | 6 | # 
 | 
        
           |  |  | 7 | # Boot LiveCD and run this script as root
 | 
        
           |  |  | 8 | #
 | 
        
           |  |  | 9 | # Urs Beyerle, PSI
 | 
        
           |  |  | 10 | #
 | 
        
           |  |  | 11 | ###############################################################
 | 
        
           |  |  | 12 |   | 
        
           |  |  | 13 |   | 
        
           |  |  | 14 | ###############################################################
 | 
        
           |  |  | 15 | # Definitions
 | 
        
           |  |  | 16 | ###############################################################
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | # files which should be restored from .ori files
 | 
        
           |  |  | 19 | FILES_RESTORE="/etc/init.d/netfs \
 | 
        
           |  |  | 20 |            /etc/init.d/autofs \
 | 
        
           |  |  | 21 |            /etc/init.d/halt \
 | 
        
           |  |  | 22 |            /etc/init.d/network
 | 
        
           |  |  | 23 |            /etc/init.d/functions \
 | 
        
           |  |  | 24 |            /etc/rc.d/rc.sysinit \
 | 
        
           |  |  | 25 |            /etc/sysconfig/afs \
 | 
        
           |  |  | 26 |            /etc/motd \
 | 
        
           |  |  | 27 |            /etc/redhat-release \
 | 
        
           |  |  | 28 |            /etc/rc.d/rc.sysinit"
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | LIVECD_INIT_SCRIPS="/etc/init.d/runfirst \
 | 
        
           |  |  | 31 |                     /etc/init.d/runveryfirst \
 | 
        
           |  |  | 32 |                     /etc/init.d/runlast \
 | 
        
           |  |  | 33 |                     /etc/init.d/kudzu-auto"
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | # Main directories which should be not be copied
 | 
        
           |  |  | 36 | DIR_NOT_COPY="/proc /dev /livecd /boot /afs /sys /mnt /media"
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 | # Mount point of the new SL system
 | 
        
           |  |  | 39 | NEW=/mnt/harddisk
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 | SCRIPTNAME=$( basename $0 )
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 | ###############################################################
 | 
        
           |  |  | 45 | # Functions
 | 
        
           |  |  | 46 | ###############################################################
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | function usage() {
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |    ## Usage
 | 
        
           |  |  | 51 |    # ----------------------------------------------------------
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |    cat <<EOF
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |    $SCRIPTNAME [OPTIONS] [PARTITION]
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |    Installes LiveCD on PARTITION.
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |    OPTIONS:
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |     -h                  print this screen
 | 
        
           |  |  | 62 |     -mbr [device]       install grub on MBR of [device]
 | 
        
           |  |  | 63 |     -swap [partition]  	use [partition] as swap
 | 
        
           |  |  | 64 |     -y                  answer all questions with yes
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 | EOF
 | 
        
           |  |  | 67 | }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 | ###############################################################
 | 
        
           |  |  | 71 | # Main
 | 
        
           |  |  | 72 | ###############################################################
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 | ### are we root
 | 
        
           |  |  | 75 | if [ "$( whoami )" != "root" ]; then
 | 
        
           |  |  | 76 |     echo "Please run this script as root."
 | 
        
           |  |  | 77 |     # exit 1
 | 
        
           |  |  | 78 | fi
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 | ### read options from command-line
 | 
        
           |  |  | 81 | while [ $# -gt 0 ]; do
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |     case "$1" in
 | 
        
           |  |  | 84 |        -h)
 | 
        
           |  |  | 85 |             usage; exit;;
 | 
        
           |  |  | 86 |        -swap)
 | 
        
           |  |  | 87 |             shift; SWAP_PART=$1; shift; continue;;
 | 
        
           |  |  | 88 |        -mbr)
 | 
        
           |  |  | 89 |             shift; MBR_DEV=$1; shift; continue;;
 | 
        
           |  |  | 90 |        -y)
 | 
        
           |  |  | 91 |             YES=$1; shift; continue;;
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |        *)
 | 
        
           |  |  | 94 |             INSTALL_PART=$1; break;;
 | 
        
           |  |  | 95 |     esac
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 | done
 | 
        
           |  |  | 98 | echo
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 | ### test if MBR_DEV is given
 | 
        
           |  |  | 101 | if [ ! $MBR_DEV ]; then
 | 
        
           |  |  | 102 |     echo "No MBR device defined - where should grub be installed?"
 | 
        
           |  |  | 103 |     echo "Please see '$SCRIPTNAME -h'."; echo
 | 
        
           |  |  | 104 |     exit 1
 | 
        
           |  |  | 105 | fi
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 | ### test if $INSTALL_PART defined and exists
 | 
        
           |  |  | 108 | if [ ! $INSTALL_PART ]; then
 | 
        
           |  |  | 109 |     echo "No partition defined for installation"
 | 
        
           |  |  | 110 |     echo "Please see '$SCRIPTNAME -h'."; echo
 | 
        
           |  |  | 111 |     exit 1
 | 
        
           |  |  | 112 | fi
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 | ### test if $INSTALL_PART exists
 | 
        
           |  |  | 115 | fdisk -l | cut -d" " -f1 | grep -q "^${INSTALL_PART}$"
 | 
        
           |  |  | 116 | if [ "$?" != "0" ]; then
 | 
        
           |  |  | 117 |     echo "Partition $INSTALL_PART not found! (see 'disk -l')"; echo
 | 
        
           |  |  | 118 |     exit 1
 | 
        
           |  |  | 119 | fi
 | 
        
           |  |  | 120 |   | 
        
           |  |  | 121 | ### set $INSTALL_DEV (eg. /dev/sda)
 | 
        
           |  |  | 122 | INSTALL_DEV=$( echo "$INSTALL_PART" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' )
 | 
        
           |  |  | 123 | INSTALL_PART_NR=$( echo "$INSTALL_PART" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' )
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 | ### test if $SWAP_PART exists
 | 
        
           |  |  | 127 | if [ $SWAP_PART ]; then
 | 
        
           |  |  | 128 |     fdisk -l | cut -d" " -f1 | grep -q "^${SWAP_PART}$"
 | 
        
           |  |  | 129 |     if [ "$?" != "0" ]; then
 | 
        
           |  |  | 130 | 	echo "Swap partition $SWAP_PART not found! (see 'disk -l')"; echo
 | 
        
           |  |  | 131 | 	exit 1
 | 
        
           |  |  | 132 |     fi
 | 
        
           |  |  | 133 |     fdisk -l | grep "Linux swap" | cut -d" " -f1 | grep -q "^${SWAP_PART}$"
 | 
        
           |  |  | 134 |     if [ "$?" != "0" ]; then
 | 
        
           |  |  | 135 | 	echo "Partition $SWAP_PART is not a Linux swap partition! (see 'disk -l')"; echo
 | 
        
           |  |  | 136 | 	exit 1
 | 
        
           |  |  | 137 |     fi
 | 
        
           |  |  | 138 | fi
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 | ### print warning
 | 
        
           |  |  | 142 | echo "-----------------------------------------------------------"
 | 
        
           |  |  | 143 | echo "   LiveCD will be installed on partition $INSTALL_PART."
 | 
        
           |  |  | 144 | [ "$SWAP_PART" ] && echo " Partition $SWAP_PART will be used as swap partition."
 | 
        
           |  |  | 145 | echo
 | 
        
           |  |  | 146 | echo " !! Master Boot Record of $MBR_DEV will be overwritten !!"
 | 
        
           |  |  | 147 | echo "     !! All data on $INSTALL_PART will be lost !!"
 | 
        
           |  |  | 148 | echo "-----------------------------------------------------------"
 | 
        
           |  |  | 149 | echo
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |   | 
        
           |  |  | 152 | ### continue ?
 | 
        
           |  |  | 153 | if [ ! $YES ]; then
 | 
        
           |  |  | 154 |     echo -n "Continue (y/N)? "
 | 
        
           |  |  | 155 |     read -n 1 key
 | 
        
           |  |  | 156 |     echo
 | 
        
           |  |  | 157 |     [ "$key" != "y" ] && exit 0
 | 
        
           |  |  | 158 | fi
 | 
        
           |  |  | 159 | echo
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |   | 
        
           |  |  | 162 | ### format $INSTALL_PART
 | 
        
           |  |  | 163 | echo -n "Format $INSTALL_PART, please wait ... " 
 | 
        
           |  |  | 164 | mkfs.ext3 -q $INSTALL_PART || exit 1
 | 
        
           |  |  | 165 | echo "done."; echo
 | 
        
           |  |  | 166 |   | 
        
           |  |  | 167 |   | 
        
           |  |  | 168 | ### mount $INSTALL_PART
 | 
        
           |  |  | 169 | echo -n "Try to mount $INSTALL_PART to $NEW ... "
 | 
        
           |  |  | 170 | mkdir -p $NEW
 | 
        
           |  |  | 171 | mount $INSTALL_PART $NEW || exit 1
 | 
        
           |  |  | 172 | echo "done."; echo
 | 
        
           |  |  | 173 |   | 
        
           |  |  | 174 |   | 
        
           |  |  | 175 | ### copy root dirs
 | 
        
           |  |  | 176 | echo "Copy Live System to $INSTALL_PART:"
 | 
        
           |  |  | 177 | root_dirs=$( ls / )
 | 
        
           |  |  | 178 | for dir in $root_dirs; do
 | 
        
           |  |  | 179 |     # check if dir is not in $DIR_NOT_COPY
 | 
        
           |  |  | 180 |     do_not_copy=""
 | 
        
           |  |  | 181 |     for not_dir in $DIR_NOT_COPY; do
 | 
        
           |  |  | 182 | 	if [ "$not_dir" = "/$dir" ]; then 
 | 
        
           |  |  | 183 | 	    do_not_copy="yes"
 | 
        
           |  |  | 184 | 	    break
 | 
        
           |  |  | 185 | 	fi
 | 
        
           |  |  | 186 |     done
 | 
        
           |  |  | 187 |     # do not copy links
 | 
        
           |  |  | 188 |     [ -L /$dir ] && do_not_copy="yes"
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 |     if [ ! $do_not_copy ]; then
 | 
        
           |  |  | 191 | 	echo -n "  * Copy  /$dir ... "
 | 
        
           |  |  | 192 | 	cp -a /$dir $NEW
 | 
        
           |  |  | 193 | 	echo "done."
 | 
        
           |  |  | 194 |     fi
 | 
        
           |  |  | 195 | done
 | 
        
           |  |  | 196 | echo
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 |   | 
        
           |  |  | 199 | ### move /usr/opt /opt
 | 
        
           |  |  | 200 | if [ -d $NEW/usr/opt ]; then
 | 
        
           |  |  | 201 |     echo -n "Move /opt back ... "
 | 
        
           |  |  | 202 |     mv $NEW/usr/opt $NEW/
 | 
        
           |  |  | 203 |     echo "done."; echo
 | 
        
           |  |  | 204 | fi
 | 
        
           |  |  | 205 |   | 
        
           |  |  | 206 |   | 
        
           |  |  | 207 | ### create dirs which were not copied
 | 
        
           |  |  | 208 | for dir in $DIR_NOT_COPY; do
 | 
        
           |  |  | 209 |     mkdir $NEW/$dir
 | 
        
           |  |  | 210 | done
 | 
        
           |  |  | 211 | rmdir $NEW/livecd $NEW/mnt
 | 
        
           |  |  | 212 |   | 
        
           |  |  | 213 |   | 
        
           |  |  | 214 | ### copy back original files
 | 
        
           |  |  | 215 | echo -n "Restore original files ... " 
 | 
        
           |  |  | 216 | for file in $FILES_RESTORE; do
 | 
        
           |  |  | 217 |     [ -r $NEW/${file}.ori ] && cp -a $NEW/${file}.ori $NEW/${file} 
 | 
        
           |  |  | 218 | done
 | 
        
           |  |  | 219 | echo "done."; echo
 | 
        
           |  |  | 220 |   | 
        
           |  |  | 221 |   | 
        
           |  |  | 222 | ### some mounts for $NEW
 | 
        
           |  |  | 223 | # mount --bind /dev $NEW/dev
 | 
        
           |  |  | 224 | # mount null -t proc $NEW/proc
 | 
        
           |  |  | 225 | # mount --bind /sys $NEW/sys 
 | 
        
           |  |  | 226 | # mount -t proc proc $NEW/proc
 | 
        
           |  |  | 227 |   | 
        
           |  |  | 228 |   | 
        
           |  |  | 229 | ### re-install grub
 | 
        
           |  |  | 230 | echo "Re-install grub:"; echo
 | 
        
           |  |  | 231 | rpm --root $NEW -e --nodeps grub
 | 
        
           |  |  | 232 | yum -y --installroot=$NEW install grub
 | 
        
           |  |  | 233 | grub-install --root-directory=$NEW $MBR_DEV
 | 
        
           |  |  | 234 | echo "done."; echo
 | 
        
           |  |  | 235 |   | 
        
           |  |  | 236 |   | 
        
           |  |  | 237 | ### re-install kernel
 | 
        
           |  |  | 238 | echo "Re-install kenel(s):"; echo
 | 
        
           |  |  | 239 |   | 
        
           |  |  | 240 | rpm --quiet --root $NEW -q kernel-smp && smp_installed="yes"
 | 
        
           |  |  | 241 |   | 
        
           |  |  | 242 | rpm --root $NEW -e --nodeps kernel
 | 
        
           |  |  | 243 | yum -y --installroot=$NEW install kernel
 | 
        
           |  |  | 244 |   | 
        
           |  |  | 245 | if [ $smp_installed ]; then
 | 
        
           |  |  | 246 |     rpm --root $NEW -e --nodeps kernel-smp
 | 
        
           |  |  | 247 |     yum -y --installroot=$NEW install kernel kernel-smp 
 | 
        
           |  |  | 248 | fi
 | 
        
           |  |  | 249 | echo "done."; echo
 | 
        
           |  |  | 250 |   | 
        
           |  |  | 251 |   | 
        
           |  |  | 252 | ### create grub.conf file
 | 
        
           |  |  | 253 | echo "Create grub.conf"
 | 
        
           |  |  | 254 | KERNEL_VERSION=$( rpm -q --qf "%{V}-%{R}" kernel )
 | 
        
           |  |  | 255 | TITLE="Scientific Linux (${KERNEL_VERSION})"
 | 
        
           |  |  | 256 |   | 
        
           |  |  | 257 | # convert dev syntax to grub syntax
 | 
        
           |  |  | 258 | device_map=$NEW/boot/grub/device.map
 | 
        
           |  |  | 259 | GRUB_INSTALL_DEV=$( grep $INSTALL_DEV $device_map | awk '{ print $1 }' )
 | 
        
           |  |  | 260 | GRUB_ROOT_PART=$( echo "$GRUB_INSTALL_DEV" | sed "s%)$%,`expr $INSTALL_PART_NR - 1`)%" )
 | 
        
           |  |  | 261 |   | 
        
           |  |  | 262 | GRUB_WIN_PART=(hd0,0)
 | 
        
           |  |  | 263 |   | 
        
           |  |  | 264 | cat > $NEW/boot/grub/grub.conf <<EOF
 | 
        
           |  |  | 265 | # grub.conf generated by $SCRIPTNAME
 | 
        
           |  |  | 266 | default=0
 | 
        
           |  |  | 267 | timeout=5
 | 
        
           |  |  | 268 | splashimage=$GRUB_ROOT_PART/boot/grub/splash.xpm.gz
 | 
        
           |  |  | 269 | #hiddenmenu
 | 
        
           |  |  | 270 | title $TITLE
 | 
        
           |  |  | 271 |         root $GRUB_ROOT_PART
 | 
        
           |  |  | 272 | 	kernel /boot/vmlinuz-$KERNEL_VERSION ro root=$INSTALL_PART
 | 
        
           |  |  | 273 | 	initrd /boot/initrd-$KERNEL_VERSION.img
 | 
        
           |  |  | 274 | title Windows
 | 
        
           |  |  | 275 |         rootnoverify $GRUB_WIN_PART
 | 
        
           |  |  | 276 |         chainloader +1
 | 
        
           |  |  | 277 | EOF
 | 
        
           |  |  | 278 |   | 
        
           |  |  | 279 | chmod 600 $NEW/boot/grub/grub.conf
 | 
        
           |  |  | 280 | ln -s ../boot/grub/grub.conf $NEW/etc/grub.conf
 | 
        
           |  |  | 281 | ln -s ./grub.conf $NEW/boot/grub/menu.lst
 | 
        
           |  |  | 282 | echo "done."; echo
 | 
        
           |  |  | 283 |   | 
        
           |  |  | 284 | grub-install --root-directory=$NEW $MBR_DEV
 | 
        
           |  |  | 285 |   | 
        
           |  |  | 286 |   | 
        
           |  |  | 287 |   | 
        
           |  |  | 288 | ### create /etc/fstab
 | 
        
           |  |  | 289 | cat > $NEW/etc/fstab <<EOF
 | 
        
           |  |  | 290 | $INSTALL_PART         /                    ext3    defaults        1 1
 | 
        
           |  |  | 291 | devpts            /dev/pts             devpts  gid=5,mode=620  0 0
 | 
        
           |  |  | 292 | tmpfs             /dev/shm             tmpfs   defaults        0 0
 | 
        
           |  |  | 293 | proc              /proc                proc    defaults        0 0
 | 
        
           |  |  | 294 | sysfs             /sys                 sysfs   defaults        0 0
 | 
        
           |  |  | 295 | EOF
 | 
        
           |  |  | 296 |   | 
        
           |  |  | 297 | if [ $SWAP_PART ]; then
 | 
        
           |  |  | 298 |     echo "$SWAP_PART         swap                 swap    defaults        0 0" >> $NEW/etc/fstab
 | 
        
           |  |  | 299 | fi
 | 
        
           |  |  | 300 |   | 
        
           |  |  | 301 |   | 
        
           |  |  | 302 | ### remove LiveCD init.d scripts
 | 
        
           |  |  | 303 | for file in $LIVECD_INIT_SCRIPS; do
 | 
        
           |  |  | 304 |     rm -f $NEW/$file
 | 
        
           |  |  | 305 | done
 | 
        
           |  |  | 306 |   | 
        
           |  |  | 307 | ### umount $INSTALL_PART
 | 
        
           |  |  | 308 | # umount $INSTALL_PART
 | 
        
           |  |  | 309 |   |