add net.ifnames=0 to the kernel args for still having eth0 on f29
[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     # ship only what is mandatory, and forget about
263     # (*) isolinux-debug.bin as its name confuses mkisofs
264     # (*) memdisk that is not useful
265     isolinuxfiles="isolinux.bin ldlinux.c32"
266     for isolinuxfile in $isolinuxfiles; do
267         [ -f $isolinuxdir/$isolinuxfile ] && cp $isolinuxdir/$isolinuxfile "${BUILDTMP}/isofs"
268     done
269
270     # Root of the ISO and USB images
271     echo "* Populating root filesystem..."
272     OVERLAY="${BUILDTMP}/overlay"
273     install -d -m 755 $OVERLAY
274     push_cleanup rm -fr $OVERLAY
275
276     # Create version files
277     echo "* Creating version files"
278
279     # Boot Manager compares pl_version in both places to make sure that
280     # the right CD is mounted. We used to boot from an initrd and mount
281     # the CD on /usr. Now we just run everything out of the initrd.
282     for file in $OVERLAY/pl_version $OVERLAY/usr/isolinux/pl_version ; do
283         mkdir -p $(dirname $file)
284         echo "$FULL_VERSION_STRING" >$file
285     done
286
287     # Install boot server configuration files
288     echo "* Installing boot server configuration files"
289
290     # We always intended to bring up and support backup boot servers,
291     # but never got around to it. Just install the same parameters for
292     # both for now.
293     for dir in $OVERLAY/usr/boot $OVERLAY/usr/boot/backup ; do
294         install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
295         install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
296         echo "$PLC_BOOT_HOST" >$dir/boot_server
297         echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
298         echo "/boot/" >$dir/boot_server_path
299     done
300
301     # Install old-style boot server configuration files
302     # as opposed to what a former comment suggested, 
303     # this is still required, somewhere in the bootmanager apparently
304     install -D -m 644 $PLC_BOOT_CA_SSL_CRT $OVERLAY/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
305     echo "$FULL_VERSION_STRING" >$OVERLAY/usr/bootme/ID
306     echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER
307     echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER_IP
308     echo "$PLC_BOOT_SSL_PORT" >$OVERLAY/usr/bootme/BOOTPORT
309
310     # Generate /etc/issue
311     echo "* Generating /etc/issue"
312
313     if [ "$PLC_WWW_PORT" = "443" ] ; then
314         PLC_WWW_URL="https://$PLC_WWW_HOST/"
315     elif [ "$PLC_WWW_PORT" != "80" ] ; then
316         PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
317     else
318         PLC_WWW_URL="http://$PLC_WWW_HOST/"
319     fi
320
321     mkdir -p $OVERLAY/etc
322     cat >$OVERLAY/etc/issue <<EOF
323 $FULL_VERSION_STRING
324 $PLC_NAME Node: \n
325 Kernel \r on an \m
326 $PLC_WWW_URL
327
328 This machine is a node in the $PLC_NAME distributed network.  It has
329 not fully booted yet. If you have cancelled the boot process at the
330 request of $PLC_NAME Support, please follow the instructions provided
331 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
332
333 Console login at this point is restricted to root. Provide the root
334 password of the default $PLC_NAME Central administrator account at the
335 time that this CD was created.
336
337 EOF
338     
339     # Set root password
340     echo "* Setting root password"
341
342     if [ -z "$ROOT_PASSWORD" ] ; then
343         # Generate an encrypted password with crypt() if not defined
344         # in a static configuration.
345         ROOT_PASSWORD=$(python <<EOF
346 import crypt, random, string
347 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
348 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
349 EOF
350 )
351     fi
352
353     # build/passwd copied out by prep.sh
354     sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" ${VARIANT}/passwd >$OVERLAY/etc/passwd
355
356 # this is more harmful than helpful
357 # idea being, since we start a full-featured fedora system now, it would
358 # have been nice to be able to enter sshd very early on - before bm has even been downloaded
359 # however somehow it appears that these lines ruin all chances to enter ssh at all
360 # either early or even later on;
361 # plus, it is unclear what this would give on non=systemd nodes, so I am backing off for now    
362 #    # recent bootCDs rely on a standard systemd startup sequence
363 #    # so allow debug key to enter in this context whenever that makes sense
364 #    mkdir -p $OVERLAY/root/.ssh
365 #    chmod 700 $OVERLAY/root/.ssh
366 #    cp $PLC_DEBUG_SSH_KEY_PUB $OVERLAY/root/.ssh/authorized_keys
367 #    chmod 600 $OVERLAY/root/.ssh/authorized_keys
368
369     # Install node configuration file (e.g., if node has no floppy disk or USB slot)
370     if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
371         echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
372         install -D -m 644 $NODE_CONFIGURATION_FILE $OVERLAY/usr/boot/plnode.txt
373         NODE_ID=$(source $NODE_CONFIGURATION_FILE; echo $NODE_ID)
374         echo "* Building network configuration for $NODE_ID"
375         plnet -- --root $OVERLAY --files-only --program BootCD $NODE_ID
376     fi
377
378     [ -n "$IS_SERIAL" ] && KERNEL_ARGS="$KERNEL_ARGS ${console_spec}"
379
380     # tmp: should be restricted to f15 nodes and above
381     # making sure the network interfaces are still numbered eth0 and above
382     KERNEL_ARGS="$KERNEL_ARGS biosdevname=0"
383     # this apparently is required instead (or in addition to) starting with f29
384     KERNEL_ARGS="$KERNEL_ARGS net.ifnames=0"
385     # making sure selinux is turned off - somehow this is needed with lxc/f14
386     KERNEL_ARGS="$KERNEL_ARGS selinux=0"
387     # add any debug flag if any (defined in the header of this script)
388     KERNEL_ARGS="$KERNEL_ARGS $KERNEL_DEBUG_ARGS"
389     # propagate kernel args for later boot stages
390     [ -n "$KERNEL_ARGS" ] && echo "$KERNEL_ARGS" > $OVERLAY/kargs.txt
391
392     # Pack overlay files into a compressed archive
393     echo "* Compressing overlay image"
394     (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
395
396     rm -rf $OVERLAY
397     pop_cleanup
398
399     if [ -n "$CUSTOM_DIR" ]; then
400         echo "* Compressing custom image"
401         (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
402     fi
403
404     # Calculate ramdisk size (total uncompressed size of both archives)
405     ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
406     ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
407
408     echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
409
410     popd
411 }
412
413 #################### plain ISO
414 function build_iso() {
415     local iso="$1" ; shift
416     local custom="$1"
417
418     # Write isolinux configuration
419     cat >$ISOFS/isolinux.cfg <<EOF
420 ${console_serial_line}
421 PROMPT 0
422 DEFAULT planetlab-bootcd
423
424 LABEL planetlab-bootcd
425   DISPLAY pl_version
426   LINUX kernel
427   APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
428 EOF
429
430     # Create ISO image
431     echo "* Generated isolinux.cfg -------------------- BEG"
432     cat $ISOFS/isolinux.cfg
433     echo "* Generated isolinux.cfg -------------------- END"
434     echo "* Creating ISO image in pwd=$(pwd)"
435     echo "* with command mkisofs -o $iso $MKISOFS_OPTS $ISOFS"
436     mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
437 }
438
439 #################### USB with partitions
440 function build_usb_partition() {
441     echo -n "* Creating USB image with partitions..."
442     local usb="$1" ; shift
443     local custom="$1"
444
445     local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
446     size=$(( $size / 1024 ))
447
448     local heads=64
449     local sectors=32
450     local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
451     local offset=$(( $sectors*512 ))
452
453     if [ -f  /usr/lib/syslinux/mkdiskimage ] ; then
454         /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
455     else
456         mkdiskimage -M -4 "$usb" $size $heads $sectors
457     fi
458
459     cat >${BUILDTMP}/mtools.conf<<EOF
460 drive z:
461 file="${usb}"
462 cylinders=$cylinders
463 heads=$heads
464 sectors=$sectors
465 offset=$offset
466 mformat_only
467 mtools_skip_check=1
468 EOF
469     # environment variable for mtools
470     export MTOOLSRC="${BUILDTMP}/mtools.conf"
471
472     ### COPIED FROM build_usb() below!!!!
473     echo -n " populating USB image... "
474     mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
475         
476     # Use syslinux instead of isolinux to make the image bootable
477     tmp="${BUILDTMP}/syslinux.cfg"
478     cat >$tmp <<EOF
479 ${console_serial_line}
480 PROMPT 0
481 DEFAULT planetlab-bootcd
482
483 LABEL planetlab-bootcd
484   DISPLAY pl_version
485   LINUX kernel
486   APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
487 EOF
488     mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
489     mcopy -i "$usb" "$tmp" z:/syslinux.cfg
490     rm -f "$tmp"
491     rm -f "${MTOOLSRC}"
492     unset MTOOLSRC
493
494     echo "making USB image bootable."
495     syslinux -o $offset "$usb"
496
497 }
498
499 #################### plain USB
500 function build_usb() {
501     echo -n "* Creating USB image... "
502     local usb="$1" ; shift
503     local custom="$1"
504
505     rm -f "$usb"
506     mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
507
508     cat >${BUILDTMP}/mtools.conf<<EOF
509 mtools_skip_check=1
510 EOF
511     # environment variable for mtools
512     export MTOOLSRC="${BUILDTMP}/mtools.conf"
513
514     # Populate it
515     echo -n " populating USB image... "
516     mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
517
518     # Use syslinux instead of isolinux to make the image bootable
519     tmp="${BUILDTMP}/syslinux.cfg"
520     cat >$tmp <<EOF
521 ${console_serial_line}
522 PROMPT 0
523 DEFAULT planetlab-bootcd
524
525 LABEL planetlab-bootcd
526   DISPLAY pl_version
527   LINUX kernel
528   APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${KERNEL_ARGS}
529 EOF
530     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
531     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
532     rm -f "$tmp"
533     rm -f "${MTOOLSRC}"
534     unset MTOOLSRC
535
536     echo "making USB image bootable."
537     syslinux "$usb"
538 }
539
540 #################### utility to setup CRAMFS related support
541 function prepare_cramfs() {
542     [ -n "$CRAMFS_PREPARED" ] && return 0
543     local custom=$1; 
544
545     echo "* Setting up CRAMFS-based images"
546     local tmp="${BUILDTMP}/cramfs-tree"
547     mkdir -p "$tmp"
548     push_cleanup rm -rf $tmp
549     pushd $tmp
550     gzip -d -c $ISOFS/bootcd.img     | cpio -diu
551     gzip -d -c $ISOFS/overlay.img    | cpio -diu
552     [ -n "$custom" ] && \
553         gzip -d -c $ISOFS/custom.img | cpio -diu
554
555     # clean out unnecessary rpm lib
556     echo "* clearing var/lib/rpm/*"
557     rm -f var/lib/rpm/*
558
559     # bootcd requires this directory
560     mkdir -p mnt/confdevice
561
562     # relocate various directory to /tmp
563     rm -rf root
564     ln -fs /tmp/root root
565     ln -fs /sbin/init linuxrc 
566     ln -fs /tmp/resolv.conf etc/resolv.conf
567     ln -fs /tmp/etc/mtab etc/mtab
568
569     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
570     # make /tmp/etc
571     echo "* renaming dirs in ./etc"
572     pushd etc
573     for dir in `find * -type d -prune | grep -v rc.d`; do
574         mv ${dir} ${dir}_o
575         ln -fs /tmp/etc/${dir} ${dir}
576     done
577     popd
578
579     echo "* renaming dirs in ./var"
580     # rename all top-level directories and put in a symlink to /tmp/var
581     pushd var
582     for dir in `find * -type d -prune`; do
583         mv ${dir} ${dir}_o
584         ln -fs /tmp/var/${dir} ${dir}
585     done
586     popd
587
588     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
589     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
590     rm -f ./etc/fstab
591     cat >./etc/fstab <<EOF
592 /dev/ram0     /              cramfs     ro              0 0
593 none          /dev/pts       devpts     gid=5,mode=620  0 0
594 none          /proc          proc       defaults        0 0
595 none          /sys           sysfs      defaults        0 0
596 EOF
597
598     pushd dev
599     rm -f console
600     mknod console c 5 1
601     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
602     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
603     #ln -fs ram1 ram
604     #ln -fs ram0 ramdisk
605     popd
606
607     # update etc/inittab to start with pl_rsysinit
608     for file in etc/inittab etc/event.d/rcS etc/init/rcS.conf; do
609         [ -f $file ] && sed -i 's,pl_sysinit,pl_rsysinit,' $file
610     done
611
612     # modify inittab to have a serial console
613     # xxx this might well be broken with f12 and above xxx
614     if [ -n "$serial" ] ; then
615         echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
616         # and let root log in
617         echo "$console_dev" >> etc/securetty
618     fi
619
620     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
621     etcsize=$(du -s ./etc | awk '{ print $1 }')
622     varsize=$(du -s ./var | awk '{ print $1 }')
623     let msize=($varsize+$etcsize+8192)/1024
624
625     # make dhclient happy
626     for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
627     ln -fs /tmp/etc/resolv.conf etc/resolv.conf
628     ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
629
630     # generate pl_rsysinit
631     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
632 #!/bin/sh
633 # generated by $COMMAND
634 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
635 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
636 mkdir -p /tmp/root
637 mkdir -p /tmp/etc
638 touch /tmp/etc/resolv.conf
639 touch /tmp/etc/mtab
640 mkdir -p /tmp/var
641
642 # make mtab happy
643 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
644
645 # copy over directory contents of all _o directories from /etc and /var
646 # /tmp/etc and /tmp/var
647 pushd /etc
648 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
649 popd
650 pushd /var
651 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
652 popd
653
654 echo "done"
655
656 # hand over to pl_sysinit
657 echo "pl_rsysinit: handing over to pl_sysinit"
658 /etc/init.d/pl_sysinit
659 EOF
660     chmod +x etc/rc.d/init.d/pl_rsysinit
661
662     popd
663
664     # create the cramfs image
665     echo "* Creating cramfs image"
666     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
667     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
668     rm -rf $tmp
669     pop_cleanup
670 }
671
672 #################### Create ISO CRAMFS image
673 function build_iso_cramfs() {
674     local iso="$1" ; shift
675     local custom="$1"
676
677     prepare_cramfs "$custom"
678     echo "* Creating ISO CRAMFS-based image"
679
680     local tmp="${BUILDTMP}/cramfs-iso"
681     mkdir -p "$tmp"
682     push_cleanup rm -rf $tmp
683     (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
684     cat >$tmp/isolinux.cfg <<EOF
685 ${console_serial_line}
686 PROMPT 0
687 DEFAULT planetlab-bootcd
688
689 LABEL planetlab-bootcd
690   DISPLAY pl_version
691   LINUX kernel
692   APPEND ramdisk_size=$ramdisk_size initrd=cramfs.img root=/dev/ram0 rw ${KERNEL_ARGS}
693 EOF
694
695     cp ${BUILDTMP}/cramfs.img $tmp
696     mkisofs -o "$iso" \
697         $MKISOFS_OPTS \
698         $tmp
699
700     rm -fr "$tmp"
701     pop_cleanup
702 }
703
704 #################### Create USB CRAMFS based image
705 function build_usb_cramfs() {
706     local usb="$1" ; shift
707     local custom="$1"
708
709     prepare_cramfs "$custom"
710     echo "* Creating USB CRAMFS based image"
711
712     let vfat_size=${cramfs_size}+$FREE_SPACE
713
714     # Make VFAT filesystem for USB
715     mkfs.vfat -C "$usb" $vfat_size
716
717     # Populate it
718     echo "* Populating USB with overlay images and cramfs"
719     mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
720     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
721
722     # Use syslinux instead of isolinux to make the image bootable
723     tmp="${BUILDTMP}/syslinux.cfg"
724     cat >$tmp <<EOF
725 ${console_serial_line}
726 PROMPT 0
727 DEFAULT planetlab-bootcd
728
729 LABEL planetlab-bootcd
730   DISPLAY pl_version
731   LINUX kernel
732   APPEND ramdisk_size=$ramdisk_size initrd=cramfs.img root=/dev/ram0 rw ${KERNEL_ARGS}
733 EOF
734
735     mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
736     rm -f "$tmp"
737
738     echo "* Making USB CRAMFS based image bootable"
739     syslinux "$usb"
740 }
741
742 #################### map on all types provided on the command-line and invoke one of the above functions
743 function build_types () {
744
745     [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
746
747     # alter output filename to reflect serial settings
748     if [ -n "$IS_SERIAL" ] ; then
749         if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
750             serial="-serial"
751         else
752             serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
753         fi
754     else
755         serial=""
756     fi
757     
758     function type_to_name() {
759         echo $1 | sed '
760         s/usb$/.usb/;
761         s/usb_partition$/-partition.usb/;
762         s/iso$/.iso/;
763         s/usb_cramfs$/-cramfs.usb/;
764         s/iso_cramfs$/-cramfs.iso/;
765         '
766     }
767
768     for t in $TYPES; do
769         arg=$t
770
771         tname=`type_to_name $t`
772         # if -o is specified (as it has no default)
773         if [ -n "$OUTPUT_NAME" ] ; then
774             output=$OUTPUT_NAME
775         else
776             output="${OUTPUT_BASE}${serial}${tname}"
777         fi
778
779         echo "*** Dealing with type=$arg"
780         echo '*' build_$t "$output" "$CUSTOM_DIR"
781         [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR" 
782     done
783 }
784
785 #################### 
786 function main () {
787
788     parse_command_line "$@"
789
790     init_and_check
791
792     echo "* Building bootcd images for $NODE_CONFIGURATION_FILE ($FULL_VERSION_STRING) - $(date +%H-%M:%S)"
793     # Do not tolerate errors
794     set -e
795     trap "do_cleanup" ERR INT EXIT
796
797     init_serial $CONSOLE_INFO
798     build_overlay
799     build_types
800
801     echo "* Done with bootcd images for $NODE_CONFIGURATION_FILE - $(date +%H-%M:%S)"
802     exit 0
803 }
804
805 ####################
806 main "$@"