3 # Builds custom BootCD ISO and USB images in the current
6 # Aaron Klingaman <alk@absarokasoft.com>
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2004-2007 The Trustees of Princeton University
13 PATH=/sbin:/bin:/usr/sbin:/usr/bin
16 NODE_CONFIGURATION_FILE=
17 DEFAULT_TYPES="usb iso"
18 # Leave 4 MB of free space
22 GRAPHIC_CONSOLE="graphic"
23 SERIAL_CONSOLE="ttyS0:115200:n:8"
24 CONSOLE_INFO=$GRAPHIC_CONSOLE
25 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
28 for x in iso usb usb_partition; do for s in "" "_serial" ; do for c in "" "_cramfs" ; do
31 usb_partition_cramfs|usb_partition_cramfs_serial)
35 ALL_TYPES="$ALL_TYPES $t" ;;
40 # the custom-dir feature is designed to let a myplc try/ship a patched bootcd
41 # without the need for a full devel environment
42 # for example, you would create /root/custom-bootcd/etc/rc.d/init.d/pl_hwinit
43 # and run this script with -C /root/custom-bootcd
44 # this creates a third .img image of the custom dir, that 'hides' the files from
45 # bootcd.img in the resulting unionfs
46 # it seems that this feature has not been used nor tested in a long time, use with care
49 echo "Usage: build.sh [OPTION]..."
50 echo " -f plnode.txt Node to customize CD for (default: none)"
51 echo " -t 'types' Build the specified images (default: $DEFAULT_TYPES)"
52 echo " -a Build all known types as listed below"
53 echo " -s console-info Enable a serial line as console and also bring up getty on that line"
54 echo " console-info: tty:baud-rate:parity:bits"
55 echo " or 'default' shortcut for $SERIAL_CONSOLE"
56 echo " Using this is recommended, rather than mentioning 'serial' in a type"
57 echo " -O output-base The prefix of the generated files (default: PLC_NAME-BootCD-VERSION)"
58 echo " useful when multiple types are provided"
59 echo " can be a full path"
60 echo " -o output-name The full name of the generated file"
61 echo " -C custom-dir Custom directory"
62 echo " -n Dry run - mostly for debug/test purposes"
63 echo " -h This message"
64 echo "All known types: $ALL_TYPES"
71 while getopts "f:t:as:O:o:C:nh" opt ; do
74 NODE_CONFIGURATION_FILE=$OPTARG ;;
76 TYPES="$TYPES $OPTARG" ;;
80 CONSOLE_INFO="$OPTARG"
81 [ "$CONSOLE_INFO" == "default" ] && CONSOLE_INFO=$SERIAL_CONSOLE
84 OUTPUT_BASE="$OPTARG" ;;
86 OUTPUT_NAME="$OPTARG" ;;
88 CUSTOM_DIR="$OPTARG" ;;
96 # use default if not set
97 [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
99 # Do not tolerate errors
102 # Change to our source directory
103 srcdir=$(cd $(dirname $0) && pwd -P)
107 isofs=$PWD/build/isofs
109 # The reference image is expected to have been built by prep.sh (see .spec)
110 # we disable the initial logic that called prep.sh if that was not the case
111 # this is because prep.sh needs to know pldistro
112 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
113 echo "You have to run prep.sh prior to calling $0 - exiting"
117 # build/version.txt written by prep.sh
118 BOOTCD_VERSION=$(cat build/version.txt)
120 if [ -f /etc/planetlab/plc_config ] ; then
121 # Source PLC configuration
122 . /etc/planetlab/plc_config
125 FULL_VERSION_STRING="${PLC_NAME} BootCD ${BOOTCD_VERSION}"
127 echo "* Building images for $FULL_VERSION_STRING"
129 # From within a myplc chroot /usr/tmp is too small
130 # to build all possible images, whereas /data is part of the host
131 # filesystem and usually has sufficient space. What we
132 # should do is check whether the expected amount of space
135 if [ -d /data/tmp ] ; then
136 isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
137 if [ -n "$isreadonly" ] ; then
143 declare -a _CLEANUPS=()
144 function do_cleanup()
147 for i in "${_CLEANUPS[@]}"; do
151 function push_cleanup()
153 _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
155 function pop_cleanup()
157 unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
160 trap "do_cleanup" ERR INT EXIT
162 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
163 push_cleanup rm -fr "${BUILDTMP}"
164 mkdir "${BUILDTMP}/isofs"
165 for i in "$isofs"/{bootcd.img,kernel}; do
166 ln -s "$i" "${BUILDTMP}/isofs"
168 cp "/usr/lib/syslinux/isolinux.bin" "${BUILDTMP}/isofs"
169 isofs="${BUILDTMP}/isofs"
171 # Root of the ISO and USB images
172 echo "* Populating root filesystem..."
173 overlay="${BUILDTMP}/overlay"
174 install -d -m 755 $overlay
175 push_cleanup rm -fr $overlay
177 # Create version files
178 echo "* Creating version files"
180 # Boot Manager compares pl_version in both places to make sure that
181 # the right CD is mounted. We used to boot from an initrd and mount
182 # the CD on /usr. Now we just run everything out of the initrd.
183 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
184 mkdir -p $(dirname $file)
185 echo "$FULL_VERSION_STRING" >$file
188 # Install boot server configuration files
189 echo "* Installing boot server configuration files"
191 # We always intended to bring up and support backup boot servers,
192 # but never got around to it. Just install the same parameters for
194 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
195 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
196 install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
197 echo "$PLC_BOOT_HOST" >$dir/boot_server
198 echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
199 echo "/boot/" >$dir/boot_server_path
202 # Install old-style boot server configuration files
203 # as opposed to what a former comment suggested,
204 # this is still required, somewhere in the bootmanager apparently
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" != "$GRAPHIC_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 ${console_baud}}
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" != "$GRAPHIC_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" != "$GRAPHIC_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" != "$GRAPHIC_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 [ -n "$serial" ] ; 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" != "$GRAPHIC_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" != "$GRAPHIC_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/;
680 s/usb_cramfs$/-cramfs.usb/;
681 s/iso_cramfs$/-cramfs.iso/;
685 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
689 console=$CONSOLE_INFO
691 # figure if this is a serial image (can be specified in the type or with -s)
692 if [[ "$t" == *serial* || "$console" != "$GRAPHIC_CONSOLE" ]]; then
693 # remove serial from type
694 t=`echo $t | sed 's/_serial//'`
696 [ "$console" == "$GRAPHIC_CONSOLE" ] && console="$SERIAL_CONSOLE"
697 # compute filename part
698 if [ "$console" == "$SERIAL_CONSOLE" ] ; then
701 serial="-serial-$(echo $console | sed -e 's,:,,g')"
707 tname=`type_to_name $t`
708 # if -o is specified (as it has no default)
709 if [ -n "$OUTPUT_NAME" ] ; then
712 output="${OUTPUT_BASE}${serial}${tname}"
715 echo "*** Dealing with type=$arg"
716 echo '*' build_$t "$output" "$console" "$CUSTOM_DIR"
717 if [ ! -n "$DRY_RUN" ] ; then
718 build_$t "$output" "$console" "$CUSTOM_DIR"