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-2007 The Trustees of Princeton University
16 PATH=/sbin:/bin:/usr/sbin:/usr/bin
19 NODE_CONFIGURATION_FILE=
20 TYPES="usb iso usb_serial iso_serial"
21 # Leave 4 MB of free space
25 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
29 echo "Usage: build.sh [OPTION]..."
30 echo " -c name (Deprecated) Static configuration to use (default: $CONFIGURATION)"
31 echo " -f planet.cnf Node to customize CD for (default: none)"
32 echo " -t 'types' Build the specified images (default: $TYPES)"
33 echo " -C custom-dir Custom directory"
34 echo " -O output-base The basename of the generated files (default: PLC_NAME-BootCD-VERSION)"
35 echo " -h This message"
40 while getopts "O:c:f:t:C:h" opt ; do
46 NODE_CONFIGURATION_FILE=$OPTARG
63 # Do not tolerate errors
66 # Change to our source directory
67 srcdir=$(cd $(dirname $0) && pwd -P)
71 isofs=$PWD/build/isofs
73 # The reference image is expected to have been built by prep.sh (see .spec)
74 # we disable the initial logic that called prep.sh if that was not the case
75 # this is because prep.sh needs to know pldistro
76 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
77 echo "You have to run prep.sh prior to calling $0 - exiting"
81 # build/version.txt written by prep.sh
82 BOOTCD_VERSION=$(cat build/version.txt)
84 if [ -f /etc/planetlab/plc_config ] ; then
85 # Source PLC configuration
86 . /etc/planetlab/plc_config
89 ### This support for backwards compatibility can be taken out in the
90 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
91 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
93 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
94 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
97 # If PLC configuration is not valid, try a static configuration
98 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
99 # (Deprecated) Source static configuration
100 . configurations/$CONFIGURATION/configuration
102 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
103 PLC_WWW_HOST="www.planet-lab.org"
105 if [ -n "$EXTRA_VERSION" ] ; then
106 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
108 PLC_BOOT_HOST=$PRIMARY_SERVER
109 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
110 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
111 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
114 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
116 echo "* Building images for $FULL_VERSION_STRING"
118 # From within a myplc chroot /usr/tmp is too small
119 # to build all possible images, whereas /data is part of the host
120 # filesystem and usually has sufficient space. What we
121 # should do is check whether the expected amount of space
124 if [ -d /data/tmp ] ; then
125 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
126 if [ -n "$isreadonly" ] ; then
132 declare -a _CLEANUPS=()
133 function do_cleanup()
136 for i in "${_CLEANUPS[@]}"; do
140 function push_cleanup()
142 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
144 function pop_cleanup()
146 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
149 trap "do_cleanup" ERR INT EXIT
151 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
152 push_cleanup rm -fr "${BUILDTMP}"
153 mkdir "${BUILDTMP}/isofs"
154 for i in "$isofs"/{bootcd.img,kernel,isolinux.bin}; do
155 ln -s "$i" "${BUILDTMP}/isofs"
157 isofs="${BUILDTMP}/isofs"
159 # Root of the ISO and USB images
160 echo "* Populating root filesystem..."
161 overlay="${BUILDTMP}/overlay"
162 install -d -m 755 $overlay
163 push_cleanup rm -fr $overlay
165 # Create version files
166 echo "* Creating version files"
168 # Boot Manager compares pl_version in both places to make sure that
169 # the right CD is mounted. We used to boot from an initrd and mount
170 # the CD on /usr. Now we just run everything out of the initrd.
171 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
172 mkdir -p $(dirname $file)
173 echo "$FULL_VERSION_STRING" >$file
176 # Install boot server configuration files
177 echo "* Installing boot server configuration files"
179 # We always intended to bring up and support backup boot servers,
180 # but never got around to it. Just install the same parameters for
182 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
183 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
184 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
185 echo "$PLC_BOOT_HOST" >$dir/boot_server
186 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
187 echo "/boot/" >$dir/boot_server_path
190 # (Deprecated) Install old-style boot server configuration files
191 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
192 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
193 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
194 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
195 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
197 # Generate /etc/issue
198 echo "* Generating /etc/issue"
200 if [ "$PLC_WWW_PORT" = "443" ] ; then
201 PLC_WWW_URL="https://$PLC_WWW_HOST/"
202 elif [ "$PLC_WWW_PORT" != "80" ] ; then
203 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
205 PLC_WWW_URL="http://$PLC_WWW_HOST/"
208 mkdir -p $overlay/etc
209 cat >$overlay/etc/issue <<EOF
215 This machine is a node in the $PLC_NAME distributed network. It has
216 not fully booted yet. If you have cancelled the boot process at the
217 request of $PLC_NAME Support, please follow the instructions provided
218 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
220 Console login at this point is restricted to root. Provide the root
221 password of the default $PLC_NAME Central administrator account at the
222 time that this CD was created.
227 echo "* Setting root password"
229 if [ -z "$ROOT_PASSWORD" ] ; then
230 # Generate an encrypted password with crypt() if not defined
231 # in a static configuration.
232 ROOT_PASSWORD=$(python <<EOF
233 import crypt, random, string
234 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
235 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
240 # build/passwd copied out by prep.sh
241 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
244 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
245 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
246 echo "* Installing node configuration file"
247 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
250 # Pack overlay files into a compressed archive
251 echo "* Compressing overlay image"
252 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
257 if [ -n "$CUSTOM_DIR" ]; then
258 echo "* Compressing custom image"
259 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
262 # Calculate ramdisk size (total uncompressed size of both archives)
263 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
264 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
266 echo "$FULL_VERSION_STRING" >$isofs/pl_version
276 # Write isolinux configuration
277 cat >$isofs/isolinux.cfg <<EOF
278 ${serial:+SERIAL 0 115200}
280 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
287 echo "* Creating ISO image"
296 echo -n "* Creating USB image... "
301 mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
304 echo -n " populating USB image... "
305 mcopy -bsQ -i "$usb" "$isofs"/* ::/
307 # Use syslinux instead of isolinux to make the image bootable
308 tmp="${BUILDTMP}/syslinux.cfg"
310 ${serial:+SERIAL 0 115200}
312 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
317 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
318 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
321 echo "making USB image bootable."
322 $srcdir/syslinux/unix/syslinux "$usb"
326 # Setup CRAMFS related support
327 function prepare_cramfs()
329 [ -n "$CRAMFS_PREPARED" ] && return 0
332 echo "* Setting up CRAMFS-based images"
333 local tmp="${BUILDTMP}/cramfs-tree"
335 push_cleanup rm -rf $tmp
337 gzip -d -c $isofs/bootcd.img | cpio -diu
338 gzip -d -c $isofs/overlay.img | cpio -diu
339 [ -n "$custom" ] && \
340 gzip -d -c $isofs/custom.img | cpio -diu
342 # clean out unnecessary rpm lib
343 echo "* clearing var/lib/rpm/*"
346 # bootcd requires this directory
347 mkdir -p mnt/confdevice
349 # relocate various directory to /tmp
351 ln -fs /tmp/root root
352 ln -fs /sbin/init linuxrc
353 ln -fs /tmp/resolv.conf etc/resolv.conf
354 ln -fs /tmp/etc/mtab etc/mtab
356 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
358 echo "* renaming dirs in ./etc"
360 for dir in `find * -type d -prune | grep -v rc.d`; do
362 ln -fs /tmp/etc/${dir} ${dir}
366 echo "* renaming dirs in ./var"
367 # rename all top-level directories and put in a symlink to /tmp/var
369 for dir in `find * -type d -prune`; do
371 ln -fs /tmp/var/${dir} ${dir}
375 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
376 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
378 cat >./etc/fstab <<EOF
379 /dev/ram0 / cramfs ro 0 0
380 none /dev/pts devpts gid=5,mode=620 0 0
381 none /proc proc defaults 0 0
382 none /sys sysfs defaults 0 0
388 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
389 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
394 # update etc/inittab to start with pl_rsysinit
395 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
397 # modify inittab to have a serial console
398 echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
399 # and let root log in
400 echo "ttyS0" >> etc/securetty
402 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
403 etcsize=$(du -s ./etc | awk '{ print $1 }')
404 varsize=$(du -s ./var | awk '{ print $1 }')
405 let msize=($varsize+$etcsize+8192)/1024
408 # generate pl_rsysinit
409 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
411 # generated by build.sh
412 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
413 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
416 touch /tmp/etc/resolv.conf
421 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
423 # copy over directory contents of all _o directories from /etc and /var
424 # /tmp/etc and /tmp/var
426 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
429 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
433 # hand over to pl_sysinit
434 echo "pl_rsysinit: handing over to pl_sysinit"
435 /etc/init.d/pl_sysinit
437 chmod +x etc/rc.d/init.d/pl_rsysinit
443 # create the cramfs image
444 echo "* Creating cramfs image"
445 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
446 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
451 # Create ISO CRAMFS image
452 function build_iso_cramfs()
457 echo "* Creating ISO CRAMFS-based image"
459 local tmp="${BUILDTMP}/cramfs-iso"
461 push_cleanup rm -rf $tmp
462 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
463 cat >$tmp/isolinux.cfg <<EOF
464 ${serial:+SERIAL 0 9600}
466 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
472 cp ${BUILDTMP}/cramfs.img $tmp
481 # Create USB CRAMFS based image
482 function build_usb_cramfs()
487 echo "* Creating USB CRAMFS based image"
489 let vfat_size=${cramfs_size}+$FREE_SPACE
491 # Make VFAT filesystem for USB
492 mkfs.vfat -C "$usb" $vfat_size
495 echo "* Populating USB with overlay images and cramfs"
496 mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
497 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
499 # Use syslinux instead of isolinux to make the image bootable
500 tmp="${BUILDTMP}/syslinux.cfg"
502 ${serial:+SERIAL 0 9600}
504 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
509 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
512 echo "* Making USB CRAMFS based image bootable"
513 $srcdir/syslinux/unix/syslinux "$usb"
516 function type_to_name()
520 s/usb_serial$/-serial.usb/;
522 s/iso_serial$/-serial.iso/;
523 s/usb_cramfs$/-cramfs.usb/;
524 s/usb_cramfs_serial$/-cramfs-serial.usb/;
525 s/iso_cramfs$/-cramfs.iso/;
526 s/iso_cramfs_serial$/-cramfs-serial.iso/;
530 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
534 tname=`type_to_name $t`
535 if [[ "$t" == *_serial ]]; then
537 t=`echo $t | sed 's/_serial$//'`
539 build_$t "${OUTPUT_BASE}${tname}" $serial $CUSTOM_DIR