File: yabootconfig

package info (click to toggle)
yaboot 1.3.16-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,508 kB
  • ctags: 3,009
  • sloc: ansic: 10,206; sh: 2,933; asm: 343; makefile: 241
file content (844 lines) | stat: -rwxr-xr-x 23,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
#! /bin/sh

###############################################################################
##
## yabootconfig generates a simple /etc/yaboot.conf
## Copyright (C) 2001, 2002, 2003 Ethan Benson
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##
###############################################################################

PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
## allow to run out of /target in boot-floppies
if [ -n "$PATH_PREFIX" ] ; then
    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"
fi
PRG="${0##*/}"
VERSION=1.0.8+debian2
CHROOT=/
## $CONFIG is relative to $CHROOT
CONFIG=etc/yaboot.conf
NOINSTALL=0
QUIET=0
DEBUG=0
SIGINT="$PRG: Interrupt caught ... exiting"
export LC_COLLATE=C

## avoid older versions of ofpath shipped in debian boot-floppies etc.
if [ -x "${PATH_PREFIX}/usr/sbin/ofpath" ] ; then
    OFPATH="${PATH_PREFIX}/usr/sbin/ofpath"
else
    OFPATH=ofpath
fi

## catch signals, clean up temporary file
trap "cleanup" 0
trap "exit 129" 1
trap "echo 1>&2 $SIGINT ; exit 130" 2
trap "exit 131" 3
trap "exit 143" 15

## check for printf, use it if possible otherwise fall back on
## unreliable echo -e -n ("SUS" says echo shall support no switches)
if [ "$(printf printf_test 2>/dev/null)" = printf_test ] ; then
    PRINTF=printf
else
    PRINTF="echo -e -n"
fi

## make sure echo is not lame if we must use it.
if [ "$PRINTF" != printf ] ; then
    if [ "$(echo -e -n echo_test)" != "echo_test" ] ; then
	echo 1>&2 "$PRG: printf unavailable and echo is broken, sorry."
	exit 1
    fi
fi

## make fake `id' if its missing, outputs 0 since if its missing we
## are probably running on boot floppies and thus are root.
if (command -v id > /dev/null 2>&1) ; then
    true
else
    id()
    {
    echo 0
    }
fi

## --version output
version()
{
echo \
"$PRG $VERSION
Written by Ethan Benson

Copyright (C) 2001, 2002, 2003 Ethan Benson
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
}

## --help output.
usage()
{
echo \
"Usage: $PRG [OPTION]...
Generate a working /etc/yaboot.conf.

  -t, --chroot               set root directory $PRG should work from
  -r, --root                 set root partition, Example: /dev/hda3
			       default: determined from {chroot}/etc/fstab
  -b, --boot                 set bootstrap partition, Example: /dev/hda2
			       default: first type: Apple_Bootstrap partition
      --kernel-args          add an append= line with specified arguments
  -q, --quiet                don't ask any questions/confirmation
      --noinstall            don't automatically run mkofboot
  -h, --help                 display this help and exit
  -V, --version              output version information and exit"
}

debug()
{
    [ "$DEBUG" = 0 ] && return 0
    $PRINTF 1>&2 "$PRG: DEBUG: $1"
}

confirm()
{
    $PRINTF \
"yaboot is the Linux Loader for PowerPC.  $PRG sets up your system to boot directly
from your hard disk, without the need for a boot CD, floppy or a network boot.\n"
[ "$NOINSTALL" = 0 ] && $PRINTF "Install yaboot bootstrap on $BOOT to boot Linux from $ROOT? [Yes] "
[ "$NOINSTALL" = 1 ] && $PRINTF "Create simple ${CHROOT}${CONFIG} to boot Linux from $ROOT? [Yes] "
    read ans
    case "$ans" in
	Y|y|Yes|yes|YES|"")
	echo "Creating a simple ${CHROOT}${CONFIG}..."
	return 0
	;;
	*)
	if [ "$NOINSTALL" = 0 ] ; then
	    $PRINTF "Create simple ${CHROOT}${CONFIG} without installing the bootstrap? [Yes] "
	    read ans
	    case "$ans" in
		Y|y|Yes|yes|YES|"")
		NOINSTALL=1
		echo 1>&2 "Creating a simple ${CHROOT}${CONFIG}..."
		return 0
		;;
		*)
		echo "OK, quitting"
		return 1
		;;
	    esac
	else
	    echo "OK, quitting"
	    return 1
	fi
	;;
    esac
}

