summaryrefslogtreecommitdiffstats
path: root/scripts/mk-images
blob: a99e3233034104c31a5c995f48e60b6e99601283 (plain)
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
#!/bin/bash

PATH=$PATH:/sbin:/usr/sbin
IMAGEUUID=$(date +%Y%m%d%H%M)

usage () {
	echo "usage: mk-images <pkgsrc> <toplevel> <template> <imgdir> <buildarch> <productname> <version> [<productpath>]"
    exit 0
}

DEBUG=""
if [ "$1" == "--debug" ]; then
    DEBUG="--debug"
fi

if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" -o -z "$5" -o -z "$6" -o -z "$7" ]; then usage; fi

PRODUCT=$6
VERSION=$7
if [ -z "$8" ]; then
    PRODUCTPATH="RedHat"
else
    PRODUCTPATH=$8
fi

TOPDIR=$(echo $0 | sed "s,/[^/]*$,,")
if [ $TOPDIR = $0 ]; then
    $TOPDIR="."
fi

TOPDIR=$(cd $TOPDIR; pwd)

# Set, verify, and create paths
    KERNELPATH=$1
    TOPDESTPATH=$2
    IMAGEPATH=$TOPDESTPATH/images
    FULLMODPATH=/tmp/instimagemods.$$
    FINALFULLMODPATH=$4/modules
    INSTIMGPATH=$TOPDESTPATH/$PRODUCTPATH/base
    INSTIMGTEMPLATE=$3
    IMGPATH=$4
    KERNELBASE=/tmp/updboot.kernel.$$
    MODDEPS=$KERNELBASE/moddeps
    MODMAPS=$KERNELBASE/modules.pcimap
    BUILDARCH=$5

    if [ "$BUILDARCH" = "ppc" -o "$BUILDARCH" = "ppc64" ]; then
	KERNELNAME=vmlinux
    else
	KERNELNAME=vmlinuz
    fi

    if [ "$BUILDARCH" = "ia64" ]; then
	KERNELDIR="/boot/efi/EFI/redhat"
    else
	KERNELDIR="/boot"
    fi
    
    if [ "$BUILDARCH" = "sparc64" ]; then
	BASEARCH=sparc
    else
	BASEARCH=$BUILDARCH
    fi

    # explicit block size setting for some arches (FIXME: we compose 
    # ppc64-ish trees as ppc, so we have to set the "wrong" block size)
    if [ "$BUILDARCH" = "sparc64" ]; then
	CRAMBS="--blocksize 8192"
    elif [ "$BUILDARCH" = "sparc" ]; then
	CRAMBS="--blocksize 4096"
    else
	CRAMBS=""
    fi

    if [ ! -d "$1" ]; then
	LATEST="rsh porkchop latest --arch $BUILDARCH"
    else
	LATEST=findPackage
    fi

    rm -rf $IMAGEPATH
    rm -rf $FULLMODPATH
    rm -rf $FINALFULLMODPATH
    rm -rf $KERNELBASE
    mkdir -p $IMAGEPATH
    mkdir -p $FULLMODPATH
    mkdir -p $FINALFULLMODPATH
    mkdir -p $KERNELBASE
    mkdir -p $INSTIMGPATH

# Stuff that we need
    TRIMPCITABLE=$IMGPATH/usr/lib/anaconda-runtime/trimpcitable
    TRIMMODMAP=$IMGPATH/usr/lib/anaconda-runtime/trimmodmap
    GETKEYMAPS=$IMGPATH/usr/lib/anaconda-runtime/getkeymaps
    KEYMAPS=/tmp/keymaps-$BUILDARCH.$$
    SCREENFONT=$IMGPATH/usr/lib/anaconda-runtime/screenfont-${BASEARCH}.gz
    GETMODDEPS=$IMGPATH/usr/lib/anaconda-runtime/moddeps
    MODLIST=$IMGPATH/usr/lib/anaconda-runtime/modlist
    MODINFO=$IMGPATH/usr/lib/anaconda-runtime/loader/module-info
    FILTERMODDEPS=$IMGPATH/usr/lib/anaconda-runtime/filtermoddeps
    LOADERBINDIR=$IMGPATH/usr/lib/anaconda-runtime/loader
    BOOTDISKDIR=$IMGPATH/usr/lib/anaconda-runtime/boot
    LANGTABLE=$IMGPATH/usr/lib/anaconda/lang-table
    PCITABLE=$IMGPATH/usr/share/hwdata/pcitable
    CACHE=$KERNELBASE/.cache
    MODULESUSED=$KERNELBASE/modules-used

    touch $MODULESUSED

    REQUIREMENTS="$TRIMPCITABLE $TRIMMODMAP $PCITABLE $GETMODDEPS $MODINFO
		  $FILTERMODDEPS $LANGTABLE $GETKEYMAPS"

    dieLater=
    for n in $REQUIREMENTS; do
	if [ ! -f $n ]; then
	    echo "$n doesn't exist"
	    dieLater=1
	fi
    done

    for n in $BOOTDISKDIR; do
	if [ ! -d $n ]; then
	    echo "$n doesn't exist"
	    dieLater=1
	fi
    done

    if [ -n "$dieLater" ]; then exit 1; fi

    if [ "$BUILDARCH" != "s390" -a "$BUILDARCH" != "s390x" ]; then
       # go ahead and create the keymaps so we only have to do it once
       if [ -f $IMGPATH/usr/lib/anaconda-runtime/keymaps-override-$BUILDARCH ]; then
	   echo "Found keymap override, using it"
	   cp $IMGPATH/usr/lib/anaconda-runtime/keymaps-override-$BUILDARCH $KEYMAPS
       else
	   echo "Running: $GETKEYMAPS $BUILDARCH $KEYMAPS $IMGPATH"
	   $GETKEYMAPS $BUILDARCH $KEYMAPS $IMGPATH
	   if [ $? != 0 ]; then
	       echo "Unable to create keymaps and thus can't create initrd."
	       exit 1
	   fi
       fi
    fi
 
findPackage() {
    dir=$1
    name=$2
    
    for n in $dir/$name*{${KERNELARCH},noarch}.rpm; do
	thisName=$(rpm --nodigest --nosignature --qf '%{NAME}' -qp $n)
	if [ -z "$thisName" ]; then
	    continue
	fi
	if [ $thisName = $name ]; then
	    echo $n
	    return
	fi
    done

    echo "cannot find package $name in path $dir" >&2
}

findmodule () {
    if [ ! -f $CACHE ]; then
        find $KERNELROOT/lib/modules/ > $CACHE
    fi
    
    grep "/$1\.o" $CACHE
}

rundepmod () {
    what=$1
    final=$2

    exclpattern="tc90x|FPT|BusLogic|Pci2000|Pci2220i"

    $FAKEARCH /sbin/depmod -e -F $KERNELROOT/boot/System.map-$version \
	    $what > $final.foo 2>&1 | 
	    egrep -v "modprobe: (${exclpattern}) multiply defined"
    $FAKEARCH /sbin/depmod -a -F $KERNELROOT/boot/System.map-$version \
            -b $KERNELROOT $version
    mv $KERNELROOT/lib/modules/$version/modules.pcimap $MODMAPS

    # For paride we need to change this:
    # aten: paride
    # ...
    # paride: parport
    # imm: parport
    # ppa: parport
    # parport_pc: parport
    # pcd: paride
    #
    # into this:
    # aten: paride
    # ...
    # paride: parport_pc
    # imm: parport_pc
    # ppa: parport_pc
    # parport_pc: parport
    # pcd: aten ...
    #
    # So we use an ugly hack. :-(

    PARIDE="aten bpck bpck6 comm dstr epat epia fit2 fit3 friq frpw kbic ktti on20 on26"
    cat $final.foo | $FILTERMODDEPS | sed -e 's/\.o//g' \
	  -e 's/: parport$/: parport_pc/g' \
	  -e 's/^\(parport_pc: parport\)_pc$/\1/' \
	  -e "s/^pcd:.*/pcd: $PARIDE/" > $final 
    rm -f $final.foo
}

getmoddeps () {
    what=$1
    final=$2

    for mod in $what ; do
	mod=$(echo $mod | sed 's/\.o  */|^/g;s/\.o$//;s/.*\///')
	egrep $mod $MODDEPS >> $final.foo
    done
    mv $final.foo $final
}

padfile () {
    PS_FILE=$1
    PS_SIZE=$2

    size=`cat $PS_FILE | wc -c`
    filler=`expr $PS_SIZE "*" 1024 - $size`
    dd if=/dev/zero bs=$filler count=1 >> $PS_FILE 2> /dev/null
}

intcopymodules () {
    dir=$2

    for n in $1; do
	m=$(findmodule $n)
	if [ -z "$m" ]; then
            [ -n "$DEBUG" ] && echo "Module $n not found in kernel rpm" >&2
        else 
	    echo $n >> $MODULESUSED
	    for o in $m ; do 
		echo $n
		ver=$(echo $o |cut -d / -f 7)
		myarch=$(echo $o |cut -d / -f 4)
		if ! [ -d $dir/$ver/$myarch ] ; then mkdir -p $dir/$ver/$myarch ; fi
		if ! cp $o $dir/$ver/$myarch; then
		    echo "Failed to copy $o to $dir/$ver/$myarch (for module $n)" >&2
		fi
	    done
        fi
    done
}

# This loops to make sure it resolves dependencies of dependencies of...
resdeps () {
    items="$*"

    before=1
    after=2

    while [ $before != $after ]; do
	before=`echo $items | wc -c`

	if [ $? != "0" ] ; then
	    kill -9 $$
	fi	
	list=`$GETMODDEPS -m $MODDEPS $items`
	items=$(for n in $items $list; do echo $n; done | sort -u)
	after=`echo $items | wc -c`
    done

    echo $items
}

expandModuleSet() {
    SET=""
    for name in $1; do
	char=$(echo $name | cut -c1)
        if [ $char = '=' ]; then
	    NAME=$(echo $name | cut -c2-)
	    SET="$SET $($MODLIST --modinfo-file $MODINFO $NAME)"
	elif [ $char = '+' ]; then
	    NAME=$(echo $name | cut -c2-)

	    # all of the modules of this type
	    $MODLIST --modinfo-file $MODINFO $NAME > /tmp/emsAll.$$
	    # the modules used that are of this type
	    sort -u $MODULESUSED | cat - /tmp/emsAll.$$ | sort | 
		    uniq -d > /tmp/emsUsed.$$
	    # the modules that are of this type, but not used
	    SET="$SET $(cat /tmp/emsAll.$$ /tmp/emsUsed.$$ | sort | uniq -u)"

	    rm -f /tmp/emsAll.$$ /tmp/emsUsed.$$
	else
	    SET="$SET $name"
	fi
    done

    echo $SET
}

makemoduleball() {
    unset MMB_DD
    MMB_MODINFO="module-info"
    MMB_NAME="modules.cgz"

    while [ x$(echo $1 | cut -c1-2) = x"--" ]; do
	if [ "$1" = "--dd" ]; then
	    MMB_DD="$2"
	    shift; shift
	    MMB_MODINFO="modinfo"
	    continue
	elif [ "$1" = "--name" ]; then
	    MMB_NAME="$2"
	    shift; shift
	    continue
	fi
	echo "unknown option passed to $0"
	exit 1
    done

    MMB_DIR=$1
    MMB_MODULESET=$(resdeps $2)

    rm -rf $MMB_DIR/modules $MMB_DIR/$MMB_NAME $MMB_DIR/modules.dep \
	   $MMB_DIR/modules.pcimap $MMB_DIR/pcitable $MMB_DIR/$MMB_MODINFO
    mkdir -p $MMB_DIR/modules

    MMB_MODULESET=$(intcopymodules "$MMB_MODULESET" $MMB_DIR/modules)

    pushd $MMB_DIR/modules > /dev/null

    GZIPPED=$(find . -name '*.o.gz')
    if [ -n "$GZIPPED" ]; then
	echo $GZIPPED | xargs gunzip
    fi

    $MODLIST --modinfo-file $MODINFO --ignore-missing --modinfo \
	$MMB_MODULESET > ../$MMB_MODINFO

    getmoddeps "$(find . -name *.o)" ../modules.dep

    # create the pcitable
    $TRIMPCITABLE $(find . -name *.o -exec basename {} \;) < $PCITABLE > ../pcitable
    
    # create the modules.pcimap
    $TRIMMODMAP $PCITABLE $(find . -name *.o -exec basename {} \;) < $MODMAPS > ../modules.pcimap

    if [ -n "$MMB_DD" ]; then
	echo $MMB_DD > $MMB_DIR/rhdd
    fi

    find . -type f | cpio --quiet -H crc -o | gzip -9 > ../$MMB_NAME
    cd ..
    rm -rf modules
    chmod 644 *
    popd  > /dev/null
}

makemainmodules() {
    modlist=`expandModuleSet "$1"`

    if [ -n "$2" ]; then
	extraargs="--name $2"
    fi

    rm -rf $FULLMODPATH
    rm -rf $FINALFULLMODPATH

    if [ "$BUILDARCH" = "s390" -o "$BUILDARCH" = "s390x" ]; then
       mkdir -p $KERNELROOT/lib/modules/misc
       find $KERNELROOT/lib/modules/*/kernel/drivers/s390/ -name "*.o" \
          -exec cp -f {} $KERNELROOT/lib/modules/misc \;
    fi
    makemoduleball $extraargs $FULLMODPATH "$modlist"
    cp -ar $FULLMODPATH $FINALFULLMODPATH
    rm -rf $FULLMODPATH
}

makeproductfile() {
    root=$1

    rm -f $root/.buildstamp
    echo $IMAGEUUID > $root/.buildstamp
    echo $PRODUCT >> $root/.buildstamp
    echo $VERSION >> $root/.buildstamp
    echo $PRODUCTPATH >> $root/.buildstamp
}

makeinitrd() {
    EXTRAINITRDPATH=""
    PCMCIA=""
    INITRDSIZE=""
    KEEP=""
    PADSIZE=""
    LOADERBIN=""
    INITRDMODULES=""
    MODULEBALLNAME="modules.cgz"
    MYLANGTABLE=$LANGTABLE
    MYLOADERTR=loader.tr
    BOGL=yes
    while [ x$(echo $1 | cut -c1-2) = x"--" ]; do
	if [ $1 = "--initrdto" ]; then
	    EXTRAINITRDPATH=$2
	    shift; shift
	    continue
	elif [ $1 = "--pcmcia" ]; then
	    PCMCIA=yes
	    shift
	    continue
	elif [ $1 = "--keep" ]; then
	    KEEP=yes
	    shift
	    continue
	elif [ $1 = "--initrdsize" ]; then
	    INITRDSIZE=$2
	    shift; shift
	    continue
	elif [ $1 = "--padsize" ]; then
	    PADSIZE=$2
	    shift; shift
	    continue
	elif [ $1 = "--loaderbin" ]; then
	    LOADERBIN=$2
	    shift; shift
	    continue
	elif [ $1 = "--modules" ]; then
	    INITRDMODULES=$2
	    shift; shift
	    continue
	elif [ $1 = "--moduleballname" ]; then
	    MODULEBALLNAME=$2
	    shift; shift
	    continue
	elif [ $1 = "--nobogl" ]; then
	    BOGL=""
	    shift
	    continue
	fi
	echo "Unknown option passed to makeinitrd"
	exit 1
    done
    if [ -z "$LOADERBIN" ]; then
	echo "no loader binary specified!" >&2
	exit 1
    fi
    if [ -z "$INITRDMODULES" ]; then
	echo "warning: no loader modules specified!" >&2
    fi
    if [ -z "$INITRDSIZE" ]; then
	echo "I don't know how big to make the initrd image!" >&2
	exit 1
    fi

    MBD_DIR=/tmp/makebootdisk.dir.$$
    MBD_FSIMAGE=/tmp/makebootdisk.initrdimage.$$
    MBD_MNTPOINT=/tmp/makebootdisk.mount.$$
    MBD_TMPIMAGE=/tmp/makebootdisk.image.$$
    MBD_BOOTTREE=/tmp/makebootdisk.tree.$$
    INITBIN=init

    rm -rf $MBD_DIR MBD_FSIMAGE
    mkdir -p $MBD_DIR/modules
    mkdir -p $MBD_DIR/sbin
    mkdir -p $MBD_DIR/dev/pts
    mkdir -p $MBD_DIR/dev/input
    mkdir -p $MBD_DIR/etc
    mkdir -p $MBD_DIR/proc
    mkdir -p $MBD_DIR/tmp
    mkdir -p $MBD_DIR/etc/terminfo/l
    mkdir -p $MBD_DIR/etc/terminfo/x
    mkdir -p $MBD_DIR/etc/terminfo/v
    mkdir -p $MBD_DIR/var/state
    mkdir -p $MBD_DIR/var/run
    ln -s /tmp $MBD_DIR/var/state/xkb

    if [ "$BUILDARCH" = "ppc" -o "$BUILDARCH" = "ppc64" ]; then
	mkdir -p $MBD_DIR/dev/iseries
    fi

    if [ -n "$INITRDMODULES" ]; then
	MODSET=`expandModuleSet "$INITRDMODULES"`
        makemoduleball --name $MODULEBALLNAME $MBD_DIR/modules "$MODSET"
    fi

    cat > $MBD_DIR/etc/passwd << EOF
    root::0:0:root:/:/bin/bash
EOF

    # set up the arch bits
    echo $arch > $MBD_DIR/etc/arch

    install -s -m 755 $LOADERBINDIR/$LOADERBIN $MBD_DIR/sbin/loader
    install -s -m 755 $LOADERBINDIR/$INITBIN $MBD_DIR/sbin
    if [ "$BUILDARCH" != "s390" -a "$BUILDARCH" != "s390x" ]; then
       install -m 644 $KEYMAPS $MBD_DIR/etc/keymaps.gz
       install -m 644 $SCREENFONT $MBD_DIR/etc/screenfont.gz
    fi
    install -m 644 $MYLANGTABLE $MBD_DIR/etc/lang-table
    
    install -m 644 $LOADERBINDIR/$MYLOADERTR $MBD_DIR/etc/loader.tr
    install -m 644 $IMGPATH/usr/share/terminfo/l/linux $MBD_DIR/etc/terminfo/l/linux
    install -m 644 $IMGPATH/usr/share/terminfo/x/xterm $MBD_DIR/etc/terminfo/x/xterm
    install -m 644 $IMGPATH/usr/share/terminfo/v/vt100 $MBD_DIR/etc/terminfo/v/vt100
    install -m 644 $IMGPATH/usr/share/terminfo/v/vt100-nav $MBD_DIR/etc/terminfo/v/vt100-nav
    makeproductfile $MBD_DIR

    mkdir -p $MBD_DIR/etc/terminfo/b
    install -m 644 $IMGPATH/usr/share/terminfo/b/bterm $MBD_DIR/etc/terminfo/b/bterm
    if [ -n "$BOGL" ]; then
	install -m 644 $LOADERBINDIR/font.bgf.gz $MBD_DIR/etc
    fi

    if [ -n "$PCMCIA" ]; then
	mkdir -p $MBD_BOOTTREE/etc
	cp -a $KERNELROOT/etc/pcmcia $MBD_DIR/etc
    fi

    for n in insmod rmmod modprobe; do
	ln -s loader $MBD_DIR/sbin/$n
    done

    ln -s /usr/bin/sh $MBD_DIR/sbin/sh
    ln -s /sbin/init $MBD_DIR/linuxrc
    ln -s /proc/mounts $MBD_DIR/etc/mtab
    ln -s sbin $MBD_DIR/bin

    awk '{ printf("mknod %s/dev/%s %s %s %s\n", $1, $2, $3, $4, $5);
	   printf("chmod %s %s/dev/%s\n", $6, $1, $2);
	   printf("chown %s %s/dev/%s\n", $7, $1, $2); 
	 }' <<EOF | sh
	$MBD_DIR console c 5 1 600 root:root
	$MBD_DIR ttyS0 c 4 64 600 root:root
	$MBD_DIR ttyS1 c 4 65 600 root:root
	$MBD_DIR ttyS2 c 4 66 600 root:root
	$MBD_DIR ttyS3 c 4 67 600 root:root
	$MBD_DIR psaux c 10 1 644 root:root
	$MBD_DIR null c 1 3 666 root:root
	$MBD_DIR zero c 1 5 666 root:root
	$MBD_DIR md0 b 9 0 644 root:root
	$MBD_DIR mem c 1 1 600 root:root
	$MBD_DIR ram b 1 1 640 root:root
	$MBD_DIR ptmx c 5 2 666 root:root
	$MBD_DIR tty  c 5 0 666 root:root
	$MBD_DIR tty0 c 4 0 600 root:tty
	$MBD_DIR tty1 c 4 1 600 root:tty
	$MBD_DIR tty2 c 4 2 600 root:tty
	$MBD_DIR tty3 c 4 3 600 root:tty
	$MBD_DIR tty4 c 4 4 600 root:tty
	$MBD_DIR tty5 c 4 5 600 root:tty
	$MBD_DIR tty6 c 4 6 600 root:tty
	$MBD_DIR tty7 c 4 7 600 root:tty
	$MBD_DIR tty8 c 4 8 600 root:tty
	$MBD_DIR tty9 c 4 9 600 root:tty
	$MBD_DIR fb0 c 29 0 600 root:tty
	$MBD_DIR fb1 c 29 32 600 root:tty
	$MBD_DIR fb2 c 29 64 600 root:tty
	$MBD_DIR fb3 c 29 96 600 root:tty
	$MBD_DIR fb4 c 29 128 600 root:tty
	$MBD_DIR fb5 c 29 160 600 root:tty
	$MBD_DIR fb6 c 29 192 600 root:tty
	$MBD_DIR fb7 c 29 224 600 root:tty
	$MBD_DIR openprom c 10 139 644 root:root
	$MBD_DIR sunmouse c 10 6 644 root:root
	$MBD_DIR kbd c 11 0 644 root:root
	$MBD_DIR ptyp0 c 2 0 644 root:root
	$MBD_DIR ttyp0 c 3 0 644 root:root
	$MBD_DIR agpgart c 10 175 664 root:root
	$MBD_DIR input/mice c 13 63 664 root:root
	$MBD_DIR fd0 b 2 0 644 root:root
	$MBD_DIR fd1 b 2 1 644 root:root
	$MBD_DIR sg0 c 21 0 666 root:root
	$MBD_DIR st0 c 9 0 666 root:root
	$MBD_DIR nst0 c 9 128 666 root:root
	$MBD_DIR random c 1 8 644 root:root
	$MBD_DIR urandom c 1 9 644 root:root
	$MBD_DIR nvram c 10 144 644 root:root
	$MBD_DIR adb c 56 0 644 root:root
        $MBD_DIR rtc c 10 135 644 root:root
EOF
    ln -s fb0 $MBD_DIR/dev/fb

    dd if=/dev/zero of=$MBD_DIR/etc/ramfs.img.big bs=1k count=16000 2>/dev/null
    mke2fs -F -q $MBD_DIR/etc/ramfs.img.big
    tune2fs -c0 -i0 $MBD_DIR/etc/ramfs.img.big
    gzip -9 < $MBD_DIR/etc/ramfs.img.big > $MBD_DIR/etc/ramfs.img
    rm -f $MBD_DIR/etc/ramfs.img.big

cat > $MBD_DIR/.profile <<EOF
PATH=/bin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/mnt/sysimage/sbin:/mnt/sysimage/usr/sbin:/mnt/sysimage/bin:/mnt/sysimage/usr/bin
export PATH
EOF

    rm -f $MBD_FSIMAGE
    dd if=/dev/zero of=$MBD_FSIMAGE bs=1k count=$INITRDSIZE 2> /dev/null
    /sbin/mke2fs -F -i 1024 -q $MBD_FSIMAGE $INITRDSIZE > /dev/null 2>/dev/null
    /sbin/tune2fs -c0 -i0 $MBD_FSIMAGE

    rm -rf $MBD_MNTPOINT
    mkdir $MBD_MNTPOINT
    mount -o loop -t ext2 $MBD_FSIMAGE $MBD_MNTPOINT
    (cd $MBD_DIR; find . | cpio --quiet -p $MBD_MNTPOINT)

    left=$(df $MBD_MNTPOINT | tail +2)
    left=$(echo $left | awk '{print $4'})

    umount $MBD_MNTPOINT

    mv -f $MBD_FSIMAGE $MBD_FSIMAGE.uncompressed
    gzip -9 < $MBD_FSIMAGE.uncompressed > $MBD_FSIMAGE
    rm -rf $MBD_FSIMAGE.uncompressed $MBD_DIR

    size=$(du $MBD_FSIMAGE | awk '{ print $1 }')

    echo "Wrote $MBD_BOOTTREE (${size}k compressed, ${left}k free)"

    rm -rf $MBD_TMPIMAGE $MBD_BOOTTREE

    if [ -n "$PADSIZE" ]; then
	padfile $MBD_FSIMAGE $PADSIZE
    fi

    if [ -n "$EXTRAINITRDPATH" ]; then
	mkdir -p `dirname $EXTRAINITRDPATH`
	cp -a $MBD_FSIMAGE $EXTRAINITRDPATH
    fi

    if [ -z "$KEEP" ]; then
	rm -rf $MBD_TMPIMAGE $MBD_FSIMAGE $MBD_MNTPOINT $MBD_BOOTTREE
    fi
}

makebootdisk() {
    BOOTDISKSIZE=""
    EXTRAKERNELPATH=""
    INITRDFLAGS=""
    MBD_FILENAME=""
    INITRDFILE=""
    while [ x$(echo $1 | cut -c1-2) = x"--" ]; do
	if [ $1 = "--bootdisksize" ]; then
	     BOOTDISKSIZE=$2
	     shift; shift
	     continue
	elif [ $1 = "--kernelto" ]; then
	    EXTRAKERNELPATH=$2
	    shift; shift
	    continue
	elif [ $1 = "--initrdflags" ]; then
	    INITRDFLAGS=$2
	    shift; shift
	    continue
	elif [ $1 = "--initrd" ]; then
	    INITRDFILE=$2
	    shift; shift
	    continue
	elif [ $1 = "--imagename" ]; then
	    MBD_FILENAME=$IMAGEPATH/$2
	    shift; shift
	    continue
	fi
	echo "Unknown option passed to makebootdisk"
	exit 1
    done

    if [ -z "$MBD_FILENAME" ]; then
	echo "No imagename passed"
	exit 1
    fi

    if [ -n "$INITRDFILE" ]; then
	MBD_FSIMAGE="$INITRDFILE"
    elif [ -n "$INITRDFLAGS" ]; then
	eval makeinitrd --keep $INITRDFLAGS
    fi

    mkdir -p $MBD_BOOTTREE

    # provided by the mk-image.$ARCH file
    prepareBootImage

    left=$(df $MBD_BOOTTREE | tail +2)
    left=$(echo $left | awk '{print $4'})

    umount $MBD_BOOTTREE

    if [ -n "$EXTRAKERNELPATH" ]; then
	mkdir -p `dirname $EXTRAKERNELPATH`
	cp -f $KERNELROOT/$KERNELDIR/${KERNELNAME}-* $EXTRAKERNELPATH
    fi

    mkdir -p `dirname $MBD_FILENAME`
    rm -f $MBD_FILENAME
    mv -f $MBD_TMPIMAGE $MBD_FILENAME
    rm -rf $MBD_TMPIMAGE $MBD_MNTPOINT $MBD_BOOTTREE
    if [ -z "$INITRDFILE" ]; then
	rm -f $MBD_FSIMAGE
    fi

    echo "Wrote $MBD_FILENAME (${left}k free)"
}

makeImage () {
    MI_FROM=$1
    MI_SIZE=$2
    MI_TO=$3
    MI_TMPIMAGE=/tmp/makeimage.img.$$
    MI_MNTPOINT=/tmp/makeimage.mnt.$$

    mkdir -p $MI_MNTPOINT

    dd if=/dev/zero of=$MI_TMPIMAGE bs=1k count=$MI_SIZE 2>/dev/null
    /sbin/mke2fs -q -F -i 4096 $MI_TMPIMAGE  > /dev/null 2>/dev/null
    /sbin/tune2fs -c0 -i0 $MI_TMPIMAGE
    mount -o loop $MI_TMPIMAGE $MI_MNTPOINT
    (cd $MI_FROM; find . | cpio --quiet -p $MI_MNTPOINT)

    left=$(df $MI_MNTPOINT | tail +2)
    left=$(echo $left | awk '{print $4'})

    umount $MI_MNTPOINT
    rmdir $MI_MNTPOINT

    gzip -9 < $MI_TMPIMAGE > $MI_TO
    chmod 644 $MI_TO

    echo "Wrote $MI_TO (${left}k of ${MI_SIZE}k free)"
    rm -f $MI_TMPIMAGE
}

makeinstimage () {
    imagename=$1
    modlist=$(expandModuleSet "$2")
    tmpdir=/tmp/instimage.dir.$$

    rm -rf $tmpimage $tmpdir
    mkdir -p $mntpoint $tmpdir

    mkdir -p $tmpdir
    (cd $INSTIMGTEMPLATE; find . | cpio --quiet -p $tmpdir)
    if [ "$BUILDARCH" = "s390" -o "$BUILDARCH" = "s390x" ]; then
       mkdir -p $KERNELROOT/lib/modules/misc
       find $KERNELROOT/lib/modules/*/kernel/drivers/s390/ -name "*.o" \
         -exec cp -f {} $KERNELROOT/lib/modules/misc \;
    fi
    makemoduleball $tmpdir/modules "$modlist"
    makeproductfile $tmpdir

    echo "Running mkcramfs $CRAMBS $tmpdir $INSTIMGPATH/${imagename}2.img"
    mkcramfs $CRAMBS $tmpdir /tmp/${imagename}2.img.$$
    cp /tmp/${imagename}2.img.$$ $INSTIMGPATH/${imagename}2.img
    size=$(ls -l $INSTIMGPATH/${imagename}2.img | awk '{print $5}')
    size=$(expr $size / 1024)
    echo "Wrote $INSTIMGPATH/${imagename}2.img (${size}k)..."
    rm -rf $tmpdir
}

findloopdevice () {
    if [ -z "$1" ]; then
	echo "usage: findloopdevice image"
	exit 1
    fi

    for devnum in 0 1 2 3 4 5 6 7 8; do
        if losetup /dev/loop$devnum $1 2>/dev/null ; then break; fi
    done
    echo /dev/loop$devnum
}

makemainimage () {
    imagename=$1
    type=$2
    mmi_tmpimage=/tmp/instimage.img.$$
    mmi_mntpoint=/tmp/instimage.mnt.$$
    
    rm -rf $mmi_tmpimage $mmi_mntpoint
    mkdir $mmi_mntpoint

    if [ $type = "ext2" ]; then
        SIZE=$(du -sk $IMGPATH | awk '{ print int($1 * 1.1) }')
        if [ -d $IMGPATH/usr/lib/anaconda-runtime ]; then
	    ERROR=$(du -sk $IMGPATH/usr/lib/anaconda-runtime | awk '{ print $1 }')
	    SIZE=$(expr $SIZE - $ERROR)
        fi
        dd if=/dev/zero bs=1k count=${SIZE} of=$mmi_tmpimage 2>/dev/null
        mke2fs -q -F $mmi_tmpimage > /dev/null 
	tune2fs -c0 -i0 $mmi_tmpimage
        mount -o loop $mmi_tmpimage $mmi_mntpoint

        (cd $IMGPATH; find . | fgrep -v "./usr/lib/anaconda-runtime" |
	        cpio -H crc -o) | (cd $mmi_mntpoint; cpio -iumd)
        makeproductfile $mmi_mntpoint
        umount $mmi_mntpoint
        rmdir $mmi_mntpoint
    elif [ $type = "cramfs" ]; then
        makeproductfile $IMGPATH
        echo "Running mkcramfs $CRAMBS $IMGPATH $mmi_tmpimage"
        mkcramfs $CRAMBS $IMGPATH $mmi_tmpimage
        SIZE=$(expr `cat $mmi_tmpimage | wc -c` / 1024)
    fi
    
    cp $mmi_tmpimage $INSTIMGPATH/${imagename}.img
    chmod 644 $INSTIMGPATH/${imagename}.img

    echo "Wrote $INSTIMGPATH/${imagename}.img (${SIZE}k)"
    
    rm $mmi_tmpimage
}

makedriverdisk () {
    PADSIZE=""

    while [ x$(echo $1 | cut -c1-2) = x"--" ]; do
	if [ $1 = "--padsize" ]; then
	    PADSIZE=$2
	    shift; shift
	    continue
	fi

	echo "Unknown option passed to makedriverdisk"
	exit 1
    done

    modlabel=$1
    diskname=$2
    modlist=$(expandModuleSet "$3")
    diskpath=/tmp/makedriverdisk.$$

    rm -rf $diskpath
    mkdir -p $diskpath

    makemoduleball --dd "$modlabel" $diskpath "$modlist"

    size=$(expr 60 + $(du -sk $diskpath | awk '{print $1}'))
    if [ $size -lt 57 ]; then
	size=64
    fi

    dd if=/dev/zero of=/tmp/$diskname.img bs=1k count=$size 2> /dev/null
    /sbin/mke2fs -q -F -i 4096 /tmp/$diskname.img  > /dev/null 2>/dev/null
    /sbin/tune2fs -c0 -i0 /tmp/$diskname.img
    mkdir -p /tmp/mkdd.$$
    mount -t ext2 -o loop /tmp/$diskname.img /tmp/mkdd.$$
    rmdir /tmp/mkdd.$$/lost+found
    (cd $diskpath; tar cSpf - .) | (cd /tmp/mkdd.$$; tar xSpf -)
    DIE=""
    diff -upr $diskpath /tmp/mkdd.$$ > /dev/null 2>&1
    if [ $? != 0 ]; then
	echo copy to disk image failed
	ls -l /tmp/mkdd.$$
	echo "disk size: $size"
	ls -l $diskpath
	DIE=1
    fi
    umount /tmp/mkdd.$$
    rm -rf /tmp/mkdd.$$

    if [ -n "$DIE" ]; then
	kill -9 $$
    fi

    cp /tmp/${diskname}.img $IMAGEPATH/${diskname}.img
    rm /tmp/${diskname}.img
    rm -rf $diskpath

    size=$(ls -l $IMAGEPATH/${diskname}.img | awk '{ print $5 }')
    size=$(expr $size / 1024)

    echo "Wrote $IMAGEPATH/${diskname}.img (${size}k)"
    echo "${diskname}.img - ${modlabel}" >> $IMAGEPATH/README

    if [ -n "$PADSIZE" ]; then
	padfile $IMAGEPATH/${diskname}.img $PADSIZE
    fi
}

# source the architecture specific mk-images file so we can call functions
# in it
if [ ${BUILDARCH} = s390x ]; then
    # FIXME: this is a bad hack for s390, but better than copying for now
    source $TOPDIR/mk-images.s390
else
    source $TOPDIR/mk-images.${BUILDARCH}
fi

# Find the kernel, unpack it, and verify it
vertag="BOOT"
if [ "$BUILDARCH" = "ppc" -o "$BUILDARCH" = "ppc64" ]; then
    arches="ppc64pseries ppc64iseries ppc"
elif [ "$BUILDARCH" = "i386" ]; then
    arches="i386 i586"
else
    arches="$BUILDARCH"
fi
foundakernel=""
for KERNELARCH in $arches; do
  for kernelvers in $vertag; do
    KERNELROOT=$KERNELBASE/$KERNELARCH
    kpackage=$($LATEST $KERNELPATH kernel-$kernelvers)
    mkdir -p $KERNELROOT/lib/modules/misc
    if [ ! -f "$kpackage" ]; then
	echo "kernel-$kernelvers does not exist for $KERNELARCH -- using kernel package"
	kpackage=$($LATEST $KERNELPATH kernel)
	kernelvers=""
    fi

    if [ ! -f "$kpackage" ]; then
	echo "kernel doesn't exist for $KERNELARCH.  skipping arch"
	continue
    fi

    foundakernel="yes"

    ppackage=$($LATEST $KERNELPATH hwdata)
    if [ ! -f "$ppackage" ]; then
	ppackage=""
    fi

    version=$(rpm --nodigest --nosignature --qf '%{VERSION}-%{RELEASE}' -qp $kpackage)${kernelvers}
    arch=$(rpm --nodigest --nosignature --qf '%{ARCH}\n' -qp $kpackage)
    echo "unpacking $kpackage.$arch"

    rpm2cpio $kpackage | (cd $KERNELROOT; cpio --quiet -iumd)
    if [ ! -z $ppackage ]; then 
	rpm2cpio $ppackage | (cd $KERNELROOT; cpio --quiet -iumd etc/pcmcia/* ./etc/pcmcia/*)
    fi

    if [ ! -d "$KERNELROOT/lib/modules/$version" ]; then
	echo "$KERNELROOT/lib/modules/$version is not a valid modules directory" 2>&1
	usage
    fi

    if [ ! -f "$KERNELROOT/$KERNELDIR/${KERNELNAME}-$version" ]; then
	echo "$KERNELROOT/$KERNELDIR/${KERNELNAME}-$version does not exist"
	usage
    fi

    if [ -f $KERNELROOT/etc/pcmcia/config ]; then
	# This gets everything mentioned in /etc/pcmcia/config.  We probably
	# want to do some module-info stuff here too
	PCMCIAMODULES=$((perl -e 'while (<>) { s/^.*class.*(network|scsi|ide).*module +"// || next; s/[" ]//g; s/,/\n/g; print }' $KERNELROOT/etc/pcmcia/config | sed 's/\.o//g') | sort -u)
    fi

    GZIPPED=$(find $KERNELROOT/lib/modules/$version -name "*.o.gz")
    if [ -n "$GZIPPED" ]; then
	echo $GZIPPED |xargs gunzip
    fi
    allmods=$(find $KERNELROOT/lib/modules/$version -name *.o)

    rundepmod "$allmods" $MODDEPS
    rm -f $MODDEPS.foo
    find $KERNELROOT/lib/modules/$version > $CACHE

    # make the boot images
    makeBootImages
  done
done

if [ -n "$foundakernel"]; then
  # make the second stage
  find $KERNELBASE/*/lib/modules/ > $CACHE
  makeSecondStage
  rm -rf $KERNELBASE
fi

cd $TOPDIR