9857ff2729c7ba09b2c146506b2dd667d6e82bd0
[debian/elilo] / debian / elilo.sh
1 #! /bin/bash
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-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     fi
219     if [ ! -e "$boot" ] ; then
220         echo 1>&2 "$PRG: $boot: No such file or directory"
221         CONFERR=1
222     elif [ ! -b "$boot" ] && [ ! -f "$boot" ] ; then
223         echo 1>&2 "$PRG: $boot: Not a regular file or block device"
224         CONFERR=1
225     elif [ ! -w "$boot" ] || [ ! -r "$boot" ] ; then
226         echo 1>&2 "$PRG: $boot: Permission denied"
227         CONFERR=1
228     fi
229
230     ## sanity check, make sure boot=bootstrap and not something dumb
231     ## like /dev/hda
232     case "$boot" in
233         *hda)
234             echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
235             CONFERR=1
236             ;;
237         *sda)
238             echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
239             CONFERR=1
240             ;;
241         *disc)
242             echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
243             CONFERR=1
244             ;;
245     esac
246
247     ## now make sure its not something dumb like the root partition
248     ROOT="$(v=`df / 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
249     BOOT="$(v=`df /boot 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
250     if [ "$boot" = "$ROOT" ] ; then
251         echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of the root filesystem"
252         CONFERR=1
253     elif [ "$boot" = "$BOOT" ] ; then
254         echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of the /boot filesystem"
255         CONFERR=1
256     fi
257
258     ## Make sure boot is not already mounted
259     mount | grep "^$boot " > /dev/null
260     if [ $? = 0 ] ; then
261         echo 1>&2 "$PRG: $boot appears to be mounted"
262         CONFERR=1
263     fi
264
265     if [ ! -e "$install" ] ; then
266         echo 1>&2 "$PRG: $install: No such file or directory"
267         CONFERR=1
268     elif [ ! -f "$install" ] ; then
269         echo 1>&2 "$PRG: $install: Not a regular file"
270         CONFERR=1
271     elif [ ! -r "$install" ] ; then
272         echo 1>&2 "$PRG: $install: Permission denied"
273         CONFERR=1
274     fi
275
276     if [ ! -e "$bootconf" ] ; then
277         echo 1>&2 "$PRG: $bootconf: No such file or directory"
278         CONFERR=1
279     elif [ ! -f "$bootconf" ] ; then
280         echo 1>&2 "$PRG: $bootconf: Not a regular file"
281         CONFERR=1
282     elif [ ! -r "$bootconf" ] ; then
283         echo 1>&2 "$PRG: $bootconf: Permission denied"
284         CONFERR=1
285     fi
286
287     # efibootmgr needs efivars, make sure kernel module is loaded
288     if modprobe -q efivars ; then
289         echo "Loaded efivars kernel module to enable use of efibootmgr"
290     fi
291
292     if [ ! -d /proc/efi/vars ] && [ ! -d /sys/firmware/efi/vars ] && [ "$efiboot" = 1 ] ; then
293         echo 1>&2 "$PRG: no efi/vars under /proc or /sys/firmware, boot menu not updated"
294         efiboot=0
295     fi
296
297     if [ "$efiboot" = 1 ] ; then
298         ## see if efibootmgr exists and is executable
299         if (command -v efibootmgr > /dev/null 2>&1) ; then
300             [ -x `command -v efibootmgr` ] || MISSING=1 ; else MISSING=1
301         fi
302
303         if [ "$MISSING" = 1 ] ; then
304             efiboot=0
305             echo 1>&2 "$PRG: Warning: \`efibootmgr' could not be found, boot menu not updated"
306         fi
307
308         if [ -f "$boot" ] ; then
309             echo 1>&2 "$PRG: $boot is a regular file, disabling boot menu update"
310             efiboot=0
311         fi
312     fi
313
314     if [ "$CONFERR" = 1 ] ; then
315         return 1
316     else
317         return 0
318     fi
319 }
320
321
322 mnt()
323 {
324     ## we can even create bootstrap filesystem images directly if you
325     ## ever wanted too.
326     if [ -f "$boot" ] ; then
327         loop=",loop"
328     fi
329
330     if [ -e "$TMP/bootstrap.$$" ] ; then
331         echo 1>&2 "$PRG: $TMP/bootstrap.$$ exists, aborting."
332         return 1
333     fi
334
335     mkdir -m 700 "$TMP/bootstrap.$$"
336     if [ $? != 0 ] ; then
337         echo 1>&2 "$PRG: Could not create mountpoint directory, aborting."
338         return 1
339     fi
340
341     mount | grep "^$boot " > /dev/null
342     if [ $? = 0 ] ; then
343         echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
344         return 1
345     fi
346
347     [ "$VERBOSE" = 1 ] && echo "$PRG: Mounting $boot..."
348     mount -t "$fstype" -o codepage=437,iocharset=iso8859-1,rw,noexec,umask=077$loop "$boot" "$TMP/bootstrap.$$"
349     if [ $? != 0 ] ; then
350         echo 1>&2 "$PRG: An error occured mounting $boot"
351         return 1
352     fi
353
354     TARGET="$TMP/bootstrap.$$"
355     return 0
356 }
357
358 copyfiles()
359 {
360     BTFILE=elilo.efi
361     CFFILE=elilo.conf
362     imagefiles=`grep '^image[[:space:]]*=' $bootconf | \
363                 sed 's/^image[[:space:]]*=[[:space:]]*//' | grep -v ':'`
364     initrdfiles=`grep '^[[:space:]]*initrd[[:space:]]*=' $bootconf | \
365                 sed 's/.*=[[:space:]]*//' | grep -v ':'`
366     vmmfiles=`grep '^[[:space:]]*vmm[[:space:]]*=' $bootconf | \
367                 sed 's/.*=[[:space:]]*//' | grep -v ':'`
368
369     ## Point of no return, removing the old EFI/debian tree
370     rm -rf $TARGET/$EFIROOT
371     if [ $? != 0 ]; then
372         echo 2>&1 "$PRG: Failed to delete old boot files, aborting"
373         return 1
374     fi
375     mkdir -p $TARGET/$EFIROOT
376
377     ## Add a README to warn that this tree is deleted every time elilo is run
378     echo -ne "\
379 This directory tree is managed by /usr/sbin/elilo, and is deleted and\n\
380 recreated every time elilo runs.  Any local changes will be lost.\n\
381 " > $TARGET/$EFIROOT/README.TXT
382
383     ## this is probably insecure on modern filesystems, but i think
384     ## safe on crippled hfs/dosfs.
385     [ "$VERBOSE" = 1 ] && echo "$PRG: Installing primary bootstrap $install onto $boot..."
386     cp -f "$install" "$TARGET/$EFIROOT/$BTFILE"
387     if [ $? != 0 ] ; then
388         echo 1>&2 "$PRG: An error occured while writing to $boot"
389         return 1
390     fi
391
392     [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $bootconf on $boot..."
393     ## we comment out boot= and install=, because they are only really
394     ## needed in the /etc/elilo.conf file, and elilo.efi currently
395     ## doesn't understand them.  We also need to add /EFI/debian on to
396     ## the front of any paths that don't contain colons (device paths),
397     ## and replace tabs with spaces.
398     sed -e "s|^boot[[:space:]]*=|# &|" -e "s|^install[[:space:]]*=|# &|" \
399         -e "s|\t| |g" \
400         -e "s|\(^image[[:space:]]*=[[:space:]]*\)\([^:]*\)$|\1$EFIROOT\2|" \
401         -e "s|\(^[[:space:]]*initrd[[:space:]]*=[[:space:]]*\)\([^:]*\)$|\1$EFIROOT\2|" \
402         -e "s|\(^[[:space:]]*vmm[[:space:]]*=[[:space:]]*\)\([^:]*\)$|\1$EFIROOT\2|" \
403         < "$bootconf" > "$TARGET/$EFIROOT/$CFFILE"
404     if [ $? != 0 ] ; then
405         echo 1>&2 "$PRG: An error occured while writing to $boot"
406         return 1
407     fi
408
409     [ "$DEBUG" = 1 ] && echo "----" && cat "$TARGET/$EFIROOT/$CFFILE" && echo "----"
410
411     for i in $imagefiles $initrdfiles $vmmfiles; do
412         [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $i on $boot..."
413         if [ -f $i ]; then
414             mkdir -p `dirname "$TARGET/$EFIROOT/$i"`
415             if [ $? != 0 ] ; then
416                 echo 1>&2 "$PRG: An error occured creating directory `dirname $EFIROOT/$i` on $boot"
417                 return 1
418             fi
419             cp -f "$i" "$TARGET/$EFIROOT/$i"
420             if [ $? != 0 ] ; then
421                 echo 1>&2 "$PRG: An error occured writing $i to $boot"
422                 return 1
423             fi
424         else
425             echo "$PRG: Warning: $i not found"
426         fi
427     done
428
429     sync ; sync
430
431     ## update the boot-device variable in EFI.
432     if [ "$efiboot" = 1 ] ; then
433         [ "$VERBOSE" = 1 ] && echo "$PRG: Updating EFI boot-device variable..."
434         efiquiet="-q"
435         [ "$VERBOSE" = 1 ] && efiquiet=""
436         if ckdevfs "$boot" ; then
437             BOOTDISK="${boot%/*}/disc"
438             BOOTPART="${boot##*part}"
439         elif ckcciss "$boot" ; then
440             BOOTDISK="${boot%p[0-9]*}"
441             BOOTPART="${boot##*[a-z]}"
442         else
443             BOOTDISK="${boot%%[0-9]*}"
444             BOOTPART="${boot##*[a-z]}"
445         fi
446         if [ -z "$BOOTDISK" ] || [ -z "$BOOTPART" ] ; then
447             echo 2>&1 "$PRG: Could not determine boot disk, aborting..."
448             return 1
449         fi
450
451         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-disk      = $BOOTDISK"
452         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-partition = $BOOTPART"
453         # delete other entries with name "Debian GNU/Linux"
454         for b in `efibootmgr | grep "Debian GNU/Linux" | awk '{print substr($1,5,4) }'`; do
455             efibootmgr $efiquiet -b $b -B
456         done
457         # Add a new entry for this installation
458         efibootmgr $efiquiet -c -d $BOOTDISK -p $BOOTPART -w -L "Debian GNU/Linux" \
459                 -l \\EFI\\debian\\elilo.efi -u -- elilo -C \\EFI\\debian\\elilo.conf
460         if [ $? != 0 ] ; then
461             echo 1>&2 "$PRG: An error occured while updating boot menu, we'll ignore it"
462         fi
463         # Now, if 2nd and 3rd boot entries are for floppy and CD/DVD,
464         # move them up to 1st and 2nd, making our entry the 3rd.
465         bootorder=$(efibootmgr | sed -n 's/^BootOrder: \(.*\)$/\1/p')
466         boot1st=$(echo $bootorder | sed -n "s/\(....\).*$/\1/p")
467         boot2nd=$(echo $bootorder | sed -n "s/....,\(....\).*$/\1/p")
468         boot3rd=$(echo $bootorder | sed -n "s/....,....,\(....\).*$/\1/p")
469         boot456=$(echo $bootorder | sed -n "s/....,....,....\(.*\).*$/\1/p")
470         name2nd=$(efibootmgr | sed -n "s/^Boot$boot2nd[\*] \(.*\)$/\1/p")
471         name3rd=$(efibootmgr | sed -n "s/^Boot$boot3rd[\*] \(.*\)$/\1/p")
472         name23="@$name2nd@$name3rd"
473         if ( echo $name23 | grep -qi "@floppy" ); then
474             if ( echo $name23 | grep -qi "@cd") || ( echo $name23 | grep -qi "@dvd"); then
475                 efibootmgr $efiquiet -o $boot2nd,$boot3rd,$boot1st$boot456
476             fi
477         fi
478     fi
479
480     return 0
481 }
482
483 ## mkefifs function.
484 mkefifs()
485 {
486     mount | grep "^$boot\>" > /dev/null
487     if [ $? = 0 ] ; then
488         echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
489         return 1
490     fi
491
492     if (command -v mkdosfs > /dev/null 2>&1) ; then
493         [ -x `command -v mkdosfs` ] || FAIL=1 ; else FAIL=1 ; fi
494         if [ "$FAIL" = 1 ] ; then
495             echo 1>&2 "$PRG: mkdosfs is not installed or cannot be found"
496             return 1
497         fi
498
499     [ "$VERBOSE" = 1 ] && echo "$PRG: Creating DOS filesystem on $boot..."
500     mkdosfs -n bootstrap "$boot" > /dev/null
501     if [ $? != 0 ] ; then
502         echo 1>&2 "$PRG: DOS filesystem creation failed!"
503         return 1
504     fi
505     return 0
506 }
507
508 mkconf()
509 {
510 ## defaults for this are defined at the beginning of the script with
511 ## other variables.
512
513 # We want to create an append= line from the current /proc/cmdline,
514 # so things like console=ttyS0 get picked up automatically.
515 # We also want to filter out bits of cmdline we are not interested in.
516
517 if [ -f /proc/cmdline ]; then
518   cmdline=`cat /proc/cmdline`
519 else
520   echo 1>&2 "$PRG: Warning: couldn't read /proc/cmdline, may need to add append=... to elilo.conf"
521   cmdline=""
522 fi
523
524 append=`echo $cmdline | tr ' ' '\n' | grep "^console=" | tr '\n' ' '`
525 if [ ! -z "$append" ]; then append="append=\"$append\""; fi
526
527 echo \
528 "## elilo configuration file generated by elilo $VERSION
529
530 install=$install
531 boot=$boot
532 delay=$timeout
533 default=$label
534 " > "$TMPCONF" || return 1
535
536 if [ "$ARCHITECTURE" = "ia64" ]
537 then
538   echo "relocatable" >> "$TMPCONF" || return 1
539 fi
540
541 echo \
542 "$append
543
544 image=$image
545         label=$label
546         root=$root
547         read-only
548         $initrdline
549
550 image=${image}.old
551         label=${label}OLD
552         root=$root
553         read-only
554         $initrdoldline
555 " >> "$TMPCONF" || return 1
556
557     ## Copy the new elilo.conf to /etc
558     if [ -f $CONF ]; then
559         echo 1>&2 "$PRG: backing up existing $CONF as ${CONF}-"
560         rm -f ${CONF}-
561         mv $CONF ${CONF}-
562     fi
563     cp -f "$TMPCONF" "$CONF"
564     if [ $? != 0 ] ; then
565         echo 1>&2 "$PRG: An error occured while writing to $conf"
566         return 1
567     fi
568
569 return 0
570 }
571
572 # check partition will be big enough for all we want to add to it
573
574 chkspace()
575 {
576     imagefiles=`grep '^image[[:space:]]*=' $bootconf | \
577                 sed 's/^image[[:space:]]*=[[:space:]]*//' | grep -v ':'`
578     initrdfiles=`grep '^[[:space:]]*initrd[[:space:]]*=' $bootconf | \
579                 sed 's/.*=[[:space:]]*//' | grep -v ':'`
580     vmmfiles=`grep '^[[:space:]]*vmm[[:space:]]*=' $bootconf | \
581                 sed 's/.*=[[:space:]]*//' | grep -v ':'`
582     bytesneeded=`cat $imagefiles $initrdfiles $vmmfiles $install $bootconf 2>/dev/null | wc -c`
583     # convert to KB, allowing 5% overhead
584     kbneeded=$(( bytesneeded / 1024 + bytesneeded / 20480 ))
585     kbavailable=$(df -P -k $TARGET | sed -n "s|^$boot[[:space:]]\+[0-9]\+[[:space:]]\+[0-9]\+[[:space:]]\+\([0-9]\+\).*$|\1|p")
586     if [ -z $kbavailable ]; then
587         echo 2>&1 "$PRG: unable to determine space on $boot, aborting"
588         return 1
589     fi
590     if [ -d $TARGET/$EFIROOT ]; then
591         kbused=$(du -ks $TARGET/$EFIROOT | sed -n "s/[  ].*$//p")
592     else
593         kbused=0
594     fi
595     [ "$VERBOSE" = 1 ] && echo "$PRG: ${kbneeded}KB needed, ${kbavailable}KB free, ${kbused}KB to reuse"
596     kbavailable=$(( kbavailable + kbused ))
597     if [ "$kbavailable" -lt "$kbneeded" ] ; then
598         echo 1>&2 "$PRG: Insufficient space on $boot, need ${kbneeded}KB, only ${kbavailable}KB available"
599         return 1
600     fi
601 return 0
602 }
603
604
605 ## take out the trash.
606 cleanup()
607 {
608     if [ -n "$TARGET" ]; then
609         TARGET=
610         [ "$VERBOSE" = 1 ] && echo "$PRG: Unmounting $boot"
611         umount "$boot"
612         [ $? != 0 ] && echo 2>&1 "$PRG: Warning, failed to unmount $TARGET"
613     fi
614     if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
615     if [ -d "$TMP/bootstrap.$$" ] ; then rmdir "$TMP/bootstrap.$$" ; fi
616     if [ "$umountproc" = 1 ] ; then umount /proc ; fi
617     return 0
618 }
619
620 ##########
621 ## Main ##
622 ##########
623
624 ## absurdly bloated case statement to parse command line options.
625 if [ $# != 0 ] ; then
626     while true ; do
627         case "$1" in 
628             -V|--version)
629                 version
630                 exit 0
631                 ;;
632             -h|--help)
633                 usage
634                 exit 0
635                 ;;
636             --debug)
637                 DEBUG=1
638                 shift
639                 ;;
640             -v|--verbose)
641                 VERBOSE=1
642                 shift
643                 ;;
644             --force)
645                 # allow --force for now, boot-floppies 3.0.20 and
646                 # systemconfigurator use that instead of --format
647                 echo 1>&2 "$PRG: Warning: --force is now deprecated.  Use --for\mat."
648                 echo 1>&2 "Try \`$PRG --help' for more information."
649                 FORMAT=yes
650                 shift
651                 ;;
652             --format)
653                 FORMAT=yes
654                 shift
655                 ;;
656             --autoconf)
657                 autoconf=1
658                 shift
659                 ;;
660             -b|--boot)
661                 if [ -n "$2" ] ; then
662                     boot="$2"
663                     ARGBT=1
664                     shift 2
665                 else
666                     echo 1>&2 "$PRG: option requires an argument $1"
667                     echo 1>&2 "Try \`$PRG --help' for more information."
668                     exit 1
669                 fi
670                 ;;
671             -i|--install)
672                 if [ -n "$2" ] ; then
673                     install="$2"
674                     ARGBF=1
675                     shift 2
676                 else
677                     echo 1>&2 "$PRG: option requires an argument $1"
678                     echo 1>&2 "Try \`$PRG --help' for more information."
679                     exit 1
680                 fi
681                 ;;
682             -C|--config)
683                 if [ -n "$2" ] ; then
684                     CONF="$2"
685                     bootconf="$2"
686                     ERR=" Error in $CONF:"
687                     shift 2
688                 else
689                     echo 1>&2 "$PRG: option requires an argument $1"
690                     echo 1>&2 "Try \`$PRG --help' for more information."
691                     exit 1
692                 fi
693                 ;;
694             --efiboot)
695                 efiboot=1
696                 ARGNV=1
697                 shift
698                 ;;
699             --timeout)
700                 if [ -n "$2" ] ; then
701                     timeout="$2"
702                     bootconf=auto
703                     shift 2
704                 else
705                     echo 1>&2 "$PRG: option requires an argument $1"
706                     echo 1>&2 "Try \`$PRG --help' for more information."
707                     exit 1
708                 fi
709                 ;;
710             --image)
711                 if [ -n "$2" ] ; then
712                     image="$2"
713                     bootconf=auto
714                     shift 2
715                 else
716                     echo 1>&2 "$PRG: option requires an argument $1"
717                     echo 1>&2 "Try \`$PRG --help' for more information."
718                     exit 1
719                 fi
720                 ;;
721             --label)
722                 if [ -n "$2" ] ; then
723                     label="$2"
724                     bootconf=auto
725                     shift 2
726                 else
727                     echo 1>&2 "$PRG: option requires an argument $1"
728                     echo 1>&2 "Try \`$PRG --help' for more information."
729                     exit 1
730                 fi
731                 ;;
732             --root)
733                 if [ -n "$2" ] ; then
734                     root="$2"
735                     bootconf=auto
736                     shift 2
737                 else
738                     echo 1>&2 "$PRG: option requires an argument $1"
739                     echo 1>&2 "Try \`$PRG --help' for more information."
740                     exit 1
741                 fi
742                 ;;
743             "")
744                 break
745                 ;;
746             *)
747                 echo 1>&2 "$PRG: unrecognized option \`$1'"
748                 echo 1>&2 "Try \`$PRG --help' for more information."
749                 exit 1
750                 ;;
751         esac
752     done
753 fi
754
755 ## check that are root
756 if [ `id -u` != 0 ] ; then
757     echo 1>&2 "$PRG: requires root privileges, go away."
758     exit 1
759 fi
760
761 ## check that autoconf options are only specified with --autoconf
762 if [ "$bootconf" = "auto" ] && [ "$autoconf" = "0" ] ; then
763     echo 1>&2 "$PRG: Auto-config options specified without --autoconf."
764     exit 1;
765 fi
766
767 ## check that specified config file exists, unless we are to generate it,
768 ## which case we assume all options are done on the command line.
769 if [ "$autoconf" = "0" ] ; then
770     confexist || exit 1
771 fi
772
773 ## /proc is needed to parse /proc/partitions, etc.
774 if [ ! -f /proc/uptime ]; then
775     [ "$VERBOSE" = 1 ] && echo "$PRG: Mounting /proc..."
776     mount -t proc proc /proc 2> /dev/null
777     if [ $? != 0 ]; then
778         echo 1>&2 "$PRG: Failed to mount /proc, aborting."
779         exit 1
780     fi
781     umountproc=1
782 fi
783
784 # We need vfat support, so make sure it is loaded
785 modprobe vfat >/dev/null 2>&1
786
787 ## elilo.conf autogeneration. MUST have secure mktemp to
788 ## avoid race conditions. Debian's mktemp qualifies.
789 if [ "$autoconf" = "1" ] ; then
790     TMPCONF=`mktemp -q "$TMP/$PRG.XXXXXX"`
791     if [ $? != 0 ] ; then
792         echo 1>&2 "$PRG: Could not create temporary file, aborting."
793         exit 1
794     fi
795     mkconf
796     if [ $? != 0 ] ; then
797         echo 1>&2 "$PRG: An error occured generating elilo.conf, aborting."
798         exit 1
799     fi
800
801     bootconf="$TMPCONF"
802 fi
803
804 ## Checks if each option was defined on the command line, and if so
805 ## don't read it from the configuration file. this avoids
806 ## configuration options from being set null, as well as command line
807 ## options from being clobbered.
808 [ "$ARGBT" != 1 ] && [ $(parseconf ck boot) = 0 ] && boot=`parseconf str boot`
809
810 ## ffs!! rtfm! foad!
811 if [ "$boot" = unconfigured ] ; then
812     echo 1>&2 "$PRG: You must specify the device for the bootstrap partition. (ie: -b /dev/hdaX)"
813     echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
814     exit 1
815 fi
816
817 ## validate configuration for sanity.
818 checkconf || exit 1
819
820 if [ "$FORMAT" = "yes" ]; then
821     mkefifs || exit 1
822 fi
823
824 [ "$VERBOSE" = 1 ] && echo "$PRG: Checking filesystem on $boot..."
825 dosfsck $boot > /dev/null
826 if [ $? != 0 ]; then
827     echo 1>&2 "$PRG: Filesystem on $boot is corrupt, please fix that and rerun $PRG."
828     exit 1
829 fi
830
831 mnt || exit 1
832 chkspace || exit 1
833 copyfiles || exit 1
834
835 umount $TARGET
836 if [ $? != 0 ]; then
837     echo 1>&2 "$PRG: Failed to unmount $boot"
838     exit 1
839 fi
840 TARGET=
841
842 [ "$VERBOSE" = 1 ] && echo "$PRG: Installation complete."
843
844 exit 0