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=${TMPDIR:-/tmp}/mkrescue_$$
100 mount=`mktemp -d -t mkrescue-flp.XXXXXX` || { echo "$0: Cannot create temporary directory" >&2; exit 1; }
101 mfile=`mktemp -t mkrescue-emu.XXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1; }
102 mtemp=`mktemp -t mkrescue-tmp.XXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1; }
103 trap " [ \"$debug\" != true ] && rm -f \"$mtemp\" ; /bin/rm -rf -- \"$mount\" \"$mfile\" " 0 1 2 3 13 15
125 VERSION=$($lilo -V | awk '{print $3}' | sed -e "s/[^a-zA-Z0-9.]//g")
127 NVERSION=$(echo $VERSION | sed "s/\\./ /g")
129 DASH=$(echo $VERSION | sed "/-/s/.*-//" )
130 if [ "$DASH" = "$VERSION" ]; then
133 NVERSION=$(echo $NVERSION | sed "s/-.*//")
135 MAJOR=$(echo $NVERSION | sed "s/ .*//")
136 MINOR=$(echo $NVERSION | sed -e "s/$MAJOR //" -e "s/ .*//" )
137 if [ "$MINOR" = "$NVERSION" ]; then
140 REVISION=$(echo $NVERSION | sed "s/$MAJOR $MINOR //")
141 if [ "$REVISION" = "$NVERSION" ]; then
144 REVISION=$(echo $REVISION | sed "s/ .*//")
145 if [ "$MINOR" -gt 49 ]; then MINOR=$(expr $MINOR % 10); fi
147 if [ $debug = true ]; then
149 echo VERSION $VERSION
153 echo REVISION $REVISION
158 #if [ "$MAJOR" -lt "$major" \
159 # -o "$MINOR" -lt "$minor" \
160 # -o $REVISION -lt "$revision" ]
162 if [ "$REVISION" -lt "$revision" ]
165 #echo $REVISION lt $revision
167 if [ "$MINOR" -gt "$minor" ]
170 #echo $MINOR gt $minor
172 if [ "$MAJOR" -gt "$major" ]
175 #echo $MAJOR gt $major
177 if [ "$skip" = "true" ]
179 echo `basename $0` version $version
180 echo "LILO version $major.$minor.$revision (or newer) is required."
184 umount $mount 2>/dev/null
189 echo "Could not create temporary directory."
193 master=`mount | grep " / " | cut -d " " -f 1`
194 master=`echo $master | sed "s/part[0-9]*$/disc/"`
195 master=`echo $master | sed "s/[0-9]*$//"`
196 if [ ! -b $master ]; then master=`echo $master | sed "s/p$//"`; fi
197 if [ ! -b $master ]; then master="" ; fi
200 while [ $# -gt 0 ]; do
264 echo `basename $0` version $version
268 echo "unrecognized argument: " $1
276 if [ -z "$image" ]; then
277 # image=`cat /proc/cmdline | sed "s/.*BOOT_IMAGE=//" | sed "s/ .*//"`
278 image=`$lilo -C $config -v0 -w -I " " D`
281 if [ -z $kernel ]; then
282 kernel=`$lilo -C $config -v0 -w -I "$image" i`
283 if [ "$kernel" = "" ]; then exit 1;
284 elif [ $debug = "true" ]; then echo kernel = "$kernel";
288 if [ -z $root ]; then
289 root=`$lilo -C $config -v0 -w -I "$image" R`
290 if [ "$root" = "No root specified" ]; then
291 root=`grep </etc/fstab -v "^[ \t]*#" |
292 grep "[[:space:]]/[[:space:]]" | \
293 sed -e "s/^[ \t]*//" -e "s/[ \t].*//"`
294 if [ -z $root ]; then
295 root=`mount | grep " on / type" | sed "s/ .*//"`
297 if [ -z $root ]; then
298 echo "Cannot find mounted root partition"
299 echo "Using current root"
303 if [ $debug = true ]; then echo root = "$root";
307 if [ -z $initrd ]; then
308 initrd=`$lilo -C $config -v0 -w -I "$image" r`
309 if [ "$initrd" = "" ]; then exit 1;
310 elif [ $debug = "true" ]; then echo initrd = "$initrd";
313 if [ "$initrd" = "No initial ramdisk specified" ]; then initrd= ; fi
315 if [ -z $append ]; then
316 append=`$lilo -C $config -v0 -w -I "$image" a`
317 if [ "$append" = "" ]; then exit 1;
318 elif [ $debug = "true" ]; then echo append = \"$append\";
321 if [ "$append" = "No append= was specified" ]; then append= ; fi
323 if [ -z $keymap ]; then
324 keymap=`$lilo -C $config -v0 -w -I "$image" k`
325 if [ "$keymap" = "" ]; then exit 1;
326 elif [ $debug = "true" ]; then echo keymap = "$keymap";
330 if [ $isoimage = yes ]; then
332 if [ $size = 0 ]; then
334 elif [ $size = HD -o $size = hd ]; then
337 if [ $device = "/dev/fd0" ]; then
341 umount $device 2>/dev/null 1>/dev/null
344 if [ $size = 0 ]; then
348 if [ $size = 1200 ]; then
350 elif [ $size = 1440 ]; then
352 elif [ $size = 2880 ]; then
355 if [ -f $boot/diag1.img -a -f $boot/diag2.img ]; then
358 elif [ $size = $hdsize ]; then
361 cylinders=$(($size/$sectors/$heads*2))
362 if [ $size != $(($sectors*$heads*$cylinders/2)) ]; then
363 echo Internal error in HDsize
367 if [ -f $boot/diag1.img -a -f $boot/diag2.img ]; then
370 elif [ $size = HD -o $size = hd ]; then
371 echo "--size $size may only be used with the --iso option."
374 echo "--size must be 1200 or 1440; --size 1440 assumed."
379 if [ $fs != msdos -a $fs != ext2 -a $fs != minix ]; then
380 echo "illegal option: --fs" $fs
381 echo " must be either msdos or ext2 or minix"
385 if [ $fs = msdos ]; then
386 mountconfig=$mount/lilo.cnf
388 mountconfig=$mount/lilo.conf
391 if [ $debug = "true" ]; then
397 echo device = $device
399 echo kernel = $kernel
400 echo initrd = $initrd
401 echo append = \"$append\"
402 echo install = $install
403 echo format = $format
407 echo compact = $compact
408 echo keymap = $keymap
409 echo isoimage = $isoimage
410 echo master = $master
412 echo pause after parameter display
416 if [ ! -f $kernel ]; then
417 echo "Kernel file " $kernel " does not exist"
421 if [ ! -z $initrd ]; then
422 if [ ! -f $initrd ]; then
423 echo "Initial ramdisk file " $initrd " does not exist"
428 if [ $isoimage != yes ]; then
430 if [ -x /usr/bin/du ]; then
431 totalsize=`/usr/bin/du -Dc $kernel $initrd $keymap |tail -1 | awk '{ print $1 }'`
432 if [ "$totalsize" -gt "$size" ] ; then
433 echo "Sorry, the ramdisk, kernel and keymap don't fit in the floppy disk."
438 echo "Insert a blank floppy into $device"
439 echo "All information on this floppy will be destroyed"
440 echo "Press [Enter] to proceed, ^C to abort"
444 if [ "$fast" = fast ]; then
446 dd bs=1024 count=$size of=$mfile if=/dev/zero
449 if [ $size = $hdsize ]; then
451 bsize=$(($size-$sectors))
462 echo Partitioning HD "file (this will take a minute)"
463 fdisk -b 512 -S $sectors -H $heads -C $cylinders \
464 $mfile < $mtemp > /dev/null 2> /dev/null
468 losetup -d /dev/$loopback 2>/dev/null
469 losetup -d /dev/$looppart 2>/dev/null
470 losetup /dev/$loopback $mfile
471 losetup -o $(($sectors*512)) /dev/$looppart $mfile
474 echo Making filesystem
475 if [ "$fs" = ext2 ]; then
476 echo y | mkfs.$fs -N 24 -b 1024 $mpart $bsize
478 echo y | mkfs.$fs $mpart
480 echo Mounting filesystem
482 if [ $size != $hdsize ]; then
483 mount -t $fs -o rw,loop $mfile $mount
484 loopback=`mount | grep $mfile | sed -e "sX.*loop=/dev/XX" -e "s/).*//"`
486 mount -t $fs -o rw $mpart $mount
488 if [ $debug = true ]; then
491 disk="/dev/$loopback"
492 if [ $debug = true ]; then
498 if [ "$format" = "yes" ]; then
499 echo Formatting $device with $fs filesystem...
500 dd of=$device if=/dev/zero bs=512 count=1
501 if [ "$fs" = ext2 ]; then
502 mkfs -t $fs -N 24 -b 1024 $device 1>/dev/null 2>/dev/null
504 mkfs -t $fs $device 1>/dev/null 2>/dev/null
511 mount -t $fs -o rw $device $mount
518 cat > $mountconfig <<EOF
519 # Begin mkrescue $version configuration file
529 disk=$disk bios=$bios
530 sectors=$sectors heads=$heads cylinders=$cylinders
532 if [ $size = $hdsize ]; then
533 echo " max-partitions=7" >>$mountconfig
534 echo " partition=/dev/$looppart start=$sectors" >>$mountconfig
535 echo static-bios-codes >>$mountconfig
541 if [ "$master" != "" -a $isoimage = yes ]; then
542 echo "disk=$master" >>$mountconfig
543 echo " bios=$bios" >>$mountconfig
544 elif [ "$master" != "" -a $debug = true ]; then
545 echo "disk=$master" >>$mountconfig
546 echo " bios=$bios" >>$mountconfig
549 if [ $keymap != us.ktl ]; then
550 echo "keytable=lang.ktl" >>$mountconfig
553 if [ $isoimage = yes ]; then
554 echo "el-torito-bootable-CD" >>$mountconfig
557 echo " " >>$mountconfig
558 echo "image=linux" >>$mountconfig
560 if [ ! -z $initrd ]; then
561 echo " initrd=initrd" >>$mountconfig
564 if [ ! -z "$append" ]; then
565 echo " append=\"$append\"" >>$mountconfig
568 cat >> $mountconfig <<EOF
573 if [ "$master" != "" -a $isoimage = yes ]; then
574 cat >> $mountconfig <<EOF
581 if [ "$diag" = yes ]; then
582 cp -pv $boot/diag1.img $mount
583 cp -pv $boot/diag2.img $mount
584 cat >> $mountconfig <<EOF
591 echo >>$mountconfig "# End of mkrescue-generated configuration file"
593 if [ $isoimage = yes ]; then
594 comment="El-Torito bootable-CD will boot at end of timeout"
596 comment="floppy will boot in 15 seconds"
599 rm -rf $mount/lost+found
600 cat > $mount/message <<EOF
602 MKRESCUE version $version $comment
603 Use "boot: linux <options>" to enter kernel options
604 The root device is currently configured as root="$root"
607 echo `uname --sysname` `uname --release` > $mount/$(uname --release)
611 if [ $debug = true ]; then
614 echo "pause after writing lilo.conf & message ..."
618 echo "Copying files..."
619 if [ $keymap != us.ktl ]; then
620 cp -pv $keymap $mount/lang.ktl
623 if [ ! -z $initrd ]; then
624 cp -pv $initrd $mount/initrd
627 cp -pv $kernel $mount/linux
635 pushd $mount >/dev/null 2>/dev/null
636 if [ "$fast" = fast ]; then
637 bootcmd="-b /dev/$loopback"
640 echo Running $lilo ...
641 if [ $debug = true ]; then
643 cp -pvf $mountconfig $clog
645 echo $lilo -w+ -C $mountconfig $compact $bootcmd -v5
646 $lilo -w+ -C $mountconfig $compact $bootcmd -v5 || fast=error
648 echo $lilo -w+ -C $mountconfig $compact $bootcmd -v5 ">$log"
649 $lilo -w+ -C $mountconfig $compact $bootcmd -v5 >$log || fast=error
653 $lilo -C $mountconfig $compact $bootcmd || fast=error
655 popd >/dev/null 2>/dev/null
656 if [ "$fast" = error ]; then
666 if [ $fast = error ]; then
670 if [ $isoimage = yes ]; then
674 if [ $size = $hdsize ]; then
675 losetup -d /dev/$looppart
676 losetup -d /dev/$loopback
679 mv $mfile $mount/boot.bin
680 mkisofs $opt -J -R -T \
681 -V LILO_BOOT -A "Linux Boot CD created by LILO mkrescue" \
682 -b boot.bin -c boot.cat -o $out $mount
685 END MKISOFS: output is in '$device'
688 The bootable CD can be burned with the 'cdrecord' utility
689 using a command of the form:
691 cdrecord [<options>] [dev=<device>] $device
694 elif [ "$fast" = fast ]; then
695 if [ $debug = true ]; then
696 echo Pause before transfering to $device
699 dd if=$mfile of=$device bs=1024