-# Get options
-while getopts "c:f:ah" opt ; do
- case $opt in
- c)
- CONFIGURATION=$OPTARG
- ;;
- f)
- NODE_CONFIGURATION_FILE=$OPTARG
- ;;
- a)
- ALL=1
- ;;
- h|*)
- usage
- ;;
- esac
-done
-
-# Do not tolerate errors
-set -e
-
-# Change to our source directory
-srcdir=$(cd $(dirname $0) && pwd -P)
-pushd $srcdir
-
-# Root of the isofs
-isofs=$PWD/build/isofs
-
-# Build reference image if it does not exist. This should only need to
-# be executed once at build time, never at run time.
-if [ ! -f $isofs/bootcd.img ] ; then
- ./prep.sh
-fi
-
-# build/version.txt written by prep.sh
-BOOTCD_VERSION=$(cat build/version.txt)
-
-if [ -f /etc/planetlab/plc_config ] ; then
- # Source PLC configuration
- . /etc/planetlab/plc_config
-fi
-
-### This support for backwards compatibility can be taken out in the
-### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
-### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
-### set.
-if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
- PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
-fi
-
-# If PLC configuration is not valid, try a static configuration
-if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
- # (Deprecated) Source static configuration
- . configurations/$CONFIGURATION/configuration
- PLC_NAME="PlanetLab"
- PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
- PLC_WWW_HOST="www.planet-lab.org"
- PLC_WWW_PORT=80
- if [ -n "$EXTRA_VERSION" ] ; then
- BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
+####################
+function parse_command_line () {
+
+ # init
+ TYPES=""
+ # Get options
+ while getopts "f:t:as:SO:o:C:V:k:nh" opt ; do
+ case $opt in
+ f) NODE_CONFIGURATION_FILE=$OPTARG ;;
+ t) TYPES="$TYPES $OPTARG" ;;
+ a) TYPES="$ALL_TYPES" ;;
+ s) CONSOLE_INFO="$OPTARG" ;;
+ S) CONSOLE_INFO=$SERIAL_CONSOLE ;;
+ O) OUTPUT_BASE="$OPTARG" ;;
+ o) OUTPUT_NAME="$OPTARG" ;;
+ C) CUSTOM_DIR="$OPTARG" ;;
+ V) VARIANT="$OPTARG" ;;
+ k) KERNEL_ARGS="$KERNEL_ARGS $OPTARG" ;;
+ n) DRY_RUN=true ;;
+ h|*) usage ;;
+ esac
+ done
+
+ # use defaults if not set
+ [ -z "$TYPES" ] && TYPES="$DEFAULT_TYPES"
+ [ -z "$VARIANT" ] && VARIANT="build"
+ [ "$CONSOLE_INFO" == "default" ] && CONSOLE_INFO=$SERIAL_CONSOLE
+
+ if [ -n "$NODE_CONFIGURATION_FILE" ] ; then
+ # check existence of NODE_CONFIGURATION_FILE and normalize as we will change directory
+ if [ ! -f "$NODE_CONFIGURATION_FILE" ] ; then
+ echo "Node configuration file $NODE_CONFIGURATION_FILE not found - exiting"
+ exit 1
+ fi
+ cf_dir="$(dirname $NODE_CONFIGURATION_FILE)"
+ cf_dir="$(cd $cf_dir; pwd -P)"
+ cf_file="$(basename $NODE_CONFIGURATION_FILE)"
+ NODE_CONFIGURATION_FILE="$cf_dir"/"$cf_file"
+ fi
+
+ # check TYPES
+ local matcher="XXX$(echo $ALL_TYPES | sed -e 's,\W,XXX,g')XXX"
+ for t in $TYPES; do
+ echo Checking type $t
+ echo $matcher | grep XXX${t}XXX &> /dev/null
+ if [ "$?" != 0 ] ; then
+ echo Unknown type $t
+ usage
+ fi
+ done
+
+}
+
+####################
+function init_serial () {
+ local console=$1; shift
+ if [ "$console" == "$GRAPHIC_CONSOLE" ] ; then
+ IS_SERIAL=
+ console_spec=""
+ echo "Standard, graphic, non-serial mode"
+ else
+ IS_SERIAL=true
+ console_dev=$(echo "$console" | awk -F: ' {print $1}')
+ console_baud=$(echo "$console" | awk -F: ' {print $2}')
+ [ -z "$console_baud" ] && console_baud="115200"
+ local console_parity=$(echo "$console" | awk -F: ' {print $3}')
+ [ -z "$console_parity" ] && console_parity="n"
+ local console_bits=$(echo "$console" | awk -F: ' {print $4}')
+ [ -z "$console_bits" ] && console_bits="8"
+ console_spec="console=${console_dev},${console_baud}${console_parity}${console_bits}"
+ local tty_nb=$(echo $console_dev | sed -e 's,[a-zA-Z],,g')
+ console_serial_line="SERIAL ${tty_nb} ${console_baud}"
+ echo "Serial mode"
+ echo "console_serial_line=${console_serial_line}"
+ echo "console_spec=${console_spec}"
+ fi
+}
+
+#################### run once : build the overlay image
+function build_overlay () {
+
+ BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
+ push_cleanup rm -fr "${BUILDTMP}"
+
+ # initialize ISOFS
+ ISOFS="${BUILDTMP}/isofs"
+ mkdir -p "$ISOFS"
+ for i in "$ISOREF"/isofs/{bootcd.img,kernel}; do
+ ln -s "$i" "$ISOFS"
+ done
+ # use new location as of fedora 12
+ # used to be in /usr/lib/syslinux/isolinux.bin
+ # removed backward compat in jan. 2015
+ # as of syslinux 6.05 (fedora 21) ldlinux.c32 is required by isolinux.bin
+ # the debug version can come in handy at times, and is 40k as well
+ isolinuxdir="/usr/share/syslinux"
+ # ship only what is mandatory, and forget about
+ # (*) isolinux-debug.bin as its name confuses mkisofs
+ # (*) memdisk that is not useful
+ isolinuxfiles="isolinux.bin ldlinux.c32"
+ for isolinuxfile in $isolinuxfiles; do
+ [ -f $isolinuxdir/$isolinuxfile ] && cp $isolinuxdir/$isolinuxfile "${BUILDTMP}/isofs"
+ done
+
+ # Root of the ISO and USB images
+ echo "* Populating root filesystem..."
+ OVERLAY="${BUILDTMP}/overlay"
+ install -d -m 755 $OVERLAY
+ push_cleanup rm -fr $OVERLAY
+
+ # Create version files
+ echo "* Creating version files"
+
+ # Boot Manager compares pl_version in both places to make sure that
+ # the right CD is mounted. We used to boot from an initrd and mount
+ # the CD on /usr. Now we just run everything out of the initrd.
+ for file in $OVERLAY/pl_version $OVERLAY/usr/isolinux/pl_version ; do
+ mkdir -p $(dirname $file)
+ echo "$FULL_VERSION_STRING" >$file
+ done
+
+ # Install boot server configuration files
+ echo "* Installing boot server configuration files"
+
+ # We always intended to bring up and support backup boot servers,
+ # but never got around to it. Just install the same parameters for
+ # both for now.
+ for dir in $OVERLAY/usr/boot $OVERLAY/usr/boot/backup ; do
+ install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
+ install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
+ echo "$PLC_BOOT_HOST" >$dir/boot_server
+ echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
+ echo "/boot/" >$dir/boot_server_path
+ done
+
+ # Install old-style boot server configuration files
+ # as opposed to what a former comment suggested,
+ # this is still required, somewhere in the bootmanager apparently
+ install -D -m 644 $PLC_BOOT_CA_SSL_CRT $OVERLAY/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
+ echo "$FULL_VERSION_STRING" >$OVERLAY/usr/bootme/ID
+ echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER
+ echo "$PLC_BOOT_HOST" >$OVERLAY/usr/bootme/BOOTSERVER_IP
+ echo "$PLC_BOOT_SSL_PORT" >$OVERLAY/usr/bootme/BOOTPORT
+
+ # Generate /etc/issue
+ echo "* Generating /etc/issue"
+
+ if [ "$PLC_WWW_PORT" = "443" ] ; then
+ PLC_WWW_URL="https://$PLC_WWW_HOST/"
+ elif [ "$PLC_WWW_PORT" != "80" ] ; then
+ PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
+ else
+ PLC_WWW_URL="http://$PLC_WWW_HOST/"