5 # 30-Oct-2001 original version 1.0
8 # 09-Apr-2002 John Coffman modify for lilo 22.3 1.1
9 # 09-Jun-2002 John Coffman get keyboard translation table 1.2
10 # (suggested by Qing Liu)
11 # 07-May-2003 John Coffman add nowarn for lilo 22.5.2 1.3
12 # 13-May-2003 John Coffman use default image, add append= 1.4
13 # 24-May-2003 John Coffman specify ext2 params for xtra space
14 # 26-May-2003 John Coffman el-torito-bootable-CD 2.1
15 # 30-May-2003 " add lilo version check 2.1
16 # 07-Jul-2003 " determine loopback dynamically 2.3
17 # 29-Aug-2004 " allow --root LABEL=lblname 2.4
18 # 01-Sep-2004 " fix check for LILO version 2.4
19 # 03-Oct-2004 " get root= from /etc/fstab 2.5
20 # 15-Nov-2004 " support --iso --size HD 3.0
21 # 18-Aug-2005 " applied temp-file security patch 3.1
22 # 25-Sep-2005 " log config file on --debug 3.2
23 # 04-Oct-2005 " new 'master' determination 3.2
24 # 07-Oct-2005 " add -v0 -w to usages of -I 3.2
30 # set the version number on this command
33 # set the version of LILO required to run
38 log=$(pwd)/mkrescue.log
39 clog=$(pwd)/mkrescue.conf.log
45 usage: `basename $0` [--help]
46 `basename $0` [--version]
47 `basename $0` [--device <device>] [--fast] [--fs ext2|msdos|minix]
48 [--image <label>] [--install text|menu] [--keymap <keymap.ktl>]
49 [--initrd <file> --kernel <file>] [--append <string>]
50 [--root <device>] [--nocompact] [--noformat]
51 [--iso] [--size 1440|1200|2880|HD]
53 --device is the floppy drive; e.g., /dev/fd0
54 --fast specifies creation using a loopback device, which may be faster
55 --fs is the filesystem to make on the device; e.g., ext2
56 --help prints this helpfile
57 --iso create a bootable ISO image to burn to a CD-R or CD-RW
58 --keymap is the keyboard translation table; default to same as lilo.conf
59 --noformat bypasses creation of a new filesystem on device
60 --nocompact omits lilo map file compaction
61 --size is assumed to be 1440 (1.44M), unless 1200 or 2880 is specified
62 HD may be specified for ISO images
63 --image specifies the label of the kernel/initrd if not the default
64 --install text is the default for floppies, menu for ISO images
65 --initrd and --kernel are the initial ramdisk & kernel files
66 --append is a string used to specify kernel options
67 --root is the root filesystem for the boot floppy; e.g., current
68 --version prints the version number of `basename $0`
70 Used without any arguments, `basename $0` will use the default kernel in
71 /etc/lilo.conf, the companion initrd (if any), and the specified root
72 filesystem to make a bootable rescue floppy.
76 # --install & --debug are undocumented above
82 if [ $debug != false ]; then
84 config=$(pwd)/lilo.conf
90 if [ ! -r "$config" ] ; then
91 echo "$0: Cannot read the configuration file $config (are you root?)"
99 tmpbase="`mktemp -dt $(basename $0).XXXXXXXXXX`" || exit
100 if [ "$debug" != true ]; then
101 trap 'rm -rf -- "$tmpbase"' EXIT
102 trap 'trap - EXIT; rm -rf -- "$tmpbase"; exit 1' HUP INT QUIT TERM
104 mount="$tmpbase/mkrescue-flp"
105 mfile="$tmpbase/mkrescue-emu"
106 mtemp="$tmpbase/mkrescue-tmp"
108 touch "$mfile" "$mtemp"
131 VERSION=$($lilo -V | awk '{print $3}' | sed -e "s/[^a-zA-Z0-9.]//g")
133 NVERSION=$(echo $VERSION | sed "s/\\./ /g")
135 DASH=$(echo $VERSION | sed "/-/s/.*-//" )
136 if [ "$DASH" = "$VERSION" ]; then
139 NVERSION=$(echo $NVERSION | sed "s/-.*//")
141 MAJOR=$(echo $NVERSION | sed "s/ .*//")
142 MINOR=$(echo $NVERSION | sed -e "s/$MAJOR //" -e "s/ .*//" )
143 if [ "$MINOR" = "$NVERSION" ]; then
146 REVISION=$(echo $NVERSION | sed "s/$MAJOR $MINOR //")
147 if [ "$REVISION" = "$NVERSION" ]; then
150 REVISION=$(echo $REVISION | sed "s/ .*//")
151 if [ "$MINOR" -gt 49 ]; then MINOR=$(expr $MINOR % 10); fi
153 if [ $debug = true ]; then
155 echo VERSION $VERSION
159 echo REVISION $REVISION
164 #if [ "$MAJOR" -lt "$major" \
165 # -o "$MINOR" -lt "$minor" \
166 # -o $REVISION -lt "$revision" ]
168 if [ "$REVISION" -lt "$revision" ]
171 #echo $REVISION lt $revision
173 if [ "$MINOR" -gt "$minor" ]
176 #echo $MINOR gt $minor
178 if [ "$MAJOR" -gt "$major" ]
181 #echo $MAJOR gt $major
183 if [ "$skip" = "true" ]
185 echo `basename $0` version $version
186 echo "LILO version $major.$minor.$revision (or newer) is required."
190 umount $mount 2>/dev/null
195 echo "Could not create temporary directory."
199 master=`mount | grep " / " | cut -d " " -f 1`
200 master=`echo $master | sed "s/part[0-9]*$/disc/"`
201 master=`echo $master | sed "s/[0-9]*$//"`
202 if [ ! -b $master ]; then master=`echo $master | sed "s/p$//"`; fi
203 if [ ! -b $master ]; then master="" ; fi
206 while [ $# -gt 0 ]; do
270 echo `basename $0` version $version
274 echo "unrecognized argument: " $1
282 if [ -z "$image" ]; then
283 # image=`cat /proc/cmdline | sed "s/.*BOOT_IMAGE=//" | sed "s/ .*//"`
284 image=`$lilo -C $config -v0 -w -I " " D`
287 if [ -z $kernel ]; then
288 kernel=`$lilo -C $config -v0 -w -I "$image" i`
289 if [ "$kernel" = "" ]; then exit 1;
290 elif [ $debug = "true" ]; then echo kernel = "$kernel";
294 if [ -z $root ]; then
295 root=`$lilo -C $config -v0 -w -I "$image" R`
296 if [ "$root" = "No root specified" ]; then
297 root=`grep </etc/fstab -v "^[ \t]*#" |
298 grep "[[:space:]]/[[:space:]]" | \
299 sed -e "s/^[ \t]*//" -e "s/[ \t].*//"`
300 if [ -z $root ]; then
301 root=`mount | grep " on / type" | sed "s/ .*//"`
303 if [ -z $root ]; then
304 echo "Cannot find mounted root partition"
305 echo "Using current root"
309 if [ $debug = true ]; then echo root = "$root";
313 if [ -z $initrd ]; then
314 initrd=`$lilo -C $config -v0 -w -I "$image" r`
315 if [ "$initrd" = "" ]; then exit 1;
316 elif [ $debug = "true" ]; then echo initrd = "$initrd";
319 if [ "$initrd" = "No initial ramdisk specified" ]; then initrd= ; fi
321 if [ -z $append ]; then
322 append=`$lilo -C $config -v0 -w -I "$image" a`
323 if [ "$append" = "" ]; then exit 1;
324 elif [ $debug = "true" ]; then echo append = \"$append\";
327 if [ "$append" = "No append= was specified" ]; then append= ; fi
329 if [ -z $keymap ]; then
330 keymap=`$lilo -C $config -v0 -w -I "$image" k`
331 if [ "$keymap" = "" ]; then exit 1;
332 elif [ $debug = "true" ]; then echo keymap = "$keymap";
336 if [ $isoimage = yes ]; then
338 if [ $size = 0 ]; then
340 elif [ $size = HD -o $size = hd ]; then
343 if [ $device = "/dev/fd0" ]; then
347 umount $device 2>/dev/null 1>/dev/null
350 if [ $size = 0 ]; then
354 if [ $size = 1200 ]; then
356 elif [ $size = 1440 ]; then
358 elif [ $size = 2880 ]; then
361 if [ -f $boot/diag1.img -a -f $boot/diag2.img ]; then
364 elif [ $size = $hdsize ]; then
367 cylinders=$(($size/$sectors/$heads*2))
368 if [ $size != $(($sectors*$heads*$cylinders/2)) ]; then
369 echo Internal error in HDsize
373 if [ -f $boot/diag1.img -a -f $boot/diag2.img ]; then
376 elif [ $size = HD -o $size = hd ]; then
377 echo "--size $size may only be used with the --iso option."
380 echo "--size must be 1200 or 1440; --size 1440 assumed."
385 if [ $fs != msdos -a $fs != ext2 -a $fs != minix ]; then
386 echo "illegal option: --fs" $fs
387 echo " must be either msdos or ext2 or minix"
391 if [ $fs = msdos ]; then
392 mountconfig=$mount/lilo.cnf
394 mountconfig=$mount/lilo.conf
397 if [ $debug = "true" ]; then
403 echo device = $device
405 echo kernel = $kernel
406 echo initrd = $initrd
407 echo append = \"$append\"
408 echo install = $install
409 echo format = $format
413 echo compact = $compact
414 echo keymap = $keymap
415 echo isoimage = $isoimage
416 echo master = $master
418 echo pause after parameter display
422 if [ ! -f $kernel ]; then
423 echo "Kernel file " $kernel " does not exist"
427 if [ ! -z $initrd ]; then
428 if [ ! -f $initrd ]; then
429 echo "Initial ramdisk file " $initrd " does not exist"
434 if [ $isoimage != yes ]; then
436 if [ -x /usr/bin/du ]; then
437 totalsize=`/usr/bin/du -Dc $kernel $initrd $keymap |tail -1 | awk '{ print $1 }'`
438 if [ "$totalsize" -gt "$size" ] ; then
439 echo "Sorry, the ramdisk, kernel and keymap don't fit in the floppy disk."
444 echo "Insert a blank floppy into $device"
445 echo "All information on this floppy will be destroyed"
446 echo "Press [Enter] to proceed, ^C to abort"
450 if [ "$fast" = fast ]; then
452 dd bs=1024 count=$size of=$mfile if=/dev/zero
455 if [ $size = $hdsize ]; then
457 bsize=$(($size-$sectors))
468 echo Partitioning HD "file (this will take a minute)"
469 fdisk -b 512 -S $sectors -H $heads -C $cylinders \
470 $mfile < $mtemp > /dev/null 2> /dev/null
474 losetup -d /dev/$loopback 2>/dev/null
475 losetup -d /dev/$looppart 2>/dev/null
476 losetup /dev/$loopback $mfile
477 losetup -o $(($sectors*512)) /dev/$looppart $mfile
480 echo Making filesystem
481 if [ "$fs" = ext2 ]; then
482 echo y | mkfs.$fs -N 24 -b 1024 $mpart $bsize
484 echo y | mkfs.$fs $mpart
486 echo Mounting filesystem
488 if [ $size != $hdsize ]; then
489 mount -t $fs -o rw,loop $mfile $mount
490 loopback=`mount | grep $mfile | sed -e "sX.*loop=/dev/XX" -e "s/).*//"`
492 mount -t $fs -o rw $mpart $mount
494 if [ $debug = true ]; then
497 disk="/dev/$loopback"
498 if [ $debug = true ]; then
504 if [ "$format" = "yes" ]; then
505 echo Formatting $device with $fs filesystem...
506 dd of=$device if=/dev/zero bs=512 count=1
507 if [ "$fs" = ext2 ]; then
508 mkfs -t $fs -N 24 -b 1024 $device 1>/dev/null 2>/dev/null
510 mkfs -t $fs $device 1>/dev/null 2>/dev/null
517 mount -t $fs -o rw $device $mount
524 cat > $mountconfig <<EOF
525 # Begin mkrescue $version configuration file
535 disk=$disk bios=$bios
536 sectors=$sectors heads=$heads cylinders=$cylinders
538 if [ $size = $hdsize ]; then
539 echo " max-partitions=7" >>$mountconfig
540 echo " partition=/dev/$looppart start=$sectors" >>$mountconfig
541 echo static-bios-codes >>$mountconfig
547 if [ "$master" != "" -a $isoimage = yes ]; then
548 echo "disk=$master" >>$mountconfig
549 echo " bios=$bios" >>$mountconfig
550 elif [ "$master" != "" -a $debug = true ]; then
551 echo "disk=$master" >>$mountconfig
552 echo " bios=$bios" >>$mountconfig
555 if [ $keymap != us.ktl ]; then
556 echo "keytable=lang.ktl" >>$mountconfig
559 if [ $isoimage = yes ]; then
560 echo "el-torito-bootable-CD" >>$mountconfig
563 echo " " >>$mountconfig
564 echo "image=linux" >>$mountconfig
566 if [ ! -z $initrd ]; then
567 echo " initrd=initrd" >>$mountconfig
570 if [ ! -z "$append" ]; then
571 echo " append=\"$append\"" >>$mountconfig
574 cat >> $mountconfig <<EOF
579 if [ "$master" != "" -a $isoimage = yes ]; then
580 cat >> $mountconfig <<EOF
587 if [ "$diag" = yes ]; then
588 cp -pv $boot/diag1.img $mount
589 cp -pv $boot/diag2.img $mount
590 cat >> $mountconfig <<EOF
597 echo >>$mountconfig "# End of mkrescue-generated configuration file"
599 if [ $isoimage = yes ]; then
600 comment="El-Torito bootable-CD will boot at end of timeout"
602 comment="floppy will boot in 15 seconds"
605 rm -rf $mount/lost+found
606 cat > $mount/message <<EOF
608 MKRESCUE version $version $comment
609 Use "boot: linux <options>" to enter kernel options
610 The root device is currently configured as root="$root"
613 echo `uname --sysname` `uname --release` > $mount/$(uname --release)
617 if [ $debug = true ]; then
620 echo "pause after writing lilo.conf & message ..."
624 echo "Copying files..."
625 if [ $keymap != us.ktl ]; then
626 cp -pv $keymap $mount/lang.ktl
629 if [ ! -z $initrd ]; then
630 cp -pv $initrd $mount/initrd
633 cp -pv $kernel $mount/linux
641 pushd $mount >/dev/null 2>/dev/null
642 if [ "$fast" = fast ]; then
643 bootcmd="-b /dev/$loopback"
646 echo Running $lilo ...
647 if [ $debug = true ]; then
649 cp -pvf $mountconfig $clog
651 echo $lilo -w+ -C $mountconfig $compact $bootcmd -v5
652 $lilo -w+ -C $mountconfig $compact $bootcmd -v5 || fast=error
654 echo $lilo -w+ -C $mountconfig $compact $bootcmd -v5 ">$log"
655 $lilo -w+ -C $mountconfig $compact $bootcmd -v5 >$log || fast=error
659 $lilo -C $mountconfig $compact $bootcmd || fast=error
661 popd >/dev/null 2>/dev/null
662 if [ "$fast" = error ]; then
672 if [ $fast = error ]; then
676 if [ $isoimage = yes ]; then
680 if [ $size = $hdsize ]; then
681 losetup -d /dev/$looppart
682 losetup -d /dev/$loopback
685 mv $mfile $mount/boot.bin
686 mkisofs $opt -J -R -T \
687 -V LILO_BOOT -A "Linux Boot CD created by LILO mkrescue" \
688 -b boot.bin -c boot.cat -o $out $mount
691 END MKISOFS: output is in '$device'
694 The bootable CD can be burned with the 'cdrecord' utility
695 using a command of the form:
697 cdrecord [<options>] [dev=<device>] $device
700 elif [ "$fast" = fast ]; then
701 if [ $debug = true ]; then
702 echo Pause before transfering to $device
705 dd if=$mfile of=$device bs=1024