X-Git-Url: https://code.grnet.gr/git/snf-image/blobdiff_plain/faff1fc913c9591d5e7691fcd6fb9727afb87749..23f62254e79ad9f53bbae981e5b308e952891125:/snf-image-host/create diff --git a/snf-image-host/create b/snf-image-host/create index 844ced1..9fa084b 100755 --- a/snf-image-host/create +++ b/snf-image-host/create @@ -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 @@ -24,10 +24,12 @@ set -o pipefail ganeti_os_main -# Save stderr to a file -stderr=$(mktemp) -add_cleanup rm "$stderr" -exec 2> >(tee -a "$stderr" >&2) +if [ "$IMAGE_DEBUG" = "yes" ]; then + PS4='$(date "+%s.%N ($LINENO) + ")' + set -x +elif [ "$IMAGE_DEBUG" != "no" ]; then + log_warning "Unsupported IMAGE_DEBUG value: \`$IMAGE_DEBUG'" +fi monitor_pipe=$(mktemp -u) mkfifo -m 600 "$monitor_pipe" @@ -37,7 +39,7 @@ if [ -n "$PROGRESS_MONITOR" ]; then { sleep 1; $PROGRESS_MONITOR "$instance" < "$monitor_pipe" ; } & monitor_pid="$!" else - sed -u 's|^|MONITOR MSG: |g' < "$monitor_pipe" & + sed -u 's|^|[MONITOR] |g' < "$monitor_pipe" & monitor_pid="$!" fi @@ -45,30 +47,71 @@ fi exec {MONITOR_FD}>${monitor_pipe} add_cleanup close_fd ${MONITOR_FD} -# 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. +# Ignore sigpipe signals. If progress monitor is dead and snf-image 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")" EXIT +trap report_and_cleanup EXIT + +echo "Processing image with ID: \`$IMG_ID' and type: \`$IMAGE_TYPE'" >&2 case $BACKEND_TYPE in local) - image_file="$IMAGE_DIR/$(echo "$IMAGE_NAME" | sed 's/^file://').$IMAGE_TYPE" + if [[ "$IMAGE_NAME" =~ ^local:// ]]; then + IMAGE_NAME="${IMAGE_NAME:8}" + elif [[ "$IMAGE_NAME" =~ ^file:// ]]; then + IMAGE_NAME="${IMAGE_NAME:7}" + log_warning "The file:// back-end identifier is deprecated and" \ + "will be removed in the future. Use local:// instead." + fi + + canonical_image_dir="$(canonicalize "$IMAGE_DIR")" + if [ ! -d "$canonical_image_dir" ]; then + log_error "The IMAGE_DIR directory: \`$IMAGE_DIR' does not exist." + report_error "Unable to retrieve image file." + fi + + image_file="$IMAGE_DIR/$IMAGE_NAME" if [ ! -e "$image_file" ]; then - log_error "Image file \`$image_file' does not exit." + if [ -e "$image_file.$IMAGE_TYPE" ] ; then + image_file="$image_file.$IMAGE_TYPE" + log_warning "The \`.$IMAGE_TYPE' extension is missing from" \ + "the local back-end id. This id form is deprecated and" \ + " will be remove in the future." + else + log_error "Image file \`$image_file' does not exist." + report_error "Unable to retrieve image file." + exit 1 + fi + fi + + canonical_image_file="$(canonicalize "$image_file")" + + if [[ "$canonical_image_file" != "$canonical_image_dir"* ]]; then + log_error "Image ID points to a file outside the image directory: \`$IMAGE_DIR'" + report_error "Invalid image ID" exit 1 fi + image_size="$(stat -L -c %s "$image_file")" ;; + null) + image_file=/dev/null + image_size=0 + # Treat it as local file from now on... + BACKEND_TYPE="local" + ;; network) - image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null" + image_cmd="$CURL $(printf "%q" "$IMAGE_NAME")" 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}")" + # For security reasons pass the database url to pithcat as an + # environmental variable. + export PITHCAT_INPUT_DB="$PITHOS_DB" + export PITHCAT_INPUT_DATA="$PITHOS_DATA" + cmd_args="$(printf "%q" "${IMAGE_NAME}")" image_cmd="./pithcat $cmd_args" image_size=$(./pithcat -s $cmd_args) ;; @@ -104,11 +147,12 @@ case "$IMAGE_TYPE" in ;; *) log_error "Unknown Image format: \`$IMAGE_TYPE'" + report_error "Unknown Image Format" exit 1 ;; esac -log_info "Starting image copy..." +report_info "Starting image copy..." monitor="./copy-monitor.py -o $MONITOR_FD -r $image_size" if [ "$BACKEND_TYPE" = "local" ]; then # dd the dump to its new home :-) @@ -119,13 +163,12 @@ if [ "$BACKEND_TYPE" = "local" ]; then else $image_cmd | $monitor dd bs=4M of="$target" oflag=direct fi -log_info "Image copy finished..." +report_info "Image copy finished." # Create a floppy image floppy=$(mktemp --tmpdir floppy.XXXXXX) add_cleanup rm "$floppy" -snf_export_DEV=/dev/vda snf_export_TYPE="$IMG_FORMAT" snf_export_PASSWORD="$IMG_PASSWD" snf_export_HOSTNAME="$instance" @@ -136,64 +179,13 @@ if [ -n "$IMG_PERSONALITY" ]; then snf_export_PERSONALITY="$IMG_PERSONALITY" fi -create_floppy "$floppy" +snf_export_DEV=$(get_img_dev) -# Invoke the helper vm to do the dirty job... -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="$jail" helperXXXXXX.img) -add_cleanup rm "$snapshot" - -"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot" - -echo -n "$(date +%Y:%m:%d-%H:%M:%S.%N) " >&2 -log_info "Starting customization VM..." -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:$(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 -u 's|^|HELPER: |g' -rc=$? -set -e -if [ $rc -ne 0 ]; then - if [ $rc -eq 124 ]; then - log_error "Image customization was terminated. Did not finish on time." - elif [ $rc -eq 137 ]; then # (128 + SIGKILL) - log_error "Image customization was killed. Did not finish on time." - elif [ $rc -eq 141 ]; then # (128 + SIGPIPE) - log_error "Image customization was terminated by a SIGPIPE." - log_error "Maybe progress monitor has died unexpectedly." - elif [ $rc -eq 125 ]; then - log_error "Internal Error. Image customization could not start." - log_error "timeout did not manage to run." - else - log_error "Image customization died unexpectedly (return code $rc)." - fi - exit 1 -else - echo -n "$(date +%Y:%m:%d-%H:%M:%S.%N)" >&2 - log_info "Customization VM finished." -fi +create_floppy "$floppy" -# Read the first line. This will remove \r and \n chars -result=$(sed 's|\r||g' "$result_file" | head -1) +launch_helper "$blockdev" "$floppy" -if [ "x$result" != "xSUCCESS" ]; then - log_error "Image customization failed." - exit 1 -fi +report_info "Image customization finished successfully." # Execute cleanups cleanup