3 ###############################################################################
5 ## yabootconfig generates a simple /etc/yaboot.conf
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"
32 ## $CONFIG is relative to $CHROOT
33 CONFIG=etc/yaboot.conf
36 SIGINT="$PRG: Interrupt caught ... exiting"
39 ## catch signals, clean up temporary file
42 trap "echo 1>&2 $SIGINT ; exit 130" 2
46 ## check for printf, use it if possible otherwise fall back on
47 ## unreliable echo -e -n ("SUS" says echo shall support no switches)
48 if [ "$(printf printf_test 2>/dev/null)" = printf_test ] ; then
54 ## make sure echo is not lame if we must use it.
55 if [ "$PRINTF" != printf ] ; then
56 if [ "$(echo -e -n echo_test)" != "echo_test" ] ; then
57 echo 1>&2 "$PRG: printf unavailable and echo is broken, sorry."
62 ## make fake `id' if its missing, outputs 0 since if its missing we
63 ## are probably running on boot floppies and thus are root.
64 if (command -v id > /dev/null 2>&1) ; then
78 Written by Ethan Benson
80 Copyright (C) 2001 Ethan Benson
81 This is free software; see the source for copying conditions. There is NO
82 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
89 "Usage: $PRG [OPTION]...
90 Generate a working /etc/yaboot.conf.
92 -t, --chroot set root directory $PRG should work from
93 -r, --root set root partition, Example: /dev/hda3
94 default: determined from {chroot}/etc/fstab
95 -b, --boot set bootstrap partition, Example: /dev/hda2
96 default: first type: Apple_Bootstrap partition
97 --kernel-args add an append= line with specified arguments
98 -q, --quiet don't ask any questions/confirmation
99 --noinstall don't automatically run mkofboot
100 -h, --help display this help and exit
101 -V, --version output version information and exit"
107 "yaboot is the Linux Loader for PowerPC. $PRG sets up your system to boot directly
108 from your hard disk, without the need for a boot CD, floppy or a network boot.\n"
109 [ "$NOINSTALL" = 0 ] && $PRINTF "Install yaboot bootstrap on $BOOT to boot Linux from $ROOT? [Yes] "
110 [ "$NOINSTALL" = 1 ] && $PRINTF "Create simple ${CHROOT}${CONFIG} to boot Linux from $ROOT? [Yes] "
114 echo "Creating a simple ${CHROOT}${CONFIG}..."
118 if [ "$NOINSTALL" = 0 ] ; then
119 $PRINTF "Create simple ${CHROOT}${CONFIG} without installing the bootstrap? [Yes] "
124 echo 1>&2 "Creating a simple ${CHROOT}${CONFIG}..."
140 ## find out whether we have mac-fdisk or pdisk (they work the same)
143 if (command -v mac-fdisk > /dev/null 2>&1) ; then
145 elif (command -v pdisk > /dev/null 2>&1) ; then
148 echo 1>&2 "$PRG: Unable to locate mac-fdisk"
152 if [ ! -x `command -v $FDISK` 2> /dev/null ] ; then
153 echo 1>&2 "$PRG: `command -v $FDISK`: Permission denied"
159 ## find out if we have ddisk or fdisk (fdisk for dos labels) debian
163 if (command -v ddisk > /dev/null 2>&1) ; then
165 elif (command -v fdisk > /dev/null 2>&1) ; then
168 echo 1>&2 "$PRG: Unable to locate fdisk"
172 if [ ! -x `command -v $FDISK` 2> /dev/null ] ; then
173 echo 1>&2 "$PRG: `command -v $FDISK`: Permission denied"
179 ## find bootstrap partition, supports IBM CHRP with msdos disklabels
182 ## mac partition table magic == ER
183 if [ "$(dd if="$DISK" bs=2 count=1 2> /dev/null)" = ER ] ; then
184 ckmacfdisk || return 1
185 if [ "$FDISK" = pdisk ] ; then
186 ## handle braindamaged pdisk
187 BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<Apple_Bootstrap\>'` ; echo ${v%%:*})"
188 if [ -n "$BOOT" ] ; then
189 BOOT="${DISK}${BOOT}"
192 BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<Apple_Bootstrap\>'` ; echo ${v%%[ ]*})"
194 if [ -z "$BOOT" ] ; then
195 echo 1>&2 "$PRG: Unable to locate bootstrap partition on $DISK..."
196 echo 1>&2 "$PRG: You must create an 800K type: Apple_Bootstrap partition to make the disk bootable"
201 BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<PPC PReP Boot\>'` ; echo ${v%%[ ]*})"
202 if [ -z "$BOOT" ] ; then
203 echo 1>&2 "$PRG: Unable to locate bootstrap partition on $DISK..."
204 echo 1>&2 "$PRG: You must create an 800K type: 0x41 PPC PReP Boot partition to make the disk bootable"
211 ## if readlink is missing use a kludge
212 if (command -v readlink > /dev/null 2>&1) ; then
217 SYMTARGET="$(v=`ls -l "$2" 2>/dev/null` ; echo ${v##*> })"
218 if [ -n "$SYMTARGET" ] ; then
227 ## we have to do some things differently with a retarded devfs name.
231 /dev/ide/*|/dev/scsi/*|/dev/discs/*)
242 if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
250 if [ $# != 0 ] ; then
262 if [ -n "$2" ] ; then
266 echo 1>&2 "$PRG: option requires an argument $1"
267 echo 1>&2 "Try \`$PRG --help' for more information."
272 if [ -n "$2" ] ; then
276 echo 1>&2 "$PRG: option requires an argument $1"
277 echo 1>&2 "Try \`$PRG --help' for more information."
282 if [ -n "$2" ] ; then
286 echo 1>&2 "$PRG: option requires an argument $1"
287 echo 1>&2 "Try \`$PRG --help' for more information."
292 if [ -n "$2" ] ; then
296 echo 1>&2 "$PRG: option requires an argument $1"
297 echo 1>&2 "Try \`$PRG --help' for more information."
313 echo 1>&2 "$PRG: unrecognized option \`$1'"
314 echo 1>&2 "Try \`$PRG --help' for more information."
321 if [ `id -u` != 0 ] ; then
322 echo 1>&2 "$PRG: You are not root, go away"
326 ## we need /proc because df does
327 if [ ! -f /proc/uptime ] ; then
328 echo 1>&2 "$PRG: This utility requires the /proc filesystem"
332 ## check that chroot exists
333 if [ -d "$CHROOT" ] ; then
334 ## HACK: add trailing / to chroot, otherwise are paths later get b0rked.
343 elif [ ! -e "$CHROOT" ] ; then
344 echo 1>&2 "$PRG: $CHROOT: No such file or directory"
346 elif [ ! -d "$CHROOT" ] ; then
347 echo 1>&2 "$PRG: $CHROOT: Not a directory"
351 ## make sure the chroot is an actual root filesystem
352 if [ ! -f "${CHROOT}etc/fstab" ] ; then
353 echo 1>&2 "$PRG: $CHROOT does not appear to be a valid root filesystem"
358 if [ -z "$ROOT" ] ; then
359 ## IMPORTANT! that last substitution is [<space><tab>] thats all ash will grok
360 ROOT="$(v=`grep '[[:blank:]]/[[:blank:]]' ${CHROOT}etc/fstab` ; echo ${v%%[ ]*})"
361 if [ -z "$ROOT" ] ; then
362 echo 1>&2 "$PRG: Could not determine root partition, aborting..."
367 ## make sure root device exists
368 if [ ! -e "$ROOT" ] ; then
369 echo 1>&2 "$PRG: $ROOT: No such file or directory"
374 if ckdevfs "$ROOT" ; then
375 DISK="${ROOT%/*}/disc"
377 DISK="${ROOT%%[0-9]*}"
379 if [ -z "$DISK" ] ; then
380 echo 1>&2 "$PRG: Could not determine root disk, aborting..."
384 ## make sure main disk exists
385 if [ ! -e "$DISK" ] ; then
386 echo 1>&2 "$PRG: $DISK: No such file or directory"
390 ## find bootstrap partition
391 if [ -z "$BOOT" ] ; then
392 findbootblock || exit 1
395 ## make sure bootstrap device exists
396 if [ ! -e "$BOOT" ] ; then
397 echo 1>&2 "$PRG: $BOOT: No such file or directory"
402 for i in "$DISK" "$ROOT" "$BOOT" ; do
403 if [ ! -b "$i" ] ; then
404 echo 1>&2 "$PRG: $i: Not a block device"
409 ## unless --quiet ask permission to proceed
410 if [ "$QUIET" = 0 ] ; then
414 ## find the kernel in the usual places and (if not --quiet) ask the
415 ## user if we cannot find one.
416 if [ -f "${CHROOT}vmlinux" ] ; then
417 KERNEL="${CHROOT}vmlinux"
418 elif [ -f "${CHROOT}boot/vmlinux" ] ; then
419 KERNEL="${CHROOT}boot/vmlinux"
420 elif [ -f "${CHROOT}boot/vmlinux-`uname -r`" ] ; then
421 KERNEL="${CHROOT}boot/vmlinux-`uname -r`"
422 elif [ -f "${CHROOT}vmlinux-`uname -r`" ] ; then
423 KERNEL="${CHROOT}vmlinux-`uname -r`"
424 elif [ "$QUIET" = 0 ] ; then
425 echo 1>&2 "$PRG: Cannot find a kernel, please locate one"
427 $PRINTF 1>&2 "Enter path to a kernel image: "
429 if [ -f "$KERN" ] ; then
432 elif [ ! -e "$KERN" ] ; then
433 echo 1>&2 "$PRG: $KERN: No such file or directory"
434 elif [ -d "$KERN" ] ; then
435 echo 1>&2 "$PRG: $KERN: Is a directory"
437 echo 1>&2 "$PRG: $KERN: Is not a regular file"
441 echo 1>&2 "$PRG: Cannot find a kernel, aborting..."
445 ## get partition number the kernel lives on, and the OF device= name
446 ## of the whole disk.
447 KERNDEV="$(v=`df "$KERNEL" 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
448 KERNDIR="$(v=`df "$KERNEL" 2> /dev/null | grep ^/dev/` ; echo ${v##*[ ]})"
449 LINKDEV="$(v=`df "${KERNEL%/*}/" 2>/dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
450 PARTITION="${KERNDEV##*[a-z]}"
452 if ckdevfs "$KERNDEV" ; then
453 KERNELDISK="${KERNDEV%/*}/disc"
455 KERNELDISK="${KERNDEV%%[0-9]*}"
459 for i in "$KERNDEV" "$KERNDIR" "$LINKDEV" "$PARTITION" "$KERNELDISK" ; do
460 if [ -z "$i" ] ; then
461 echo 1>&2 "$PRG: Could not determine necessary information, aborting..."
466 ## check for cross device symlink
467 if [ -L "$KERNEL" ] ; then
468 if [ "$KERNDEV" != "$LINKDEV" ] ; then
469 echo 1>&2 "$PRG: Warning: Cross device symlink $KERNEL, using it's target instead"
470 KERNEL="$(readlink -f "$KERNEL" 2>/dev/null)"
471 if [ ! -f "$KERNEL" ] ; then
472 echo 1>&2 "$PRG: Unable to canonicalize symlink's target. Do not create cross device symlinks."
478 ## only powermacs appear to need device=
479 if (cat /proc/cpuinfo 2>/dev/null | grep -q pmac-generation 2> /dev/null) ; then
480 DEVICE="\ndevice=$(ofpath $KERNELDISK)"
481 if [ $? != 0 ] ; then
482 echo 1>&2 "$PRG: Unable to determine OpenFirmware device name to $KERNELDISK, aborting..."
487 ## IBM hardware needs fstype=raw for ybin
488 if (cat /proc/cpuinfo 2>/dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
489 FSTYPE="\nfstype=raw"
492 ## if there is a separate /boot partition we must strip off the /boot
493 ## mountpoint or else yaboot will not find the kernel.
494 if [ "$KERNDIR" != "$CHROOT" ] ; then
495 IMAGE="${KERNEL##*$KERNDIR}"
501 if [ "$CHROOT" != / ] ; then
502 IMAGE="${IMAGE##*$CHROOT}"
505 ## fix relative path (caused by chroot path fix)
515 ## figure out if yaboot is installed in /usr/local or not
516 if [ -f /usr/local/lib/yaboot/yaboot ] ; then
517 INSTALL=/usr/local/lib/yaboot/yaboot
518 elif [ -f /usr/lib/yaboot/yaboot ] ; then
519 INSTALL=/usr/lib/yaboot/yaboot
521 echo 1>&2 "$PRG: yaboot is not installed correctly"
525 ## newworld powermacs need the ofboot first stage loader
526 if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:})" = NewWorld ] ; then
527 if [ -f /usr/local/lib/yaboot/ofboot ] ; then
528 OFBOOT="\nmagicboot=/usr/local/lib/yaboot/ofboot"
529 elif [ -f /usr/lib/yaboot/ofboot ] ; then
530 OFBOOT="\nmagicboot=/usr/lib/yaboot/ofboot"
532 echo 1>&2 "$PRG: yaboot is not installed correctly"
537 ## check for properly (read debian) packaged yaboot.
538 if [ -d ${CHROOT}usr/share/doc/yaboot/examples ] ; then
539 HEADER="## see also: /usr/share/doc/yaboot/examples for example configurations.\n"
543 if [ -n "$KERNARGS" ] ; then
544 APPEND="\tappend=\"${KERNARGS}\"\n"
547 ## generate global section of yaboot.conf
548 GLOBAL="## yaboot.conf generated by $PRG $VERSION
550 ## run: \"man yaboot.conf\" for details. Do not make changes until you have!!
552 ## For a dual-boot menu, add one or more of:
553 ## bsd=/dev/hdaX, macos=/dev/hdaY, macosx=/dev/hdaZ\n
554 boot=${BOOT}${FSTYPE:-}${DEVICE:-}
558 install=${INSTALL}${OFBOOT:-}\n"
560 ## generate image= section
564 \tread-only\n${APPEND:-}"
566 ## safely create a tmp file then move it into place after we are sure
568 TMPCONF=`mktemp -q "${CHROOT}${CONFIG}.XXXXXX"`
569 if [ $? != 0 ] ; then
570 echo 1>&2 "$PRG: Unable to write to ${CHROOT}${CONFIG%/*}"
574 $PRINTF "${GLOBAL}${IMAGES}" > "$TMPCONF"
575 if [ $? != 0 ] ; then
576 echo 1>&2 "$PRG: Unable to write temporary file ${TMPCONF}, aborting..."
580 ## rotate backups of /etc/yaboot.conf, 3 backups are kept
581 if [ -f "${CHROOT}${CONFIG}.old" ] ; then
583 if [ -f "${CHROOT}${CONFIG}.old.${i}" ] ; then
584 mv -f "${CHROOT}${CONFIG}.old.$i" "${CHROOT}${CONFIG}.old.$(($i + 1))"
585 if [ $? != 0 ] ; then
586 echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}.old.$i, aborting..."
592 mv -f "${CHROOT}${CONFIG}.old" "${CHROOT}${CONFIG}.old.0"
593 if [ $? != 0 ] ; then
594 echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}.old, aborting..."
599 ## backup /etc/yaboot.conf
600 if [ -f "${CHROOT}${CONFIG}" ] ; then
601 mv -f "${CHROOT}${CONFIG}" "${CHROOT}${CONFIG}.old"
602 if [ $? != 0 ] ; then
603 echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}, aborting..."
608 ## move new config into place
609 mv -f "${TMPCONF}" "${CHROOT}${CONFIG}"
610 if [ $? != 0 ] ; then
611 echo "$PRG: Unable to write file ${CHROOT}${CONFIG}"
614 ## nothing sensitive in generated config, comply with debian policy
615 chmod 644 "${CHROOT}${CONFIG}"
618 ## tell mkofboot where to find the config file if necessary
619 if [ "${CHROOT}${CONFIG}" != /etc/yaboot.conf ] ; then
620 YBINARGS="-C ${CHROOT}${CONFIG}"
623 ## run mkofboot to install the bootstrap, unless --noinstall
624 if [ "$NOINSTALL" = 0 ] ; then
625 if (command -v mkofboot 2>&1 > /dev/null) ; then
626 [ "$QUIET" = 0 ] && echo "Running mkofboot to make the disk bootable..."
627 mkofboot -f $YBINARGS || exit 1
628 [ "$QUIET" = 0 ] && echo "Done"
630 echo 1>&2 "$PRG: yaboot is not installed correctly, not running mkofboot"