3 ###############################################################################
5 ## yabootconfig generates a simple /etc/yaboot.conf
6 ## Copyright (C) 2001, 2002 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"
32 ## $CONFIG is relative to $CHROOT
33 CONFIG=etc/yaboot.conf
37 SIGINT="$PRG: Interrupt caught ... exiting"
40 ## catch signals, clean up temporary file
43 trap "echo 1>&2 $SIGINT ; exit 130" 2
47 ## check for printf, use it if possible otherwise fall back on
48 ## unreliable echo -e -n ("SUS" says echo shall support no switches)
49 if [ "$(printf printf_test 2>/dev/null)" = printf_test ] ; then
55 ## make sure echo is not lame if we must use it.
56 if [ "$PRINTF" != printf ] ; then
57 if [ "$(echo -e -n echo_test)" != "echo_test" ] ; then
58 echo 1>&2 "$PRG: printf unavailable and echo is broken, sorry."
63 ## make fake `id' if its missing, outputs 0 since if its missing we
64 ## are probably running on boot floppies and thus are root.
65 if (command -v id > /dev/null 2>&1) ; then
79 Written by Ethan Benson
81 Copyright (C) 2001, 2002 Ethan Benson
82 This is free software; see the source for copying conditions. There is NO
83 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
90 "Usage: $PRG [OPTION]...
91 Generate a working /etc/yaboot.conf.
93 -t, --chroot set root directory $PRG should work from
94 -r, --root set root partition, Example: /dev/hda3
95 default: determined from {chroot}/etc/fstab
96 -b, --boot set bootstrap partition, Example: /dev/hda2
97 default: first type: Apple_Bootstrap partition
98 --kernel-args add an append= line with specified arguments
99 -q, --quiet don't ask any questions/confirmation
100 --noinstall don't automatically run mkofboot
101 -h, --help display this help and exit
102 -V, --version output version information and exit"
107 [ "$DEBUG" = 0 ] && return 0
108 $PRINTF 1>&2 "$PRG: DEBUG: $1"
114 "yaboot is the Linux Loader for PowerPC. $PRG sets up your system to boot directly
115 from your hard disk, without the need for a boot CD, floppy or a network boot.\n"
116 [ "$NOINSTALL" = 0 ] && $PRINTF "Install yaboot bootstrap on $BOOT to boot Linux from $ROOT? [Yes] "
117 [ "$NOINSTALL" = 1 ] && $PRINTF "Create simple ${CHROOT}${CONFIG} to boot Linux from $ROOT? [Yes] "
121 echo "Creating a simple ${CHROOT}${CONFIG}..."
125 if [ "$NOINSTALL" = 0 ] ; then
126 $PRINTF "Create simple ${CHROOT}${CONFIG} without installing the bootstrap? [Yes] "
131 echo 1>&2 "Creating a simple ${CHROOT}${CONFIG}..."
147 ## find out whether we have mac-fdisk or pdisk (they work the same)
150 if (command -v mac-fdisk > /dev/null 2>&1) ; then
152 elif (command -v pdisk > /dev/null 2>&1) ; then
155 echo 1>&2 "$PRG: Unable to locate mac-fdisk"
159 if [ ! -x `command -v $FDISK` 2> /dev/null ] ; then
160 echo 1>&2 "$PRG: `command -v $FDISK`: Permission denied"
163 debug "mac-fdisk is: $FDISK\n"
167 ## find out if we have ddisk or fdisk (fdisk for dos labels) debian
171 if (command -v ddisk > /dev/null 2>&1) ; then
173 elif (command -v fdisk > /dev/null 2>&1) ; then
176 echo 1>&2 "$PRG: Unable to locate fdisk"
180 if [ ! -x `command -v $FDISK` 2> /dev/null ] ; then
181 echo 1>&2 "$PRG: `command -v $FDISK`: Permission denied"
184 debug "fdisk is: $FDISK\n"
188 ## find bootstrap partition, supports IBM CHRP with msdos disklabels
191 ## mac partition table magic == ER
192 if [ "$(dd if="$DISK" bs=2 count=1 2> /dev/null)" = ER ] ; then
193 ckmacfdisk || return 1
194 if [ "$FDISK" = pdisk ] ; then
195 ## handle braindamaged pdisk
196 debug "dealing with pdisk deficiency...\n"
197 BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<Apple_Bootstrap\>'` ; echo ${v%%:*})"
198 debug "BOOT before fixup: $BOOT\n"
199 if [ -n "$BOOT" ] ; then
200 BOOT="${DISK}${BOOT}"
202 debug "BOOT after fixup: $BOOT\n"
204 BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<Apple_Bootstrap\>'` ; echo ${v%%[ ]*})"
207 if [ -z "$BOOT" ] ; then
208 echo 1>&2 "$PRG: Unable to locate bootstrap partition on $DISK..."
209 echo 1>&2 "$PRG: You must create an 800K type: Apple_Bootstrap partition to make the disk bootable"
214 BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<PPC PReP Boot\>'` ; echo ${v%%[ ]*})"
216 if [ -z "$BOOT" ] ; then
217 echo 1>&2 "$PRG: Unable to locate bootstrap partition on $DISK..."
218 echo 1>&2 "$PRG: You must create an 800K type: 0x41 PPC PReP Boot partition to make the disk bootable"
225 ## if readlink is missing use a kludge
226 if (command -v readlink > /dev/null 2>&1) ; then
231 SYMTARGET="$(v=`ls -l "$2" 2>/dev/null` ; echo ${v##*> })"
232 if [ -n "$SYMTARGET" ] ; then
241 ## we have to do some things differently with a retarded devfs name.
245 /dev/ide/*|/dev/scsi/*|/dev/discs/*)
256 if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
264 if [ $# != 0 ] ; then
276 if [ -n "$2" ] ; then
280 echo 1>&2 "$PRG: option requires an argument $1"
281 echo 1>&2 "Try \`$PRG --help' for more information."
286 if [ -n "$2" ] ; then
290 echo 1>&2 "$PRG: option requires an argument $1"
291 echo 1>&2 "Try \`$PRG --help' for more information."
296 if [ -n "$2" ] ; then
300 echo 1>&2 "$PRG: option requires an argument $1"
301 echo 1>&2 "Try \`$PRG --help' for more information."
306 if [ -n "$2" ] ; then
310 echo 1>&2 "$PRG: option requires an argument $1"
311 echo 1>&2 "Try \`$PRG --help' for more information."
331 echo 1>&2 "$PRG: unrecognized option \`$1'"
332 echo 1>&2 "Try \`$PRG --help' for more information."
339 if [ `id -u` != 0 ] ; then
340 echo 1>&2 "$PRG: You are not root, go away"
344 ## we need /proc because df does
345 if [ ! -f /proc/uptime ] ; then
346 echo 1>&2 "$PRG: This utility requires the /proc filesystem"
350 ## check that chroot exists
351 if [ -d "$CHROOT" ] ; then
352 ## HACK: add trailing / to chroot, otherwise are paths later get b0rked.
361 elif [ ! -e "$CHROOT" ] ; then
362 echo 1>&2 "$PRG: $CHROOT: No such file or directory"
364 elif [ ! -d "$CHROOT" ] ; then
365 echo 1>&2 "$PRG: $CHROOT: Not a directory"
369 ## make sure the chroot is an actual root filesystem
370 if [ ! -f "${CHROOT}etc/fstab" ] ; then
371 echo 1>&2 "$PRG: $CHROOT does not appear to be a valid root filesystem"
376 if [ -z "$ROOT" ] ; then
377 ## IMPORTANT! that last substitution is [<space><tab>] thats all ash will grok
378 ROOT="$(v=`grep '^[^#].*[[:blank:]]/[[:blank:]]' ${CHROOT}etc/fstab` ; echo ${v%%[ ]*})"
380 if [ -z "$ROOT" ] ; then
381 echo 1>&2 "$PRG: Could not determine root partition, aborting..."
386 ## make sure root device exists
387 if [ ! -e "$ROOT" ] ; then
388 echo 1>&2 "$PRG: $ROOT: No such file or directory"
393 if ckdevfs "$ROOT" ; then
394 DISK="${ROOT%/*}/disc"
396 DISK="${ROOT%%[0-9]*}"
398 if [ -z "$DISK" ] ; then
399 echo 1>&2 "$PRG: Could not determine root disk, aborting..."
403 ## make sure main disk exists
404 if [ ! -e "$DISK" ] ; then
405 echo 1>&2 "$PRG: $DISK: No such file or directory"
409 ## find bootstrap partition
410 if [ -z "$BOOT" ] ; then
411 findbootblock || exit 1
414 ## make sure bootstrap device exists
415 if [ ! -e "$BOOT" ] ; then
416 echo 1>&2 "$PRG: $BOOT: No such file or directory"
421 for i in "$DISK" "$ROOT" "$BOOT" ; do
422 if [ ! -b "$i" ] ; then
423 echo 1>&2 "$PRG: $i: Not a block device"
428 ## unless --quiet ask permission to proceed
429 if [ "$QUIET" = 0 ] ; then
433 ## find the kernel in the usual places and (if not --quiet) ask the
434 ## user if we cannot find one.
435 if [ -f "${CHROOT}vmlinux" ] ; then
436 KERNEL="${CHROOT}vmlinux"
437 elif [ -f "${CHROOT}boot/vmlinux" ] ; then
438 KERNEL="${CHROOT}boot/vmlinux"
439 elif [ -f "${CHROOT}boot/vmlinux-`uname -r`" ] ; then
440 KERNEL="${CHROOT}boot/vmlinux-`uname -r`"
441 elif [ -f "${CHROOT}vmlinux-`uname -r`" ] ; then
442 KERNEL="${CHROOT}vmlinux-`uname -r`"
443 elif [ "$QUIET" = 0 ] ; then
444 echo 1>&2 "$PRG: Cannot find a kernel, please locate one"
446 $PRINTF 1>&2 "Enter path to a kernel image: "
448 if [ -f "$KERN" ] ; then
451 elif [ ! -e "$KERN" ] ; then
452 echo 1>&2 "$PRG: $KERN: No such file or directory"
453 elif [ -d "$KERN" ] ; then
454 echo 1>&2 "$PRG: $KERN: Is a directory"
456 echo 1>&2 "$PRG: $KERN: Is not a regular file"
460 echo 1>&2 "$PRG: Cannot find a kernel, aborting..."
464 debug "KERNEL=$KERNEL\n"
466 ## get partition number the kernel lives on, and the OF device= name
467 ## of the whole disk.
468 KERNDEV="$(v=`df "$KERNEL" 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
469 KERNDIR="$(v=`df "$KERNEL" 2> /dev/null | grep ^/dev/` ; echo ${v##*[ ]})"
470 LINKDEV="$(v=`df "${KERNEL%/*}/" 2>/dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
471 PARTITION="${KERNDEV##*[a-z]}"
473 if ckdevfs "$KERNDEV" ; then
474 KERNELDISK="${KERNDEV%/*}/disc"
476 KERNELDISK="${KERNDEV%%[0-9]*}"
479 debug "KERNEL=$KERNEL\nKERNDEV=$KERNDEV\nKERNDIR=$KERNDIR\nLINKDEV=$LINKDEV\nPARTITION=$PARTITION\nKERNELDISK=$KERNELDISK\n"
482 for i in "$KERNDEV" "$KERNDIR" "$LINKDEV" "$PARTITION" "$KERNELDISK" ; do
483 if [ -z "$i" ] ; then
484 echo 1>&2 "$PRG: Could not determine necessary information, aborting..."
485 echo 1>&2 "$PRG: Are you using chroot $PRG instead of $PRG --chroot ?"
490 ## check for cross device symlink
491 if [ -L "$KERNEL" ] ; then
492 if [ "$KERNDEV" != "$LINKDEV" ] ; then
493 echo 1>&2 "$PRG: Warning: Cross device symlink $KERNEL, using it's target instead"
494 KERNEL="$(readlink -f "$KERNEL" 2>/dev/null)"
495 if [ ! -f "$KERNEL" ] ; then
496 echo 1>&2 "$PRG: Unable to canonicalize symlink's target. Do not create cross device symlinks."
502 ## only powermacs appear to need device=
503 if (cat /proc/cpuinfo 2>/dev/null | grep -q pmac-generation 2> /dev/null) ; then
504 DEVICE="\ndevice=$(ofpath $KERNELDISK)"
505 if [ $? != 0 ] ; then
506 echo 1>&2 "$PRG: Unable to determine OpenFirmware device name to $KERNELDISK, aborting..."
511 ## if there is a separate /boot partition we must strip off the /boot
512 ## mountpoint or else yaboot will not find the kernel.
513 if [ "$KERNDIR" != "$CHROOT" ] ; then
514 IMAGE="${KERNEL##*$KERNDIR}"
520 if [ "$CHROOT" != / ] ; then
521 IMAGE="${IMAGE##*$CHROOT}"
524 ## fix relative path (caused by chroot path fix)
534 ## figure out if yaboot is installed in /usr/local or not
535 if [ -f /usr/local/lib/yaboot/yaboot ] ; then
536 INSTALL=/usr/local/lib/yaboot/yaboot
537 elif [ -f /usr/lib/yaboot/yaboot ] ; then
538 INSTALL=/usr/lib/yaboot/yaboot
540 echo 1>&2 "$PRG: yaboot is not installed correctly"
544 ## newworld powermacs need the ofboot first stage loader
545 if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:})" = NewWorld ] ; then
546 if [ -f /usr/local/lib/yaboot/ofboot ] ; then
547 OFBOOT="\nmagicboot=/usr/local/lib/yaboot/ofboot"
548 elif [ -f /usr/lib/yaboot/ofboot ] ; then
549 OFBOOT="\nmagicboot=/usr/lib/yaboot/ofboot"
551 echo 1>&2 "$PRG: yaboot is not installed correctly"
556 ## check for properly (read debian) packaged yaboot.
557 if [ -d ${CHROOT}usr/share/doc/yaboot/examples ] ; then
558 HEADER="## see also: /usr/share/doc/yaboot/examples for example configurations.\n"
562 if [ -n "$KERNARGS" ] ; then
563 APPEND="\tappend=\"${KERNARGS}\"\n"
566 ## generate global section of yaboot.conf
567 GLOBAL="## yaboot.conf generated by $PRG $VERSION
569 ## run: \"man yaboot.conf\" for details. Do not make changes until you have!!
571 ## For a dual-boot menu, add one or more of:
572 ## bsd=/dev/hdaX, macos=/dev/hdaY, macosx=/dev/hdaZ\n
573 boot=${BOOT}${DEVICE:-}
577 install=${INSTALL}${OFBOOT:-}\n"
579 ## generate image= section
583 \tread-only\n${APPEND:-}"
585 ## safely create a tmp file then move it into place after we are sure
587 TMPCONF=`mktemp -q "${CHROOT}${CONFIG}.XXXXXX"`
588 if [ $? != 0 ] ; then
589 echo 1>&2 "$PRG: Unable to write to ${CHROOT}${CONFIG%/*}"
593 $PRINTF "${GLOBAL}${IMAGES}" > "$TMPCONF"
594 if [ $? != 0 ] ; then
595 echo 1>&2 "$PRG: Unable to write temporary file ${TMPCONF}, aborting..."
599 ## rotate backups of /etc/yaboot.conf, 3 backups are kept
600 if [ -f "${CHROOT}${CONFIG}.old" ] ; then
602 if [ -f "${CHROOT}${CONFIG}.old.${i}" ] ; then
603 mv -f "${CHROOT}${CONFIG}.old.$i" "${CHROOT}${CONFIG}.old.$(($i + 1))"
604 if [ $? != 0 ] ; then
605 echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}.old.$i, aborting..."
611 mv -f "${CHROOT}${CONFIG}.old" "${CHROOT}${CONFIG}.old.0"
612 if [ $? != 0 ] ; then
613 echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}.old, aborting..."
618 ## backup /etc/yaboot.conf
619 if [ -f "${CHROOT}${CONFIG}" ] ; then
620 mv -f "${CHROOT}${CONFIG}" "${CHROOT}${CONFIG}.old"
621 if [ $? != 0 ] ; then
622 echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}, aborting..."
627 ## move new config into place
628 mv -f "${TMPCONF}" "${CHROOT}${CONFIG}"
629 if [ $? != 0 ] ; then
630 echo "$PRG: Unable to write file ${CHROOT}${CONFIG}"
633 ## nothing sensitive in generated config, comply with debian policy
634 chmod 644 "${CHROOT}${CONFIG}"
637 ## tell mkofboot where to find the config file if necessary
638 if [ "${CHROOT}${CONFIG}" != /etc/yaboot.conf ] ; then
639 YBINARGS="-C ${CHROOT}${CONFIG}"
642 ## run mkofboot to install the bootstrap, unless --noinstall
643 if [ "$NOINSTALL" = 0 ] ; then
644 if (command -v mkofboot 2>&1 > /dev/null) ; then
645 [ "$QUIET" = 0 ] && echo "Running mkofboot to make the disk bootable..."
646 mkofboot -f $YBINARGS || exit 1
647 [ "$QUIET" = 0 ] && echo "Done"
649 echo 1>&2 "$PRG: yaboot is not installed correctly, not running mkofboot"