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-2006 The Trustees of Princeton University
13 # $Id: build.sh,v 1.42 2006/11/22 20:40:48 mef Exp $
16 PATH=/sbin:/bin:/usr/sbin:/usr/bin
19 NODE_CONFIGURATION_FILE=
21 # Leave 4 MB of free space
26 echo "Usage: build.sh [OPTION]..."
27 echo " -c name (Deprecated) Static configuration to use (default: $CONFIGURATION)"
28 echo " -f planet.cnf Node to customize CD for (default: none)"
29 echo " -a Build all images (default: only base images)"
30 echo " -h This message"
35 while getopts "c:f:ah" opt ; do
41 NODE_CONFIGURATION_FILE=$OPTARG
52 # Do not tolerate errors
55 # Change to our source directory
56 srcdir=$(cd $(dirname $0) && pwd -P)
60 isofs=$PWD/build/isofs
62 # Build reference image if it does not exist. This should only need to
63 # be executed once at build time, never at run time.
64 if [ ! -f $isofs/bootcd.img ] ; then
68 # build/version.txt written by prep.sh
69 BOOTCD_VERSION=$(cat build/version.txt)
71 if [ -f /etc/planetlab/plc_config ] ; then
72 # Source PLC configuration
73 . /etc/planetlab/plc_config
76 ### This support for backwards compatibility can be taken out in the
77 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
78 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
80 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
81 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
84 # If PLC configuration is not valid, try a static configuration
85 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
86 # (Deprecated) Source static configuration
87 . configurations/$CONFIGURATION/configuration
89 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
90 PLC_WWW_HOST="www.planet-lab.org"
92 if [ -n "$EXTRA_VERSION" ] ; then
93 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
95 PLC_BOOT_HOST=$PRIMARY_SERVER
96 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
97 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
98 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
101 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
103 echo "* Building images for $FULL_VERSION_STRING"
105 # From within a myplc chroot /tmp is too small to build
106 # all possible images, whereas /data is part of the host
107 # filesystem and usually has sufficient space. What we
108 # should do is check whether the expected amount of space
110 [ -d /data ] && BUILDTMP=/data || BUILDTMP=/tmp
112 # Root of the ISO and USB images
113 echo "* Populating root filesystem..."
114 overlay=$(mktemp -d ${BUILDTMP}/overlay.XXXXXX)
115 install -d -m 755 $overlay
116 trap "rm -rf $overlay" ERR INT
118 # Create version files
119 echo "* Creating version files"
121 # Boot Manager compares pl_version in both places to make sure that
122 # the right CD is mounted. We used to boot from an initrd and mount
123 # the CD on /usr. Now we just run everything out of the initrd.
124 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
125 mkdir -p $(dirname $file)
126 echo "$FULL_VERSION_STRING" >$file
129 # Install boot server configuration files
130 echo "* Installing boot server configuration files"
132 # We always intended to bring up and support backup boot servers,
133 # but never got around to it. Just install the same parameters for
135 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
136 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
137 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
138 echo "$PLC_BOOT_HOST" >$dir/boot_server
139 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
140 echo "/boot/" >$dir/boot_server_path
143 # (Deprecated) Install old-style boot server configuration files
144 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
145 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
146 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
147 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
148 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
150 # Generate /etc/issue
151 echo "* Generating /etc/issue"
153 if [ "$PLC_WWW_PORT" = "443" ] ; then
154 PLC_WWW_URL="https://$PLC_WWW_HOST/"
155 elif [ "$PLC_WWW_PORT" != "80" ] ; then
156 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
158 PLC_WWW_URL="http://$PLC_WWW_HOST/"
161 mkdir -p $overlay/etc
162 cat >$overlay/etc/issue <<EOF
168 This machine is a node in the $PLC_NAME distributed network. It has
169 not fully booted yet. If you have cancelled the boot process at the
170 request of $PLC_NAME Support, please follow the instructions provided
171 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
173 Console login at this point is restricted to root. Provide the root
174 password of the default $PLC_NAME Central administrator account at the
175 time that this CD was created.
180 echo "* Setting root password"
182 if [ -z "$ROOT_PASSWORD" ] ; then
183 # Generate an encrypted password with crypt() if not defined
184 # in a static configuration.
185 ROOT_PASSWORD=$(python <<EOF
186 import crypt, random, string
187 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
188 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
193 # build/passwd copied out by prep.sh
194 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
197 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
198 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
199 echo "* Installing node configuration file"
200 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
203 # Pack overlay files into a compressed archive
204 echo "* Compressing overlay image"
205 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
210 # Calculate ramdisk size (total uncompressed size of both archives)
211 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img | tail -1 | awk '{ print $2; }') # bytes
212 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
214 # Write isolinux configuration
215 echo "$FULL_VERSION_STRING" >$isofs/pl_version
216 cat >$isofs/isolinux.cfg <<EOF
218 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
224 # Change back to output directory
228 echo "* Creating ISO image"
229 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
231 -R -allow-leading-dots -J -r \
232 -b isolinux.bin -c boot.cat \
233 -no-emul-boot -boot-load-size 4 -boot-info-table \
236 echo "* Creating ISO image with serial line support"
237 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.iso"
238 cat >$isofs/isolinux.cfg <<EOF
246 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw console=ttyS0,115200n8
249 -R -allow-leading-dots -J -r \
250 -b isolinux.bin -c boot.cat \
251 -no-emul-boot -boot-load-size 4 -boot-info-table \
255 echo -n "* Creating USB image... "
256 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
258 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + $FREE_SPACE))
261 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
262 mount -o loop "$usb" $tmp
263 trap "umount $tmp; rm -rf $tmp" ERR INT
266 echo -n " populating USB image... "
267 (cd $isofs && find . | cpio -p -d -u $tmp/)
269 # Use syslinux instead of isolinux to make the image bootable
270 rm -f $tmp/isolinux.cfg
271 cat >$tmp/syslinux.cfg <<EOF
273 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
282 echo "making USB image bootable."
283 $srcdir/syslinux/unix/syslinux "$usb"
286 # Create USB image with serial line support
287 echo -n "* Creating USB image... "
288 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.usb"
290 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + $FREE_SPACE))
293 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
294 mount -o loop "$usb" $tmp
295 trap "umount $tmp; rm -rf $tmp" ERR INT
298 echo -n " populating USB image... "
299 (cd $isofs && find . | cpio -p -d -u $tmp/)
301 # Use syslinux instead of isolinux to make the image bootable
302 rm -f $tmp/isolinux.cfg
303 cat >$tmp/syslinux.cfg <<EOF
311 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw console=ttyS0,115200n8
318 echo "making USB image with serial line support bootable."
319 $srcdir/syslinux/unix/syslinux "$usb"
321 [ $ALL -eq 0 ] && exit 0
323 # Setup CRAMFS related support
324 echo "* Setting up CRAMFS-based images"
325 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
326 cramfs=$(mktemp ${BUILDTMP}/cramfs.XXXXXX)
327 trap "$tmp; rm -rf $tmp $cramfs" ERR INT
329 gzip -d -c $isofs/bootcd.img | cpio -diu
330 gzip -d -c $isofs/overlay.img | cpio -diu
332 # clean out unnecessary rpm lib
333 echo "* clearing var/lib/rpm/*"
336 # bootcd requires this directory
337 mkdir -p mnt/confdevice
339 # relocate various directory to /tmp
341 ln -fs /tmp/root root
342 ln -fs /sbin/init linuxrc
343 ln -fs /tmp/resolv.conf etc/resolv.conf
344 ln -fs /tmp/etc/mtab etc/mtab
346 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
348 echo "* renaming dirs in ./etc"
350 for dir in `find * -type d -prune | grep -v rc.d`; do mv ${dir} ${dir}_o; ln -fs /tmp/etc/${dir} ${dir} ; done
353 echo "* renaming dirs in ./var"
354 # rename all top-level directories and put in a symlink to /tmp/var
356 for dir in `find * -type d -prune`; do mv ${dir} ${dir}_o; ln -fs /tmp/var/${dir} ${dir} ; done
359 #overwrite fstab to mount / as cramfs and /tmp as tmpfs
360 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
362 cat >./etc/fstab <<EOF
363 /dev/ram0 / cramfs ro 0 0
364 none /dev/pts devpts gid=5,mode=620 0 0
365 none /proc proc defaults 0 0
366 none /sys sysfs defaults 0 0
372 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
373 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
378 # update etc/inittab to start with pl_rsysinit
379 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
381 # modify inittab to have a serial console
382 echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
383 # and let root log in
384 echo "ttyS0" >> etc/securetty
386 #calculate the size of /tmp based on the size of /etc & /var + 8MB slack
387 etcsize=$(du -s ./etc | awk '{ print $1 }')
388 varsize=$(du -s ./etc | awk '{ print $1 }')
389 let msize=($vsize+$esize+8192)/1024
392 # generate pl_rsysinit
393 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
395 # generated by build.sh
396 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
397 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
400 touch /tmp/etc/resolv.conf
405 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
407 # copy over directory contents of all _o directories from /etc and /var
408 # /tmp/etc and /tmp/var
410 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
413 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
417 # hand over to pl_sysinit
418 echo "pl_rsysinit: handing over to pl_sysinit"
419 /etc/init.d/pl_sysinit
421 chmod +x etc/rc.d/init.d/pl_rsysinit
427 #create the cramfs image
428 echo "* Creating cramfs image"
429 mkfs.cramfs $tmp/ $cramfs
430 cramfs_size=$(($(du -sk $cramfs | awk '{ print $1; }')))
431 mv $cramfs ${BUILDTMP}/cramfs.img
435 # Create ISO CRAMFS image
436 echo "* Creating ISO CRAMFS-based image"
437 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.iso"
439 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
440 trap "$tmp; rm -rf $tmp" ERR INT
441 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
442 cat >$tmp/isolinux.cfg <<EOF
444 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
450 cp ${BUILDTMP}/cramfs.img $tmp
452 -R -allow-leading-dots -J -r \
453 -b isolinux.bin -c boot.cat \
454 -no-emul-boot -boot-load-size 4 -boot-info-table \
457 # Create ISO CRAMFS image with serial line support
458 echo "* Creating ISO image with cramfs and serial line support"
459 cat >$tmp/isolinux.cfg <<EOF
467 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,115200n8
470 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.iso"
472 -R -allow-leading-dots -J -r \
473 -b isolinux.bin -c boot.cat \
474 -no-emul-boot -boot-load-size 4 -boot-info-table \
480 # Create USB CRAMFS based image
481 echo "* Creating USB CRAMFS based image"
482 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.usb"
484 let vfat_size=${cramfs_size}+$FREE_SPACE
486 # Make VFAT filesystem for USB
487 mkfs.vfat -C "$usb" $vfat_size
490 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
491 mount -o loop "$usb" $tmp
492 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
495 echo "* Populating USB with overlay images and cramfs"
496 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
497 cp ${BUILDTMP}/cramfs.img $tmp/
499 # Use syslinux instead of isolinux to make the image bootable
500 cat >$tmp/syslinux.cfg <<EOF
506 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
512 echo "* Making USB CRAMFS based image bootable"
513 $srcdir/syslinux/unix/syslinux "$usb"
515 # Create USB CRAMFS based image w/ serial line support
516 echo "* Creating USB CRAMFS based image w/ serial line support"
517 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.usb"
519 let vfat_size=${cramfs_size}+$FREE_SPACE
521 # Make VFAT filesystem for USB
522 mkfs.vfat -C "$usb" $vfat_size
525 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
526 mount -o loop "$usb" $tmp
527 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
530 echo "* Populating USB with overlay images and cramfs"
531 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
532 cp ${BUILDTMP}/cramfs.img $tmp/
534 # Use syslinux instead of isolinux to make the image bootable
535 cat >$tmp/syslinux.cfg <<EOF
543 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,9600n8
549 echo "* Making USB CRAMFS based image /w serial line support bootable"
550 $srcdir/syslinux/unix/syslinux "$usb"