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 /usr/tmp is too small
104 # to build 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
109 if [ -d /data ] ; then
110 isreadonly=$(mktemp /data/isreadonly.XXXXXX || /bin/true)
111 if [ -n "$isreadonly" ] ; then
117 # Root of the ISO and USB images
118 echo "* Populating root filesystem..."
119 overlay=$(mktemp -d ${BUILDTMP}/overlay.XXXXXX)
120 install -d -m 755 $overlay
121 trap "rm -rf $overlay" ERR INT
123 # Create version files
124 echo "* Creating version files"
126 # Boot Manager compares pl_version in both places to make sure that
127 # the right CD is mounted. We used to boot from an initrd and mount
128 # the CD on /usr. Now we just run everything out of the initrd.
129 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
130 mkdir -p $(dirname $file)
131 echo "$FULL_VERSION_STRING" >$file
134 # Install boot server configuration files
135 echo "* Installing boot server configuration files"
137 # We always intended to bring up and support backup boot servers,
138 # but never got around to it. Just install the same parameters for
140 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
141 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
142 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
143 echo "$PLC_BOOT_HOST" >$dir/boot_server
144 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
145 echo "/boot/" >$dir/boot_server_path
148 # (Deprecated) Install old-style boot server configuration files
149 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
150 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
151 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
152 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
153 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
155 # Generate /etc/issue
156 echo "* Generating /etc/issue"
158 if [ "$PLC_WWW_PORT" = "443" ] ; then
159 PLC_WWW_URL="https://$PLC_WWW_HOST/"
160 elif [ "$PLC_WWW_PORT" != "80" ] ; then
161 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
163 PLC_WWW_URL="http://$PLC_WWW_HOST/"
166 mkdir -p $overlay/etc
167 cat >$overlay/etc/issue <<EOF
173 This machine is a node in the $PLC_NAME distributed network. It has
174 not fully booted yet. If you have cancelled the boot process at the
175 request of $PLC_NAME Support, please follow the instructions provided
176 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
178 Console login at this point is restricted to root. Provide the root
179 password of the default $PLC_NAME Central administrator account at the
180 time that this CD was created.
185 echo "* Setting root password"
187 if [ -z "$ROOT_PASSWORD" ] ; then
188 # Generate an encrypted password with crypt() if not defined
189 # in a static configuration.
190 ROOT_PASSWORD=$(python <<EOF
191 import crypt, random, string
192 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
193 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
198 # build/passwd copied out by prep.sh
199 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
202 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
203 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
204 echo "* Installing node configuration file"
205 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
208 # Pack overlay files into a compressed archive
209 echo "* Compressing overlay image"
210 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
215 # Calculate ramdisk size (total uncompressed size of both archives)
216 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img | tail -1 | awk '{ print $2; }') # bytes
217 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
219 # Write isolinux configuration
220 echo "$FULL_VERSION_STRING" >$isofs/pl_version
221 cat >$isofs/isolinux.cfg <<EOF
223 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
229 # Change back to output directory
233 echo "* Creating ISO image"
234 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
236 -R -allow-leading-dots -J -r \
237 -b isolinux.bin -c boot.cat \
238 -no-emul-boot -boot-load-size 4 -boot-info-table \
241 echo "* Creating ISO image with serial line support"
242 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.iso"
243 cat >$isofs/isolinux.cfg <<EOF
251 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw console=ttyS0,115200n8
254 -R -allow-leading-dots -J -r \
255 -b isolinux.bin -c boot.cat \
256 -no-emul-boot -boot-load-size 4 -boot-info-table \
260 echo -n "* Creating USB image... "
261 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
263 # Leave 1 MB of free space on the VFAT filesystem
264 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
267 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
268 mount -o loop "$usb" $tmp
269 trap "umount $tmp; rm -rf $tmp" ERR INT
272 echo -n " populating USB image... "
273 (cd $isofs && find . | cpio -p -d -u $tmp/)
275 # Use syslinux instead of isolinux to make the image bootable
276 rm -f $tmp/isolinux.cfg
277 cat >$tmp/syslinux.cfg <<EOF
279 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
288 echo "making USB image bootable."
289 $srcdir/syslinux/unix/syslinux "$usb"
292 # Create USB image with serial line support
293 echo -n "* Creating USB image... "
294 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.usb"
296 # Leave 1 MB of free space on the VFAT filesystem
297 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
300 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
301 mount -o loop "$usb" $tmp
302 trap "umount $tmp; rm -rf $tmp" ERR INT
305 echo -n " populating USB image... "
306 (cd $isofs && find . | cpio -p -d -u $tmp/)
308 # Use syslinux instead of isolinux to make the image bootable
309 rm -f $tmp/isolinux.cfg
310 cat >$tmp/syslinux.cfg <<EOF
318 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw console=ttyS0,115200n8
325 echo "making USB image with serial line support bootable."
326 $srcdir/syslinux/unix/syslinux "$usb"
328 [ $ALL -eq 0 ] && exit 0
330 # Setup CRAMFS related support
331 echo "* Setting up CRAMFS-based images"
332 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
333 cramfs=$(mktemp ${BUILDTMP}/cramfs.XXXXXX)
334 trap "$tmp; rm -rf $tmp $cramfs" ERR INT
336 gzip -d -c $isofs/bootcd.img | cpio -diu
337 gzip -d -c $isofs/overlay.img | cpio -diu
339 # clean out unnecessary rpm lib
340 echo "* clearing var/lib/rpm/*"
343 # bootcd requires this directory
344 mkdir -p mnt/confdevice
346 # relocate various directory to /tmp
348 ln -fs /tmp/root root
349 ln -fs /sbin/init linuxrc
350 ln -fs /tmp/resolv.conf etc/resolv.conf
351 ln -fs /tmp/etc/mtab etc/mtab
353 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
355 echo "* renaming dirs in ./etc"
357 for dir in `find * -type d -prune | grep -v rc.d`; do mv ${dir} ${dir}_o; ln -fs /tmp/etc/${dir} ${dir} ; done
360 echo "* renaming dirs in ./var"
361 # rename all top-level directories and put in a symlink to /tmp/var
363 for dir in `find * -type d -prune`; do mv ${dir} ${dir}_o; ln -fs /tmp/var/${dir} ${dir} ; done
366 #overwrite fstab to mount / as cramfs and /tmp as tmpfs
367 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
369 cat >./etc/fstab <<EOF
370 /dev/ram0 / cramfs ro 0 0
371 none /dev/pts devpts gid=5,mode=620 0 0
372 none /proc proc defaults 0 0
373 none /sys sysfs defaults 0 0
379 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
380 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
385 # update etc/inittab to start with pl_rsysinit
386 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
388 # modify inittab to have a serial console
389 echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
390 # and let root log in
391 echo "ttyS0" >> etc/securetty
393 #calculate the size of /tmp based on the size of /etc & /var + 8MB slack
394 etcsize=$(du -s ./etc | awk '{ print $1 }')
395 varsize=$(du -s ./etc | awk '{ print $1 }')
396 let msize=($vsize+$esize+8192)/1024
399 # generate pl_rsysinit
400 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
402 # generated by build.sh
403 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
404 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
407 touch /tmp/etc/resolv.conf
412 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
414 # copy over directory contents of all _o directories from /etc and /var
415 # /tmp/etc and /tmp/var
417 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
420 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
424 # hand over to pl_sysinit
425 echo "pl_rsysinit: handing over to pl_sysinit"
426 /etc/init.d/pl_sysinit
428 chmod +x etc/rc.d/init.d/pl_rsysinit
434 #create the cramfs image
435 echo "* Creating cramfs image"
436 mkfs.cramfs $tmp/ $cramfs
437 # Leave 1 MB of free space on the VFAT filesystem
438 cramfs_size=$(($(du -sk $cramfs | awk '{ print $1; }')))
439 mv $cramfs ${BUILDTMP}/cramfs.img
443 # Create ISO CRAMFS image
444 echo "* Creating ISO CRAMFS-based image"
445 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.iso"
447 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
448 trap "$tmp; rm -rf $tmp" ERR INT
449 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
450 cat >$tmp/isolinux.cfg <<EOF
452 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
458 cp ${BUILDTMP}/cramfs.img $tmp
460 -R -allow-leading-dots -J -r \
461 -b isolinux.bin -c boot.cat \
462 -no-emul-boot -boot-load-size 4 -boot-info-table \
465 # Create ISO CRAMFS image with serial line support
466 echo "* Creating ISO image with cramfs and serial line support"
467 cat >$tmp/isolinux.cfg <<EOF
475 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,115200n8
478 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.iso"
480 -R -allow-leading-dots -J -r \
481 -b isolinux.bin -c boot.cat \
482 -no-emul-boot -boot-load-size 4 -boot-info-table \
488 # Create USB CRAMFS based image
489 echo "* Creating USB CRAMFS based image"
490 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.usb"
492 # leave 1MB of space on the USB VFAT
493 let vfat_size=${cramfs_size}+2048
495 # Make VFAT filesystem for USB
496 mkfs.vfat -C "$usb" $vfat_size
499 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
500 mount -o loop "$usb" $tmp
501 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
504 echo "* Populating USB with overlay images and cramfs"
505 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
506 cp ${BUILDTMP}/cramfs.img $tmp/
508 # Use syslinux instead of isolinux to make the image bootable
509 cat >$tmp/syslinux.cfg <<EOF
515 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
521 echo "* Making USB CRAMFS based image bootable"
522 $srcdir/syslinux/unix/syslinux "$usb"
524 # Create USB CRAMFS based image w/ serial line support
525 echo "* Creating USB CRAMFS based image w/ serial line support"
526 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.usb"
528 # leave 4MB of space on the USB VFAT
529 let vfat_size=${cramfs_size}+2048
531 # Make VFAT filesystem for USB
532 mkfs.vfat -C "$usb" $vfat_size
535 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
536 mount -o loop "$usb" $tmp
537 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
540 echo "* Populating USB with overlay images and cramfs"
541 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
542 cp ${BUILDTMP}/cramfs.img $tmp/
544 # Use syslinux instead of isolinux to make the image bootable
545 cat >$tmp/syslinux.cfg <<EOF
553 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,9600n8
559 echo "* Making USB CRAMFS based image /w serial line support bootable"
560 $srcdir/syslinux/unix/syslinux "$usb"