3 # Builds custom BootCD ISO and USB images in the current
6 # Aaron Klingaman <alk@absarokasoft.com>
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2004-2007 The Trustees of Princeton University
13 PATH=/sbin:/bin:/usr/sbin:/usr/bin
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"
24 # command-line settable args
25 NODE_CONFIGURATION_FILE=
35 FULL_VERSION_STRING=""
43 console_serial_line=""
47 #################### compute all supported types
48 # removing support for serial in the type
49 # this is because kargs.txt goes in the overlay, that is computed only once
50 # so we cannot handle serial and graphic modes within the same invokation of this script
53 for x in iso usb usb_partition; do for c in "" "_cramfs" ; do
60 ALL_TYPES="$ALL_TYPES $t" ;;
64 #################### cleanup utilities
65 declare -a _CLEANUPS=()
66 function do_cleanup() {
67 cd / ; for i in "${_CLEANUPS[@]}"; do $i ; done
69 function push_cleanup() {
70 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
72 function pop_cleanup() {
73 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
76 #################### initialization
77 function init_and_check () {
79 # Change to our source directory
80 local srcdir=$(cd $(dirname $0) && pwd -P)
86 # The reference image is expected to have been built by prep.sh (see .spec)
87 # we disable the initial logic that called prep.sh if that was not the case
88 # this is because prep.sh needs to know pldistro
89 if [ ! -f $ISOREF/isofs/bootcd.img -o ! -f $ISOREF/version.txt ] ; then
90 echo "You have to run prep.sh prior to calling $0 - exiting"
94 # build/version.txt written by prep.sh
95 BOOTCD_VERSION=$(cat build/version.txt)
97 if [ -f /etc/planetlab/plc_config ] ; then
98 # Source PLC configuration
99 . /etc/planetlab/plc_config
102 # use /var/tmp that should be large enough on both chroot- or vserver-based myplc
105 FULL_VERSION_STRING="${PLC_NAME} BootCD ${BOOTCD_VERSION}"
110 # the custom-dir feature is designed to let a myplc try/ship a patched bootcd
111 # without the need for a full devel environment
112 # for example, you would create /root/custom-bootcd/etc/rc.d/init.d/pl_hwinit
113 # and run this script with -C /root/custom-bootcd
114 # this creates a third .img image of the custom dir, that 'hides' the files from
115 # bootcd.img in the resulting unionfs
116 # it seems that this feature has not been used nor tested in a long time, use with care
119 echo "Usage: build.sh [OPTION]..."
120 echo " -f plnode.txt Node to customize CD for (default: none)"
121 echo " -t 'types' Build the specified images (default: $DEFAULT_TYPES)"
122 echo " NOTE: mentioning 'serial' as part of the type is not supported anymore"
123 echo " -a Build all known types as listed below"
124 echo " -s console-info Enable a serial line as console and also bring up getty on that line"
125 echo " console-info: tty:baud-rate:parity:bits"
126 echo " or 'default' shortcut for $SERIAL_CONSOLE"
127 echo " -S equivalent to -s default"
128 echo " -O output-base The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
129 echo " useful when multiple types are provided"
130 echo " can be a full path"
131 echo " -o output-name The full name of the generated file"
132 echo " -C custom-dir Custom directory"
133 echo " -n Dry run - mostly for debug/test purposes"
134 echo " -k Add additional parameters to the kargs.txt file"
135 echo " -h This message"
136 echo "All known types: $ALL_TYPES"
141 function parse_command_line () {
146 while getopts "f:t:as:SO:o:C:k:nh" opt ; do
148 f) NODE_CONFIGURATION_FILE=$OPTARG ;;
149 t) TYPES="$TYPES $OPTARG" ;;
150 a) TYPES="$ALL_TYPES" ;;
151 s) CONSOLE_INFO="$OPTARG" ;;
152 S) CONSOLE_INFO=$SERIAL_CONSOLE ;;
153 O) OUTPUT_BASE="$OPTARG" ;;
154 o) OUTPUT_NAME="$OPTARG" ;;
155 C) CUSTOM_DIR="$OPTARG" ;;
156 k) KARGS_STR="$KARGS_STR $OPTARG" ;;
162 # use defaults if not set
163 [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
164 [ "$CONSOLE_INFO" == "default" ] && CONSOLE_INFO=$SERIAL_CONSOLE
167 local matcher="XXX$(echo $ALL_TYPES | sed -e 's,\W,XXX,g')XXX"
169 echo Checking type $t
170 echo $matcher | grep XXX${t}XXX &> /dev/null
171 if [ "$?" != 0 ] ; then
180 function init_serial () {
181 local console=$1; shift
182 if [ "$console" == "$GRAPHIC_CONSOLE" ] ; then
185 echo "Standard, graphic, non-serial mode"
188 console_dev=$(echo "$console" | awk -F: ' {print $1}')
189 console_baud=$(echo "$console" | awk -F: ' {print $2}')
190 [ -z "$console_baud" ] && console_baud="115200"
191 local console_parity=$(echo "$console" | awk -F: ' {print $3}')
192 [ -z "$console_parity" ] && console_parity="n"
193 local console_bits=$(echo "$console" | awk -F: ' {print $4}')
194 [ -z "$console_bits" ] && console_bits="8"
195 console_spec="console=${console_dev},${console_baud}${console_parity}${console_bits}"
196 local tty_nb=$(echo $console_dev | sed -e 's,[a-zA-Z],,g')
197 console_serial_line="SERIAL ${tty_nb} ${console_baud}"
199 echo "console_serial_line=${console_serial_line}"
200 echo "console_spec=${console_spec}"
204 #################### run once : build the overlay image
205 function build_overlay () {
207 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
208 push_cleanup rm -fr "${BUILDTMP}"
209 mkdir "${BUILDTMP}/isofs"
210 for i in "$ISOREF"/isofs/{bootcd.img,kernel}; do
211 ln -s "$i" "${BUILDTMP}/isofs"
213 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
214 ISOFS="${BUILDTMP}/isofs"
216 # Root of the ISO and USB images
217 echo "* Populating root filesystem..."
218 OVERLAY="${BUILDTMP}/overlay"
219 install -d -m 755 $OVERLAY
220 push_cleanup rm -fr $OVERLAY
222 # Create version files
223 echo "* Creating version files"
225 # Boot Manager compares pl_version in both places to make sure that
226 # the right CD is mounted. We used to boot from an initrd and mount
227 # the CD on /usr. Now we just run everything out of the initrd.
228 for file in $OVERLAY/pl_version $OVERLAY/usr/isolinux/pl_version ; do
229 mkdir -p $(dirname $file)
230 echo "$FULL_VERSION_STRING" >$file
233 # Install boot server configuration files
234 echo "* Installing boot server configuration files"
236 # We always intended to bring up and support backup boot servers,
237 # but never got around to it. Just install the same parameters for
239 for dir in $OVERLAY/usr/boot $OVERLAY/usr/boot/backup ; do
240 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
241 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
242 echo "$PLC_BOOT_HOST" >$dir/boot_server
243 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
244 echo "/boot/" >$dir/boot_server_path
247 # Install old-style boot server configuration files
248 # as opposed to what a former comment suggested,
249 # this is still required, somewhere in the bootmanager apparently
250 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $OVERLAY/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
251 echo "$FULL_VERSION_STRING" >$OVERLAY/usr/bootme/ID
252 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER
253 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER_IP
254 echo "$PLC_BOOT_SSL_PORT" >$OVERLAY/usr/bootme/BOOTPORT
256 # Generate /etc/issue
257 echo "* Generating /etc/issue"
259 if [ "$PLC_WWW_PORT" = "443" ] ; then
260 PLC_WWW_URL="https://$PLC_WWW_HOST/"
261 elif [ "$PLC_WWW_PORT" != "80" ] ; then
262 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
264 PLC_WWW_URL="http://$PLC_WWW_HOST/"
267 mkdir -p $OVERLAY/etc
268 cat >$OVERLAY/etc/issue <<EOF
274 This machine is a node in the $PLC_NAME distributed network. It has
275 not fully booted yet. If you have cancelled the boot process at the
276 request of $PLC_NAME Support, please follow the instructions provided
277 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
279 Console login at this point is restricted to root. Provide the root
280 password of the default $PLC_NAME Central administrator account at the
281 time that this CD was created.
286 echo "* Setting root password"
288 if [ -z "$ROOT_PASSWORD" ] ; then
289 # Generate an encrypted password with crypt() if not defined
290 # in a static configuration.
291 ROOT_PASSWORD=$(python <<EOF
292 import crypt, random, string
293 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
294 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
299 # build/passwd copied out by prep.sh
300 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd >$OVERLAY/etc/passwd
302 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
303 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
304 echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
305 install -D -m 644 $NODE_CONFIGURATION_FILE $OVERLAY/usr/boot/plnode.txt
306 NODE_ID=$(source $NODE_CONFIGURATION_FILE; echo $NODE_ID)
307 echo "* Building network configuration for $NODE_ID"
308 plnet -- --root $OVERLAY --files-only --program BootCD $NODE_ID
311 if [ -n "$IS_SERIAL" ] ; then
312 KARGS_STR="$KARGS_STR ${console_spec}"
315 if [ -n "$KARGS_STR" ] ; then
316 echo "$KARGS_STR" > $OVERLAY/kargs.txt
317 kernel_args=$KARGS_STR
320 # Pack overlay files into a compressed archive
321 echo "* Compressing overlay image"
322 (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
327 if [ -n "$CUSTOM_DIR" ]; then
328 echo "* Compressing custom image"
329 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
332 # Calculate ramdisk size (total uncompressed size of both archives)
333 ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
334 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
336 echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
341 #################### plain ISO
342 function build_iso() {
343 local iso="$1" ; shift
346 # Write isolinux configuration
347 cat >$ISOFS/isolinux.cfg <<EOF
348 ${console_serial_line}
350 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
357 echo "* Creating ISO image"
358 mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
361 #################### USB with partitions
362 function build_usb_partition() {
363 echo -n "* Creating USB image with partitions..."
364 local usb="$1" ; shift
367 local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
368 size=$(( $size / 1024 ))
372 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
373 local offset=$(( $sectors*512 ))
375 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
377 cat >${BUILDTMP}/mtools.conf<<EOF
386 # environment variable for mtools
387 export MTOOLSRC="${BUILDTMP}/mtools.conf"
389 ### COPIED FROM build_usb() below!!!!
390 echo -n " populating USB image... "
391 mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
393 # Use syslinux instead of isolinux to make the image bootable
394 tmp="${BUILDTMP}/syslinux.cfg"
396 ${console_serial_line}
398 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
403 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
404 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
406 rm -f "${BUILDTMP}/mtools.conf"
409 echo "making USB image bootable."
410 syslinux -o $offset "$usb"
414 #################### plain USB
415 function build_usb() {
416 echo -n "* Creating USB image... "
417 local usb="$1" ; shift
420 mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
423 echo -n " populating USB image... "
424 mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
426 # Use syslinux instead of isolinux to make the image bootable
427 tmp="${BUILDTMP}/syslinux.cfg"
429 ${console_serial_line}
431 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
436 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
437 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
440 echo "making USB image bootable."
444 #################### utility to setup CRAMFS related support
445 function prepare_cramfs() {
446 [ -n "$CRAMFS_PREPARED" ] && return 0
449 echo "* Setting up CRAMFS-based images"
450 local tmp="${BUILDTMP}/cramfs-tree"
452 push_cleanup rm -rf $tmp
454 gzip -d -c $ISOFS/bootcd.img | cpio -diu
455 gzip -d -c $ISOFS/overlay.img | cpio -diu
456 [ -n "$custom" ] && \
457 gzip -d -c $ISOFS/custom.img | cpio -diu
459 # clean out unnecessary rpm lib
460 echo "* clearing var/lib/rpm/*"
463 # bootcd requires this directory
464 mkdir -p mnt/confdevice
466 # relocate various directory to /tmp
468 ln -fs /tmp/root root
469 ln -fs /sbin/init linuxrc
470 ln -fs /tmp/resolv.conf etc/resolv.conf
471 ln -fs /tmp/etc/mtab etc/mtab
473 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
475 echo "* renaming dirs in ./etc"
477 for dir in `find * -type d -prune | grep -v rc.d`; do
479 ln -fs /tmp/etc/${dir} ${dir}
483 echo "* renaming dirs in ./var"
484 # rename all top-level directories and put in a symlink to /tmp/var
486 for dir in `find * -type d -prune`; do
488 ln -fs /tmp/var/${dir} ${dir}
492 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
493 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
495 cat >./etc/fstab <<EOF
496 /dev/ram0 / cramfs ro 0 0
497 none /dev/pts devpts gid=5,mode=620 0 0
498 none /proc proc defaults 0 0
499 none /sys sysfs defaults 0 0
505 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
506 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
511 # update etc/inittab to start with pl_rsysinit
512 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
514 # modify inittab to have a serial console
515 if [ -n "$serial" ] ; then
516 echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
517 # and let root log in
518 echo "$console_dev" >> etc/securetty
521 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
522 etcsize=$(du -s ./etc | awk '{ print $1 }')
523 varsize=$(du -s ./var | awk '{ print $1 }')
524 let msize=($varsize+$etcsize+8192)/1024
526 # make dhclient happy
527 for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
528 ln -fs /tmp/etc/resolv.conf etc/resolv.conf
529 ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
531 # generate pl_rsysinit
532 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
534 # generated by build.sh
535 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
536 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
539 touch /tmp/etc/resolv.conf
544 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
546 # copy over directory contents of all _o directories from /etc and /var
547 # /tmp/etc and /tmp/var
549 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
552 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
557 # hand over to pl_sysinit
558 echo "pl_rsysinit: handing over to pl_sysinit"
559 /etc/init.d/pl_sysinit
561 chmod +x etc/rc.d/init.d/pl_rsysinit
565 # create the cramfs image
566 echo "* Creating cramfs image"
567 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
568 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
573 #################### Create ISO CRAMFS image
574 function build_iso_cramfs() {
575 local iso="$1" ; shift
578 prepare_cramfs "$custom"
579 echo "* Creating ISO CRAMFS-based image"
581 local tmp="${BUILDTMP}/cramfs-iso"
583 push_cleanup rm -rf $tmp
584 (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
585 cat >$tmp/isolinux.cfg <<EOF
586 ${console_serial_line}
588 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
594 cp ${BUILDTMP}/cramfs.img $tmp
603 #################### Create USB CRAMFS based image
604 function build_usb_cramfs() {
605 local usb="$1" ; shift
608 prepare_cramfs "$custom"
609 echo "* Creating USB CRAMFS based image"
611 let vfat_size=${cramfs_size}+$FREE_SPACE
613 # Make VFAT filesystem for USB
614 mkfs.vfat -C "$usb" $vfat_size
617 echo "* Populating USB with overlay images and cramfs"
618 mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
619 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
621 # Use syslinux instead of isolinux to make the image bootable
622 tmp="${BUILDTMP}/syslinux.cfg"
624 ${console_serial_line}
626 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
632 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
635 echo "* Making USB CRAMFS based image bootable"
639 #################### map on all types provided on the command-line and invoke one of the above functions
640 function build_types () {
642 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
644 # alter output filename to reflect serial settings
645 if [ -n "$IS_SERIAL" ] ; then
646 if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
649 serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
655 function type_to_name() {
658 s/usb_partition$/-partition.usb/;
660 s/usb_cramfs$/-cramfs.usb/;
661 s/iso_cramfs$/-cramfs.iso/;
668 tname=`type_to_name $t`
669 # if -o is specified (as it has no default)
670 if [ -n "$OUTPUT_NAME" ] ; then
673 output="${OUTPUT_BASE}${serial}${tname}"
676 echo "*** Dealing with type=$arg"
677 echo '*' build_$t "$output" "$CUSTOM_DIR"
678 [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR"
687 parse_command_line "$@"
689 echo "* Building images for $FULL_VERSION_STRING"
690 # Do not tolerate errors
692 trap "do_cleanup" ERR INT EXIT
694 init_serial $CONSOLE_INFO