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=
34 FULL_VERSION_STRING=""
42 console_serial_line=""
45 #################### compute all supported types
46 # removing support for serial in the type
47 # this is because kargs.txt goes in the overlay, that is computed only once
48 # so we cannot handle serial and graphic modes within the same invokation of this script
51 for x in iso usb usb_partition; do for c in "" "_cramfs" ; do
58 ALL_TYPES="$ALL_TYPES $t" ;;
62 #################### cleanup utilities
63 declare -a _CLEANUPS=()
64 function do_cleanup() {
65 cd / ; for i in "${_CLEANUPS[@]}"; do $i ; done
67 function push_cleanup() {
68 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
70 function pop_cleanup() {
71 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
74 #################### initialization
75 function init_and_check () {
77 # Change to our source directory
78 local srcdir=$(cd $(dirname $0) && pwd -P)
84 # The reference image is expected to have been built by prep.sh (see .spec)
85 # we disable the initial logic that called prep.sh if that was not the case
86 # this is because prep.sh needs to know pldistro
87 if [ ! -f $ISOREF/isofs/bootcd.img -o ! -f $ISOREF/version.txt ] ; then
88 echo "You have to run prep.sh prior to calling $0 - exiting"
92 # build/version.txt written by prep.sh
93 BOOTCD_VERSION=$(cat build/version.txt)
95 if [ -f /etc/planetlab/plc_config ] ; then
96 # Source PLC configuration
97 . /etc/planetlab/plc_config
100 # From within a myplc chroot /usr/tmp is too small
101 # to build all possible images, whereas /data is part of the host
102 # filesystem and usually has sufficient space. What we
103 # should do is check whether the expected amount of space
106 if [ -d /data/tmp ] ; then
107 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
108 if [ -n "$isreadonly" ] ; then
114 FULL_VERSION_STRING="${PLC_NAME} BootCD ${BOOTCD_VERSION}"
119 # the custom-dir feature is designed to let a myplc try/ship a patched bootcd
120 # without the need for a full devel environment
121 # for example, you would create /root/custom-bootcd/etc/rc.d/init.d/pl_hwinit
122 # and run this script with -C /root/custom-bootcd
123 # this creates a third .img image of the custom dir, that 'hides' the files from
124 # bootcd.img in the resulting unionfs
125 # it seems that this feature has not been used nor tested in a long time, use with care
128 echo "Usage: build.sh [OPTION]..."
129 echo " -f plnode.txt Node to customize CD for (default: none)"
130 echo " -t 'types' Build the specified images (default: $DEFAULT_TYPES)"
131 echo " NOTE: mentioning 'serial' as part of the type is not supported anymore"
132 echo " -a Build all known types as listed below"
133 echo " -s console-info Enable a serial line as console and also bring up getty on that line"
134 echo " console-info: tty:baud-rate:parity:bits"
135 echo " or 'default' shortcut for $SERIAL_CONSOLE"
136 echo " -S equivalent to -s default"
137 echo " -O output-base The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
138 echo " useful when multiple types are provided"
139 echo " can be a full path"
140 echo " -o output-name The full name of the generated file"
141 echo " -C custom-dir Custom directory"
142 echo " -n Dry run - mostly for debug/test purposes"
143 echo " -h This message"
144 echo "All known types: $ALL_TYPES"
149 function parse_command_line () {
154 while getopts "f:t:as:SO:o:C:nh" opt ; do
156 f) NODE_CONFIGURATION_FILE=$OPTARG ;;
157 t) TYPES="$TYPES $OPTARG" ;;
158 a) TYPES="$ALL_TYPES" ;;
159 s) CONSOLE_INFO="$OPTARG" ;;
160 S) CONSOLE_INFO=$SERIAL_CONSOLE ;;
161 O) OUTPUT_BASE="$OPTARG" ;;
162 o) OUTPUT_NAME="$OPTARG" ;;
163 C) CUSTOM_DIR="$OPTARG" ;;
169 # use defaults if not set
170 [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
171 [ "$CONSOLE_INFO" == "default" ] && CONSOLE_INFO=$SERIAL_CONSOLE
174 local matcher="XXX$(echo $ALL_TYPES | sed -e 's,\W,XXX,g')XXX"
176 echo Checking type $t
177 echo $matcher | grep XXX${t}XXX &> /dev/null
178 if [ "$?" != 0 ] ; then
187 function init_serial () {
188 local console=$1; shift
189 if [ "$console" == "$GRAPHIC_CONSOLE" ] ; then
192 echo "Standard, graphic, non-serial mode"
195 console_dev=$(echo "$console" | awk -F: ' {print $1}')
196 console_baud=$(echo "$console" | awk -F: ' {print $2}')
197 [ -z "$console_baud" ] && console_baud="115200"
198 local console_parity=$(echo "$console" | awk -F: ' {print $3}')
199 [ -z "$console_parity" ] && console_parity="n"
200 local console_bits=$(echo "$console" | awk -F: ' {print $4}')
201 [ -z "$console_bits" ] && console_bits="8"
202 console_spec="console=${console_dev},${console_baud}${console_parity}${console_bits}"
203 local tty_nb=$(echo $console_dev | sed -e 's,[a-zA-Z],,g')
204 console_serial_line="SERIAL ${tty_nb} ${console_baud}"
206 echo "console_serial_line=${console_serial_line}"
207 echo "console_spec=${console_spec}"
211 #################### run once : build the overlay image
212 function build_overlay () {
214 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
215 push_cleanup rm -fr "${BUILDTMP}"
216 mkdir "${BUILDTMP}/isofs"
217 for i in "$ISOREF"/isofs/{bootcd.img,kernel}; do
218 ln -s "$i" "${BUILDTMP}/isofs"
220 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
221 ISOFS="${BUILDTMP}/isofs"
223 # Root of the ISO and USB images
224 echo "* Populating root filesystem..."
225 OVERLAY="${BUILDTMP}/overlay"
226 install -d -m 755 $OVERLAY
227 push_cleanup rm -fr $OVERLAY
229 # Create version files
230 echo "* Creating version files"
232 # Boot Manager compares pl_version in both places to make sure that
233 # the right CD is mounted. We used to boot from an initrd and mount
234 # the CD on /usr. Now we just run everything out of the initrd.
235 for file in $OVERLAY/pl_version $OVERLAY/usr/isolinux/pl_version ; do
236 mkdir -p $(dirname $file)
237 echo "$FULL_VERSION_STRING" >$file
240 # Install boot server configuration files
241 echo "* Installing boot server configuration files"
243 # We always intended to bring up and support backup boot servers,
244 # but never got around to it. Just install the same parameters for
246 for dir in $OVERLAY/usr/boot $OVERLAY/usr/boot/backup ; do
247 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
248 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
249 echo "$PLC_BOOT_HOST" >$dir/boot_server
250 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
251 echo "/boot/" >$dir/boot_server_path
254 # Install old-style boot server configuration files
255 # as opposed to what a former comment suggested,
256 # this is still required, somewhere in the bootmanager apparently
257 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $OVERLAY/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
258 echo "$FULL_VERSION_STRING" >$OVERLAY/usr/bootme/ID
259 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER
260 echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER_IP
261 echo "$PLC_BOOT_SSL_PORT" >$OVERLAY/usr/bootme/BOOTPORT
263 # Generate /etc/issue
264 echo "* Generating /etc/issue"
266 if [ "$PLC_WWW_PORT" = "443" ] ; then
267 PLC_WWW_URL="https://$PLC_WWW_HOST/"
268 elif [ "$PLC_WWW_PORT" != "80" ] ; then
269 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
271 PLC_WWW_URL="http://$PLC_WWW_HOST/"
274 mkdir -p $OVERLAY/etc
275 cat >$OVERLAY/etc/issue <<EOF
281 This machine is a node in the $PLC_NAME distributed network. It has
282 not fully booted yet. If you have cancelled the boot process at the
283 request of $PLC_NAME Support, please follow the instructions provided
284 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
286 Console login at this point is restricted to root. Provide the root
287 password of the default $PLC_NAME Central administrator account at the
288 time that this CD was created.
293 echo "* Setting root password"
295 if [ -z "$ROOT_PASSWORD" ] ; then
296 # Generate an encrypted password with crypt() if not defined
297 # in a static configuration.
298 ROOT_PASSWORD=$(python <<EOF
299 import crypt, random, string
300 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
301 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
306 # build/passwd copied out by prep.sh
307 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd >$OVERLAY/etc/passwd
309 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
310 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
311 echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
312 install -D -m 644 $NODE_CONFIGURATION_FILE $OVERLAY/usr/boot/plnode.txt
315 if [ -n "$IS_SERIAL" ] ; then
316 echo "${console_spec}" > $OVERLAY/kargs.txt
319 # Pack overlay files into a compressed archive
320 echo "* Compressing overlay image"
321 (cd $OVERLAY && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/overlay.img
326 if [ -n "$CUSTOM_DIR" ]; then
327 echo "* Compressing custom image"
328 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$ISOFS/custom.img
331 # Calculate ramdisk size (total uncompressed size of both archives)
332 ramdisk_size=$(gzip -l $ISOFS/bootcd.img $ISOFS/overlay.img ${CUSTOM_DIR:+$ISOFS/custom.img} | tail -1 | awk '{ print $2; }') # bytes
333 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
335 echo "$FULL_VERSION_STRING" >$ISOFS/pl_version
340 #################### plain ISO
341 function build_iso() {
342 local iso="$1" ; shift
345 # Write isolinux configuration
346 cat >$ISOFS/isolinux.cfg <<EOF
347 ${console_serial_line}
349 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${console_spec}
356 echo "* Creating ISO image"
362 #################### USB with partitions
363 function build_usb_partition() {
364 echo -n "* Creating USB image with partitions..."
365 local usb="$1" ; shift
368 local size=$(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
369 size=$(( $size / 1024 ))
373 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
374 local offset=$(( $sectors*512 ))
376 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
378 cat >${BUILDTMP}/mtools.conf<<EOF
387 # environment variable for mtools
388 export MTOOLSRC="${BUILDTMP}/mtools.conf"
390 ### COPIED FROM build_usb() below!!!!
391 echo -n " populating USB image... "
392 mcopy -bsQ -i "$usb" "$ISOFS"/* z:/
394 # Use syslinux instead of isolinux to make the image bootable
395 tmp="${BUILDTMP}/syslinux.cfg"
397 ${console_serial_line}
399 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${console_spec}
404 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
405 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
407 rm -f "${BUILDTMP}/mtools.conf"
410 echo "making USB image bootable."
411 syslinux -o $offset "$usb"
415 #################### plain USB
416 function build_usb() {
417 echo -n "* Creating USB image... "
418 local usb="$1" ; shift
421 mkfs.vfat -C "$usb" $(($(du -Lsk $ISOFS | awk '{ print $1; }') + $FREE_SPACE))
424 echo -n " populating USB image... "
425 mcopy -bsQ -i "$usb" "$ISOFS"/* ::/
427 # Use syslinux instead of isolinux to make the image bootable
428 tmp="${BUILDTMP}/syslinux.cfg"
430 ${console_serial_line}
432 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${console_spec}
437 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
438 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
441 echo "making USB image bootable."
445 #################### utility to setup CRAMFS related support
446 function prepare_cramfs() {
447 [ -n "$CRAMFS_PREPARED" ] && return 0
450 echo "* Setting up CRAMFS-based images"
451 local tmp="${BUILDTMP}/cramfs-tree"
453 push_cleanup rm -rf $tmp
455 gzip -d -c $ISOFS/bootcd.img | cpio -diu
456 gzip -d -c $ISOFS/overlay.img | cpio -diu
457 [ -n "$custom" ] && \
458 gzip -d -c $ISOFS/custom.img | cpio -diu
460 # clean out unnecessary rpm lib
461 echo "* clearing var/lib/rpm/*"
464 # bootcd requires this directory
465 mkdir -p mnt/confdevice
467 # relocate various directory to /tmp
469 ln -fs /tmp/root root
470 ln -fs /sbin/init linuxrc
471 ln -fs /tmp/resolv.conf etc/resolv.conf
472 ln -fs /tmp/etc/mtab etc/mtab
474 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
476 echo "* renaming dirs in ./etc"
478 for dir in `find * -type d -prune | grep -v rc.d`; do
480 ln -fs /tmp/etc/${dir} ${dir}
484 echo "* renaming dirs in ./var"
485 # rename all top-level directories and put in a symlink to /tmp/var
487 for dir in `find * -type d -prune`; do
489 ln -fs /tmp/var/${dir} ${dir}
493 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
494 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
496 cat >./etc/fstab <<EOF
497 /dev/ram0 / cramfs ro 0 0
498 none /dev/pts devpts gid=5,mode=620 0 0
499 none /proc proc defaults 0 0
500 none /sys sysfs defaults 0 0
506 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
507 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
512 # update etc/inittab to start with pl_rsysinit
513 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
515 # modify inittab to have a serial console
516 if [ -n "$serial" ] ; then
517 echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
518 # and let root log in
519 echo "$console_dev" >> etc/securetty
522 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
523 etcsize=$(du -s ./etc | awk '{ print $1 }')
524 varsize=$(du -s ./var | awk '{ print $1 }')
525 let msize=($varsize+$etcsize+8192)/1024
527 # make dhclient happy
528 for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
529 ln -fs /tmp/etc/resolv.conf etc/resolv.conf
530 ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
532 # generate pl_rsysinit
533 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
535 # generated by build.sh
536 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
537 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
540 touch /tmp/etc/resolv.conf
545 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
547 # copy over directory contents of all _o directories from /etc and /var
548 # /tmp/etc and /tmp/var
550 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
553 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
558 # hand over to pl_sysinit
559 echo "pl_rsysinit: handing over to pl_sysinit"
560 /etc/init.d/pl_sysinit
562 chmod +x etc/rc.d/init.d/pl_rsysinit
566 # create the cramfs image
567 echo "* Creating cramfs image"
568 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
569 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
574 #################### Create ISO CRAMFS image
575 function build_iso_cramfs() {
576 local iso="$1" ; shift
579 prepare_cramfs "$custom"
580 echo "* Creating ISO CRAMFS-based image"
582 local tmp="${BUILDTMP}/cramfs-iso"
584 push_cleanup rm -rf $tmp
585 (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
586 cat >$tmp/isolinux.cfg <<EOF
587 ${console_serial_line}
589 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${console_spec}
595 cp ${BUILDTMP}/cramfs.img $tmp
604 #################### Create USB CRAMFS based image
605 function build_usb_cramfs() {
606 local usb="$1" ; shift
609 prepare_cramfs "$custom"
610 echo "* Creating USB CRAMFS based image"
612 let vfat_size=${cramfs_size}+$FREE_SPACE
614 # Make VFAT filesystem for USB
615 mkfs.vfat -C "$usb" $vfat_size
618 echo "* Populating USB with overlay images and cramfs"
619 mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
620 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
622 # Use syslinux instead of isolinux to make the image bootable
623 tmp="${BUILDTMP}/syslinux.cfg"
625 ${console_serial_line}
627 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${console_spec}
633 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
636 echo "* Making USB CRAMFS based image bootable"
640 #################### map on all types provided on the command-line and invoke one of the above functions
641 function build_types () {
643 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
645 # alter output filename to reflect serial settings
646 if [ -n "$IS_SERIAL" ] ; then
647 if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
650 serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
656 function type_to_name() {
659 s/usb_partition$/-partition.usb/;
661 s/usb_cramfs$/-cramfs.usb/;
662 s/iso_cramfs$/-cramfs.iso/;
669 tname=`type_to_name $t`
670 # if -o is specified (as it has no default)
671 if [ -n "$OUTPUT_NAME" ] ; then
674 output="${OUTPUT_BASE}${serial}${tname}"
677 echo "*** Dealing with type=$arg"
678 echo '*' build_$t "$output" "$CUSTOM_DIR"
679 [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR"
688 parse_command_line "$@"
690 echo "* Building images for $FULL_VERSION_STRING"
691 # Do not tolerate errors
693 trap "do_cleanup" ERR INT EXIT
695 init_serial $CONSOLE_INFO