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