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