#!/bin/bash # 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 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # 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. # # 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 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" add_cleanup rm -f "$monitor_pipe" if [ -n "$PROGRESS_MONITOR" ]; then { sleep 1; $PROGRESS_MONITOR "$instance" < "$monitor_pipe" ; } & monitor_pid="$!" else sed -u 's|^|[MONITOR] |g' < "$monitor_pipe" & monitor_pid="$!" fi # Create file descriptor to monitor_pipe exec {MONITOR_FD}>${monitor_pipe} add_cleanup close_fd ${MONITOR_FD} # 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 EXIT echo "Processing image with ID: \`$IMG_ID' and type: \`$IMAGE_TYPE'" >&2 case $BACKEND_TYPE in local) 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 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")" image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2) ;; pithos) # 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) ;; 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" fi case "$IMAGE_TYPE" in ntfsdump|extdump) # Create partitions format_disk0 "$blockdev" "$IMAGE_TYPE" # Install a new MBR $INSTALL_MBR -p 1 -i n "$blockdev" target="$(map_disk0 "$blockdev")-1" #the root device add_cleanup unmap_disk0 "$blockdev" snf_export_PROPERTY_ROOT_PARTITION=1 if [ "$IMAGE_TYPE" = "ntfsdump" ]; then snf_export_PROPERTY_OSFAMILY="windows" else snf_export_PROPERTY_OSFAMILY="linux" fi ;; diskdump) target="$blockdev" ;; *) log_error "Unknown Image format: \`$IMAGE_TYPE'" report_error "Unknown Image Format" exit 1 ;; esac 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 :-) # 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 report_info "Image copy finished." # Create a floppy image floppy=$(mktemp --tmpdir floppy.XXXXXX) add_cleanup rm "$floppy" snf_export_TYPE="$IMG_FORMAT" snf_export_PASSWORD="$IMG_PASSWD" snf_export_HOSTNAME="$instance" if [ -n "$IMG_PROPERTIES" ]; then snf_export_PROPERTIES="$IMG_PROPERTIES" fi if [ -n "$IMG_PERSONALITY" ]; then snf_export_PERSONALITY="$IMG_PERSONALITY" fi snf_export_DEV=$(get_img_dev) create_floppy "$floppy" launch_helper "$blockdev" "$floppy" report_info "Image customization finished successfully." # Execute cleanups cleanup trap - EXIT exit 0 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :