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 # Leave 4 MB of free space
25 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
29 echo "Usage: build.sh [OPTION]..."
30 echo " -c name (Deprecated) Static configuration to use (default: $CONFIGURATION)"
31 echo " -f planet.cnf Node to customize CD for (default: none)"
32 echo " -t 'types' Build the specified images (default: $TYPES)"
33 echo " -a Build all supported images"
34 echo " -C custom-dir Custom directory"
35 echo " -O output-base The basename of the generated files (default: PLC_NAME-BootCD-VERSION)"
36 echo " -h This message"
41 while getopts "O:c:f:t:C:ah" opt ; do
47 NODE_CONFIGURATION_FILE=$OPTARG
59 TYPES="usb iso usb_serial iso_serial usb_cramfs iso_cramfs usb_cramfs_serial iso_cramfs_serial"
67 # Do not tolerate errors
70 # Change to our source directory
71 srcdir=$(cd $(dirname $0) && pwd -P)
75 isofs=$PWD/build/isofs
77 # The reference image is expected to have been built by prep.sh (see .spec)
78 # we disable the initial logic that called prep.sh if that was not the case
79 # this is because prep.sh needs to know pldistro
80 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
81 echo "You have to run prep.sh prior to calling $0 - exiting"
85 # build/version.txt written by prep.sh
86 BOOTCD_VERSION=$(cat build/version.txt)
88 if [ -f /etc/planetlab/plc_config ] ; then
89 # Source PLC configuration
90 . /etc/planetlab/plc_config
93 ### This support for backwards compatibility can be taken out in the
94 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
95 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
97 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
98 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
101 # If PLC configuration is not valid, try a static configuration
102 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
103 # (Deprecated) Source static configuration
104 . configurations/$CONFIGURATION/configuration
106 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
107 PLC_WWW_HOST="www.planet-lab.org"
109 if [ -n "$EXTRA_VERSION" ] ; then
110 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
112 PLC_BOOT_HOST=$PRIMARY_SERVER
113 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
114 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
115 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
118 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
120 echo "* Building images for $FULL_VERSION_STRING"
122 # From within a myplc chroot /usr/tmp is too small
123 # to build all possible images, whereas /data is part of the host
124 # filesystem and usually has sufficient space. What we
125 # should do is check whether the expected amount of space
128 if [ -d /data/tmp ] ; then
129 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
130 if [ -n "$isreadonly" ] ; then
136 declare -a _CLEANUPS=()
137 function do_cleanup()
140 for i in "${_CLEANUPS[@]}"; do
144 function push_cleanup()
146 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
148 function pop_cleanup()
150 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
153 trap "do_cleanup" ERR INT EXIT
155 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
156 push_cleanup rm -fr "${BUILDTMP}"
157 mkdir "${BUILDTMP}/isofs"
158 for i in "$isofs"/{bootcd.img,kernel,isolinux.bin}; do
159 ln -s "$i" "${BUILDTMP}/isofs"
161 isofs="${BUILDTMP}/isofs"
163 # Root of the ISO and USB images
164 echo "* Populating root filesystem..."
165 overlay="${BUILDTMP}/overlay"
166 install -d -m 755 $overlay
167 push_cleanup rm -fr $overlay
169 # Create version files
170 echo "* Creating version files"
172 # Boot Manager compares pl_version in both places to make sure that
173 # the right CD is mounted. We used to boot from an initrd and mount
174 # the CD on /usr. Now we just run everything out of the initrd.
175 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
176 mkdir -p $(dirname $file)
177 echo "$FULL_VERSION_STRING" >$file
180 # Install boot server configuration files
181 echo "* Installing boot server configuration files"
183 # We always intended to bring up and support backup boot servers,
184 # but never got around to it. Just install the same parameters for
186 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
187 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
188 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
189 echo "$PLC_BOOT_HOST" >$dir/boot_server
190 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
191 echo "/boot/" >$dir/boot_server_path
194 # (Deprecated) Install old-style boot server configuration files
195 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
196 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
197 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
198 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
199 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
201 # Generate /etc/issue
202 echo "* Generating /etc/issue"
204 if [ "$PLC_WWW_PORT" = "443" ] ; then
205 PLC_WWW_URL="https://$PLC_WWW_HOST/"
206 elif [ "$PLC_WWW_PORT" != "80" ] ; then
207 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
209 PLC_WWW_URL="http://$PLC_WWW_HOST/"
212 mkdir -p $overlay/etc
213 cat >$overlay/etc/issue <<EOF
219 This machine is a node in the $PLC_NAME distributed network. It has
220 not fully booted yet. If you have cancelled the boot process at the
221 request of $PLC_NAME Support, please follow the instructions provided
222 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
224 Console login at this point is restricted to root. Provide the root
225 password of the default $PLC_NAME Central administrator account at the
226 time that this CD was created.
231 echo "* Setting root password"
233 if [ -z "$ROOT_PASSWORD" ] ; then
234 # Generate an encrypted password with crypt() if not defined
235 # in a static configuration.
236 ROOT_PASSWORD=$(python <<EOF
237 import crypt, random, string
238 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
239 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
244 # build/passwd copied out by prep.sh
245 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
248 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
249 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
250 echo "* Installing node configuration file"
251 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
254 # Pack overlay files into a compressed archive
255 echo "* Compressing overlay image"
256 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
261 if [ -n "$CUSTOM_DIR" ]; then
262 echo "* Compressing custom image"
263 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
266 # Calculate ramdisk size (total uncompressed size of both archives)
267 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
268 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
270 echo "$FULL_VERSION_STRING" >$isofs/pl_version
280 # Write isolinux configuration
281 cat >$isofs/isolinux.cfg <<EOF
282 ${serial:+SERIAL 0 115200}
284 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
291 echo "* Creating ISO image"
300 echo -n "* Creating USB image... "
305 mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
308 echo -n " populating USB image... "
309 mcopy -bsQ -i "$usb" "$isofs"/* ::/
311 # Use syslinux instead of isolinux to make the image bootable
312 tmp="${BUILDTMP}/syslinux.cfg"
314 ${serial:+SERIAL 0 115200}
316 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
321 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
322 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
325 echo "making USB image bootable."
326 $srcdir/syslinux/unix/syslinux "$usb"
330 # Setup CRAMFS related support
331 function prepare_cramfs()
333 [ -n "$CRAMFS_PREPARED" ] && return 0
336 echo "* Setting up CRAMFS-based images"
337 local tmp="${BUILDTMP}/cramfs-tree"
339 push_cleanup rm -rf $tmp
341 gzip -d -c $isofs/bootcd.img | cpio -diu
342 gzip -d -c $isofs/overlay.img | cpio -diu
343 [ -n "$custom" ] && \
344 gzip -d -c $isofs/custom.img | cpio -diu
346 # clean out unnecessary rpm lib
347 echo "* clearing var/lib/rpm/*"
350 # bootcd requires this directory
351 mkdir -p mnt/confdevice
353 # relocate various directory to /tmp
355 ln -fs /tmp/root root
356 ln -fs /sbin/init linuxrc
357 ln -fs /tmp/resolv.conf etc/resolv.conf
358 ln -fs /tmp/etc/mtab etc/mtab
360 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
362 echo "* renaming dirs in ./etc"
364 for dir in `find * -type d -prune | grep -v rc.d`; do
366 ln -fs /tmp/etc/${dir} ${dir}
370 echo "* renaming dirs in ./var"
371 # rename all top-level directories and put in a symlink to /tmp/var
373 for dir in `find * -type d -prune`; do
375 ln -fs /tmp/var/${dir} ${dir}
379 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
380 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
382 cat >./etc/fstab <<EOF
383 /dev/ram0 / cramfs ro 0 0
384 none /dev/pts devpts gid=5,mode=620 0 0
385 none /proc proc defaults 0 0
386 none /sys sysfs defaults 0 0
392 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
393 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
398 # update etc/inittab to start with pl_rsysinit
399 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
401 # modify inittab to have a serial console
402 echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
403 # and let root log in
404 echo "ttyS0" >> etc/securetty
406 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
407 etcsize=$(du -s ./etc | awk '{ print $1 }')
408 varsize=$(du -s ./var | awk '{ print $1 }')
409 let msize=($varsize+$etcsize+8192)/1024
412 # generate pl_rsysinit
413 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
415 # generated by build.sh
416 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
417 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
420 touch /tmp/etc/resolv.conf
425 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
427 # copy over directory contents of all _o directories from /etc and /var
428 # /tmp/etc and /tmp/var
430 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
433 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
437 # hand over to pl_sysinit
438 echo "pl_rsysinit: handing over to pl_sysinit"
439 /etc/init.d/pl_sysinit
441 chmod +x etc/rc.d/init.d/pl_rsysinit
447 # create the cramfs image
448 echo "* Creating cramfs image"
449 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
450 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
455 # Create ISO CRAMFS image
456 function build_iso_cramfs()
461 echo "* Creating ISO CRAMFS-based image"
463 local tmp="${BUILDTMP}/cramfs-iso"
465 push_cleanup rm -rf $tmp
466 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
467 cat >$tmp/isolinux.cfg <<EOF
468 ${serial:+SERIAL 0 9600}
470 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
476 cp ${BUILDTMP}/cramfs.img $tmp
485 # Create USB CRAMFS based image
486 function build_usb_cramfs()
491 echo "* Creating USB CRAMFS based image"
493 let vfat_size=${cramfs_size}+$FREE_SPACE
495 # Make VFAT filesystem for USB
496 mkfs.vfat -C "$usb" $vfat_size
499 echo "* Populating USB with overlay images and cramfs"
500 mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
501 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
503 # Use syslinux instead of isolinux to make the image bootable
504 tmp="${BUILDTMP}/syslinux.cfg"
506 ${serial:+SERIAL 0 9600}
508 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
513 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
516 echo "* Making USB CRAMFS based image bootable"
517 $srcdir/syslinux/unix/syslinux "$usb"
520 function type_to_name()
524 s/usb_serial$/-serial.usb/;
526 s/iso_serial$/-serial.iso/;
527 s/usb_cramfs$/-cramfs.usb/;
528 s/usb_cramfs_serial$/-cramfs-serial.usb/;
529 s/iso_cramfs$/-cramfs.iso/;
530 s/iso_cramfs_serial$/-cramfs-serial.iso/;
534 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
538 tname=`type_to_name $t`
539 if [[ "$t" == *_serial ]]; then
541 t=`echo $t | sed 's/_serial$//'`
543 build_$t "${OUTPUT_BASE}${tname}" $serial $CUSTOM_DIR