trying out magic kernel option to avoid using em<x> ifnames
[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     # tmp: should be restricted to f15 nodes and above
336     # making sure the network interfaces are still numbered eth0 and above
337     KERNEL_ARGS="$KERNEL_ARGS biosdevname=0"
338
339     [ -n "$KERNEL_ARGS" ] && echo "$KERNEL_ARGS" > $OVERLAY/kargs.txt
340
341     # Pack overlay files into a compressed archive
342     echo "* Compressing overlay image"
343     (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
344
345     rm -rf $OVERLAY
346     pop_cleanup
347
348     if [ -n "$CUSTOM_DIR" ]; then
349         echo "* Compressing custom image"
350         (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
351     fi
352
353     # Calculate ramdisk size (total uncompressed size of both archives)
354     ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
355     ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
356
357     echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
358
359     popd
360 }
361
362 #################### plain ISO
363 function build_iso() {
364     local iso="$1" ; shift
365     local custom="$1"
366
367     # Write isolinux configuration
368     cat >$ISOFS/isolinux.cfg <<EOF
369 ${console_serial_line}
370 DEFAULT kernel
371 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
372 DISPLAY pl_version
373 PROMPT 0
374 TIMEOUT 40
375 EOF
376
377     # Create ISO image
378     echo "* Creating ISO image"
379     mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
380 }
381
382 #################### USB with partitions
383 function build_usb_partition() {
384     echo -n "* Creating USB image with partitions..."
385     local usb="$1" ; shift
386     local custom="$1"
387
388     local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
389     size=$(( $size / 1024 ))
390
391     local heads=64
392     local sectors=32
393     local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
394     local offset=$(( $sectors*512 ))
395
396     if [ -f  /usr/lib/syslinux/mkdiskimage ] ; then
397         /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
398     else
399         mkdiskimage -M -4 "$usb" $size $heads $sectors
400     fi
401
402     cat >${BUILDTMP}/mtools.conf<<EOF
403 drive z:
404 file="${usb}"
405 cylinders=$cylinders
406 heads=$heads
407 sectors=$sectors
408 offset=$offset
409 mformat_only
410 mtools_skip_check=1
411 EOF
412     # environment variable for mtools
413     export MTOOLSRC="${BUILDTMP}/mtools.conf"
414
415     ### COPIED FROM build_usb() below!!!!
416     echo -n " populating USB image... "
417     mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
418         
419     # Use syslinux instead of isolinux to make the image bootable
420     tmp="${BUILDTMP}/syslinux.cfg"
421     cat >$tmp <<EOF
422 ${console_serial_line}
423 DEFAULT kernel
424 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
425 DISPLAY pl_version
426 PROMPT 0
427 TIMEOUT 40
428 EOF
429     mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
430     mcopy -i "$usb" "$tmp" z:/syslinux.cfg
431     rm -f "$tmp"
432     rm -f "${MTOOLSRC}"
433     unset MTOOLSRC
434
435     echo "making USB image bootable."
436     syslinux -o $offset "$usb"
437
438 }
439
440 #################### plain USB
441 function build_usb() {
442     echo -n "* Creating USB image... "
443     local usb="$1" ; shift
444     local custom="$1"
445
446     rm -f "$usb"
447     mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
448
449     cat >${BUILDTMP}/mtools.conf<<EOF
450 mtools_skip_check=1
451 EOF
452     # environment variable for mtools
453     export MTOOLSRC="${BUILDTMP}/mtools.conf"
454
455     # Populate it
456     echo -n " populating USB image... "
457     mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
458
459     # Use syslinux instead of isolinux to make the image bootable
460     tmp="${BUILDTMP}/syslinux.cfg"
461     cat >$tmp <<EOF
462 ${console_serial_line}
463 DEFAULT kernel
464 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
465 DISPLAY pl_version
466 PROMPT 0
467 TIMEOUT 40
468 EOF
469     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
470     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
471     rm -f "$tmp"
472     rm -f "${MTOOLSRC}"
473     unset MTOOLSRC
474
475     echo "making USB image bootable."
476     syslinux "$usb"
477 }
478
479 #################### utility to setup CRAMFS related support
480 function prepare_cramfs() {
481     [ -n "$CRAMFS_PREPARED" ] && return 0
482     local custom=$1; 
483
484     echo "* Setting up CRAMFS-based images"
485     local tmp="${BUILDTMP}/cramfs-tree"
486     mkdir -p "$tmp"
487     push_cleanup rm -rf $tmp
488     pushd $tmp
489     gzip -d -c $ISOFS/bootcd.img     | cpio -diu
490     gzip -d -c $ISOFS/overlay.img    | cpio -diu
491     [ -n "$custom" ] && \
492         gzip -d -c $ISOFS/custom.img | cpio -diu
493
494     # clean out unnecessary rpm lib
495     echo "* clearing var/lib/rpm/*"
496     rm -f var/lib/rpm/*
497
498     # bootcd requires this directory
499     mkdir -p mnt/confdevice
500
501     # relocate various directory to /tmp
502     rm -rf root
503     ln -fs /tmp/root root
504     ln -fs /sbin/init linuxrc 
505     ln -fs /tmp/resolv.conf etc/resolv.conf
506     ln -fs /tmp/etc/mtab etc/mtab
507
508     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
509     # make /tmp/etc
510     echo "* renaming dirs in ./etc"
511     pushd etc
512     for dir in `find * -type d -prune | grep -v rc.d`; do
513         mv ${dir} ${dir}_o
514         ln -fs /tmp/etc/${dir} ${dir}
515     done
516     popd
517
518     echo "* renaming dirs in ./var"
519     # rename all top-level directories and put in a symlink to /tmp/var
520     pushd var
521     for dir in `find * -type d -prune`; do
522         mv ${dir} ${dir}_o
523         ln -fs /tmp/var/${dir} ${dir}
524     done
525     popd
526
527     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
528     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
529     rm -f ./etc/fstab
530     cat >./etc/fstab <<EOF
531 /dev/ram0     /              cramfs     ro              0 0
532 none          /dev/pts       devpts     gid=5,mode=620  0 0
533 none          /proc          proc       defaults        0 0
534 none          /sys           sysfs      defaults        0 0
535 EOF
536
537     pushd dev
538     rm -f console
539     mknod console c 5 1
540     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
541     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
542     #ln -fs ram1 ram
543     #ln -fs ram0 ramdisk
544     popd
545
546     # update etc/inittab to start with pl_rsysinit
547     for file in etc/inittab etc/event.d/rcS etc/init/rcS.conf; do
548         [ -f $file ] && sed -i 's,pl_sysinit,pl_rsysinit,' $file
549     done
550
551     # modify inittab to have a serial console
552     # xxx this might well be broken with f12 and above xxx
553     if [ -n "$serial" ] ; then
554         echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
555         # and let root log in
556         echo "$console_dev" >> etc/securetty
557     fi
558
559     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
560     etcsize=$(du -s ./etc | awk '{ print $1 }')
561     varsize=$(du -s ./var | awk '{ print $1 }')
562     let msize=($varsize+$etcsize+8192)/1024
563
564     # make dhclient happy
565     for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
566     ln -fs /tmp/etc/resolv.conf etc/resolv.conf
567     ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
568
569     # generate pl_rsysinit
570     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
571 #!/bin/sh
572 # generated by $COMMAND
573 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
574 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
575 mkdir -p /tmp/root
576 mkdir -p /tmp/etc
577 touch /tmp/etc/resolv.conf
578 touch /tmp/etc/mtab
579 mkdir -p /tmp/var
580
581 # make mtab happy
582 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
583
584 # copy over directory contents of all _o directories from /etc and /var
585 # /tmp/etc and /tmp/var
586 pushd /etc
587 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
588 popd
589 pushd /var
590 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
591 popd
592
593 echo "done"
594
595 # hand over to pl_sysinit
596 echo "pl_rsysinit: handing over to pl_sysinit"
597 /etc/init.d/pl_sysinit
598 EOF
599     chmod +x etc/rc.d/init.d/pl_rsysinit
600
601     popd
602
603     # create the cramfs image
604     echo "* Creating cramfs image"
605     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
606     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
607     rm -rf $tmp
608     pop_cleanup
609 }
610
611 #################### Create ISO CRAMFS image
612 function build_iso_cramfs() {
613     local iso="$1" ; shift
614     local custom="$1"
615
616     prepare_cramfs "$custom"
617     echo "* Creating ISO CRAMFS-based image"
618
619     local tmp="${BUILDTMP}/cramfs-iso"
620     mkdir -p "$tmp"
621     push_cleanup rm -rf $tmp
622     (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
623     cat >$tmp/isolinux.cfg <<EOF
624 ${console_serial_line}
625 DEFAULT kernel
626 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${KERNEL_ARGS}
627 DISPLAY pl_version
628 PROMPT 0
629 TIMEOUT 40
630 EOF
631
632     cp ${BUILDTMP}/cramfs.img $tmp
633     mkisofs -o "$iso" \
634         $MKISOFS_OPTS \
635         $tmp
636
637     rm -fr "$tmp"
638     pop_cleanup
639 }
640
641 #################### Create USB CRAMFS based image
642 function build_usb_cramfs() {
643     local usb="$1" ; shift
644     local custom="$1"
645
646     prepare_cramfs "$custom"
647     echo "* Creating USB CRAMFS based image"
648
649     let vfat_size=${cramfs_size}+$FREE_SPACE
650
651     # Make VFAT filesystem for USB
652     mkfs.vfat -C "$usb" $vfat_size
653
654     # Populate it
655     echo "* Populating USB with overlay images and cramfs"
656     mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
657     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
658
659     # Use syslinux instead of isolinux to make the image bootable
660     tmp="${BUILDTMP}/syslinux.cfg"
661     cat >$tmp <<EOF
662 ${console_serial_line}
663 DEFAULT kernel
664 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${KERNEL_ARGS}
665 DISPLAY pl_version
666 PROMPT 0
667 TIMEOUT 40
668 EOF
669
670     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
671     rm -f "$tmp"
672
673     echo "* Making USB CRAMFS based image bootable"
674     syslinux "$usb"
675 }
676
677 #################### map on all types provided on the command-line and invoke one of the above functions
678 function build_types () {
679
680     [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
681
682     # alter output filename to reflect serial settings
683     if [ -n "$IS_SERIAL" ] ; then
684         if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
685             serial="-serial"
686         else
687             serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
688         fi
689     else
690         serial=""
691     fi
692     
693     function type_to_name() {
694         echo $1 | sed '
695         s/usb$/.usb/;
696         s/usb_partition$/-partition.usb/;
697         s/iso$/.iso/;
698         s/usb_cramfs$/-cramfs.usb/;
699         s/iso_cramfs$/-cramfs.iso/;
700         '
701     }
702
703     for t in $TYPES; do
704         arg=$t
705
706         tname=`type_to_name $t`
707         # if -o is specified (as it has no default)
708         if [ -n "$OUTPUT_NAME" ] ; then
709             output=$OUTPUT_NAME
710         else
711             output="${OUTPUT_BASE}${serial}${tname}"
712         fi
713
714         echo "*** Dealing with type=$arg"
715         echo '*' build_$t "$output" "$CUSTOM_DIR"
716         [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR" 
717     done
718 }
719
720 #################### 
721 function main () {
722
723     parse_command_line "$@"
724
725     init_and_check
726
727     echo "* Building images for $FULL_VERSION_STRING"
728     # Do not tolerate errors
729     set -e
730     trap "do_cleanup" ERR INT EXIT
731
732     init_serial $CONSOLE_INFO
733     build_overlay
734     build_types
735
736     exit 0
737 }
738
739 ####################
740 main "$@"