allow -k multiple times
[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     fi
307
308     if [ -n "$IS_SERIAL" ] ; then
309         KARGS_STR="$KARGS_STR ${console_spec}"
310     fi
311
312     if [ -n "$KARGS_STR" ] ; then
313         echo "$KARGS_STR" > $OVERLAY/kargs.txt
314         kernel_args=$KARGS_STR
315     fi
316
317     # Pack overlay files into a compressed archive
318     echo "* Compressing overlay image"
319     (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
320
321     rm -rf $OVERLAY
322     pop_cleanup
323
324     if [ -n "$CUSTOM_DIR" ]; then
325         echo "* Compressing custom image"
326         (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
327     fi
328
329     # Calculate ramdisk size (total uncompressed size of both archives)
330     ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
331     ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
332
333     echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
334
335     popd
336 }
337
338 #################### plain ISO
339 function build_iso() {
340     local iso="$1" ; shift
341     local custom="$1"
342
343     # Write isolinux configuration
344     cat >$ISOFS/isolinux.cfg <<EOF
345 ${console_serial_line}
346 DEFAULT kernel
347 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
348 DISPLAY pl_version
349 PROMPT 0
350 TIMEOUT 40
351 EOF
352
353     # Create ISO image
354     echo "* Creating ISO image"
355     mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
356 }
357
358 #################### USB with partitions
359 function build_usb_partition() {
360     echo -n "* Creating USB image with partitions..."
361     local usb="$1" ; shift
362     local custom="$1"
363
364     local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
365     size=$(( $size / 1024 ))
366
367     local heads=64
368     local sectors=32
369     local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
370     local offset=$(( $sectors*512 ))
371
372     /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
373     
374     cat >${BUILDTMP}/mtools.conf<<EOF
375 drive z:
376 file="${usb}"
377 cylinders=$cylinders
378 heads=$heads
379 sectors=$sectors
380 offset=$offset
381 mformat_only
382 EOF
383     # environment variable for mtools
384     export MTOOLSRC="${BUILDTMP}/mtools.conf"
385
386     ### COPIED FROM build_usb() below!!!!
387     echo -n " populating USB image... "
388     mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
389         
390     # Use syslinux instead of isolinux to make the image bootable
391     tmp="${BUILDTMP}/syslinux.cfg"
392     cat >$tmp <<EOF
393 ${console_serial_line}
394 DEFAULT kernel
395 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
396 DISPLAY pl_version
397 PROMPT 0
398 TIMEOUT 40
399 EOF
400     mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
401     mcopy -i "$usb" "$tmp" z:/syslinux.cfg
402     rm -f "$tmp"
403     rm -f "${BUILDTMP}/mtools.conf"
404     unset MTOOLSRC
405
406     echo "making USB image bootable."
407     syslinux -o $offset "$usb"
408
409 }
410
411 #################### plain USB
412 function build_usb() {
413     echo -n "* Creating USB image... "
414     local usb="$1" ; shift
415     local custom="$1"
416
417     mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
418
419     # Populate it
420     echo -n " populating USB image... "
421     mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
422
423     # Use syslinux instead of isolinux to make the image bootable
424     tmp="${BUILDTMP}/syslinux.cfg"
425     cat >$tmp <<EOF
426 ${console_serial_line}
427 DEFAULT kernel
428 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
429 DISPLAY pl_version
430 PROMPT 0
431 TIMEOUT 40
432 EOF
433     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
434     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
435     rm -f "$tmp"
436
437     echo "making USB image bootable."
438     syslinux "$usb"
439 }
440
441 #################### utility to setup CRAMFS related support
442 function prepare_cramfs() {
443     [ -n "$CRAMFS_PREPARED" ] && return 0
444     local custom=$1; 
445
446     echo "* Setting up CRAMFS-based images"
447     local tmp="${BUILDTMP}/cramfs-tree"
448     mkdir -p "$tmp"
449     push_cleanup rm -rf $tmp
450     pushd $tmp
451     gzip -d -c $ISOFS/bootcd.img     | cpio -diu
452     gzip -d -c $ISOFS/overlay.img    | cpio -diu
453     [ -n "$custom" ] && \
454         gzip -d -c $ISOFS/custom.img | cpio -diu
455
456     # clean out unnecessary rpm lib
457     echo "* clearing var/lib/rpm/*"
458     rm -f var/lib/rpm/*
459
460     # bootcd requires this directory
461     mkdir -p mnt/confdevice
462
463     # relocate various directory to /tmp
464     rm -rf root
465     ln -fs /tmp/root root
466     ln -fs /sbin/init linuxrc 
467     ln -fs /tmp/resolv.conf etc/resolv.conf
468     ln -fs /tmp/etc/mtab etc/mtab
469
470     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
471     # make /tmp/etc
472     echo "* renaming dirs in ./etc"
473     pushd etc
474     for dir in `find * -type d -prune | grep -v rc.d`; do
475         mv ${dir} ${dir}_o
476         ln -fs /tmp/etc/${dir} ${dir}
477     done
478     popd
479
480     echo "* renaming dirs in ./var"
481     # rename all top-level directories and put in a symlink to /tmp/var
482     pushd var
483     for dir in `find * -type d -prune`; do
484         mv ${dir} ${dir}_o
485         ln -fs /tmp/var/${dir} ${dir}
486     done
487     popd
488
489     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
490     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
491     rm -f ./etc/fstab
492     cat >./etc/fstab <<EOF
493 /dev/ram0     /              cramfs     ro              0 0
494 none          /dev/pts       devpts     gid=5,mode=620  0 0
495 none          /proc          proc       defaults        0 0
496 none          /sys           sysfs      defaults        0 0
497 EOF
498
499     pushd dev
500     rm -f console
501     mknod console c 5 1
502     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
503     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
504     #ln -fs ram1 ram
505     #ln -fs ram0 ramdisk
506     popd
507
508     # update etc/inittab to start with pl_rsysinit
509     sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
510
511     # modify inittab to have a serial console
512     if [ -n "$serial" ] ; then
513         echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
514         # and let root log in
515         echo "$console_dev" >> etc/securetty
516     fi
517
518     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
519     etcsize=$(du -s ./etc | awk '{ print $1 }')
520     varsize=$(du -s ./var | awk '{ print $1 }')
521     let msize=($varsize+$etcsize+8192)/1024
522
523     # make dhclient happy
524     for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
525     ln -fs /tmp/etc/resolv.conf etc/resolv.conf
526     ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
527
528     # generate pl_rsysinit
529     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
530 #!/bin/sh
531 # generated by build.sh
532 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
533 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
534 mkdir -p /tmp/root
535 mkdir -p /tmp/etc
536 touch /tmp/etc/resolv.conf
537 touch /tmp/etc/mtab
538 mkdir -p /tmp/var
539
540 # make mtab happy
541 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
542
543 # copy over directory contents of all _o directories from /etc and /var
544 # /tmp/etc and /tmp/var
545 pushd /etc
546 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
547 popd
548 pushd /var
549 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
550 popd
551
552 echo "done"
553
554 # hand over to pl_sysinit
555 echo "pl_rsysinit: handing over to pl_sysinit"
556 /etc/init.d/pl_sysinit
557 EOF
558     chmod +x etc/rc.d/init.d/pl_rsysinit
559
560     popd
561
562     # create the cramfs image
563     echo "* Creating cramfs image"
564     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
565     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
566     rm -rf $tmp
567     pop_cleanup
568 }
569
570 #################### Create ISO CRAMFS image
571 function build_iso_cramfs() {
572     local iso="$1" ; shift
573     local custom="$1"
574
575     prepare_cramfs "$custom"
576     echo "* Creating ISO CRAMFS-based image"
577
578     local tmp="${BUILDTMP}/cramfs-iso"
579     mkdir -p "$tmp"
580     push_cleanup rm -rf $tmp
581     (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
582     cat >$tmp/isolinux.cfg <<EOF
583 ${console_serial_line}
584 DEFAULT kernel
585 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
586 DISPLAY pl_version
587 PROMPT 0
588 TIMEOUT 40
589 EOF
590
591     cp ${BUILDTMP}/cramfs.img $tmp
592     mkisofs -o "$iso" \
593         $MKISOFS_OPTS \
594         $tmp
595
596     rm -fr "$tmp"
597     pop_cleanup
598 }
599
600 #################### Create USB CRAMFS based image
601 function build_usb_cramfs() {
602     local usb="$1" ; shift
603     local custom="$1"
604
605     prepare_cramfs "$custom"
606     echo "* Creating USB CRAMFS based image"
607
608     let vfat_size=${cramfs_size}+$FREE_SPACE
609
610     # Make VFAT filesystem for USB
611     mkfs.vfat -C "$usb" $vfat_size
612
613     # Populate it
614     echo "* Populating USB with overlay images and cramfs"
615     mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
616     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
617
618     # Use syslinux instead of isolinux to make the image bootable
619     tmp="${BUILDTMP}/syslinux.cfg"
620     cat >$tmp <<EOF
621 ${console_serial_line}
622 DEFAULT kernel
623 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
624 DISPLAY pl_version
625 PROMPT 0
626 TIMEOUT 40
627 EOF
628
629     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
630     rm -f "$tmp"
631
632     echo "* Making USB CRAMFS based image bootable"
633     syslinux "$usb"
634 }
635
636 #################### map on all types provided on the command-line and invoke one of the above functions
637 function build_types () {
638
639     [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
640
641     # alter output filename to reflect serial settings
642     if [ -n "$IS_SERIAL" ] ; then
643         if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
644             serial="-serial"
645         else
646             serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
647         fi
648     else
649         serial=""
650     fi
651     
652     function type_to_name() {
653         echo $1 | sed '
654         s/usb$/.usb/;
655         s/usb_partition$/-partition.usb/;
656         s/iso$/.iso/;
657         s/usb_cramfs$/-cramfs.usb/;
658         s/iso_cramfs$/-cramfs.iso/;
659         '
660     }
661
662     for t in $TYPES; do
663         arg=$t
664
665         tname=`type_to_name $t`
666         # if -o is specified (as it has no default)
667         if [ -n "$OUTPUT_NAME" ] ; then
668             output=$OUTPUT_NAME
669         else
670             output="${OUTPUT_BASE}${serial}${tname}"
671         fi
672
673         echo "*** Dealing with type=$arg"
674         echo '*' build_$t "$output" "$CUSTOM_DIR"
675         [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR" 
676     done
677 }
678
679 #################### 
680 function main () {
681
682     init_and_check
683
684     parse_command_line "$@"
685
686     echo "* Building images for $FULL_VERSION_STRING"
687     # Do not tolerate errors
688     set -e
689     trap "do_cleanup" ERR INT EXIT
690
691     init_serial $CONSOLE_INFO
692     build_overlay
693     build_types
694
695     exit 0
696 }
697
698 ####################
699 main "$@"