## find out whether we have mac-fdisk or pdisk (they work the same)
ckmacfdisk()
{
    if (command -v mac-fdisk > /dev/null 2>&1) ; then
	FDISK=mac-fdisk
    elif (command -v pdisk > /dev/null 2>&1) ; then
	FDISK=pdisk
    else
	echo 1>&2 "$PRG: Unable to locate mac-fdisk"
	return 1
    fi

    if [ ! -x `command -v $FDISK` 2> /dev/null ] ; then
	echo 1>&2 "$PRG: `command -v $FDISK`: Permission denied"
	return 1
    fi
    debug "mac-fdisk is: $FDISK\n"
    return 0
}

## find out if we have ddisk or fdisk (fdisk for dos labels) debian
## uses both names
ckfdisk()
{
    if (command -v ddisk > /dev/null 2>&1) ; then
	FDISK=ddisk
    elif (command -v fdisk > /dev/null 2>&1) ; then
	FDISK=fdisk
    else
	echo 1>&2 "$PRG: Unable to locate fdisk"
	return 1
    fi

    if [ ! -x `command -v $FDISK` 2> /dev/null ] ; then
	echo 1>&2 "$PRG: `command -v $FDISK`: Permission denied"
	return 1
    fi
    debug "fdisk is: $FDISK\n"
    return 0
}

## find bootstrap partition, supports IBM CHRP with msdos disklabels
findbootblock()
{
    ## mac partition table magic == ER
    if [ "$(dd if="$DISK" bs=2 count=1 2> /dev/null)" = ER ] ; then
	ckmacfdisk || return 1
	if [ "$FDISK" = pdisk ] ; then
	    ## handle braindamaged pdisk
	    debug "dealing with pdisk deficiency...\n"
	    BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<Apple_Bootstrap\>'` ; echo ${v%%:*})"
	    debug "BOOT before fixup: $BOOT\n"
	    if [ -n "$BOOT" ] ; then
		BOOT="${DISK}${BOOT}"
	    fi
	    debug "BOOT after fixup: $BOOT\n"
	else
	    BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<Apple_Bootstrap\>'` ; echo ${v%%[ ]*})"
	    debug "BOOT=$BOOT\n"
	fi
	if [ -z "$BOOT" ] ; then
	    echo 1>&2 "$PRG: Unable to locate bootstrap partition on $DISK..."
	    echo 1>&2 "$PRG: You must create an 800K type: Apple_Bootstrap partition to make the disk bootable"
	    return 1
	fi
    else
	ckfdisk || return 1
	BOOT="$(v=`$FDISK -l "$DISK" 2>/dev/null | grep '\<PPC PReP Boot\>'` ; echo ${v%%[ ]*})"
	debug "BOOT=$BOOT\n"
	if [ -z "$BOOT" ] ; then
	    echo 1>&2 "$PRG: Unable to locate bootstrap partition on $DISK..."
	    echo 1>&2 "$PRG: You must create an 800K type: 0x41 PPC PReP Boot partition to make the disk bootable"
	    return 1
	fi
    fi
    return 0
}

## if readlink is missing use a kludge
if (command -v readlink > /dev/null 2>&1) ; then
    true
else
    readlink()
    {
	SYMTARGET="$(v=`ls -l "$2" 2>/dev/null` ; echo ${v##*> })"
	if [ -n "$SYMTARGET" ] ; then
	    echo "$SYMTARGET"
	    return 0
	else
	    return 1
	fi
    }
fi

