-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkrescue
executable file
·711 lines (623 loc) · 15 KB
/
mkrescue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#!/bin/bash
#
# mkrescue
#
# 30-Oct-2001 original version 1.0
#
# Revision history:
# 09-Apr-2002 John Coffman modify for lilo 22.3 1.1
# 09-Jun-2002 John Coffman get keyboard translation table 1.2
# (suggested by Qing Liu)
# 07-May-2003 John Coffman add nowarn for lilo 22.5.2 1.3
# 13-May-2003 John Coffman use default image, add append= 1.4
# 24-May-2003 John Coffman specify ext2 params for xtra space
# 26-May-2003 John Coffman el-torito-bootable-CD 2.1
# 30-May-2003 " add lilo version check 2.1
# 07-Jul-2003 " determine loopback dynamically 2.3
# 29-Aug-2004 " allow --root LABEL=lblname 2.4
# 01-Sep-2004 " fix check for LILO version 2.4
# 03-Oct-2004 " get root= from /etc/fstab 2.5
# 15-Nov-2004 " support --iso --size HD 3.0
# 18-Aug-2005 " applied temp-file security patch 3.1
# 25-Sep-2005 " log config file on --debug 3.2
# 04-Oct-2005 " new 'master' determination 3.2
# 07-Oct-2005 " add -v0 -w to usages of -I 3.2
#
debug=false
#debug=true
# set the version number on this command
version=3.2
# set the version of LILO required to run
major=22
minor=6
revision=1
log=$(pwd)/mkrescue.log
clog=$(pwd)/mkrescue.conf.log
usage () {
cat <<EOF
usage: `basename $0` [--help]
`basename $0` [--version]
`basename $0` [--device <device>] [--fast] [--fs ext2|msdos|minix]
[--image <label>] [--install text|menu] [--keymap <keymap.ktl>]
[--initrd <file> --kernel <file>] [--append <string>]
[--root <device>] [--nocompact] [--noformat]
[--iso] [--size 1440|1200|2880|HD]
--device is the floppy drive; e.g., /dev/fd0
--fast specifies creation using a loopback device, which may be faster
--fs is the filesystem to make on the device; e.g., ext2
--help prints this helpfile
--iso create a bootable ISO image to burn to a CD-R or CD-RW
--keymap is the keyboard translation table; default to same as lilo.conf
--noformat bypasses creation of a new filesystem on device
--nocompact omits lilo map file compaction
--size is assumed to be 1440 (1.44M), unless 1200 or 2880 is specified
HD may be specified for ISO images
--image specifies the label of the kernel/initrd if not the default
--install text is the default for floppies, menu for ISO images
--initrd and --kernel are the initial ramdisk & kernel files
--append is a string used to specify kernel options
--root is the root filesystem for the boot floppy; e.g., current
--version prints the version number of `basename $0`
Used without any arguments, `basename $0` will use the default kernel in
/etc/lilo.conf, the companion initrd (if any), and the specified root
filesystem to make a bootable rescue floppy.
EOF
#
# --install & --debug are undocumented above
#
#
exit $1
}
if [ $debug != false ]; then
lilo=$(pwd)/lilo
config=$(pwd)/lilo.conf
else
lilo=/sbin/lilo
config=/etc/lilo.conf
fi
if [ ! -r "$config" ] ; then
echo "$0: Cannot read the configuration file $config (are you root?)"
exit 1
fi
compact=-c
device=/dev/fd0
fs=ext2
tmpbase="`mktemp -dt $(basename $0).XXXXXXXXXX`" || exit
if [ "$debug" != true ]; then
trap 'rm -rf -- "$tmpbase"' EXIT
trap 'trap - EXIT; rm -rf -- "$tmpbase"; exit 1' HUP INT QUIT TERM
fi
mount="$tmpbase/mkrescue-flp"
mfile="$tmpbase/mkrescue-emu"
mtemp="$tmpbase/mkrescue-tmp"
mkdir "$mount"
touch "$mfile" "$mtemp"
loopback=loop0
looppart=loop1
install=text
isoimage=no
format=yes
image=
root=
bios=0x00
fast=slow
size=0
heads=2
sectors=18
cylinders=80
hdsize=16384
bootcmd=
append=
initrd=
boot=/boot
diag=no
master=
VERSION=$($lilo -V | awk '{print $3}' | sed -e "s/[^a-zA-Z0-9.]//g")
NVERSION=$(echo $VERSION | sed "s/\\./ /g")
DASH=$(echo $VERSION | sed "/-/s/.*-//" )
if [ "$DASH" = "$VERSION" ]; then
DASH=0
else
NVERSION=$(echo $NVERSION | sed "s/-.*//")
fi
MAJOR=$(echo $NVERSION | sed "s/ .*//")
MINOR=$(echo $NVERSION | sed -e "s/$MAJOR //" -e "s/ .*//" )
if [ "$MINOR" = "$NVERSION" ]; then
MINOR=0
fi
REVISION=$(echo $NVERSION | sed "s/$MAJOR $MINOR //")
if [ "$REVISION" = "$NVERSION" ]; then
REVISION=0
fi
REVISION=$(echo $REVISION | sed "s/ .*//")
if [ "$MINOR" -gt 49 ]; then MINOR=$(expr $MINOR % 10); fi
if [ $debug = true ]; then
echo ""
echo VERSION $VERSION
echo ""
echo MAJOR $MAJOR
echo MINOR $MINOR
echo REVISION $REVISION
echo DASH $DASH
echo ""
fi
#if [ "$MAJOR" -lt "$major" \
# -o "$MINOR" -lt "$minor" \
# -o $REVISION -lt "$revision" ]
skip=false
if [ "$REVISION" -lt "$revision" ]
then
skip=true
#echo $REVISION lt $revision
fi
if [ "$MINOR" -gt "$minor" ]
then
skip=false
#echo $MINOR gt $minor
fi
if [ "$MAJOR" -gt "$major" ]
then
skip=false
#echo $MAJOR gt $major
fi
if [ "$skip" = "true" ]
then
echo `basename $0` version $version
echo "LILO version $major.$minor.$revision (or newer) is required."
exit 0
fi
umount $mount 2>/dev/null
rm -rf $mount/*
> $mfile
mkdir $tmpbase || {
echo "Could not create temporary directory."
exit 1
}
master=`mount | grep " / " | cut -d " " -f 1`
master=`echo $master | sed "s/part[0-9]*$/disc/"`
master=`echo $master | sed "s/[0-9]*$//"`
if [ ! -b $master ]; then master=`echo $master | sed "s/p$//"`; fi
if [ ! -b $master ]; then master="" ; fi
while [ $# -gt 0 ]; do
case $1 in
--append)
shift
append=$1
;;
--debug)
debug=true
;;
--device)
shift
device=$1
;;
--fast)
fast=fast
;;
--fs)
shift
fs=$1
;;
-h)
usage 0
;;
--help)
usage 0
;;
--image)
shift
image=$1
;;
--initrd)
shift
initrd=$1
;;
--install)
shift
install=$1
;;
--iso)
isoimage=yes
;;
--kernel)
shift
kernel=$1
;;
--keymap)
shift
keymap=$1
;;
--nocompact)
compact=
;;
--noformat)
format=no
;;
--root)
shift
root=$1
;;
--size)
shift
size=$1
;;
--version)
echo `basename $0` version $version
exit 0
;;
*)
echo "unrecognized argument: " $1
usage 1
;;
esac
shift
done
if [ -z "$image" ]; then
# image=`cat /proc/cmdline | sed "s/.*BOOT_IMAGE=//" | sed "s/ .*//"`
image=`$lilo -C $config -v0 -w -I " " D`
fi
if [ -z $kernel ]; then
kernel=`$lilo -C $config -v0 -w -I "$image" i`
if [ "$kernel" = "" ]; then exit 1;
elif [ $debug = "true" ]; then echo kernel = "$kernel";
fi
fi
if [ -z $root ]; then
root=`$lilo -C $config -v0 -w -I "$image" R`
if [ "$root" = "No root specified" ]; then
root=`grep </etc/fstab -v "^[ \t]*#" |
grep "[[:space:]]/[[:space:]]" | \
sed -e "s/^[ \t]*//" -e "s/[ \t].*//"`
if [ -z $root ]; then
root=`mount | grep " on / type" | sed "s/ .*//"`
fi
if [ -z $root ]; then
echo "Cannot find mounted root partition"
echo "Using current root"
root=current
fi
fi
if [ $debug = true ]; then echo root = "$root";
fi
fi
if [ -z $initrd ]; then
initrd=`$lilo -C $config -v0 -w -I "$image" r`
if [ "$initrd" = "" ]; then exit 1;
elif [ $debug = "true" ]; then echo initrd = "$initrd";
fi
fi
if [ "$initrd" = "No initial ramdisk specified" ]; then initrd= ; fi
if [ -z $append ]; then
append=`$lilo -C $config -v0 -w -I "$image" a`
if [ "$append" = "" ]; then exit 1;
elif [ $debug = "true" ]; then echo append = \"$append\";
fi
fi
if [ "$append" = "No append= was specified" ]; then append= ; fi
if [ -z $keymap ]; then
keymap=`$lilo -C $config -v0 -w -I "$image" k`
if [ "$keymap" = "" ]; then exit 1;
elif [ $debug = "true" ]; then echo keymap = "$keymap";
fi
fi
if [ $isoimage = yes ]; then
fast=fast
if [ $size = 0 ]; then
size=$hdsize
elif [ $size = HD -o $size = hd ]; then
size=$hdsize
fi
if [ $device = "/dev/fd0" ]; then
device=rescue.iso
fi
else
umount $device 2>/dev/null 1>/dev/null
fi
if [ $size = 0 ]; then
size=1440
fi
if [ $size = 1200 ]; then
sectors=15
elif [ $size = 1440 ]; then
sectors=18
elif [ $size = 2880 ]; then
sectors=36
install=menu
if [ -f $boot/diag1.img -a -f $boot/diag2.img ]; then
diag=yes
fi
elif [ $size = $hdsize ]; then
sectors=32
heads=8
cylinders=$(($size/$sectors/$heads*2))
if [ $size != $(($sectors*$heads*$cylinders/2)) ]; then
echo Internal error in HDsize
exit 1
fi
install=menu
if [ -f $boot/diag1.img -a -f $boot/diag2.img ]; then
diag=yes
fi
elif [ $size = HD -o $size = hd ]; then
echo "--size $size may only be used with the --iso option."
exit 1
else
echo "--size must be 1200 or 1440; --size 1440 assumed."
sectors=18
size=1440
fi
if [ $fs != msdos -a $fs != ext2 -a $fs != minix ]; then
echo "illegal option: --fs" $fs
echo " must be either msdos or ext2 or minix"
exit 1
fi
if [ $fs = msdos ]; then
mountconfig=$mount/lilo.cnf
else
mountconfig=$mount/lilo.conf
fi
if [ $debug = "true" ]; then
umount $mfile
echo ""
echo lilo = $lilo
echo device = $device
echo image = $image
echo kernel = $kernel
echo initrd = $initrd
echo append = \"$append\"
echo install = $install
echo format = $format
echo fs = $fs
echo size = $size
echo root = $root
echo compact = $compact
echo keymap = $keymap
echo isoimage = $isoimage
echo master = $master
echo ""
echo pause after parameter display
read aline
fi
if [ ! -f $kernel ]; then
echo "Kernel file " $kernel " does not exist"
exit 1
fi
if [ ! -z $initrd ]; then
if [ ! -f $initrd ]; then
echo "Initial ramdisk file " $initrd " does not exist"
exit 1
fi
fi
if [ $isoimage != yes ]; then
# Calculate size
if [ -x /usr/bin/du ]; then
totalsize=`/usr/bin/du -Dc $kernel $initrd $keymap |tail -1 | awk '{ print $1 }'`
if [ "$totalsize" -gt "$size" ] ; then
echo "Sorry, the ramdisk, kernel and keymap don't fit in the floppy disk."
exit 1
fi
fi
echo ""
echo "Insert a blank floppy into $device"
echo "All information on this floppy will be destroyed"
echo "Press [Enter] to proceed, ^C to abort"
read aline
fi
if [ "$fast" = fast ]; then
dd bs=1024 count=$size of=$mfile if=/dev/zero
bsize=$size
mpart=$mfile
if [ $size = $hdsize ]; then
bios=0x80
bsize=$(($size-$sectors))
cat > $mtemp <<EOF
n
p
1
a
1
w
EOF
echo Partitioning HD "file (this will take a minute)"
fdisk -b 512 -S $sectors -H $heads -C $cylinders \
$mfile < $mtemp > /dev/null 2> /dev/null
rm -f $mtemp
echo bsize = $bsize
#read aline
losetup -d /dev/$loopback 2>/dev/null
losetup -d /dev/$looppart 2>/dev/null
losetup /dev/$loopback $mfile
losetup -o $(($sectors*512)) /dev/$looppart $mfile
mpart=/dev/$looppart
fi
echo Making filesystem
if [ "$fs" = ext2 ]; then
echo y | mkfs.$fs -N 24 -b 1024 $mpart $bsize
else
echo y | mkfs.$fs $mpart
fi
echo Mounting filesystem
#read aline
if [ $size != $hdsize ]; then
mount -t $fs -o rw,loop $mfile $mount
loopback=`mount | grep $mfile | sed -e "sX.*loop=/dev/XX" -e "s/).*//"`
else
mount -t $fs -o rw $mpart $mount
fi
if [ $debug = true ]; then
mount
fi
disk="/dev/$loopback"
if [ $debug = true ]; then
echo "disk=$disk"
fi
else
if [ "$format" = "yes" ]; then
echo Formatting $device with $fs filesystem...
dd of=$device if=/dev/zero bs=512 count=1
if [ "$fs" = ext2 ]; then
mkfs -t $fs -N 24 -b 1024 $device 1>/dev/null 2>/dev/null
else
mkfs -t $fs $device 1>/dev/null 2>/dev/null
fi
echo done.
echo ""
fi
rm -rf $mount/*
mount -t $fs -o rw $device $mount
rm -rf $mount/*
disk=$device
fi
cat > $mountconfig <<EOF
# Begin mkrescue $version configuration file
install=$install
boot=$device
map=map
backup=/dev/null
message=message
prompt
timeout=150
nowarn
geometric
disk=$disk bios=$bios
sectors=$sectors heads=$heads cylinders=$cylinders
EOF
if [ $size = $hdsize ]; then
echo " max-partitions=7" >>$mountconfig
echo " partition=/dev/$looppart start=$sectors" >>$mountconfig
echo static-bios-codes >>$mountconfig
bios=0x81
else
bios=0x80
fi
if [ "$master" != "" -a $isoimage = yes ]; then
echo "disk=$master" >>$mountconfig
echo " bios=$bios" >>$mountconfig
elif [ "$master" != "" -a $debug = true ]; then
echo "disk=$master" >>$mountconfig
echo " bios=$bios" >>$mountconfig
fi
if [ $keymap != us.ktl ]; then
echo "keytable=lang.ktl" >>$mountconfig
fi
if [ $isoimage = yes ]; then
echo "el-torito-bootable-CD" >>$mountconfig
fi
echo " " >>$mountconfig
echo "image=linux" >>$mountconfig
if [ ! -z $initrd ]; then
echo " initrd=initrd" >>$mountconfig
fi
if [ ! -z "$append" ]; then
echo " append=\"$append\"" >>$mountconfig
fi
cat >> $mountconfig <<EOF
root="$root"
read-only
EOF
if [ "$master" != "" -a $isoimage = yes ]; then
cat >> $mountconfig <<EOF
other=$master
unsafe
label=hard_disk
EOF
fi
if [ "$diag" = yes ]; then
cp -pv $boot/diag1.img $mount
cp -pv $boot/diag2.img $mount
cat >> $mountconfig <<EOF
image=diag1.img
label=diagnostic_1
image=diag2.img
label=diagnostic_2
EOF
fi
echo >>$mountconfig "# End of mkrescue-generated configuration file"
if [ $isoimage = yes ]; then
comment="El-Torito bootable-CD will boot at end of timeout"
else
comment="floppy will boot in 15 seconds"
fi
rm -rf $mount/lost+found
cat > $mount/message <<EOF
MKRESCUE version $version $comment
Use "boot: linux <options>" to enter kernel options
The root device is currently configured as root="$root"
EOF
echo `uname --sysname` `uname --release` > $mount/$(uname --release)
sync
if [ $debug = true ]; then
echo root=\"$root\"
echo ""
echo "pause after writing lilo.conf & message ..."
read aline
fi
echo "Copying files..."
if [ $keymap != us.ktl ]; then
cp -pv $keymap $mount/lang.ktl
fi
if [ ! -z $initrd ]; then
cp -pv $initrd $mount/initrd
fi
cp -pv $kernel $mount/linux
sync
echo "done."
echo ""
pushd $mount >/dev/null 2>/dev/null
if [ "$fast" = fast ]; then
bootcmd="-b /dev/$loopback"
fi
echo Running $lilo ...
if [ $debug = true ]; then
cp -pvf $mountconfig $clog
if [ -z $log ]; then
echo $lilo -w+ -C $mountconfig $compact $bootcmd -v5
$lilo -w+ -C $mountconfig $compact $bootcmd -v5 || fast=error
else
echo $lilo -w+ -C $mountconfig $compact $bootcmd -v5 ">$log"
$lilo -w+ -C $mountconfig $compact $bootcmd -v5 >$log || fast=error
fi
else
$lilo -C $mountconfig $compact $bootcmd || fast=error
fi
popd >/dev/null 2>/dev/null
if [ "$fast" = error ]; then
echo -n `$lilo -V`
echo " failure."
else
echo done.
fi
echo ""
umount $mount
if [ $fast = error ]; then
exit 1
fi
if [ $isoimage = yes ]; then
echo MKISOFS
out=$device
opt=
if [ $size = $hdsize ]; then
losetup -d /dev/$looppart
losetup -d /dev/$loopback
opt=-hard-disk-boot
fi
mv $mfile $mount/boot.bin
mkisofs $opt -J -R -T \
-V LILO_BOOT -A "Linux Boot CD created by LILO mkrescue" \
-b boot.bin -c boot.cat -o $out $mount
cat <<EOF
END MKISOFS: output is in '$device'
The bootable CD can be burned with the 'cdrecord' utility
using a command of the form:
cdrecord [<options>] [dev=<device>] $device
EOF
elif [ "$fast" = fast ]; then
if [ $debug = true ]; then
echo Pause before transfering to $device
read aline
fi
dd if=$mfile of=$device bs=1024
fi
echo "All done."
echo ""
exit 0