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 " -a Build all supported images"
34 echo " -C custom-dir Custom directory"
35 echo " -O output-base The basename of the generated files (default: PLC_NAME-BootCD-VERSION)"
36 echo " -h This message"
41 while getopts "O:c:f:t:C:ah" opt ; do
47 NODE_CONFIGURATION_FILE=$OPTARG
59 TYPES="usb iso usb_serial iso_serial usb_cramfs iso_cramfs usb_cramfs_serial iso_cramfs_serial"
67 # Do not tolerate errors
70 # Change to our source directory
71 srcdir=$(cd $(dirname $0) && pwd -P)
75 isofs=$PWD/build/isofs
77 # The reference image is expected to have been built by prep.sh (see .spec)
78 # we disable the initial logic that called prep.sh if that was not the case
79 # this is because prep.sh needs to know pldistro
80 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
81 echo "You have to run prep.sh prior to calling $0 - exiting"
85 # build/version.txt written by prep.sh
86 BOOTCD_VERSION=$(cat build/version.txt)
88 if [ -f /etc/planetlab/plc_config ] ; then
89 # Source PLC configuration
90 . /etc/planetlab/plc_config
93 ### This support for backwards compatibility can be taken out in the
94 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
95 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
97 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
98 PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
101 # If PLC configuration is not valid, try a static configuration
102 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
103 # (Deprecated) Source static configuration
104 . configurations/$CONFIGURATION/configuration
106 PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
107 PLC_WWW_HOST="www.planet-lab.org"
109 if [ -n "$EXTRA_VERSION" ] ; then
110 BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
112 PLC_BOOT_HOST=$PRIMARY_SERVER
113 PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
114 PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
115 PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
118 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
120 echo "* Building images for $FULL_VERSION_STRING"
122 # From within a myplc chroot /usr/tmp is too small
123 # to build all possible images, whereas /data is part of the host
124 # filesystem and usually has sufficient space. What we
125 # should do is check whether the expected amount of space
128 if [ -d /data/tmp ] ; then
129 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
130 if [ -n "$isreadonly" ] ; then
136 declare -a _CLEANUPS=()
137 function do_cleanup()
140 for i in "${_CLEANUPS[@]}"; do
144 function push_cleanup()
146 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
148 function pop_cleanup()
150 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
153 trap "do_cleanup" ERR INT EXIT
155 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
156 push_cleanup rm -fr "${BUILDTMP}"
157 mkdir "${BUILDTMP}/isofs"
158 for i in "$isofs"/{bootcd.img,kernel}; do
159 ln -s "$i" "${BUILDTMP}/isofs"
161 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
162 isofs="${BUILDTMP}/isofs"
164 # Root of the ISO and USB images
165 echo "* Populating root filesystem..."
166 overlay="${BUILDTMP}/overlay"
167 install -d -m 755 $overlay
168 push_cleanup rm -fr $overlay
170 # Create version files
171 echo "* Creating version files"
173 # Boot Manager compares pl_version in both places to make sure that
174 # the right CD is mounted. We used to boot from an initrd and mount
175 # the CD on /usr. Now we just run everything out of the initrd.
176 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
177 mkdir -p $(dirname $file)
178 echo "$FULL_VERSION_STRING" >$file
181 # Install boot server configuration files
182 echo "* Installing boot server configuration files"
184 # We always intended to bring up and support backup boot servers,
185 # but never got around to it. Just install the same parameters for
187 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
188 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
189 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
190 echo "$PLC_BOOT_HOST" >$dir/boot_server
191 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
192 echo "/boot/" >$dir/boot_server_path
195 # (Deprecated) Install old-style boot server configuration files
196 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
197 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
198 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
199 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
200 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
202 # Generate /etc/issue
203 echo "* Generating /etc/issue"
205 if [ "$PLC_WWW_PORT" = "443" ] ; then
206 PLC_WWW_URL="https://$PLC_WWW_HOST/"
207 elif [ "$PLC_WWW_PORT" != "80" ] ; then
208 PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
210 PLC_WWW_URL="http://$PLC_WWW_HOST/"
213 mkdir -p $overlay/etc
214 cat >$overlay/etc/issue <<EOF
220 This machine is a node in the $PLC_NAME distributed network. It has
221 not fully booted yet. If you have cancelled the boot process at the
222 request of $PLC_NAME Support, please follow the instructions provided
223 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
225 Console login at this point is restricted to root. Provide the root
226 password of the default $PLC_NAME Central administrator account at the
227 time that this CD was created.
232 echo "* Setting root password"
234 if [ -z "$ROOT_PASSWORD" ] ; then
235 # Generate an encrypted password with crypt() if not defined
236 # in a static configuration.
237 ROOT_PASSWORD=$(python <<EOF
238 import crypt, random, string
239 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
240 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
245 # build/passwd copied out by prep.sh
246 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
249 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
250 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
251 echo "* Installing node configuration file"
252 install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
255 # Pack overlay files into a compressed archive
256 echo "* Compressing overlay image"
257 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
262 if [ -n "$CUSTOM_DIR" ]; then
263 echo "* Compressing custom image"
264 (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
267 # Calculate ramdisk size (total uncompressed size of both archives)
268 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
269 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
271 echo "$FULL_VERSION_STRING" >$isofs/pl_version
281 # Write isolinux configuration
282 cat >$isofs/isolinux.cfg <<EOF
283 ${serial:+SERIAL 0 115200}
285 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
292 echo "* Creating ISO image"
297 function build_usb_partition()
299 echo -n "* Creating USB image with partitions..."
304 local size=$(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
305 size=$(( $size / 1024 ))
309 local cylinders=$(( ($size*1024*2)/($heads*$sectors) ))
310 local offset=$(( $sectors*512 ))
312 /usr/lib/syslinux/mkdiskimage -M -4 "$usb" $size $heads $sectors
314 cat >${BUILDTMP}/mtools.conf<<EOF
323 # environment variable for mtools
324 export MTOOLSRC="${BUILDTMP}/mtools.conf"
326 ### COPIED FROM build_usb() below!!!!
327 echo -n " populating USB image... "
328 mcopy -bsQ -i "$usb" "$isofs"/* z:/
330 # Use syslinux instead of isolinux to make the image bootable
331 tmp="${BUILDTMP}/syslinux.cfg"
333 ${serial:+SERIAL 0 115200}
335 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
340 mdel -i "$usb" z:/isolinux.cfg 2>/dev/null || :
341 mcopy -i "$usb" "$tmp" z:/syslinux.cfg
343 rm -f "${BUILDTMP}/mtools.conf"
346 echo "making USB image bootable."
347 syslinux -o $offset "$usb"
354 echo -n "* Creating USB image... "
359 mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
362 echo -n " populating USB image... "
363 mcopy -bsQ -i "$usb" "$isofs"/* ::/
365 # Use syslinux instead of isolinux to make the image bootable
366 tmp="${BUILDTMP}/syslinux.cfg"
368 ${serial:+SERIAL 0 115200}
370 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
375 mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
376 mcopy -i "$usb" "$tmp" ::/syslinux.cfg
379 echo "making USB image bootable."
384 # Setup CRAMFS related support
385 function prepare_cramfs()
387 [ -n "$CRAMFS_PREPARED" ] && return 0
390 echo "* Setting up CRAMFS-based images"
391 local tmp="${BUILDTMP}/cramfs-tree"
393 push_cleanup rm -rf $tmp
395 gzip -d -c $isofs/bootcd.img | cpio -diu
396 gzip -d -c $isofs/overlay.img | cpio -diu
397 [ -n "$custom" ] && \
398 gzip -d -c $isofs/custom.img | cpio -diu
400 # clean out unnecessary rpm lib
401 echo "* clearing var/lib/rpm/*"
404 # bootcd requires this directory
405 mkdir -p mnt/confdevice
407 # relocate various directory to /tmp
409 ln -fs /tmp/root root
410 ln -fs /sbin/init linuxrc
411 ln -fs /tmp/resolv.conf etc/resolv.conf
412 ln -fs /tmp/etc/mtab etc/mtab
414 # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
416 echo "* renaming dirs in ./etc"
418 for dir in `find * -type d -prune | grep -v rc.d`; do
420 ln -fs /tmp/etc/${dir} ${dir}
424 echo "* renaming dirs in ./var"
425 # rename all top-level directories and put in a symlink to /tmp/var
427 for dir in `find * -type d -prune`; do
429 ln -fs /tmp/var/${dir} ${dir}
433 # overwrite fstab to mount / as cramfs and /tmp as tmpfs
434 echo "* Overwriting etc/fstab to use cramfs and tmpfs"
436 cat >./etc/fstab <<EOF
437 /dev/ram0 / cramfs ro 0 0
438 none /dev/pts devpts gid=5,mode=620 0 0
439 none /proc proc defaults 0 0
440 none /sys sysfs defaults 0 0
446 #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
447 #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
452 # update etc/inittab to start with pl_rsysinit
453 sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
455 # modify inittab to have a serial console
456 echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
457 # and let root log in
458 echo "ttyS0" >> etc/securetty
460 # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
461 etcsize=$(du -s ./etc | awk '{ print $1 }')
462 varsize=$(du -s ./var | awk '{ print $1 }')
463 let msize=($varsize+$etcsize+8192)/1024
466 # generate pl_rsysinit
467 cat > etc/rc.d/init.d/pl_rsysinit <<EOF
469 # generated by build.sh
470 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
471 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
474 touch /tmp/etc/resolv.conf
479 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
481 # copy over directory contents of all _o directories from /etc and /var
482 # /tmp/etc and /tmp/var
484 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
487 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
491 # hand over to pl_sysinit
492 echo "pl_rsysinit: handing over to pl_sysinit"
493 /etc/init.d/pl_sysinit
495 chmod +x etc/rc.d/init.d/pl_rsysinit
499 # create the cramfs image
500 echo "* Creating cramfs image"
501 mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
502 cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
507 # Create ISO CRAMFS image
508 function build_iso_cramfs()
513 echo "* Creating ISO CRAMFS-based image"
515 local tmp="${BUILDTMP}/cramfs-iso"
517 push_cleanup rm -rf $tmp
518 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
519 cat >$tmp/isolinux.cfg <<EOF
520 ${serial:+SERIAL 0 9600}
522 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
528 cp ${BUILDTMP}/cramfs.img $tmp
537 # Create USB CRAMFS based image
538 function build_usb_cramfs()
543 echo "* Creating USB CRAMFS based image"
545 let vfat_size=${cramfs_size}+$FREE_SPACE
547 # Make VFAT filesystem for USB
548 mkfs.vfat -C "$usb" $vfat_size
551 echo "* Populating USB with overlay images and cramfs"
552 mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
553 mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
555 # Use syslinux instead of isolinux to make the image bootable
556 tmp="${BUILDTMP}/syslinux.cfg"
558 ${serial:+SERIAL 0 9600}
560 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
565 mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
568 echo "* Making USB CRAMFS based image bootable"
572 function type_to_name()
576 s/usb_partition$/-partition.usb/;
577 s/usb_serial$/-serial.usb/;
579 s/iso_serial$/-serial.iso/;
580 s/usb_cramfs$/-cramfs.usb/;
581 s/usb_cramfs_serial$/-cramfs-serial.usb/;
582 s/iso_cramfs$/-cramfs.iso/;
583 s/iso_cramfs_serial$/-cramfs-serial.iso/;
587 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
591 tname=`type_to_name $t`
592 if [[ "$t" == *_serial ]]; then
594 t=`echo $t | sed 's/_serial$//'`
596 build_$t "${OUTPUT_BASE}${tname}" $serial $CUSTOM_DIR