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