Update ChangeLog and configure.ac for ver. 0.7.2
[snf-image] / snf-image-host / common.sh.in
index f581c97..150fb4c 100644 (file)
@@ -22,13 +22,19 @@ LOSETUP="losetup"
 SFDISK="sfdisk"
 QEMU_IMG="qemu-img"
 INSTALL_MBR="install-mbr"
-TIMELIMIT="timelimit"
+TIMEOUT="timeout"
 CURL="curl"
-PROGRESS_MONITOR="snf-progress-monitor"
+DATE="date -u" # Time in UTC
 
-progress_monitor_support="@progress_monitor_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=( )
 
 add_cleanup() {
     local cmd=""
@@ -37,7 +43,39 @@ add_cleanup() {
 }
 
 log_error() {
-    echo "$@" >&2
+    echo "[ERROR] $*" >&2
+}
+
+report_error() {
+    ERROR_MSGS+=("$@")
+}
+
+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}>&-
+}
+
+send_errors() {
+    local report=""
+    if [ ${#ERROR_MSGS[@]} -gt 0 ]; then
+        local msg=""
+        for err in "${ERROR_MSGS[@]}"; do
+            msg+="$(echo "$err")"
+        done
+        report="$(./host-monitor.py error <<< "$msg")"
+    else
+        report=$(./host-monitor.py error <<< "Internal Error: Image deployment failed.")
+    fi
+
+    eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
 }
 
 get_api5_arguments() {
@@ -86,9 +124,9 @@ get_api10_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"
@@ -105,28 +143,28 @@ get_api10_arguments() {
 
 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() {
@@ -168,9 +206,11 @@ EOF
 }
 
 create_floppy() {
-    local img=$1
+    local img target
+
+    img=$1
 
-    local target=$(mktemp -d)
+    target=$(mktemp -d)
     add_cleanup rmdir "$target"
 
     dd bs=512 count=2880 if=/dev/zero of="$img"
@@ -178,9 +218,37 @@ create_floppy() {
     mount "$img" "$target" -o loop
     set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
         > "$target/rules"
+    if [ -n "$UNATTEND" ]; then
+        if [ -f "$UNATTEND" ]; then
+            cat "$UNATTEND" > "$target/unattend.xml"
+        else
+            log_error "Unattend file: \`"$UNATTEND"' does not exist"
+        fi
+    fi
     umount "$target"
 }
 
+get_backend_type() {
+    local id=$1
+
+    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";
+        else
+            log_error "Retrieving images from the network is not supported."
+            exit 1
+        fi
+    elif [ "$id" = "null" ]; then
+        echo "null"
+    else
+        echo "local"
+    fi
+}
+
 # this one is only to be called by create
 ganeti_os_main() {
     if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
@@ -192,8 +260,9 @@ ganeti_os_main() {
         get_api10_arguments
     elif [ "$OS_API_VERSION" = "20" ]; then
         get_api20_arguments
-        IMAGE_NAME=$IMG_ID
-        IMAGE_TYPE=$IMG_FORMAT
+        IMAGE_NAME="$IMG_ID"
+        IMAGE_TYPE="$IMG_FORMAT"
+        BACKEND_TYPE=$(get_backend_type $IMG_ID)
     else
         log_error "Unknown OS API VERSION $OS_API_VERSION"
         exit 1
@@ -228,9 +297,40 @@ ganeti_os_main() {
 
 }
 
+do_multistrap() {
+   local target="$1"
+   local cache="$2"
+   local pkgs="$3"
+
+    # 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"
+    cat > "$target/usr/sbin/policy-rc.d" <<EOF
+#!/bin/sh
+exit 101
+EOF
+    chmod +x "$target/usr/sbin/policy-rc.d"
+
+   multistrap -d "$target" -f "$MULTISTRAP_CONFIG" 2>&1 | sed -u -e 's/^/MULTISTRAP: /g'
+
+   rm "$target/usr/sbin/policy-rc.d"
+   rm -rf "$target/etc/apt/preferences.d"
+}
+
+report_and_cleanup(){
+    send_errors
+    cleanup
+}
+
 cleanup() {
-# if something fails here, it souldn't call cleanup again...
+    # if something fails here, it souldn't call cleanup again...
     trap - EXIT
+
     if [ ${#CLEANUP[*]} -gt 0 ]; then
         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
@@ -239,11 +339,12 @@ cleanup() {
             # before we give up with an error. This is needed for kpartx when
             # dealing with ntfs partitions mounted through fuse. umount is not
             # synchronous and may return while the partition is still busy. A
-            # premature attempt to delete partition mappings through kpartx on a
-            # device that hosts previously mounted ntfs partition may fail with
-            # an  `device-mapper: remove ioctl failed: Device or resource busy'
-            # error. A sensible workaround for this is to wait for a while and
-            # then try again.
+            # premature attempt to delete partition mappings through kpartx on
+            # a device that hosts previously mounted ntfs partition may fail
+            # with errors like this one:
+            # `device-mapper: remove ioctl failed: Device or resource busy'
+            # A sensible workaround for this is to wait for a while and then
+            # retry it.
             local cmd=${CLEANUP[$i]}
             $cmd || for interval in 0.25 0.5 1 2 4; do
             echo "Command $cmd failed!"
@@ -268,36 +369,24 @@ fi
 
 : ${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:=15}
+: ${HELPER_SOFT_TIMEOUT:=20}
 : ${HELPER_HARD_TIMEOUT:=5}
 : ${HELPER_USER:="nobody"}
-: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
-: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw"}
+: ${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)
 
-if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
-    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
-    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
-else
-    for dir in /lib/udev /sbin; do
-        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
-            VOL_ID="$dir/vol_id -u"
-            VOL_TYPE="$dir/vol_id -t"
-        fi
-    done
-fi
-
-if [ -z "$VOL_ID" ]; then
-    log_error "vol_id or blkid not found, please install udev or util-linux"
-    exit 1
-fi
-
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :