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