## we have to do some things differently with a retarded devfs name.
ckdevfs()
{
    case "$1" in
	/dev/ide/*|/dev/scsi/*|/dev/discs/*)
	return 0
	;;
	*)
	return 1
	;;
    esac
}

cleanup()
{
    if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
    return 0
}

##########
## Main ##
##########

if [ $# != 0 ] ; then
    while true ; do
        case "$1" in
            -V|--version)
                version
                exit 0
                ;;
            -h|--help)
                usage
                exit 0
                ;;
	    -t|--chroot)
                if [ -n "$2" ] ; then
                    CHROOT="$2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
                    echo 1>&2 "Try \`$PRG --help' for more information."
                    exit 1
                fi
		;;
	    -b|--boot)
                if [ -n "$2" ] ; then
                    BOOT="$2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
                    echo 1>&2 "Try \`$PRG --help' for more information."
                    exit 1
                fi
		;;
	    -r|--root)
                if [ -n "$2" ] ; then
                    ROOT="$2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
                    echo 1>&2 "Try \`$PRG --help' for more information."
                    exit 1
                fi
		;;
	    --kernel-args)
                if [ -n "$2" ] ; then
                    KERNARGS="$2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
                    echo 1>&2 "Try \`$PRG --help' for more information."
                    exit 1
                fi
		;;
	    -q|--quiet)
		QUIET=1
		shift 1
		;;
	    --noinstall)
		NOINSTALL=1
		shift 1
		;;
	    --debug)
		DEBUG=1
		shift 1
		;;
            "")
                break
                ;;
            *)
                echo 1>&2 "$PRG: unrecognized option \`$1'"
                echo 1>&2 "Try \`$PRG --help' for more information."
                exit 1
                ;;
        esac
    done
fi

if [ `id -u` != 0 ] ; then
    echo 1>&2 "$PRG: You are not root, go away"
    exit 1
fi

## we need /proc because df does
if [ ! -f /proc/uptime ] ; then
    echo 1>&2 "$PRG: This utility requires the /proc filesystem"
    exit 1
fi

## check that chroot exists
if [ -d "$CHROOT" ] ; then
    ## HACK: add trailing / to chroot, otherwise are paths later get b0rked.
    case "$CHROOT" in
	*/)
	true
	;;
	*)
	CHROOT="${CHROOT}/"
	;;
    esac
elif [ ! -e "$CHROOT" ] ; then
    echo 1>&2 "$PRG: $CHROOT: No such file or directory"
    exit 1
elif [ ! -d "$CHROOT" ] ; then
    echo 1>&2 "$PRG: $CHROOT: Not a directory"
    exit 1
fi

## make sure the chroot is an actual root filesystem
if [ ! -f "${CHROOT}etc/fstab" ] ; then
    echo 1>&2 "$PRG: $CHROOT does not appear to be a valid root filesystem"
    exit 1
fi

## find / device
if [ -z "$ROOT" ] ; then
    ## IMPORTANT! that last substitution is [<space><tab>] thats all ash will grok
    ROOT="$(v=`grep '^[^#].*[[:blank:]]/[[:blank:]]' ${CHROOT}etc/fstab` ; echo ${v%%[ 	]*})"
    debug "ROOT=$ROOT\n"
    if [ -z "$ROOT" ] ; then
	echo 1>&2 "$PRG: Could not determine root partition, aborting..."
	exit 1
    fi
fi

## dereference label or uuid if necessary
case "$ROOT" in
    LABEL=*|UUID=*)
	if ! (command -v findfs > /dev/null 2>&1) ; then
	    echo 1>&2 "$PRG: Unable to locate findfs, aborting..."
	    exit 1
	fi
	ROOT="$(findfs "$ROOT")"
	if [ -z "$ROOT" -o $? != 0 ] ; then
	    echo 1>&2 "$PRG: Could not determine root partition, aborting..."
	    exit 1
	fi
	;;
esac

## make sure root device exists
if [ ! -e "$ROOT" ] ; then
    echo 1>&2 "$PRG: $ROOT: No such file or directory"
    exit 1
fi

## find root disk.
if ckdevfs "$ROOT" ; then
    DISK="${ROOT%/*}/disc"
else
    DISK="${ROOT%%[0-9]*}"
fi
if [ -z "$DISK" ] ; then
    echo 1>&2 "$PRG: Could not determine root disk, aborting..."
    exit 1
fi

## make sure main disk exists
if [ ! -e "$DISK" ] ; then
    echo 1>&2 "$PRG: $DISK: No such file or directory"
    exit 1
