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 # The reference image is expected to have been built by prep.sh (see .spec)
61 # we disable the initial logic that called prep.sh if that was not the case
62 # this is because prep.sh needs to know pldistro
63 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
64 echo "you have to run prep.sh prior to calling $0 - exiting"
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 /usr/tmp is too small
106 # to build 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
111 if [ -d /data ] ; then
112 isreadonly=$(mktemp /data/isreadonly.XXXXXX || /bin/true)
113 if [ -n "$isreadonly" ] ; then
119 # Root of the ISO and USB images
120 echo "* Populating root filesystem..."
121 overlay=$(mktemp -d ${BUILDTMP}/overlay.XXXXXX)
122 install -d -m 755 $overlay
123 trap "rm -rf $overlay" ERR INT
125 # Create version files
126 echo "* Creating version files"
128 # Boot Manager compares pl_version in both places to make sure that
129 # the right CD is mounted. We used to boot from an initrd and mount
130 # the CD on /usr. Now we just run everything out of the initrd.
131 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
132 mkdir -p $(dirname $file)
133 echo "$FULL_VERSION_STRING" >$file
136 # Install boot server configuration files
137 echo "* Installing boot server configuration files"
139 # We always intended to bring up and support backup boot servers,
140 # but never got around to it. Just install the same parameters for
142 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
143 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
144 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
145 echo "$PLC_BOOT_HOST" >$dir/boot_server
146 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
147 echo "/boot/" >$dir/boot_server_path
150 # (Deprecated) Install old-style boot server configuration files
151 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
152 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
153 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
154 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
155 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
157 # Generate /etc/issue
158 echo "* Generating /etc/issue"
160 if [ "$PLC_WWW_PORT" = "443" ] ; then
161 PLC_WWW_URL="https://$PLC_WWW_HOST/"
162 elif [ "$PLC_WWW_PORT" != "80" ] ; then
163 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
165 PLC_WWW_URL="http://$PLC_WWW_HOST/"
168 mkdir -p $overlay/etc
169 cat >$overlay/etc/issue <<EOF
175 This machine is a node in the $PLC_NAME distributed network. It has
176 not fully booted yet. If you have cancelled the boot process at the
177 request of $PLC_NAME Support, please follow the instructions provided
178 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
180 Console login at this point is restricted to root. Provide the root
181 password of the default $PLC_NAME Central administrator account at the
182 time that this CD was created.
187 echo "* Setting root password"
189 if [ -z "$ROOT_PASSWORD" ] ; then
190 # Generate an encrypted password with crypt() if not defined
191 # in a static configuration.
192 ROOT_PASSWORD=$(python <<EOF
193 import crypt, random, string
194 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
195 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
200 # build/passwd copied out by prep.sh
201 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
204 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
205 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
206 echo "* Installing node configuration file"
207 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
210 # Pack overlay files into a compressed archive
211 echo "* Compressing overlay image"
212 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
217 # Calculate ramdisk size (total uncompressed size of both archives)
218 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img | tail -1 | awk '{ print $2; }') # bytes
219 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
221 # Write isolinux configuration
222 echo "$FULL_VERSION_STRING" >$isofs/pl_version
223 cat >$isofs/isolinux.cfg <<EOF
225 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
231 # Change back to output directory
235 echo "* Creating ISO image"
236 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
238 -R -allow-leading-dots -J -r \
239 -b isolinux.bin -c boot.cat \
240 -no-emul-boot -boot-load-size 4 -boot-info-table \
243 echo "* Creating ISO image with serial line support"
244 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.iso"
245 cat >$isofs/isolinux.cfg <<EOF
253 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw console=ttyS0,115200n8
256 -R -allow-leading-dots -J -r \
257 -b isolinux.bin -c boot.cat \
258 -no-emul-boot -boot-load-size 4 -boot-info-table \
262 echo -n "* Creating USB image... "
263 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
265 # Leave 1 MB of free space on the VFAT filesystem
266 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
269 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
270 mount -o loop "$usb" $tmp
271 trap "umount $tmp; rm -rf $tmp" ERR INT
274 echo -n " populating USB image... "
275 (cd $isofs && find . | cpio -p -d -u $tmp/)
277 # Use syslinux instead of isolinux to make the image bootable
278 rm -f $tmp/isolinux.cfg
279 cat >$tmp/syslinux.cfg <<EOF
281 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
290 echo "making USB image bootable."
291 $srcdir/syslinux/unix/syslinux "$usb"
294 # Create USB image with serial line support
295 echo -n "* Creating USB image... "
296 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.usb"
298 # Leave 1 MB of free space on the VFAT filesystem
299 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
302 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
303 mount -o loop "$usb" $tmp
304 trap "umount $tmp; rm -rf $tmp" ERR INT
307 echo -n " populating USB image... "
308 (cd $isofs && find . | cpio -p -d -u $tmp/)
310 # Use syslinux instead of isolinux to make the image bootable
311 rm -f $tmp/isolinux.cfg
312 cat >$tmp/syslinux.cfg <<EOF
320 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw console=ttyS0,115200n8
327 echo "making USB image with serial line support bootable."
328 $srcdir/syslinux/unix/syslinux "$usb"
330 [ $ALL -eq 0 ] && exit 0
332 # Setup CRAMFS related support
333 echo "* Setting up CRAMFS-based images"
334 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
335 cramfs=$(mktemp ${BUILDTMP}/cramfs.XXXXXX)
336 trap "$tmp; rm -rf $tmp $cramfs" ERR INT
338 gzip -d -c $isofs/bootcd.img | cpio -diu
339 gzip -d -c $isofs/overlay.img | cpio -diu
341 # clean out unnecessary rpm lib
342 echo "* clearing var/lib/rpm/*"
345 # bootcd requires this directory
346 mkdir -p mnt/confdevice
348 # relocate various directory to /tmp
350 ln -fs /tmp/root root
351 ln -fs /sbin/init linuxrc
352 ln -fs /tmp/resolv.conf etc/resolv.conf
353 ln -fs /tmp/etc/mtab etc/mtab
355 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
357 echo "* renaming dirs in ./etc"
359 for dir in `find * -type d -prune | grep -v rc.d`; do mv ${dir} ${dir}_o; ln -fs /tmp/etc/${dir} ${dir} ; done
362 echo "* renaming dirs in ./var"
363 # rename all top-level directories and put in a symlink to /tmp/var
365 for dir in `find * -type d -prune`; do mv ${dir} ${dir}_o; ln -fs /tmp/var/${dir} ${dir} ; done
368 #overwrite fstab to mount / as cramfs and /tmp as tmpfs
369 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
371 cat >./etc/fstab <<EOF
372 /dev/ram0 / cramfs ro 0 0
373 none /dev/pts devpts gid=5,mode=620 0 0
374 none /proc proc defaults 0 0
375 none /sys sysfs defaults 0 0
381 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
382 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
387 # update etc/inittab to start with pl_rsysinit
388 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
390 # modify inittab to have a serial console
391 echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
392 # and let root log in
393 echo "ttyS0" >> etc/securetty
395 #calculate the size of /tmp based on the size of /etc & /var + 8MB slack
396 etcsize=$(du -s ./etc | awk '{ print $1 }')
397 varsize=$(du -s ./etc | awk '{ print $1 }')
398 let msize=($vsize+$esize+8192)/1024
401 # generate pl_rsysinit
402 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
404 # generated by build.sh
405 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
406 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
409 touch /tmp/etc/resolv.conf
414 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
416 # copy over directory contents of all _o directories from /etc and /var
417 # /tmp/etc and /tmp/var
419 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
422 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
426 # hand over to pl_sysinit
427 echo "pl_rsysinit: handing over to pl_sysinit"
428 /etc/init.d/pl_sysinit
430 chmod +x etc/rc.d/init.d/pl_rsysinit
436 #create the cramfs image
437 echo "* Creating cramfs image"
438 mkfs.cramfs $tmp/ $cramfs
439 # Leave 1 MB of free space on the VFAT filesystem
440 cramfs_size=$(($(du -sk $cramfs | awk '{ print $1; }')))
441 mv $cramfs ${BUILDTMP}/cramfs.img
445 # Create ISO CRAMFS image
446 echo "* Creating ISO CRAMFS-based image"
447 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.iso"
449 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
450 trap "$tmp; rm -rf $tmp" ERR INT
451 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
452 cat >$tmp/isolinux.cfg <<EOF
454 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
460 cp ${BUILDTMP}/cramfs.img $tmp
462 -R -allow-leading-dots -J -r \
463 -b isolinux.bin -c boot.cat \
464 -no-emul-boot -boot-load-size 4 -boot-info-table \
467 # Create ISO CRAMFS image with serial line support
468 echo "* Creating ISO image with cramfs and serial line support"
469 cat >$tmp/isolinux.cfg <<EOF
477 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,115200n8
480 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.iso"
482 -R -allow-leading-dots -J -r \
483 -b isolinux.bin -c boot.cat \
484 -no-emul-boot -boot-load-size 4 -boot-info-table \
490 # Create USB CRAMFS based image
491 echo "* Creating USB CRAMFS based image"
492 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.usb"
494 # leave 1MB of space on the USB VFAT
495 let vfat_size=${cramfs_size}+2048
497 # Make VFAT filesystem for USB
498 mkfs.vfat -C "$usb" $vfat_size
501 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
502 mount -o loop "$usb" $tmp
503 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
506 echo "* Populating USB with overlay images and cramfs"
507 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
508 cp ${BUILDTMP}/cramfs.img $tmp/
510 # Use syslinux instead of isolinux to make the image bootable
511 cat >$tmp/syslinux.cfg <<EOF
517 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
523 echo "* Making USB CRAMFS based image bootable"
524 $srcdir/syslinux/unix/syslinux "$usb"
526 # Create USB CRAMFS based image w/ serial line support
527 echo "* Creating USB CRAMFS based image w/ serial line support"
528 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.usb"
530 # leave 4MB of space on the USB VFAT
531 let vfat_size=${cramfs_size}+2048
533 # Make VFAT filesystem for USB
534 mkfs.vfat -C "$usb" $vfat_size
537 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
538 mount -o loop "$usb" $tmp
539 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
542 echo "* Populating USB with overlay images and cramfs"
543 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
544 cp ${BUILDTMP}/cramfs.img $tmp/
546 # Use syslinux instead of isolinux to make the image bootable
547 cat >$tmp/syslinux.cfg <<EOF
555 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,9600n8
561 echo "* Making USB CRAMFS based image /w serial line support bootable"
562 $srcdir/syslinux/unix/syslinux "$usb"