Use pyplnet to bring up the network.
[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     mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
421
422     # Populate it
423     echo -n " populating USB image... "
424     mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
425
426     # Use syslinux instead of isolinux to make the image bootable
427     tmp="${BUILDTMP}/syslinux.cfg"
428     cat >$tmp <<EOF
429 ${console_serial_line}
430 DEFAULT kernel
431 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
432 DISPLAY pl_version
433 PROMPT 0
434 TIMEOUT 40
435 EOF
436     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
437     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
438     rm -f "$tmp"
439
440     echo "making USB image bootable."
441     syslinux "$usb"
442 }
443
444 #################### utility to setup CRAMFS related support
445 function prepare_cramfs() {
446     [ -n "$CRAMFS_PREPARED" ] && return 0
447     local custom=$1; 
448
449     echo "* Setting up CRAMFS-based images"
450     local tmp="${BUILDTMP}/cramfs-tree"
451     mkdir -p "$tmp"
452     push_cleanup rm -rf $tmp
453     pushd $tmp
454     gzip -d -c $ISOFS/bootcd.img     | cpio -diu
455     gzip -d -c $ISOFS/overlay.img    | cpio -diu
456     [ -n "$custom" ] && \
457         gzip -d -c $ISOFS/custom.img | cpio -diu
458
459     # clean out unnecessary rpm lib
460     echo "* clearing var/lib/rpm/*"
461     rm -f var/lib/rpm/*
462
463     # bootcd requires this directory
464     mkdir -p mnt/confdevice
465
466     # relocate various directory to /tmp
467     rm -rf root
468     ln -fs /tmp/root root
469     ln -fs /sbin/init linuxrc 
470     ln -fs /tmp/resolv.conf etc/resolv.conf
471     ln -fs /tmp/etc/mtab etc/mtab
472
473     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
474     # make /tmp/etc
475     echo "* renaming dirs in ./etc"
476     pushd etc
477     for dir in `find * -type d -prune | grep -v rc.d`; do
478         mv ${dir} ${dir}_o
479         ln -fs /tmp/etc/${dir} ${dir}
480     done
481     popd
482
483     echo "* renaming dirs in ./var"
484     # rename all top-level directories and put in a symlink to /tmp/var
485     pushd var
486     for dir in `find * -type d -prune`; do
487         mv ${dir} ${dir}_o
488         ln -fs /tmp/var/${dir} ${dir}
489     done
490     popd
491
492     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
493     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
494     rm -f ./etc/fstab
495     cat >./etc/fstab <<EOF
496 /dev/ram0     /              cramfs     ro              0 0
497 none          /dev/pts       devpts     gid=5,mode=620  0 0
498 none          /proc          proc       defaults        0 0
499 none          /sys           sysfs      defaults        0 0
500 EOF
501
502     pushd dev
503     rm -f console
504     mknod console c 5 1
505     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
506     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
507     #ln -fs ram1 ram
508     #ln -fs ram0 ramdisk
509     popd
510
511     # update etc/inittab to start with pl_rsysinit
512     sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
513
514     # modify inittab to have a serial console
515     if [ -n "$serial" ] ; then
516         echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
517         # and let root log in
518         echo "$console_dev" >> etc/securetty
519     fi
520
521     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
522     etcsize=$(du -s ./etc | awk '{ print $1 }')
523     varsize=$(du -s ./var | awk '{ print $1 }')
524     let msize=($varsize+$etcsize+8192)/1024
525
526     # make dhclient happy
527     for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
528     ln -fs /tmp/etc/resolv.conf etc/resolv.conf
529     ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
530
531     # generate pl_rsysinit
532     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
533 #!/bin/sh
534 # generated by build.sh
535 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
536 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
537 mkdir -p /tmp/root
538 mkdir -p /tmp/etc
539 touch /tmp/etc/resolv.conf
540 touch /tmp/etc/mtab
541 mkdir -p /tmp/var
542
543 # make mtab happy
544 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
545
546 # copy over directory contents of all _o directories from /etc and /var
547 # /tmp/etc and /tmp/var
548 pushd /etc
549 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
550 popd
551 pushd /var
552 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
553 popd
554
555 echo "done"
556
557 # hand over to pl_sysinit
558 echo "pl_rsysinit: handing over to pl_sysinit"
559 /etc/init.d/pl_sysinit
560 EOF
561     chmod +x etc/rc.d/init.d/pl_rsysinit
562
563     popd
564
565     # create the cramfs image
566     echo "* Creating cramfs image"
567     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
568     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
569     rm -rf $tmp
570     pop_cleanup
571 }
572
573 #################### Create ISO CRAMFS image
574 function build_iso_cramfs() {
575     local iso="$1" ; shift
576     local custom="$1"
577
578     prepare_cramfs "$custom"
579     echo "* Creating ISO CRAMFS-based image"
580
581     local tmp="${BUILDTMP}/cramfs-iso"
582     mkdir -p "$tmp"
583     push_cleanup rm -rf $tmp
584     (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
585     cat >$tmp/isolinux.cfg <<EOF
586 ${console_serial_line}
587 DEFAULT kernel
588 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
589 DISPLAY pl_version
590 PROMPT 0
591 TIMEOUT 40
592 EOF
593
594     cp ${BUILDTMP}/cramfs.img $tmp
595     mkisofs -o "$iso" \
596         $MKISOFS_OPTS \
597         $tmp
598
599     rm -fr "$tmp"
600     pop_cleanup
601 }
602
603 #################### Create USB CRAMFS based image
604 function build_usb_cramfs() {
605     local usb="$1" ; shift
606     local custom="$1"
607
608     prepare_cramfs "$custom"
609     echo "* Creating USB CRAMFS based image"
610
611     let vfat_size=${cramfs_size}+$FREE_SPACE
612
613     # Make VFAT filesystem for USB
614     mkfs.vfat -C "$usb" $vfat_size
615
616     # Populate it
617     echo "* Populating USB with overlay images and cramfs"
618     mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
619     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
620
621     # Use syslinux instead of isolinux to make the image bootable
622     tmp="${BUILDTMP}/syslinux.cfg"
623     cat >$tmp <<EOF
624 ${console_serial_line}
625 DEFAULT kernel
626 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
627 DISPLAY pl_version
628 PROMPT 0
629 TIMEOUT 40
630 EOF
631
632     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
633     rm -f "$tmp"
634
635     echo "* Making USB CRAMFS based image bootable"
636     syslinux "$usb"
637 }
638
639 #################### map on all types provided on the command-line and invoke one of the above functions
640 function build_types () {
641
642     [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
643
644     # alter output filename to reflect serial settings
645     if [ -n "$IS_SERIAL" ] ; then
646         if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
647             serial="-serial"
648         else
649             serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
650         fi
651     else
652         serial=""
653     fi
654     
655     function type_to_name() {
656         echo $1 | sed '
657         s/usb$/.usb/;
658         s/usb_partition$/-partition.usb/;
659         s/iso$/.iso/;
660         s/usb_cramfs$/-cramfs.usb/;
661         s/iso_cramfs$/-cramfs.iso/;
662         '
663     }
664
665     for t in $TYPES; do
666         arg=$t
667
668         tname=`type_to_name $t`
669         # if -o is specified (as it has no default)
670         if [ -n "$OUTPUT_NAME" ] ; then
671             output=$OUTPUT_NAME
672         else
673             output="${OUTPUT_BASE}${serial}${tname}"
674         fi
675
676         echo "*** Dealing with type=$arg"
677         echo '*' build_$t "$output" "$CUSTOM_DIR"
678         [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR" 
679     done
680 }
681
682 #################### 
683 function main () {
684
685     init_and_check
686
687     parse_command_line "$@"
688
689     echo "* Building images for $FULL_VERSION_STRING"
690     # Do not tolerate errors
691     set -e
692     trap "do_cleanup" ERR INT EXIT
693
694     init_serial $CONSOLE_INFO
695     build_overlay
696     build_types
697
698     exit 0
699 }
700
701 ####################
702 main "$@"