missed closing bracket when substituting kargs for serial.
[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     # write kargs.txt with additional args that should be executed by kexec to production mode
341     cat >$isofs/kargs.txt <<EOF
342 ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
343 EOF
344
345     # Create ISO image
346     echo "* Creating ISO image"
347     mkisofs -o "$iso" \
348         $MKISOFS_OPTS \
349         $isofs
350 }
351
352 function build_usb_partition()
353 {
354     echo -n "* Creating USB image with partitions..."
355     local usb="$1" ; shift
356     local console="$1" ; shift
357     local custom="$1"
358     local serial=
359
360     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
361         serial=1
362         console_dev=$(extract_console_dev $console)
363         console_baud=$(extract_console_baud $console)
364         console_parity=$(extract_console_parity $console)
365         console_bits=$(extract_console_bits $console)
366     fi
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 ${serial:+SERIAL 0 ${console_baud}}
398 DEFAULT kernel
399 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}}
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 # Create USB image
416 function build_usb()
417 {
418     echo -n "* Creating USB image... "
419     local usb="$1" ; shift
420     local console="$1" ; shift
421     local custom="$1"
422     local serial=
423
424     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
425         serial=1
426         console_dev=$(extract_console_dev $console)
427         console_baud=$(extract_console_baud $console)
428         console_parity=$(extract_console_parity $console)
429         console_bits=$(extract_console_bits $console)
430     fi
431
432     mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
433
434     # write kargs.txt with additional args that should be executed by kexec to production mode
435     tmp="${BUILDTMP}/kargs.txt"
436     cat >$tmp <<EOF
437 ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
438 EOF
439     mcopy -i "$usb" "$tmp" ::/kargs.txt
440     rm -f "$tmp"
441
442     # Populate it
443     echo -n " populating USB image... "
444     mcopy -bsQ -i "$usb" "$isofs"/* ::/
445
446     # Use syslinux instead of isolinux to make the image bootable
447     tmp="${BUILDTMP}/syslinux.cfg"
448     cat >$tmp <<EOF
449 ${serial:+SERIAL 0 $console_baud}
450 DEFAULT kernel
451 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}}
452 DISPLAY pl_version
453 PROMPT 0
454 TIMEOUT 40
455 EOF
456     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
457     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
458     rm -f "$tmp"
459
460     echo "making USB image bootable."
461     syslinux "$usb"
462 }
463
464
465 # Setup CRAMFS related support
466 function prepare_cramfs()
467 {
468     [ -n "$CRAMFS_PREPARED" ] && return 0
469     local console=$1; shift
470     local custom=$1; 
471     local serial=
472     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
473         serial=1
474         console_dev=$(extract_console_dev $console)
475         console_baud=$(extract_console_baud $console)
476     fi
477
478     echo "* Setting up CRAMFS-based images"
479     local tmp="${BUILDTMP}/cramfs-tree"
480     mkdir -p "$tmp"
481     push_cleanup rm -rf $tmp
482     pushd $tmp
483     gzip -d -c $isofs/bootcd.img     | cpio -diu
484     gzip -d -c $isofs/overlay.img    | cpio -diu
485     [ -n "$custom" ] && \
486         gzip -d -c $isofs/custom.img | cpio -diu
487
488     # clean out unnecessary rpm lib
489     echo "* clearing var/lib/rpm/*"
490     rm -f var/lib/rpm/*
491
492     # bootcd requires this directory
493     mkdir -p mnt/confdevice
494
495     # relocate various directory to /tmp
496     rm -rf root
497     ln -fs /tmp/root root
498     ln -fs /sbin/init linuxrc 
499     ln -fs /tmp/resolv.conf etc/resolv.conf
500     ln -fs /tmp/etc/mtab etc/mtab
501
502     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
503     # make /tmp/etc
504     echo "* renaming dirs in ./etc"
505     pushd etc
506     for dir in `find * -type d -prune | grep -v rc.d`; do
507         mv ${dir} ${dir}_o
508         ln -fs /tmp/etc/${dir} ${dir}
509     done
510     popd
511
512     echo "* renaming dirs in ./var"
513     # rename all top-level directories and put in a symlink to /tmp/var
514     pushd var
515     for dir in `find * -type d -prune`; do
516         mv ${dir} ${dir}_o
517         ln -fs /tmp/var/${dir} ${dir}
518     done
519     popd
520
521     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
522     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
523     rm -f ./etc/fstab
524     cat >./etc/fstab <<EOF
525 /dev/ram0     /              cramfs     ro              0 0
526 none          /dev/pts       devpts     gid=5,mode=620  0 0
527 none          /proc          proc       defaults        0 0
528 none          /sys           sysfs      defaults        0 0
529 EOF
530
531     pushd dev
532     rm -f console
533     mknod console c 5 1
534     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
535     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
536     #ln -fs ram1 ram
537     #ln -fs ram0 ramdisk
538     popd
539
540     # update etc/inittab to start with pl_rsysinit
541     sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
542
543     # modify inittab to have a serial console
544     if [ -n "$serial" ] ; then
545         echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
546         # and let root log in
547         echo "$console_dev" >> etc/securetty
548     fi
549
550     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
551     etcsize=$(du -s ./etc | awk '{ print $1 }')
552     varsize=$(du -s ./var | awk '{ print $1 }')
553     let msize=($varsize+$etcsize+8192)/1024
554
555     # make dhclient happy
556     for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
557     ln -fs /tmp/etc/resolv.conf etc/resolv.conf
558     ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
559
560     # generate pl_rsysinit
561     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
562 #!/bin/sh
563 # generated by build.sh
564 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
565 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
566 mkdir -p /tmp/root
567 mkdir -p /tmp/etc
568 touch /tmp/etc/resolv.conf
569 touch /tmp/etc/mtab
570 mkdir -p /tmp/var
571
572 # make mtab happy
573 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
574
575 # copy over directory contents of all _o directories from /etc and /var
576 # /tmp/etc and /tmp/var
577 pushd /etc
578 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
579 popd
580 pushd /var
581 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
582 popd
583
584 echo "done"
585
586 # hand over to pl_sysinit
587 echo "pl_rsysinit: handing over to pl_sysinit"
588 /etc/init.d/pl_sysinit
589 EOF
590     chmod +x etc/rc.d/init.d/pl_rsysinit
591
592     popd
593
594     # create the cramfs image
595     echo "* Creating cramfs image"
596     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
597     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
598     rm -rf $tmp
599     pop_cleanup
600 }
601
602 # Create ISO CRAMFS image
603 function build_iso_cramfs()
604 {
605     local iso="$1" ; shift
606     local console="$1" ; shift
607     local custom="$1"
608     local serial=
609
610     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
611         serial=1
612         console_dev=$(extract_console_dev $console)
613         console_baud=$(extract_console_baud $console)
614         console_parity=$(extract_console_parity $console)
615         console_bits=$(extract_console_bits $console)
616     fi
617     prepare_cramfs "$console" "$custom"
618     echo "* Creating ISO CRAMFS-based image"
619
620     local tmp="${BUILDTMP}/cramfs-iso"
621     mkdir -p "$tmp"
622     push_cleanup rm -rf $tmp
623     (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
624     cat >$tmp/isolinux.cfg <<EOF
625 ${serial:+SERIAL 0 $console_baud}
626 DEFAULT kernel
627 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
628 DISPLAY pl_version
629 PROMPT 0
630 TIMEOUT 40
631 EOF
632
633     # write kargs.txt with additional args that should be executed by kexec to production mode
634     cat >$tmp/kargs.txt <<EOF
635 ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
636 EOF
637
638     cp ${BUILDTMP}/cramfs.img $tmp
639     mkisofs -o "$iso" \
640         $MKISOFS_OPTS \
641         $tmp
642
643     rm -fr "$tmp"
644     pop_cleanup
645 }
646
647 # Create USB CRAMFS based image
648 function build_usb_cramfs()
649 {
650     local usb="$1" ; shift
651     local console="$1" ; shift
652     local custom="$1"
653     local serial=
654
655     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
656         serial=1
657         console_dev=$(extract_console_dev $console)
658         console_baud=$(extract_console_baud $console)
659         console_parity=$(extract_console_parity $console)
660         console_bits=$(extract_console_bits $console)
661     fi
662     prepare_cramfs "$console" "$custom"
663     echo "* Creating USB CRAMFS based image"
664
665     let vfat_size=${cramfs_size}+$FREE_SPACE
666
667     # Make VFAT filesystem for USB
668     mkfs.vfat -C "$usb" $vfat_size
669
670     # write kargs.txt with additional args that should be executed by kexec to production mode
671     tmp="${BUILDTMP}/kargs.txt"
672     cat >$tmp <<EOF
673 ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
674 EOF
675     mcopy -i "$usb" "$tmp" ::/kargs.txt
676     rm -f "$tmp"
677
678     # Populate it
679     echo "* Populating USB with overlay images and cramfs"
680     mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
681     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
682
683     # Use syslinux instead of isolinux to make the image bootable
684     tmp="${BUILDTMP}/syslinux.cfg"
685     cat >$tmp <<EOF
686 ${serial:+SERIAL 0 $console_baud}
687 DEFAULT kernel
688 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
689 DISPLAY pl_version
690 PROMPT 0
691 TIMEOUT 40
692 EOF
693
694     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
695     rm -f "$tmp"
696
697     echo "* Making USB CRAMFS based image bootable"
698     syslinux "$usb"
699 }
700
701 function type_to_name()
702 {
703     echo $1 | sed '
704         s/usb$/.usb/;
705         s/usb_partition$/-partition.usb/;
706         s/iso$/.iso/;
707         s/usb_cramfs$/-cramfs.usb/;
708         s/iso_cramfs$/-cramfs.iso/;
709         '
710 }
711
712 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
713
714 for t in $TYPES; do
715     arg=$t
716     console=$CONSOLE_INFO
717
718     # figure if this is a serial image (can be specified in the type or with -s)
719     if  [[ "$t" == *serial* || "$console" != "$GRAPHIC_CONSOLE" ]]; then
720         # remove serial from type
721         t=`echo $t | sed 's/_serial//'`
722         # check console
723         [ "$console" == "$GRAPHIC_CONSOLE" ] && console="$SERIAL_CONSOLE"
724         # compute filename part
725         if [ "$console" == "$SERIAL_CONSOLE" ] ; then
726             serial="-serial"
727         else
728             serial="-serial-$(echo $console | sed -e 's,:,,g')"
729         fi
730     else
731         serial=""
732     fi
733     
734     tname=`type_to_name $t`
735     # if -o is specified (as it has no default)
736     if [ -n "$OUTPUT_NAME" ] ; then
737         output=$OUTPUT_NAME
738     else
739         output="${OUTPUT_BASE}${serial}${tname}"
740     fi
741
742     echo "*** Dealing with type=$arg"
743     echo '*' build_$t "$output" "$console" "$CUSTOM_DIR"
744     if [ ! -n "$DRY_RUN" ] ; then
745         build_$t "$output" "$console" "$CUSTOM_DIR" 
746     fi
747 done
748
749 exit 0