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