Change the local-backend identifier to local://
[snf-image] / snf-image-host / snf-image-update-helper.in
index 1c7c071..56a48e8 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-# Copyright (C) 2011 GRNET S.A. 
+# Copyright (C) 2011 GRNET S.A.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # 02110-1301, USA.
 
 set -e
-set -o pipefail
 set -o errtrace
 
 . @osdir@/common.sh
 
+DEFAULT_FILE="@sysconfdir@/default/snf-image-update-helper"
+
+if [ -f "$DEFAULT_FILE" ]; then
+    . "$DEFAULT_FILE"
+fi
+
+: ${HELPER_URL:="@HELPER_URL@"}
+
 usage() {
     local rc="$1"
 
@@ -30,71 +37,49 @@ usage() {
 
 Usage: $0 [options]
 
-This script runs a debootstrap and creates a small Debian image populated with
-the snf-image-helper package. This needs to be done before the first use of
-ganeti's \`snf-image' guest OS type.
+This script will download a suitable fixed snf-image-helper image and will
+install it under $IMAGE_DIR.
 
 OPTIONS:
-    -d DIRECTORY
-        Use this directory to host the created files, instead of the default
-        [default: $HELPER_DIR]
-
-    -h  Print this message
+    -h Print this message
 
-    -p PACKAGE
-        Install this deb package in the helper image, instead of the default
+    -u URL
+       Download URL to use for the helper image instead of $HELPER_URL
 
-    -r  Don't use any existing cache
+    -y Assume Yes to all queries and do not prompt
 
-    -y  Assume Yes to all queries and do not prompt
+    -f Force the installation even if the snf-image and helper versions don't match
 
 EOF
 
     exit "$rc"
 }
 
-while getopts "d:hp:ry" opt; do
+while getopts "hu:yf" opt; do
     case $opt in
-        d) HELPER_DIR="$OPTARG"
-            ;;
         h) usage 0
             ;;
-        p) HELPER_PKG="$OPTARG"
-            ;;
-        r) NO_CACHE="yes"
+        u) HELPER_URL="$OPTARG"
             ;;
         y) NO_PROMPT="yes"
             ;;
-        ?) log_error "Use \`-h' for help"; exit 1
+        f) FORCE="yes"
+            ;;
+        ?) log_error "User \`-h' for help"; exit 1
             ;;
     esac
 done
 
-echo
-echo "This is the update helper image script for snf-image."
-echo "If you don't know what to do, use \`-h'."
-echo
-
-if [ ! -d "$HELPER_DIR" -o ! -w "$HELPER_DIR" ]; then
-    log_error "Helper directory \`$HELPER_DIR' does not exist or the script" \
-    "has no write permission on it."
-    exit 1
-fi
-
-if [ ! -e "$MULTISTRAP_CONFIG" ]; then
-    log_error "Multistrap configuration file: \`$MULTISTRAP_CONFIG' does not" \
-    "exist or is not readable by the script."
-    exit 1
-fi
-
 cat >&1 <<EOF
