Subversion Repositories livecd

Rev

Rev 18 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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 \
16 beyerle@PS 28
	   /etc/rc.d/rc.local \
15 beyerle@PS 29
           /etc/rc.d/rc.sysinit"
30
 
16 beyerle@PS 31
LIVECD_INIT_SCRIPTS="runfirst \
32
                    runveryfirst \
33
                    runlast \
34
                    kudzu-auto"
15 beyerle@PS 35
 
36
# Main directories which should be not be copied
16 beyerle@PS 37
DIR_NOT_COPY="/proc \
38
              /dev \
39
              /livecd \
40
              /boot \
41
              /afs \
42
              /sys \
43
              /mnt \
44
              /media"
15 beyerle@PS 45
 
46
# Mount point of the new SL system
47
NEW=/mnt/harddisk
48
 
21 beyerle@PS 49
# Name of the script
15 beyerle@PS 50
SCRIPTNAME=$( basename $0 )
51
 
16 beyerle@PS 52
 
53
 
21 beyerle@PS 54
 
15 beyerle@PS 55
###############################################################
56
# Functions
57
###############################################################
58
 
59
function usage() {
60
 
61
   ## Usage
62
   # ----------------------------------------------------------
63
 
64
   cat <<EOF
21 beyerle@PS 65
 
66
  $SCRIPTNAME [OPTIONS] -mbr [DEVICE] [PARTITION]
15 beyerle@PS 67
 
21 beyerle@PS 68
    Installes LiveCD on PARTITION and the boot loader grub
69
    into the Master Boot Record (MBR) of DEVICE.
15 beyerle@PS 70
 
21 beyerle@PS 71
  OPTIONS:
15 beyerle@PS 72
 
21 beyerle@PS 73
    -h   --help         print this screen
74
    -swap [partition]   use [partition] as swap
18 beyerle@PS 75
    -nogrub             do not install grub (leave MBR untouched) 
21 beyerle@PS 76
    -floppy             needed, if grub should be installed on floppy
15 beyerle@PS 77
    -y                  answer all questions with yes
78
 
21 beyerle@PS 79
 
80
  Example:
81
 
82
  $SCRIPTNAME -swap /dev/sda3 -mbr /dev/sda /dev/sda2
83
 
84
    Will install LiveCD on /dev/sda2 (= second partition on 
85
    first SATA disk). All data on /dev/sda2 will be deleted.
86
    /dev/sda3 has to be a Linux Swap partion. GRUB will be 
87
    installed in the MBR of /dev/sda (= first SATA disk).
88
 
15 beyerle@PS 89
EOF
90
}
91
 
92
 
21 beyerle@PS 93
function exit_now() {
94
 
95
    local exitcode=$1
96
    umount $INSTALL_PART 2>/dev/null
97
    exit $exitcode
98
}
99
 
100
 
101
 
15 beyerle@PS 102
###############################################################
103
# Main
104
###############################################################
105
 
16 beyerle@PS 106
### are we root?
107
### -----------------------------------------------------------
15 beyerle@PS 108
if [ "$( whoami )" != "root" ]; then
109
    echo "Please run this script as root."
21 beyerle@PS 110
    exit_now 1
15 beyerle@PS 111
fi
112
 
16 beyerle@PS 113
 
15 beyerle@PS 114
### read options from command-line
16 beyerle@PS 115
### -----------------------------------------------------------
15 beyerle@PS 116
while [ $# -gt 0 ]; do
117
 
118
    case "$1" in
119
       -h)
21 beyerle@PS 120
            usage; exit_now;;
121
       --help)
122
            usage; exit_now;;
15 beyerle@PS 123
       -swap)
124
            shift; SWAP_PART=$1; shift; continue;;
125
       -mbr)
126
            shift; MBR_DEV=$1; shift; continue;;
16 beyerle@PS 127
       -nogrub)
128
            NOGRUB=$1; shift; continue;;
18 beyerle@PS 129
       -floppy)
130
            FLOPPY=$1; shift; continue;;
15 beyerle@PS 131
       -y)
132
            YES=$1; shift; continue;;
133
 
134
       *)
135
            INSTALL_PART=$1; break;;
136
    esac
137
 
138
done
139
echo
140
 
