3 # Copyright (C) 2011 GRNET S.A.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 # Save stderr to a file
27 add_cleanup rm "$stderr"
28 exec 2> >(tee -a "$stderr" >&2)
30 monitor_pipe=$(mktemp -u)
31 mkfifo -m 600 "$monitor_pipe"
32 add_cleanup rm -f "$monitor_pipe"
34 if [ -n "$PROGRESS_MONITOR" ]; then
35 { cat "$monitor_pipe" | tee >( $PROGRESS_MONITOR ) ; } &
40 # Create file descriptor to monitor_pipe
41 eval "exec ${MONITOR_FD}>${monitor_pipe}"
42 add_cleanup close_fd ${MONITOR_FD}
44 trap "cleanup $(printf "%q" "$stderr")" EXIT
50 image_file="$IMAGE_DIR/$(echo "$IMAGE_NAME" | sed 's/^file://').$IMAGE_TYPE"
51 if [ ! -e "$image_file" ]; then
52 log_error "Image file \`$image_file' does not exit."
55 image_size="$(stat -L -c %s "$image_file")"
58 image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null"
59 image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
62 cmd_args="--db $(printf "%q" "${PITHOS_DB}") \
63 --data $(printf "%q" "${PITHOS_DATA}") \
64 $(printf "%q" "${IMAGE_NAME}")"
65 image_cmd="./pithcat $cmd_args"
66 image_size=$(./pithcat -s $cmd_args)
70 # If the target device is not a real block device we'll first losetup it.
71 # This is needed for file disks.
72 if [ ! -b "$blockdev" ]; then
73 original_blockdev="$blockdev"
74 blockdev=$($LOSETUP -sf "$blockdev")
75 add_cleanup $LOSETUP -d "$blockdev"
81 format_disk0 "$blockdev" "$IMAGE_TYPE"
84 $INSTALL_MBR -p 1 -i n "$blockdev"
86 target="$(map_disk0 "$blockdev")-1" #the root device
87 add_cleanup unmap_disk0 "$blockdev"
88 snf_export_PROPERTY_ROOT_PARTITION=1
89 if [ "$IMAGE_TYPE" = "ntfsdump" ]; then
90 snf_export_PROPERTY_OSFAMILY="windows"
92 snf_export_PROPERTY_OSFAMILY="linux"
99 log_error "Unknown Image format: \`$IMAGE_TYPE'"
104 monitor="./copy-monitor.py -o $(printf "%q" "$monitor_pipe") \
105 -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
106 if [ "$BACKEND_TYPE" = "local" ]; then
107 # dd the dump to its new home :-)
108 # Deploying an image file on a target block device is a streaming copy
109 # operation. Enable the direct I/O flag on the output fd to avoid polluting
110 # the host cache with useless data.
111 $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
113 $image_cmd | $monitor dd bs=4M of="$target" oflag=direct
116 # Create a floppy image
117 floppy=$(mktemp --tmpdir floppy.XXXXXX)
118 add_cleanup rm "$floppy"
120 snf_export_DEV=/dev/vda
121 snf_export_TYPE="$IMG_FORMAT"
122 snf_export_PASSWORD="$IMG_PASSWD"
123 snf_export_HOSTNAME="$instance"
124 if [ -n "$IMG_PROPERTIES" ]; then
125 snf_export_PROPERTIES="$IMG_PROPERTIES"
127 if [ -n "$IMG_PERSONALITY" ]; then
128 snf_export_PERSONALITY="$IMG_PERSONALITY"
131 create_floppy "$floppy"
133 # Invoke the helper vm to do the dirty job...
134 jail=$(mktemp -d --tmpdir tmpfsXXXXXXX)
135 add_cleanup rmdir "$jail"
137 mount tmpfs -t tmpfs "$jail" -o size=50M
138 add_cleanup umount "$jail"
140 result_file=$(mktemp --tmpdir="$jail" result.XXXXXX)
141 add_cleanup rm "$result_file"
143 snapshot=$(mktemp --tmpdir="$jail" helperXXXXXX.img)
144 add_cleanup rm "$snapshot"
146 "$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
148 echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..."
149 $TIMELIMIT -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
150 kvm -runas "$HELPER_USER" -drive file="$snapshot" \
151 -drive file="$blockdev",format=raw,if=virtio,cache=none \
152 -boot c -serial stdio -serial file:"$result_file" \
153 -fda "$floppy" -vga none -nographic -parallel none -monitor null \
154 -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
155 -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \
156 2>&1 | sed 's|^|HELPER: |g'
157 echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished."
159 if [ $? -ne 0 ]; then
160 if [ $? -gt 128 ]; then
161 log_error "Helper was terminated. Did not finish on time."
166 # Read the first line. This will remove \r and \n chars
167 result=$(sed 's|\r||g' "$result_file" | head -1)
169 if [ "x$result" != "xSUCCESS" ]; then
170 log_error "Helper VM returned error:"
181 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :