Use a seperated dir for update-helper cache files
authorNikos Skalkotos <skalkoto@grnet.gr>
Thu, 4 Oct 2012 15:44:12 +0000 (18:44 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Fri, 5 Oct 2012 07:18:51 +0000 (10:18 +0300)
By default this directory is $(LOCALSTATEDIR)/cache/snf-image/helper

snf-image-host/Makefile.am
snf-image-host/common.sh.in
snf-image-host/configure.ac
snf-image-host/defaults
snf-image-host/snf-image-update-helper.in

index 51a9b4a..24ef529 100644 (file)
@@ -22,6 +22,7 @@ edit = sed \
           -e 's|@osname[@]|$(osname)|g' \
           -e 's|@defaultdir[@]|$(defaultdir)|g' \
           -e 's|@HELPER_DIR[@]|$(HELPER_DIR)|g' \
+          -e 's|@HELPER_CACHE_DIR[@]|$(HELPER_CACHE_DIR)|g' \
           -e 's|@HELPER_IMG[@]|$(HELPER_IMG)|g' \
           -e 's|@HELPER_KERNEL[@]|$(HELPER_KERNEL)|g' \
           -e 's|@HELPER_INITRD[@]|$(HELPER_INITRD)|g' \
index 8b15239..6487006 100644 (file)
@@ -292,6 +292,8 @@ ganeti_os_main() {
 
 do_debootstrap() {
     local target="$1"
+    local cache="$2"
+    local pkgs="$3"
 
     echo "Debootstraping to create a new root filesystem:"
 
@@ -308,7 +310,7 @@ EOF
         "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
 
     # Save the package list
-    chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
+    chroot "$target" dpkg-query -W -f "\${Package}\n" > "$pkgs"
 
     rm "$target/usr/sbin/policy-rc.d"
 
@@ -316,15 +318,17 @@ EOF
     find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
         xargs -r0 rm -f
 
-    local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
+    local tmp_cache=$(mktemp "$cache.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"
+    mv -f "$tmp_cache" "$cache"
 }
 
 do_multistrap() {
    local target="$1"
+   local cache="$2"
+   local pkgs="$3"
 
     # Create a policy-rc.d file to deny init script execution
     mkdir -p "$target/usr/sbin"
@@ -338,15 +342,15 @@ EOF
         -f "$MULTISTRAP_CONFIG" 2>&1 | sed -u -e 's/^/MULTISTRAP: /g'
 
    # Save the package list
-   chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
+   chroot "$target" dpkg-query -W -f "\${Package}\n" > "$pkgs"
 
    rm "$target/usr/sbin/policy-rc.d"
 
-   local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
+   local tmp_cache=$(mktemp "$cache.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"
+   mv -f "$tmp_cache" "$cache"
 }
 
 report_and_cleanup(){
@@ -401,6 +405,7 @@ fi
 : ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
 : ${IMAGE_DEBUG:="no"}
 : ${HELPER_DIR:="@HELPER_DIR@"}
+: ${HELPER_CACHE_DIR:="@HELPER_CACHE_DIR@"}
 : ${HELPER_IMG:="@HELPER_IMG@"}
 : ${HELPER_KERNEL:="@HELPER_KERNEL@"}
 : ${HELPER_INITRD:="@HELPER_INITRD@"}
@@ -408,8 +413,6 @@ fi
 : ${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"}
index 7df7605..e7fd429 100644 (file)
@@ -65,6 +65,16 @@ AC_ARG_WITH([helper-dir],
     [helper_dir="$localstatedir/lib/snf-image/helper"])
 AC_SUBST(HELPER_DIR, $helper_dir)
 
+# --with-helper-cache-dir
+AC_ARG_WITH([helper-cache-dir],
+    [AS_HELP_STRING([--with-helper-cache-dir=DIR],
+        [top-level directory to host the helper VM cache files
+        [LOCALSTATEDIR/cache/snf-image/helper]]
+    )],
+    [helper_cache_dir="$withval"],
+    [helper_cache_dir="$localstatedir/cache/snf-image/helper"])
+AC_SUBST(HELPER_CACHE_DIR, $helper_cache_dir)
+
 # --with-helper-img
 AC_ARG_WITH([helper-img],
     [AS_HELP_STRING([--with-helper-img=IMG_PATH],
index a8251aa..5dc246a 100644 (file)
@@ -15,6 +15,9 @@
 # HELPER_DIR: Directory hosting the helper files
 # HELPER_DIR="/var/lib/snf-image/helper/"
 
+# HELPER_CACHE_DIR: Directory hosting the helper cache files
+# HELPER_CACHE_DIR="/var/cache/snf-image/helper/"
+
 # HELPER_IMG: Path to the helper VM image
 # HELPER_IMG="${HELPER_DIR}/image"
 
 # HELPER_USER imeddiately before starting execution of the helper VM.
 # HELPER_USER="nobody"
 
-# HELPER_CACHE_FILE: Cache file update-helper script uses to avoid running
-# `debootstrap' all the time.
-# HELPER_CACHE_FILE="${HELPER_DIR}/cache.tar"
-
-# HELPER_CACHE_PKGS: Debian Packages the cache file contains.
-# HELPER_CACHE_PKGS="${HELPER_DIR}/packages"
-
 # MULTISTRAP_CONFIG: Configuration file to be used with multistrap to create
 # the rootfs of the helper image.
 # MULTISTRAP_CONFIG="/etc/snf-image/multistrap.conf"
index 2f15f0e..cb6e395 100644 (file)
@@ -35,10 +35,6 @@ the snf-image-helper package. This needs to be done before the first use of
 ganeti's \`snf-image' guest OS type.
 
 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]
@@ -49,6 +45,8 @@ OPTIONS:
         Install this deb package in the helper image, instead of the default
         [default: $HELPER_PKG]
 
+    -r  Don't use any existing cache
+
     -y  Assume Yes to all queries and do not prompt
 
 EOF
@@ -56,17 +54,16 @@ EOF
     exit "$rc"
 }
 
-while getopts "c:d:hp:y" opt; do
+while getopts "d:hp:ry" opt; do
     case $opt in
-        c) CACHE_FILE="$OPTARG"
-        OVERWRITTEN_CACHE_FILE="yes"
-            ;;
         d) HELPER_DIR="$OPTARG"
             ;;
         h) usage 0
             ;;
         p) HELPER_PKG="$OPTARG"
             ;;
+        r) NO_CACHE="yes"
+            ;;
         y) NO_PROMPT="yes"
             ;;
         ?) log_error "Use \`-h' for help"; exit 1
