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