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