273d4b34b394fc23df3eddceb7d04ab562f23f2d
[bootcd.git] / build.sh
1 #!/bin/bash
2 #
3 # Builds custom BootCD ISO and USB images in the current
4 # directory. 
5 #
6 # Aaron Klingaman <alk@absarokasoft.com>
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2004-2007 The Trustees of Princeton University
9 #
10
11 COMMAND=$(basename $0)
12 DIRNAME=$(dirname $0)
13 PATH=/sbin:/bin:/usr/sbin:/usr/bin
14
15 # debugging flags
16 # keep KERNEL_DEBUG_ARGS void for production
17 KERNEL_DEBUG_ARGS=""
18 # and uncomment these to augment verbosity of boot phase
19 # not everything works or is helpful, but well
20 #KERNEL_DEBUG_ARGS="$KERNEL_DEBUG_ARGS debuginitrd showerr"
21 #KERNEL_DEBUG_ARGS="$KERNEL_DEBUG_ARGS earlyprintk=vga loglevel=6"
22 # for systemd - might slow down boot 
23 #KERNEL_DEBUG_ARGS="$KERNEL_DEBUG_ARGS systemd.log_level=debug systemd.journald.forward_to_console=1"
24
25
26 # defaults
27 DEFAULT_TYPES="usb iso"
28 # Leave 4 MB of free space
29 GRAPHIC_CONSOLE="graphic"
30 SERIAL_CONSOLE="ttyS0:115200:n:8"
31 CONSOLE_INFO=$GRAPHIC_CONSOLE
32 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
33 #isolinux-debug.bin is supposedly helpful as well if available,
34 # when things don't work as expected
35 #MKISOFS_OPTS="-R -J -r -f -b isolinux-debug.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
36 FREE_SPACE=4096
37
38 # command-line settable args
39 NODE_CONFIGURATION_FILE=
40 CUSTOM_DIR=
41 OUTPUT_BASE=
42 DRY_RUN=""
43 OUTPUT_NAME=""
44 TYPES=""
45 KERNEL_ARGS=""
46
47 # various globals
48 BUILDTMP=""
49 FULL_VERSION_STRING=""
50 ISOREF=""
51 ISOFS=""
52 OVERLAY=""
53 IS_SERIAL=""
54 console_dev=""
55 console_baud=""
56 console_spec=""
57 console_serial_line=""
58
59
60 #################### compute all supported types
61 # removing support for serial in the type
62 # this is because kargs.txt goes in the overlay, that is computed only once
63 # so we cannot handle serial and graphic modes within the same invokation of this script
64
65 ALL_TYPES=""
66 for x in iso usb usb_partition; do for c in "" "_cramfs" ; do
67   t="${x}${c}"
68   case $t in
69       usb_partition_cramfs)
70           # unsupported
71           ;;
72       *)
73           ALL_TYPES="$ALL_TYPES $t" ;;
74   esac
75 done; done
76
77 #################### cleanup utilities
78 declare -a _CLEANUPS=()
79 function do_cleanup() {
80     cd / ; for i in "${_CLEANUPS[@]}"; do $i ; done
81 }
82 function push_cleanup() {
83     _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
84 }
85 function pop_cleanup() {
86     unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
87 }
88
89 #################### initialization
90 function init_and_check () {
91
92     # Change to our source directory
93     local srcdir=$(cd $DIRNAME && pwd -P)
94     pushd $srcdir
95
96     # Root of the isofs
97     ISOREF=$PWD/${VARIANT}
98
99     # The reference image is expected to have been built by prep.sh (see .spec)
100     # we disable the initial logic that called prep.sh if that was not the case
101     # this is because prep.sh needs to know pldistro 
102     if [ ! -f $ISOREF/isofs/bootcd.img -o ! -f $ISOREF/version.txt ] ; then
103         echo "Could not find isofs and version.txt in $ISOREF"
104         if [ "$VARIANT" == "build" ] ; then
105             echo "You have to run prep.sh prior to calling $COMMAND"
106         else
107             echo "You need to create your variant image, see kvariant.sh"
108         fi
109         echo "Exiting .."
110         exit 1
111     fi
112
113     # build/version.txt written by prep.sh
114     BOOTCD_VERSION=$(cat ${VARIANT}/version.txt)
115
116     if [ -f /etc/planetlab/plc_config ] ; then
117         # Source PLC configuration
118         . /etc/planetlab/plc_config
119     fi
120
121     # use /var/tmp that should be large enough on both chroot- or vserver-based myplc
122     BUILDTMP=/var/tmp
123
124     FULL_VERSION_STRING="${PLC_NAME} BootCD ${BOOTCD_VERSION}"
125
126 }
127
128 # NOTE
129 # the custom-dir feature is designed to let a myplc try/ship a patched bootcd
130 # without the need for a full devel environment
131 # for example, you would create /root/custom-bootcd/etc/rc.d/init.d/pl_hwinit
132 # and run this script with -C /root/custom-bootcd
133 # this creates a third .img image of the custom dir, that 'hides' the files from 
134 # bootcd.img in the resulting unionfs
135 # it seems that this feature has not been used nor tested in a long time, use with care
136
137 usage() {
138     echo "Usage: $COMMAND [OPTION]..."
139     echo "    -f plnode.txt    Node to customize CD for (default: none)"
140     echo "    -t 'types'       Build the specified images (default: $DEFAULT_TYPES)"
141     echo "                     NOTE: mentioning 'serial' as part of the type is not supported anymore"
142     echo "    -a               Build all known types as listed below"
143     echo "    -s console-info  Enable a serial line as console and also bring up getty on that line"
144     echo "                     console-info: tty:baud-rate:parity:bits"
145     echo "                     or 'default' shortcut for $SERIAL_CONSOLE"
146     echo "    -S               equivalent to -s default"
147     echo "    -O output-base   The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
148     echo "                     useful when multiple types are provided"
149     echo "                     can be a full path"
150     echo "    -o output-name   The full name of the generated file"
151     echo "    -C custom-dir    Custom directory"
152     echo "    -V variant       Use a variant - see kvariant.sh"
153     echo "    -n               Dry run - mostly for debug/test purposes"
154     echo "    -k               Add additional parameters to the kargs.txt file"
155     echo "    -h               This message"
156     echo "All known types: $ALL_TYPES"
157     exit 1
158 }
159
160 #################### 
161 function parse_command_line () {
162
163     # init
164     TYPES=""
165     # Get options
166     while getopts "f:t:as:SO:o:C:V:k:nh" opt ; do
167         case $opt in
168             f) NODE_CONFIGURATION_FILE=$OPTARG ;;
169             t) TYPES="$TYPES $OPTARG" ;;
170             a) TYPES="$ALL_TYPES" ;;
171             s) CONSOLE_INFO="$OPTARG" ;;
172             S) CONSOLE_INFO=$SERIAL_CONSOLE ;;
173             O) OUTPUT_BASE="$OPTARG" ;;
174             o) OUTPUT_NAME="$OPTARG" ;;
175             C) CUSTOM_DIR="$OPTARG" ;;
176             V) VARIANT="$OPTARG" ;;
177             k) KERNEL_ARGS="$KERNEL_ARGS $OPTARG" ;;
178             n) DRY_RUN=true ;;
179             h|*) usage ;;
180         esac
181     done
182
183     # use defaults if not set
184     [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
185     [ -z "$VARIANT" ] && VARIANT="build"
186     [ "$CONSOLE_INFO" == "default" ] && CONSOLE_INFO=$SERIAL_CONSOLE
187
188     if [ -n "$NODE_CONFIGURATION_FILE" ] ; then
189     # check existence of NODE_CONFIGURATION_FILE and normalize as we will change directory
190         if [ ! -f "$NODE_CONFIGURATION_FILE" ] ; then
191             echo "Node configuration file $NODE_CONFIGURATION_FILE not found - exiting"
192             exit 1
193         fi
194         cf_dir="$(dirname $NODE_CONFIGURATION_FILE)"
195         cf_dir="$(cd $cf_dir; pwd -P)"
196         cf_file="$(basename $NODE_CONFIGURATION_FILE)"
197         NODE_CONFIGURATION_FILE="$cf_dir"/"$cf_file"
198     fi
199
200     # check TYPES 
201     local matcher="XXX$(echo $ALL_TYPES | sed -e 's,\W,XXX,g')XXX"
202     for t in $TYPES; do
203         echo Checking type $t
204         echo $matcher | grep XXX${t}XXX &> /dev/null
205         if [ "$?" != 0 ] ; then
206             echo Unknown type $t
207             usage
208         fi
209     done
210
211 }
212
213 ####################
214 function init_serial () {
215     local console=$1; shift
216     if [ "$console" == "$GRAPHIC_CONSOLE" ] ; then
217         IS_SERIAL=
218         console_spec=""
219         echo "Standard, graphic, non-serial mode"
220     else
221         IS_SERIAL=true
222         console_dev=$(echo "$console" | awk -F: ' {print $1}')
223         console_baud=$(echo "$console" | awk -F: ' {print $2}')
224         [ -z "$console_baud" ] && console_baud="115200"
225         local console_parity=$(echo "$console" | awk -F: ' {print $3}')
226         [ -z "$console_parity" ] && console_parity="n"
227         local console_bits=$(echo "$console" | awk -F: ' {print $4}')
228         [ -z "$console_bits" ] && console_bits="8"
229         console_spec="console=${console_dev},${console_baud}${console_parity}${console_bits}"
230         local tty_nb=$(echo $console_dev | sed -e 's,[a-zA-Z],,g')
231         console_serial_line="SERIAL ${tty_nb} ${console_baud}"
232         echo "Serial mode"
233         echo "console_serial_line=${console_serial_line}"
234         echo "console_spec=${console_spec}"
235     fi
236 }
237
238 #################### run once : build the overlay image
239 function build_overlay () {
240
241     BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
242     push_cleanup rm -fr "${BUILDTMP}"
243
244     # initialize ISOFS
245     ISOFS="${BUILDTMP}/isofs"
246     mkdir -p "$ISOFS"
247     for i in "$ISOREF"/isofs/{bootcd.img,kernel}; do
248         ln -s "$i" "$ISOFS"
249     done
250     # use new location as of fedora 12
251     # used to be in /usr/lib/syslinux/isolinux.bin
252     # removed backward compat in jan. 2015
253     # as of syslinux 5.0 (fedora 21) ldlinux.c32 is required by isolinux.bin
254     # the debug version can come in handy at times, and is 40k as well
255     isolinuxdir="/usr/share/syslinux"
256     isolinuxfiles="isolinux.bin ldlinux.c32 isolinux-debug.bin memdisk"
257     for isolinuxfile in $isolinuxfiles; do
258         [ -f $isolinuxdir/$isolinuxfile ] && cp $isolinuxdir/$isolinuxfile "${BUILDTMP}/isofs"
259     done
260
261     # Root of the ISO and USB images
262     echo "* Populating root filesystem..."
263     OVERLAY="${BUILDTMP}/overlay"
264     install -d -m 755 $OVERLAY
265     push_cleanup rm -fr $OVERLAY
266
267     # Create version files
268     echo "* Creating version files"
269
270     # Boot Manager compares pl_version in both places to make sure that
271     # the right CD is mounted. We used to boot from an initrd and mount
272     # the CD on /usr. Now we just run everything out of the initrd.
273     for file in $OVERLAY/pl_version $OVERLAY/usr/isolinux/pl_version ; do
274         mkdir -p $(dirname $file)
275         echo "$FULL_VERSION_STRING" >$file
276     done
277
278     # Install boot server configuration files
279     echo "* Installing boot server configuration files"
280
281     # We always intended to bring up and support backup boot servers,
282     # but never got around to it. Just install the same parameters for
283     # both for now.
284     for dir in $OVERLAY/usr/boot $OVERLAY/usr/boot/backup ; do
285         install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
286         install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
287         echo "$PLC_BOOT_HOST" >$dir/boot_server
288         echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
289         echo "/boot/" >$dir/boot_server_path
290     done
291
292     # Install old-style boot server configuration files
293     # as opposed to what a former comment suggested, 
294     # this is still required, somewhere in the bootmanager apparently
295     install -D -m 644 $PLC_BOOT_CA_SSL_CRT $OVERLAY/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
296     echo "$FULL_VERSION_STRING" >$OVERLAY/usr/bootme/ID
297     echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER
298     echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER_IP
299     echo "$PLC_BOOT_SSL_PORT" >$OVERLAY/usr/bootme/BOOTPORT
300
301     # Generate /etc/issue
302     echo "* Generating /etc/issue"
303
304     if [ "$PLC_WWW_PORT" = "443" ] ; then
305         PLC_WWW_URL="https://$PLC_WWW_HOST/"
306     elif [ "$PLC_WWW_PORT" != "80" ] ; then
307         PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
308     else
309         PLC_WWW_URL="http://$PLC_WWW_HOST/"
310     fi
311
312     mkdir -p $OVERLAY/etc
313     cat >$OVERLAY/etc/issue <<EOF
314 $FULL_VERSION_STRING
315 $PLC_NAME Node: \n
316 Kernel \r on an \m
317 $PLC_WWW_URL
318
319 This machine is a node in the $PLC_NAME distributed network.  It has
320 not fully booted yet. If you have cancelled the boot process at the
321 request of $PLC_NAME Support, please follow the instructions provided
322 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
323
324 Console login at this point is restricted to root. Provide the root
325 password of the default $PLC_NAME Central administrator account at the
326 time that this CD was created.
327
328 EOF
329     
330     # Set root password
331     echo "* Setting root password"
332
333     if [ -z "$ROOT_PASSWORD" ] ; then
334         # Generate an encrypted password with crypt() if not defined
335         # in a static configuration.
336         ROOT_PASSWORD=$(python <<EOF
337 import crypt, random, string
338 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
339 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
340 EOF
341 )
342     fi
343
344     # build/passwd copied out by prep.sh
345     sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" ${VARIANT}/passwd >$OVERLAY/etc/passwd
346
347     # Install node configuration file (e.g., if node has no floppy disk or USB slot)
348     if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
349         echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
350         install -D -m 644 $NODE_CONFIGURATION_FILE $OVERLAY/usr/boot/plnode.txt
351         NODE_ID=$(source $NODE_CONFIGURATION_FILE; echo $NODE_ID)
352         echo "* Building network configuration for $NODE_ID"
353         plnet -- --root $OVERLAY --files-only --program BootCD $NODE_ID
354     fi
355
356     [ -n "$IS_SERIAL" ] && KERNEL_ARGS="$KERNEL_ARGS ${console_spec}"
357
358     # tmp: should be restricted to f15 nodes and above
359     # making sure the network interfaces are still numbered eth0 and above
360     KERNEL_ARGS="$KERNEL_ARGS biosdevname=0"
361     # making sure selinux is turned off - somehow this is needed with lxc/f14
362     KERNEL_ARGS="$KERNEL_ARGS selinux=0"
363 # cannot use this mecahnism to set systemd default target because this applies to kexec boots as well
364 #    # set default target for systemd
365 #    KERNEL_ARGS="$KERNEL_ARGS systemd.unit=pl_boot.target"
366     # output more systemd-related messages on the console
367     KERNEL_ARGS="$KERNEL_ARGS systemd.log_target=console"
368     # add any debug flag if any (defined in the header of this script)
369     KERNEL_ARGS="$KERNEL_ARGS $KERNEL_DEBUG_ARGS"
370     # propagate kernel args for later boot stages
371     [ -n "$KERNEL_ARGS" ] && echo "$KERNEL_ARGS" > $OVERLAY/kargs.txt
372
373     # Pack overlay files into a compressed archive
374     echo "* Compressing overlay image"
375     (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
376
377     rm -rf $OVERLAY
378     pop_cleanup
379
380     if [ -n "$CUSTOM_DIR" ]; then
381         echo "* Compressing custom image"
382         (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
383     fi
384
385     # Calculate ramdisk size (total uncompressed size of both archives)
386     ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
387     ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
388
389     echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
390
391     popd
392 }
393
394 #################### plain ISO
395 function build_iso() {
396     local iso="$1" ; shift
397     local custom="$1"
398
399     # Write isolinux configuration
400     cat >$ISOFS/isolinux.cfg <<EOF
401 ${console_serial_line}
402 DEFAULT kernel
403 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
404 DISPLAY pl_version
405 PROMPT 0
406 TIMEOUT 40
407 EOF
408
409     # Create ISO image
410     echo "* Creating ISO image in $(pwd)"
411     mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
412 }
413
414 #################### USB with partitions
415 function build_usb_partition() {
416     echo -n "* Creating USB image with partitions..."
417     local usb="$1" ; shift
418     local custom="$1"
419
420     local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
421     size=$(( $size / 1024 ))
422
423     local heads=64
424     local sectors=32
425     local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
426     local offset=$(( $sectors*512 ))
427
428     if [ -f  /usr/lib/syslinux/mkdiskimage ] ; then
429         /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
430     else
431         mkdiskimage -M -4 "$usb" $size $heads $sectors
432     fi
433
434     cat >${BUILDTMP}/mtools.conf<<EOF
435 drive z:
436 file="${usb}"
437 cylinders=$cylinders
438 heads=$heads
439 sectors=$sectors
440 offset=$offset
441 mformat_only
442 mtools_skip_check=1
443 EOF
444     # environment variable for mtools
445     export MTOOLSRC="${BUILDTMP}/mtools.conf"
446
447     ### COPIED FROM build_usb() below!!!!
448     echo -n " populating USB image... "
449     mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
450         
451     # Use syslinux instead of isolinux to make the image bootable
452     tmp="${BUILDTMP}/syslinux.cfg"
453     cat >$tmp <<EOF
454 ${console_serial_line}
455 DEFAULT kernel
456 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
457 DISPLAY pl_version
458 PROMPT 0
459 TIMEOUT 40
460 EOF
461     mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
462     mcopy -i "$usb" "$tmp" z:/syslinux.cfg
463     rm -f "$tmp"
464     rm -f "${MTOOLSRC}"
465     unset MTOOLSRC
466
467     echo "making USB image bootable."
468     syslinux -o $offset "$usb"
469
470 }
471
472 #################### plain USB
473 function build_usb() {
474     echo -n "* Creating USB image... "
475     local usb="$1" ; shift
476     local custom="$1"
477
478     rm -f "$usb"
479     mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
480
481     cat >${BUILDTMP}/mtools.conf<<EOF
482 mtools_skip_check=1
483 EOF
484     # environment variable for mtools
485     export MTOOLSRC="${BUILDTMP}/mtools.conf"
486
487     # Populate it
488     echo -n " populating USB image... "
489     mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
490
491     # Use syslinux instead of isolinux to make the image bootable
492     tmp="${BUILDTMP}/syslinux.cfg"
493     cat >$tmp <<EOF
494 ${console_serial_line}
495 DEFAULT kernel
496 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
497 DISPLAY pl_version
498 PROMPT 0
499 TIMEOUT 40
500 EOF
501     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
502     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
503     rm -f "$tmp"
504     rm -f "${MTOOLSRC}"
505     unset MTOOLSRC
506
507     echo "making USB image bootable."
508     syslinux "$usb"
509 }
510
511 #################### utility to setup CRAMFS related support
512 function prepare_cramfs() {
513     [ -n "$CRAMFS_PREPARED" ] && return 0
514     local custom=$1; 
515
516     echo "* Setting up CRAMFS-based images"
517     local tmp="${BUILDTMP}/cramfs-tree"
518     mkdir -p "$tmp"
519     push_cleanup rm -rf $tmp
520     pushd $tmp
521     gzip -d -c $ISOFS/bootcd.img     | cpio -diu
522     gzip -d -c $ISOFS/overlay.img    | cpio -diu
523     [ -n "$custom" ] && \
524         gzip -d -c $ISOFS/custom.img | cpio -diu
525
526     # clean out unnecessary rpm lib
527     echo "* clearing var/lib/rpm/*"
528     rm -f var/lib/rpm/*
529
530     # bootcd requires this directory
531     mkdir -p mnt/confdevice
532
533     # relocate various directory to /tmp
534     rm -rf root
535     ln -fs /tmp/root root
536     ln -fs /sbin/init linuxrc 
537     ln -fs /tmp/resolv.conf etc/resolv.conf
538     ln -fs /tmp/etc/mtab etc/mtab
539
540     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
541     # make /tmp/etc
542     echo "* renaming dirs in ./etc"
543     pushd etc
544     for dir in `find * -type d -prune | grep -v rc.d`; do
545         mv ${dir} ${dir}_o
546         ln -fs /tmp/etc/${dir} ${dir}
547     done
548     popd
549
550     echo "* renaming dirs in ./var"
551     # rename all top-level directories and put in a symlink to /tmp/var
552     pushd var
553     for dir in `find * -type d -prune`; do
554         mv ${dir} ${dir}_o
555         ln -fs /tmp/var/${dir} ${dir}
556     done
557     popd
558
559     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
560     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
561     rm -f ./etc/fstab
562     cat >./etc/fstab <<EOF
563 /dev/ram0     /              cramfs     ro              0 0
564 none          /dev/pts       devpts     gid=5,mode=620  0 0
565 none          /proc          proc       defaults        0 0
566 none          /sys           sysfs      defaults        0 0
567 EOF
568
569     pushd dev
570     rm -f console
571     mknod console c 5 1
572     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
573     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
574     #ln -fs ram1 ram
575     #ln -fs ram0 ramdisk
576     popd
577
578     # update etc/inittab to start with pl_rsysinit
579     for file in etc/inittab etc/event.d/rcS etc/init/rcS.conf; do
580         [ -f $file ] && sed -i 's,pl_sysinit,pl_rsysinit,' $file
581     done
582
583     # modify inittab to have a serial console
584     # xxx this might well be broken with f12 and above xxx
585     if [ -n "$serial" ] ; then
586         echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
587         # and let root log in
588         echo "$console_dev" >> etc/securetty
589     fi
590
591     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
592     etcsize=$(du -s ./etc | awk '{ print $1 }')
593     varsize=$(du -s ./var | awk '{ print $1 }')
594     let msize=($varsize+$etcsize+8192)/1024
595
596     # make dhclient happy
597     for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
598     ln -fs /tmp/etc/resolv.conf etc/resolv.conf
599     ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
600
601     # generate pl_rsysinit
602     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
603 #!/bin/sh
604 # generated by $COMMAND
605 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
606 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
607 mkdir -p /tmp/root
608 mkdir -p /tmp/etc
609 touch /tmp/etc/resolv.conf
610 touch /tmp/etc/mtab
611 mkdir -p /tmp/var
612
613 # make mtab happy
614 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
615
616 # copy over directory contents of all _o directories from /etc and /var
617 # /tmp/etc and /tmp/var
618 pushd /etc
619 for odir in \$(cd /etc && ls -d *_o); do dir=\$(echo \$odir | sed 's,\_o$,,'); (mkdir -p /tmp/etc/\$dir && cd \$odir && find . | cpio -p -d -u /tmp/etc/\$dir); done
620 popd
621 pushd /var
622 for odir in \$(cd /var && ls -d *_o); do dir=\$(echo \$odir | sed 's,\_o$,,'); (mkdir -p /tmp/var/\$dir && cd \$odir && find . | cpio -p -d -u /tmp/var/\$dir); done
623 popd
624
625 echo "done"
626
627 # hand over to pl_sysinit
628 echo "pl_rsysinit: handing over to pl_sysinit"
629 /etc/init.d/pl_sysinit
630 EOF
631     chmod +x etc/rc.d/init.d/pl_rsysinit
632
633     popd
634
635     # create the cramfs image
636     echo "* Creating cramfs image"
637     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
638     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
639     rm -rf $tmp
640     pop_cleanup
641 }
642
643 #################### Create ISO CRAMFS image
644 function build_iso_cramfs() {
645     local iso="$1" ; shift
646     local custom="$1"
647
648     prepare_cramfs "$custom"
649     echo "* Creating ISO CRAMFS-based image"
650
651     local tmp="${BUILDTMP}/cramfs-iso"
652     mkdir -p "$tmp"
653     push_cleanup rm -rf $tmp
654     (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
655     cat >$tmp/isolinux.cfg <<EOF
656 ${console_serial_line}
657 DEFAULT kernel
658 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${KERNEL_ARGS}
659 DISPLAY pl_version
660 PROMPT 0
661 TIMEOUT 40
662 EOF
663
664     cp ${BUILDTMP}/cramfs.img $tmp
665     mkisofs -o "$iso" \
666         $MKISOFS_OPTS \
667         $tmp
668
669     rm -fr "$tmp"
670     pop_cleanup
671 }
672
673 #################### Create USB CRAMFS based image
674 function build_usb_cramfs() {
675     local usb="$1" ; shift
676     local custom="$1"
677
678     prepare_cramfs "$custom"
679     echo "* Creating USB CRAMFS based image"
680
681     let vfat_size=${cramfs_size}+$FREE_SPACE
682
683     # Make VFAT filesystem for USB
684     mkfs.vfat -C "$usb" $vfat_size
685
686     # Populate it
687     echo "* Populating USB with overlay images and cramfs"
688     mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
689     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
690
691     # Use syslinux instead of isolinux to make the image bootable
692     tmp="${BUILDTMP}/syslinux.cfg"
693     cat >$tmp <<EOF
694 ${console_serial_line}
695 DEFAULT kernel
696 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${KERNEL_ARGS}
697 DISPLAY pl_version
698 PROMPT 0
699 TIMEOUT 40
700 EOF
701
702     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
703     rm -f "$tmp"
704
705     echo "* Making USB CRAMFS based image bootable"
706     syslinux "$usb"
707 }
708
709 #################### map on all types provided on the command-line and invoke one of the above functions
710 function build_types () {
711
712     [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
713
714     # alter output filename to reflect serial settings
715     if [ -n "$IS_SERIAL" ] ; then
716         if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
717             serial="-serial"
718         else
719             serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
720         fi
721     else
722         serial=""
723     fi
724     
725     function type_to_name() {
726         echo $1 | sed '
727         s/usb$/.usb/;
728         s/usb_partition$/-partition.usb/;
729         s/iso$/.iso/;
730         s/usb_cramfs$/-cramfs.usb/;
731         s/iso_cramfs$/-cramfs.iso/;
732         '
733     }
734
735     for t in $TYPES; do
736         arg=$t
737
738         tname=`type_to_name $t`
739         # if -o is specified (as it has no default)
740         if [ -n "$OUTPUT_NAME" ] ; then
741             output=$OUTPUT_NAME
742         else
743             output="${OUTPUT_BASE}${serial}${tname}"
744         fi
745
746         echo "*** Dealing with type=$arg"
747         echo '*' build_$t "$output" "$CUSTOM_DIR"
748         [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR" 
749     done
750 }
751
752 #################### 
753 function main () {
754
755     parse_command_line "$@"
756
757     init_and_check
758
759     echo "* Building images for $FULL_VERSION_STRING"
760     # Do not tolerate errors
761     set -e
762     trap "do_cleanup" ERR INT EXIT
763
764     init_serial $CONSOLE_INFO
765     build_overlay
766     build_types
767
768     exit 0
769 }
770
771 ####################
772 main "$@"