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