From 53a5929f08c09a228e703f932127150cb52d2774 Mon Sep 17 00:00:00 2001 From: Nikos Skalkotos Date: Thu, 4 Oct 2012 18:44:12 +0300 Subject: [PATCH] Use a seperated dir for update-helper cache files By default this directory is $(LOCALSTATEDIR)/cache/snf-image/helper --- snf-image-host/Makefile.am | 1 + snf-image-host/common.sh.in | 19 ++++---- snf-image-host/configure.ac | 10 +++++ snf-image-host/defaults | 10 ++--- snf-image-host/snf-image-update-helper.in | 68 +++++++++++++---------------- 5 files changed, 55 insertions(+), 53 deletions(-) diff --git a/snf-image-host/Makefile.am b/snf-image-host/Makefile.am index 51a9b4a..24ef529 100644 --- a/snf-image-host/Makefile.am +++ b/snf-image-host/Makefile.am @@ -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' \ diff --git a/snf-image-host/common.sh.in b/snf-image-host/common.sh.in index 8b15239..6487006 100644 --- a/snf-image-host/common.sh.in +++ b/snf-image-host/common.sh.in @@ -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"} diff --git a/snf-image-host/configure.ac b/snf-image-host/configure.ac index 7df7605..e7fd429 100644 --- a/snf-image-host/configure.ac +++ b/snf-image-host/configure.ac @@ -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], diff --git a/snf-image-host/defaults b/snf-image-host/defaults index a8251aa..5dc246a 100644 --- a/snf-image-host/defaults +++ b/snf-image-host/defaults @@ -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" @@ -43,13 +46,6 @@ # 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" diff --git a/snf-image-host/snf-image-update-helper.in b/snf-image-host/snf-image-update-helper.in index 2f15f0e..cb6e395 100644 --- a/snf-image-host/snf-image-update-helper.in +++ b/snf-image-host/snf-image-update-helper.in @@ -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" -- 1.7.10.4