fi

## find bootstrap partition
if [ -z "$BOOT" ] ; then
    findbootblock || exit 1
fi

## make sure bootstrap device exists
if [ ! -e "$BOOT" ] ; then
    echo 1>&2 "$PRG: $BOOT: No such file or directory"
    exit 1
fi

## sanity check
for i in "$DISK" "$ROOT" "$BOOT" ; do
    if [ ! -b "$i" ] ; then
	echo 1>&2 "$PRG: $i: Not a block device"
	exit 1
    fi
done

## unless --quiet ask permission to proceed
if [ "$QUIET" = 0 ] ; then
    confirm || exit 2
fi

READLINKKV=`readlink /usr/src/linux`
## find the kernel in the usual places and (if not --quiet) ask the
## user if we cannot find one.
for k in "vmlinux" "vmlinux-`uname -r`" "vmlinux-`uname -r`" "$READLINKKV" ; do
	if [ -f "${CHROOT}${k}" ] ; then
	    KERNEL="${CHROOT}${k}"
	    break;
	elif [ -f "${CHROOT}boot/${k}" ] ; then
	    KERNEL="${CHROOT}boot/${k}"
	    break;
	fi
done

for i in "initrd" "initrd-`uname -r`" "`echo $READLINKKV | cut -f1 -d- --complement`" ; do
    for b in "" "boot/" ; do
	    if [ -f "${CHROOT}${b}${i}" ] ; then
		INITRD="${CHROOT}${b}${i}"
	    elif [ -f "${CHROOT}${b}${i}.img" ] ; then
		INITRD="${CHROOT}${b}${i}.img"
	    elif [ -f "${CHROOT}${b}${i}.gz" ] ; then
		INITRD="${CHROOT}${b}${i}.gz"
	    fi
    done
done

