removed all legacy code wrt alternative configurations (-c option),
[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 # Generate /etc/issue
203 echo "* Generating /etc/issue"
204
205 if [ "$PLC_WWW_PORT" = "443" ] ; then
206     PLC_WWW_URL="https://$PLC_WWW_HOST/"
207 elif [ "$PLC_WWW_PORT" != "80" ] ; then
208     PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
209 else
210     PLC_WWW_URL="http://$PLC_WWW_HOST/"
211 fi
212
213 mkdir -p $overlay/etc
214 cat >$overlay/etc/issue <<EOF
215 $FULL_VERSION_STRING
216 $PLC_NAME Node: \n
217 Kernel \r on an \m
218 $PLC_WWW_URL
219
220 This machine is a node in the $PLC_NAME distributed network.  It has
221 not fully booted yet. If you have cancelled the boot process at the
222 request of $PLC_NAME Support, please follow the instructions provided
223 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
224
225 Console login at this point is restricted to root. Provide the root
226 password of the default $PLC_NAME Central administrator account at the
227 time that this CD was created.
228
229 EOF
230
231 # Set root password
232 echo "* Setting root password"
233
234 if [ -z "$ROOT_PASSWORD" ] ; then
235     # Generate an encrypted password with crypt() if not defined
236     # in a static configuration.
237     ROOT_PASSWORD=$(python <<EOF
238 import crypt, random, string
239 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
240 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
241 EOF
242 )
243 fi
244
245 # build/passwd copied out by prep.sh
246 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
247     >$overlay/etc/passwd
248
249 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
250 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
251     echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
252     install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
253 fi
254
255 # Pack overlay files into a compressed archive
256 echo "* Compressing overlay image"
257 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
258
259 rm -rf $overlay
260 pop_cleanup
261
262 if [ -n "$CUSTOM_DIR" ]; then
263     echo "* Compressing custom image"
264     (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
265 fi
266
267 # Calculate ramdisk size (total uncompressed size of both archives)
268 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
269 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
270
271 echo "$FULL_VERSION_STRING" >$isofs/pl_version
272
273 popd
274
275 function extract_console_dev()
276 {
277     local console="$1"
278     dev=$(echo $console| awk -F: ' {print $1}')
279     echo $dev
280 }
281
282 function extract_console_baud()
283 {
284     local console="$1"
285     baud=$(echo $console| awk -F: ' {print $2}')
286     [ -z "$baud" ] && baud="115200"
287     echo $baud
288 }
289
290 function extract_console_parity()
291 {
292     local console="$1"
293     parity=$(echo $console| awk -F: ' {print $3}')
294     [ -z "$parity" ] && parity="n"
295     echo $parity
296 }
297
298 function extract_console_bits()
299 {
300     local console="$1"
301     bits=$(echo $console| awk -F: ' {print $4}')
302     [ -z "$bits" ] && bits="8"
303     echo $bits
304 }
305
306 function build_iso()
307 {
308     local iso="$1" ; shift
309     local console="$1" ; shift
310     local custom="$1"
311     local serial=
312
313     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
314         serial=1
315         console_dev=$(extract_console_dev $console)
316         console_baud=$(extract_console_baud $console)
317         console_parity=$(extract_console_parity $console)
318         console_bits=$(extract_console_bits $console)
319     fi
320
321     # Write isolinux configuration
322     cat >$isofs/isolinux.cfg <<EOF
323 ${serial:+SERIAL 0 ${console_baud}}
324 DEFAULT kernel
325 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}}
326 DISPLAY pl_version
327 PROMPT 0
328 TIMEOUT 40
329 EOF
330
331     # Create ISO image
332     echo "* Creating ISO image"
333     mkisofs -o "$iso" \
334         $MKISOFS_OPTS \
335         $isofs
336 }
337
338 function build_usb_partition()
339 {
340     echo -n "* Creating USB image with partitions..."
341     local usb="$1" ; shift
342     local console="$1" ; shift
343     local custom="$1"
344     local serial=
345
346     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
347         serial=1
348         console_dev=$(extract_console_dev $console)
349         console_baud=$(extract_console_baud $console)
350         console_parity=$(extract_console_parity $console)
351         console_bits=$(extract_console_bits $console)
352     fi
353
354     local size=$(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
355     size=$(( $size / 1024 ))
356
357     local heads=64
358     local sectors=32
359     local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
360     local offset=$(( $sectors*512 ))
361
362     /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
363     
364     cat >${BUILDTMP}/mtools.conf<<EOF
365 drive z:
366 file="${usb}"
367 cylinders=$cylinders
368 heads=$heads
369 sectors=$sectors
370 offset=$offset
371 mformat_only
372 EOF
373     # environment variable for mtools
374     export MTOOLSRC="${BUILDTMP}/mtools.conf"
375
376     ### COPIED FROM build_usb() below!!!!
377     echo -n " populating USB image... "
378     mcopy -bsQ -i "$usb" "$isofs"/* z:/
379         
380     # Use syslinux instead of isolinux to make the image bootable
381     tmp="${BUILDTMP}/syslinux.cfg"
382     cat >$tmp <<EOF
383 ${serial:+SERIAL 0 ${console_baud}}
384 DEFAULT kernel
385 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}}
386 DISPLAY pl_version
387 PROMPT 0
388 TIMEOUT 40
389 EOF
390     mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
391     mcopy -i "$usb" "$tmp" z:/syslinux.cfg
392     rm -f "$tmp"
393     rm -f "${BUILDTMP}/mtools.conf"
394     unset MTOOLSRC
395
396     echo "making USB image bootable."
397     syslinux -o $offset "$usb"
398
399 }
400
401 # Create USB image
402 function build_usb()
403 {
404     echo -n "* Creating USB image... "
405     local usb="$1" ; shift
406     local console="$1" ; shift
407     local custom="$1"
408     local serial=
409
410     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
411         serial=1
412         console_dev=$(extract_console_dev $console)
413         console_baud=$(extract_console_baud $console)
414         console_parity=$(extract_console_parity $console)
415         console_bits=$(extract_console_bits $console)
416     fi
417
418     mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
419
420     # Populate it
421     echo -n " populating USB image... "
422     mcopy -bsQ -i "$usb" "$isofs"/* ::/
423
424     # Use syslinux instead of isolinux to make the image bootable
425     tmp="${BUILDTMP}/syslinux.cfg"
426     cat >$tmp <<EOF
427 ${serial:+SERIAL 0 $console_baud}
428 DEFAULT kernel
429 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}}
430 DISPLAY pl_version
431 PROMPT 0
432 TIMEOUT 40
433 EOF
434     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
435     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
436     rm -f "$tmp"
437
438     echo "making USB image bootable."
439     syslinux "$usb"
440 }
441
442
443 # Setup CRAMFS related support
444 function prepare_cramfs()
445 {
446     [ -n "$CRAMFS_PREPARED" ] && return 0
447     local console=$1; shift
448     local custom=$1; 
449     local serial=
450     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
451         serial=1
452         console_dev=$(extract_console_dev $console)
453         console_baud=$(extract_console_baud $console)
454     fi
455
456     echo "* Setting up CRAMFS-based images"
457     local tmp="${BUILDTMP}/cramfs-tree"
458     mkdir -p "$tmp"
459     push_cleanup rm -rf $tmp
460     pushd $tmp
461     gzip -d -c $isofs/bootcd.img     | cpio -diu
462     gzip -d -c $isofs/overlay.img    | cpio -diu
463     [ -n "$custom" ] && \
464         gzip -d -c $isofs/custom.img | cpio -diu
465
466     # clean out unnecessary rpm lib
467     echo "* clearing var/lib/rpm/*"
468     rm -f var/lib/rpm/*
469
470     # bootcd requires this directory
471     mkdir -p mnt/confdevice
472
473     # relocate various directory to /tmp
474     rm -rf root
475     ln -fs /tmp/root root
476     ln -fs /sbin/init linuxrc 
477     ln -fs /tmp/resolv.conf etc/resolv.conf
478     ln -fs /tmp/etc/mtab etc/mtab
479
480     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
481     # make /tmp/etc
482     echo "* renaming dirs in ./etc"
483     pushd etc
484     for dir in `find * -type d -prune | grep -v rc.d`; do
485         mv ${dir} ${dir}_o
486         ln -fs /tmp/etc/${dir} ${dir}
487     done
488     popd
489
490     echo "* renaming dirs in ./var"
491     # rename all top-level directories and put in a symlink to /tmp/var
492     pushd var
493     for dir in `find * -type d -prune`; do
494         mv ${dir} ${dir}_o
495         ln -fs /tmp/var/${dir} ${dir}
496     done
497     popd
498
499     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
500     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
501     rm -f ./etc/fstab
502     cat >./etc/fstab <<EOF
503 /dev/ram0     /              cramfs     ro              0 0
504 none          /dev/pts       devpts     gid=5,mode=620  0 0
505 none          /proc          proc       defaults        0 0
506 none          /sys           sysfs      defaults        0 0
507 EOF
508
509     pushd dev
510     rm -f console
511     mknod console c 5 1
512     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
513     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
514     #ln -fs ram1 ram
515     #ln -fs ram0 ramdisk
516     popd
517
518     # update etc/inittab to start with pl_rsysinit
519     sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
520
521     # modify inittab to have a serial console
522     if [ -n "$serial" ] ; then
523         echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
524         # and let root log in
525         echo "$console_dev" >> etc/securetty
526     fi
527
528     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
529     etcsize=$(du -s ./etc | awk '{ print $1 }')
530     varsize=$(du -s ./var | awk '{ print $1 }')
531     let msize=($varsize+$etcsize+8192)/1024
532
533     # make dhclient happy
534     for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
535     ln -fs /tmp/etc/resolv.conf etc/resolv.conf
536     ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
537
538     # generate pl_rsysinit
539     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
540 #!/bin/sh
541 # generated by build.sh
542 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
543 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
544 mkdir -p /tmp/root
545 mkdir -p /tmp/etc
546 touch /tmp/etc/resolv.conf
547 touch /tmp/etc/mtab
548 mkdir -p /tmp/var
549
550 # make mtab happy
551 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
552
553 # copy over directory contents of all _o directories from /etc and /var
554 # /tmp/etc and /tmp/var
555 pushd /etc
556 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
557 popd
558 pushd /var
559 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
560 popd
561
562 echo "done"
563
564 # hand over to pl_sysinit
565 echo "pl_rsysinit: handing over to pl_sysinit"
566 /etc/init.d/pl_sysinit
567 EOF
568     chmod +x etc/rc.d/init.d/pl_rsysinit
569
570     popd
571
572     # create the cramfs image
573     echo "* Creating cramfs image"
574     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
575     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
576     rm -rf $tmp
577     pop_cleanup
578 }
579
580 # Create ISO CRAMFS image
581 function build_iso_cramfs()
582 {
583     local iso="$1" ; shift
584     local console="$1" ; shift
585     local custom="$1"
586     local serial=
587
588     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
589         serial=1
590         console_dev=$(extract_console_dev $console)
591         console_baud=$(extract_console_baud $console)
592         console_parity=$(extract_console_parity $console)
593         console_bits=$(extract_console_bits $console)
594     fi
595     prepare_cramfs "$console" "$custom"
596     echo "* Creating ISO CRAMFS-based image"
597
598     local tmp="${BUILDTMP}/cramfs-iso"
599     mkdir -p "$tmp"
600     push_cleanup rm -rf $tmp
601     (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
602     cat >$tmp/isolinux.cfg <<EOF
603 ${serial:+SERIAL 0 $console_baud}
604 DEFAULT kernel
605 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
606 DISPLAY pl_version
607 PROMPT 0
608 TIMEOUT 40
609 EOF
610
611     cp ${BUILDTMP}/cramfs.img $tmp
612     mkisofs -o "$iso" \
613         $MKISOFS_OPTS \
614         $tmp
615
616     rm -fr "$tmp"
617     pop_cleanup
618 }
619
620 # Create USB CRAMFS based image
621 function build_usb_cramfs()
622 {
623     local usb="$1" ; shift
624     local console="$1" ; shift
625     local custom="$1"
626     local serial=
627
628     if [ "$console" != "$GRAPHIC_CONSOLE" ] ; then
629         serial=1
630         console_dev=$(extract_console_dev $console)
631         console_baud=$(extract_console_baud $console)
632         console_parity=$(extract_console_parity $console)
633         console_bits=$(extract_console_bits $console)
634     fi
635     prepare_cramfs "$console" "$custom"
636     echo "* Creating USB CRAMFS based image"
637
638     let vfat_size=${cramfs_size}+$FREE_SPACE
639
640     # Make VFAT filesystem for USB
641     mkfs.vfat -C "$usb" $vfat_size
642
643     # Populate it
644     echo "* Populating USB with overlay images and cramfs"
645     mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
646     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
647
648     # Use syslinux instead of isolinux to make the image bootable
649     tmp="${BUILDTMP}/syslinux.cfg"
650     cat >$tmp <<EOF
651 ${serial:+SERIAL 0 $console_baud}
652 DEFAULT kernel
653 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
654 DISPLAY pl_version
655 PROMPT 0
656 TIMEOUT 40
657 EOF
658     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
659     rm -f "$tmp"
660
661     echo "* Making USB CRAMFS based image bootable"
662     syslinux "$usb"
663 }
664
665 function type_to_name()
666 {
667     echo $1 | sed '
668         s/usb$/.usb/;
669         s/usb_partition$/-partition.usb/;
670         s/iso$/.iso/;
671         s/usb_cramfs$/-cramfs.usb/;
672         s/iso_cramfs$/-cramfs.iso/;
673         '
674 }
675
676 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
677
678 for t in $TYPES; do
679     arg=$t
680     console=$CONSOLE_INFO
681
682     # figure if this is a serial image (can be specified in the type or with -s)
683     if  [[ "$t" == *serial* || "$console" != "$GRAPHIC_CONSOLE" ]]; then
684         # remove serial from type
685         t=`echo $t | sed 's/_serial//'`
686         # check console
687         [ "$console" == "$GRAPHIC_CONSOLE" ] && console="$SERIAL_CONSOLE"
688         # compute filename part
689         if [ "$console" == "$SERIAL_CONSOLE" ] ; then
690             serial="-serial"
691         else
692             serial="-serial-$(echo $console | sed -e 's,:,,g')"
693         fi
694     else
695         serial=""
696     fi
697     
698     tname=`type_to_name $t`
699     # if -o is specified (as it has no default)
700     if [ -n "$OUTPUT_NAME" ] ; then
701         output=$OUTPUT_NAME
702     else
703         output="${OUTPUT_BASE}${serial}${tname}"
704     fi
705
706     echo "*** Dealing with type=$arg"
707     echo '*' build_$t "$output" "$console" "$CUSTOM_DIR"
708     if [ ! -n "$DRY_RUN" ] ; then
709         build_$t "$output" "$console" "$CUSTOM_DIR" 
710     fi
711 done
712
713 exit 0