16 beyerle@PS 141
 
15 beyerle@PS 142
### test if MBR_DEV is given
16 beyerle@PS 143
### -----------------------------------------------------------
144
if [ ! $NOGRUB ]; then
145
    if [ ! $MBR_DEV ]; then
18 beyerle@PS 146
	echo "No MBR device defined."
16 beyerle@PS 147
	echo "Please see '$SCRIPTNAME -h'."; echo
21 beyerle@PS 148
	exit_now 1
16 beyerle@PS 149
    fi
15 beyerle@PS 150
fi
151
 
16 beyerle@PS 152
 
21 beyerle@PS 153
### test if $INSTALL_PART is defined and exists
16 beyerle@PS 154
### -----------------------------------------------------------
15 beyerle@PS 155
if [ ! $INSTALL_PART ]; then
156
    echo "No partition defined for installation"
157
    echo "Please see '$SCRIPTNAME -h'."; echo
21 beyerle@PS 158
    exit_now 1
15 beyerle@PS 159
fi
160
 
16 beyerle@PS 161
 
15 beyerle@PS 162
### test if $INSTALL_PART exists
16 beyerle@PS 163
### -----------------------------------------------------------
15 beyerle@PS 164
fdisk -l | cut -d" " -f1 | grep -q "^${INSTALL_PART}$"
165
if [ "$?" != "0" ]; then
166
    echo "Partition $INSTALL_PART not found! (see 'disk -l')"; echo
21 beyerle@PS 167
    exit_now 1
15 beyerle@PS 168
fi
169
 
16 beyerle@PS 170
 
