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 COMMAND=$(basename $0)
15 PATH=/sbin:/bin:/usr/sbin:/usr/bin
18 DEFAULT_TYPES="usb iso"
19 # Leave 4 MB of free space
20 GRAPHIC_CONSOLE="graphic"
21 SERIAL_CONSOLE="ttyS0:115200:n:8"
22 CONSOLE_INFO=$GRAPHIC_CONSOLE
23 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
26 # command-line settable args
27 NODE_CONFIGURATION_FILE=
37 FULL_VERSION_STRING=""
45 console_serial_line=""
49 #################### compute all supported types
50 # removing support for serial in the type
51 # this is because kargs.txt goes in the overlay, that is computed only once
52 # so we cannot handle serial and graphic modes within the same invokation of this script
55 for x in iso usb usb_partition; do for c in "" "_cramfs" ; do
62 ALL_TYPES="$ALL_TYPES $t" ;;
66 #################### cleanup utilities
67 declare -a _CLEANUPS=()
68 function do_cleanup() {
69 cd / ; for i in "${_CLEANUPS[@]}"; do $i ; done
71 function push_cleanup() {
72 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
74 function pop_cleanup() {
75 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
78 #################### initialization
79 function init_and_check () {
81 # Change to our source directory
82 local srcdir=$(cd $DIRNAME && pwd -P)
86 ISOREF=$PWD/${VARIANT}
88 # The reference image is expected to have been built by prep.sh (see .spec)
89 # we disable the initial logic that called prep.sh if that was not the case
90 # this is because prep.sh needs to know pldistro
91 if [ ! -f $ISOREF/isofs/bootcd.img -o ! -f $ISOREF/version.txt ] ; then
92 echo "Could not find isofs and version.txt in $ISOREF"
93 if [ "$VARIANT" == "build" ] ; then
94 echo "You have to run prep.sh prior to calling $COMMAND"
96 echo "You need to create your variant image, see kvariant.sh"
102 # build/version.txt written by prep.sh
103 BOOTCD_VERSION=$(cat ${VARIANT}/version.txt)
105 if [ -f /etc/planetlab/plc_config ] ; then
106 # Source PLC configuration
107 . /etc/planetlab/plc_config
110 # use /var/tmp that should be large enough on both chroot- or vserver-based myplc
113 FULL_VERSION_STRING="${PLC_NAME} BootCD ${BOOTCD_VERSION}"
118 # the custom-dir feature is designed to let a myplc try/ship a patched bootcd
119 # without the need for a full devel environment
120 # for example, you would create /root/custom-bootcd/etc/rc.d/init.d/pl_hwinit
121 # and run this script with -C /root/custom-bootcd
122 # this creates a third .img image of the custom dir, that 'hides' the files from
123 # bootcd.img in the resulting unionfs
124 # it seems that this feature has not been used nor tested in a long time, use with care
127 echo "Usage: $COMMAND [OPTION]..."
128 echo " -f plnode.txt Node to customize CD for (default: none)"
129 echo " -t 'types' Build the specified images (default: $DEFAULT_TYPES)"
130 echo " NOTE: mentioning 'serial' as part of the type is not supported anymore"
131 echo " -a Build all known types as listed below"
132 echo " -s console-info Enable a serial line as console and also bring up getty on that line"
133 echo " console-info: tty:baud-rate:parity:bits"
134 echo " or 'default' shortcut for $SERIAL_CONSOLE"
135 echo " -S equivalent to -s default"
136 echo " -O output-base The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
137 echo " useful when multiple types are provided"
138 echo " can be a full path"
139 echo " -o output-name The full name of the generated file"
140 echo " -C custom-dir Custom directory"
141 echo " -V variant Use a variant - see kvariant.sh"
142 echo " -n Dry run - mostly for debug/test purposes"
143 echo " -k Add additional parameters to the kargs.txt file"
144 echo " -h This message"
145 echo "All known types: $ALL_TYPES"
150 function parse_command_line () {
155 while getopts "f:t:as:SO:o:C:V:k:nh" opt ; do
157 f) NODE_CONFIGURATION_FILE=$OPTARG ;;
158 t) TYPES="$TYPES $OPTARG" ;;
159 a) TYPES="$ALL_TYPES" ;;
160 s) CONSOLE_INFO="$OPTARG" ;;
161 S) CONSOLE_INFO=$SERIAL_CONSOLE ;;
162 O) OUTPUT_BASE="$OPTARG" ;;
163 o) OUTPUT_NAME="$OPTARG" ;;
164 C) CUSTOM_DIR="$OPTARG" ;;
165 V) VARIANT="$OPTARG" ;;
166 k) KARGS_STR="$KARGS_STR $OPTARG" ;;
172 # use defaults if not set
173 [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
174 [ -z "$VARIANT" ] && VARIANT="build"
175 [ "$CONSOLE_INFO" == "default" ] && CONSOLE_INFO=$SERIAL_CONSOLE
177 if [ -n "$NODE_CONFIGURATION_FILE" ] ; then
178 # check existence of NODE_CONFIGURATION_FILE and normalize as we will change directory
179 if [ ! -f "$NODE_CONFIGURATION_FILE" ] ; then
180 echo "Node configuration file $NODE_CONFIGURATION_FILE not found - exiting"
183 cf_dir="$(dirname $NODE_CONFIGURATION_FILE)"
184 cf_dir="$(cd $cf_dir; pwd -P)"
185 cf_file="$(basename $NODE_CONFIGURATION_FILE)"
186 NODE_CONFIGURATION_FILE="$cf_dir"/"$cf_file"
190 local matcher="XXX$(echo $ALL_TYPES | sed -e 's,\W,XXX,g')XXX"
192 echo Checking type $t
193 echo $matcher | grep XXX${t}XXX &> /dev/null
194 if [ "$?" != 0 ] ; then
203 function init_serial () {
204 local console=$1; shift
205 if [ "$console" == "$GRAPHIC_CONSOLE" ] ; then
208 echo "Standard, graphic, non-serial mode"
211 console_dev=$(echo "$console" | awk -F: ' {print $1}')
212 console_baud=$(echo "$console" | awk -F: ' {print $2}')
213 [ -z "$console_baud" ] && console_baud="115200"
214 local console_parity=$(echo "$console" | awk -F: ' {print $3}')
215 [ -z "$console_parity" ] && console_parity="n"
216 local console_bits=$(echo "$console" | awk -F: ' {print $4}')
217 [ -z "$console_bits" ] && console_bits="8"
218 console_spec="console=${console_dev},${console_baud}${console_parity}${console_bits}"
219 local tty_nb=$(echo $console_dev | sed -e 's,[a-zA-Z],,g')
220 console_serial_line="SERIAL ${tty_nb} ${console_baud}"
222 echo "console_serial_line=${console_serial_line}"
223 echo "console_spec=${console_spec}"
227 #################### run once : build the overlay image
228 function build_overlay () {
230 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
231 push_cleanup rm -fr "${BUILDTMP}"
232 mkdir "${BUILDTMP}/isofs"
233 for i in "$ISOREF"/isofs/{bootcd.img,kernel}; do
234 ln -s "$i" "${BUILDTMP}/isofs"
236 # the syslinux that comes with f12 has this file in a new location
237 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs" \
238 || cp "/usr/share/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
239 ISOFS="${BUILDTMP}/isofs"
241 # Root of the ISO and USB images
242 echo "* Populating root filesystem..."
243 OVERLAY="${BUILDTMP}/overlay"
244 install -d -m 755 $OVERLAY
245 push_cleanup rm -fr $OVERLAY
247 # Create version files
248 echo "* Creating version files"
250 # Boot Manager compares pl_version in both places to make sure that
251 # the right CD is mounted. We used to boot from an initrd and mount
252 # the CD on /usr. Now we just run everything out of the initrd.
253 for file in $OVERLAY/pl_version $OVERLAY/usr/isolinux/pl_version ; do
254 mkdir -p $(dirname $file)
255 echo "$FULL_VERSION_STRING" >$file
258 # Install boot server configuration files
259 echo "* Installing boot server configuration files"
261 # We always intended to bring up and support backup boot servers,
262 # but never got around to it. Just install the same parameters for
264 for dir in $OVERLAY/usr/boot $OVERLAY/usr/boot/backup ; do
265 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
266 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
267 echo "$PLC_BOOT_HOST" >$dir/boot_server
268 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
269 echo "/boot/" >$dir/boot_server_path
272 # Install old-style boot server configuration files
273 # as opposed to what a former comment suggested,
274 # this is still required, somewhere in the bootmanager apparently
275 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $OVERLAY/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
276 echo "$FULL_VERSION_STRING" >$OVERLAY/usr/bootme/ID
277 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER
278 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER_IP
279 echo "$PLC_BOOT_SSL_PORT" >$OVERLAY/usr/bootme/BOOTPORT
281 # Generate /etc/issue
282 echo "* Generating /etc/issue"
284 if [ "$PLC_WWW_PORT" = "443" ] ; then
285 PLC_WWW_URL="https://$PLC_WWW_HOST/"
286 elif [ "$PLC_WWW_PORT" != "80" ] ; then
287 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
289 PLC_WWW_URL="http://$PLC_WWW_HOST/"
292 mkdir -p $OVERLAY/etc
293 cat >$OVERLAY/etc/issue <<EOF
299 This machine is a node in the $PLC_NAME distributed network. It has
300 not fully booted yet. If you have cancelled the boot process at the
301 request of $PLC_NAME Support, please follow the instructions provided
302 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
304 Console login at this point is restricted to root. Provide the root
305 password of the default $PLC_NAME Central administrator account at the
306 time that this CD was created.
311 echo "* Setting root password"
313 if [ -z "$ROOT_PASSWORD" ] ; then
314 # Generate an encrypted password with crypt() if not defined
315 # in a static configuration.
316 ROOT_PASSWORD=$(python <<EOF
317 import crypt, random, string
318 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
319 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
324 # build/passwd copied out by prep.sh
325 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" ${VARIANT}/passwd >$OVERLAY/etc/passwd
327 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
328 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
329 echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
330 install -D -m 644 $NODE_CONFIGURATION_FILE $OVERLAY/usr/boot/plnode.txt
331 NODE_ID=$(source $NODE_CONFIGURATION_FILE; echo $NODE_ID)
332 echo "* Building network configuration for $NODE_ID"
333 plnet -- --root $OVERLAY --files-only --program BootCD $NODE_ID
336 if [ -n "$IS_SERIAL" ] ; then
337 KARGS_STR="$KARGS_STR ${console_spec}"
340 if [ -n "$KARGS_STR" ] ; then
341 echo "$KARGS_STR" > $OVERLAY/kargs.txt
342 kernel_args=$KARGS_STR
345 # Pack overlay files into a compressed archive
346 echo "* Compressing overlay image"
347 (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
352 if [ -n "$CUSTOM_DIR" ]; then
353 echo "* Compressing custom image"
354 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
357 # Calculate ramdisk size (total uncompressed size of both archives)
358 ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
359 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
361 echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
366 #################### plain ISO
367 function build_iso() {
368 local iso="$1" ; shift
371 # Write isolinux configuration
372 cat >$ISOFS/isolinux.cfg <<EOF
373 ${console_serial_line}
375 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
382 echo "* Creating ISO image"
383 mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
386 #################### USB with partitions
387 function build_usb_partition() {
388 echo -n "* Creating USB image with partitions..."
389 local usb="$1" ; shift
392 local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
393 size=$(( $size / 1024 ))
397 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
398 local offset=$(( $sectors*512 ))
400 if [ -f /usr/lib/syslinux/mkdiskimage ] ; then
401 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
403 mkdiskimage -M -4 "$usb" $size $heads $sectors
406 cat >${BUILDTMP}/mtools.conf<<EOF
416 # environment variable for mtools
417 export MTOOLSRC="${BUILDTMP}/mtools.conf"
419 ### COPIED FROM build_usb() below!!!!
420 echo -n " populating USB image... "
421 mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
423 # Use syslinux instead of isolinux to make the image bootable
424 tmp="${BUILDTMP}/syslinux.cfg"
426 ${console_serial_line}
428 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
433 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
434 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
439 echo "making USB image bootable."
440 syslinux -o $offset "$usb"
444 #################### plain USB
445 function build_usb() {
446 echo -n "* Creating USB image... "
447 local usb="$1" ; shift
451 mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
453 cat >${BUILDTMP}/mtools.conf<<EOF
456 # environment variable for mtools
457 export MTOOLSRC="${BUILDTMP}/mtools.conf"
460 echo -n " populating USB image... "
461 mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
463 # Use syslinux instead of isolinux to make the image bootable
464 tmp="${BUILDTMP}/syslinux.cfg"
466 ${console_serial_line}
468 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
473 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
474 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
479 echo "making USB image bootable."
483 #################### utility to setup CRAMFS related support
484 function prepare_cramfs() {
485 [ -n "$CRAMFS_PREPARED" ] && return 0
488 echo "* Setting up CRAMFS-based images"
489 local tmp="${BUILDTMP}/cramfs-tree"
491 push_cleanup rm -rf $tmp
493 gzip -d -c $ISOFS/bootcd.img | cpio -diu
494 gzip -d -c $ISOFS/overlay.img | cpio -diu
495 [ -n "$custom" ] && \
496 gzip -d -c $ISOFS/custom.img | cpio -diu
498 # clean out unnecessary rpm lib
499 echo "* clearing var/lib/rpm/*"
502 # bootcd requires this directory
503 mkdir -p mnt/confdevice
505 # relocate various directory to /tmp
507 ln -fs /tmp/root root
508 ln -fs /sbin/init linuxrc
509 ln -fs /tmp/resolv.conf etc/resolv.conf
510 ln -fs /tmp/etc/mtab etc/mtab
512 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
514 echo "* renaming dirs in ./etc"
516 for dir in `find * -type d -prune | grep -v rc.d`; do
518 ln -fs /tmp/etc/${dir} ${dir}
522 echo "* renaming dirs in ./var"
523 # rename all top-level directories and put in a symlink to /tmp/var
525 for dir in `find * -type d -prune`; do
527 ln -fs /tmp/var/${dir} ${dir}
531 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
532 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
534 cat >./etc/fstab <<EOF
535 /dev/ram0 / cramfs ro 0 0
536 none /dev/pts devpts gid=5,mode=620 0 0
537 none /proc proc defaults 0 0
538 none /sys sysfs defaults 0 0
544 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
545 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
550 # update etc/inittab to start with pl_rsysinit
551 for file in etc/inittab etc/event.d/rcS etc/init/rcS.conf; do
552 [ -f $file ] && sed -i 's,pl_sysinit,pl_rsysinit,' $file
555 # modify inittab to have a serial console
556 # xxx this might well be broken with f12 and above xxx
557 if [ -n "$serial" ] ; then
558 echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
559 # and let root log in
560 echo "$console_dev" >> etc/securetty
563 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
564 etcsize=$(du -s ./etc | awk '{ print $1 }')
565 varsize=$(du -s ./var | awk '{ print $1 }')
566 let msize=($varsize+$etcsize+8192)/1024
568 # make dhclient happy
569 for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
570 ln -fs /tmp/etc/resolv.conf etc/resolv.conf
571 ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
573 # generate pl_rsysinit
574 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
576 # generated by $COMMAND
577 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
578 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
581 touch /tmp/etc/resolv.conf
586 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
588 # copy over directory contents of all _o directories from /etc and /var
589 # /tmp/etc and /tmp/var
591 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 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
599 # hand over to pl_sysinit
600 echo "pl_rsysinit: handing over to pl_sysinit"
601 /etc/init.d/pl_sysinit
603 chmod +x etc/rc.d/init.d/pl_rsysinit
607 # create the cramfs image
608 echo "* Creating cramfs image"
609 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
610 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
615 #################### Create ISO CRAMFS image
616 function build_iso_cramfs() {
617 local iso="$1" ; shift
620 prepare_cramfs "$custom"
621 echo "* Creating ISO CRAMFS-based image"
623 local tmp="${BUILDTMP}/cramfs-iso"
625 push_cleanup rm -rf $tmp
626 (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
627 cat >$tmp/isolinux.cfg <<EOF
628 ${console_serial_line}
630 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
636 cp ${BUILDTMP}/cramfs.img $tmp
645 #################### Create USB CRAMFS based image
646 function build_usb_cramfs() {
647 local usb="$1" ; shift
650 prepare_cramfs "$custom"
651 echo "* Creating USB CRAMFS based image"
653 let vfat_size=${cramfs_size}+$FREE_SPACE
655 # Make VFAT filesystem for USB
656 mkfs.vfat -C "$usb" $vfat_size
659 echo "* Populating USB with overlay images and cramfs"
660 mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
661 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
663 # Use syslinux instead of isolinux to make the image bootable
664 tmp="${BUILDTMP}/syslinux.cfg"
666 ${console_serial_line}
668 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
674 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
677 echo "* Making USB CRAMFS based image bootable"
681 #################### map on all types provided on the command-line and invoke one of the above functions
682 function build_types () {
684 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
686 # alter output filename to reflect serial settings
687 if [ -n "$IS_SERIAL" ] ; then
688 if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
691 serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
697 function type_to_name() {
700 s/usb_partition$/-partition.usb/;
702 s/usb_cramfs$/-cramfs.usb/;
703 s/iso_cramfs$/-cramfs.iso/;
710 tname=`type_to_name $t`
711 # if -o is specified (as it has no default)
712 if [ -n "$OUTPUT_NAME" ] ; then
715 output="${OUTPUT_BASE}${serial}${tname}"
718 echo "*** Dealing with type=$arg"
719 echo '*' build_$t "$output" "$CUSTOM_DIR"
720 [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR"
727 parse_command_line "$@"
731 echo "* Building images for $FULL_VERSION_STRING"
732 # Do not tolerate errors
734 trap "do_cleanup" ERR INT EXIT
736 init_serial $CONSOLE_INFO