-This program will overwrite the following files if present:
+
+$(basename $0) will download a snf-image-helper image from:
+\`$HELPER_URL' and will install it under $IMAGE_DIR.
+The following files will be overwritten if present:
   \`$HELPER_DIR/initrd'
   \`$HELPER_DIR/kernel'
-  \`$HELPER_DIR/initrd-xen'
-  \`$HELPER_DIR/kernel-xen'
   \`$HELPER_DIR/image'
   \`$HELPER_DIR/packages'
+  \`$HELPER_DIR/version'
 EOF
 
 while [[ 1 ]]; do
@@ -112,125 +97,41 @@ while [[ 1 ]]; do
     fi
 done
 
-rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
-
-echo -n "Allocating space for helper disk image..."
-helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX")
-add_cleanup rm -f "$helper_img"
-dd if=/dev/zero of="$helper_img" bs=1M count="$HELPER_SIZE" &> /dev/null
-echo "done"
-
-echo "Creating partitions..."
-blockdev=$("$LOSETUP" -sf $helper_img)
-add_cleanup "$LOSETUP" -d "$blockdev"
-
-sleep 1 # sometimes losetup returns and the device is still busy..
-
-format_disk0 "$blockdev" "extdump"  2>&1 | sed -e 's/^/CFDISK: /g'
+IMAGE_DIR=$(mktemp -d)
+add_cleanup rmdir "$IMAGE_DIR"
 
-root_dev=$(map_disk0 "$blockdev")-1
-add_cleanup unmap_disk0 "$blockdev"
+cd "$IMAGE_DIR"
 
-echo Creating and configuring filesystem...
-mkfs.ext3 "$root_dev" 2>&1 | sed -e 's/^/MKFS.EXT3: /g'
-# The helper vm should never do filesystem checks...
-tune2fs -i 0 -c 0 "$root_dev" 2>&1 | sed -e 's/^/TUNE2FS: /g'
+IMAGE=$(basename "$HELPER_URL")
+echo >&2
+echo "Downloading helper image from $HELPER_URL ..." >&2
+$CURL -O -L -f "$HELPER_URL"
+add_cleanup rm -f "$IMAGE_DIR/$IMAGE"
 
-root_uuid=$(blkid -s UUID -o value "$root_dev")
+cd "$HELPER_DIR"
 
-target=$(mktemp -d)
-add_cleanup rmdir "$target"
+echo >&2
+echo "Extracting helper image under \`$HELPER_DIR':" >&2
+tar -xvf "$IMAGE_DIR/$IMAGE"
 
-mount "$root_dev" "$target"
-add_cleanup umount "$root_dev"
-
-do_multistrap "$target"
-
-# Save the package list
-chroot "$target" dpkg-query -W  > "$HELPER_DIR/packages"
-
-echo -n "Configuring the helper image..."
-echo snf-image-helper > "$target/etc/hostname"
-
-cat > "$target/etc/fstab" <<EOF
-# /etc/fstab: static file system information.
-#
-# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
-UUID=$root_uuid         /               ext3    defaults        0       1
-proc              /proc           proc    defaults        0       0
-EOF
-
-# We need this since we mount the helper VM ro
-ln -sf /proc/mounts "$target/etc/mtab"
-
-echo "done"
-
-echo -n "Extracting kernel..."
-if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
-    echo -e "\033[1;31mfailed\033[0m"
-    log_error "vmlinuz or initrd.img link in root is missing."
-    log_error "I don't know how to find a usable kernel/initrd pair."
-    exit 1
-fi
-echo "done"
-
-# Make sure extended globbing is enabled
-shopt -s extglob
-
-kernel=$(ls "$target"/boot/vmlinuz-+([0-9.])-+([a-z0-9])-amd64)
-initrd=$(ls "$target"/boot/initrd.img-+([0-9.])-+([a-z0-9])-amd64)
-
-kernel_xen=$(ls "$target"/boot/vmlinuz-+([0-9.])-+([a-z0-9])-xen-amd64)
-initrd_xen=$(ls "$target"/boot/initrd.img-+([0-9.])-+([a-z0-9])-xen-amd64)
-
-echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
-cp "$kernel" "$initrd" "$HELPER_DIR"
-
-echo "Moving $(basename "$kernel_xen") and $(basename "$initrd_xen") to \`$HELPER_DIR'"
-cp "$kernel_xen" "$initrd_xen" "$HELPER_DIR"
-
-kernel=$(basename "$kernel")
-initrd=$(basename "$initrd")
-
-kernel_xen=$(basename "$kernel_xen")
-initrd_xen=$(basename "$initrd_xen")
-(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
-(cd "$HELPER_DIR"; ln -fs "$kernel_xen" kernel-xen; ln -fs "$initrd_xen" initrd-xen)
-
-
-pkg_installed=$(grep snf-image-helper "$HELPER_DIR/packages" > /dev/null && echo yes)
-
-if [ -z "$HELPER_PKG" -a -z "$pkg_installed" ]; then
-    log_error "No helper package was specified and non was found by the apt."
-    exit 1
-fi
-
-if [ -r "$HELPER_PKG" ]; then
-    echo "Installing snf-image-helper pkg in the new image..."
-    cp "$HELPER_PKG" "$target/tmp/"
-    pkg_name=$(basename "$HELPER_PKG")
-    add_cleanup rm "$target/tmp/$pkg_name"
-    chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
-
-    # Recreate package list
-    chroot "$target" dpkg-query -W  > "$HELPER_DIR/packages"
-else
-    echo "snf-image-helper pkg was installed from the apt repository."
-fi
-
-if [ "$VERSION_CHECK" == yes -a -z "$HELPER_PKG" ]; then
-    helper_version="$(grep ^snf-image-helper[[:space:]] "$HELPER_DIR/packages" | cut -f2)"
-    host_version="$(dpkg-query -W -f "\${Version}\n" snf-image-host)"
-    if [ "$host_version" != "$helper_version" ]; then
-        log_error "snf-image-host version (=$host_version) and " \
-            "snf-image-helper version (=$helper_version) don't match."
+if [ "x$FORCE" != "xyes" ]; then
+    echo >&2
+    echo "Checking helper image version ..." >&2
+    if [ ! -f "$HELPER_DIR/version" ]; then
+        log_error "File: \`$HELPER_DIR/version' is missing!"
+        exit 1
+    fi
+    helper_version="$(cat $HELPER_DIR/version)"
+    if [ "x$SNF_IMAGE_VERSION" != "x$helper_version" ]; then
+        log_error "snf-image version (=$SNF_IMAGE_VERSION) and " \
+            "helper image version (=$helper_version) don't match!"
+        log_error "Use \`-f' to bypass the version check."
         exit 1
     fi
 fi
 
-mv "$helper_img" "$HELPER_DIR/image"
-
-echo "Files in \`$HELPER_DIR' were updated successfully"
+echo >&2
+echo "Helper image was installed successfully!" >&2
 exit 0
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :