Subversion Repositories livecd

Rev

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