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