892b6ee8b7c5f5a5731ddf94dc499c1ebbbaa332
[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 TYPES="usb iso"
21 ALL_TYPES="usb iso usb_cramfs iso_cramfs"
22 # Leave 4 MB of free space
23 FREE_SPACE=4096
24 CUSTOM_DIR=
25 OUTPUT_BASE=
26 DEFAULT_CONSOLE="graphic"
27 CONSOLE_INFO=$DEFAULT_CONSOLE
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 usage()
31 {
32     echo "Usage: build.sh [OPTION]..."
33     echo "    -c name          (Deprecated) Static configuration to use (default: $CONFIGURATION)"
34     echo "    -f planet.cnf    Node to customize CD for (default: none)"
35     echo "    -t 'types'       Build the specified images (default: $TYPES)"
36     echo "                     All known types: $ALL_TYPES"
37     echo "    -s console-info  Enable a serial line as console and also bring up getty on that line"
38     echo "                     console-info=devicename:baudrate"
39     echo "    -a               Build all supported images"
40     echo "    -C custom-dir    Custom directory"
41     echo "    -O output-base   The basename of the generated files (default: PLC_NAME-BootCD-VERSION)"
42     echo "    -h               This message"
43     exit 1
44 }
45
46 # Get options
47 while getopts "O:c:f:s:t:C:ah" opt ; do
48     case $opt in
49     c)
50         CONFIGURATION=$OPTARG
51         ;;
52     f)
53         NODE_CONFIGURATION_FILE=$OPTARG
54         ;;
55     t)
56         TYPES="$OPTARG"
57         ;;
58     C)
59         CUSTOM_DIR="$OPTARG"
60         ;;
61     O)
62         OUTPUT_BASE="$OPTARG"
63         ;;
64     a)
65         TYPES="$ALL_TYPES"
66         ;;
67     s)
68         CONSOLE_INFO="$OPTARG"
69         ;;
70     h|*)
71         usage
72         ;;
73     esac
74 done
75
76 # Do not tolerate errors
77 set -e
78
79 # Change to our source directory
80 srcdir=$(cd $(dirname $0) && pwd -P)
81 pushd $srcdir
82
83 # Root of the isofs
84 isofs=$PWD/build/isofs
85
86 # The reference image is expected to have been built by prep.sh (see .spec)
87 # we disable the initial logic that called prep.sh if that was not the case
88 # this is because prep.sh needs to know pldistro 
89 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
90     echo "You have to run prep.sh prior to calling $0 - exiting"
91     exit 1
92 fi
93
94 # build/version.txt written by prep.sh
95 BOOTCD_VERSION=$(cat build/version.txt)
96
97 if [ -f /etc/planetlab/plc_config ] ; then
98     # Source PLC configuration
99     . /etc/planetlab/plc_config
100 fi
101
102 ### This support for backwards compatibility can be taken out in the
103 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
104 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
105 ### set.
106 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
107     PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
108 fi
109
110 # If PLC configuration is not valid, try a static configuration
111 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
112     # (Deprecated) Source static configuration
113     . configurations/$CONFIGURATION/configuration
114     PLC_NAME="PlanetLab"
115     PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
116     PLC_WWW_HOST="www.planet-lab.org"
117     PLC_WWW_PORT=80
118     if [ -n "$EXTRA_VERSION" ] ; then
119     BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
120     fi
121     PLC_BOOT_HOST=$PRIMARY_SERVER
122     PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
123     PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
124     PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
125 fi
126
127 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
128
129 echo "* Building images for $FULL_VERSION_STRING"
130
131 # From within a myplc chroot /usr/tmp is too small 
132 # to build all possible images, whereas /data is part of the host
133 # filesystem and usually has sufficient space.  What we
134 # should do is check whether the expected amount of space
135 # is available.
136 BUILDTMP=/usr/tmp
137 if [ -d /data/tmp ] ; then
138     isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
139     if [ -n "$isreadonly" ] ; then
140         rm -f "$isreadonly"
141         BUILDTMP=/data/tmp
142     fi
143 fi
144
145 declare -a _CLEANUPS=()
146 function do_cleanup()
147 {
148     cd /
149     for i in "${_CLEANUPS[@]}"; do
150         $i
151     done
152 }
153 function push_cleanup()
154 {
155     _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
156 }
157 function pop_cleanup()
158 {
159     unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
160 }
161
162 trap "do_cleanup" ERR INT EXIT
163
164 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
165 push_cleanup rm -fr "${BUILDTMP}"
166 mkdir "${BUILDTMP}/isofs"
167 for i in "$isofs"/{bootcd.img,kernel}; do
168     ln -s "$i" "${BUILDTMP}/isofs"
169 done
170 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
171 isofs="${BUILDTMP}/isofs"
172
173 # Root of the ISO and USB images
174 echo "* Populating root filesystem..."
175 overlay="${BUILDTMP}/overlay"
176 install -d -m 755 $overlay
177 push_cleanup rm -fr $overlay
178
179 # Create version files
180 echo "* Creating version files"
181
182 # Boot Manager compares pl_version in both places to make sure that
183 # the right CD is mounted. We used to boot from an initrd and mount
184 # the CD on /usr. Now we just run everything out of the initrd.
185 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
186     mkdir -p $(dirname $file)
187     echo "$FULL_VERSION_STRING" >$file
188 done
189
190 # Install boot server configuration files
191 echo "* Installing boot server configuration files"
192
193 # We always intended to bring up and support backup boot servers,
194 # but never got around to it. Just install the same parameters for
195 # both for now.
196 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
197     install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
198     install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
199     echo "$PLC_BOOT_HOST" >$dir/boot_server
200     echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
201     echo "/boot/" >$dir/boot_server_path
202 done
203
204 # (Deprecated) Install old-style boot server configuration files
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" != "$DEFAULT_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 115200}
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" != "$DEFAULT_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" != "$DEFAULT_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" != "$DEFAULT_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 [ $serial -eq 1 ] ; 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" != "$DEFAULT_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" != "$DEFAULT_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/usb_serial$/-serial.usb/;
680         s/iso$/.iso/;
681         s/iso_serial$/-serial.iso/;
682         s/usb_cramfs$/-cramfs.usb/;
683         s/usb_cramfs_serial$/-cramfs-serial.usb/;
684         s/iso_cramfs$/-cramfs.iso/;
685         s/iso_cramfs_serial$/-cramfs-serial.iso/;
686         '
687 }
688
689 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
690
691 for t in $TYPES; do
692     CONSOLE=$CONSOLE_INFO
693     tname=`type_to_name $t`
694     if [[ "$t" == *_serial ]]; then
695         if [ "$CONSOLE_INFO" == "$DEFAULT_CONSOLE" ] ; then
696             CONSOLE="ttyS0:115200"
697         fi
698         t=`echo $t | sed 's/_serial$//'`
699     fi
700     
701     OUTPUTNAME="${OUTPUT_BASE}${tname}"
702     if [ "$CONSOLE" != "$DEFAULT_CONSOLE" ] ; then
703         CONSOLE_NAME=$(echo $CONSOLE | sed 's,\:,,g')
704         OUTPUTNAME="${OUTPUT_BASE}-serial-${CONSOLE_NAME}${tname}"
705     fi
706     build_$t "$OUTPUTNAME" "$CONSOLE" "$CUSTOM_DIR"
707 done
708
709 exit 0