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