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
11 COMMAND=$(basename $0)
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 && pwd -P)
84 ISOREF=$PWD/${VARIANT}
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 "Could not find isofs and version.txt in $ISOREF"
91 if [ "$VARIANT" == "build" ] ; then
92 echo "You have to run prep.sh prior to calling $COMMAND"
94 echo "You need to create your variant image, see kvariant.sh"
100 # build/version.txt written by prep.sh
101 BOOTCD_VERSION=$(cat ${VARIANT}/version.txt)
103 if [ -f /etc/planetlab/plc_config ] ; then
104 # Source PLC configuration
105 . /etc/planetlab/plc_config
108 # use /var/tmp that should be large enough on both chroot- or vserver-based myplc
111 FULL_VERSION_STRING="${PLC_NAME} BootCD ${BOOTCD_VERSION}"
116 # the custom-dir feature is designed to let a myplc try/ship a patched bootcd
117 # without the need for a full devel environment
118 # for example, you would create /root/custom-bootcd/etc/rc.d/init.d/pl_hwinit
119 # and run this script with -C /root/custom-bootcd
120 # this creates a third .img image of the custom dir, that 'hides' the files from
121 # bootcd.img in the resulting unionfs
122 # it seems that this feature has not been used nor tested in a long time, use with care
125 echo "Usage: $COMMAND [OPTION]..."
126 echo " -f plnode.txt Node to customize CD for (default: none)"
127 echo " -t 'types' Build the specified images (default: $DEFAULT_TYPES)"
128 echo " NOTE: mentioning 'serial' as part of the type is not supported anymore"
129 echo " -a Build all known types as listed below"
130 echo " -s console-info Enable a serial line as console and also bring up getty on that line"
131 echo " console-info: tty:baud-rate:parity:bits"
132 echo " or 'default' shortcut for $SERIAL_CONSOLE"
133 echo " -S equivalent to -s default"
134 echo " -O output-base The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
135 echo " useful when multiple types are provided"
136 echo " can be a full path"
137 echo " -o output-name The full name of the generated file"
138 echo " -C custom-dir Custom directory"
139 echo " -V variant Use a variant - see kvariant.sh"
140 echo " -n Dry run - mostly for debug/test purposes"
141 echo " -k Add additional parameters to the kargs.txt file"
142 echo " -h This message"
143 echo "All known types: $ALL_TYPES"
148 function parse_command_line () {
153 while getopts "f:t:as:SO:o:C:V:k:nh" opt ; do
155 f) NODE_CONFIGURATION_FILE=$OPTARG ;;
156 t) TYPES="$TYPES $OPTARG" ;;
157 a) TYPES="$ALL_TYPES" ;;
158 s) CONSOLE_INFO="$OPTARG" ;;
159 S) CONSOLE_INFO=$SERIAL_CONSOLE ;;
160 O) OUTPUT_BASE="$OPTARG" ;;
161 o) OUTPUT_NAME="$OPTARG" ;;
162 C) CUSTOM_DIR="$OPTARG" ;;
163 V) VARIANT="$OPTARG" ;;
164 k) KARGS_STR="$KARGS_STR $OPTARG" ;;
170 # use defaults if not set
171 [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
172 [ -z "$VARIANT" ] && VARIANT="build"
173 [ "$CONSOLE_INFO" == "default" ] && CONSOLE_INFO=$SERIAL_CONSOLE
175 if [ -n "$NODE_CONFIGURATION_FILE" ] ; then
176 # check existence of NODE_CONFIGURATION_FILE and normalize as we will change directory
177 if [ ! -f "$NODE_CONFIGURATION_FILE" ] ; then
178 echo "Node configuration file $NODE_CONFIGURATION_FILE not found - exiting"
181 cf_dir="$(dirname $NODE_CONFIGURATION_FILE)"
182 cf_dir="$(cd $cf_dir; pwd -P)"
183 cf_file="$(basename $NODE_CONFIGURATION_FILE)"
184 NODE_CONFIGURATION_FILE="$cf_dir"/"$cf_file"
188 local matcher="XXX$(echo $ALL_TYPES | sed -e 's,\W,XXX,g')XXX"
190 echo Checking type $t
191 echo $matcher | grep XXX${t}XXX &> /dev/null
192 if [ "$?" != 0 ] ; then
201 function init_serial () {
202 local console=$1; shift
203 if [ "$console" == "$GRAPHIC_CONSOLE" ] ; then
206 echo "Standard, graphic, non-serial mode"
209 console_dev=$(echo "$console" | awk -F: ' {print $1}')
210 console_baud=$(echo "$console" | awk -F: ' {print $2}')
211 [ -z "$console_baud" ] && console_baud="115200"
212 local console_parity=$(echo "$console" | awk -F: ' {print $3}')
213 [ -z "$console_parity" ] && console_parity="n"
214 local console_bits=$(echo "$console" | awk -F: ' {print $4}')
215 [ -z "$console_bits" ] && console_bits="8"
216 console_spec="console=${console_dev},${console_baud}${console_parity}${console_bits}"
217 local tty_nb=$(echo $console_dev | sed -e 's,[a-zA-Z],,g')
218 console_serial_line="SERIAL ${tty_nb} ${console_baud}"
220 echo "console_serial_line=${console_serial_line}"
221 echo "console_spec=${console_spec}"
225 #################### run once : build the overlay image
226 function build_overlay () {
228 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
229 push_cleanup rm -fr "${BUILDTMP}"
230 mkdir "${BUILDTMP}/isofs"
231 for i in "$ISOREF"/isofs/{bootcd.img,kernel}; do
232 ln -s "$i" "${BUILDTMP}/isofs"
234 # the syslinux that comes with f12 has this file in a new location
235 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs" \
236 || cp "/usr/share/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
237 ISOFS="${BUILDTMP}/isofs"
239 # Root of the ISO and USB images
240 echo "* Populating root filesystem..."
241 OVERLAY="${BUILDTMP}/overlay"
242 install -d -m 755 $OVERLAY
243 push_cleanup rm -fr $OVERLAY
245 # Create version files
246 echo "* Creating version files"
248 # Boot Manager compares pl_version in both places to make sure that
249 # the right CD is mounted. We used to boot from an initrd and mount
250 # the CD on /usr. Now we just run everything out of the initrd.
251 for file in $OVERLAY/pl_version $OVERLAY/usr/isolinux/pl_version ; do
252 mkdir -p $(dirname $file)
253 echo "$FULL_VERSION_STRING" >$file
256 # Install boot server configuration files
257 echo "* Installing boot server configuration files"
259 # We always intended to bring up and support backup boot servers,
260 # but never got around to it. Just install the same parameters for
262 for dir in $OVERLAY/usr/boot $OVERLAY/usr/boot/backup ; do
263 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
264 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
265 echo "$PLC_BOOT_HOST" >$dir/boot_server
266 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
267 echo "/boot/" >$dir/boot_server_path
270 # Install old-style boot server configuration files
271 # as opposed to what a former comment suggested,
272 # this is still required, somewhere in the bootmanager apparently
273 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $OVERLAY/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
274 echo "$FULL_VERSION_STRING" >$OVERLAY/usr/bootme/ID
275 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER
276 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER_IP
277 echo "$PLC_BOOT_SSL_PORT" >$OVERLAY/usr/bootme/BOOTPORT
279 # Generate /etc/issue
280 echo "* Generating /etc/issue"
282 if [ "$PLC_WWW_PORT" = "443" ] ; then
283 PLC_WWW_URL="https://$PLC_WWW_HOST/"
284 elif [ "$PLC_WWW_PORT" != "80" ] ; then
285 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
287 PLC_WWW_URL="http://$PLC_WWW_HOST/"
290 mkdir -p $OVERLAY/etc
291 cat >$OVERLAY/etc/issue <<EOF
297 This machine is a node in the $PLC_NAME distributed network. It has
298 not fully booted yet. If you have cancelled the boot process at the
299 request of $PLC_NAME Support, please follow the instructions provided
300 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
302 Console login at this point is restricted to root. Provide the root
303 password of the default $PLC_NAME Central administrator account at the
304 time that this CD was created.
309 echo "* Setting root password"
311 if [ -z "$ROOT_PASSWORD" ] ; then
312 # Generate an encrypted password with crypt() if not defined
313 # in a static configuration.
314 ROOT_PASSWORD=$(python <<EOF
315 import crypt, random, string
316 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
317 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
322 # build/passwd copied out by prep.sh
323 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" ${VARIANT}/passwd >$OVERLAY/etc/passwd
325 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
326 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
327 echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
328 install -D -m 644 $NODE_CONFIGURATION_FILE $OVERLAY/usr/boot/plnode.txt
329 NODE_ID=$(source $NODE_CONFIGURATION_FILE; echo $NODE_ID)
330 echo "* Building network configuration for $NODE_ID"
331 plnet -- --root $OVERLAY --files-only --program BootCD $NODE_ID
334 if [ -n "$IS_SERIAL" ] ; then
335 KARGS_STR="$KARGS_STR ${console_spec}"
338 if [ -n "$KARGS_STR" ] ; then
339 echo "$KARGS_STR" > $OVERLAY/kargs.txt
340 kernel_args=$KARGS_STR
343 # Pack overlay files into a compressed archive
344 echo "* Compressing overlay image"
345 (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
350 if [ -n "$CUSTOM_DIR" ]; then
351 echo "* Compressing custom image"
352 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
355 # Calculate ramdisk size (total uncompressed size of both archives)
356 ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
357 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
359 echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
364 #################### plain ISO
365 function build_iso() {
366 local iso="$1" ; shift
369 # Write isolinux configuration
370 cat >$ISOFS/isolinux.cfg <<EOF
371 ${console_serial_line}
373 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
380 echo "* Creating ISO image"
381 mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
384 #################### USB with partitions
385 function build_usb_partition() {
386 echo -n "* Creating USB image with partitions..."
387 local usb="$1" ; shift
390 local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
391 size=$(( $size / 1024 ))
395 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
396 local offset=$(( $sectors*512 ))
398 if [ -f /usr/lib/syslinux/mkdiskimage ] ; then
399 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
401 mkdiskimage -M -4 "$usb" $size $heads $sectors
404 cat >${BUILDTMP}/mtools.conf<<EOF
414 # environment variable for mtools
415 export MTOOLSRC="${BUILDTMP}/mtools.conf"
417 ### COPIED FROM build_usb() below!!!!
418 echo -n " populating USB image... "
419 mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
421 # Use syslinux instead of isolinux to make the image bootable
422 tmp="${BUILDTMP}/syslinux.cfg"
424 ${console_serial_line}
426 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
431 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
432 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
437 echo "making USB image bootable."
438 syslinux -o $offset "$usb"
442 #################### plain USB
443 function build_usb() {
444 echo -n "* Creating USB image... "
445 local usb="$1" ; shift
449 mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
451 cat >${BUILDTMP}/mtools.conf<<EOF
454 # environment variable for mtools
455 export MTOOLSRC="${BUILDTMP}/mtools.conf"
458 echo -n " populating USB image... "
459 mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
461 # Use syslinux instead of isolinux to make the image bootable
462 tmp="${BUILDTMP}/syslinux.cfg"
464 ${console_serial_line}
466 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
471 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
472 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
477 echo "making USB image bootable."
481 #################### utility to setup CRAMFS related support
482 function prepare_cramfs() {
483 [ -n "$CRAMFS_PREPARED" ] && return 0
486 echo "* Setting up CRAMFS-based images"
487 local tmp="${BUILDTMP}/cramfs-tree"
489 push_cleanup rm -rf $tmp
491 gzip -d -c $ISOFS/bootcd.img | cpio -diu
492 gzip -d -c $ISOFS/overlay.img | cpio -diu
493 [ -n "$custom" ] && \
494 gzip -d -c $ISOFS/custom.img | cpio -diu
496 # clean out unnecessary rpm lib
497 echo "* clearing var/lib/rpm/*"
500 # bootcd requires this directory
501 mkdir -p mnt/confdevice
503 # relocate various directory to /tmp
505 ln -fs /tmp/root root
506 ln -fs /sbin/init linuxrc
507 ln -fs /tmp/resolv.conf etc/resolv.conf
508 ln -fs /tmp/etc/mtab etc/mtab
510 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
512 echo "* renaming dirs in ./etc"
514 for dir in `find * -type d -prune | grep -v rc.d`; do
516 ln -fs /tmp/etc/${dir} ${dir}
520 echo "* renaming dirs in ./var"
521 # rename all top-level directories and put in a symlink to /tmp/var
523 for dir in `find * -type d -prune`; do
525 ln -fs /tmp/var/${dir} ${dir}
529 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
530 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
532 cat >./etc/fstab <<EOF
533 /dev/ram0 / cramfs ro 0 0
534 none /dev/pts devpts gid=5,mode=620 0 0
535 none /proc proc defaults 0 0
536 none /sys sysfs defaults 0 0
542 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
543 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
548 # update etc/inittab to start with pl_rsysinit
549 for file in etc/inittab etc/event.d/rcS etc/init/rcS.conf; do
550 [ -f $file ] && sed -i 's,pl_sysinit,pl_rsysinit,' $file
553 # modify inittab to have a serial console
554 # xxx this might well be broken with f12 and above xxx
555 if [ -n "$serial" ] ; then
556 echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
557 # and let root log in
558 echo "$console_dev" >> etc/securetty
561 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
562 etcsize=$(du -s ./etc | awk '{ print $1 }')
563 varsize=$(du -s ./var | awk '{ print $1 }')
564 let msize=($varsize+$etcsize+8192)/1024
566 # make dhclient happy
567 for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
568 ln -fs /tmp/etc/resolv.conf etc/resolv.conf
569 ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
571 # generate pl_rsysinit
572 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
574 # generated by $COMMAND
575 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
576 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
579 touch /tmp/etc/resolv.conf
584 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
586 # copy over directory contents of all _o directories from /etc and /var
587 # /tmp/etc and /tmp/var
589 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
592 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 # hand over to pl_sysinit
598 echo "pl_rsysinit: handing over to pl_sysinit"
599 /etc/init.d/pl_sysinit
601 chmod +x etc/rc.d/init.d/pl_rsysinit
605 # create the cramfs image
606 echo "* Creating cramfs image"
607 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
608 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
613 #################### Create ISO CRAMFS image
614 function build_iso_cramfs() {
615 local iso="$1" ; shift
618 prepare_cramfs "$custom"
619 echo "* Creating ISO CRAMFS-based image"
621 local tmp="${BUILDTMP}/cramfs-iso"
623 push_cleanup rm -rf $tmp
624 (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
625 cat >$tmp/isolinux.cfg <<EOF
626 ${console_serial_line}
628 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
634 cp ${BUILDTMP}/cramfs.img $tmp
643 #################### Create USB CRAMFS based image
644 function build_usb_cramfs() {
645 local usb="$1" ; shift
648 prepare_cramfs "$custom"
649 echo "* Creating USB CRAMFS based image"
651 let vfat_size=${cramfs_size}+$FREE_SPACE
653 # Make VFAT filesystem for USB
654 mkfs.vfat -C "$usb" $vfat_size
657 echo "* Populating USB with overlay images and cramfs"
658 mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
659 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
661 # Use syslinux instead of isolinux to make the image bootable
662 tmp="${BUILDTMP}/syslinux.cfg"
664 ${console_serial_line}
666 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
672 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
675 echo "* Making USB CRAMFS based image bootable"
679 #################### map on all types provided on the command-line and invoke one of the above functions
680 function build_types () {
682 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
684 # alter output filename to reflect serial settings
685 if [ -n "$IS_SERIAL" ] ; then
686 if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
689 serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
695 function type_to_name() {
698 s/usb_partition$/-partition.usb/;
700 s/usb_cramfs$/-cramfs.usb/;
701 s/iso_cramfs$/-cramfs.iso/;
708 tname=`type_to_name $t`
709 # if -o is specified (as it has no default)
710 if [ -n "$OUTPUT_NAME" ] ; then
713 output="${OUTPUT_BASE}${serial}${tname}"
716 echo "*** Dealing with type=$arg"
717 echo '*' build_$t "$output" "$CUSTOM_DIR"
718 [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR"
725 parse_command_line "$@"
729 echo "* Building images for $FULL_VERSION_STRING"
730 # Do not tolerate errors
732 trap "do_cleanup" ERR INT EXIT
734 init_serial $CONSOLE_INFO