--- /dev/null
+#!/bin/sh
+
+. ${CIRROS_LIB:=/lib/cirros/shlib_cirros} ||
+ { echo "failed to read ${CIRROS_LIB}" 1>&2; exit 1; }
+
+SSH_ROOT_PREFIX="command=\"echo Please login as \\'cirros\\' user, not as root; echo; sleep 10\""
+
+Usage() {
+ cat <<EOF
+Usage: ${0##*/} [ options ] mode
+
+ Call appropriate datasource for its 'apply-<mode>' stage,
+ and then apply generic 'apply-<mode>' hooks.
+
+ If there is no data source, or datasource configured is
+ not of type 'mode', then it will exit silently with success.
+
+ mode is one of 'net' or 'local'
+
+ options:
+ -v | --verbose : be more verbose
+EOF
+}
+
+cirros_apply() {
+ local short_opts="hv"
+ local long_opts="help,verbose"
+ local getopt_out=""
+ getopt_out=$(getopt --name "${0##*/}" \
+ --options "${short_opts}" --long "${long_opts}" -- "$@") &&
+ eval set -- "${getopt_out}" ||
+ { bad_Usage; return; }
+
+ local cur="" next="" mode="" VERBOSITY
+
+ while [ $# -ne 0 ]; do
+ cur=${1}; next=${2};
+ case "$cur" in
+ -h|--help) Usage ; exit 0;;
+ -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
+ --) shift; break;;
+ esac
+ shift;
+ done
+
+ [ $# -ne 0 ] || { bad_Usage "must provide mode"; return; }
+ mode="$1"
+ shift
+
+ [ "$mode" = "net" -o "$mode" = "local" ] ||
+ { bad_Usage "mode must be 'net' or 'local'"; return; }
+
+ local dsname dsmode root
+
+ assert_datasource ||
+ { debug 1 "no datasource present"; return 0; }
+
+ dsname="${_DATASOURCE_NAME}"
+ dsmode="${_DATASOURCE_MODE}"
+
+ [ "${dsmode}" = "$mode" ] || {
+ debug 1 "datasource found is mode '${dsmode}', skipping";
+ return 0;
+ }
+
+ PATH="${DS_D}:$PATH"
+ local root="${TARGET_ROOT}"
+ if [ -n "$root" ]; then
+ local sd="$PWD"
+ cd "$root" && root="$PWD" && cd "$sd" ||
+ return 1;
+ else
+ root="/"
+ fi
+ TARGET_ROOT=$root "$dsname" "apply-${dsmode}" "$RESULTS_D" ||
+ { error "$dsname failed in apply-$dsmode"; return 1; }
+
+ #
+ # here do any common things for $mode
+ #
+ local hostname=""
+ if ds_get_item hostname || ds_get_item local-hostname; then
+ hostname="${_RET%%.*}"
+ echo "$hostname" > /etc/hostname
+ hostname -F /etc/hostname
+ fi
+
+
+ ## if there is a 'ephemeral0' block device, mount it
+ if find_devs_with LABEL=ephemeral0 && [ -n "${_RET}" ]; then
+ # get the first one only
+ local dev="${_RET% *}"
+ echo "${dev} /mnt auto rw,defaults 0 0" >> /etc/fstab
+ mount /mnt ||
+ error "failed to mount ${dev}"
+ fi
+
+ # if we were provided with a network/interfaces file copy it
+ if [ -f "$RESULTS_D/network-interfaces" ]; then
+ local nif="$TARGET_ROOT/etc/network/interfaces"
+ [ -d "$TARGET_ROOT/etc/network" ] ||
+ mkdir -p "$TARGET_ROOT/etc/network"
+ [ ! -f "$nif" ] || cp "$nif" "$nif.dist" ||
+ error "failed to copy network interfaces"
+ debug 1 "copied network/interfaces"
+ fi
+
+ # if we have public-keys, put them in cirros user
+ if ds_get_item_path public-keys; then
+ local keyfile="$_RET"
+ ds_get_item public-keys ||
+ error "failed to get public-keys"
+ mkdir -p "${TARGET_ROOT}/home/cirros" "${TARGET_ROOT}/root"
+ HOME=$TARGET_ROOT/home/cirros su -c "ssh-add-key --replace -" cirros < "$keyfile"
+ HOME=$TARGET_ROOT/root ssh-add-key --replace \
+ --prefix "$SSH_ROOT_PREFIX" - < "$keyfile"
+ fi
+
+ local name=""
+ if [ -c /dev/urandom ]; then
+ for name in random_seed random-seed; do
+ if ds_has_item "$name"; then
+ ds_cat_item "$name" > /dev/urandom && break
+ fi
+ done
+ fi
+
+ return 0
+}
+
+cirros_apply "$@"
+
+# vi: ts=4 noexpandtab