- reviewed usage, for more relevance (lists all actually supported types)
[bootcd.git] / build.sh
1 #!/bin/bash
2 #
3 # Builds custom BootCD ISO and USB images in the current
4 # directory. For backward compatibility, if an old-style static
5 # configuration is specified, that configuration file will be parsed
6 # instead of the current PLC configuration in
7 # /etc/planetlab/plc_config.
8 #
9 # Aaron Klingaman <alk@absarokasoft.com>
10 # Mark Huang <mlhuang@cs.princeton.edu>
11 # Copyright (C) 2004-2007 The Trustees of Princeton University
12 #
13 # $Id$
14 #
15
16 PATH=/sbin:/bin:/usr/sbin:/usr/bin
17
18 CONFIGURATION=default
19 NODE_CONFIGURATION_FILE=
20 DEFAULT_TYPES="usb iso"
21 # Leave 4 MB of free space
22 FREE_SPACE=4096
23 CUSTOM_DIR=
24 OUTPUT_BASE=
25 DEFAULT_CONSOLE="graphic"
26 CONSOLE_INFO=$DEFAULT_CONSOLE
27 DEFAULT_SERIAL_CONSOLE="ttyS0:115200"
28 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
29
30 ALL_TYPES=""
31 for x in iso usb usb_partition; do for s in "" "_serial" ; do for c in "" "_cramfs" ; do
32   t="${x}${c}${s}"
33   case $t in
34       usb_partition_cramfs|usb_partition_cramfs_serial)
35           # unsupported
36           ;;
37       *)
38           ALL_TYPES="$ALL_TYPES $t" ;;
39   esac
40 done; done; done
41
42 usage()
43 {
44     echo "Usage: build.sh [OPTION]..."
45     echo "    -c name          (Deprecated) Static configuration to use (default: $CONFIGURATION)"
46     echo "    -f plnode.txt    Node to customize CD for (default: none)"
47     echo "    -t 'types'       Build the specified images (default: $DEFAULT_TYPES)"
48     echo "    -a               Build all known types as listed below"
49     echo "    -s console-info  Enable a serial line as console and also bring up getty on that line"
50     echo "                     defaults to $DEFAULT_SERIAL_CONSOLE"
51     echo "    -O output-base   The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
52     echo "    -C custom-dir    Custom directory"
53     echo "                     can be a full path"
54     echo "    -n               Dry run - mostly for debug/test purposes"
55     echo "    -h               This message"
56     echo "All known types: $ALL_TYPES"
57     exit 1
58 }
59
60 # init
61 TYPES=""
62 # Get options
63 while getopts "c:f:t:as:O:C:nh" opt ; do
64     case $opt in
65     c)
66         CONFIGURATION=$OPTARG ;;
67     f)
68         NODE_CONFIGURATION_FILE=$OPTARG ;;
69     t)
70         TYPES="$TYPES $OPTARG" ;;
71     a)
72         TYPES="$ALL_TYPES" ;;
73     s)
74         CONSOLE_INFO="$OPTARG" ;;
75     O)
76         OUTPUT_BASE="$OPTARG" ;;
77     C)
78         CUSTOM_DIR="$OPTARG" ;;
79     n)
80         DRY_RUN=true ;;
81     h|*)
82         usage ;;
83     esac
84 done
85
86 # use default if not set
87 [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
88
89 # Do not tolerate errors
90 set -e
91
92 # Change to our source directory
93 srcdir=$(cd $(dirname $0) && pwd -P)
94 pushd $srcdir
95
96 # Root of the isofs
97 isofs=$PWD/build/isofs
98
99 # The reference image is expected to have been built by prep.sh (see .spec)
100 # we disable the initial logic that called prep.sh if that was not the case
101 # this is because prep.sh needs to know pldistro 
102 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
103     echo "You have to run prep.sh prior to calling $0 - exiting"
104     exit 1
105 fi
106
107 # build/version.txt written by prep.sh
108 BOOTCD_VERSION=$(cat build/version.txt)
109
110 if [ -f /etc/planetlab/plc_config ] ; then
111     # Source PLC configuration
112     . /etc/planetlab/plc_config
113 fi
114
115 ### This support for backwards compatibility can be taken out in the
116 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
117 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
118 ### set.
119 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
120     PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
121 fi
122
123 # If PLC configuration is not valid, try a static configuration
124 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
125     # (Deprecated) Source static configuration
126     . configurations/$CONFIGURATION/configuration
127     PLC_NAME="PlanetLab"
128     PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
129     PLC_WWW_HOST="www.planet-lab.org"
130     PLC_WWW_PORT=80
131     if [ -n "$EXTRA_VERSION" ] ; then
132     BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
133     fi
134     PLC_BOOT_HOST=$PRIMARY_SERVER
135     PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
136     PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
137     PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
138 fi
139
140 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
141
142 echo "* Building images for $FULL_VERSION_STRING"
143
144 # From within a myplc chroot /usr/tmp is too small 
145 # to build all possible images, whereas /data is part of the host
146 # filesystem and usually has sufficient space.  What we
147 # should do is check whether the expected amount of space
148 # is available.
149 BUILDTMP=/usr/tmp
150 if [ -d /data/tmp ] ; then
151     isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
152     if [ -n "$isreadonly" ] ; then
153         rm -f "$isreadonly"
154         BUILDTMP=/data/tmp
155     fi
156 fi
157
158 declare -a _CLEANUPS=()
159 function do_cleanup()
160 {
161     cd /
162     for i in "${_CLEANUPS[@]}"; do
163         $i
164     done
165 }
166 function push_cleanup()
167 {
168     _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
169 }
170 function pop_cleanup()
171 {
172     unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
173 }
174
175 trap "do_cleanup" ERR INT EXIT
176
177 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
178 push_cleanup rm -fr "${BUILDTMP}"
179 mkdir "${BUILDTMP}/isofs"
180 for i in "$isofs"/{bootcd.img,kernel}; do
181     ln -s "$i" "${BUILDTMP}/isofs"
182 done
183 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
184 isofs="${BUILDTMP}/isofs"
185
186 # Root of the ISO and USB images
187 echo "* Populating root filesystem..."
188 overlay="${BUILDTMP}/overlay"
189 install -d -m 755 $overlay
190 push_cleanup rm -fr $overlay
191
192 # Create version files
193 echo "* Creating version files"
194
195 # Boot Manager compares pl_version in both places to make sure that
196 # the right CD is mounted. We used to boot from an initrd and mount
197 # the CD on /usr. Now we just run everything out of the initrd.
198 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
199     mkdir -p $(dirname $file)
200     echo "$FULL_VERSION_STRING" >$file
201 done
202
203 # Install boot server configuration files
204 echo "* Installing boot server configuration files"
205
206 # We always intended to bring up and support backup boot servers,
207 # but never got around to it. Just install the same parameters for
208 # both for now.
209 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
210     install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
211     install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
212     echo "$PLC_BOOT_HOST" >$dir/boot_server
213     echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
214     echo "/boot/" >$dir/boot_server_path
215 done
216
217 # (Deprecated) Install old-style boot server configuration files
218 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
219 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
220 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
221 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
222 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
223
224 # Generate /etc/issue
225 echo "* Generating /etc/issue"
226
227 if [ "$PLC_WWW_PORT" = "443" ] ; then
228     PLC_WWW_URL="https://$PLC_WWW_HOST/"
229 elif [ "$PLC_WWW_PORT" != "80" ] ; then
230     PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
231 else
232     PLC_WWW_URL="http://$PLC_WWW_HOST/"
233 fi
234
235 mkdir -p $overlay/etc
236 cat >$overlay/etc/issue <<EOF
237 $FULL_VERSION_STRING
238 $PLC_NAME Node: \n
239 Kernel \r on an \m
240 $PLC_WWW_URL
241
242 This machine is a node in the $PLC_NAME distributed network.  It has
243 not fully booted yet. If you have cancelled the boot process at the
244 request of $PLC_NAME Support, please follow the instructions provided
245 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
246
247 Console login at this point is restricted to root. Provide the root
248 password of the default $PLC_NAME Central administrator account at the
249 time that this CD was created.
250
251 EOF
252
253 # Set root password
254 echo "* Setting root password"
255
256 if [ -z "$ROOT_PASSWORD" ] ; then
257     # Generate an encrypted password with crypt() if not defined
258     # in a static configuration.
259     ROOT_PASSWORD=$(python <<EOF
260 import crypt, random, string
261 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
262 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
263 EOF
264 )
265 fi
266
267 # build/passwd copied out by prep.sh
268 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
269     >$overlay/etc/passwd
270
271 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
272 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
273     echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
274     install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
275 fi
276
277 # Pack overlay files into a compressed archive
278 echo "* Compressing overlay image"
279 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
280
281 rm -rf $overlay
282 pop_cleanup
283
284 if [ -n "$CUSTOM_DIR" ]; then
285     echo "* Compressing custom image"
286     (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
287 fi
288
289 # Calculate ramdisk size (total uncompressed size of both archives)
290 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
291 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
292
293 echo "$FULL_VERSION_STRING" >$isofs/pl_version
294
295 popd
296
297 function extract_console_dev()
298 {
299     local console="$1"
300     dev=$(echo $console| awk -F: ' {print $1}')
301     echo $dev
302 }
303
304 function extract_console_baud()
305 {
306     local console="$1"
307     baud=$(echo $console| awk -F: ' {print $2}')
308     [ -z "$baud" ] && baud="115200"
309     echo $baud
310 }
311
312 function extract_console_parity()
313 {
314     local console="$1"
315     parity=$(echo $console| awk -F: ' {print $3}')
316     [ -z "$parity" ] && parity="n"
317     echo $parity
318 }
319
320 function extract_console_bits()
321 {
322     local console="$1"
323     bits=$(echo $console| awk -F: ' {print $4}')
324     [ -z "$bits" ] && bits="8"
325     echo $bits
326 }
327
328 function build_iso()
329 {
330     local iso="$1" ; shift
331     local console="$1" ; shift
332     local custom="$1"
333     local serial=
334
335     if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
336         serial=1
337         console_dev=$(extract_console_dev $console)
338         console_baud=$(extract_console_baud $console)
339         console_parity=$(extract_console_parity $console)
340         console_bits=$(extract_console_bits $console)
341     fi
342
343     # Write isolinux configuration
344     cat >$isofs/isolinux.cfg <<EOF
345 ${serial:+SERIAL 0 ${console_baud}}
346 DEFAULT kernel
347 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}}
348 DISPLAY pl_version
349 PROMPT 0
350 TIMEOUT 40
351 EOF
352
353     # Create ISO image
354     echo "* Creating ISO image"
355     mkisofs -o "$iso" \
356         $MKISOFS_OPTS \
357         $isofs
358 }
359
360 function build_usb_partition()
361 {
362     echo -n "* Creating USB image with partitions..."
363     local usb="$1" ; shift
364     local console="$1" ; shift
365     local custom="$1"
366     local serial=
367
368     if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
369         serial=1
370         console_dev=$(extract_console_dev $console)
371         console_baud=$(extract_console_baud $console)
372         console_parity=$(extract_console_parity $console)
373         console_bits=$(extract_console_bits $console)
374     fi
375
376     local size=$(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
377     size=$(( $size / 1024 ))
378
379     local heads=64
380     local sectors=32
381     local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
382     local offset=$(( $sectors*512 ))
383
384     /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
385     
386     cat >${BUILDTMP}/mtools.conf<<EOF
387 drive z:
388 file="${usb}"
389 cylinders=$cylinders
390 heads=$heads
391 sectors=$sectors
392 offset=$offset
393 mformat_only
394 EOF
395     # environment variable for mtools
396     export MTOOLSRC="${BUILDTMP}/mtools.conf"
397
398     ### COPIED FROM build_usb() below!!!!
399     echo -n " populating USB image... "
400     mcopy -bsQ -i "$usb" "$isofs"/* z:/
401         
402     # Use syslinux instead of isolinux to make the image bootable
403     tmp="${BUILDTMP}/syslinux.cfg"
404     cat >$tmp <<EOF
405 ${serial:+SERIAL 0 ${console_baud}}
406 DEFAULT kernel
407 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}}
408 DISPLAY pl_version
409 PROMPT 0
410 TIMEOUT 40
411 EOF
412     mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
413     mcopy -i "$usb" "$tmp" z:/syslinux.cfg
414     rm -f "$tmp"
415     rm -f "${BUILDTMP}/mtools.conf"
416     unset MTOOLSRC
417
418     echo "making USB image bootable."
419     syslinux -o $offset "$usb"
420
421 }
422
423 # Create USB image
424 function build_usb()
425 {
426     echo -n "* Creating USB image... "
427     local usb="$1" ; shift
428     local console="$1" ; shift
429     local custom="$1"
430     local serial=
431
432     if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
433         serial=1
434         console_dev=$(extract_console_dev $console)
435         console_baud=$(extract_console_baud $console)
436         console_parity=$(extract_console_parity $console)
437         console_bits=$(extract_console_bits $console)
438     fi
439
440     mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
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" != "$DEFAULT_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" != "$DEFAULT_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     cp ${BUILDTMP}/cramfs.img $tmp
634     mkisofs -o "$iso" \
635         $MKISOFS_OPTS \
636         $tmp
637
638     rm -fr "$tmp"
639     pop_cleanup
640 }
641
642 # Create USB CRAMFS based image
643 function build_usb_cramfs()
644 {
645     local usb="$1" ; shift
646     local console="$1" ; shift
647     local custom="$1"
648     local serial=
649
650     if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
651         serial=1
652         console_dev=$(extract_console_dev $console)
653         console_baud=$(extract_console_baud $console)
654         console_parity=$(extract_console_parity $console)
655         console_bits=$(extract_console_bits $console)
656     fi
657     prepare_cramfs "$console" "$custom"
658     echo "* Creating USB CRAMFS based image"
659
660     let vfat_size=${cramfs_size}+$FREE_SPACE
661
662     # Make VFAT filesystem for USB
663     mkfs.vfat -C "$usb" $vfat_size
664
665     # Populate it
666     echo "* Populating USB with overlay images and cramfs"
667     mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
668     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
669
670     # Use syslinux instead of isolinux to make the image bootable
671     tmp="${BUILDTMP}/syslinux.cfg"
672     cat >$tmp <<EOF
673 ${serial:+SERIAL 0 $console_baud}
674 DEFAULT kernel
675 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
676 DISPLAY pl_version
677 PROMPT 0
678 TIMEOUT 40
679 EOF
680     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
681     rm -f "$tmp"
682
683     echo "* Making USB CRAMFS based image bootable"
684     syslinux "$usb"
685 }
686
687 function type_to_name()
688 {
689     echo $1 | sed '
690         s/usb$/.usb/;
691         s/usb_partition$/-partition.usb/;
692         s/usb_serial$/-serial.usb/;
693         s/iso$/.iso/;
694         s/iso_serial$/-serial.iso/;
695         s/usb_cramfs$/-cramfs.usb/;
696         s/usb_cramfs_serial$/-cramfs-serial.usb/;
697         s/iso_cramfs$/-cramfs.iso/;
698         s/iso_cramfs_serial$/-cramfs-serial.iso/;
699         '
700 }
701
702 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
703
704 for t in $TYPES; do
705     arg=$t
706     CONSOLE=$CONSOLE_INFO
707     tname=`type_to_name $t`
708     if [[ "$t" == *_serial ]]; then
709         if [ "$CONSOLE_INFO" == "$DEFAULT_CONSOLE" ] ; then
710             CONSOLE="$DEFAULT_SERIAL_CONSOLE"
711         fi
712         t=`echo $t | sed 's/_serial$//'`
713     fi
714     
715     OUTPUTNAME="${OUTPUT_BASE}${tname}"
716     if [ "$CONSOLE" != "$DEFAULT_CONSOLE" ] ; then
717         CONSOLE_NAME=$(echo $CONSOLE | sed 's,\:,,g')
718         OUTPUTNAME="${OUTPUT_BASE}-serial-${CONSOLE_NAME}${tname}"
719     fi
720     echo "*** Dealing with type=$arg"
721     echo "* " build_$t "$OUTPUTNAME" "$CONSOLE" "$CUSTOM_DIR"
722     if [ ! -n "$DRY_RUN" ] ; then
723         build_$t "$OUTPUTNAME" "$CONSOLE" "$CUSTOM_DIR" || true
724     fi
725 done
726
727 exit 0