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
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 # The reference image is expected to have been built by prep.sh (see .spec)
63 # we disable the initial logic that called prep.sh if that was not the case
64 # this is because prep.sh needs to know pldistro
65 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
66 echo "you have to run prep.sh prior to calling $0 - exiting"
70 # build/version.txt written by prep.sh
71 BOOTCD_VERSION=$(cat build/version.txt)
73 if [ -f /etc/planetlab/plc_config ] ; then
74 # Source PLC configuration
75 . /etc/planetlab/plc_config
78 ### This support for backwards compatibility can be taken out in the
79 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
80 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
82 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
83 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
86 # If PLC configuration is not valid, try a static configuration
87 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
88 # (Deprecated) Source static configuration
89 . configurations/$CONFIGURATION/configuration
91 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
92 PLC_WWW_HOST="www.planet-lab.org"
94 if [ -n "$EXTRA_VERSION" ] ; then
95 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
97 PLC_BOOT_HOST=$PRIMARY_SERVER
98 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
99 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
100 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
103 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
105 echo "* Building images for $FULL_VERSION_STRING"
107 # From within a myplc chroot /usr/tmp is too small
108 # to build all possible images, whereas /data is part of the host
109 # filesystem and usually has sufficient space. What we
110 # should do is check whether the expected amount of space
113 if [ -d /data ] ; then
114 isreadonly=$(mktemp /data/isreadonly.XXXXXX || /bin/true)
115 if [ -n "$isreadonly" ] ; then
121 # Root of the ISO and USB images
122 echo "* Populating root filesystem..."
123 overlay=$(mktemp -d ${BUILDTMP}/overlay.XXXXXX)
124 install -d -m 755 $overlay
125 trap "rm -rf $overlay" ERR INT
127 # Create version files
128 echo "* Creating version files"
130 # Boot Manager compares pl_version in both places to make sure that
131 # the right CD is mounted. We used to boot from an initrd and mount
132 # the CD on /usr. Now we just run everything out of the initrd.
133 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
134 mkdir -p $(dirname $file)
135 echo "$FULL_VERSION_STRING" >$file
138 # Install boot server configuration files
139 echo "* Installing boot server configuration files"
141 # We always intended to bring up and support backup boot servers,
142 # but never got around to it. Just install the same parameters for
144 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
145 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
146 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
147 echo "$PLC_BOOT_HOST" >$dir/boot_server
148 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
149 echo "/boot/" >$dir/boot_server_path
152 # (Deprecated) Install old-style boot server configuration files
153 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
154 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
155 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
156 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
157 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
159 # Generate /etc/issue
160 echo "* Generating /etc/issue"
162 if [ "$PLC_WWW_PORT" = "443" ] ; then
163 PLC_WWW_URL="https://$PLC_WWW_HOST/"
164 elif [ "$PLC_WWW_PORT" != "80" ] ; then
165 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
167 PLC_WWW_URL="http://$PLC_WWW_HOST/"
170 mkdir -p $overlay/etc
171 cat >$overlay/etc/issue <<EOF
177 This machine is a node in the $PLC_NAME distributed network. It has
178 not fully booted yet. If you have cancelled the boot process at the
179 request of $PLC_NAME Support, please follow the instructions provided
180 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
182 Console login at this point is restricted to root. Provide the root
183 password of the default $PLC_NAME Central administrator account at the
184 time that this CD was created.
189 echo "* Setting root password"
191 if [ -z "$ROOT_PASSWORD" ] ; then
192 # Generate an encrypted password with crypt() if not defined
193 # in a static configuration.
194 ROOT_PASSWORD=$(python <<EOF
195 import crypt, random, string
196 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
197 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
202 # build/passwd copied out by prep.sh
203 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
206 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
207 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
208 echo "* Installing node configuration file"
209 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
212 # Pack overlay files into a compressed archive
213 echo "* Compressing overlay image"
214 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
219 # Calculate ramdisk size (total uncompressed size of both archives)
220 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img | tail -1 | awk '{ print $2; }') # bytes
221 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
223 # Write isolinux configuration
224 echo "$FULL_VERSION_STRING" >$isofs/pl_version
225 cat >$isofs/isolinux.cfg <<EOF
227 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
233 # Change back to output directory
237 echo "* Creating ISO image"
238 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
240 -R -allow-leading-dots -J -r \
241 -b isolinux.bin -c boot.cat \
242 -no-emul-boot -boot-load-size 4 -boot-info-table \
245 echo "* Creating ISO image with serial line support"
246 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.iso"
247 cat >$isofs/isolinux.cfg <<EOF
255 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw console=ttyS0,115200n8
258 -R -allow-leading-dots -J -r \
259 -b isolinux.bin -c boot.cat \
260 -no-emul-boot -boot-load-size 4 -boot-info-table \
264 echo -n "* Creating USB image... "
265 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
267 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + $FREE_SPACE))
270 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
271 mount -o loop "$usb" $tmp
272 trap "umount $tmp; rm -rf $tmp" ERR INT
275 echo -n " populating USB image... "
276 (cd $isofs && find . | cpio -p -d -u $tmp/)
278 # Use syslinux instead of isolinux to make the image bootable
279 rm -f $tmp/isolinux.cfg
280 cat >$tmp/syslinux.cfg <<EOF
282 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
291 echo "making USB image bootable."
292 $srcdir/syslinux/unix/syslinux "$usb"
295 # Create USB image with serial line support
296 echo -n "* Creating USB image... "
297 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.usb"
299 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + $FREE_SPACE))
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 ./var | awk '{ print $1 }')
398 let msize=($varsize+$etcsize+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 cramfs_size=$(($(du -sk $cramfs | awk '{ print $1; }')))
440 mv $cramfs ${BUILDTMP}/cramfs.img
444 # Create ISO CRAMFS image
445 echo "* Creating ISO CRAMFS-based image"
446 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.iso"
448 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
449 trap "cd /; rm -rf $tmp" ERR INT
450 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
451 cat >$tmp/isolinux.cfg <<EOF
453 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
459 cp ${BUILDTMP}/cramfs.img $tmp
461 -R -allow-leading-dots -J -r \
462 -b isolinux.bin -c boot.cat \
463 -no-emul-boot -boot-load-size 4 -boot-info-table \
466 # Create ISO CRAMFS image with serial line support
467 echo "* Creating ISO image with cramfs and serial line support"
468 cat >$tmp/isolinux.cfg <<EOF
476 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,115200n8
479 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.iso"
481 -R -allow-leading-dots -J -r \
482 -b isolinux.bin -c boot.cat \
483 -no-emul-boot -boot-load-size 4 -boot-info-table \
489 # Create USB CRAMFS based image
490 echo "* Creating USB CRAMFS based image"
491 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.usb"
493 let vfat_size=${cramfs_size}+$FREE_SPACE
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 let vfat_size=${cramfs_size}+$FREE_SPACE
530 # Make VFAT filesystem for USB
531 mkfs.vfat -C "$usb" $vfat_size
534 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
535 mount -o loop "$usb" $tmp
536 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
539 echo "* Populating USB with overlay images and cramfs"
540 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
541 cp ${BUILDTMP}/cramfs.img $tmp/
543 # Use syslinux instead of isolinux to make the image bootable
544 cat >$tmp/syslinux.cfg <<EOF
552 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro console=ttyS0,9600n8
558 echo "* Making USB CRAMFS based image /w serial line support bootable"
559 $srcdir/syslinux/unix/syslinux "$usb"