3 ###############################################################################
5 ## elilo installs efi bootloader onto a bootstrap partition (based on ybin)
6 ## Copyright (C) 2001 Ethan Benson
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.
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.
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.
22 ###############################################################################
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"
30 SIGINT="$PRG: Interrupt caught ... exiting"
36 # Beware, /EFI/debian occurs with double backslashes in the script too
37 # so change those as well as EFIROOT, if need be.
41 ## catch signals, clean up junk in /tmp.
43 trap "cleanup; exit 129" HUP
44 trap "echo 1>&2 $SIGINT ; cleanup; exit 130" INT
45 trap "cleanup; exit 131" QUIT
46 trap "cleanup; exit 143" TERM
48 ## define default config file
51 ERR=" Error in $CONF:"
53 ## define default configuration
56 ## allow default to work on packaged and non-packaged elilo.
57 if [ -f /usr/local/lib/elilo/elilo.efi ] ; then
58 install=/usr/local/lib/elilo/elilo.efi
59 elif [ -f /usr/lib/elilo/elilo.efi ] ; then
60 install=/usr/lib/elilo/elilo.efi
69 ## elilo autoconf defaults
74 # image default is controlled by /etc/kernel-img.conf, if it exists
75 if [ -f /etc/kernel-img.conf ] &&
76 grep -E -qi "^(image|link)_in_boot *= *yes" /etc/kernel-img.conf; then
78 initrdline=initrd=/boot/initrd.img
79 initrdoldline=initrd=/boot/initrd.img.old
82 initrdline=initrd=/initrd.img
83 initrdoldline=initrd=/initrd.img.old
85 if [ -f /etc/kernel-img.conf ] &&
86 ! grep -qi "^do_initrd *= *yes" /etc/kernel-img.conf; then
91 ## make fake `id' if its missing, outputs 0 since if its missing we
92 ## are probably running on boot floppies and thus are root.
93 if (command -v id > /dev/null 2>&1) ; then
107 Written by Richard Hirst, based on work by Ethan Benson
109 This is free software; see the source for copying conditions. There is NO
110 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
117 "Usage: $PRG [OPTION]...
118 Update/install bootloader onto a bootstrap partition.
120 -b, --boot set bootstrap partition device [ -b /dev/sda1 ]
121 -i, --install pathname to the actual bootloader binary
122 default: /usr/{local/}lib/elilo/elilo.efi
123 -C, --config use alternate configuration file [ -C config_file ]
124 --autoconf auto-generate a /etc/elilo.conf
125 --efiboot elilo auto configuration: create an efi boot
126 manager entry for elilo
127 --timeout elilo auto configuration: sets the time elilo
128 will wait for user input before booting default
129 image default: 20 (2 seconds)
130 --image elilo auto configuration: sets the path to the
131 kernel image. default: /vmlinuz
132 --label elilo auto configuration: sets the image label
134 --root elilo auto configuration: sets the root device
136 --format create a new FAT filesystem on the boot partition
137 -v, --verbose make $PRG more verbose
138 --debug print boring junk only useful for debugging
139 -h, --help display this help and exit
140 -V, --version output version information and exit"
143 ## we have to do some things differently with a retarded devfs name.
147 /dev/ide/*|/dev/scsi/*|/dev/discs/*)
156 ## the SmartArray RAID controllers use /dev/cciss/c0d0p1 kinds of names...
170 ## configuration file parsing. FIXME: need a method which can parse
176 v=`grep "^$2[\ ,=]" "$CONF"` ; echo "${v#*=}"
179 grep "^$2\>" "$CONF" > /dev/null && echo 0 || echo 1
182 grep "^$2[\ ,=]" "$CONF" > /dev/null && echo 0 || echo 1
187 ## check for existence of a configuration file, and make sure we have
191 if [ ! -e "$CONF" ] ; then
192 echo 1>&2 "$PRG: $CONF: No such file or directory"
194 elif [ ! -f "$CONF" ] ; then
195 echo 1>&2 "$PRG: $CONF: Not a regular file"
197 elif [ ! -r "$CONF" ] ; then
198 echo 1>&2 "$PRG: $CONF: Permission denied"
205 ## check to make sure the configuration file is sane and correct.
206 ## maybe this is an insane ammount of error checking, but I want to
207 ## make sure (hopefully) nothing unexpected ever happens. and i just
208 ## like useful errors from programs. every error just marks an error
209 ## variable so we give the user as much info as possible before we
213 if [ -L "$boot" ] ; then
215 boot=$(readlink -f $oldboot)
216 echo 1>&2 "$PRG: $oldboot is a symbolic link, using $boot instead"
218 if [ ! -e "$boot" ] ; then
219 echo 1>&2 "$PRG: $boot: No such file or directory"
221 elif [ ! -b "$boot" ] && [ ! -f "$boot" ] ; then
222 echo 1>&2 "$PRG: $boot: Not a regular file or block device"
224 elif [ ! -w "$boot" ] || [ ! -r "$boot" ] ; then
225 echo 1>&2 "$PRG: $boot: Permission denied"
229 ## sanity check, make sure boot=bootstrap and not something dumb
233 echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
237 echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
241 echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of all data on $boot"
246 ## now make sure its not something dumb like the root partition
247 ROOT="$(v=`df / 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
248 BOOT="$(v=`df /boot 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
249 if [ "$boot" = "$ROOT" ] ; then
250 echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of the root filesystem"
252 elif [ "$boot" = "$BOOT" ] ; then
253 echo 1>&2 "$PRG:$ERR \`boot=$boot' would result in the destruction of the /boot filesystem"
257 ## Make sure boot is not already mounted
258 mount | grep "^$boot " > /dev/null
260 echo 1>&2 "$PRG: $boot appears to be mounted"
264 if [ ! -e "$install" ] ; then
265 echo 1>&2 "$PRG: $install: No such file or directory"
267 elif [ ! -f "$install" ] ; then
268 echo 1>&2 "$PRG: $install: Not a regular file"
270 elif [ ! -r "$install" ] ; then
271 echo 1>&2 "$PRG: $install: Permission denied"
275 if [ ! -e "$bootconf" ] ; then
276 echo 1>&2 "$PRG: $bootconf: No such file or directory"
278 elif [ ! -f "$bootconf" ] ; then
279 echo 1>&2 "$PRG: $bootconf: Not a regular file"
281 elif [ ! -r "$bootconf" ] ; then
282 echo 1>&2 "$PRG: $bootconf: Permission denied"
286 if [ ! -d /proc/efi/vars ] && [ ! -d /sys/firmware/efi/vars ] && [ "$efiboot" = 1 ] ; then
287 echo 1>&2 "$PRG: no efi/vars under /proc or /sys/firmware, boot menu not updated"
291 if [ "$efiboot" = 1 ] ; then
292 ## see if efibootmgr exists and is executable
293 if (command -v efibootmgr > /dev/null 2>&1) ; then
294 [ -x `command -v efibootmgr` ] || MISSING=1 ; else MISSING=1
297 if [ "$MISSING" = 1 ] ; then
299 echo 1>&2 "$PRG: Warning: \`efibootmgr' could not be found, boot menu not updated"
302 if [ -f "$boot" ] ; then
303 echo 1>&2 "$PRG: $boot is a regular file, disabling boot menu update"
308 if [ "$CONFERR" = 1 ] ; then
318 ## we can even create bootstrap filesystem images directly if you
320 if [ -f "$boot" ] ; then
324 if [ -e "$TMP/bootstrap.$$" ] ; then
325 echo 1>&2 "$PRG: $TMP/bootstrap.$$ exists, aborting."
329 mkdir -m 700 "$TMP/bootstrap.$$"
330 if [ $? != 0 ] ; then
331 echo 1>&2 "$PRG: Could not create mountpoint directory, aborting."
335 mount | grep "^$boot " > /dev/null
337 echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
341 [ "$VERBOSE" = 1 ] && echo "$PRG: Mounting $boot..."
342 mount -t "$fstype" -o codepage=437,iocharset=iso8859-1,rw,noexec,umask=077$loop "$boot" "$TMP/bootstrap.$$"
343 if [ $? != 0 ] ; then
344 echo 1>&2 "$PRG: An error occured mounting $boot"
348 TARGET="$TMP/bootstrap.$$"
356 imagefiles=`grep '^image[[:space:]]*=' $bootconf | \
357 sed 's/^image[[:space:]]*=[[:space:]]*//' | grep -v ':'`
358 initrdfiles=`grep '^[[:space:]]*initrd[[:space:]]*=' $bootconf | \
359 sed 's/.*=[[:space:]]*//' | grep -v ':'`
361 ## Point of no return, removing the old EFI/debian tree
362 rm -rf $TARGET/$EFIROOT
364 echo 2>&1 "$PRG: Failed to delete old boot files, aborting"
367 mkdir -p $TARGET/$EFIROOT
369 ## Add a README to warn that this tree is deleted every time elilo is run
371 This directory tree is managed by /usr/sbin/elilo, and is deleted and\n\
372 recreated every time elilo runs. Any local changes will be lost.\n\
373 " > $TARGET/$EFIROOT/README.TXT
375 ## this is probably insecure on modern filesystems, but i think
376 ## safe on crippled hfs/dosfs.
377 [ "$VERBOSE" = 1 ] && echo "$PRG: Installing primary bootstrap $install onto $boot..."
378 cp -f "$install" "$TARGET/$EFIROOT/$BTFILE"
379 if [ $? != 0 ] ; then
380 echo 1>&2 "$PRG: An error occured while writing to $boot"
384 [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $bootconf on $boot..."
385 ## we comment out boot= and install=, because they are only really
386 ## needed in the /etc/elilo.conf file, and elilo.efi currently
387 ## doesn't understand them. We also need to add /EFI/debian on to
388 ## the front of any paths that don't contain colons (device paths),
389 ## and replace tabs with spaces.
390 sed -e "s|^boot[[:space:]]*=|# &|" -e "s|^install[[:space:]]*=|# &|" \
392 -e "s|\(^image[[:space:]]*=[[:space:]]*\)\([^:]*\)$|\1$EFIROOT\2|" \
393 -e "s|\(^[[:space:]]*initrd[[:space:]]*=[[:space:]]*\)\([^:]*\)$|\1$EFIROOT\2|" \
394 < "$bootconf" > "$TARGET/$EFIROOT/$CFFILE"
395 if [ $? != 0 ] ; then
396 echo 1>&2 "$PRG: An error occured while writing to $boot"
400 [ "$DEBUG" = 1 ] && echo "----" && cat "$TARGET/$EFIROOT/$CFFILE" && echo "----"
402 for i in $imagefiles $initrdfiles; do
403 [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $i on $boot..."
405 mkdir -p `dirname "$TARGET/$EFIROOT/$i"`
406 if [ $? != 0 ] ; then
407 echo 1>&2 "$PRG: An error occured creating directory `dirname $EFIROOT/$i` on $boot"
410 cp -f "$i" "$TARGET/$EFIROOT/$i"
411 if [ $? != 0 ] ; then
412 echo 1>&2 "$PRG: An error occured writing $i to $boot"
416 echo "$PRG: Warning: $i not found"
422 ## update the boot-device variable in EFI.
423 if [ "$efiboot" = 1 ] ; then
424 [ "$VERBOSE" = 1 ] && echo "$PRG: Updating EFI boot-device variable..."
426 [ "$VERBOSE" = 1 ] && efiquiet=""
427 if ckdevfs "$boot" ; then
428 BOOTDISK="${boot%/*}/disc"
429 BOOTPART="${boot##*part}"
430 elif ckcciss "$boot" ; then
431 BOOTDISK="${boot%p[0-9]*}"
432 BOOTPART="${boot##*[a-z]}"
434 BOOTDISK="${boot%%[0-9]*}"
435 BOOTPART="${boot##*[a-z]}"
437 if [ -z "$BOOTDISK" ] || [ -z "$BOOTPART" ] ; then
438 echo 2>&1 "$PRG: Could not determine boot disk, aborting..."
442 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-disk = $BOOTDISK"
443 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-partition = $BOOTPART"
444 # delete other entries with name "Debian GNU/Linux"
445 for b in `efibootmgr | grep "Debian GNU/Linux" | awk '{print substr($1,5,4) }'`; do
446 efibootmgr $efiquiet -b $b -B
448 # Add a new entry for this installation
449 efibootmgr $efiquiet -c -d $BOOTDISK -p $BOOTPART -w -L "Debian GNU/Linux" \
450 -l \\EFI\\debian\\elilo.efi -u -- elilo -C \\EFI\\debian\\elilo.conf
451 if [ $? != 0 ] ; then
452 echo 1>&2 "$PRG: An error occured while updating boot menu, we'll ignore it"
454 # Now, if 2nd and 3rd boot entries are for floppy and CD/DVD,
455 # move them up to 1st and 2nd, making our entry the 3rd.
456 bootorder=$(efibootmgr | sed -n 's/^BootOrder: \(.*\)$/\1/p')
457 boot1st=$(echo $bootorder | sed -n "s/\(....\).*$/\1/p")
458 boot2nd=$(echo $bootorder | sed -n "s/....,\(....\).*$/\1/p")
459 boot3rd=$(echo $bootorder | sed -n "s/....,....,\(....\).*$/\1/p")
460 boot456=$(echo $bootorder | sed -n "s/....,....,....\(.*\).*$/\1/p")
461 name2nd=$(efibootmgr | sed -n "s/^Boot$boot2nd[\*] \(.*\)$/\1/p")
462 name3rd=$(efibootmgr | sed -n "s/^Boot$boot3rd[\*] \(.*\)$/\1/p")
463 name23="@$name2nd@$name3rd"
464 if ( echo $name23 | grep -qi "@floppy" ); then
465 if ( echo $name23 | grep -qi "@cd") || ( echo $name23 | grep -qi "@dvd"); then
466 efibootmgr $efiquiet -o $boot2nd,$boot3rd,$boot1st$boot456
477 mount | grep "^$boot\>" > /dev/null
479 echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
483 if (command -v mkdosfs > /dev/null 2>&1) ; then
484 [ -x `command -v mkdosfs` ] || FAIL=1 ; else FAIL=1 ; fi
485 if [ "$FAIL" = 1 ] ; then
486 echo 1>&2 "$PRG: mkdosfs is not installed or cannot be found"
490 [ "$VERBOSE" = 1 ] && echo "$PRG: Creating DOS filesystem on $boot..."
491 mkdosfs -n bootstrap "$boot" > /dev/null
492 if [ $? != 0 ] ; then
493 echo 1>&2 "$PRG: DOS filesystem creation failed!"
501 ## defaults for this are defined at the beginning of the script with
504 # We want to create an append= line from the current /proc/cmdline,
505 # so things like console=ttyS0 get picked up automatically.
506 # We also want to filter out bits of cmdline we are not interested in.
508 if [ -f /proc/cmdline ]; then
509 cmdline=`cat /proc/cmdline`
511 echo 1>&2 "$PRG: Warning: couldn't read /proc/cmdline, may need to add append=... to elilo.conf"
515 append=`echo $cmdline | tr ' ' '\n' | grep "^console=" | tr '\n' ' '`
516 if [ ! -z "$append" ]; then append="append=\"$append\""; fi
519 "## elilo configuration file generated by elilo $VERSION
525 " > "$TMPCONF" || return 1
527 if [ -d /proc/sgi_sn ]
529 # aha! This is an SGI Altix, and they need the relocatable option.
530 echo "relocatable" >> "$TMPCONF" || return 1
547 " >> "$TMPCONF" || return 1
549 ## Copy the new elilo.conf to /etc
550 if [ -f $CONF ]; then
551 echo 1>&2 "$PRG: backing up existing $CONF as ${CONF}-"
555 cp -f "$TMPCONF" "$CONF"
556 if [ $? != 0 ] ; then
557 echo 1>&2 "$PRG: An error occured while writing to $conf"
564 # check partition will be big enough for all we want to add to it
568 imagefiles=`grep '^image[[:space:]]*=' $bootconf | \
569 sed 's/^image[[:space:]]*=[[:space:]]*//' | grep -v ':'`
570 initrdfiles=`grep '^[[:space:]]*initrd[[:space:]]*=' $bootconf | \
571 sed 's/.*=[[:space:]]*//' | grep -v ':'`
572 bytesneeded=`cat $imagefiles $initrdfiles $install $bootconf 2>/dev/null | wc -c`
573 # convert to KB, allowing 5% overhead
574 kbneeded=$(( bytesneeded / 1024 + bytesneeded / 20480 ))
575 kbavailable=$(df -P -k $TARGET | sed -n "s|^$boot[[:space:]]\+[0-9]\+[[:space:]]\+[0-9]\+[[:space:]]\+\([0-9]\+\).*$|\1|p")
576 if [ -z $kbavailable ]; then
577 echo 2>&1 "$PRG: unable to determine space on $boot, aborting"
580 if [ -d $TARGET/$EFIROOT ]; then
581 kbused=$(du -ks $TARGET/$EFIROOT | sed -n "s/[ ].*$//p")
585 [ "$VERBOSE" = 1 ] && echo "$PRG: ${kbneeded}KB needed, ${kbavailable}KB free, ${kbused}KB to reuse"
586 kbavailable=$(( kbavailable + kbused ))
587 if [ "$kbavailable" -lt "$kbneeded" ] ; then
588 echo 1>&2 "$PRG: Insufficient space on $boot, need ${kbneeded}KB, only ${kbavailable}KB available"
595 ## take out the trash.
598 if [ -n "$TARGET" ]; then
600 [ "$VERBOSE" = 1 ] && echo "$PRG: Unmounting $boot"
602 [ $? != 0 ] && echo 2>&1 "$PRG: Warning, failed to unmount $TARGET"
604 if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
605 if [ -d "$TMP/bootstrap.$$" ] ; then rmdir "$TMP/bootstrap.$$" ; fi
606 if [ "$umountproc" = 1 ] ; then umount /proc ; fi
614 ## absurdly bloated case statement to parse command line options.
615 if [ $# != 0 ] ; then
635 # allow --force for now, boot-floppies 3.0.20 and
636 # systemconfigurator use that instead of --format
637 echo 1>&2 "$PRG: Warning: --force is now deprecated. Use --for\mat."
638 echo 1>&2 "Try \`$PRG --help' for more information."
651 if [ -n "$2" ] ; then
656 echo 1>&2 "$PRG: option requires an argument $1"
657 echo 1>&2 "Try \`$PRG --help' for more information."
662 if [ -n "$2" ] ; then
667 echo 1>&2 "$PRG: option requires an argument $1"
668 echo 1>&2 "Try \`$PRG --help' for more information."
673 if [ -n "$2" ] ; then
676 ERR=" Error in $CONF:"
679 echo 1>&2 "$PRG: option requires an argument $1"
680 echo 1>&2 "Try \`$PRG --help' for more information."
690 if [ -n "$2" ] ; then
695 echo 1>&2 "$PRG: option requires an argument $1"
696 echo 1>&2 "Try \`$PRG --help' for more information."
701 if [ -n "$2" ] ; then
706 echo 1>&2 "$PRG: option requires an argument $1"
707 echo 1>&2 "Try \`$PRG --help' for more information."
712 if [ -n "$2" ] ; then
717 echo 1>&2 "$PRG: option requires an argument $1"
718 echo 1>&2 "Try \`$PRG --help' for more information."
723 if [ -n "$2" ] ; then
728 echo 1>&2 "$PRG: option requires an argument $1"
729 echo 1>&2 "Try \`$PRG --help' for more information."
737 echo 1>&2 "$PRG: unrecognized option \`$1'"
738 echo 1>&2 "Try \`$PRG --help' for more information."
745 ## check that are root
746 if [ `id -u` != 0 ] ; then
747 echo 1>&2 "$PRG: requires root privileges, go away."
751 ## check that autoconf options are only specified with --autoconf
752 if [ "$bootconf" = "auto" ] && [ "$autoconf" = "0" ] ; then
753 echo 1>&2 "$PRG: Auto-config options specified without --autoconf."
757 ## check that specified config file exists, unless we are to generate it,
758 ## which case we assume all options are done on the command line.
759 if [ "$autoconf" = "0" ] ; then
763 ## /proc is needed to parse /proc/partitions, etc.
764 if [ ! -f /proc/uptime ]; then
765 [ "$VERBOSE" = 1 ] && echo "$PRG: Mounting /proc..."
766 mount -t proc proc /proc 2> /dev/null
768 echo 1>&2 "$PRG: Failed to mount /proc, aborting."
774 # We need vfat support, so make sure it is loaded
775 modprobe vfat >/dev/null 2>&1
777 ## elilo.conf autogeneration. MUST have secure mktemp to
778 ## avoid race conditions. Debian's mktemp qualifies.
779 if [ "$autoconf" = "1" ] ; then
780 TMPCONF=`mktemp -q "$TMP/$PRG.XXXXXX"`
781 if [ $? != 0 ] ; then
782 echo 1>&2 "$PRG: Could not create temporary file, aborting."
786 if [ $? != 0 ] ; then
787 echo 1>&2 "$PRG: An error occured generating elilo.conf, aborting."
794 ## Checks if each option was defined on the command line, and if so
795 ## don't read it from the configuration file. this avoids
796 ## configuration options from being set null, as well as command line
797 ## options from being clobbered.
798 [ "$ARGBT" != 1 ] && [ $(parseconf ck boot) = 0 ] && boot=`parseconf str boot`
801 if [ "$boot" = unconfigured ] ; then
802 echo 1>&2 "$PRG: You must specify the device for the bootstrap partition. (ie: -b /dev/hdaX)"
803 echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
807 ## validate configuration for sanity.
810 if [ "$FORMAT" = "yes" ]; then
814 [ "$VERBOSE" = 1 ] && echo "$PRG: Checking filesystem on $boot..."
815 dosfsck $boot > /dev/null
817 echo 1>&2 "$PRG: Filesystem on $boot is corrupt, please fix that and rerun $PRG."
827 echo 1>&2 "$PRG: Failed to unmount $boot"
832 [ "$VERBOSE" = 1 ] && echo "$PRG: Installation complete."