Optimize translation files of debconf
[rrq/maintain_lilo.git] / mkrescue
1 #!/bin/bash
2 #
3 # mkrescue
4 #
5 #       30-Oct-2001     original version 1.0
6 #
7 # Revision history:
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
25 #
26
27 debug=false
28 #debug=true
29
30 # set the version number on this command
31 version=3.2
32
33 # set the version of LILO required to run
34 major=22
35 minor=6
36 revision=1
37
38 log=$(pwd)/mkrescue.log
39 clog=$(pwd)/mkrescue.conf.log
40
41
42 usage () {
43     cat <<EOF
44
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]
52
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`
69
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.
73
74 EOF
75 #
76 # --install & --debug are undocumented above
77 #
78 #
79     exit $1
80 }
81
82 if [ $debug != false ]; then
83         lilo=$(pwd)/lilo
84         config=$(pwd)/lilo.conf
85 else
86         lilo=/sbin/lilo
87         config=/etc/lilo.conf
88 fi
89
90 if [ ! -r "$config" ] ; then
91        echo "$0: Cannot read the configuration file $config (are you root?)"
92        exit 1
93 fi
94
95
96 compact=-c
97 device=/dev/fd0
98 fs=ext2
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
104 loopback=loop0
105 looppart=loop1
106 install=text
107 isoimage=no
108 format=yes
109 image=
110 root=
111 bios=0x00
112 fast=slow
113 size=0
114 heads=2
115 sectors=18
116 cylinders=80
117 hdsize=16384
118 bootcmd=
119 append=
120 initrd=
121 boot=/boot
122 diag=no
123 master=
124
125 VERSION=$($lilo -V | awk '{print $3}' | sed -e "s/[^a-zA-Z0-9.]//g")
126
127 NVERSION=$(echo $VERSION | sed "s/\\./ /g")
128
129 DASH=$(echo $VERSION | sed "/-/s/.*-//" )
130 if [ "$DASH" = "$VERSION" ]; then
131         DASH=0
132 else
133         NVERSION=$(echo $NVERSION | sed "s/-.*//")
134 fi
135 MAJOR=$(echo $NVERSION | sed "s/ .*//")
136 MINOR=$(echo $NVERSION | sed -e "s/$MAJOR //" -e "s/ .*//" )
137 if [ "$MINOR" = "$NVERSION" ]; then
138         MINOR=0
139 fi
140 REVISION=$(echo $NVERSION | sed "s/$MAJOR $MINOR //")
141 if [ "$REVISION" = "$NVERSION" ]; then
142         REVISION=0
143 fi
144 REVISION=$(echo $REVISION | sed "s/ .*//")
145 if [ "$MINOR" -gt 49 ]; then MINOR=$(expr $MINOR % 10); fi
146
147 if [ $debug = true ]; then
148 echo ""
149 echo VERSION $VERSION
150 echo ""
151 echo MAJOR $MAJOR
152 echo MINOR $MINOR
153 echo REVISION $REVISION
154 echo DASH $DASH
155 echo ""
156 fi
157
158 #if [ "$MAJOR" -lt "$major" \
159 #       -o "$MINOR" -lt "$minor" \
160 #               -o $REVISION -lt "$revision" ]
161 skip=false
162 if [ "$REVISION" -lt "$revision" ]
163 then
164 skip=true
165 #echo $REVISION lt $revision
166 fi
167 if [ "$MINOR" -gt "$minor" ]
168 then
169 skip=false
170 #echo $MINOR gt $minor
171 fi
172 if [ "$MAJOR" -gt "$major" ]
173 then
174 skip=false
175 #echo $MAJOR gt $major
176 fi
177 if [ "$skip" = "true" ]
178 then
179     echo `basename $0` version $version
180     echo "LILO version $major.$minor.$revision (or newer) is required."
181     exit 0
182 fi
183
184 umount $mount 2>/dev/null
185 rm -rf $mount/*
186 > $mfile
187
188 mkdir $tmpbase || {
189     echo "Could not create temporary directory."
190     exit 1
191 }
192
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
198
199
200 while [ $# -gt 0 ]; do
201     case $1 in
202         --append)
203             shift
204             append=$1
205             ;;
206         --debug)
207             debug=true
208             ;;
209         --device)
210             shift
211             device=$1
212             ;;
213         --fast)
214             fast=fast
215             ;;
216         --fs)
217             shift
218             fs=$1
219             ;;
220         -h)
221             usage 0
222             ;;
223         --help)
224             usage 0
225             ;;
226         --image)
227             shift
228             image=$1
229             ;;
230         --initrd)
231             shift
232             initrd=$1
233             ;;
234         --install)
235             shift
236             install=$1
237             ;;
238         --iso)
239             isoimage=yes
240             ;;
241         --kernel)
242             shift
243             kernel=$1
244             ;;
245         --keymap)
246             shift
247             keymap=$1
248             ;;
249         --nocompact)
250             compact=
251             ;;
252         --noformat)
253             format=no
254             ;;
255         --root)
256             shift
257             root=$1
258             ;;
259         --size)
260             shift
261             size=$1
262             ;;
263         --version)
264             echo `basename $0` version $version
265             exit 0
266             ;;
267         *)
268             echo "unrecognized argument: " $1
269             usage 1
270             ;;
271     esac
272
273     shift
274 done
275
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`
279 fi
280
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";
285         fi
286 fi
287
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/ .*//"`
296                 fi
297                 if [ -z $root ]; then
298                         echo "Cannot find mounted root partition"
299                         echo "Using current root"
300                         root=current
301                 fi
302         fi
303         if [ $debug = true ]; then echo root = "$root";
304         fi
305 fi
306
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";
311         fi
312 fi
313 if [ "$initrd" = "No initial ramdisk specified" ]; then initrd= ; fi
314
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\";
319         fi
320 fi
321 if [ "$append" = "No append= was specified" ]; then append= ; fi
322
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";
327         fi
328 fi
329
330 if [ $isoimage = yes ]; then
331     fast=fast
332     if [ $size = 0 ]; then
333         size=$hdsize
334     elif [ $size = HD -o $size = hd ]; then
335         size=$hdsize
336     fi
337     if [ $device = "/dev/fd0" ]; then
338         device=rescue.iso
339     fi
340 else
341         umount $device 2>/dev/null 1>/dev/null
342 fi
343
344 if [ $size = 0 ]; then
345     size=1440
346 fi
347
348 if [ $size = 1200 ]; then
349     sectors=15
350 elif [ $size = 1440 ]; then
351     sectors=18
352 elif [ $size = 2880 ]; then
353     sectors=36
354     install=menu
355     if [ -f $boot/diag1.img -a -f $boot/diag2.img ]; then
356         diag=yes
357     fi
358 elif [ $size = $hdsize ]; then
359     sectors=32
360     heads=8
361     cylinders=$(($size/$sectors/$heads*2))
362     if [ $size != $(($sectors*$heads*$cylinders/2)) ]; then
363         echo Internal error in HDsize
364         exit 1
365     fi
366     install=menu
367     if [ -f $boot/diag1.img -a -f $boot/diag2.img ]; then
368         diag=yes
369     fi
370 elif [ $size = HD -o $size = hd ]; then
371     echo "--size $size  may only be used with the  --iso  option."
372     exit 1
373 else
374     echo "--size must be 1200 or 1440; --size 1440 assumed."
375     sectors=18
376     size=1440
377 fi
378
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"
382     exit 1
383 fi
384
385 if [ $fs = msdos ]; then
386         mountconfig=$mount/lilo.cnf
387 else
388         mountconfig=$mount/lilo.conf
389 fi
390
391 if [ $debug = "true" ]; then
392         umount $mfile
393
394         echo ""
395
396         echo lilo = $lilo
397         echo device = $device
398         echo image = $image
399         echo kernel = $kernel
400         echo initrd = $initrd
401         echo append = \"$append\"
402         echo install = $install
403         echo format = $format
404         echo fs = $fs
405         echo size = $size
406         echo root = $root
407         echo compact = $compact
408         echo keymap = $keymap
409         echo isoimage = $isoimage
410         echo master = $master
411         echo ""
412         echo pause after parameter display
413         read aline
414 fi
415
416 if [ ! -f $kernel ]; then
417         echo "Kernel file " $kernel " does not exist"
418         exit 1
419 fi
420
421 if [ ! -z $initrd ]; then
422         if [ ! -f $initrd ]; then
423                 echo "Initial ramdisk file " $initrd " does not exist"
424                 exit 1
425         fi
426 fi
427
428 if [ $isoimage != yes ]; then
429        # Calculate size
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."
434                         exit 1
435                 fi
436         fi
437         echo ""
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"
441         read aline
442 fi
443
444 if [ "$fast" = fast ]; then
445
446         dd bs=1024 count=$size of=$mfile if=/dev/zero
447         bsize=$size
448         mpart=$mfile
449         if [ $size = $hdsize ]; then
450             bios=0x80
451             bsize=$(($size-$sectors))
452             cat > $mtemp <<EOF
453 n
454 p
455 1
456
457
458 a
459 1
460 w
461 EOF
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
465             rm -f $mtemp
466             echo bsize = $bsize
467 #read aline
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
472             mpart=/dev/$looppart
473         fi
474         echo Making filesystem
475         if [ "$fs" = ext2 ]; then
476                 echo y | mkfs.$fs -N 24 -b 1024 $mpart $bsize
477         else
478                 echo y | mkfs.$fs $mpart
479         fi
480         echo Mounting filesystem
481 #read aline
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/).*//"`
485         else
486             mount -t $fs -o rw $mpart $mount
487         fi
488         if [ $debug = true ]; then
489                 mount
490         fi
491         disk="/dev/$loopback"
492         if [ $debug = true ]; then
493                 echo "disk=$disk"
494         fi
495
496 else
497
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
503                 else
504                         mkfs -t $fs $device 1>/dev/null 2>/dev/null
505                 fi
506                 echo done.
507                 echo ""
508         fi
509
510         rm -rf $mount/*
511         mount -t $fs -o rw $device $mount
512
513         rm -rf $mount/*
514         disk=$device
515
516 fi
517
518 cat > $mountconfig <<EOF
519 #  Begin mkrescue $version configuration file
520 install=$install
521 boot=$device
522 map=map
523 backup=/dev/null
524 message=message
525 prompt
526 timeout=150
527 nowarn
528 geometric
529 disk=$disk bios=$bios
530   sectors=$sectors heads=$heads cylinders=$cylinders
531 EOF
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
536         bios=0x81
537 else
538         bios=0x80
539 fi
540
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
547 fi
548
549 if [ $keymap != us.ktl ]; then 
550         echo "keytable=lang.ktl" >>$mountconfig
551 fi
552
553 if [ $isoimage = yes ]; then 
554         echo "el-torito-bootable-CD" >>$mountconfig
555 fi
556
557 echo " " >>$mountconfig
558 echo "image=linux" >>$mountconfig
559
560 if [ ! -z $initrd ]; then
561         echo "  initrd=initrd" >>$mountconfig
562 fi
563
564 if [ ! -z "$append" ]; then
565         echo "  append=\"$append\"" >>$mountconfig
566 fi
567
568 cat >> $mountconfig <<EOF
569   root="$root"
570   read-only
571 EOF
572
573 if [ "$master" != "" -a $isoimage = yes ]; then
574 cat >> $mountconfig <<EOF
575 other=$master
576   unsafe
577   label=hard_disk
578 EOF
579 fi
580
581 if [ "$diag" = yes ]; then
582 cp -pv $boot/diag1.img $mount
583 cp -pv $boot/diag2.img $mount
584 cat >> $mountconfig <<EOF
585 image=diag1.img
586   label=diagnostic_1
587 image=diag2.img
588   label=diagnostic_2
589 EOF
590 fi
591 echo >>$mountconfig "#  End of mkrescue-generated configuration file"
592
593 if [ $isoimage = yes ]; then
594         comment="El-Torito bootable-CD will boot at end of timeout"
595 else
596         comment="floppy will boot in 15 seconds"
597 fi
598
599 rm -rf $mount/lost+found
600 cat > $mount/message <<EOF
601
602 MKRESCUE version $version $comment
603 Use  "boot: linux <options>"  to enter kernel options
604 The root device is currently configured as  root="$root"
605
606 EOF
607 echo `uname --sysname` `uname --release` > $mount/$(uname --release)
608
609 sync
610
611 if [ $debug = true ]; then
612         echo root=\"$root\"
613         echo ""
614         echo "pause after writing lilo.conf & message ..."
615         read aline
616 fi
617
618 echo "Copying files..."
619 if [ $keymap != us.ktl ]; then 
620         cp -pv $keymap $mount/lang.ktl
621 fi
622
623 if [ ! -z $initrd ]; then
624         cp -pv $initrd $mount/initrd
625 fi
626
627 cp -pv $kernel $mount/linux
628 sync
629
630 echo "done."
631 echo ""
632
633
634
635 pushd $mount >/dev/null 2>/dev/null
636 if [ "$fast" = fast ]; then
637         bootcmd="-b /dev/$loopback"
638 fi
639
640 echo Running $lilo ...
641 if [ $debug = true ]; then
642
643 cp -pvf $mountconfig $clog
644 if [ -z $log ]; then
645  echo   $lilo -w+ -C $mountconfig $compact $bootcmd -v5
646         $lilo -w+ -C $mountconfig $compact $bootcmd -v5 || fast=error
647 else
648  echo   $lilo -w+ -C $mountconfig $compact $bootcmd -v5 ">$log"
649         $lilo -w+ -C $mountconfig $compact $bootcmd -v5 >$log || fast=error
650 fi
651
652 else
653         $lilo -C $mountconfig $compact $bootcmd || fast=error
654 fi
655 popd >/dev/null 2>/dev/null
656 if [ "$fast" = error ]; then
657         echo -n `$lilo -V`
658         echo " failure."
659 else
660         echo done.
661 fi
662 echo ""
663
664 umount $mount
665
666 if [ $fast = error ]; then
667         exit 1
668 fi
669
670 if [ $isoimage = yes ]; then
671         echo MKISOFS
672         out=$device
673         opt=
674         if [ $size = $hdsize ]; then
675             losetup -d /dev/$looppart
676             losetup -d /dev/$loopback
677             opt=-hard-disk-boot
678         fi
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
683         cat <<EOF
684
685 END MKISOFS:  output is in  '$device'
686
687
688 The bootable CD can be burned with the 'cdrecord' utility
689 using a command of the form:
690
691         cdrecord [<options>] [dev=<device>] $device
692
693 EOF
694 elif [ "$fast" = fast ]; then
695         if [ $debug = true ]; then
696                 echo Pause before transfering to $device
697                 read aline
698         fi
699         dd if=$mfile of=$device bs=1024
700 fi
701
702 echo "All done."
703 echo ""
704 exit 0
705