#!/bin/bash
-# Copyright 2011 GRNET S.A. All rights reserved.
+# Copyright (C) 2011 GRNET S.A.
#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
+# 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
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
#
-# 1. Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
#
-# 2. Redistributions in binary form must reproduce the above copyright
-# notice, this list of conditions and the following disclaimer in the
-# documentation and/or other materials provided with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-# SUCH DAMAGE.
-#
-# The views and conclusions contained in the software and documentation are
-# those of the authors and should not be interpreted as representing official
-# policies, either expressed or implied, of GRNET S.A.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
set -e
set -o pipefail
. common.sh
-ganeti_os_main
+# Save stderr to a file
+stderr=$(mktemp)
+add_cleanup rm "$stderr"
+exec 2> >(tee -a "$stderr" >&2)
+
+monitor_pipe=$(mktemp -u)
+mkfifo -m 600 "$monitor_pipe"
+add_cleanup rm -f "$monitor_pipe"
-if [ "$IMAGE_TYPE" = custom ]; then
- image_file=/dev/null
- image_size=\
- $("$CURL" -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
+if [ -n "$PROGRESS_MONITOR" ]; then
+ { sleep 1; $PROGRESS_MONITOR "$instance" < "$monitor_pipe" ; } &
+ monitor_pid="$!"
else
- image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.$IMAGE_TYPE"
- image_size="$(stat -L -c %s "$image_file")"
+ sed -u 's|^|MONITOR MSG: |g' < "$monitor_pipe" &
+ monitor_pid="$!"
fi
-if [ ! -e "$image_file" ]; then
- log_error "Image file \`$image_file' does not exit."
- exit 1
-fi
+# Create file descriptor to monitor_pipe
+exec {MONITOR_FD}>${monitor_pipe}
+add_cleanup close_fd ${MONITOR_FD}
-monitor="" #Empty if progress monitor support is disabled
-if [ "$progress_monitor_support" = "yes" ]; then
- monitor="$(printf "%q" "$PROGRESS_MONITOR") \
- -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
-fi
+# Ignore sigpipe signals. If progress monitor is dead and snf-image-host tries
+# to output something to the opened pipe, then a sigpipe will be raised. If we
+# do not catch this, the program will terminate.
+trap "" SIGPIPE
+
+trap "report_and_cleanup $(printf "%q" "$stderr") ${MONITOR_FD}" EXIT
+
+ganeti_os_main
+
+case $BACKEND_TYPE in
+ local)
+ image_file="$IMAGE_DIR/$(echo "$IMAGE_NAME" | sed 's/^file://').$IMAGE_TYPE"
+ if [ ! -e "$image_file" ]; then
+ log_error "Image file \`$image_file' does not exit."
+ exit 1
+ fi
+ image_size="$(stat -L -c %s "$image_file")"
+ ;;
+ network)
+ image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null"
+ image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
+ ;;
+ pithos)
+ cmd_args="--db $(printf "%q" "${PITHOS_DB}") \
+ --data $(printf "%q" "${PITHOS_DATA}") \
+ $(printf "%q" "${IMAGE_NAME}")"
+ image_cmd="./pithcat $cmd_args"
+ image_size=$(./pithcat -s $cmd_args)
+ ;;
+esac
# If the target device is not a real block device we'll first losetup it.
# This is needed for file disks.
if [ ! -b "$blockdev" ]; then
original_blockdev="$blockdev"
- blockdev=$("$LOSETUP" -sf "$blockdev")
- add_cleanup "$LOSETUP" -d "$blockdev"
+ blockdev=$($LOSETUP -sf "$blockdev")
+ add_cleanup $LOSETUP -d "$blockdev"
fi
case "$IMAGE_TYPE" in
format_disk0 "$blockdev" "$IMAGE_TYPE"
# Install a new MBR
- "$INSTALL_MBR" -p 1 -i n "$blockdev"
+ $INSTALL_MBR -p 1 -i n "$blockdev"
target="$(map_disk0 "$blockdev")-1" #the root device
add_cleanup unmap_disk0 "$blockdev"
snf_export_PROPERTY_OSFAMILY="linux"
fi
;;
- diskdump|custom)
+ diskdump)
target="$blockdev"
;;
+ *)
+ log_error "Unknown Image format: \`$IMAGE_TYPE'"
+ exit 1
+ ;;
esac
-if [ "$IMAGE_TYPE" = "custom" ]; then
- "$CURL" "$IMAGE_NAME" 2>/dev/null |\
- $monitor dd bs=4M of="$target" oflag=direct
-else
+monitor="./copy-monitor.py -o $MONITOR_FD -r $image_size"
+if [ "$BACKEND_TYPE" = "local" ]; then
# dd the dump to its new home :-)
# Deploying an image file on a target block device is a streaming copy
# operation. Enable the direct I/O flag on the output fd to avoid polluting
# the host cache with useless data.
$monitor dd bs=4M if="$image_file" of="$target" oflag=direct
+else
+ $image_cmd | $monitor dd bs=4M of="$target" oflag=direct
fi
# Create a floppy image
snf_export_TYPE="$IMG_FORMAT"
snf_export_PASSWORD="$IMG_PASSWD"
snf_export_HOSTNAME="$instance"
-snf_export_PROPERTIES="$IMG_PROPERTIES"
+if [ -n "$IMG_PROPERTIES" ]; then
+ snf_export_PROPERTIES="$IMG_PROPERTIES"
+fi
if [ -n "$IMG_PERSONALITY" ]; then
snf_export_PERSONALITY="$IMG_PERSONALITY"
fi
create_floppy "$floppy"
# Invoke the helper vm to do the dirty job...
-result_file=$(mktemp --tmpdir result.XXXXXX)
+jail=$(mktemp -d --tmpdir tmpfsXXXXXXX)
+add_cleanup rmdir "$jail"
+
+mount tmpfs -t tmpfs "$jail" -o size=50M
+add_cleanup umount "$jail"
+
+result_file=$(mktemp --tmpdir="$jail" result.XXXXXX)
add_cleanup rm "$result_file"
-snapshot=$(mktemp --tmpdir helperXXXXXX.img)
+snapshot=$(mktemp --tmpdir="$jail" helperXXXXXX.img)
add_cleanup rm "$snapshot"
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..."
-"$TIMELIMIT" -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
+set +e
+$TIMEOUT -k "$HELPER_HARD_TIMEOUT" "$HELPER_SOFT_TIMEOUT" \
kvm -runas "$HELPER_USER" -drive file="$snapshot" \
-drive file="$blockdev",format=raw,if=virtio,cache=none \
- -boot c -serial stdio -serial file:"$result_file" \
+ -boot c -serial stdio -serial "file:$(printf "%q" "$result_file")" \
+ -serial file:>(./helper-monitor.py ${MONITOR_FD}) \
-fda "$floppy" -vga none -nographic -parallel none -monitor null \
-kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
-append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \
- 2>&1 | sed 's|^|HELPER: |g'
-echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished."
-
-if [ $? -ne 0 ]; then
- if [ $? -gt 128 ]; then
- log_error "Helper was terminated. Did not finish on time."
+ 2>&1 | sed -u 's|^|HELPER: |g'
+rc=$?
+set -e
+if [ $rc -ne 0 ]; then
+ if [ $rc -eq 124 ]; then
+ log_error "Helper VM was terminated. Did not finish on time."
+ elif [ $rc -eq 137 ]; then # (128 + SIGKILL)
+ log_error "Helper VM was killed. Did not finish on time."
+ elif [ $rc -eq 141 ]; then # (128 + SIGPIPE)
+ log_error "Helper was killed by a SIGPIPE signal."
+ log_error "Maybe progress monitor has died unexpectedly."
+ elif [ $rc -eq 125 ]; then
+ log_error "timeout did not manage to run."
+ else
+ log_error "KVM returned an error (return code $rc)."
fi
exit 1
+else
+ echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished."
fi
# Read the first line. This will remove \r and \n chars
result=$(sed 's|\r||g' "$result_file" | head -1)
if [ "x$result" != "xSUCCESS" ]; then
- log_error "Helper VM returned error"
+ log_error "Helper VM did not return SUCCESS"
exit 1
fi