- refactored marc's kupdate.sh into a new script kvariant.sh
[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 COMMAND=$(basename $0)
14 DIRNAME=$(dirname $0)
15 PATH=/sbin:/bin:/usr/sbin:/usr/bin
16
17 # defaults
18 DEFAULT_TYPES="usb iso"
19 # Leave 4 MB of free space
20 GRAPHIC_CONSOLE="graphic"
21 SERIAL_CONSOLE="ttyS0:115200:n:8"
22 CONSOLE_INFO=$GRAPHIC_CONSOLE
23 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
24 FREE_SPACE=4096
25
26 # command-line settable args
27 NODE_CONFIGURATION_FILE=
28 CUSTOM_DIR=
29 OUTPUT_BASE=
30 DRY_RUN=""
31 OUTPUT_NAME=""
32 TYPES=""
33 KARGS_STR=""
34
35 # various globals
36 BUILDTMP=""
37 FULL_VERSION_STRING=""
38 ISOREF=""
39 ISOFS=""
40 OVERLAY=""
41 IS_SERIAL=""
42 console_dev=""
43 console_baud=""
44 console_spec=""
45 console_serial_line=""
46 kernel_args=""
47
48
49 #################### compute all supported types
50 # removing support for serial in the type
51 # this is because kargs.txt goes in the overlay, that is computed only once
52 # so we cannot handle serial and graphic modes within the same invokation of this script
53
54 ALL_TYPES=""
55 for x in iso usb usb_partition; do for c in "" "_cramfs" ; do
56   t="${x}${c}"
57   case $t in
58       usb_partition_cramfs)
59           # unsupported
60           ;;
61       *)
62           ALL_TYPES="$ALL_TYPES $t" ;;
63   esac
64 done; done
65
66 #################### cleanup utilities
67 declare -a _CLEANUPS=()
68 function do_cleanup() {
69     cd / ; for i in "${_CLEANUPS[@]}"; do $i ; done
70 }
71 function push_cleanup() {
72     _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
73 }
74 function pop_cleanup() {
75     unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
76 }
77
78 #################### initialization
79 function init_and_check () {
80
81     # Change to our source directory
82     local srcdir=$(cd $DIRNAME && pwd -P)
83     pushd $srcdir
84
85     # Root of the isofs
86     ISOREF=$PWD/${VARIANT}
87
88     # The reference image is expected to have been built by prep.sh (see .spec)
89     # we disable the initial logic that called prep.sh if that was not the case
90     # this is because prep.sh needs to know pldistro 
91     if [ ! -f $ISOREF/isofs/bootcd.img -o ! -f $ISOREF/version.txt ] ; then
92         echo "Could not find isofs and version.txt in $ISOREF"
93         if [ "$VARIANT" == "build" ] ; then
94             echo "You have to run prep.sh prior to calling $COMMAND"
95         else
96             echo "You need to create your variant image, see kvariant.sh"
97         fi
98         echo "Exiting .."
99         exit 1
100     fi
101
102     # build/version.txt written by prep.sh
103     BOOTCD_VERSION=$(cat ${VARIANT}/version.txt)
104
105     if [ -f /etc/planetlab/plc_config ] ; then
106         # Source PLC configuration
107         . /etc/planetlab/plc_config
108     fi
109
110     # use /var/tmp that should be large enough on both chroot- or vserver-based myplc
111     BUILDTMP=/var/tmp
112
113     FULL_VERSION_STRING="${PLC_NAME} BootCD ${BOOTCD_VERSION}"
114
115 }
116
117 # NOTE
118 # the custom-dir feature is designed to let a myplc try/ship a patched bootcd
119 # without the need for a full devel environment
120 # for example, you would create /root/custom-bootcd/etc/rc.d/init.d/pl_hwinit
121 # and run this script with -C /root/custom-bootcd
122 # this creates a third .img image of the custom dir, that 'hides' the files from 
123 # bootcd.img in the resulting unionfs
124 # it seems that this feature has not been used nor tested in a long time, use with care
125
126 usage() {
127     echo "Usage: $COMMAND [OPTION]..."
128     echo "    -f plnode.txt    Node to customize CD for (default: none)"
129     echo "    -t 'types'       Build the specified images (default: $DEFAULT_TYPES)"
130     echo "                     NOTE: mentioning 'serial' as part of the type is not supported anymore"
131     echo "    -a               Build all known types as listed below"
132     echo "    -s console-info  Enable a serial line as console and also bring up getty on that line"
133     echo "                     console-info: tty:baud-rate:parity:bits"
134     echo "                     or 'default' shortcut for $SERIAL_CONSOLE"
135     echo "    -S               equivalent to -s default"
136     echo "    -O output-base   The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
137     echo "                     useful when multiple types are provided"
138     echo "                     can be a full path"
139     echo "    -o output-name   The full name of the generated file"
140     echo "    -C custom-dir    Custom directory"
141     echo "    -V variant       Use a variant - see kvariant.sh"
142     echo "    -n               Dry run - mostly for debug/test purposes"
143     echo "    -k               Add additional parameters to the kargs.txt file"
144     echo "    -h               This message"
145     echo "All known types: $ALL_TYPES"
146     exit 1
147 }
148
149 #################### 
150 function parse_command_line () {
151
152     # init
153     TYPES=""
154     # Get options
155     while getopts "f:t:as:SO:o:C:V:k:nh" opt ; do
156         case $opt in
157             f) NODE_CONFIGURATION_FILE=$OPTARG ;;
158             t) TYPES="$TYPES $OPTARG" ;;
159             a) TYPES="$ALL_TYPES" ;;
160             s) CONSOLE_INFO="$OPTARG" ;;
161             S) CONSOLE_INFO=$SERIAL_CONSOLE ;;
162             O) OUTPUT_BASE="$OPTARG" ;;
163             o) OUTPUT_NAME="$OPTARG" ;;
164             C) CUSTOM_DIR="$OPTARG" ;;
165             V) VARIANT="$OPTARG" ;;
166             k) KARGS_STR="$KARGS_STR $OPTARG" ;;
167             n) DRY_RUN=true ;;
168             h|*) usage ;;
169         esac
170     done
171
172     # use defaults if not set
173     [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
174     [ -z "$VARIANT" ] && VARIANT="build"
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@" ${VARIANT}/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         NODE_ID=$(source $NODE_CONFIGURATION_FILE; echo $NODE_ID)
318         echo "* Building network configuration for $NODE_ID"
319         plnet -- --root $OVERLAY --files-only --program BootCD $NODE_ID
320     fi
321
322     if [ -n "$IS_SERIAL" ] ; then
323         KARGS_STR="$KARGS_STR ${console_spec}"
324     fi
325
326     if [ -n "$KARGS_STR" ] ; then
327         echo "$KARGS_STR" > $OVERLAY/kargs.txt
328         kernel_args=$KARGS_STR
329     fi
330
331     # Pack overlay files into a compressed archive
332     echo "* Compressing overlay image"
333     (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
334
335     rm -rf $OVERLAY
336     pop_cleanup
337
338     if [ -n "$CUSTOM_DIR" ]; then
339         echo "* Compressing custom image"
340         (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
341     fi
342
343     # Calculate ramdisk size (total uncompressed size of both archives)
344     ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
345     ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
346
347     echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
348
349     popd
350 }
351
352 #################### plain ISO
353 function build_iso() {
354     local iso="$1" ; shift
355     local custom="$1"
356
357     # Write isolinux configuration
358     cat >$ISOFS/isolinux.cfg <<EOF
359 ${console_serial_line}
360 DEFAULT kernel
361 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
362 DISPLAY pl_version
363 PROMPT 0
364 TIMEOUT 40
365 EOF
366
367     # Create ISO image
368     echo "* Creating ISO image"
369     mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
370 }
371
372 #################### USB with partitions
373 function build_usb_partition() {
374     echo -n "* Creating USB image with partitions..."
375     local usb="$1" ; shift
376     local custom="$1"
377
378     local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
379     size=$(( $size / 1024 ))
380
381     local heads=64
382     local sectors=32
383     local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
384     local offset=$(( $sectors*512 ))
385
386     /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
387     
388     cat >${BUILDTMP}/mtools.conf<<EOF
389 drive z:
390 file="${usb}"
391 cylinders=$cylinders
392 heads=$heads
393 sectors=$sectors
394 offset=$offset
395 mformat_only
396 EOF
397     # environment variable for mtools
398     export MTOOLSRC="${BUILDTMP}/mtools.conf"
399
400     ### COPIED FROM build_usb() below!!!!
401     echo -n " populating USB image... "
402     mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
403         
404     # Use syslinux instead of isolinux to make the image bootable
405     tmp="${BUILDTMP}/syslinux.cfg"
406     cat >$tmp <<EOF
407 ${console_serial_line}
408 DEFAULT kernel
409 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
410 DISPLAY pl_version
411 PROMPT 0
412 TIMEOUT 40
413 EOF
414     mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
415     mcopy -i "$usb" "$tmp" z:/syslinux.cfg
416     rm -f "$tmp"
417     rm -f "${BUILDTMP}/mtools.conf"
418     unset MTOOLSRC
419
420     echo "making USB image bootable."
421     syslinux -o $offset "$usb"
422
423 }
424
425 #################### plain USB
426 function build_usb() {
427     echo -n "* Creating USB image... "
428     local usb="$1" ; shift
429     local custom="$1"
430
431     rm -f "$usb"
432     mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
433
434     # Populate it
435     echo -n " populating USB image... "
436     mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
437
438     # Use syslinux instead of isolinux to make the image bootable
439     tmp="${BUILDTMP}/syslinux.cfg"
440     cat >$tmp <<EOF
441 ${console_serial_line}
442 DEFAULT kernel
443 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
444 DISPLAY pl_version
445 PROMPT 0
446 TIMEOUT 40
447 EOF
448     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
449     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
450     rm -f "$tmp"
451
452     echo "making USB image bootable."
453     syslinux "$usb"
454 }
455
456 #################### utility to setup CRAMFS related support
457 function prepare_cramfs() {
458     [ -n "$CRAMFS_PREPARED" ] && return 0
459     local custom=$1; 
460
461     echo "* Setting up CRAMFS-based images"
462     local tmp="${BUILDTMP}/cramfs-tree"
463     mkdir -p "$tmp"
464     push_cleanup rm -rf $tmp
465     pushd $tmp
466     gzip -d -c $ISOFS/bootcd.img     | cpio -diu
467     gzip -d -c $ISOFS/overlay.img    | cpio -diu
468     [ -n "$custom" ] && \
469         gzip -d -c $ISOFS/custom.img | cpio -diu
470
471     # clean out unnecessary rpm lib
472     echo "* clearing var/lib/rpm/*"
473     rm -f var/lib/rpm/*
474
475     # bootcd requires this directory
476     mkdir -p mnt/confdevice
477
478     # relocate various directory to /tmp
479     rm -rf root
480     ln -fs /tmp/root root
481     ln -fs /sbin/init linuxrc 
482     ln -fs /tmp/resolv.conf etc/resolv.conf
483     ln -fs /tmp/etc/mtab etc/mtab
484
485     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
486     # make /tmp/etc
487     echo "* renaming dirs in ./etc"
488     pushd etc
489     for dir in `find * -type d -prune | grep -v rc.d`; do
490         mv ${dir} ${dir}_o
491         ln -fs /tmp/etc/${dir} ${dir}
492     done
493     popd
494
495     echo "* renaming dirs in ./var"
496     # rename all top-level directories and put in a symlink to /tmp/var
497     pushd var
498     for dir in `find * -type d -prune`; do
499         mv ${dir} ${dir}_o
500         ln -fs /tmp/var/${dir} ${dir}
501     done
502     popd
503
504     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
505     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
506     rm -f ./etc/fstab
507     cat >./etc/fstab <<EOF
508 /dev/ram0     /              cramfs     ro              0 0
509 none          /dev/pts       devpts     gid=5,mode=620  0 0
510 none          /proc          proc       defaults        0 0
511 none          /sys           sysfs      defaults        0 0
512 EOF
513
514     pushd dev
515     rm -f console
516     mknod console c 5 1
517     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
518     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
519     #ln -fs ram1 ram
520     #ln -fs ram0 ramdisk
521     popd
522
523     # update etc/inittab to start with pl_rsysinit
524     sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
525
526     # modify inittab to have a serial console
527     if [ -n "$serial" ] ; then
528         echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
529         # and let root log in
530         echo "$console_dev" >> etc/securetty
531     fi
532
533     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
534     etcsize=$(du -s ./etc | awk '{ print $1 }')
535     varsize=$(du -s ./var | awk '{ print $1 }')
536     let msize=($varsize+$etcsize+8192)/1024
537
538     # make dhclient happy
539     for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
540     ln -fs /tmp/etc/resolv.conf etc/resolv.conf
541     ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
542
543     # generate pl_rsysinit
544     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
545 #!/bin/sh
546 # generated by $COMMAND
547 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
548 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
549 mkdir -p /tmp/root
550 mkdir -p /tmp/etc
551 touch /tmp/etc/resolv.conf
552 touch /tmp/etc/mtab
553 mkdir -p /tmp/var
554
555 # make mtab happy
556 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
557
558 # copy over directory contents of all _o directories from /etc and /var
559 # /tmp/etc and /tmp/var
560 pushd /etc
561 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
562 popd
563 pushd /var
564 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
565 popd
566
567 echo "done"
568
569 # hand over to pl_sysinit
570 echo "pl_rsysinit: handing over to pl_sysinit"
571 /etc/init.d/pl_sysinit
572 EOF
573     chmod +x etc/rc.d/init.d/pl_rsysinit
574
575     popd
576
577     # create the cramfs image
578     echo "* Creating cramfs image"
579     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
580     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
581     rm -rf $tmp
582     pop_cleanup
583 }
584
585 #################### Create ISO CRAMFS image
586 function build_iso_cramfs() {
587     local iso="$1" ; shift
588     local custom="$1"
589
590     prepare_cramfs "$custom"
591     echo "* Creating ISO CRAMFS-based image"
592
593     local tmp="${BUILDTMP}/cramfs-iso"
594     mkdir -p "$tmp"
595     push_cleanup rm -rf $tmp
596     (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
597     cat >$tmp/isolinux.cfg <<EOF
598 ${console_serial_line}
599 DEFAULT kernel
600 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
601 DISPLAY pl_version
602 PROMPT 0
603 TIMEOUT 40
604 EOF
605
606     cp ${BUILDTMP}/cramfs.img $tmp
607     mkisofs -o "$iso" \
608         $MKISOFS_OPTS \
609         $tmp
610
611     rm -fr "$tmp"
612     pop_cleanup
613 }
614
615 #################### Create USB CRAMFS based image
616 function build_usb_cramfs() {
617     local usb="$1" ; shift
618     local custom="$1"
619
620     prepare_cramfs "$custom"
621     echo "* Creating USB CRAMFS based image"
622
623     let vfat_size=${cramfs_size}+$FREE_SPACE
624
625     # Make VFAT filesystem for USB
626     mkfs.vfat -C "$usb" $vfat_size
627
628     # Populate it
629     echo "* Populating USB with overlay images and cramfs"
630     mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
631     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
632
633     # Use syslinux instead of isolinux to make the image bootable
634     tmp="${BUILDTMP}/syslinux.cfg"
635     cat >$tmp <<EOF
636 ${console_serial_line}
637 DEFAULT kernel
638 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
639 DISPLAY pl_version
640 PROMPT 0
641 TIMEOUT 40
642 EOF
643
644     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
645     rm -f "$tmp"
646
647     echo "* Making USB CRAMFS based image bootable"
648     syslinux "$usb"
649 }
650
651 #################### map on all types provided on the command-line and invoke one of the above functions
652 function build_types () {
653
654     [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
655
656     # alter output filename to reflect serial settings
657     if [ -n "$IS_SERIAL" ] ; then
658         if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
659             serial="-serial"
660         else
661             serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
662         fi
663     else
664         serial=""
665     fi
666     
667     function type_to_name() {
668         echo $1 | sed '
669         s/usb$/.usb/;
670         s/usb_partition$/-partition.usb/;
671         s/iso$/.iso/;
672         s/usb_cramfs$/-cramfs.usb/;
673         s/iso_cramfs$/-cramfs.iso/;
674         '
675     }
676
677     for t in $TYPES; do
678         arg=$t
679
680         tname=`type_to_name $t`
681         # if -o is specified (as it has no default)
682         if [ -n "$OUTPUT_NAME" ] ; then
683             output=$OUTPUT_NAME
684         else
685             output="${OUTPUT_BASE}${serial}${tname}"
686         fi
687
688         echo "*** Dealing with type=$arg"
689         echo '*' build_$t "$output" "$CUSTOM_DIR"
690         [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR" 
691     done
692 }
693
694 #################### 
695 function main () {
696
697     parse_command_line "$@"
698
699     init_and_check
700
701     echo "* Building images for $FULL_VERSION_STRING"
702     # Do not tolerate errors
703     set -e
704     trap "do_cleanup" ERR INT EXIT
705
706     init_serial $CONSOLE_INFO
707     build_overlay
708     build_types
709
710     exit 0
711 }
712
713 ####################
714 main "$@"