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.40 2006/07/25 23:51:39 mlhuang Exp $
16 PATH=/sbin:/bin:/usr/sbin:/usr/bin
19 NODE_CONFIGURATION_FILE=
24 echo "Usage: build.sh [OPTION]..."
25 echo " -c name (Deprecated) Static configuration to use (default: $CONFIGURATION)"
26 echo " -f planet.cnf Node to customize CD for (default: none)"
27 echo " -a Build all images (default: only base images)"
28 echo " -h This message"
33 while getopts "c:f:ah" opt ; do
39 NODE_CONFIGURATION_FILE=$OPTARG
50 # Do not tolerate errors
53 # Change to our source directory
54 srcdir=$(cd $(dirname $0) && pwd -P)
58 isofs=$PWD/build/isofs
60 # Build reference image if it does not exist. This should only need to
61 # be executed once at build time, never at run time.
62 if [ ! -f $isofs/bootcd.img ] ; then
66 # build/version.txt written by prep.sh
67 BOOTCD_VERSION=$(cat build/version.txt)
69 if [ -f /etc/planetlab/plc_config ] ; then
70 # Source PLC configuration
71 . /etc/planetlab/plc_config
74 ### This support for backwards compatibility can be taken out in the
75 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
76 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
78 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
79 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
82 # If PLC configuration is not valid, try a static configuration
83 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
84 # (Deprecated) Source static configuration
85 . configurations/$CONFIGURATION/configuration
87 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
88 PLC_WWW_HOST="www.planet-lab.org"
90 if [ -n "$EXTRA_VERSION" ] ; then
91 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
93 PLC_BOOT_HOST=$PRIMARY_SERVER
94 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
95 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
96 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
99 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
101 echo "* Building images for $FULL_VERSION_STRING"
103 # From within a myplc chroot /tmp is too small to build
104 # all possible images, whereas /data is part of the host
105 # filesystem and usually has sufficient space. What we
106 # should do is check whether the expected amount of space
108 [ -d /data ] && BUILDTMP=/data || BUILDTMP=/tmp
110 # Root of the ISO and USB images
111 echo "* Populating root filesystem..."
112 overlay=$(mktemp -d ${BUILDTMP}/overlay.XXXXXX)
113 install -d -m 755 $overlay
114 trap "rm -rf $overlay" ERR INT
116 # Create version files
117 echo "* Creating version files"
119 # Boot Manager compares pl_version in both places to make sure that
120 # the right CD is mounted. We used to boot from an initrd and mount
121 # the CD on /usr. Now we just run everything out of the initrd.
122 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
123 mkdir -p $(dirname $file)
124 echo "$FULL_VERSION_STRING" >$file
127 # Install boot server configuration files
128 echo "* Installing boot server configuration files"
130 # We always intended to bring up and support backup boot servers,
131 # but never got around to it. Just install the same parameters for
133 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
134 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
135 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
136 echo "$PLC_BOOT_HOST" >$dir/boot_server
137 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
138 echo "/boot/" >$dir/boot_server_path
141 # (Deprecated) Install old-style boot server configuration files
142 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
143 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
144 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
145 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
146 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
148 # Generate /etc/issue
149 echo "* Generating /etc/issue"
151 if [ "$PLC_WWW_PORT" = "443" ] ; then
152 PLC_WWW_URL="https://$PLC_WWW_HOST/"
153 elif [ "$PLC_WWW_PORT" != "80" ] ; then
154 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
156 PLC_WWW_URL="http://$PLC_WWW_HOST/"
159 mkdir -p $overlay/etc
160 cat >$overlay/etc/issue <<EOF
166 This machine is a node in the $PLC_NAME distributed network. It has
167 not fully booted yet. If you have cancelled the boot process at the
168 request of $PLC_NAME Support, please follow the instructions provided
169 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
171 Console login at this point is restricted to root. Provide the root
172 password of the default $PLC_NAME Central administrator account at the
173 time that this CD was created.
178 echo "* Setting root password"
180 if [ -z "$ROOT_PASSWORD" ] ; then
181 # Generate an encrypted password with crypt() if not defined
182 # in a static configuration.
183 ROOT_PASSWORD=$(python <<EOF
184 import crypt, random, string
185 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
186 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
191 # build/passwd copied out by prep.sh
192 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
195 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
196 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
197 echo "* Installing node configuration file"
198 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
201 # Pack overlay files into a compressed archive
202 echo "* Compressing overlay image"
203 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
208 # Calculate ramdisk size (total uncompressed size of both archives)
209 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img | tail -1 | awk '{ print $2; }') # bytes
210 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
212 # Write isolinux configuration
213 echo "$FULL_VERSION_STRING" >$isofs/pl_version
214 cat >$isofs/isolinux.cfg <<EOF
216 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
222 # Change back to output directory
226 echo "* Creating ISO image"
227 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
229 -R -allow-leading-dots -J -r \
230 -b isolinux.bin -c boot.cat \
231 -no-emul-boot -boot-load-size 4 -boot-info-table \
234 echo "* Creating ISO image with serial line support"
235 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.iso"
236 cat >$isofs/isolinux.cfg <<EOF
244 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw console=ttyS0,115200n8
247 -R -allow-leading-dots -J -r \
248 -b isolinux.bin -c boot.cat \
249 -no-emul-boot -boot-load-size 4 -boot-info-table \
253 echo -n "* Creating USB image... "
254 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
256 # Leave 1 MB of free space on the VFAT filesystem
257 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
260 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
261 mount -o loop "$usb" $tmp
262 trap "umount $tmp; rm -rf $tmp" ERR INT
265 echo -n " populating USB image... "
266 (cd $isofs && find . | cpio -p -d -u $tmp/)
268 # Use syslinux instead of isolinux to make the image bootable
269 rm -f $tmp/isolinux.cfg
270 cat >$tmp/syslinux.cfg <<EOF
272 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
281 echo "making USB image bootable."
282 $srcdir/syslinux/unix/syslinux "$usb"
285 # Create USB image with serial line support
286 echo -n "* Creating USB image... "
287 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.usb"
289 # Leave 1 MB of free space on the VFAT filesystem
290 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
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 # Leave 1 MB of free space on the VFAT filesystem
431 cramfs_size=$(($(du -sk $cramfs | awk '{ print $1; }')))
432 mv $cramfs ${BUILDTMP}/cramfs.img
436 # Create ISO CRAMFS image
437 echo "* Creating ISO CRAMFS-based image"
438 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.iso"
440 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
441 trap "$tmp; rm -rf $tmp" ERR INT
442 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
443 cat >$tmp/isolinux.cfg <<EOF
445 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
451 cp ${BUILDTMP}/cramfs.img $tmp
453 -R -allow-leading-dots -J -r \
454 -b isolinux.bin -c boot.cat \
455 -no-emul-boot -boot-load-size 4 -boot-info-table \
458 # Create ISO CRAMFS image with serial line support
459 echo "* Creating ISO image with cramfs and serial line support"
460 cat >$tmp/isolinux.cfg <<EOF
468 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,115200n8
471 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.iso"
473 -R -allow-leading-dots -J -r \
474 -b isolinux.bin -c boot.cat \
475 -no-emul-boot -boot-load-size 4 -boot-info-table \
481 # Create USB CRAMFS based image
482 echo "* Creating USB CRAMFS based image"
483 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.usb"
485 # leave 1MB of space on the USB VFAT
486 let vfat_size=${cramfs_size}+2048
488 # Make VFAT filesystem for USB
489 mkfs.vfat -C "$usb" $vfat_size
492 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
493 mount -o loop "$usb" $tmp
494 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
497 echo "* Populating USB with overlay images and cramfs"
498 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
499 cp ${BUILDTMP}/cramfs.img $tmp/
501 # Use syslinux instead of isolinux to make the image bootable
502 cat >$tmp/syslinux.cfg <<EOF
508 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
514 echo "* Making USB CRAMFS based image bootable"
515 $srcdir/syslinux/unix/syslinux "$usb"
517 # Create USB CRAMFS based image w/ serial line support
518 echo "* Creating USB CRAMFS based image w/ serial line support"
519 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.usb"
521 # leave 4MB of space on the USB VFAT
522 let vfat_size=${cramfs_size}+2048
524 # Make VFAT filesystem for USB
525 mkfs.vfat -C "$usb" $vfat_size
528 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
529 mount -o loop "$usb" $tmp
530 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
533 echo "* Populating USB with overlay images and cramfs"
534 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
535 cp ${BUILDTMP}/cramfs.img $tmp/
537 # Use syslinux instead of isolinux to make the image bootable
538 cat >$tmp/syslinux.cfg <<EOF
546 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,9600n8
552 echo "* Making USB CRAMFS based image /w serial line support bootable"
553 $srcdir/syslinux/unix/syslinux "$usb"