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