Imported Debian patch 3.6-1
[debian/elilo] / debian / elilo.sh
1 #! /bin/sh
2
3 ###############################################################################
4 ##
5 ## elilo installs efi bootloader onto a bootstrap partition (based on ybin)
6 ## Copyright (C) 2001 Ethan Benson
7 ##
8 ## This program is free software; you can redistribute it and/or
9 ## modify it under the terms of the GNU General Public License
10 ## as published by the Free Software Foundation; either version 2
11 ## of the License, or (at your option) any later version.
12 ##
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ## GNU General Public License for more details.
17 ##
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 ##
22 ###############################################################################
23
24 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
25 ## allow to run out of /target in boot-floppies
26 if [ -n "$PATH_PREFIX" ] ; then
27     PATH="${PATH}:${PATH_PREFIX}/sbin:${PATH_PREFIX}/bin:${PATH_PREFIX}/usr/sbin:${PATH_PREFIX}/usr/bin:${PATH_PREFIX}/usr/local/sbin:${PATH_PREFIX}/usr/local/bin"
28 fi
29 PRG="${0##*/}"
30 SIGINT="$PRG: Interrupt caught ... exiting"
31 VERSION=##VERSION##
32 DEBUG=0
33 VERBOSE=0
34 TMP="${TMPDIR:-/tmp}"
35 TARGET=
36 # Beware, /EFI/debian occurs with double backslashes in the script too
37 # so change those as well as EFIROOT, if need be.
38 EFIROOT=/EFI/debian
39 export LC_COLLATE=C
40
41 ARCHITECTURE=$(dpkg --print-installation-architecture)
42
43 ## catch signals, clean up junk in /tmp.
44 trap "cleanup" 0
45 trap "cleanup; exit 129" HUP
46 trap "echo 1>&2 $SIGINT ; cleanup; exit 130" INT
47 trap "cleanup; exit 131" QUIT
48 trap "cleanup; exit 143" TERM
49
50 ## define default config file
51 CONF=/etc/elilo.conf
52 bootconf=$CONF
53 ERR=" Error in $CONF:"
54
55 ## define default configuration
56 boot=unconfigured
57
58 ## allow default to work on packaged and non-packaged elilo. 
59 if [ -f /usr/local/lib/elilo/elilo.efi ] ; then
60     install=/usr/local/lib/elilo/elilo.efi
61 elif [ -f /usr/lib/elilo/elilo.efi ] ; then
62     install=/usr/lib/elilo/elilo.efi
63 fi
64
65 ## defaults
66 efiboot=0
67 autoconf=0
68 fstype=vfat
69 umountproc=0
70
71 ## elilo autoconf defaults
72 label=Linux
73 timeout=20
74 root=/dev/sda3
75
76 # image default is controlled by /etc/kernel-img.conf, if it exists
77 if [ -f /etc/kernel-img.conf ] &&
78         grep -E -qi "^(image|link)_in_boot *= *yes" /etc/kernel-img.conf; then
79     image=/boot/vmlinuz
80     initrdline=initrd=/boot/initrd.img
81     initrdoldline=initrd=/boot/initrd.img.old
82 else
83     image=/vmlinuz
84     initrdline=initrd=/initrd.img
85     initrdoldline=initrd=/initrd.img.old
86 fi
87 if [ -f /etc/kernel-img.conf ] &&
88         ! grep -qi "^do_initrd *= *yes" /etc/kernel-img.conf; then
89     initrdline=
90     initrdoldline=
91 fi
92
93 ## make fake `id' if its missing, outputs 0 since if its missing we
94 ## are probably running on boot floppies and thus are root.
95 if (command -v id > /dev/null 2>&1) ; then 
96     true
97 else
98     id()
99     {
100     echo 0
101     }
102 fi
103
104 ## --version output
105 version()
106 {
107 echo \
108 "$PRG $VERSION
109 Written by Richard Hirst, based on work by Ethan Benson
110
111 This is free software; see the source for copying conditions.  There is NO
112 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
113 }
114
115 ## --help output.
116 usage()
117 {
118 echo \
119 "Usage: $PRG [OPTION]...
120 Update/install bootloader onto a bootstrap partition.
121
122   -b, --boot                 set bootstrap partition device [ -b /dev/sda1 ]
123   -i, --install              pathname to the actual bootloader binary
124                                default: /usr/{local/}lib/elilo/elilo.efi
125   -C, --config               use alternate configuration file [ -C config_file ]
126       --autoconf             auto-generate a /etc/elilo.conf
127       --efiboot              elilo auto configuration: create an efi boot
128                                manager entry for elilo
129       --timeout              elilo auto configuration: sets the time elilo
130                                will wait for user input before booting default
131                                image default: 20 (2 seconds)
132       --image                elilo auto configuration: sets the path to the
133                                kernel image. default: /vmlinuz
134       --label                elilo auto configuration: sets the image label
135                                default: Linux
136       --root                 elilo auto configuration: sets the root device
137                                default: /dev/sda3
138       --format               create a new FAT filesystem on the boot partition
139   -v, --verbose              make $PRG more verbose
140       --debug                print boring junk only useful for debugging
141   -h, --help                 display this help and exit
142   -V, --version              output version information and exit"
143 }
144
145 ## we have to do some things differently with a retarded devfs name.
146 ckdevfs()
147 {
148     case "$1" in
149         /dev/ide/*|/dev/scsi/*|/dev/discs/*)
150         return 0
151         ;;
152     *)
153         return 1
154         ;;
155     esac
156 }
157
158 ## the SmartArray RAID controllers use /dev/cciss/c0d0p1 kinds of names...
159 ckcciss()
160 {
161     case "$1" in
162         /dev/cciss/*)
163         return 0
164         ;;
165     *)
166         return 1
167         ;;
168     esac
169 }
170
171
172 ## configuration file parsing. FIXME: need a method which can parse
173 ## image= sections.
174 parseconf()
175 {
176 case "$1" in
177     str)
178        v=`grep "^$2[\ ,=]" "$CONF"` ; echo "${v#*=}"
179        ;;
180     flag)
181        grep "^$2\>" "$CONF" > /dev/null && echo 0 || echo 1
182        ;;
183     ck)
184        grep "^$2[\ ,=]" "$CONF" > /dev/null && echo 0 || echo 1
185        ;;
186 esac
187 }
188
189 ## check for existence of a configuration file, and make sure we have
190 ## read permission.
191 confexist()
192 {
193     if [ ! -e "$CONF" ] ; then
194         echo 1>&2 "$PRG: $CONF: No such file or directory"
195         return 1
196     elif [ ! -f "$CONF" ] ; then
197         echo 1>&2 "$PRG: $CONF: Not a regular file"
198         return 1
199     elif [ ! -r "$CONF" ] ; then
200         echo 1>&2 "$PRG: $CONF: Permission denied"
201         return 1
202     else
203         return 0
204     fi
205 }
206
207 ## check to make sure the configuration file is sane and correct.
208 ## maybe this is an insane ammount of error checking, but I want to
209 ## make sure (hopefully) nothing unexpected ever happens.  and i just
210 ## like useful errors from programs.  every error just marks an error
211 ## variable so we give the user as much info as possible before we
212 ## abandon ship.
213 checkconf()
214 {
215     if [ -L "$boot" ] ; then
216         oldboot=$boot
217         boot=$(readlink -f $oldboot)
218         echo 1>&2 "$PRG: $oldboot is a symbolic link, using $boot instead"
219     fi
220     if [ ! -e "$boot" ] ; then
221         echo 1>&2 "$PRG: $boot: No such file or directory"
222         CONFERR=1
223     elif [ ! -b "$boot" ] && [ ! -f "$boot" ] ; then
224         echo 1>&2 "$PRG: $boot: Not a regular file or block device"
225         CONFERR=1
226     elif [ ! -w "$boot" ] || [ ! -r "$boot" ] ; then
227         echo 1>&2 "$PRG: $boot: Permission denied"
228         CONFERR=1
229     fi
230
231     ## sanity check, make sure boot=bootstrap and not something dumb
232     ## like /dev/hda
233     case "$boot" in
234         *hda)
235             echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
236             CONFERR=1
237             ;;
238         *sda)
239             echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
240             CONFERR=1
241             ;;
242         *disc)
243             echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
244             CONFERR=1
245             ;;
246     esac
247
248     ## now make sure its not something dumb like the root partition
249     ROOT="$(v=`df / 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
250     BOOT="$(v=`df /boot 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
251     if [ "$boot" = "$ROOT" ] ; then
252         echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of the root filesystem"
253         CONFERR=1
254     elif [ "$boot" = "$BOOT" ] ; then
255         echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of the /boot filesystem"
256         CONFERR=1
257     fi
258
259     ## Make sure boot is not already mounted
260     mount | grep "^$boot " > /dev/null
261     if [ $? = 0 ] ; then
262         echo 1>&2 "$PRG: $boot appears to be mounted"
263         CONFERR=1
264     fi
265
266     if [ ! -e "$install" ] ; then
267         echo 1>&2 "$PRG: $install: No such file or directory"
268         CONFERR=1
269     elif [ ! -f "$install" ] ; then
270         echo 1>&2 "$PRG: $install: Not a regular file"
271         CONFERR=1
272     elif [ ! -r "$install" ] ; then
273         echo 1>&2 "$PRG: $install: Permission denied"
274         CONFERR=1
275     fi
276
277     if [ ! -e "$bootconf" ] ; then
278         echo 1>&2 "$PRG: $bootconf: No such file or directory"
279         CONFERR=1
280     elif [ ! -f "$bootconf" ] ; then
281         echo 1>&2 "$PRG: $bootconf: Not a regular file"
282         CONFERR=1
283     elif [ ! -r "$bootconf" ] ; then
284         echo 1>&2 "$PRG: $bootconf: Permission denied"
285         CONFERR=1
286     fi
287
288     # efibootmgr needs efivars, make sure kernel module is loaded
289     if modprobe -q efivars ; then
290         echo "Loaded efivars kernel module to enable use of efibootmgr"
291     fi
292
293     if [ ! -d /proc/efi/vars ] && [ ! -d /sys/firmware/efi/vars ] && [ "$efiboot" = 1 ] ; then
294         echo 1>&2 "$PRG: no efi/vars under /proc or /sys/firmware, boot menu not updated"
295         efiboot=0
296     fi
297
298     if [ "$efiboot" = 1 ] ; then
299         ## see if efibootmgr exists and is executable
300         if (command -v efibootmgr > /dev/null 2>&1) ; then
301             [ -x `command -v efibootmgr` ] || MISSING=1 ; else MISSING=1
302         fi
303
304         if [ "$MISSING" = 1 ] ; then
305             efiboot=0
306             echo 1>&2 "$PRG: Warning: \`efibootmgr' could not be found, boot menu not updated"
307         fi
308
309         if [ -f "$boot" ] ; then
310             echo 1>&2 "$PRG: $boot is a regular file, disabling boot menu update"
311             efiboot=0
312         fi
313     fi
314
315     if [ "$CONFERR" = 1 ] ; then
316         return 1
317     else
318         return 0
319     fi
320 }
321
322
323 mnt()
324 {
325     ## we can even create bootstrap filesystem images directly if you
326     ## ever wanted too.
327     if [ -f "$boot" ] ; then
328         loop=",loop"
329     fi
330
331     if [ -e "$TMP/bootstrap.$$" ] ; then
332         echo 1>&2 "$PRG: $TMP/bootstrap.$$ exists, aborting."
333         return 1
334     fi
335
336     mkdir -m 700 "$TMP/bootstrap.$$"
337     if [ $? != 0 ] ; then
338         echo 1>&2 "$PRG: Could not create mountpoint directory, aborting."
339         return 1
340     fi
341
342     mount | grep "^$boot " > /dev/null
343     if [ $? = 0 ] ; then
344         echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
345         return 1
346     fi
347
348     [ "$VERBOSE" = 1 ] && echo "$PRG: Mounting $boot..."
349     mount -t "$fstype" -o codepage=437,iocharset=iso8859-1,rw,noexec,umask=077$loop "$boot" "$TMP/bootstrap.$$"
350     if [ $? != 0 ] ; then
351         echo 1>&2 "$PRG: An error occured mounting $boot"
352         return 1
353     fi
354
355     TARGET="$TMP/bootstrap.$$"
356     return 0
357 }
358
359 copyfiles()
360 {
361     BTFILE=elilo.efi
362     CFFILE=elilo.conf
363     imagefiles=`grep '^image[[:space:]]*=' $bootconf | \
364                 sed 's/^image[[:space:]]*=[[:space:]]*//' | grep -v ':'`
365     initrdfiles=`grep '^[[:space:]]*initrd[[:space:]]*=' $bootconf | \
366                 sed 's/.*=[[:space:]]*//' | grep -v ':'`
367
368     ## Point of no return, removing the old EFI/debian tree
369     rm -rf $TARGET/$EFIROOT
370     if [ $? != 0 ]; then
371         echo 2>&1 "$PRG: Failed to delete old boot files, aborting"
372         return 1
373     fi
374     mkdir -p $TARGET/$EFIROOT
375
376     ## Add a README to warn that this tree is deleted every time elilo is run
377     echo -ne "\
378 This directory tree is managed by /usr/sbin/elilo, and is deleted and\n\
379 recreated every time elilo runs.  Any local changes will be lost.\n\
380 " > $TARGET/$EFIROOT/README.TXT
381
382     ## this is probably insecure on modern filesystems, but i think
383     ## safe on crippled hfs/dosfs.
384     [ "$VERBOSE" = 1 ] && echo "$PRG: Installing primary bootstrap $install onto $boot..."
385     cp -f "$install" "$TARGET/$EFIROOT/$BTFILE"
386     if [ $? != 0 ] ; then
387         echo 1>&2 "$PRG: An error occured while writing to $boot"
388         return 1
389     fi
390
391     [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $bootconf on $boot..."
392     ## we comment out boot= and install=, because they are only really
393     ## needed in the /etc/elilo.conf file, and elilo.efi currently
394     ## doesn't understand them.  We also need to add /EFI/debian on to
395     ## the front of any paths that don't contain colons (device paths),
396     ## and replace tabs with spaces.
397     sed -e "s|^boot[[:space:]]*=|# &|" -e "s|^install[[:space:]]*=|# &|" \
398         -e "s|\t| |g" \
399         -e "s|\(^image[[:space:]]*=[[:space:]]*\)\([^:]*\)$|\1$EFIROOT\2|" \
400         -e "s|\(^[[:space:]]*initrd[[:space:]]*=[[:space:]]*\)\([^:]*\)$|\1$EFIROOT\2|" \
401         < "$bootconf" > "$TARGET/$EFIROOT/$CFFILE"
402     if [ $? != 0 ] ; then
403         echo 1>&2 "$PRG: An error occured while writing to $boot"
404         return 1
405     fi
406
407     [ "$DEBUG" = 1 ] && echo "----" && cat "$TARGET/$EFIROOT/$CFFILE" && echo "----"
408
409     for i in $imagefiles $initrdfiles; do
410         [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $i on $boot..."
411         if [ -f $i ]; then
412             mkdir -p `dirname "$TARGET/$EFIROOT/$i"`
413             if [ $? != 0 ] ; then
414                 echo 1>&2 "$PRG: An error occured creating directory `dirname $EFIROOT/$i` on $boot"
415                 return 1
416             fi
417             cp -f "$i" "$TARGET/$EFIROOT/$i"
418             if [ $? != 0 ] ; then
419                 echo 1>&2 "$PRG: An error occured writing $i to $boot"
420                 return 1
421             fi
422         else
423             echo "$PRG: Warning: $i not found"
424         fi
425     done
426
427     sync ; sync
428
429     ## update the boot-device variable in EFI.
430     if [ "$efiboot" = 1 ] ; then
431         [ "$VERBOSE" = 1 ] && echo "$PRG: Updating EFI boot-device variable..."
432         efiquiet="-q"
433         [ "$VERBOSE" = 1 ] && efiquiet=""
434         if ckdevfs "$boot" ; then
435             BOOTDISK="${boot%/*}/disc"
436             BOOTPART="${boot##*part}"
437         elif ckcciss "$boot" ; then
438             BOOTDISK="${boot%p[0-9]*}"
439             BOOTPART="${boot##*[a-z]}"
440         else
441             BOOTDISK="${boot%%[0-9]*}"
442             BOOTPART="${boot##*[a-z]}"
443         fi
444         if [ -z "$BOOTDISK" ] || [ -z "$BOOTPART" ] ; then
445             echo 2>&1 "$PRG: Could not determine boot disk, aborting..."
446             return 1
447         fi
448
449         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-disk      = $BOOTDISK"
450         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-partition = $BOOTPART"
451         # delete other entries with name "Debian GNU/Linux"
452         for b in `efibootmgr | grep "Debian GNU/Linux" | awk '{print substr($1,5,4) }'`; do
453             efibootmgr $efiquiet -b $b -B
454         done
455         # Add a new entry for this installation
456         efibootmgr $efiquiet -c -d $BOOTDISK -p $BOOTPART -w -L "Debian GNU/Linux" \
457                 -l \\EFI\\debian\\elilo.efi -u -- elilo -C \\EFI\\debian\\elilo.conf
458         if [ $? != 0 ] ; then
459             echo 1>&2 "$PRG: An error occured while updating boot menu, we'll ignore it"
460         fi
461         # Now, if 2nd and 3rd boot entries are for floppy and CD/DVD,
462         # move them up to 1st and 2nd, making our entry the 3rd.
463         bootorder=$(efibootmgr | sed -n 's/^BootOrder: \(.*\)$/\1/p')
464         boot1st=$(echo $bootorder | sed -n "s/\(....\).*$/\1/p")
465         boot2nd=$(echo $bootorder | sed -n "s/....,\(....\).*$/\1/p")
466         boot3rd=$(echo $bootorder | sed -n "s/....,....,\(....\).*$/\1/p")
467         boot456=$(echo $bootorder | sed -n "s/....,....,....\(.*\).*$/\1/p")
468         name2nd=$(efibootmgr | sed -n "s/^Boot$boot2nd[\*] \(.*\)$/\1/p")
469         name3rd=$(efibootmgr | sed -n "s/^Boot$boot3rd[\*] \(.*\)$/\1/p")
470         name23="@$name2nd@$name3rd"
471         if ( echo $name23 | grep -qi "@floppy" ); then
472             if ( echo $name23 | grep -qi "@cd") || ( echo $name23 | grep -qi "@dvd"); then
473                 efibootmgr $efiquiet -o $boot2nd,$boot3rd,$boot1st$boot456
474             fi
475         fi
476     fi
477
478     return 0
479 }
480
481 ## mkefifs function.
482 mkefifs()
483 {
484     mount | grep "^$boot\>" > /dev/null
485     if [ $? = 0 ] ; then
486         echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
487         return 1
488     fi
489
490     if (command -v mkdosfs > /dev/null 2>&1) ; then
491         [ -x `command -v mkdosfs` ] || FAIL=1 ; else FAIL=1 ; fi
492         if [ "$FAIL" = 1 ] ; then
493             echo 1>&2 "$PRG: mkdosfs is not installed or cannot be found"
494             return 1
495         fi
496
497     [ "$VERBOSE" = 1 ] && echo "$PRG: Creating DOS filesystem on $boot..."
498     mkdosfs -n bootstrap "$boot" > /dev/null
499     if [ $? != 0 ] ; then
500         echo 1>&2 "$PRG: DOS filesystem creation failed!"
501         return 1
502     fi
503     return 0
504 }
505
506 mkconf()
507 {
508 ## defaults for this are defined at the beginning of the script with
509 ## other variables.
510
511 # We want to create an append= line from the current /proc/cmdline,
512 # so things like console=ttyS0 get picked up automatically.
513 # We also want to filter out bits of cmdline we are not interested in.
514
515 if [ -f /proc/cmdline ]; then
516   cmdline=`cat /proc/cmdline`
517 else
518   echo 1>&2 "$PRG: Warning: couldn't read /proc/cmdline, may need to add append=... to elilo.conf"
519   cmdline=""
520 fi
521
522 append=`echo $cmdline | tr ' ' '\n' | grep "^console=" | tr '\n' ' '`
523 if [ ! -z "$append" ]; then append="append=\"$append\""; fi
524
525 echo \
526 "## elilo configuration file generated by elilo $VERSION
527
528 install=$install
529 boot=$boot
530 delay=$timeout
531 default=$label
532 " > "$TMPCONF" || return 1
533
534 if [ "$ARCHITECTURE" = "ia64" ]
535 then
536   echo "relocatable" >> "$TMPCONF" || return 1
537 fi
538
539 echo \
540 "$append
541
542 image=$image
543         label=$label
544         root=$root
545         read-only
546         $initrdline
547
548 image=${image}.old
549         label=${label}OLD
550         root=$root
551         read-only
552         $initrdoldline
553 " >> "$TMPCONF" || return 1
554
555     ## Copy the new elilo.conf to /etc
556     if [ -f $CONF ]; then
557         echo 1>&2 "$PRG: backing up existing $CONF as ${CONF}-"
558         rm -f ${CONF}-
559         mv $CONF ${CONF}-
560     fi
561     cp -f "$TMPCONF" "$CONF"
562     if [ $? != 0 ] ; then
563         echo 1>&2 "$PRG: An error occured while writing to $conf"
564         return 1
565     fi
566
567 return 0
568 }
569
570 # check partition will be big enough for all we want to add to it
571
572 chkspace()
573 {
574     imagefiles=`grep '^image[[:space:]]*=' $bootconf | \
575                 sed 's/^image[[:space:]]*=[[:space:]]*//' | grep -v ':'`
576     initrdfiles=`grep '^[[:space:]]*initrd[[:space:]]*=' $bootconf | \
577                 sed 's/.*=[[:space:]]*//' | grep -v ':'`
578     bytesneeded=`cat $imagefiles $initrdfiles $install $bootconf 2>/dev/null | wc -c`
579     # convert to KB, allowing 5% overhead
580     kbneeded=$(( bytesneeded / 1024 + bytesneeded / 20480 ))
581     kbavailable=$(df -P -k $TARGET | sed -n "s|^$boot[[:space:]]\+[0-9]\+[[:space:]]\+[0-9]\+[[:space:]]\+\([0-9]\+\).*$|\1|p")
582     if [ -z $kbavailable ]; then
583         echo 2>&1 "$PRG: unable to determine space on $boot, aborting"
584         return 1
585     fi
586     if [ -d $TARGET/$EFIROOT ]; then
587         kbused=$(du -ks $TARGET/$EFIROOT | sed -n "s/[  ].*$//p")
588     else
589         kbused=0
590     fi
591     [ "$VERBOSE" = 1 ] && echo "$PRG: ${kbneeded}KB needed, ${kbavailable}KB free, ${kbused}KB to reuse"
592     kbavailable=$(( kbavailable + kbused ))
593     if [ "$kbavailable" -lt "$kbneeded" ] ; then
594         echo 1>&2 "$PRG: Insufficient space on $boot, need ${kbneeded}KB, only ${kbavailable}KB available"
595         return 1
596     fi
597 return 0
598 }
599
600
601 ## take out the trash.
602 cleanup()
603 {
604     if [ -n "$TARGET" ]; then
605         TARGET=
606         [ "$VERBOSE" = 1 ] && echo "$PRG: Unmounting $boot"
607         umount "$boot"
608         [ $? != 0 ] && echo 2>&1 "$PRG: Warning, failed to unmount $TARGET"
609     fi
610     if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
611     if [ -d "$TMP/bootstrap.$$" ] ; then rmdir "$TMP/bootstrap.$$" ; fi
612     if [ "$umountproc" = 1 ] ; then umount /proc ; fi
613     return 0
614 }
615
616 ##########
617 ## Main ##
618 ##########
619
620 ## absurdly bloated case statement to parse command line options.
621 if [ $# != 0 ] ; then
622     while true ; do
623         case "$1" in 
624             -V|--version)
625                 version
626                 exit 0
627                 ;;
628             -h|--help)
629                 usage
630                 exit 0
631                 ;;
632             --debug)
633                 DEBUG=1
634                 shift
635                 ;;
636             -v|--verbose)
637                 VERBOSE=1
638                 shift
639                 ;;
640             --force)
641                 # allow --force for now, boot-floppies 3.0.20 and
642                 # systemconfigurator use that instead of --format
643                 echo 1>&2 "$PRG: Warning: --force is now deprecated.  Use --for\mat."
644                 echo 1>&2 "Try \`$PRG --help' for more information."
645                 FORMAT=yes
646                 shift
647                 ;;
648             --format)
649                 FORMAT=yes
650                 shift
651                 ;;
652             --autoconf)
653                 autoconf=1
654                 shift
655                 ;;
656             -b|--boot)
657                 if [ -n "$2" ] ; then
658                     boot="$2"
659                     ARGBT=1
660                     shift 2
661                 else
662                     echo 1>&2 "$PRG: option requires an argument $1"
663                     echo 1>&2 "Try \`$PRG --help' for more information."
664                     exit 1
665                 fi
666                 ;;
667             -i|--install)
668                 if [ -n "$2" ] ; then
669                     install="$2"
670                     ARGBF=1
671                     shift 2
672                 else
673                     echo 1>&2 "$PRG: option requires an argument $1"
674                     echo 1>&2 "Try \`$PRG --help' for more information."
675                     exit 1
676                 fi
677                 ;;
678             -C|--config)
679                 if [ -n "$2" ] ; then
680                     CONF="$2"
681                     bootconf="$2"
682                     ERR=" Error in $CONF:"
683                     shift 2
684                 else
685                     echo 1>&2 "$PRG: option requires an argument $1"
686                     echo 1>&2 "Try \`$PRG --help' for more information."
687                     exit 1
688                 fi
689                 ;;
690             --efiboot)
691                 efiboot=1
692                 ARGNV=1
693                 shift
694                 ;;
695             --timeout)
696                 if [ -n "$2" ] ; then
697                     timeout="$2"
698                     bootconf=auto
699                     shift 2
700                 else
701                     echo 1>&2 "$PRG: option requires an argument $1"
702                     echo 1>&2 "Try \`$PRG --help' for more information."
703                     exit 1
704                 fi
705                 ;;
706             --image)
707                 if [ -n "$2" ] ; then
708                     image="$2"
709                     bootconf=auto
710                     shift 2
711                 else
712                     echo 1>&2 "$PRG: option requires an argument $1"
713                     echo 1>&2 "Try \`$PRG --help' for more information."
714                     exit 1
715                 fi
716                 ;;
717             --label)
718                 if [ -n "$2" ] ; then
719                     label="$2"
720                     bootconf=auto
721                     shift 2
722                 else
723                     echo 1>&2 "$PRG: option requires an argument $1"
724                     echo 1>&2 "Try \`$PRG --help' for more information."
725                     exit 1
726                 fi
727                 ;;
728             --root)
729                 if [ -n "$2" ] ; then
730                     root="$2"
731                     bootconf=auto
732                     shift 2
733                 else
734                     echo 1>&2 "$PRG: option requires an argument $1"
735                     echo 1>&2 "Try \`$PRG --help' for more information."
736                     exit 1
737                 fi
738                 ;;
739             "")
740                 break
741                 ;;
742             *)
743                 echo 1>&2 "$PRG: unrecognized option \`$1'"
744                 echo 1>&2 "Try \`$PRG --help' for more information."
745                 exit 1
746                 ;;
747         esac
748     done
749 fi
750
751 ## check that are root
752 if [ `id -u` != 0 ] ; then
753     echo 1>&2 "$PRG: requires root privileges, go away."
754     exit 1
755 fi
756
757 ## check that autoconf options are only specified with --autoconf
758 if [ "$bootconf" = "auto" ] && [ "$autoconf" = "0" ] ; then
759     echo 1>&2 "$PRG: Auto-config options specified without --autoconf."
760     exit 1;
761 fi
762
763 ## check that specified config file exists, unless we are to generate it,
764 ## which case we assume all options are done on the command line.
765 if [ "$autoconf" = "0" ] ; then
766     confexist || exit 1
767 fi
768
769 ## /proc is needed to parse /proc/partitions, etc.
770 if [ ! -f /proc/uptime ]; then
771     [ "$VERBOSE" = 1 ] && echo "$PRG: Mounting /proc..."
772     mount -t proc proc /proc 2> /dev/null
773     if [ $? != 0 ]; then
774         echo 1>&2 "$PRG: Failed to mount /proc, aborting."
775         exit 1
776     fi
777     umountproc=1
778 fi
779
780 # We need vfat support, so make sure it is loaded
781 modprobe vfat >/dev/null 2>&1
782
783 ## elilo.conf autogeneration. MUST have secure mktemp to
784 ## avoid race conditions. Debian's mktemp qualifies.
785 if [ "$autoconf" = "1" ] ; then
786     TMPCONF=`mktemp -q "$TMP/$PRG.XXXXXX"`
787     if [ $? != 0 ] ; then
788         echo 1>&2 "$PRG: Could not create temporary file, aborting."
789         exit 1
790     fi
791     mkconf
792     if [ $? != 0 ] ; then
793         echo 1>&2 "$PRG: An error occured generating elilo.conf, aborting."
794         exit 1
795     fi
796
797     bootconf="$TMPCONF"
798 fi
799
800 ## Checks if each option was defined on the command line, and if so
801 ## don't read it from the configuration file. this avoids
802 ## configuration options from being set null, as well as command line
803 ## options from being clobbered.
804 [ "$ARGBT" != 1 ] && [ $(parseconf ck boot) = 0 ] && boot=`parseconf str boot`
805
806 ## ffs!! rtfm! foad!
807 if [ "$boot" = unconfigured ] ; then
808     echo 1>&2 "$PRG: You must specify the device for the bootstrap partition. (ie: -b /dev/hdaX)"
809     echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
810     exit 1
811 fi
812
813 ## validate configuration for sanity.
814 checkconf || exit 1
815
816 if [ "$FORMAT" = "yes" ]; then
817     mkefifs || exit 1
818 fi
819
820 [ "$VERBOSE" = 1 ] && echo "$PRG: Checking filesystem on $boot..."
821 dosfsck $boot > /dev/null
822 if [ $? != 0 ]; then
823     echo 1>&2 "$PRG: Filesystem on $boot is corrupt, please fix that and rerun $PRG."
824     exit 1
825 fi
826
827 mnt || exit 1
828 chkspace || exit 1
829 copyfiles || exit 1
830
831 umount $TARGET
832 if [ $? != 0 ]; then
833     echo 1>&2 "$PRG: Failed to unmount $boot"
834     exit 1
835 fi
836 TARGET=
837
838 [ "$VERBOSE" = 1 ] && echo "$PRG: Installation complete."
839
840 exit 0