3 # Builds custom BootCD ISO and USB images in the current
4 # directory. For backward compatibility, if an old-style static
5 # configuration is specified, that configuration file will be parsed
6 # instead of the current PLC configuration in
7 # /etc/planetlab/plc_config.
9 # Aaron Klingaman <alk@absarokasoft.com>
10 # Mark Huang <mlhuang@cs.princeton.edu>
11 # Copyright (C) 2004-2007 The Trustees of Princeton University
16 PATH=/sbin:/bin:/usr/sbin:/usr/bin
19 NODE_CONFIGURATION_FILE=
20 DEFAULT_TYPES="usb iso"
21 # Leave 4 MB of free space
25 DEFAULT_CONSOLE="graphic"
26 CONSOLE_INFO=$DEFAULT_CONSOLE
27 DEFAULT_SERIAL_CONSOLE="ttyS0:115200"
28 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
31 for x in iso usb usb_partition; do for s in "" "_serial" ; do for c in "" "_cramfs" ; do
34 usb_partition_cramfs|usb_partition_cramfs_serial)
38 ALL_TYPES="$ALL_TYPES $t" ;;
44 echo "Usage: build.sh [OPTION]..."
45 echo " -c name (Deprecated) Static configuration to use (default: $CONFIGURATION)"
46 echo " -f plnode.txt Node to customize CD for (default: none)"
47 echo " -t 'types' Build the specified images (default: $DEFAULT_TYPES)"
48 echo " -a Build all known types as listed below"
49 echo " -s console-info Enable a serial line as console and also bring up getty on that line"
50 echo " defaults to $DEFAULT_SERIAL_CONSOLE"
51 echo " -O output-base The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
52 echo " -C custom-dir Custom directory"
53 echo " can be a full path"
54 echo " -n Dry run - mostly for debug/test purposes"
55 echo " -h This message"
56 echo "All known types: $ALL_TYPES"
63 while getopts "c:f:t:as:O:C:nh" opt ; do
66 CONFIGURATION=$OPTARG ;;
68 NODE_CONFIGURATION_FILE=$OPTARG ;;
70 TYPES="$TYPES $OPTARG" ;;
74 CONSOLE_INFO="$OPTARG" ;;
76 OUTPUT_BASE="$OPTARG" ;;
78 CUSTOM_DIR="$OPTARG" ;;
86 # use default if not set
87 [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
89 # Do not tolerate errors
92 # Change to our source directory
93 srcdir=$(cd $(dirname $0) && pwd -P)
97 isofs=$PWD/build/isofs
99 # The reference image is expected to have been built by prep.sh (see .spec)
100 # we disable the initial logic that called prep.sh if that was not the case
101 # this is because prep.sh needs to know pldistro
102 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
103 echo "You have to run prep.sh prior to calling $0 - exiting"
107 # build/version.txt written by prep.sh
108 BOOTCD_VERSION=$(cat build/version.txt)
110 if [ -f /etc/planetlab/plc_config ] ; then
111 # Source PLC configuration
112 . /etc/planetlab/plc_config
115 ### This support for backwards compatibility can be taken out in the
116 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
117 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
119 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
120 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
123 # If PLC configuration is not valid, try a static configuration
124 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
125 # (Deprecated) Source static configuration
126 . configurations/$CONFIGURATION/configuration
128 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
129 PLC_WWW_HOST="www.planet-lab.org"
131 if [ -n "$EXTRA_VERSION" ] ; then
132 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
134 PLC_BOOT_HOST=$PRIMARY_SERVER
135 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
136 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
137 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
140 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
142 echo "* Building images for $FULL_VERSION_STRING"
144 # From within a myplc chroot /usr/tmp is too small
145 # to build all possible images, whereas /data is part of the host
146 # filesystem and usually has sufficient space. What we
147 # should do is check whether the expected amount of space
150 if [ -d /data/tmp ] ; then
151 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
152 if [ -n "$isreadonly" ] ; then
158 declare -a _CLEANUPS=()
159 function do_cleanup()
162 for i in "${_CLEANUPS[@]}"; do
166 function push_cleanup()
168 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
170 function pop_cleanup()
172 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
175 trap "do_cleanup" ERR INT EXIT
177 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
178 push_cleanup rm -fr "${BUILDTMP}"
179 mkdir "${BUILDTMP}/isofs"
180 for i in "$isofs"/{bootcd.img,kernel}; do
181 ln -s "$i" "${BUILDTMP}/isofs"
183 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
184 isofs="${BUILDTMP}/isofs"
186 # Root of the ISO and USB images
187 echo "* Populating root filesystem..."
188 overlay="${BUILDTMP}/overlay"
189 install -d -m 755 $overlay
190 push_cleanup rm -fr $overlay
192 # Create version files
193 echo "* Creating version files"
195 # Boot Manager compares pl_version in both places to make sure that
196 # the right CD is mounted. We used to boot from an initrd and mount
197 # the CD on /usr. Now we just run everything out of the initrd.
198 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
199 mkdir -p $(dirname $file)
200 echo "$FULL_VERSION_STRING" >$file
203 # Install boot server configuration files
204 echo "* Installing boot server configuration files"
206 # We always intended to bring up and support backup boot servers,
207 # but never got around to it. Just install the same parameters for
209 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
210 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
211 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
212 echo "$PLC_BOOT_HOST" >$dir/boot_server
213 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
214 echo "/boot/" >$dir/boot_server_path
217 # (Deprecated) Install old-style boot server configuration files
218 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
219 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
220 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
221 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
222 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
224 # Generate /etc/issue
225 echo "* Generating /etc/issue"
227 if [ "$PLC_WWW_PORT" = "443" ] ; then
228 PLC_WWW_URL="https://$PLC_WWW_HOST/"
229 elif [ "$PLC_WWW_PORT" != "80" ] ; then
230 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
232 PLC_WWW_URL="http://$PLC_WWW_HOST/"
235 mkdir -p $overlay/etc
236 cat >$overlay/etc/issue <<EOF
242 This machine is a node in the $PLC_NAME distributed network. It has
243 not fully booted yet. If you have cancelled the boot process at the
244 request of $PLC_NAME Support, please follow the instructions provided
245 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
247 Console login at this point is restricted to root. Provide the root
248 password of the default $PLC_NAME Central administrator account at the
249 time that this CD was created.
254 echo "* Setting root password"
256 if [ -z "$ROOT_PASSWORD" ] ; then
257 # Generate an encrypted password with crypt() if not defined
258 # in a static configuration.
259 ROOT_PASSWORD=$(python <<EOF
260 import crypt, random, string
261 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
262 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
267 # build/passwd copied out by prep.sh
268 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
271 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
272 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
273 echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
274 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
277 # Pack overlay files into a compressed archive
278 echo "* Compressing overlay image"
279 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
284 if [ -n "$CUSTOM_DIR" ]; then
285 echo "* Compressing custom image"
286 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
289 # Calculate ramdisk size (total uncompressed size of both archives)
290 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
291 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
293 echo "$FULL_VERSION_STRING" >$isofs/pl_version
297 function extract_console_dev()
300 dev=$(echo $console| awk -F: ' {print $1}')
304 function extract_console_baud()
307 baud=$(echo $console| awk -F: ' {print $2}')
308 [ -z "$baud" ] && baud="115200"
312 function extract_console_parity()
315 parity=$(echo $console| awk -F: ' {print $3}')
316 [ -z "$parity" ] && parity="n"
320 function extract_console_bits()
323 bits=$(echo $console| awk -F: ' {print $4}')
324 [ -z "$bits" ] && bits="8"
330 local iso="$1" ; shift
331 local console="$1" ; shift
335 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
337 console_dev=$(extract_console_dev $console)
338 console_baud=$(extract_console_baud $console)
339 console_parity=$(extract_console_parity $console)
340 console_bits=$(extract_console_bits $console)
343 # Write isolinux configuration
344 cat >$isofs/isolinux.cfg <<EOF
345 ${serial:+SERIAL 0 ${console_baud}}
347 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
354 echo "* Creating ISO image"
360 function build_usb_partition()
362 echo -n "* Creating USB image with partitions..."
363 local usb="$1" ; shift
364 local console="$1" ; shift
368 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
370 console_dev=$(extract_console_dev $console)
371 console_baud=$(extract_console_baud $console)
372 console_parity=$(extract_console_parity $console)
373 console_bits=$(extract_console_bits $console)
376 local size=$(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
377 size=$(( $size / 1024 ))
381 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
382 local offset=$(( $sectors*512 ))
384 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
386 cat >${BUILDTMP}/mtools.conf<<EOF
395 # environment variable for mtools
396 export MTOOLSRC="${BUILDTMP}/mtools.conf"
398 ### COPIED FROM build_usb() below!!!!
399 echo -n " populating USB image... "
400 mcopy -bsQ -i "$usb" "$isofs"/* z:/
402 # Use syslinux instead of isolinux to make the image bootable
403 tmp="${BUILDTMP}/syslinux.cfg"
405 ${serial:+SERIAL 0 ${console_baud}}
407 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
412 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
413 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
415 rm -f "${BUILDTMP}/mtools.conf"
418 echo "making USB image bootable."
419 syslinux -o $offset "$usb"
426 echo -n "* Creating USB image... "
427 local usb="$1" ; shift
428 local console="$1" ; shift
432 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
434 console_dev=$(extract_console_dev $console)
435 console_baud=$(extract_console_baud $console)
436 console_parity=$(extract_console_parity $console)
437 console_bits=$(extract_console_bits $console)
440 mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
443 echo -n " populating USB image... "
444 mcopy -bsQ -i "$usb" "$isofs"/* ::/
446 # Use syslinux instead of isolinux to make the image bootable
447 tmp="${BUILDTMP}/syslinux.cfg"
449 ${serial:+SERIAL 0 $console_baud}
451 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
456 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
457 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
460 echo "making USB image bootable."
465 # Setup CRAMFS related support
466 function prepare_cramfs()
468 [ -n "$CRAMFS_PREPARED" ] && return 0
469 local console=$1; shift
472 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
474 console_dev=$(extract_console_dev $console)
475 console_baud=$(extract_console_baud $console)
478 echo "* Setting up CRAMFS-based images"
479 local tmp="${BUILDTMP}/cramfs-tree"
481 push_cleanup rm -rf $tmp
483 gzip -d -c $isofs/bootcd.img | cpio -diu
484 gzip -d -c $isofs/overlay.img | cpio -diu
485 [ -n "$custom" ] && \
486 gzip -d -c $isofs/custom.img | cpio -diu
488 # clean out unnecessary rpm lib
489 echo "* clearing var/lib/rpm/*"
492 # bootcd requires this directory
493 mkdir -p mnt/confdevice
495 # relocate various directory to /tmp
497 ln -fs /tmp/root root
498 ln -fs /sbin/init linuxrc
499 ln -fs /tmp/resolv.conf etc/resolv.conf
500 ln -fs /tmp/etc/mtab etc/mtab
502 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
504 echo "* renaming dirs in ./etc"
506 for dir in `find * -type d -prune | grep -v rc.d`; do
508 ln -fs /tmp/etc/${dir} ${dir}
512 echo "* renaming dirs in ./var"
513 # rename all top-level directories and put in a symlink to /tmp/var
515 for dir in `find * -type d -prune`; do
517 ln -fs /tmp/var/${dir} ${dir}
521 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
522 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
524 cat >./etc/fstab <<EOF
525 /dev/ram0 / cramfs ro 0 0
526 none /dev/pts devpts gid=5,mode=620 0 0
527 none /proc proc defaults 0 0
528 none /sys sysfs defaults 0 0
534 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
535 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
540 # update etc/inittab to start with pl_rsysinit
541 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
543 # modify inittab to have a serial console
544 if [ -n "$serial" ] ; then
545 echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
546 # and let root log in
547 echo "$console_dev" >> etc/securetty
550 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
551 etcsize=$(du -s ./etc | awk '{ print $1 }')
552 varsize=$(du -s ./var | awk '{ print $1 }')
553 let msize=($varsize+$etcsize+8192)/1024
555 # make dhclient happy
556 for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
557 ln -fs /tmp/etc/resolv.conf etc/resolv.conf
558 ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
560 # generate pl_rsysinit
561 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
563 # generated by build.sh
564 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
565 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
568 touch /tmp/etc/resolv.conf
573 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
575 # copy over directory contents of all _o directories from /etc and /var
576 # /tmp/etc and /tmp/var
578 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
581 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
586 # hand over to pl_sysinit
587 echo "pl_rsysinit: handing over to pl_sysinit"
588 /etc/init.d/pl_sysinit
590 chmod +x etc/rc.d/init.d/pl_rsysinit
594 # create the cramfs image
595 echo "* Creating cramfs image"
596 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
597 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
602 # Create ISO CRAMFS image
603 function build_iso_cramfs()
605 local iso="$1" ; shift
606 local console="$1" ; shift
610 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
612 console_dev=$(extract_console_dev $console)
613 console_baud=$(extract_console_baud $console)
614 console_parity=$(extract_console_parity $console)
615 console_bits=$(extract_console_bits $console)
617 prepare_cramfs "$console" "$custom"
618 echo "* Creating ISO CRAMFS-based image"
620 local tmp="${BUILDTMP}/cramfs-iso"
622 push_cleanup rm -rf $tmp
623 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
624 cat >$tmp/isolinux.cfg <<EOF
625 ${serial:+SERIAL 0 $console_baud}
627 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
633 cp ${BUILDTMP}/cramfs.img $tmp
642 # Create USB CRAMFS based image
643 function build_usb_cramfs()
645 local usb="$1" ; shift
646 local console="$1" ; shift
650 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
652 console_dev=$(extract_console_dev $console)
653 console_baud=$(extract_console_baud $console)
654 console_parity=$(extract_console_parity $console)
655 console_bits=$(extract_console_bits $console)
657 prepare_cramfs "$console" "$custom"
658 echo "* Creating USB CRAMFS based image"
660 let vfat_size=${cramfs_size}+$FREE_SPACE
662 # Make VFAT filesystem for USB
663 mkfs.vfat -C "$usb" $vfat_size
666 echo "* Populating USB with overlay images and cramfs"
667 mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
668 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
670 # Use syslinux instead of isolinux to make the image bootable
671 tmp="${BUILDTMP}/syslinux.cfg"
673 ${serial:+SERIAL 0 $console_baud}
675 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
680 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
683 echo "* Making USB CRAMFS based image bootable"
687 function type_to_name()
691 s/usb_partition$/-partition.usb/;
692 s/usb_serial$/-serial.usb/;
694 s/iso_serial$/-serial.iso/;
695 s/usb_cramfs$/-cramfs.usb/;
696 s/usb_cramfs_serial$/-cramfs-serial.usb/;
697 s/iso_cramfs$/-cramfs.iso/;
698 s/iso_cramfs_serial$/-cramfs-serial.iso/;
702 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
706 CONSOLE=$CONSOLE_INFO
707 tname=`type_to_name $t`
708 if [[ "$t" == *_serial ]]; then
709 if [ "$CONSOLE_INFO" == "$DEFAULT_CONSOLE" ] ; then
710 CONSOLE="$DEFAULT_SERIAL_CONSOLE"
712 t=`echo $t | sed 's/_serial$//'`
715 OUTPUTNAME="${OUTPUT_BASE}${tname}"
716 if [ "$CONSOLE" != "$DEFAULT_CONSOLE" ] ; then
717 CONSOLE_NAME=$(echo $CONSOLE | sed 's,\:,,g')
718 OUTPUTNAME="${OUTPUT_BASE}-serial-${CONSOLE_NAME}${tname}"
720 echo "*** Dealing with type=$arg"
721 echo "* " build_$t "$OUTPUTNAME" "$CONSOLE" "$CUSTOM_DIR"
722 if [ ! -n "$DRY_RUN" ] ; then
723 build_$t "$OUTPUTNAME" "$CONSOLE" "$CUSTOM_DIR" || true