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=
21 ALL_TYPES="usb iso usb_cramfs iso_cramfs"
22 # Leave 4 MB of free space
26 DEFAULT_CONSOLE="graphic"
27 CONSOLE_INFO=$DEFAULT_CONSOLE
28 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
32 echo "Usage: build.sh [OPTION]..."
33 echo " -c name (Deprecated) Static configuration to use (default: $CONFIGURATION)"
34 echo " -f planet.cnf Node to customize CD for (default: none)"
35 echo " -t 'types' Build the specified images (default: $TYPES)"
36 echo " All known types: $ALL_TYPES"
37 echo " -s console-info Enable a serial line as console and also bring up getty on that line"
38 echo " console-info=devicename:baudrate"
39 echo " -a Build all supported images"
40 echo " -C custom-dir Custom directory"
41 echo " -O output-base The basename of the generated files (default: PLC_NAME-BootCD-VERSION)"
42 echo " -h This message"
47 while getopts "O:c:f:s:t:C:ah" opt ; do
53 NODE_CONFIGURATION_FILE=$OPTARG
68 CONSOLE_INFO="$OPTARG"
76 # Do not tolerate errors
79 # Change to our source directory
80 srcdir=$(cd $(dirname $0) && pwd -P)
84 isofs=$PWD/build/isofs
86 # The reference image is expected to have been built by prep.sh (see .spec)
87 # we disable the initial logic that called prep.sh if that was not the case
88 # this is because prep.sh needs to know pldistro
89 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
90 echo "You have to run prep.sh prior to calling $0 - exiting"
94 # build/version.txt written by prep.sh
95 BOOTCD_VERSION=$(cat build/version.txt)
97 if [ -f /etc/planetlab/plc_config ] ; then
98 # Source PLC configuration
99 . /etc/planetlab/plc_config
102 ### This support for backwards compatibility can be taken out in the
103 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
104 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
106 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
107 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
110 # If PLC configuration is not valid, try a static configuration
111 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
112 # (Deprecated) Source static configuration
113 . configurations/$CONFIGURATION/configuration
115 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
116 PLC_WWW_HOST="www.planet-lab.org"
118 if [ -n "$EXTRA_VERSION" ] ; then
119 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
121 PLC_BOOT_HOST=$PRIMARY_SERVER
122 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
123 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
124 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
127 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
129 echo "* Building images for $FULL_VERSION_STRING"
131 # From within a myplc chroot /usr/tmp is too small
132 # to build all possible images, whereas /data is part of the host
133 # filesystem and usually has sufficient space. What we
134 # should do is check whether the expected amount of space
137 if [ -d /data/tmp ] ; then
138 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
139 if [ -n "$isreadonly" ] ; then
145 declare -a _CLEANUPS=()
146 function do_cleanup()
149 for i in "${_CLEANUPS[@]}"; do
153 function push_cleanup()
155 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
157 function pop_cleanup()
159 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
162 trap "do_cleanup" ERR INT EXIT
164 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
165 push_cleanup rm -fr "${BUILDTMP}"
166 mkdir "${BUILDTMP}/isofs"
167 for i in "$isofs"/{bootcd.img,kernel}; do
168 ln -s "$i" "${BUILDTMP}/isofs"
170 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
171 isofs="${BUILDTMP}/isofs"
173 # Root of the ISO and USB images
174 echo "* Populating root filesystem..."
175 overlay="${BUILDTMP}/overlay"
176 install -d -m 755 $overlay
177 push_cleanup rm -fr $overlay
179 # Create version files
180 echo "* Creating version files"
182 # Boot Manager compares pl_version in both places to make sure that
183 # the right CD is mounted. We used to boot from an initrd and mount
184 # the CD on /usr. Now we just run everything out of the initrd.
185 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
186 mkdir -p $(dirname $file)
187 echo "$FULL_VERSION_STRING" >$file
190 # Install boot server configuration files
191 echo "* Installing boot server configuration files"
193 # We always intended to bring up and support backup boot servers,
194 # but never got around to it. Just install the same parameters for
196 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
197 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
198 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
199 echo "$PLC_BOOT_HOST" >$dir/boot_server
200 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
201 echo "/boot/" >$dir/boot_server_path
204 # (Deprecated) Install old-style boot server configuration files
205 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
206 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
207 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
208 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
209 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
211 # Generate /etc/issue
212 echo "* Generating /etc/issue"
214 if [ "$PLC_WWW_PORT" = "443" ] ; then
215 PLC_WWW_URL="https://$PLC_WWW_HOST/"
216 elif [ "$PLC_WWW_PORT" != "80" ] ; then
217 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
219 PLC_WWW_URL="http://$PLC_WWW_HOST/"
222 mkdir -p $overlay/etc
223 cat >$overlay/etc/issue <<EOF
229 This machine is a node in the $PLC_NAME distributed network. It has
230 not fully booted yet. If you have cancelled the boot process at the
231 request of $PLC_NAME Support, please follow the instructions provided
232 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
234 Console login at this point is restricted to root. Provide the root
235 password of the default $PLC_NAME Central administrator account at the
236 time that this CD was created.
241 echo "* Setting root password"
243 if [ -z "$ROOT_PASSWORD" ] ; then
244 # Generate an encrypted password with crypt() if not defined
245 # in a static configuration.
246 ROOT_PASSWORD=$(python <<EOF
247 import crypt, random, string
248 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
249 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
254 # build/passwd copied out by prep.sh
255 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
258 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
259 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
260 echo "* Installing node configuration file $NODE_CONFIGURATION_FILE -> /usr/boot/plnode.txt of the bootcd image"
261 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
264 # Pack overlay files into a compressed archive
265 echo "* Compressing overlay image"
266 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
271 if [ -n "$CUSTOM_DIR" ]; then
272 echo "* Compressing custom image"
273 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
276 # Calculate ramdisk size (total uncompressed size of both archives)
277 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
278 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
280 echo "$FULL_VERSION_STRING" >$isofs/pl_version
284 function extract_console_dev()
287 dev=$(echo $console| awk -F: ' {print $1}')
291 function extract_console_baud()
294 baud=$(echo $console| awk -F: ' {print $2}')
295 [ -z "$baud" ] && baud="115200"
299 function extract_console_parity()
302 parity=$(echo $console| awk -F: ' {print $3}')
303 [ -z "$parity" ] && parity="n"
307 function extract_console_bits()
310 bits=$(echo $console| awk -F: ' {print $4}')
311 [ -z "$bits" ] && bits="8"
317 local iso="$1" ; shift
318 local console="$1" ; shift
322 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
324 console_dev=$(extract_console_dev $console)
325 console_baud=$(extract_console_baud $console)
326 console_parity=$(extract_console_parity $console)
327 console_bits=$(extract_console_bits $console)
330 # Write isolinux configuration
331 cat >$isofs/isolinux.cfg <<EOF
332 ${serial:+SERIAL 0 115200}
334 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
341 echo "* Creating ISO image"
347 function build_usb_partition()
349 echo -n "* Creating USB image with partitions..."
350 local usb="$1" ; shift
351 local console="$1" ; shift
355 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
357 console_dev=$(extract_console_dev $console)
358 console_baud=$(extract_console_baud $console)
359 console_parity=$(extract_console_parity $console)
360 console_bits=$(extract_console_bits $console)
363 local size=$(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
364 size=$(( $size / 1024 ))
368 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
369 local offset=$(( $sectors*512 ))
371 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
373 cat >${BUILDTMP}/mtools.conf<<EOF
382 # environment variable for mtools
383 export MTOOLSRC="${BUILDTMP}/mtools.conf"
385 ### COPIED FROM build_usb() below!!!!
386 echo -n " populating USB image... "
387 mcopy -bsQ -i "$usb" "$isofs"/* z:/
389 # Use syslinux instead of isolinux to make the image bootable
390 tmp="${BUILDTMP}/syslinux.cfg"
392 ${serial:+SERIAL 0 ${console_baud}}
394 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
399 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
400 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
402 rm -f "${BUILDTMP}/mtools.conf"
405 echo "making USB image bootable."
406 syslinux -o $offset "$usb"
413 echo -n "* Creating USB image... "
414 local usb="$1" ; shift
415 local console="$1" ; shift
419 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
421 console_dev=$(extract_console_dev $console)
422 console_baud=$(extract_console_baud $console)
423 console_parity=$(extract_console_parity $console)
424 console_bits=$(extract_console_bits $console)
427 mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
430 echo -n " populating USB image... "
431 mcopy -bsQ -i "$usb" "$isofs"/* ::/
433 # Use syslinux instead of isolinux to make the image bootable
434 tmp="${BUILDTMP}/syslinux.cfg"
436 ${serial:+SERIAL 0 $console_baud}
438 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
443 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
444 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
447 echo "making USB image bootable."
452 # Setup CRAMFS related support
453 function prepare_cramfs()
455 [ -n "$CRAMFS_PREPARED" ] && return 0
456 local console=$1; shift
459 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
461 console_dev=$(extract_console_dev $console)
462 console_baud=$(extract_console_baud $console)
465 echo "* Setting up CRAMFS-based images"
466 local tmp="${BUILDTMP}/cramfs-tree"
468 push_cleanup rm -rf $tmp
470 gzip -d -c $isofs/bootcd.img | cpio -diu
471 gzip -d -c $isofs/overlay.img | cpio -diu
472 [ -n "$custom" ] && \
473 gzip -d -c $isofs/custom.img | cpio -diu
475 # clean out unnecessary rpm lib
476 echo "* clearing var/lib/rpm/*"
479 # bootcd requires this directory
480 mkdir -p mnt/confdevice
482 # relocate various directory to /tmp
484 ln -fs /tmp/root root
485 ln -fs /sbin/init linuxrc
486 ln -fs /tmp/resolv.conf etc/resolv.conf
487 ln -fs /tmp/etc/mtab etc/mtab
489 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
491 echo "* renaming dirs in ./etc"
493 for dir in `find * -type d -prune | grep -v rc.d`; do
495 ln -fs /tmp/etc/${dir} ${dir}
499 echo "* renaming dirs in ./var"
500 # rename all top-level directories and put in a symlink to /tmp/var
502 for dir in `find * -type d -prune`; do
504 ln -fs /tmp/var/${dir} ${dir}
508 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
509 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
511 cat >./etc/fstab <<EOF
512 /dev/ram0 / cramfs ro 0 0
513 none /dev/pts devpts gid=5,mode=620 0 0
514 none /proc proc defaults 0 0
515 none /sys sysfs defaults 0 0
521 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
522 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
527 # update etc/inittab to start with pl_rsysinit
528 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
530 # modify inittab to have a serial console
531 if [ $serial -eq 1 ] ; then
532 echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
533 # and let root log in
534 echo "$console_dev" >> etc/securetty
537 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
538 etcsize=$(du -s ./etc | awk '{ print $1 }')
539 varsize=$(du -s ./var | awk '{ print $1 }')
540 let msize=($varsize+$etcsize+8192)/1024
542 # make dhclient happy
543 for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
544 ln -fs /tmp/etc/resolv.conf etc/resolv.conf
545 ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
547 # generate pl_rsysinit
548 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
550 # generated by build.sh
551 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
552 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
555 touch /tmp/etc/resolv.conf
560 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
562 # copy over directory contents of all _o directories from /etc and /var
563 # /tmp/etc and /tmp/var
565 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
568 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
573 # hand over to pl_sysinit
574 echo "pl_rsysinit: handing over to pl_sysinit"
575 /etc/init.d/pl_sysinit
577 chmod +x etc/rc.d/init.d/pl_rsysinit
581 # create the cramfs image
582 echo "* Creating cramfs image"
583 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
584 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
589 # Create ISO CRAMFS image
590 function build_iso_cramfs()
592 local iso="$1" ; shift
593 local console="$1" ; shift
597 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
599 console_dev=$(extract_console_dev $console)
600 console_baud=$(extract_console_baud $console)
601 console_parity=$(extract_console_parity $console)
602 console_bits=$(extract_console_bits $console)
604 prepare_cramfs "$console" "$custom"
605 echo "* Creating ISO CRAMFS-based image"
607 local tmp="${BUILDTMP}/cramfs-iso"
609 push_cleanup rm -rf $tmp
610 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
611 cat >$tmp/isolinux.cfg <<EOF
612 ${serial:+SERIAL 0 $console_baud}
614 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
620 cp ${BUILDTMP}/cramfs.img $tmp
629 # Create USB CRAMFS based image
630 function build_usb_cramfs()
632 local usb="$1" ; shift
633 local console="$1" ; shift
637 if [ "$console" != "$DEFAULT_CONSOLE" ] ; then
639 console_dev=$(extract_console_dev $console)
640 console_baud=$(extract_console_baud $console)
641 console_parity=$(extract_console_parity $console)
642 console_bits=$(extract_console_bits $console)
644 prepare_cramfs "$console" "$custom"
645 echo "* Creating USB CRAMFS based image"
647 let vfat_size=${cramfs_size}+$FREE_SPACE
649 # Make VFAT filesystem for USB
650 mkfs.vfat -C "$usb" $vfat_size
653 echo "* Populating USB with overlay images and cramfs"
654 mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
655 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
657 # Use syslinux instead of isolinux to make the image bootable
658 tmp="${BUILDTMP}/syslinux.cfg"
660 ${serial:+SERIAL 0 $console_baud}
662 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=${console_dev},${console_baud}${console_parity}${console_bits}}
667 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
670 echo "* Making USB CRAMFS based image bootable"
674 function type_to_name()
678 s/usb_partition$/-partition.usb/;
679 s/usb_serial$/-serial.usb/;
681 s/iso_serial$/-serial.iso/;
682 s/usb_cramfs$/-cramfs.usb/;
683 s/usb_cramfs_serial$/-cramfs-serial.usb/;
684 s/iso_cramfs$/-cramfs.iso/;
685 s/iso_cramfs_serial$/-cramfs-serial.iso/;
689 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
692 CONSOLE=$CONSOLE_INFO
693 tname=`type_to_name $t`
694 if [[ "$t" == *_serial ]]; then
695 if [ "$CONSOLE_INFO" == "$DEFAULT_CONSOLE" ] ; then
696 CONSOLE="ttyS0:115200"
698 t=`echo $t | sed 's/_serial$//'`
701 OUTPUTNAME="${OUTPUT_BASE}${tname}"
702 if [ "$CONSOLE" != "$DEFAULT_CONSOLE" ] ; then
703 CONSOLE_NAME=$(echo $CONSOLE | sed 's,\:,,g')
704 OUTPUTNAME="${OUTPUT_BASE}-serial-${CONSOLE_NAME}${tname}"
706 build_$t "$OUTPUTNAME" "$CONSOLE" "$CUSTOM_DIR"