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
308 if [ -n "$IS_SERIAL" ] ; then
309 KARGS_STR="$KARGS_STR ${console_spec}"
312 if [ -n "$KARGS_STR" ] ; then
313 echo "$KARGS_STR" > $OVERLAY/kargs.txt
314 kernel_args=$KARGS_STR
317 # Pack overlay files into a compressed archive
318 echo "* Compressing overlay image"
319 (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
324 if [ -n "$CUSTOM_DIR" ]; then
325 echo "* Compressing custom image"
326 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
329 # Calculate ramdisk size (total uncompressed size of both archives)
330 ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
331 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
333 echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
338 #################### plain ISO
339 function build_iso() {
340 local iso="$1" ; shift
343 # Write isolinux configuration
344 cat >$ISOFS/isolinux.cfg <<EOF
345 ${console_serial_line}
347 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
354 echo "* Creating ISO image"
355 mkisofs -o "$iso" $MKISOFS_OPTS $ISOFS
358 #################### USB with partitions
359 function build_usb_partition() {
360 echo -n "* Creating USB image with partitions..."
361 local usb="$1" ; shift
364 local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
365 size=$(( $size / 1024 ))
369 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
370 local offset=$(( $sectors*512 ))
372 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
374 cat >${BUILDTMP}/mtools.conf<<EOF
383 # environment variable for mtools
384 export MTOOLSRC="${BUILDTMP}/mtools.conf"
386 ### COPIED FROM build_usb() below!!!!
387 echo -n " populating USB image... "
388 mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
390 # Use syslinux instead of isolinux to make the image bootable
391 tmp="${BUILDTMP}/syslinux.cfg"
393 ${console_serial_line}
395 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${kernel_args}
400 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
401 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
403 rm -f "${BUILDTMP}/mtools.conf"
406 echo "making USB image bootable."
407 syslinux -o $offset "$usb"
411 #################### plain USB
412 function build_usb() {
413 echo -n "* Creating USB image... "
414 local usb="$1" ; shift
417 mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
420 echo -n " populating USB image... "
421 mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
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" ::/isolinux.cfg 2>/dev/null || :
434 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
437 echo "making USB image bootable."
441 #################### utility to setup CRAMFS related support
442 function prepare_cramfs() {
443 [ -n "$CRAMFS_PREPARED" ] && return 0
446 echo "* Setting up CRAMFS-based images"
447 local tmp="${BUILDTMP}/cramfs-tree"
449 push_cleanup rm -rf $tmp
451 gzip -d -c $ISOFS/bootcd.img | cpio -diu
452 gzip -d -c $ISOFS/overlay.img | cpio -diu
453 [ -n "$custom" ] && \
454 gzip -d -c $ISOFS/custom.img | cpio -diu
456 # clean out unnecessary rpm lib
457 echo "* clearing var/lib/rpm/*"
460 # bootcd requires this directory
461 mkdir -p mnt/confdevice
463 # relocate various directory to /tmp
465 ln -fs /tmp/root root
466 ln -fs /sbin/init linuxrc
467 ln -fs /tmp/resolv.conf etc/resolv.conf
468 ln -fs /tmp/etc/mtab etc/mtab
470 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
472 echo "* renaming dirs in ./etc"
474 for dir in `find * -type d -prune | grep -v rc.d`; do
476 ln -fs /tmp/etc/${dir} ${dir}
480 echo "* renaming dirs in ./var"
481 # rename all top-level directories and put in a symlink to /tmp/var
483 for dir in `find * -type d -prune`; do
485 ln -fs /tmp/var/${dir} ${dir}
489 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
490 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
492 cat >./etc/fstab <<EOF
493 /dev/ram0 / cramfs ro 0 0
494 none /dev/pts devpts gid=5,mode=620 0 0
495 none /proc proc defaults 0 0
496 none /sys sysfs defaults 0 0
502 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
503 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
508 # update etc/inittab to start with pl_rsysinit
509 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
511 # modify inittab to have a serial console
512 if [ -n "$serial" ] ; then
513 echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
514 # and let root log in
515 echo "$console_dev" >> etc/securetty
518 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
519 etcsize=$(du -s ./etc | awk '{ print $1 }')
520 varsize=$(du -s ./var | awk '{ print $1 }')
521 let msize=($varsize+$etcsize+8192)/1024
523 # make dhclient happy
524 for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
525 ln -fs /tmp/etc/resolv.conf etc/resolv.conf
526 ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
528 # generate pl_rsysinit
529 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
531 # generated by build.sh
532 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
533 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
536 touch /tmp/etc/resolv.conf
541 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
543 # copy over directory contents of all _o directories from /etc and /var
544 # /tmp/etc and /tmp/var
546 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
549 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
554 # hand over to pl_sysinit
555 echo "pl_rsysinit: handing over to pl_sysinit"
556 /etc/init.d/pl_sysinit
558 chmod +x etc/rc.d/init.d/pl_rsysinit
562 # create the cramfs image
563 echo "* Creating cramfs image"
564 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
565 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
570 #################### Create ISO CRAMFS image
571 function build_iso_cramfs() {
572 local iso="$1" ; shift
575 prepare_cramfs "$custom"
576 echo "* Creating ISO CRAMFS-based image"
578 local tmp="${BUILDTMP}/cramfs-iso"
580 push_cleanup rm -rf $tmp
581 (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
582 cat >$tmp/isolinux.cfg <<EOF
583 ${console_serial_line}
585 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
591 cp ${BUILDTMP}/cramfs.img $tmp
600 #################### Create USB CRAMFS based image
601 function build_usb_cramfs() {
602 local usb="$1" ; shift
605 prepare_cramfs "$custom"
606 echo "* Creating USB CRAMFS based image"
608 let vfat_size=${cramfs_size}+$FREE_SPACE
610 # Make VFAT filesystem for USB
611 mkfs.vfat -C "$usb" $vfat_size
614 echo "* Populating USB with overlay images and cramfs"
615 mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
616 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
618 # Use syslinux instead of isolinux to make the image bootable
619 tmp="${BUILDTMP}/syslinux.cfg"
621 ${console_serial_line}
623 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
629 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
632 echo "* Making USB CRAMFS based image bootable"
636 #################### map on all types provided on the command-line and invoke one of the above functions
637 function build_types () {
639 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
641 # alter output filename to reflect serial settings
642 if [ -n "$IS_SERIAL" ] ; then
643 if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
646 serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
652 function type_to_name() {
655 s/usb_partition$/-partition.usb/;
657 s/usb_cramfs$/-cramfs.usb/;
658 s/iso_cramfs$/-cramfs.iso/;
665 tname=`type_to_name $t`
666 # if -o is specified (as it has no default)
667 if [ -n "$OUTPUT_NAME" ] ; then
670 output="${OUTPUT_BASE}${serial}${tname}"
673 echo "*** Dealing with type=$arg"
674 echo '*' build_$t "$output" "$CUSTOM_DIR"
675 [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR"
684 parse_command_line "$@"
686 echo "* Building images for $FULL_VERSION_STRING"
687 # Do not tolerate errors
689 trap "do_cleanup" ERR INT EXIT
691 init_serial $CONSOLE_INFO