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 TYPES="usb iso usb_serial iso_serial"
21 ALL_TYPES="usb iso usb_serial iso_serial usb_cramfs iso_cramfs usb_cramfs_serial iso_cramfs_serial"
22 # Leave 4 MB of free space
26 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
30 echo "Usage: build.sh [OPTION]..."
31 echo " -c name (Deprecated) Static configuration to use (default: $CONFIGURATION)"
32 echo " -f planet.cnf Node to customize CD for (default: none)"
33 echo " -t 'types' Build the specified images (default: $TYPES)"
34 echo " All known types: $ALL_TYPES"
35 echo " -a Build all supported images"
36 echo " -C custom-dir Custom directory"
37 echo " -O output-base The basename of the generated files (default: PLC_NAME-BootCD-VERSION)"
38 echo " -h This message"
43 while getopts "O:c:f:t:C:ah" opt ; do
49 NODE_CONFIGURATION_FILE=$OPTARG
69 # Do not tolerate errors
72 # Change to our source directory
73 srcdir=$(cd $(dirname $0) && pwd -P)
77 isofs=$PWD/build/isofs
79 # The reference image is expected to have been built by prep.sh (see .spec)
80 # we disable the initial logic that called prep.sh if that was not the case
81 # this is because prep.sh needs to know pldistro
82 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
83 echo "You have to run prep.sh prior to calling $0 - exiting"
87 # build/version.txt written by prep.sh
88 BOOTCD_VERSION=$(cat build/version.txt)
90 if [ -f /etc/planetlab/plc_config ] ; then
91 # Source PLC configuration
92 . /etc/planetlab/plc_config
95 ### This support for backwards compatibility can be taken out in the
96 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
97 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
99 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
100 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
103 # If PLC configuration is not valid, try a static configuration
104 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
105 # (Deprecated) Source static configuration
106 . configurations/$CONFIGURATION/configuration
108 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
109 PLC_WWW_HOST="www.planet-lab.org"
111 if [ -n "$EXTRA_VERSION" ] ; then
112 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
114 PLC_BOOT_HOST=$PRIMARY_SERVER
115 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
116 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
117 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
120 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
122 echo "* Building images for $FULL_VERSION_STRING"
124 # From within a myplc chroot /usr/tmp is too small
125 # to build all possible images, whereas /data is part of the host
126 # filesystem and usually has sufficient space. What we
127 # should do is check whether the expected amount of space
130 if [ -d /data/tmp ] ; then
131 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
132 if [ -n "$isreadonly" ] ; then
138 declare -a _CLEANUPS=()
139 function do_cleanup()
142 for i in "${_CLEANUPS[@]}"; do
146 function push_cleanup()
148 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
150 function pop_cleanup()
152 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
155 trap "do_cleanup" ERR INT EXIT
157 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
158 push_cleanup rm -fr "${BUILDTMP}"
159 mkdir "${BUILDTMP}/isofs"
160 for i in "$isofs"/{bootcd.img,kernel}; do
161 ln -s "$i" "${BUILDTMP}/isofs"
163 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
164 isofs="${BUILDTMP}/isofs"
166 # Root of the ISO and USB images
167 echo "* Populating root filesystem..."
168 overlay="${BUILDTMP}/overlay"
169 install -d -m 755 $overlay
170 push_cleanup rm -fr $overlay
172 # Create version files
173 echo "* Creating version files"
175 # Boot Manager compares pl_version in both places to make sure that
176 # the right CD is mounted. We used to boot from an initrd and mount
177 # the CD on /usr. Now we just run everything out of the initrd.
178 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
179 mkdir -p $(dirname $file)
180 echo "$FULL_VERSION_STRING" >$file
183 # Install boot server configuration files
184 echo "* Installing boot server configuration files"
186 # We always intended to bring up and support backup boot servers,
187 # but never got around to it. Just install the same parameters for
189 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
190 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
191 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
192 echo "$PLC_BOOT_HOST" >$dir/boot_server
193 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
194 echo "/boot/" >$dir/boot_server_path
197 # (Deprecated) Install old-style boot server configuration files
198 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
199 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
200 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
201 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
202 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
204 # Generate /etc/issue
205 echo "* Generating /etc/issue"
207 if [ "$PLC_WWW_PORT" = "443" ] ; then
208 PLC_WWW_URL="https://$PLC_WWW_HOST/"
209 elif [ "$PLC_WWW_PORT" != "80" ] ; then
210 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
212 PLC_WWW_URL="http://$PLC_WWW_HOST/"
215 mkdir -p $overlay/etc
216 cat >$overlay/etc/issue <<EOF
222 This machine is a node in the $PLC_NAME distributed network. It has
223 not fully booted yet. If you have cancelled the boot process at the
224 request of $PLC_NAME Support, please follow the instructions provided
225 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
227 Console login at this point is restricted to root. Provide the root
228 password of the default $PLC_NAME Central administrator account at the
229 time that this CD was created.
234 echo "* Setting root password"
236 if [ -z "$ROOT_PASSWORD" ] ; then
237 # Generate an encrypted password with crypt() if not defined
238 # in a static configuration.
239 ROOT_PASSWORD=$(python <<EOF
240 import crypt, random, string
241 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
242 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
247 # build/passwd copied out by prep.sh
248 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
251 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
252 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
253 echo "* Installing node configuration file"
254 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
257 # Pack overlay files into a compressed archive
258 echo "* Compressing overlay image"
259 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
264 if [ -n "$CUSTOM_DIR" ]; then
265 echo "* Compressing custom image"
266 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
269 # Calculate ramdisk size (total uncompressed size of both archives)
270 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
271 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
273 echo "$FULL_VERSION_STRING" >$isofs/pl_version
279 local iso="$1" ; shift
280 local serial="$1" ; shift
283 # Write isolinux configuration
284 cat >$isofs/isolinux.cfg <<EOF
285 ${serial:+SERIAL 0 115200}
287 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
294 echo "* Creating ISO image"
299 function build_usb_partition()
301 echo -n "* Creating USB image with partitions..."
302 local usb="$1" ; shift
303 local serial="$1" ; shift
306 local size=$(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
307 size=$(( $size / 1024 ))
311 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
312 local offset=$(( $sectors*512 ))
314 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
316 cat >${BUILDTMP}/mtools.conf<<EOF
325 # environment variable for mtools
326 export MTOOLSRC="${BUILDTMP}/mtools.conf"
328 ### COPIED FROM build_usb() below!!!!
329 echo -n " populating USB image... "
330 mcopy -bsQ -i "$usb" "$isofs"/* z:/
332 # Use syslinux instead of isolinux to make the image bootable
333 tmp="${BUILDTMP}/syslinux.cfg"
335 ${serial:+SERIAL 0 115200}
337 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
342 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
343 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
345 rm -f "${BUILDTMP}/mtools.conf"
348 echo "making USB image bootable."
349 syslinux -o $offset "$usb"
356 echo -n "* Creating USB image... "
357 local usb="$1" ; shift
358 local serial="$1" ; shift
361 mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
364 echo -n " populating USB image... "
365 mcopy -bsQ -i "$usb" "$isofs"/* ::/
367 # Use syslinux instead of isolinux to make the image bootable
368 tmp="${BUILDTMP}/syslinux.cfg"
370 ${serial:+SERIAL 0 115200}
372 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
377 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
378 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
381 echo "making USB image bootable."
386 # Setup CRAMFS related support
387 function prepare_cramfs()
389 [ -n "$CRAMFS_PREPARED" ] && return 0
392 echo "* Setting up CRAMFS-based images"
393 local tmp="${BUILDTMP}/cramfs-tree"
395 push_cleanup rm -rf $tmp
397 gzip -d -c $isofs/bootcd.img | cpio -diu
398 gzip -d -c $isofs/overlay.img | cpio -diu
399 [ -n "$custom" ] && \
400 gzip -d -c $isofs/custom.img | cpio -diu
402 # clean out unnecessary rpm lib
403 echo "* clearing var/lib/rpm/*"
406 # bootcd requires this directory
407 mkdir -p mnt/confdevice
409 # relocate various directory to /tmp
411 ln -fs /tmp/root root
412 ln -fs /sbin/init linuxrc
413 ln -fs /tmp/resolv.conf etc/resolv.conf
414 ln -fs /tmp/etc/mtab etc/mtab
416 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
418 echo "* renaming dirs in ./etc"
420 for dir in `find * -type d -prune | grep -v rc.d`; do
422 ln -fs /tmp/etc/${dir} ${dir}
426 echo "* renaming dirs in ./var"
427 # rename all top-level directories and put in a symlink to /tmp/var
429 for dir in `find * -type d -prune`; do
431 ln -fs /tmp/var/${dir} ${dir}
435 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
436 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
438 cat >./etc/fstab <<EOF
439 /dev/ram0 / cramfs ro 0 0
440 none /dev/pts devpts gid=5,mode=620 0 0
441 none /proc proc defaults 0 0
442 none /sys sysfs defaults 0 0
448 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
449 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
454 # update etc/inittab to start with pl_rsysinit
455 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
457 # modify inittab to have a serial console
458 echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
459 # and let root log in
460 echo "ttyS0" >> etc/securetty
462 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
463 etcsize=$(du -s ./etc | awk '{ print $1 }')
464 varsize=$(du -s ./var | awk '{ print $1 }')
465 let msize=($varsize+$etcsize+8192)/1024
468 # generate pl_rsysinit
469 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
471 # generated by build.sh
472 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
473 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
476 touch /tmp/etc/resolv.conf
481 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
483 # copy over directory contents of all _o directories from /etc and /var
484 # /tmp/etc and /tmp/var
486 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
489 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
493 # hand over to pl_sysinit
494 echo "pl_rsysinit: handing over to pl_sysinit"
495 /etc/init.d/pl_sysinit
497 chmod +x etc/rc.d/init.d/pl_rsysinit
501 # create the cramfs image
502 echo "* Creating cramfs image"
503 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
504 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
509 # Create ISO CRAMFS image
510 function build_iso_cramfs()
512 local iso="$1" ; shift
513 local serial="$1" ; shift
515 prepare_cramfs "$custom"
516 echo "* Creating ISO CRAMFS-based image"
518 local tmp="${BUILDTMP}/cramfs-iso"
520 push_cleanup rm -rf $tmp
521 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
522 cat >$tmp/isolinux.cfg <<EOF
523 ${serial:+SERIAL 0 9600}
525 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
531 cp ${BUILDTMP}/cramfs.img $tmp
540 # Create USB CRAMFS based image
541 function build_usb_cramfs()
544 local serial="$1" ; shift
546 prepare_cramfs "$custom"
547 echo "* Creating USB CRAMFS based image"
549 let vfat_size=${cramfs_size}+$FREE_SPACE
551 # Make VFAT filesystem for USB
552 mkfs.vfat -C "$usb" $vfat_size
555 echo "* Populating USB with overlay images and cramfs"
556 mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
557 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
559 # Use syslinux instead of isolinux to make the image bootable
560 tmp="${BUILDTMP}/syslinux.cfg"
562 ${serial:+SERIAL 0 9600}
564 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
569 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
572 echo "* Making USB CRAMFS based image bootable"
576 function type_to_name()
580 s/usb_partition$/-partition.usb/;
581 s/usb_serial$/-serial.usb/;
583 s/iso_serial$/-serial.iso/;
584 s/usb_cramfs$/-cramfs.usb/;
585 s/usb_cramfs_serial$/-cramfs-serial.usb/;
586 s/iso_cramfs$/-cramfs.iso/;
587 s/iso_cramfs_serial$/-cramfs-serial.iso/;
591 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
595 tname=`type_to_name $t`
596 if [[ "$t" == *_serial ]]; then
598 t=`echo $t | sed 's/_serial$//'`
600 build_$t "${OUTPUT_BASE}${tname}" "$serial" "$CUSTOM_DIR"