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