3 # Copyright 2011 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 # The views and conclusions contained in the software and documentation are
29 # those of the authors and should not be interpreted as representing official
30 # policies, either expressed or implied, of GRNET S.A.
41 image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.extdump";;
43 image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.ntfsdump";;
45 image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.diskdump";;
47 log_error "Unknown image type: \`$IMAGE_TYPE'.";
51 if [ ! -e "$image_file" ]; then
52 log_error "Image file \`$image_file' does not exit."
56 monitor="" #Empty if progress monitor support is disabled
57 if [ "$progress_monitor_support" = "yes" ]; then
58 image_size="$(stat -L -c %s "$image_file")"
59 monitor="$(printf "%q" "$PROGRESS_MONITOR") \
60 -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
63 # If the target device is not a real block device we'll first losetup it.
64 # This is needed for file disks.
65 if [ ! -b "$blockdev" ]; then
66 original_blockdev="$blockdev"
67 blockdev=$("$LOSETUP" -sf "$blockdev")
68 add_cleanup "$LOSETUP" -d "$blockdev"
73 format_disk0 "$blockdev" "$IMAGE_TYPE"
75 target="$(map_disk0 "$blockdev")-1" #the root device
76 add_cleanup unmap_disk0 "$blockdev"
83 # dd the dump to its new home :-)
84 # Deploying an image file on a target block device is a streaming copy
85 # operation. Enable the direct I/O flag on the output fd to avoid polluting the
86 # host cache with useless data.
87 $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
89 # Create a floppy image
90 floppy=$(mktemp --tmpdir floppy.XXXXXX)
91 add_cleanup rm "$floppy"
93 snf_export_DEV=/dev/vda
94 snf_export_TYPE="$IMG_FORMAT"
95 snf_export_PASSWORD="$IMG_PASSWD"
96 snf_export_HOSTNAME="$instance"
97 if [ -n "$IMG_PERSONALITY" ]; then
98 snf_export_PERSONALITY="$IMG_PERSONALITY"
101 create_floppy "$floppy"
103 # Invoke the helper vm to do the dirty job...
104 result_file=$(mktemp --tmpdir result.XXXXXX)
105 add_cleanup rm "$result_file"
107 snapshot=$(mktemp --tmpdir helperXXXXXX.img)
108 add_cleanup rm "$snapshot"
110 "$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
112 echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..."
113 "$TIMELIMIT" -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
114 kvm -runas "$HELPER_USER" -drive file="$snapshot" \
115 -drive file="$blockdev",format=raw,if=virtio,cache=none \
116 -boot c -serial stdio -serial file:"$result_file" \
117 -fda "$floppy" -vga none -nographic -parallel none -monitor null \
118 -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
119 -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \
120 2>&1 | sed 's|^|HELPER: |g'
121 echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished."
123 if [ $? -ne 0 ]; then
124 if [ $? -gt 128 ]; then
125 log_error "Helper was terminated. Did not finish on time."
130 # Read the first line. This will remove \r and \n chars
131 result=$(sed 's|\r||g' "$result_file" | head -1)
133 if [ "x$result" != "xSUCCESS" ]; then
134 log_error "Helper VM returned error"
139 "$INSTALL_MBR" -p 1 -i n "$blockdev"
147 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :