summaryrefslogtreecommitdiffstats
path: root/scripts/mk-images
blob: 476a64ed74a2d0555d3f45b26fbfc76e97ef695c (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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
#!/bin/bash
LANG=C

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

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

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

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

PRODUCT=$6
VERSION=$7
PRODUCTPATH=$8
if [ -n "$9" ]; then
    BUGURL=$9
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
    USBMAPS=$KERNELBASE/modules.usbmap
    BUILDARCH=$5

    KERNELNAME=vmlinuz

    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

    if [ $BUILDARCH = x86_64 -o $BUILDARCH = s390x ]; then
	LIBDIR=lib64
    else
	LIBDIR=lib
    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
    TRIMPCIIDS=$IMGPATH/usr/lib/anaconda-runtime/trimpciids
    TRIMUSBMAP=$IMGPATH/usr/lib/anaconda-runtime/trimusbmap
    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
    PCIIDS=$IMGPATH/usr/share/hwdata/pci.ids
    CACHE=$KERNELBASE/.cache
    MODULESUSED=$KERNELBASE/modules-used

    touch $MODULESUSED

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

    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\.ko" $CACHE
}

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

    $FAKEARCH /sbin/depmod -a -F $KERNELROOT/boot/System.map-$version \
            -b $KERNELROOT $version
    mv $KERNELROOT/lib/modules/$version/modules.pcimap $MODMAPS
    mv $KERNELROOT/lib/modules/$version/modules.usbmap $USBMAPS

    cat $KERNELROOT/lib/modules/$version/modules.dep | $FILTERMODDEPS > $final 
}

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

    for mod in $what ; do
	mod=$(echo $mod | sed 's/\.ko  */|^/g;s/\.ko$//;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 \
	   $MMB_DIR/pci.ids
    mkdir -p $MMB_DIR/modules

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

    pushd $MMB_DIR/modules > /dev/null

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

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

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

    # create the modules.usbmap
    $TRIMUSBMAP $(find . -name *.ko -exec basename {} \;) < $USBMAPS > ../modules.usbmap
    # remove a zero-byte modules.usbmap
    if [ "$(ls -s ../modules.usbmap |cut -f 1 -d ' ')" = "0" ]; then
	rm -f ../modules.usbmap
    fi
    # create the pci.ids
    awk '!/^(\t\t|#)/ { print ;if ($0 == "ffff  Illegal Vendor ID") nextfile; }' < $PCIIDS | \
      $TRIMPCIIDS ../pcitable ../modules.pcimap > ../pci.ids

    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 "*.ko" \
          -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
    if [ -n "$BUGURL" ]; then
	echo $BUGURL >> $root/.buildstamp
    fi
}

setupShellEnvironment() {
    # setup password/group entries
    cat > $MBD_DIR/etc/passwd << EOF
root::0:0::/:/sbin/loader
debug::0:0:debug shell:/:/bin/bash
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
EOF
    cat > $MBD_DIR/etc/group << EOF
root:x:0:root
tty:x:5:
EOF
    echo "sshd:x:74:" >> $MBD_DIR/etc/group
    echo "telnet          23/tcp" > $MBD_DIR/etc/services 
    echo "tcp     6       TCP" > $MBD_DIR/etc/protocols

    # PAM configuration
    for i in pam_limits.so pam_env.so pam_unix.so pam_deny.so; do
       cp -f $IMGPATH/$LIBDIR/security/$i $MBD_DIR/$LIBDIR/security
    done
    
    cp -f $IMGPATH/etc/pam.d/other $MBD_DIR/etc/pam.d
    cat > $MBD_DIR/etc/pam.d/login << EOF
#%PAM-1.0
auth        required      pam_env.so
auth        sufficient    pam_unix.so likeauth nullok
auth        required      pam_deny.so
account     required      pam_unix.so
password    sufficient    pam_unix.so nullok use_authtok md5 shadow
password    required      pam_deny.so
session     required      pam_limits.so
session     required      pam_unix.so
EOF
    cp -f $MBD_DIR/etc/pam.d/login $MBD_DIR/etc/pam.d/sshd
    cp -f $MBD_DIR/etc/pam.d/login $MBD_DIR/etc/pam.d/remote

    cp -f $IMGPATH/etc/security/{limits.conf,pam_env.conf} $MBD_DIR/etc/security/

    # setup xinetd for telnet
    cp -f $IMGPATH/etc/xinetd.conf $MBD_DIR/etc/xinetd.conf
    mkdir -p $MBD_DIR/etc/xinetd.d
    cat > $MBD_DIR/etc/xinetd.d/telnet <<EOF
service telnet
{
        flags           = REUSE
        socket_type     = stream
        wait            = no
        user            = root
        server          = /sbin/in.telnetd
        log_on_failure  += USERID
        disable         = no
}
EOF

    # key generation takes ages on s390, you really don't want this for every
    # installation attempt. These are NOT the keys of the installed system!
    mkdir -m 0700 -p $MBD_DIR/etc/ssh
    echo -n "Generating SSH1 RSA host key: "
    /usr/bin/ssh-keygen -q -t rsa1 -f $MBD_DIR/etc/ssh/ssh_host_key \
                        -C '' -N '' >&/dev/null
    echo
    echo -n "Generating SSH2 RSA host key: "
    /usr/bin/ssh-keygen -q -t rsa -f $MBD_DIR/etc/ssh/ssh_host_rsa_key \
                        -C '' -N '' >&/dev/null
    echo
    echo -n "Generating SSH2 DSA host key: "
    /usr/bin/ssh-keygen -q -t dsa -f $MBD_DIR/etc/ssh/ssh_host_dsa_key \
                        -C '' -N '' >&/dev/null
    echo
    (cd $MBD_DIR/etc/ssh; \
        chmod 600 ssh_host_key ssh_host_rsa_key ssh_host_dsa_key; \
        chmod 644 ssh_host_key.pub ssh_host_rsa_key.pub ssh_host_dsa_key.pub; )


    cat > $MBD_DIR/etc/ssh/sshd_config <<EOF
Port 22
HostKey /etc/ssh/ssh_host_key
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
PermitRootLogin yes
IgnoreRhosts yes
StrictModes yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd yes
XAuthLocation /sbin/xauth
KeepAlive yes
SyslogFacility AUTHPRIV
RSAAuthentication yes
PasswordAuthentication yes
PermitEmptyPasswords yes
PermitUserEnvironment yes
EOF
    chmod 600 $MBD_DIR/etc/ssh/sshd_config

    # copy in the binaries
    cp -f $IMGPATH/usr/bin/login $MBD_DIR/sbin/login
    cp -f $IMGPATH/usr/sbin/sshd $MBD_DIR/sbin/sshd
    cp -f $IMGPATH/usr/sbin/xinetd $MBD_DIR/sbin/xinetd
    cp -f $IMGPATH/usr/sbin/in.telnetd $MBD_DIR/sbin/in.telnetd
    cp -f $IMGPATH/usr/bin/bash $MBD_DIR/sbin/bash
    cp -f $IMGPATH/usr/bin/busybox $MBD_DIR/sbin/busybox
    
    # make some symlinks
    ln -sf bash $MBD_DIR/sbin/sh
    (cd $MBD_DIR/sbin;
    	set $(./busybox 2>&1| awk '/^\t([[:alnum:]_\[]+,)+/' | sed 's/,//g' | sed 's/ +//');
    	while [ -n "$1" ]; do
    	      if [ $1 != "busybox" -a $1 != "sh" ]; then
              	 # if file doesnt already exist, link to busybox
		   if [ ! -f "$1" ]; then
	    	      ln -sf ./busybox $1
		   else
		      [ -n "$DEBUG" ] && echo "Overriding busybox version of $1"
		   fi
    	      fi
    	      shift
	 done )

    # and copy libraries too
    mkdir -p $MBD_DIR/$LIBDIR
    for lib in $IMGPATH/$LIBDIR/libc.so* \
	       $IMGPATH/$LIBDIR/libc-*.so* \
       	       $IMGPATH/$LIBDIR/ld-*.so* \
       	       $IMGPATH/$LIBDIR/ld.so* \
       	       $IMGPATH/$LIBDIR/ld64.so* \
	       $IMGPATH/$LIBDIR/libtermcap.so* \
               $IMGPATH/$LIBDIR/libpam.so* \
    	       $IMGPATH/$LIBDIR/libdl.so* \
    	       $IMGPATH/$LIBDIR/libdl-*.so* \
	       $IMGPATH/$LIBDIR/libnss_files* \
	       $IMGPATH/$LIBDIR/libnsl* \
	       $IMGPATH/$LIBDIR/libutil.so* \
	       $IMGPATH/$LIBDIR/libutil-*.so* \
	       $IMGPATH/$LIBDIR/libcrypt*so* \
	       $IMGPATH/$LIBDIR/libselinux*so* \
	       $IMGPATH/$LIBDIR/libsepol*so* \
	       $IMGPATH/$LIBDIR/libattr*so* \
	       $IMGPATH/$LIBDIR/libresolv.so* \
	       $IMGPATH/$LIBDIR/libresolv-*.so* \
	       $IMGPATH/$LIBDIR/libm.so* \
	       $IMGPATH/$LIBDIR/libm-*.so* \
	       $IMGPATH/usr/$LIBDIR/libz.so* \
	       $IMGPATH/$LIBDIR/libssl* \
	       $IMGPATH/$LIBDIR/libcom_err* \
	       $IMGPATH/$LIBDIR/libe2p* \
	       $IMGPATH/$LIBDIR/libuuid* \
	       $IMGPATH/usr/$LIBDIR/libwrap*.so* \
               $IMGPATH/usr/X11R6/$LIBDIR/libX11.so* \
	       $IMGPATH/usr/X11R6/$LIBDIR/libXmuu.so* \
	       $IMGPATH/usr/X11R6/$LIBDIR/libXext.so* \
	       $IMGPATH/usr/X11R6/$LIBDIR/libXt.so* \
	       $IMGPATH/usr/X11R6/$LIBDIR/libSM.so* \
	       $IMGPATH/usr/X11R6/$LIBDIR/libICE.so* ; do
	if [ -e $lib ]; then cp -df $lib $MBD_DIR/$LIBDIR/ ; fi
    done 

    mkdir -p $MBD_DIR/usr/$LIBDIR
    for lib in $IMGPATH/usr/$LIBDIR/libkrb5.so* \
	       $IMGPATH/usr/$LIBDIR/libkrb5support.so* \
	       $IMGPATH/usr/$LIBDIR/libgssapi_krb5.so* \
	       $IMGPATH/usr/$LIBDIR/libk5crypto.so* \
	       $IMGPATH/usr/$LIBDIR/libcom_err.so* ; do
	if [ -e $lib ]; then cp -df $lib $MBD_DIR/usr/$LIBDIR/ ; fi
    done 
}

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 = "--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_BOOTTREE=/tmp/makebootdisk.tree.$$

    rm -rf $MBD_DIR MBD_FSIMAGE
    mkdir -p $MBD_DIR/modules
    mkdir -p $MBD_DIR/sbin
    mkdir -p $MBD_DIR/dev
    mkdir -p $MBD_DIR/etc
    mkdir -p $MBD_DIR/proc
    mkdir -p $MBD_DIR/selinux
    mkdir -p $MBD_DIR/sys
    mkdir -p $MBD_DIR/tmp
    mkdir -p $MBD_DIR/etc/terminfo/{a,b,d,l,s,v,x}
    mkdir -p $MBD_DIR/var/state
    mkdir -p $MBD_DIR/var/run
    mkdir -p $MBD_DIR/var/lock/rpm

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

    if [ "$BUILDARCH" = "s390" -o "$BUILDARCH" = "s390x" ]; then
	mkdir -m 111 -p $MBD_DIR/var/empty/sshd
	mkdir -p $MBD_DIR/etc/{pam.d,security}
	mkdir -p $MBD_DIR/$LIBDIR/security
	cp $IMGPATH/$LIBDIR/libpam_misc.so.0.* $MBD_DIR/$LIBDIR/libpam_misc.so.0
	ln -s /tmp $MBD_DIR/var/state/xkb
	cp $IMGPATH/usr/X11R6/bin/xauth $MBD_DIR/sbin/xauth
        cp $IMGPATH/usr/sbin/cmsfs* $MBD_DIR/sbin/
    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
    if [ "$BUILDARCH" != "s390" -a "$BUILDARCH" != "s390x" ]; then
	install -s -m 755 $LOADERBINDIR/init $MBD_DIR/sbin
	ln -s ./init $MBD_DIR/sbin/shutdown
	ln -s ./init $MBD_DIR/sbin/halt
	ln -s ./init $MBD_DIR/sbin/poweroff
    else
	install -s -m 755 $LOADERBINDIR/shutdown $MBD_DIR/sbin/shutdown
	install -m 755 $IMGPATH/usr/lib/anaconda-runtime/loader/linuxrc.s390 $MBD_DIR/sbin/init
	install -m 755 $IMGPATH/usr/sbin/dasdfmt $MBD_DIR/sbin/dasdfmt
    fi

    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
    for i in a/ansi d/dumb l/linux s/screen v/vt100 v/vt100-nav v/vt102 x/xterm x/xterm-color b/bterm g/gnome ; do
	[ -f $IMGPATH/usr/share/terminfo/$i ] && \
	  install -m 644 $IMGPATH/usr/share/terminfo/$i $MBD_DIR/etc/terminfo/$i
    done

    makeproductfile $MBD_DIR

    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/init
    ln -s /proc/mounts $MBD_DIR/etc/mtab
    ln -s sbin $MBD_DIR/bin

    # s390/s390x need sshd/telnet setup
    if [ "$BUILDARCH" = "s390" -o "$BUILDARCH" = "s390x" ]; then
	setupShellEnvironment
    fi

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
    (cd $MBD_DIR; find . |cpio --quiet -c -o) |gzip -9 > $MBD_FSIMAGE

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

    echo "Wrote $MBD_FSIMAGE (${size}k compressed)"

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

    if [ -z "$KEEP" ]; then
	rm -rf $MBD_FSIMAGE $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
    mkdir -p `dirname $MBD_FILENAME`
    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

    rm -rf $MBD_MNTPOINT $MBD_BOOTTREE
    if [ -z "$INITRDFILE" ]; then
	rm -f $MBD_FSIMAGE
    fi

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

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 "*.ko" \
         -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 >/dev/null
        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 >/dev/null
    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 ppc64 ppc64iseries ppc"
elif [ "$BUILDARCH" = "i386" ]; then
    arches="i586"
else
    arches="$BUILDARCH"
fi
foundakernel=""
for KERNELARCH in $arches; do
  for kernelvers in $vertag; do
    kpackage=$($LATEST $KERNELPATH kernel-$kernelvers)
    if [ ! -f "$kpackage" ]; then
	echo "kernel-$kernelvers does not exist for $KERNELARCH -- using kernel package"
	kpackage=$($LATEST $KERNELPATH kernel)
	kernelvers=""
    fi

    if [ "$KERNELARCH" = "i586" -a ! -f "$kpackage" ]; then
	echo "No i586 kernel, trying i686..."
	KERNELARCH="i686"
	kpackage=$($LATEST $KERNELPATH kernel)
    fi

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

    KERNELROOT=$KERNELBASE/$KERNELARCH
    mkdir -p $KERNELROOT/lib/modules/misc

    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
	exit 1
    fi

    if [ ! -f "$KERNELROOT/$KERNELDIR/${KERNELNAME}-$version" ]; then
	echo "$KERNELROOT/$KERNELDIR/${KERNELNAME}-$version does not exist"
	exit 1
    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/\.ko//g') | sort -u)
    fi

    allmods=$(find $KERNELROOT/lib/modules/$version -name *.ko)

    rundepmod "$allmods" $MODDEPS
    find $KERNELROOT/lib/modules/$version > $CACHE

    # make the boot images
    makeBootImages
  done
done

# do some clean up before making the cramfs of the second stage
rm -rf $INSTIMGTEMPLATE/usr/lib/anaconda-runtime $INSTIMGTEMPLATE/usr/lib/syslinux $IMGPATH/usr/lib/syslinux

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

cd $TOPDIR