15 beyerle@PS 171
### set $INSTALL_DEV (eg. /dev/sda)
16 beyerle@PS 172
### -----------------------------------------------------------
15 beyerle@PS 173
INSTALL_DEV=$( echo "$INSTALL_PART" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' )
174
INSTALL_PART_NR=$( echo "$INSTALL_PART" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' )
175
 
176
 
177
### test if $SWAP_PART exists
16 beyerle@PS 178
### -----------------------------------------------------------
15 beyerle@PS 179
if [ $SWAP_PART ]; then
180
    fdisk -l | cut -d" " -f1 | grep -q "^${SWAP_PART}$"
181
    if [ "$?" != "0" ]; then
182
	echo "Swap partition $SWAP_PART not found! (see 'disk -l')"; echo
21 beyerle@PS 183
	exit_now 1
15 beyerle@PS 184
    fi
185
    fdisk -l | grep "Linux swap" | cut -d" " -f1 | grep -q "^${SWAP_PART}$"
186
    if [ "$?" != "0" ]; then
187
	echo "Partition $SWAP_PART is not a Linux swap partition! (see 'disk -l')"; echo
21 beyerle@PS 188
	exit_now 1
15 beyerle@PS 189
    fi
190
fi
191
 
192
 
193
### print warning
16 beyerle@PS 194
### -----------------------------------------------------------
15 beyerle@PS 195
echo "-----------------------------------------------------------"
196
echo "   LiveCD will be installed on partition $INSTALL_PART."
197
[ "$SWAP_PART" ] && echo " Partition $SWAP_PART will be used as swap partition."
16 beyerle@PS 198
 
15 beyerle@PS 199
echo
16 beyerle@PS 200
[ ! $NOGRUB ] && echo " !! Master Boot Record of $MBR_DEV will be overwritten !!"
201
 
15 beyerle@PS 202
echo "     !! All data on $INSTALL_PART will be lost !!"
203
echo "-----------------------------------------------------------"
204
echo
205
 
206
 
207
### continue ?
16 beyerle@PS 208
### -----------------------------------------------------------
15 beyerle@PS 209
if [ ! $YES ]; then
210
    echo -n "Continue (y/N)? "
18 beyerle@PS 211
    read key
15 beyerle@PS 212
    echo
21 beyerle@PS 213
    [ "$key" != "y" ] && exit_now 0
15 beyerle@PS 214
fi
215
echo
216
 
217
 
21 beyerle@PS 218
### Backup MBR
219
### -----------------------------------------------------------
220
### to do !!!!!!!!!!
221
###
222
 
223
 
15 beyerle@PS 224
### format $INSTALL_PART
16 beyerle@PS 225
### -----------------------------------------------------------
15 beyerle@PS 226
echo -n "Format $INSTALL_PART, please wait ... " 
21 beyerle@PS 227
mkfs.ext3 -q $INSTALL_PART || exit_now 1
15 beyerle@PS 228
echo "done."; echo
229
 
230
 
231
### mount $INSTALL_PART
16 beyerle@PS 232
### -----------------------------------------------------------
15 beyerle@PS 233
echo -n "Try to mount $INSTALL_PART to $NEW ... "
234
mkdir -p $NEW
21 beyerle@PS 235
mount $INSTALL_PART $NEW || exit_now 1
15 beyerle@PS 236
echo "done."; echo
237
 
238
 
239
### copy root dirs
16 beyerle@PS 240
### -----------------------------------------------------------
15 beyerle@PS 241
echo "Copy Live System to $INSTALL_PART:"
242
root_dirs=$( ls / )
243
for dir in $root_dirs; do
244
    # check if dir is not in $DIR_NOT_COPY
245
    do_not_copy=""
246
    for not_dir in $DIR_NOT_COPY; do
247
	if [ "$not_dir" = "/$dir" ]; then 
248
	    do_not_copy="yes"
249
	    break
250
	fi
251
    done
252
    # do not copy links
253
    [ -L /$dir ] && do_not_copy="yes"
254
 
21 beyerle@PS 255
    fail=""
15 beyerle@PS 256
    if [ ! $do_not_copy ]; then
257
	echo -n "  * Copy  /$dir ... "
21 beyerle@PS 258
	cp -a /$dir $NEW || fail=true
15 beyerle@PS 259
	echo "done."
260
    fi
261
done
262
echo
21 beyerle@PS 263
if [ $fail ]; then
264
    echo "ERROR: Not everything was copied to $INSTALL_PART"
265
    exit_now 1
266
fi
15 beyerle@PS 267
 
268
 
16 beyerle@PS 269
### move /usr/opt back to /opt
270
### -----------------------------------------------------------
15 beyerle@PS 271
if [ -d $NEW/usr/opt ]; then
272
    echo -n "Move /opt back ... "
273
    mv $NEW/usr/opt $NEW/
274
    echo "done."; echo
275
fi
276
 
277
 
278
### create dirs which were not copied
16 beyerle@PS 279
### -----------------------------------------------------------
15 beyerle@PS 280
for dir in $DIR_NOT_COPY; do
281
    mkdir $NEW/$dir
282
done
283
rmdir $NEW/livecd $NEW/mnt
284
 
285
 
286
### copy back original files
16 beyerle@PS 287
### -----------------------------------------------------------
15 beyerle@PS 288
echo -n "Restore original files ... " 
289
for file in $FILES_RESTORE; do
290
    [ -r $NEW/${file}.ori ] && cp -a $NEW/${file}.ori $NEW/${file} 
291
done
292
echo "done."; echo
293
 
294
 
21 beyerle@PS 295
### do some mounts for chroot $NEW (no more needed)
16 beyerle@PS 296
### -----------------------------------------------------------
21 beyerle@PS 297
# mount --bind /dev $NEW/dev
298
# mount --bind /sys $NEW/sys 
299
# mount -t proc proc $NEW/proc
15 beyerle@PS 300
 
301
 
16 beyerle@PS 302
### install grub
303
### -----------------------------------------------------------
304
echo "Run grub-install: "; echo
305
mkdir -p $NEW/boot/grub
18 beyerle@PS 306
if [ $FLOPPY ]; then
307
    grub-install --root-directory=$NEW $MBR_DEV
308
else
309
    grub-install --no-floppy --root-directory=$NEW $MBR_DEV
310
fi
15 beyerle@PS 311
echo "done."; echo
312
 
313
 
16 beyerle@PS 314
### define kernel version
315
### -----------------------------------------------------------
316
rpm --quiet -q kernel     && UP_installed=true
317
rpm --quiet -q kernel-smp && SMP_installed=true
318
[ $UP_installed ]  && KERNEL_VERSION=$( rpm -q --qf "%{V}-%{R}" kernel 2>/dev/null )
319
[ $SMP_installed ] && KERNEL_VERSION=$( rpm -q --qf "%{V}-%{R}" kernel-smp  2>/dev/null )
320
if [ ! $KERNEL_VERSION ]; then
321
    echo "ERROR: Kernel version could not be determined - installation failed"; echo
21 beyerle@PS 322
    exit_now 1
16 beyerle@PS 323
fi    
15 beyerle@PS 324
 
18 beyerle@PS 325
 
16 beyerle@PS 326
### convert dev syntax to grub syntax
327
### -----------------------------------------------------------
328
device_map=$NEW/boot/grub/device.map
329
GRUB_INSTALL_DEV=$( grep $INSTALL_DEV $device_map | awk '{ print $1 }' )
330
GRUB_ROOT_PART=$( echo "$GRUB_INSTALL_DEV" | sed "s%)$%,`expr $INSTALL_PART_NR - 1`)%" )
15 beyerle@PS 331
 
332
 
16 beyerle@PS 333
### define active Windows partition -  to be fixed  !!!
334
### -----------------------------------------------------------
335
WIN_installed=true
336
GRUB_WIN_PART=(hd0,0)
15 beyerle@PS 337
 
338
 
339
### create grub.conf file
16 beyerle@PS 340
### -----------------------------------------------------------
21 beyerle@PS 341
echo "Create grub.conf:"
15 beyerle@PS 342
 
343
cat > $NEW/boot/grub/grub.conf <<EOF
344
# grub.conf generated by $SCRIPTNAME
345
default=0
346
timeout=5
347
splashimage=$GRUB_ROOT_PART/boot/grub/splash.xpm.gz
348
#hiddenmenu
16 beyerle@PS 349
EOF
350
 
351
if [ $UP_installed ]; then
21 beyerle@PS 352
    echo " Add entry for UP kernel into grub.conf"
16 beyerle@PS 353
    cat >> $NEW/boot/grub/grub.conf <<EOF
354
title Scientific Linux (${KERNEL_VERSION})
15 beyerle@PS 355
        root $GRUB_ROOT_PART
356
	kernel /boot/vmlinuz-$KERNEL_VERSION ro root=$INSTALL_PART
357
	initrd /boot/initrd-$KERNEL_VERSION.img
16 beyerle@PS 358
EOF
359
fi
360
 
361
if [ $SMP_installed ]; then
21 beyerle@PS 362
    echo " Add entry for SMP kernel into grub.conf"
16 beyerle@PS 363
    cat >> $NEW/boot/grub/grub.conf <<EOF
364
title Scientific Linux (${KERNEL_VERSION}smp)
365
        root $GRUB_ROOT_PART
366
	kernel /boot/vmlinuz-${KERNEL_VERSION}smp ro root=$INSTALL_PART
367
	initrd /boot/initrd-${KERNEL_VERSION}smp.img
368
EOF
369
fi
370
 
371
if [ $WIN_installed ]; then
21 beyerle@PS 372
    echo " Add entry for Windows into grub.conf"
16 beyerle@PS 373
    cat >> $NEW/boot/grub/grub.conf <<EOF
15 beyerle@PS 374
title Windows
375
        rootnoverify $GRUB_WIN_PART
376
        chainloader +1
377
EOF
16 beyerle@PS 378
fi
15 beyerle@PS 379
 
380
chmod 600 $NEW/boot/grub/grub.conf
381
ln -s ../boot/grub/grub.conf $NEW/etc/grub.conf
382
ln -s ./grub.conf $NEW/boot/grub/menu.lst
383
echo "done."; echo
384
 
385
 
16 beyerle@PS 386
### install kernel into /boot
387
### -----------------------------------------------------------
21 beyerle@PS 388
echo "Install kernel(s) ..."
15 beyerle@PS 389
 
16 beyerle@PS 390
[ -e /boot/vmlinuz ]      && BOOT_DIR=/boot
391
[ -e /boot/boot/vmlinuz ] && BOOT_DIR=/boot/boot
15 beyerle@PS 392
 
16 beyerle@PS 393
if [ ! $BOOT_DIR ]; then
394
    echo "ERROR: No kernel found - installation failed"; echo
21 beyerle@PS 395
    exit_now 1
16 beyerle@PS 396
fi
397
 
398
cp -a $BOOT_DIR/vmlinuz            $NEW/boot/vmlinuz-${KERNEL_VERSION}
399
cp -a $BOOT_DIR/vmlinuzs           $NEW/boot/vmlinuz-${KERNEL_VERSION}smp  2>/dev/null
400
cp -a $BOOT_DIR/System.map-*       $NEW/boot/
401
cp -a $BOOT_DIR/config-*           $NEW/boot/
402
cp -a $BOOT_DIR/grub/splash.xpm.gz $NEW/boot/grub/
403
echo "done."; echo
404
 
405
 
21 beyerle@PS 406
### create /etc/fstab
16 beyerle@PS 407
### -----------------------------------------------------------
21 beyerle@PS 408
cat > $NEW/etc/fstab <<EOF
409
$INSTALL_PART         /                    ext3    defaults        1 1
410
devpts            /dev/pts             devpts  gid=5,mode=620  0 0
411
tmpfs             /dev/shm             tmpfs   defaults        0 0
412
proc              /proc                proc    defaults        0 0
413
sysfs             /sys                 sysfs   defaults        0 0
414
EOF
415
if [ $SWAP_PART ]; then
416
    echo "$SWAP_PART         swap                 swap    defaults        0 0" >> $NEW/etc/fstab
417
fi
418
 
419
 
420
### make initrd 
421
### (needs $NEW/etc/fstab to find correct modules for root filesystem !!)
422
### -----------------------------------------------------------
423
echo "Create initrd(s) ..."
16 beyerle@PS 424
if [ $UP_installed ]; then
21 beyerle@PS 425
    depmod -a ${KERNEL_VERSION}
426
    livecd-mkinitrd --fstab=$NEW/etc/fstab \
427
                    $NEW//boot/initrd-${KERNEL_VERSION}.img ${KERNEL_VERSION}
18 beyerle@PS 428
    if [ ! -e $NEW/boot/initrd-${KERNEL_VERSION}.img ]; then
429
	echo "ERROR: Failed to create $NEW/boot/initrd-${KERNEL_VERSION}.img"
21 beyerle@PS 430
	exit_now 1
18 beyerle@PS 431
    fi
16 beyerle@PS 432
fi
433
if [ $SMP_installed ]; then
21 beyerle@PS 434
    depmod -a ${KERNEL_VERSION}smp
435
    livecd-mkinitrd --fstab=$NEW/etc/fstab \
436
                    $NEW/boot/initrd-${KERNEL_VERSION}smp.img ${KERNEL_VERSION}smp
18 beyerle@PS 437
    if [ ! -e $NEW/boot/initrd-${KERNEL_VERSION}smp.img ]; then
438
	echo "ERROR: Failed to create $NEW/boot/initrd-${KERNEL_VERSION}smp.img"
21 beyerle@PS 439
	exit_now 1
18 beyerle@PS 440
    fi
16 beyerle@PS 441
fi
442
echo "done."; echo
443
 
444
 
15 beyerle@PS 445
### remove LiveCD init.d scripts
16 beyerle@PS 446
### -----------------------------------------------------------
447
for file in $LIVECD_INIT_SCRIPTS; do
448
    rm -f $NEW/etc/init.d/$file
449
    for n in 0 1 2 3 4 5 6; do
450
	rm -f $NEW/etc/rc.d/rc${n}.d/*$file
451
    done
15 beyerle@PS 452
done
453
 
16 beyerle@PS 454
 
455
### restore cronjobs
456
### -----------------------------------------------------------
457
mv /etc/cron_backup/sysstat /etc/cron.d/ 2>/dev/null
458
mv /etc/cron_backup/00-makewhatis.cron.weekly /etc/cron.weekly/00-makewhatis.cron 2>/dev/null
459
mv /etc/cron_backup/* /etc/cron.daily/
460
 
461
 
18 beyerle@PS 462
### turn on kudzu again
463
### -----------------------------------------------------------
464
chkconfig kudzu on
465
 
466
 
15 beyerle@PS 467
### umount $INSTALL_PART
16 beyerle@PS 468
### -----------------------------------------------------------
469
umount $INSTALL_PART
15 beyerle@PS 470