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}; do
159 ln -s "$i" "${BUILDTMP}/isofs"
161 cp "$isofs/isolinux.bin" "${BUILDTMP}/isofs"
162 isofs="${BUILDTMP}/isofs"
164 # Root of the ISO and USB images
165 echo "* Populating root filesystem..."
166 overlay="${BUILDTMP}/overlay"
167 install -d -m 755 $overlay
168 push_cleanup rm -fr $overlay
170 # Create version files
171 echo "* Creating version files"
173 # Boot Manager compares pl_version in both places to make sure that
174 # the right CD is mounted. We used to boot from an initrd and mount
175 # the CD on /usr. Now we just run everything out of the initrd.
176 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
177 mkdir -p $(dirname $file)
178 echo "$FULL_VERSION_STRING" >$file
181 # Install boot server configuration files
182 echo "* Installing boot server configuration files"
184 # We always intended to bring up and support backup boot servers,
185 # but never got around to it. Just install the same parameters for
187 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
188 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
189 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
190 echo "$PLC_BOOT_HOST" >$dir/boot_server
191 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
192 echo "/boot/" >$dir/boot_server_path
195 # (Deprecated) Install old-style boot server configuration files
196 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
197 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
198 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
199 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
200 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
202 # Generate /etc/issue
203 echo "* Generating /etc/issue"
205 if [ "$PLC_WWW_PORT" = "443" ] ; then
206 PLC_WWW_URL="https://$PLC_WWW_HOST/"
207 elif [ "$PLC_WWW_PORT" != "80" ] ; then
208 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
210 PLC_WWW_URL="http://$PLC_WWW_HOST/"
213 mkdir -p $overlay/etc
214 cat >$overlay/etc/issue <<EOF
220 This machine is a node in the $PLC_NAME distributed network. It has
221 not fully booted yet. If you have cancelled the boot process at the
222 request of $PLC_NAME Support, please follow the instructions provided
223 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
225 Console login at this point is restricted to root. Provide the root
226 password of the default $PLC_NAME Central administrator account at the
227 time that this CD was created.
232 echo "* Setting root password"
234 if [ -z "$ROOT_PASSWORD" ] ; then
235 # Generate an encrypted password with crypt() if not defined
236 # in a static configuration.
237 ROOT_PASSWORD=$(python <<EOF
238 import crypt, random, string
239 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
240 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
245 # build/passwd copied out by prep.sh
246 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
249 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
250 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
251 echo "* Installing node configuration file"
252 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
255 # Pack overlay files into a compressed archive
256 echo "* Compressing overlay image"
257 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
262 if [ -n "$CUSTOM_DIR" ]; then
263 echo "* Compressing custom image"
264 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
267 # Calculate ramdisk size (total uncompressed size of both archives)
268 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
269 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
271 echo "$FULL_VERSION_STRING" >$isofs/pl_version
281 # Write isolinux configuration
282 cat >$isofs/isolinux.cfg <<EOF
283 ${serial:+SERIAL 0 115200}
285 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
292 echo "* Creating ISO image"
301 echo -n "* Creating USB image... "
306 mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
309 echo -n " populating USB image... "
310 mcopy -bsQ -i "$usb" "$isofs"/* ::/
312 # Use syslinux instead of isolinux to make the image bootable
313 tmp="${BUILDTMP}/syslinux.cfg"
315 ${serial:+SERIAL 0 115200}
317 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
322 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
323 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
326 echo "making USB image bootable."
327 $srcdir/syslinux/unix/syslinux "$usb"
331 # Setup CRAMFS related support
332 function prepare_cramfs()
334 [ -n "$CRAMFS_PREPARED" ] && return 0
337 echo "* Setting up CRAMFS-based images"
338 local tmp="${BUILDTMP}/cramfs-tree"
340 push_cleanup rm -rf $tmp
342 gzip -d -c $isofs/bootcd.img | cpio -diu
343 gzip -d -c $isofs/overlay.img | cpio -diu
344 [ -n "$custom" ] && \
345 gzip -d -c $isofs/custom.img | cpio -diu
347 # clean out unnecessary rpm lib
348 echo "* clearing var/lib/rpm/*"
351 # bootcd requires this directory
352 mkdir -p mnt/confdevice
354 # relocate various directory to /tmp
356 ln -fs /tmp/root root
357 ln -fs /sbin/init linuxrc
358 ln -fs /tmp/resolv.conf etc/resolv.conf
359 ln -fs /tmp/etc/mtab etc/mtab
361 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
363 echo "* renaming dirs in ./etc"
365 for dir in `find * -type d -prune | grep -v rc.d`; do
367 ln -fs /tmp/etc/${dir} ${dir}
371 echo "* renaming dirs in ./var"
372 # rename all top-level directories and put in a symlink to /tmp/var
374 for dir in `find * -type d -prune`; do
376 ln -fs /tmp/var/${dir} ${dir}
380 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
381 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
383 cat >./etc/fstab <<EOF
384 /dev/ram0 / cramfs ro 0 0
385 none /dev/pts devpts gid=5,mode=620 0 0
386 none /proc proc defaults 0 0
387 none /sys sysfs defaults 0 0
393 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
394 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
399 # update etc/inittab to start with pl_rsysinit
400 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
402 # modify inittab to have a serial console
403 echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
404 # and let root log in
405 echo "ttyS0" >> etc/securetty
407 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
408 etcsize=$(du -s ./etc | awk '{ print $1 }')
409 varsize=$(du -s ./var | awk '{ print $1 }')
410 let msize=($varsize+$etcsize+8192)/1024
413 # generate pl_rsysinit
414 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
416 # generated by build.sh
417 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
418 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
421 touch /tmp/etc/resolv.conf
426 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
428 # copy over directory contents of all _o directories from /etc and /var
429 # /tmp/etc and /tmp/var
431 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
434 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
438 # hand over to pl_sysinit
439 echo "pl_rsysinit: handing over to pl_sysinit"
440 /etc/init.d/pl_sysinit
442 chmod +x etc/rc.d/init.d/pl_rsysinit
446 # create the cramfs image
447 echo "* Creating cramfs image"
448 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
449 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
454 # Create ISO CRAMFS image
455 function build_iso_cramfs()
460 echo "* Creating ISO CRAMFS-based image"
462 local tmp="${BUILDTMP}/cramfs-iso"
464 push_cleanup rm -rf $tmp
465 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
466 cat >$tmp/isolinux.cfg <<EOF
467 ${serial:+SERIAL 0 9600}
469 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
475 cp ${BUILDTMP}/cramfs.img $tmp
484 # Create USB CRAMFS based image
485 function build_usb_cramfs()
490 echo "* Creating USB CRAMFS based image"
492 let vfat_size=${cramfs_size}+$FREE_SPACE
494 # Make VFAT filesystem for USB
495 mkfs.vfat -C "$usb" $vfat_size
498 echo "* Populating USB with overlay images and cramfs"
499 mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
500 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
502 # Use syslinux instead of isolinux to make the image bootable
503 tmp="${BUILDTMP}/syslinux.cfg"
505 ${serial:+SERIAL 0 9600}
507 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
512 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
515 echo "* Making USB CRAMFS based image bootable"
516 $srcdir/syslinux/unix/syslinux "$usb"
519 function type_to_name()
523 s/usb_serial$/-serial.usb/;
525 s/iso_serial$/-serial.iso/;
526 s/usb_cramfs$/-cramfs.usb/;
527 s/usb_cramfs_serial$/-cramfs-serial.usb/;
528 s/iso_cramfs$/-cramfs.iso/;
529 s/iso_cramfs_serial$/-cramfs-serial.iso/;
533 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
537 tname=`type_to_name $t`
538 if [[ "$t" == *_serial ]]; then
540 t=`echo $t | sed 's/_serial$//'`
542 build_$t "${OUTPUT_BASE}${tname}" $serial $CUSTOM_DIR