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=""
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
52 for x in iso usb usb_partition; do for c in "" "_cramfs" ; do
59 ALL_TYPES="$ALL_TYPES $t" ;;
63 #################### cleanup utilities
64 declare -a _CLEANUPS=()
65 function do_cleanup() {
66 cd / ; for i in "${_CLEANUPS[@]}"; do $i ; done
68 function push_cleanup() {
69 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
71 function pop_cleanup() {
72 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
75 #################### initialization
76 function init_and_check () {
78 # Change to our source directory
79 local srcdir=$(cd $(dirname $0) && pwd -P)
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 "You have to run prep.sh prior to calling $0 - exiting"
93 # build/version.txt written by prep.sh
94 BOOTCD_VERSION=$(cat build/version.txt)
96 if [ -f /etc/planetlab/plc_config ] ; then
97 # Source PLC configuration
98 . /etc/planetlab/plc_config
101 # From within a myplc chroot /usr/tmp is too small
102 # to build all possible images, whereas /data is part of the host
103 # filesystem and usually has sufficient space. What we
104 # should do is check whether the expected amount of space
107 if [ -d /data/tmp ] ; then
108 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
109 if [ -n "$isreadonly" ] ; then
115 FULL_VERSION_STRING="${PLC_NAME} BootCD ${BOOTCD_VERSION}"
120 # the custom-dir feature is designed to let a myplc try/ship a patched bootcd
121 # without the need for a full devel environment
122 # for example, you would create /root/custom-bootcd/etc/rc.d/init.d/pl_hwinit
123 # and run this script with -C /root/custom-bootcd
124 # this creates a third .img image of the custom dir, that 'hides' the files from
125 # bootcd.img in the resulting unionfs
126 # it seems that this feature has not been used nor tested in a long time, use with care
129 echo "Usage: build.sh [OPTION]..."
130 echo " -f plnode.txt Node to customize CD for (default: none)"
131 echo " -t 'types' Build the specified images (default: $DEFAULT_TYPES)"
132 echo " NOTE: mentioning 'serial' as part of the type is not supported anymore"
133 echo " -a Build all known types as listed below"
134 echo " -s console-info Enable a serial line as console and also bring up getty on that line"
135 echo " console-info: tty:baud-rate:parity:bits"
136 echo " or 'default' shortcut for $SERIAL_CONSOLE"
137 echo " -S equivalent to -s default"
138 echo " -O output-base The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
139 echo " useful when multiple types are provided"
140 echo " can be a full path"
141 echo " -o output-name The full name of the generated file"
142 echo " -C custom-dir Custom directory"
143 echo " -n Dry run - mostly for debug/test purposes"
144 echo " -k Add additional parameters to the kargs.txt file"
145 echo " -h This message"
146 echo "All known types: $ALL_TYPES"
151 function parse_command_line () {
156 while getopts "f:t:as:SO:o:C:k:nh" opt ; do
158 f) NODE_CONFIGURATION_FILE=$OPTARG ;;
159 t) TYPES="$TYPES $OPTARG" ;;
160 a) TYPES="$ALL_TYPES" ;;
161 s) CONSOLE_INFO="$OPTARG" ;;
162 S) CONSOLE_INFO=$SERIAL_CONSOLE ;;
163 O) OUTPUT_BASE="$OPTARG" ;;
164 o) OUTPUT_NAME="$OPTARG" ;;
165 C) CUSTOM_DIR="$OPTARG" ;;
166 k) KARGS_STR="$OPTARG" ;;
172 # use defaults if not set
173 [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
174 [ "$CONSOLE_INFO" == "default" ] && CONSOLE_INFO=$SERIAL_CONSOLE
177 local matcher="XXX$(echo $ALL_TYPES | sed -e 's,\W,XXX,g')XXX"
179 echo Checking type $t
180 echo $matcher | grep XXX${t}XXX &> /dev/null
181 if [ "$?" != 0 ] ; then
190 function init_serial () {
191 local console=$1; shift
192 if [ "$console" == "$GRAPHIC_CONSOLE" ] ; then
195 echo "Standard, graphic, non-serial mode"
198 console_dev=$(echo "$console" | awk -F: ' {print $1}')
199 console_baud=$(echo "$console" | awk -F: ' {print $2}')
200 [ -z "$console_baud" ] && console_baud="115200"
201 local console_parity=$(echo "$console" | awk -F: ' {print $3}')
202 [ -z "$console_parity" ] && console_parity="n"
203 local console_bits=$(echo "$console" | awk -F: ' {print $4}')
204 [ -z "$console_bits" ] && console_bits="8"
205 console_spec="console=${console_dev},${console_baud}${console_parity}${console_bits}"
206 local tty_nb=$(echo $console_dev | sed -e 's,[a-zA-Z],,g')
207 console_serial_line="SERIAL ${tty_nb} ${console_baud}"
209 echo "console_serial_line=${console_serial_line}"
210 echo "console_spec=${console_spec}"
214 #################### run once : build the overlay image
215 function build_overlay () {
217 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
218 push_cleanup rm -fr "${BUILDTMP}"
219 mkdir "${BUILDTMP}/isofs"
220 for i in "$ISOREF"/isofs/{bootcd.img,kernel}; do
221 ln -s "$i" "${BUILDTMP}/isofs"
223 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
224 ISOFS="${BUILDTMP}/isofs"
226 # Root of the ISO and USB images
227 echo "* Populating root filesystem..."
228 OVERLAY="${BUILDTMP}/overlay"
229 install -d -m 755 $OVERLAY
230 push_cleanup rm -fr $OVERLAY
232 # Create version files
233 echo "* Creating version files"
235 # Boot Manager compares pl_version in both places to make sure that
236 # the right CD is mounted. We used to boot from an initrd and mount
237 # the CD on /usr. Now we just run everything out of the initrd.
238 for file in $OVERLAY/pl_version $OVERLAY/usr/isolinux/pl_version ; do
239 mkdir -p $(dirname $file)
240 echo "$FULL_VERSION_STRING" >$file
243 # Install boot server configuration files
244 echo "* Installing boot server configuration files"
246 # We always intended to bring up and support backup boot servers,
247 # but never got around to it. Just install the same parameters for
249 for dir in $OVERLAY/usr/boot $OVERLAY/usr/boot/backup ; do
250 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
251 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
252 echo "$PLC_BOOT_HOST" >$dir/boot_server
253 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
254 echo "/boot/" >$dir/boot_server_path
257 # Install old-style boot server configuration files
258 # as opposed to what a former comment suggested,
259 # this is still required, somewhere in the bootmanager apparently
260 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $OVERLAY/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
261 echo "$FULL_VERSION_STRING" >$OVERLAY/usr/bootme/ID
262 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER
263 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER_IP
264 echo "$PLC_BOOT_SSL_PORT" >$OVERLAY/usr/bootme/BOOTPORT
266 # Generate /etc/issue
267 echo "* Generating /etc/issue"
269 if [ "$PLC_WWW_PORT" = "443" ] ; then
270 PLC_WWW_URL="https://$PLC_WWW_HOST/"
271 elif [ "$PLC_WWW_PORT" != "80" ] ; then
272 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
274 PLC_WWW_URL="http://$PLC_WWW_HOST/"
277 mkdir -p $OVERLAY/etc
278 cat >$OVERLAY/etc/issue <<EOF
284 This machine is a node in the $PLC_NAME distributed network. It has
285 not fully booted yet. If you have cancelled the boot process at the
286 request of $PLC_NAME Support, please follow the instructions provided
287 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
289 Console login at this point is restricted to root. Provide the root
290 password of the default $PLC_NAME Central administrator account at the
291 time that this CD was created.
296 echo "* Setting root password"
298 if [ -z "$ROOT_PASSWORD" ] ; then
299 # Generate an encrypted password with crypt() if not defined
300 # in a static configuration.
301 ROOT_PASSWORD=$(python <<EOF
302 import crypt, random, string
303 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
304 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
309 # build/passwd copied out by prep.sh
310 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd >$OVERLAY/etc/passwd
312 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
313 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
314 echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
315 install -D -m 644 $NODE_CONFIGURATION_FILE $OVERLAY/usr/boot/plnode.txt
318 if [ -n "$IS_SERIAL" ] ; then
319 KARGS_STR="$KARGS_STR ${console_spec}"
322 if [ -n "$KARGS_STR" ] ; then
323 echo "$KARGS_STR" > $OVERLAY/kargs.txt
326 # Pack overlay files into a compressed archive
327 echo "* Compressing overlay image"
328 (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
333 if [ -n "$CUSTOM_DIR" ]; then
334 echo "* Compressing custom image"
335 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
338 # Calculate ramdisk size (total uncompressed size of both archives)
339 ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
340 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
342 echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
347 #################### plain ISO
348 function build_iso() {
349 local iso="$1" ; shift
352 # Write isolinux configuration
353 cat >$ISOFS/isolinux.cfg <<EOF
354 ${console_serial_line}
356 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${console_spec}
363 echo "* Creating ISO image"
369 #################### USB with partitions
370 function build_usb_partition() {
371 echo -n "* Creating USB image with partitions..."
372 local usb="$1" ; shift
375 local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
376 size=$(( $size / 1024 ))
380 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
381 local offset=$(( $sectors*512 ))
383 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
385 cat >${BUILDTMP}/mtools.conf<<EOF
394 # environment variable for mtools
395 export MTOOLSRC="${BUILDTMP}/mtools.conf"
397 ### COPIED FROM build_usb() below!!!!
398 echo -n " populating USB image... "
399 mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
401 # Use syslinux instead of isolinux to make the image bootable
402 tmp="${BUILDTMP}/syslinux.cfg"
404 ${console_serial_line}
406 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${console_spec}
411 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
412 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
414 rm -f "${BUILDTMP}/mtools.conf"
417 echo "making USB image bootable."
418 syslinux -o $offset "$usb"
422 #################### plain USB
423 function build_usb() {
424 echo -n "* Creating USB image... "
425 local usb="$1" ; shift
428 mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
431 echo -n " populating USB image... "
432 mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
434 # Use syslinux instead of isolinux to make the image bootable
435 tmp="${BUILDTMP}/syslinux.cfg"
437 ${console_serial_line}
439 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${console_spec}
444 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
445 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
448 echo "making USB image bootable."
452 #################### utility to setup CRAMFS related support
453 function prepare_cramfs() {
454 [ -n "$CRAMFS_PREPARED" ] && return 0
457 echo "* Setting up CRAMFS-based images"
458 local tmp="${BUILDTMP}/cramfs-tree"
460 push_cleanup rm -rf $tmp
462 gzip -d -c $ISOFS/bootcd.img | cpio -diu
463 gzip -d -c $ISOFS/overlay.img | cpio -diu
464 [ -n "$custom" ] && \
465 gzip -d -c $ISOFS/custom.img | cpio -diu
467 # clean out unnecessary rpm lib
468 echo "* clearing var/lib/rpm/*"
471 # bootcd requires this directory
472 mkdir -p mnt/confdevice
474 # relocate various directory to /tmp
476 ln -fs /tmp/root root
477 ln -fs /sbin/init linuxrc
478 ln -fs /tmp/resolv.conf etc/resolv.conf
479 ln -fs /tmp/etc/mtab etc/mtab
481 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
483 echo "* renaming dirs in ./etc"
485 for dir in `find * -type d -prune | grep -v rc.d`; do
487 ln -fs /tmp/etc/${dir} ${dir}
491 echo "* renaming dirs in ./var"
492 # rename all top-level directories and put in a symlink to /tmp/var
494 for dir in `find * -type d -prune`; do
496 ln -fs /tmp/var/${dir} ${dir}
500 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
501 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
503 cat >./etc/fstab <<EOF
504 /dev/ram0 / cramfs ro 0 0
505 none /dev/pts devpts gid=5,mode=620 0 0
506 none /proc proc defaults 0 0
507 none /sys sysfs defaults 0 0
513 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
514 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
519 # update etc/inittab to start with pl_rsysinit
520 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
522 # modify inittab to have a serial console
523 if [ -n "$serial" ] ; then
524 echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
525 # and let root log in
526 echo "$console_dev" >> etc/securetty
529 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
530 etcsize=$(du -s ./etc | awk '{ print $1 }')
531 varsize=$(du -s ./var | awk '{ print $1 }')
532 let msize=($varsize+$etcsize+8192)/1024
534 # make dhclient happy
535 for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
536 ln -fs /tmp/etc/resolv.conf etc/resolv.conf
537 ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
539 # generate pl_rsysinit
540 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
542 # generated by build.sh
543 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
544 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
547 touch /tmp/etc/resolv.conf
552 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
554 # copy over directory contents of all _o directories from /etc and /var
555 # /tmp/etc and /tmp/var
557 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
560 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
565 # hand over to pl_sysinit
566 echo "pl_rsysinit: handing over to pl_sysinit"
567 /etc/init.d/pl_sysinit
569 chmod +x etc/rc.d/init.d/pl_rsysinit
573 # create the cramfs image
574 echo "* Creating cramfs image"
575 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
576 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
581 #################### Create ISO CRAMFS image
582 function build_iso_cramfs() {
583 local iso="$1" ; shift
586 prepare_cramfs "$custom"
587 echo "* Creating ISO CRAMFS-based image"
589 local tmp="${BUILDTMP}/cramfs-iso"
591 push_cleanup rm -rf $tmp
592 (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
593 cat >$tmp/isolinux.cfg <<EOF
594 ${console_serial_line}
596 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${console_spec}
602 cp ${BUILDTMP}/cramfs.img $tmp
611 #################### Create USB CRAMFS based image
612 function build_usb_cramfs() {
613 local usb="$1" ; shift
616 prepare_cramfs "$custom"
617 echo "* Creating USB CRAMFS based image"
619 let vfat_size=${cramfs_size}+$FREE_SPACE
621 # Make VFAT filesystem for USB
622 mkfs.vfat -C "$usb" $vfat_size
625 echo "* Populating USB with overlay images and cramfs"
626 mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
627 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
629 # Use syslinux instead of isolinux to make the image bootable
630 tmp="${BUILDTMP}/syslinux.cfg"
632 ${console_serial_line}
634 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${console_spec}
640 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
643 echo "* Making USB CRAMFS based image bootable"
647 #################### map on all types provided on the command-line and invoke one of the above functions
648 function build_types () {
650 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
652 # alter output filename to reflect serial settings
653 if [ -n "$IS_SERIAL" ] ; then
654 if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
657 serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
663 function type_to_name() {
666 s/usb_partition$/-partition.usb/;
668 s/usb_cramfs$/-cramfs.usb/;
669 s/iso_cramfs$/-cramfs.iso/;
676 tname=`type_to_name $t`
677 # if -o is specified (as it has no default)
678 if [ -n "$OUTPUT_NAME" ] ; then
681 output="${OUTPUT_BASE}${serial}${tname}"
684 echo "*** Dealing with type=$arg"
685 echo '*' build_$t "$output" "$CUSTOM_DIR"
686 [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR"
695 parse_command_line "$@"
697 echo "* Building images for $FULL_VERSION_STRING"
698 # Do not tolerate errors
700 trap "do_cleanup" ERR INT EXIT
702 init_serial $CONSOLE_INFO