@@ -74,10 +71,6 @@ while getopts "c:d:hp:y" opt; do
     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'."
@@ -157,35 +150,34 @@ 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 -n "Checking for cached root filesystem in \`$HELPER_CACHE_DIR'..."
+if [ "$NO_CACHE" == "yes" ]; then
+    echo "don't use cache (option -r applied)"
+    do_multistrap "$target" "$HELPER_CACHE_DIR/root.tar" "$HELPER_CACHE_DIR/packages"
+elif [ -f "$HELPER_CACHE_DIR/root.tar" ]; 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
-            extra_pkgs=$(grep ^packages= "$MULTISTRAP_CONFIG" | cut -d= -f2 | tr  '\n' ' ')
-            for pkg in $extra_pkgs; do
-                if ! grep "^$pkg\$" "$HELPER_CACHE_PKGS" > /dev/null; then
-                   missing_pkgs="yes"
-                    echo "$pkg is missing."
-                    break
-                fi
-            done
-            if [ "$missing_pkgs" == "no" ]; then
-                echo "done"
+    echo -n "Checking if needed packages are present..."
+    if [ ! -f "$HELPER_CACHE_DIR/packages" ]; then
+        missing_pkgs="yes"
+        echo "packages file: \`$HELPER_CACHE_DIR/packages' does not exist"
+    else
+        extra_pkgs=$(grep ^packages= "$MULTISTRAP_CONFIG" | cut -d= -f2 | tr  '\n' ' ')
+        for pkg in $extra_pkgs; do
+            if ! grep "^$pkg\$" "$HELPER_CACHE_DIR/packages" > /dev/null; then
+               missing_pkgs="yes"
+                echo "$pkg is missing."
+                break
             fi
-       fi
-        if [ "$missing_pkgs" == "yes" ]; then
-                do_multistrap "$target"
+        done
+        if [ "$missing_pkgs" == "no" ]; then
+            echo "done"
         fi
     fi
-
+    if [ "$missing_pkgs" == "yes" ]; then
+            do_multistrap "$target" "$HELPER_CACHE_DIR/root.tar" "$HELPER_CACHE_DIR/packages"
+    fi
 
     test "$missing_pkgs" == "no" && while [[ 1 ]]; do
         echo -n "Use the cached file [Y/n]? "
@@ -199,17 +191,17 @@ if [ -f "$CACHE_FILE" ]; then
         if [ -z "$answer" -o "$(tr [A-Z] [a-z] <<< "$answer")" = "y" ]; then
             break;
         elif [ "$(tr [A-Z] [a-z] <<< "$answer" )" = "n" ]; then
-            do_multistrap "$target"
+            do_multistrap "$target" "$HELPER_CACHE_DIR/root.tar" "$HELPER_CACHE_DIR/packages"
             break;
         fi
     done
 
 else
     echo "not found"
-    do_multistrap "$target"
+    do_multistrap "$target" "$HELPER_CACHE_DIR/root.tar" "$HELPER_CACHE_DIR/packages"
 fi
 
-tar xf "$HELPER_CACHE_FILE" -C "$target"
+tar xf "$HELPER_CACHE_DIR/root.tar" -C "$target"
 
 echo -n "Configuring the helper image..."
 echo snf-image-helper > "$target/etc/hostname"