INSTALL_MBR="install-mbr"
TIMEOUT="timeout"
CURL="curl"
+DATE="date -u" # Time in UTC
-network_backend_support="@network_backend_support@"
+# Temporary use stderr as monitoring file descriptor.
+# `create' will overwrite this
+MONITOR_FD="2"
MSG_TYPE_ERROR="image-error"
+MSG_TYPE_INFO="image-info"
CLEANUP=( )
ERROR_MSGS=( )
}
log_error() {
+ echo "[ERROR] $*" >&2
+}
+
+report_error() {
ERROR_MSGS+=("$@")
- echo "$@" >&2
}
+report_info() {
+ local report
+ echo "[INFO] $*" >&2
+ report="$(./host-monitor.py info <<< "$*")"
+ eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
+}
+
+
close_fd() {
local fd="$1"
exec {fd}>&-
}
-report_error() {
- local error_file=$1
- local monitor_fd=$2
-
- local type="$MSG_TYPE_ERROR"
-
- report="{\"type\":\"$type\","
- report+="\"timestamp\":$(date +%s.%N),"
-
+send_errors() {
+ local report=""
if [ ${#ERROR_MSGS[@]} -gt 0 ]; then
- local msg="["
+ local msg=""
for err in "${ERROR_MSGS[@]}"; do
- msg+="\"$(sed 's/"/\\"/g' <<< "$err")\","
+ msg+="$(echo "$err")"
done
- if [ ${#msg} -gt 1 ]; then
- # remove last comma (,)
- msg="${msg%?}"
- fi
- msg+="]"
- report+="\"messages\":$msg}"
+ report="$(./host-monitor.py error <<< "$msg")"
else
- local stderr="$(cat "$error_file" | sed 's/"/\\"/g')"
- report+="\"stderr\":\"$stderr\"}"
+ report=$(./host-monitor.py error <<< "Internal Error: Image deployment failed.")
fi
- eval "echo $(printf "%q" "$report") >&${monitor_fd}"
+ eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
}
get_api5_arguments() {
fi
if [ "$SCRIPT_NAME" = "export" ]; then
if [ -z "$EXPORT_DEVICE" ]; then
- log_error "Missing OS API Variable EXPORT_DEVICE"
- fi
- blockdev=$EXPORT_DEVICE
+ log_error "Missing OS API Variable EXPORT_DEVICE"
+ fi
+ blockdev=$EXPORT_DEVICE
elif [ "$SCRIPT_NAME" = "import" ]; then
if [ -z "$IMPORT_DEVICE" ]; then
log_error "Missing OS API Variable IMPORT_DEVICE"
get_api20_arguments() {
get_api10_arguments
- if [ -z "$OSP_IMG_ID" ]; then
- log_error "Missing OS API Parameter: OSP_IMG_ID"
- exit 1
- fi
- if [ -z "$OSP_IMG_FORMAT" ]; then
- log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
- exit 1
- fi
- if [ -z "$OSP_IMG_PASSWD" ]; then
- log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
- exit 1
- fi
- IMG_ID=$OSP_IMG_ID
- IMG_FORMAT=$OSP_IMG_FORMAT
- IMG_PASSWD=$OSP_IMG_PASSWD
- if [ -n "$OSP_IMG_PROPERTIES" ]; then
- IMG_PROPERTIES="$OSP_IMG_PROPERTIES"
- fi
- if [ -n "$OSP_IMG_PERSONALITY" ]; then
- IMG_PERSONALITY="$OSP_IMG_PERSONALITY"
+ local required_osparams="IMG_ID IMG_FORMAT IMG_PASSWD"
+ local osparams="$required_osparams IMG_PROPERTIES IMG_PERSONALITY CONFIG_URL"
+
+ # Store OSP_VAR in VAR
+ for param in $osparams; do
+ eval $param=\"\$OSP_$param\"
+ done
+
+ if [ -n "$CONFIG_URL" ]; then
+ local config config_params
+ config=$($CURL -f "$CONFIG_URL")
+ config_params=$(./decode-config.py $osparams <<< "$config")
+ eval "$config_params"
fi
+
+ for var in $required_osparams; do
+ if [ -z "${!var}" ]; then
+ log_error "Missing OS API Parameter: ${var}"
+ exit 1
+ fi
+ done
}
map_disk0() {
}
create_floppy() {
- local img=$1
+ local img target
- local target=$(mktemp -d)
+ img=$1
+
+ target=$(mktemp -d)
add_cleanup rmdir "$target"
dd bs=512 count=2880 if=/dev/zero of="$img"
if [[ "$id" =~ ^pithos: ]]; then
echo "pithos"
+ elif [[ "$id" =~ ^pithosmap: ]]; then
+ echo "pithos"
elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
if [ "$network_backend_support" = "yes" ]; then
echo "network";
log_error "Retrieving images from the network is not supported."
exit 1
fi
+ elif [ "$id" = "null" ]; then
+ echo "null"
else
- echo "local";
+ echo "local"
fi
}
}
-do_debootstrap() {
- local target="$1"
+do_multistrap() {
+ local target="$1"
+ local cache="$2"
+ local pkgs="$3"
- echo "Debootstraping to create a new root filesystem:"
+ # Create preferences.d for apt
+ mkdir -p "$target/etc/apt/preferences.d"
+ if [ -d "$MULTISTRAP_APTPREFDIR" ]; then
+ find "$MULTISTRAP_APTPREFDIR" -maxdepth 1 -type f -exec cp {} "$target/etc/apt/preferences.d" \;
+ fi
# Create a policy-rc.d file to deny init script execution
mkdir -p "$target/usr/sbin"
EOF
chmod +x "$target/usr/sbin/policy-rc.d"
- debootstrap --arch $(dpkg --print-architecture) \
- --include "$HELPER_EXTRA_PKGS" --variant=minbase stable "$target" \
- "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
-
- # Save the package list
- chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
+ multistrap -d "$target" -f "$MULTISTRAP_CONFIG" 2>&1 | sed -u -e 's/^/MULTISTRAP: /g'
- rm "$target/usr/sbin/policy-rc.d"
-
- # remove the downloaded debs, as they are no longer needed
- find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
- xargs -r0 rm -f
-
- local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
- tar cf "$tmp_cache" --one-file-system -C "$target" . || \
- { rm "$tmp_cache"; false; }
- # Overwrite the default cache file. Not the user specified if present.
- mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
+ rm "$target/usr/sbin/policy-rc.d"
+ rm -rf "$target/etc/apt/preferences.d"
}
report_and_cleanup(){
-
- local err_file="$1"
- local fd="$2"
-
- report_error "$err_file" "$fd"
+ send_errors
cleanup
}
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
+: ${IMAGE_DEBUG:="no"}
+: ${VERSION_CHECK:="@VERSION_CHECK@"}
: ${HELPER_DIR:="@HELPER_DIR@"}
: ${HELPER_IMG:="@HELPER_IMG@"}
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
: ${HELPER_INITRD:="@HELPER_INITRD@"}
-: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
: ${HELPER_SOFT_TIMEOUT:=20}
: ${HELPER_HARD_TIMEOUT:=5}
: ${HELPER_USER:="nobody"}
-: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
-: ${HELPER_CACHE_PKGS:="@HELPER_DIR@/packages"}
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw,util-linux"}
: ${HELPER_MIRROR:=""}
: ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
: ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
: ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
: ${UNATTEND:="@UNATTEND@"}
+: ${MULTISTRAP_CONFIG:="@MULTISTRAP_CONFIG@"}
+: ${MULTISTRAP_APTPREFDIR:="@MULTISTRAP_APTPREFDIR@"}
SCRIPT_NAME=$(basename $0)