if [ ! -f "$KERNEL" ] && [ ${QUIET} == 0 ] ; then
    echo 1>&2 "$PRG: Cannot find a kernel, please provide one"
    while true ; do
    	if [ "$CHROOT" == "/" ] ; then
		$PRINTF 1>&2 "Enter path to a kernel image: "
	else
		$PRINTF 1>&2 "Enter path to a kernel image (not including chroot): "
	fi
	read KERN

	### Remove leading /
	case "${KERN}" in
	/*)	KERN=${CHROOT}${KERN:1} ;;
	*)	KERN=${CHROOT}${KERN} ;;
	esac

	if [ -f "$KERN" ] ; then
	    KERNEL="$KERN"
	    break
	elif [ ${KERN} == ${CHROOT} ] ; then
	    echo 1>&2 "$PRG: You must provide a valid kernel"
	elif [ ! -e "$KERN" ] ; then
	    echo 1>&2 "$PRG: $KERN: No such file or directory"
	elif [ -d "$KERN" ] ; then
	    echo 1>&2 "$PRG: $KERN: Is a directory"
	else
	    echo 1>&2 "$PRG: $KERN: Is not a regular file"
	fi
    done
    while true ; do
    	if [ "$CHROOT" == "/" ] ; then
		$PRINTF 1>&2 "Enter path to an initrd image (hit Enter for none): "
	else
		$PRINTF 1>&2 "Enter path to an initrd image (hit Enter for none, don't include the chroot): "
	fi
	read IRD

	if [ "$IRD" == "" ]; then
	    break
	fi
	
	### Remove leading /
	case "${IRD}" in
	/*)	IRD=${CHROOT}${IRD:1} ;;
	*)	IRD=${CHROOT}${IRD} ;;
	esac

	if [ -f "$IRD" ] ; then
	    INITRD="$IRD"
	    break
	elif [ ! -e "$IRD" ] ; then
	    echo 1>&2 "$PRG: $IRD: No such file or directory"
	elif [ -d "$IRD" ] ; then
	    echo 1>&2 "$PRG: $IRD: Is a directory"
	else
	    echo 1>&2 "$PRG: $IRD: Is not a regular file"
	fi
    done
elif [ ! -f "$KERNEL" ] ; then
    echo 1>&2 "$PRG: Cannot find a kernel, aborting..."
    exit 1
fi

debug "KERNEL=$KERNEL\n"
debug "INITRD=$INITRD\n"

## get partition number the kernel lives on, and the OF device= name
## of the whole disk.
KERNDEV="$(v=`df "$KERNEL" 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
KERNDIR="$(v=`df "$KERNEL" 2> /dev/null | grep ^/dev/` ; echo ${v##*[ ]})"
LINKDEV="$(v=`df "${KERNEL%/*}/" 2>/dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
PARTITION="${KERNDEV##*[a-z]}"

if ckdevfs "$KERNDEV" ; then
    KERNELDISK="${KERNDEV%/*}/disc"
else
    KERNELDISK="${KERNDEV%%[0-9]*}"
fi

if [ -n "$INITRD" ] ; then
    ## get partition number the initrd lives on, and the OF device= name
    ## of the whole disk.
    IRDDEV="$(v=`df "$INITRD" 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
    IRDDIR="$(v=`df "$INITRD" 2> /dev/null | grep ^/dev/` ; echo ${v##*[ ]})"
    IRDLINKDEV="$(v=`df "${INITRD%/*}/" 2>/dev/null | grep ^/dev/` ; echo ${v%%[ ]*})"
    IRDPARTITION="${KERNDEV##*[a-z]}"

    if ckdevfs "$IRDDEV" ; then
	INITRDDISK="${IRDDEV%/*}/disc"
    else
	INITRDDISK="${IRDDEV%%[0-9]*}"
    fi
fi

debug "KERNEL=$KERNEL\nKERNDEV=$KERNDEV\nKERNDIR=$KERNDIR\nLINKDEV=$LINKDEV\nPARTITION=$PARTITION\nKERNELDISK=$KERNELDISK\n"

if [ -n "$INITRD" ] ; then
    debug "INITRD=$INITRD\nIRDDEV=$IRDDEV\nIRDDIR=$IRDDIR\nIRDLINKDEV=$IRDLINKDEV\nIRDPARTITION=$IRDPARTITION\nINITRDDISK=$INITRDDISK\n"
fi

## sanity check
for i in "$KERNDEV" "$KERNDIR" "$LINKDEV" "$PARTITION" "$KERNELDISK" ; do
    if [ -z "$i" ] ; then
	echo 1>&2 "$PRG: Could not determine necessary information, aborting..."
	echo 1>&2 "$PRG: Are you using chroot $PRG instead of $PRG --chroot ?"
	exit 1
    fi
done

if [ -n "$INITRD" ] ; then
    ## sanity check
    for i in "$IRDDEV" "$IRDDIR" "$IRDLINKDEV" "$IRDPARTITION" "$INITRDDISK" ; do
	if [ -z "$i" ] ; then
	    echo 1>&2 "$PRG: Could not determine necessary information, aborting..."
	    echo 1>&2 "$PRG: Are you using chroot $PRG instead of $PRG --chroot ?"
	    exit 1
	fi
    done
fi

## check for cross device symlink
if [ -L "$KERNEL" ] ; then
    if [ "$KERNDEV" != "$LINKDEV" ] ; then
	echo 1>&2 "$PRG: Warning: Cross device symlink $KERNEL, using it's target instead"
	KERNEL="$(readlink -f "$KERNEL" 2>/dev/null)"
	if [ ! -f "$KERNEL" ] ; then
	    echo 1>&2 "$PRG: Unable to canonicalize symlink's target.  Do not create cross device symlinks."
	    exit 1
	fi
    fi
fi

if [ -n "$INITRD" ] ; then
    ## initrd must be on same device as kernel.
    if [ "$IRDDEV" != "$KERNDEV" -o "$IRDPARTITION" != "$IRDPARTITION" -o "$INITRDDISK" != "$KERNELDISK" ] ; then
	echo 1>&2 "$PRG: Initrd image must be on same device as kernel image."
	exit 1
    fi

    ## check for cross device symlink
    if [ -L "$INITRD" ] ; then
	if [ "$IRDDEV" != "$IRDLINKDEV" ] ; then
	    echo 1>&2 "$PRG: Warning: Cross device symlink $INITRD, using it's target instead"
	    INITRD="$(readlink -f "$INITRD" 2>/dev/null)"
	    if [ ! -f "$INITRD" ] ; then
		echo 1>&2 "$PRG: Unable to canonicalize symlink's target.  Do not create cross device symlinks."
		exit 1
	    fi
	fi
    fi
fi

## only powermacs appear to need device=
if (cat /proc/cpuinfo 2>/dev/null | grep -q pmac-generation 2> /dev/null) ; then
    DEVICE="\ndevice=$($OFPATH $KERNELDISK)"
    if [ $? != 0 ] ; then
	echo 1>&2 "$PRG: Unable to determine OpenFirmware device name to $KERNELDISK, aborting..."
	exit 1
    fi
fi

## if there is a separate /boot partition we must strip off the /boot
## mountpoint or else yaboot will not find the kernel.
if [ "$KERNDIR" != "$CHROOT" ] ; then
    IMAGE="${KERNEL##*$KERNDIR}"
else
    IMAGE="$KERNEL"
fi

## fix chrooted path
if [ "$CHROOT" != / ] ; then
    IMAGE="${IMAGE##*$CHROOT}"
fi

## fix relative path (caused by chroot path fix)
case "$IMAGE" in
    /*)
    true
    ;;
    *)
    IMAGE="/${IMAGE}"
    ;;
esac

if [ -n "$INITRD" ] ; then
    ## if there is a separate /boot partition we must strip off the /boot
    ## mountpoint or else yaboot will not find the kernel.
    if [ "$IRDDIR" != "$CHROOT" ] ; then
	INITRDIMG="${INITRD##*$IRDDIR}"
    else
	INITRDIMG="$INITRD"
    fi

    ## fix chrooted path
    if [ "$CHROOT" != / ] ; then
	INITRDIMG="${INITRDIMG##*$CHROOT}"
    fi

    ## fix relative path (caused by chroot path fix)
    case "$INITRDIMG" in
	/*)
	    true
	    ;;
	*)
	    INITRDIMG="/${INITRDIMG}"
	    ;;
    esac
fi

## figure out if yaboot is installed in /usr/local or not
if [ -f /usr/local/lib/yaboot/yaboot ] ; then
    INSTALL=/usr/local/lib/yaboot/yaboot
elif [ -f /usr/lib/yaboot/yaboot ] ; then
    INSTALL=/usr/lib/yaboot/yaboot
else
    echo 1>&2 "$PRG: yaboot is not installed correctly"
    exit 1
fi

## newworld powermacs need the ofboot first stage loader
if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:})" = NewWorld ] ; then
    if [ -f /usr/local/lib/yaboot/ofboot ] ; then
	OFBOOT="\nmagicboot=/usr/local/lib/yaboot/ofboot"
    elif [ -f /usr/lib/yaboot/ofboot ] ; then
	OFBOOT="\nmagicboot=/usr/lib/yaboot/ofboot"
    else
	echo 1>&2 "$PRG: yaboot is not installed correctly"
	exit 1
    fi
fi

## check for properly (read debian) packaged yaboot.
if [ -d ${CHROOT}usr/share/doc/yaboot/examples ] ; then
    HEADER="## see also: /usr/share/doc/yaboot/examples for example configurations.\n"
fi

## setup append line
if [ -n "$KERNARGS" ] ; then
    APPEND="\tappend=\"${KERNARGS}\"\n"
fi

## avoid user confusion when they boot an installer with video=ofonly
## (usually via a install-safe label) and then reboot and have the box
## not boot properly again.
if [ -z "$APPEND" ] ; then
    if (grep -q '\<video=ofonly\>' /proc/cmdline 2> /dev/null) ; then
	APPEND="\tappend=\"video=ofonly\"\n"
    fi
fi

## generate initrd= lines
if [ -n "$INITRDIMG" ] ; then
    INITRDIMGS="\tinitrd=$INITRDIMG\n\tinitrd-size=8192\n"
fi

## Get a symlink which always points to the correct block device.
## If it does not exist, give the unix block device name back.
devsymlink() {
  [ "$2" != "nobyid" ] && BYID="/dev/disk/by-id/scsi* /dev/disk/by-id/*"
  for SYMLINK in $BYID /dev/disk/by-uuid/* /dev/disk/by-label/*; do
    [ "`readlink -f "$SYMLINK"`" = "$1" ] && {
      echo $SYMLINK
      return
    }
  done
  echo $1
}

## Get UUID= or LABEL= for the given unix partition block device.
## If it could not be determined, give the unix partition block device back.
uuidlabel() {
  echo `devsymlink "$1" nobyid` \
  | sed -e "s,/dev/disk/by-uuid/,UUID=," \
        -e "s,/dev/disk/by-label/,LABEL=,"
}

## generate global section of yaboot.conf
GLOBAL="## yaboot.conf generated by $PRG $VERSION
##
## run: \"man yaboot.conf\" for details. Do not make changes until you have!!
${HEADER}##
## For a dual-boot menu, add one or more of:
## bsd=/dev/hdaX, macos=/dev/hdaY, macosx=/dev/hdaZ\n
boot=\"`devsymlink "${BOOT}"`\"${DEVICE:-}
partition=$PARTITION
root=\"`uuidlabel "${ROOT}"`\"
timeout=30
install=${INSTALL}${OFBOOT:-}\n"

## generate image= section
IMAGES="
image=$IMAGE
\tlabel=Linux
\tread-only\n${APPEND:-}${INITRDIMGS:-}"

## safely create a tmp file then move it into place after we are sure
## it was written.
TMPCONF=`mktemp -q "${CHROOT}${CONFIG}.XXXXXX"`
if [ $? != 0 ] ; then
    echo 1>&2 "$PRG: Unable to write to ${CHROOT}${CONFIG%/*}"
    exit 1
fi

$PRINTF "${GLOBAL}${IMAGES}" > "$TMPCONF"
if [ $? != 0 ] ; then
    echo 1>&2 "$PRG: Unable to write temporary file ${TMPCONF}, aborting..."
    exit 1
fi

## rotate backups of /etc/yaboot.conf, 3 backups are kept
if [ -f "${CHROOT}${CONFIG}.old" ] ; then
    for i in 1 0 ; do
	if [ -f "${CHROOT}${CONFIG}.old.${i}" ] ; then
	    mv -f "${CHROOT}${CONFIG}.old.$i" "${CHROOT}${CONFIG}.old.$(($i + 1))"
	    if [ $? != 0 ] ; then
		echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}.old.$i, aborting..."
		exit 1
	    fi
	fi
    done

    mv -f "${CHROOT}${CONFIG}.old" "${CHROOT}${CONFIG}.old.0"
    if [ $? != 0 ] ; then
	echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}.old, aborting..."
	exit 1
    fi
fi

## backup /etc/yaboot.conf
if [ -f "${CHROOT}${CONFIG}" ] ; then
    mv -f "${CHROOT}${CONFIG}" "${CHROOT}${CONFIG}.old"
    if [ $? != 0 ] ; then
	echo 1>&2 "$PRG: Unable to make backup of existing ${CHROOT}${CONFIG}, aborting..."
	exit 1
    fi
fi

## move new config into place
mv -f "${TMPCONF}" "${CHROOT}${CONFIG}"
if [ $? != 0 ] ; then
    echo "$PRG: Unable to write file ${CHROOT}${CONFIG}"
    exit 1
else
    ## nothing sensitive in generated config, comply with debian policy
    chmod 644 "${CHROOT}${CONFIG}"
fi

## tell mkofboot where to find the config file if necessary
if [ "${CHROOT}${CONFIG}" != /etc/yaboot.conf ] ; then
    YBINARGS="-C ${CHROOT}${CONFIG}"
fi

## run mkofboot to install the bootstrap, unless --noinstall
if [ "$NOINSTALL" = 0 ] ; then
    if (command -v mkofboot 2>&1 > /dev/null) ; then
	[ "$QUIET" = 0 ] && echo "Running mkofboot to make the disk bootable..."
	mkofboot -f $YBINARGS || exit 1
	[ "$QUIET" = 0 ] && echo "Done"
    else
	echo 1>&2 "$PRG: yaboot is not installed correctly, not running mkofboot"
	exit 1
    fi
fi

exit 0