#!/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"
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:
- -c CACHE_FILE
- Use this cache file, instead of the default
- [default: $HELPER_CACHE_FILE]
-
- -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
+ -u URL
+ Download URL to use for the helper image instead of $HELPER_URL
- -p PACKAGE
- Install this deb package in the helper image, instead of the default
- [default: $HELPER_PKG]
+ -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 "c:d:hp:y" opt; do
+while getopts "hu:yf" opt; do
case $opt in
- c) CACHE_FILE="$OPTARG"
- OVERWRITTEN_CACHE_FILE="yes"
- ;;
- d) HELPER_DIR="$OPTARG"
- ;;
h) usage 0
;;
- p) HELPER_PKG="$OPTARG"
+ 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
-if [ x"$OVERWRITTEN_CACHE_FILE" != "xyes" ] ; then
- CACHE_FILE="$HELPER_CACHE_FILE"
-fi
-
-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 "ERROR:"
- log_error "Helper directory \`$HELPER_DIR' does not exist or the script" \
- "has no write permission on it."
- exit 1
-fi
-
-if [ ! -r "$HELPER_PKG" ]; then
- log_error "ERROR:"
- log_error "Helper package \`$HELPER_PKG' 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/image'
+ \`$HELPER_DIR/packages'
+ \`$HELPER_DIR/version'
EOF
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")
+IMAGE_DIR=$(mktemp -d)
+add_cleanup rmdir "$IMAGE_DIR"
-dd if=/dev/zero of="$helper_img" bs=1k count=800000 &> /dev/null
-echo "done"
+cd "$IMAGE_DIR"
-echo "Creating partitions..."
-blockdev=$("$LOSETUP" -sf $helper_img)
-add_cleanup "$LOSETUP" -d "$blockdev"
+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"
-sleep 1 # sometimes losetup returns and the device is still busy..
+cd "$HELPER_DIR"
-format_disk0 "$blockdev" "extdump" 2>&1 | sed -e 's/^/CFDISK: /g'
+echo >&2
+echo "Extracting helper image under \`$HELPER_DIR':" >&2
+tar -xvf "$IMAGE_DIR/$IMAGE"
-root_dev=$(map_disk0 "$blockdev")-1
-add_cleanup unmap_disk0 "$blockdev"
-
-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'
-
-blkid=$(blkid -s UUID -o value $root_dev)
-
-target=$(mktemp -d)
-add_cleanup rmdir "$target"
-
-mount "$root_dev" "$target"
-add_cleanup umount "$root_dev"
-
-echo -n "Checking for cached root filesystem file \`$CACHE_FILE'..."
-if [ -f "$CACHE_FILE" ]; then
- echo "found"
-
-
- missing_pkgs="no"
- if [ "$CACHE_FILE" == "$HELPER_CACHE_FILE" ]; then
- echo -n "Checking if needed packages are present..."
- if [ ! -f "$HELPER_CACHE_PKGS" ]; then
- missing_pkgs="yes"
- echo "packages file: \`$HELPER_CACHE_PKGS' does not exist"
- else
- OLD_IFS="$IFS"
- IFS=,; for pkg in $HELPER_EXTRA_PKGS; do
- if ! grep "^$pkg\$" "$HELPER_CACHE_PKGS" > /dev/null; then
- missing_pkgs="yes"
- echo "$pkg is missing."
- break
- fi
- done
- IFS="$OLD_IFS"
- if [ "$missing_pkgs" == "no" ]; then
- echo "done"
- fi
- fi
- if [ "$missing_pkgs" == "yes" ]; then
- do_debootstrap "$target"
- fi
+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
-
-
- test "$missing_pkgs" == "no" && while [[ 1 ]]; do
- echo -n "Use the cached file [Y/n]? "
- if [ "x$NO_PROMPT" = "xyes" ]; then
- echo "y";
- break;
- fi
-
- read answer
-
- if [ -z "$answer" -o "$(tr [A-Z] [a-z] <<< "$answer")" = "y" ]; then
- break;
- elif [ "$(tr [A-Z] [a-z] <<< "$answer" )" = "n" ]; then
- do_debootstrap "$target"
- break;
- fi
- done
-
-else
- echo "not found"
- do_debootstrap "$target"
-fi
-
-tar xf "$HELPER_CACHE_FILE" -C "$target"
-
-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=$blkid / ext3 defaults 0 1
-proc /proc proc defaults 0 0
-EOF
-echo "done"
-
-
-
-
-setup_image() {
- # setup_image pkg suffix
-
- echo "Getting linux image $1"
- chroot "$target" apt-get install -y --force-yes -qq "$1" &>/dev/null
-
- 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."
+ 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
- echo "done"
-
- kernel=$(readlink -en "$target/vmlinuz")
- initrd=$(readlink -en "$target/initrd.img")
-
- echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
- mv "$kernel" "$initrd" "$HELPER_DIR"
-
- kernel=$(basename "$kernel")
- initrd=$(basename "$initrd")
-
- (
- cd "$HELPER_DIR";
- ln -fs "$kernel" kernel$2;
- ln -fs "$initrd" initrd$2;
- )
-
- rm "$target/vmlinuz" "$target/initrd.img"
-
-}
-
-
-setup_image $HELPER_LINUX_IMAGE_PKG ""
-setup_image $HELPER_LINUX_IMAGE_XEN_PKG "-xen"
-
-
-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'
-
-cat > "$target/etc/rc.local" <<EOF
-#!/bin/sh -e
-#
-# rc.local
-#
-# This script is executed at the end of each multiuser runlevel.
-# Make sure that the script will "exit 0" on success or any other
-# value on error.
-#
-# In order to enable or disable this script just change the execution
-# bits.
-#
-# By default this script does nothing.
-
-if ! grep -q snf_image_activate_helper /proc/cmdline; then
- echo "WARNING: NOT calling snf-image-helper, add snf_image_activate_helper"
- echo "to the kernel command line if you want to do so."
-else
- for x in \$(cat /proc/cmdline); do
- case x in
- hypervisor*) eval \$x
- ;;
- esac
- done
- case \$hypervisor in
- xen-pvm|xen-hvm)
- mount -t xenfs xenfs /proc/xen
- ip link set eth0 up
- echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
- ;;
- esac
- /usr/bin/snf-image-helper --force
fi
-exit 0
-EOF
-
-chmod +x "$target/etc/rc.local"
-
-echo "done"
-
-cleanup
-
-mv "$helper_img" "$HELPER_DIR/image"
-
-trap - EXIT
-
-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 :