]> git.ozlabs.org Git - yaboot.git/blob - ybin/yabootconfig
Merge ubuntu ofpath fixes
[yaboot.git] / ybin / yabootconfig
1 #! /bin/sh
2
3 ###############################################################################
4 ##
5 ## yabootconfig generates a simple /etc/yaboot.conf
6 ## Copyright (C) 2001, 2002, 2003 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 VERSION=1.0.8
31 CHROOT=/
32 ## $CONFIG is relative to $CHROOT
33 CONFIG=etc/yaboot.conf
34 NOINSTALL=0
35 QUIET=0
36 DEBUG=0
37 SIGINT="$PRG: Interrupt caught ... exiting"
38 export LC_COLLATE=C
39
40 ## avoid older versions of ofpath shipped in debian boot-floppies etc.
41 if [ -x "${PATH_PREFIX}/usr/sbin/ofpath" ] ; then
42     OFPATH="${PATH_PREFIX}/usr/sbin/ofpath"
43 else
44     OFPATH=ofpath
45 fi
46
47 ## catch signals, clean up temporary file
48 trap "cleanup" 0
49 trap "exit 129" 1
50 trap "echo 1>&2 $SIGINT ; exit 130" 2
51 trap "exit 131" 3
52 trap "exit 143" 15
53
54 ## check for printf, use it if possible otherwise fall back on
55 ## unreliable echo -e -n ("SUS" says echo shall support no switches)
56 if [ "$(printf printf_test 2>/dev/null)" = printf_test ] ; then
57     PRINTF=printf
58 else
59     PRINTF="echo -e -n"
60 fi
61
62 ## make sure echo is not lame if we must use it.
63 if [ "$PRINTF" != printf ] ; then
64     if [ "$(echo -e -n echo_test)" != "echo_test" ] ; then
65         echo 1>&2 "$PRG: printf unavailable and echo is broken, sorry."
66         exit 1
67     fi
68 fi
69
70 ## make fake `id' if its missing, outputs 0 since if its missing we
71 ## are probably running on boot floppies and thus are root.
72 if (command -v id > /dev/null 2>&1) ; then
73     true
74 else
75     id()
76     {
77     echo 0
78     }
79 fi
80
81 ## --version output
82 version()
83 {
84 echo \
85 "$PRG $VERSION
86 Written by Ethan Benson
87
88 Copyright (C) 2001, 2002, 2003 Ethan Benson
89 This is free software; see the source for copying conditions.  There is NO
90 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
91 }
92
93 ## --help output.
94 usage()
95 {
96 echo \
97 "Usage: $PRG [OPTION]...
98 Generate a working /etc/yaboot.conf.
99
100   -t, --chroot               set root directory $PRG should work from
101   -r, --root                 set root partition, Example: /dev/hda3
102                                default: determined from {chroot}/etc/fstab
103   -b, --boot                 set bootstrap partition, Example: /dev/hda2
104                                default: first type: Apple_Bootstrap partition
105       --kernel-args          add an append= line with specified arguments
106   -q, --quiet                don't ask any questions/confirmation
107       --noinstall            don't automatically run mkofboot
108   -h, --help                 display this help and exit
109   -V, --version              output version information and exit"
110 }
111
112 debug()
113 {
114     [ "$DEBUG" = 0 ] && return 0
115     $PRINTF 1>&2 "$PRG: DEBUG: $1"
116 }
117
118 confirm()
119 {
120     $PRINTF \
121 "yaboot is the Linux Loader for PowerPC.  $PRG sets up your system to boot directly
122 from your hard disk, without the need for a boot CD, floppy or a network boot.\n"
123 [ "$NOINSTALL" = 0 ] && $PRINTF "Install yaboot bootstrap on $BOOT to boot Linux from $ROOT? [Yes] "
124 [ "$NOINSTALL" = 1 ] && $PRINTF "Create simple ${CHROOT}${CONFIG} to boot Linux from $ROOT? [Yes] "
125     read ans
126     case "$ans" in
127         Y|y|Yes|yes|YES|"")
128         echo "Creating a simple ${CHROOT}${CONFIG}..."
129         return 0
130         ;;
131         *)
132         if [ "$NOINSTALL" = 0 ] ; then
133             $PRINTF "Create simple ${CHROOT}${CONFIG} without installing the bootstrap? [Yes] "
134             read ans
135             case "$ans" in
136                 Y|y|Yes|yes|YES|"")
137                 NOINSTALL=1
138                 echo 1>&2 "Creating a simple ${CHROOT}${CONFIG}..."
139                 return 0
140                 ;;
141                 *)
142                 echo "OK, quitting"
143                 return 1
144                 ;;
145             esac
146         else
147             echo "OK, quitting"
148             return 1
149         fi
150         ;;
151     esac
152 }
153
154 ## find out whether we have mac-fdisk or pdisk (they work the same)
155 ckmacfdisk()
156 {
157     if (command -v mac-fdisk > /dev/null 2>&1) ; then
158         FDISK=mac-fdisk
159     elif (command -v pdisk > /dev/null 2>&1) ; then
160         FDISK=pdisk
161     else
162         echo 1>&2 "$PRG: Unable to locate mac-fdisk"
163         return 1
164     fi
165
166     if [ ! -x `command -v $FDISK` 2> /dev/null ] ; then
167         echo 1>&2 "$PRG: `command -v $FDISK`: Permission denied"
168         return 1
169     fi
170     debug "mac-fdisk is: $FDISK\n"
171     return 0
172 }
173
174 ## find out if we have ddisk or fdisk (fdisk for dos labels) debian
175 ## uses both names
176 ckfdisk()
177 {
178     if (command -v ddisk > /dev/null 2>&1) ; then
179         FDISK=ddisk
180     elif (command -v fdisk > /dev/null 2>&1) ; then
181         FDISK=fdisk
182     else
183         echo 1>&2 "$PRG: Unable to locate fdisk"
184         return 1
185     fi
186
187     if [ ! -x `command -v $FDISK` 2> /dev/null ] ; then
188         echo 1>&2 "$PRG: `command -v $FDISK`: Permission denied"
189         return 1
190     fi
191     debug "fdisk is: $FDISK\n"
192     return 0
193 }
194
195 ## find bootstrap partition, supports IBM CHRP with msdos disklabels
196 findbootblock()
197 {
198     ## mac partition table magic == ER
199     if [ "$(dd if="$DISK" bs=2 count=1 2> /dev/null)" = ER ] ; then
200         ckmacfdisk || return 1
201         if [ "$FDISK" = pdisk ] ; then
202             ## handle braindamaged pdisk
203             debug "dealing with pdisk deficiency...\n"
204             BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<Apple_Bootstrap\>'` ; echo ${v%%:*})"
205             debug "BOOT before fixup: $BOOT\n"
206             if [ -n "$BOOT" ] ; then
207                 BOOT="${DISK}${BOOT}"
208             fi
209             debug "BOOT after fixup: $BOOT\n"
210         else
211             BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<Apple_Bootstrap\>'` ; echo ${v%%[ ]*})"
212             debug "BOOT=$BOOT\n"
213         fi
214         if [ -z "$BOOT" ] ; then
215             echo 1>&2 "$PRG: Unable to locate bootstrap partition on $DISK..."
216             echo 1>&2 "$PRG: You must create an 800K type: Apple_Bootstrap partition to make the disk bootable"
217             return 1
218         fi
219     else
220         ckfdisk || return 1
221         BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<PPC PReP Boot\>'` ; echo ${v%%[ ]*})"
222         debug "BOOT=$BOOT\n"
223         if [ -z "$BOOT" ] ; then
224             echo 1>&2 "$PRG: Unable to locate bootstrap partition on $DISK..."
225             echo 1>&2 "$PRG: You must create an 800K type: 0x41 PPC PReP Boot partition to make the disk bootable"
226             return 1
227         fi
228     fi
229     return 0
230 }
231
232 ## if readlink is missing use a kludge
233 if (command -v readlink > /dev/null 2>&1) ; then
234     true
235 else
236     readlink()
237     {
238         SYMTARGET="$(v=`ls -l "$2" 2>/dev/null` ; echo ${v##*> })"
239         if [ -n "$SYMTARGET" ] ; then
240             echo "$SYMTARGET"
241             return 0
242         else
243             return 1
244         fi
245     }
246 fi
247
248 ## we have to do some things differently with a retarded devfs name.
249 ckdevfs()
250 {
251     case "$1" in
252         /dev/ide/*|/dev/scsi/*|/dev/discs/*)
253         return 0
254         ;;
255         *)
256         return 1
257         ;;
258     esac
259 }
260
261 cleanup()
262 {
263     if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
264     return 0
265 }
266
267 ##########
268 ## Main ##
269 ##########
270
271 if [ $# != 0 ] ; then
272     while true ; do
273         case "$1" in
274             -V|--version)
275                 version
276                 exit 0
277                 ;;
278             -h|--help)
279                 usage
280                 exit 0
281                 ;;
282             -t|--chroot)
283                 if [ -n "$2" ] ; then
284                     CHROOT="$2"
285                     shift 2
286                 else
287                     echo 1>&2 "$PRG: option requires an argument $1"
288                     echo 1>&2 "Try \`$PRG --help' for more information."
289                     exit 1
290                 fi
291                 ;;
292             -b|--boot)
293                 if [ -n "$2" ] ; then
294                     BOOT="$2"
295                     shift 2
296                 else
297                     echo 1>&2 "$PRG: option requires an argument $1"
298                     echo 1>&2 "Try \`$PRG --help' for more information."
299                     exit 1
300                 fi
301                 ;;
302             -r|--root)
303                 if [ -n "$2" ] ; then
304                     ROOT="$2"
305                     shift 2
306                 else
307                     echo 1>&2 "$PRG: option requires an argument $1"
308                     echo 1>&2 "Try \`$PRG --help' for more information."
309                     exit 1
310                 fi
311                 ;;
312             --kernel-args)
313                 if [ -n "$2" ] ; then
314                     KERNARGS="$2"
315                     shift 2
316                 else
317                     echo 1>&2 "$PRG: option requires an argument $1"
318                     echo 1>&2 "Try \`$PRG --help' for more information."
319                     exit 1
320                 fi
321                 ;;
322             -q|--quiet)
323                 QUIET=1
324                 shift 1
325                 ;;
326             --noinstall)
327                 NOINSTALL=1
328                 shift 1
329                 ;;
330             --debug)
331                 DEBUG=1
332                 shift 1
333                 ;;
334             "")
335                 break
336                 ;;
337             *)
338                 echo 1>&2 "$PRG: unrecognized option \`$1'"
339                 echo 1>&2 "Try \`$PRG --help' for more information."
340                 exit 1
341                 ;;
342         esac
343     done
344 fi
345
346 if [ `id -u` != 0 ] ; then
347     echo 1>&2 "$PRG: You are not root, go away"
348     exit 1
349 fi
350
351 ## we need /proc because df does
352 if [ ! -f /proc/uptime ] ; then
353     echo 1>&2 "$PRG: This utility requires the /proc filesystem"
354     exit 1
355 fi
356
357 ## check that chroot exists
358 if [ -d "$CHROOT" ] ; then
359     ## HACK: add trailing / to chroot, otherwise are paths later get b0rked.
360     case "$CHROOT" in
361         */)
362         true
363         ;;
364         *)
365         CHROOT="${CHROOT}/"
366         ;;
367     esac
368 elif [ ! -e "$CHROOT" ] ; then
369     echo 1>&2 "$PRG: $CHROOT: No such file or directory"
370     exit 1
371 elif [ ! -d "$CHROOT" ] ; then
372     echo 1>&2 "$PRG: $CHROOT: Not a directory"
373     exit 1
374 fi
375
376 ## make sure the chroot is an actual root filesystem
377 if [ ! -f "${CHROOT}etc/fstab" ] ; then
378     echo 1>&2 "$PRG: $CHROOT does not appear to be a valid root filesystem"
379     exit 1
380 fi
381
382 ## find / device
383 if [ -z "$ROOT" ] ; then
384     ## IMPORTANT! that last substitution is [<space><tab>] thats all ash will grok
385     ROOT="$(v=`grep '^[^#].*[[:blank:]]/[[:blank:]]' ${CHROOT}etc/fstab` ; echo ${v%%[  ]*})"
386     debug "ROOT=$ROOT\n"
387     if [ -z "$ROOT" ] ; then
388         echo 1>&2 "$PRG: Could not determine root partition, aborting..."
389         exit 1
390     fi
391 fi
392
393 ## dereference label or uuid if necessary
394 case "$ROOT" in
395     LABEL=*|UUID=*)
396         if ! (command -v findfs > /dev/null 2>&1) ; then
397             echo 1>&2 "$PRG: Unable to locate findfs, aborting..."
398             exit 1
399         fi
400         ROOT="$(findfs "$ROOT")"
401         if [ -z "$ROOT" -o $? != 0 ] ; then
402             echo 1>&2 "$PRG: Could not determine root partition, aborting..."
403             exit 1
404         fi
405         ;;
406 esac
407
408 ## make sure root device exists
409 if [ ! -e "$ROOT" ] ; then
410     echo 1>&2 "$PRG: $ROOT: No such file or directory"
411     exit 1
412 fi
413
414 ## find root disk.
415 if ckdevfs "$ROOT" ; then
416     DISK="${ROOT%/*}/disc"
417 else
418     DISK="${ROOT%%[0-9]*}"
419 fi
420 if [ -z "$DISK" ] ; then
421     echo 1>&2 "$PRG: Could not determine root disk, aborting..."
422     exit 1
423 fi
424
425 ## make sure main disk exists
426 if [ ! -e "$DISK" ] ; then
427     echo 1>&2 "$PRG: $DISK: No such file or directory"
428     exit 1
429 fi
430
431 ## find bootstrap partition
432 if [ -z "$BOOT" ] ; then
433     findbootblock || exit 1
434 fi
435
436 ## make sure bootstrap device exists
437 if [ ! -e "$BOOT" ] ; then
438     echo 1>&2 "$PRG: $BOOT: No such file or directory"
439     exit 1
440 fi
441
442 ## sanity check
443 for i in "$DISK" "$ROOT" "$BOOT" ; do
444     if [ ! -b "$i" ] ; then
445         echo 1>&2 "$PRG: $i: Not a block device"
446         exit 1
447     fi
448 done
449
450 ## unless --quiet ask permission to proceed
451 if [ "$QUIET" = 0 ] ; then
452     confirm || exit 2
453 fi
454
455 ## find the kernel in the usual places and (if not --quiet) ask the
456 ## user if we cannot find one.
457 if [ -f "${CHROOT}vmlinux" ] ; then
458     KERNEL="${CHROOT}vmlinux"
459     if [ -f "${CHROOT}initrd" ] ; then
460         INITRD="${CHROOT}initrd"
461     elif [ -f "${CHROOT}initrd.img" ] ; then
462         INITRD="${CHROOT}initrd.img"
463     elif [ -f "${CHROOT}initrd.gz" ] ; then
464         INITRD="${CHROOT}initrd.gz"
465     fi
466 elif [ -f "${CHROOT}boot/vmlinux" ] ; then
467     KERNEL="${CHROOT}boot/vmlinux"
468     if [ -f "${CHROOT}boot/initrd" ] ; then
469         INITRD="${CHROOT}boot/initrd"
470     elif [ -f "${CHROOT}boot/initrd.img" ] ; then
471         INITRD="${CHROOT}boot/initrd.img"
472     elif [ -f "${CHROOT}boot/initrd.gz" ] ; then
473         INITRD="${CHROOT}boot/initrd.gz"
474     fi
475 elif [ -f "${CHROOT}boot/vmlinux-`uname -r`" ] ; then
476     KERNEL="${CHROOT}boot/vmlinux-`uname -r`"
477     if [ -f "${CHROOT}boot/initrd-`uname -r`" ] ; then
478         INITRD="${CHROOT}boot/initrd-`uname -r`"
479     elif [ -f "${CHROOT}boot/initrd-`uname -r`.img" ] ; then
480         INITRD="${CHROOT}boot/initrd-`uname -r`.img"
481     elif [ -f "${CHROOT}boot/initrd-`uname -r`.gz" ] ; then
482         INITRD="${CHROOT}boot/initrd-`uname -r`.gz"
483     fi
484 elif [ -f "${CHROOT}vmlinux-`uname -r`" ] ; then
485     KERNEL="${CHROOT}vmlinux-`uname -r`"
486     if [ -f "${CHROOT}initrd-`uname -r`" ] ; then
487         INITRD="${CHROOT}initrd-`uname -r`"
488     elif [ -f "${CHROOT}initrd-`uname -r`.img" ] ; then
489         INITRD="${CHROOT}initrd-`uname -r`.img"
490     elif [ -f "${CHROOT}initrd-`uname -r`.gz" ] ; then
491         INITRD="${CHROOT}initrd-`uname -r`.gz"
492     fi
493 elif [ "$QUIET" = 0 ] ; then
494     echo 1>&2 "$PRG: Cannot find a kernel, please locate one"
495     while true ; do
496         $PRINTF 1>&2 "Enter path to a kernel image: "
497         read KERN
498         if [ -f "$KERN" ] ; then
499             KERNEL="$KERN"
500             break
501         elif [ ! -e "$KERN" ] ; then
502             echo 1>&2 "$PRG: $KERN: No such file or directory"
503         elif [ -d "$KERN" ] ; then
504             echo 1>&2 "$PRG: $KERN: Is a directory"
505         else
506             echo 1>&2 "$PRG: $KERN: Is not a regular file"
507         fi
508     done
509     while true ; do
510         $PRINTF 1>&2 "Enter path to an initrd image (hit Enter for none): "
511         read IRD
512         if [ -z "$IRD" ] ; then
513             break
514         fi
515         if [ -f "$IRD" ] ; then
516             INITRD="$IRD"
517             break
518         elif [ ! -e "$IRD" ] ; then
519             echo 1>&2 "$PRG: $IRD: No such file or directory"
520         elif [ -d "$IRD" ] ; then
521             echo 1>&2 "$PRG: $IRD: Is a directory"
522         else
523             echo 1>&2 "$PRG: $IRD: Is not a regular file"
524         fi
525     done
526 else
527     echo 1>&2 "$PRG: Cannot find a kernel, aborting..."
528     exit 1
529 fi
530
531 debug "KERNEL=$KERNEL\n"
532 debug "INITRD=$INITRD\n"
533
534 ## get partition number the kernel lives on, and the OF device= name
535 ## of the whole disk.
536 KERNDEV="$(v=`df "$KERNEL" 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
537 KERNDIR="$(v=`df "$KERNEL" 2> /dev/null | grep ^/dev/` ; echo ${v##*[ ]})"
538 LINKDEV="$(v=`df "${KERNEL%/*}/" 2>/dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
539 PARTITION="${KERNDEV##*[a-z]}"
540
541 if ckdevfs "$KERNDEV" ; then
542     KERNELDISK="${KERNDEV%/*}/disc"
543 else
544     KERNELDISK="${KERNDEV%%[0-9]*}"
545 fi
546
547 if [ -n "$INITRD" ] ; then
548     ## get partition number the initrd lives on, and the OF device= name
549     ## of the whole disk.
550     IRDDEV="$(v=`df "$INITRD" 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
551     IRDDIR="$(v=`df "$INITRD" 2> /dev/null | grep ^/dev/` ; echo ${v##*[ ]})"
552     IRDLINKDEV="$(v=`df "${INITRD%/*}/" 2>/dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
553     IRDPARTITION="${KERNDEV##*[a-z]}"
554
555     if ckdevfs "$IRDDEV" ; then
556         INITRDDISK="${IRDDEV%/*}/disc"
557     else
558         INITRDDISK="${IRDDEV%%[0-9]*}"
559     fi
560 fi
561
562 debug "KERNEL=$KERNEL\nKERNDEV=$KERNDEV\nKERNDIR=$KERNDIR\nLINKDEV=$LINKDEV\nPARTITION=$PARTITION\nKERNELDISK=$KERNELDISK\n"
563
564 if [ -n "$INITRD" ] ; then
565     debug "INITRD=$INITRD\nIRDDEV=$IRDDEV\nIRDDIR=$IRDDIR\nIRDLINKDEV=$IRDLINKDEV\nIRDPARTITION=$IRDPARTITION\nINITRDDISK=$INITRDDISK\n"
566 fi
567
568 ## sanity check
569 for i in "$KERNDEV" "$KERNDIR" "$LINKDEV" "$PARTITION" "$KERNELDISK" ; do
570     if [ -z "$i" ] ; then
571         echo 1>&2 "$PRG: Could not determine necessary information, aborting..."
572         echo 1>&2 "$PRG: Are you using chroot $PRG instead of $PRG --chroot ?"
573         exit 1
574     fi
575 done
576
577 if [ -n "$INITRD" ] ; then
578     ## sanity check
579     for i in "$IRDDEV" "$IRDDIR" "$IRDLINKDEV" "$IRDPARTITION" "$INITRDDISK" ; do
580         if [ -z "$i" ] ; then
581             echo 1>&2 "$PRG: Could not determine necessary information, aborting..."
582             echo 1>&2 "$PRG: Are you using chroot $PRG instead of $PRG --chroot ?"
583             exit 1
584         fi
585     done
586 fi
587
588 ## check for cross device symlink
589 if [ -L "$KERNEL" ] ; then
590     if [ "$KERNDEV" != "$LINKDEV" ] ; then
591         echo 1>&2 "$PRG: Warning: Cross device symlink $KERNEL, using it's target instead"
592         KERNEL="$(readlink -f "$KERNEL" 2>/dev/null)"
593         if [ ! -f "$KERNEL" ] ; then
594             echo 1>&2 "$PRG: Unable to canonicalize symlink's target.  Do not create cross device symlinks."
595             exit 1
596         fi
597     fi
598 fi
599
600 if [ -n "$INITRD" ] ; then
601     ## initrd must be on same device as kernel.
602     if [ "$IRDDEV" != "$KERNDEV" -o "$IRDPARTITION" != "$IRDPARTITION" -o "$INITRDDISK" != "$KERNELDISK" ] ; then
603         echo 1>&2 "$PRG: Initrd image must be on same device as kernel image."
604         exit 1
605     fi
606
607     ## check for cross device symlink
608     if [ -L "$INITRD" ] ; then
609         if [ "$IRDDEV" != "$IRDLINKDEV" ] ; then
610             echo 1>&2 "$PRG: Warning: Cross device symlink $INITRD, using it's target instead"
611             INITRD="$(readlink -f "$INITRD" 2>/dev/null)"
612             if [ ! -f "$INITRD" ] ; then
613                 echo 1>&2 "$PRG: Unable to canonicalize symlink's target.  Do not create cross device symlinks."
614                 exit 1
615             fi
616         fi
617     fi
618 fi
619
620 ## only powermacs appear to need device=
621 if (cat /proc/cpuinfo 2>/dev/null | grep -q pmac-generation 2> /dev/null) ; then
622     DEVICE="\ndevice=$($OFPATH $KERNELDISK)"
623     if [ $? != 0 ] ; then
624         echo 1>&2 "$PRG: Unable to determine OpenFirmware device name to $KERNELDISK, aborting..."
625         exit 1
626     fi
627 fi
628
629 ## if there is a separate /boot partition we must strip off the /boot
630 ## mountpoint or else yaboot will not find the kernel.
631 if [ "$KERNDIR" != "$CHROOT" ] ; then
632     IMAGE="${KERNEL##*$KERNDIR}"
633 else
634     IMAGE="$KERNEL"
635 fi
636
637 ## fix chrooted path
638 if [ "$CHROOT" != / ] ; then
639     IMAGE="${IMAGE##*$CHROOT}"
640 fi
641
642 ## fix relative path (caused by chroot path fix)
643 case "$IMAGE" in
644     /*)
645     true
646     ;;
647     *)
648     IMAGE="/${IMAGE}"
649     ;;
650 esac
651
652 if [ -n "$INITRD" ] ; then
653     ## if there is a separate /boot partition we must strip off the /boot
654     ## mountpoint or else yaboot will not find the kernel.
655     if [ "$IRDDIR" != "$CHROOT" ] ; then
656         INITRDIMG="${INITRD##*$IRDDIR}"
657     else
658         INITRDIMG="$INITRD"
659     fi
660
661     ## fix chrooted path
662     if [ "$CHROOT" != / ] ; then
663         INITRDIMG="${INITRDIMG##*$CHROOT}"
664     fi
665
666     ## fix relative path (caused by chroot path fix)
667     case "$INITRDIMG" in
668         /*)
669             true
670             ;;
671         *)
672             INITRDIMG="/${INITRDIMG}"
673             ;;
674     esac
675 fi
676
677 ## figure out if yaboot is installed in /usr/local or not
678 if [ -f /usr/local/lib/yaboot/yaboot ] ; then
679     INSTALL=/usr/local/lib/yaboot/yaboot
680 elif [ -f /usr/lib/yaboot/yaboot ] ; then
681     INSTALL=/usr/lib/yaboot/yaboot
682 else
683     echo 1>&2 "$PRG: yaboot is not installed correctly"
684     exit 1
685 fi
686
687 ## newworld powermacs need the ofboot first stage loader
688 if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:})" = NewWorld ] ; then
689     if [ -f /usr/local/lib/yaboot/ofboot ] ; then
690         OFBOOT="\nmagicboot=/usr/local/lib/yaboot/ofboot"
691     elif [ -f /usr/lib/yaboot/ofboot ] ; then
692         OFBOOT="\nmagicboot=/usr/lib/yaboot/ofboot"
693     else
694         echo 1>&2 "$PRG: yaboot is not installed correctly"
695         exit 1
696     fi
697 fi
698
699 ## check for properly (read debian) packaged yaboot.
700 if [ -d ${CHROOT}usr/share/doc/yaboot/examples ] ; then
701     HEADER="## see also: /usr/share/doc/yaboot/examples for example configurations.\n"
702 fi
703
704 ## setup append line
705 if [ -n "$KERNARGS" ] ; then
706     APPEND="\tappend=\"${KERNARGS}\"\n"
707 fi
708
709 ## avoid user confusion when they boot an installer with video=ofonly
710 ## (usually via a install-safe label) and then reboot and have the box
711 ## not boot properly again.
712 if [ -z "$APPEND" ] ; then
713     if (grep -q '\<video=ofonly\>' /proc/cmdline 2> /dev/null) ; then
714         APPEND="\tappend=\"video=ofonly\"\n"
715     fi
716 fi
717
718 ## generate initrd= lines
719 if [ -n "$INITRDIMG" ] ; then
720     INITRDIMGS="\tinitrd=$INITRDIMG\n\tinitrd-size=8192\n"
721 fi
722
723 ## generate global section of yaboot.conf
724 GLOBAL="## yaboot.conf generated by $PRG $VERSION
725 ##
726 ## run: \"man yaboot.conf\" for details. Do not make changes until you have!!
727 ${HEADER}##
728 ## For a dual-boot menu, add one or more of:
729 ## bsd=/dev/hdaX, macos=/dev/hdaY, macosx=/dev/hdaZ\n
730 boot=${BOOT}${DEVICE:-}
731 partition=$PARTITION
732 root=$ROOT
733 timeout=30
734 install=${INSTALL}${OFBOOT:-}\n"
735
736 ## generate image= section
737 IMAGES="
738 image=$IMAGE
739 \tlabel=Linux
740 \tread-only\n${APPEND:-}${INITRDIMGS:-}"
741
742 ## safely create a tmp file then move it into place after we are sure
743 ## it was written.
744 TMPCONF=`mktemp -q "${CHROOT}${CONFIG}.XXXXXX"`
745 if [ $? != 0 ] ; then
746     echo 1>&2 "$PRG: Unable to write to ${CHROOT}${CONFIG%/*}"
747     exit 1
748 fi
749
750 $PRINTF "${GLOBAL}${IMAGES}" > "$TMPCONF"
751 if [ $? != 0 ] ; then
752     echo 1>&2 "$PRG: Unable to write temporary file ${TMPCONF}, aborting..."
753     exit 1
754 fi
755
756 ## rotate backups of /etc/yaboot.conf, 3 backups are kept
757 if [ -f "${CHROOT}${CONFIG}.old" ] ; then
758     for i in 1 0 ; do
759         if [ -f "${CHROOT}${CONFIG}.old.${i}" ] ; then
760             mv -f "${CHROOT}${CONFIG}.old.$i" "${CHROOT}${CONFIG}.old.$(($i + 1))"
761             if [ $? != 0 ] ; then
762                 echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}.old.$i, aborting..."
763                 exit 1
764             fi
765         fi
766     done
767
768     mv -f "${CHROOT}${CONFIG}.old" "${CHROOT}${CONFIG}.old.0"
769     if [ $? != 0 ] ; then
770         echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}.old, aborting..."
771         exit 1
772     fi
773 fi
774
775 ## backup /etc/yaboot.conf
776 if [ -f "${CHROOT}${CONFIG}" ] ; then
777     mv -f "${CHROOT}${CONFIG}" "${CHROOT}${CONFIG}.old"
778     if [ $? != 0 ] ; then
779         echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}, aborting..."
780         exit 1
781     fi
782 fi
783
784 ## move new config into place
785 mv -f "${TMPCONF}" "${CHROOT}${CONFIG}"
786 if [ $? != 0 ] ; then
787     echo "$PRG: Unable to write file ${CHROOT}${CONFIG}"
788     exit 1
789 else
790     ## nothing sensitive in generated config, comply with debian policy
791     chmod 644 "${CHROOT}${CONFIG}"
792 fi
793
794 ## tell mkofboot where to find the config file if necessary
795 if [ "${CHROOT}${CONFIG}" != /etc/yaboot.conf ] ; then
796     YBINARGS="-C ${CHROOT}${CONFIG}"
797 fi
798
799 ## run mkofboot to install the bootstrap, unless --noinstall
800 if [ "$NOINSTALL" = 0 ] ; then
801     if (command -v mkofboot 2>&1 > /dev/null) ; then
802         [ "$QUIET" = 0 ] && echo "Running mkofboot to make the disk bootable..."
803         mkofboot -f $YBINARGS || exit 1
804         [ "$QUIET" = 0 ] && echo "Done"
805     else
806         echo 1>&2 "$PRG: yaboot is not installed correctly, not running mkofboot"
807         exit 1
808     fi
809 fi
810
811 exit 0