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
27 if [ "$IMAGE_TYPE" = custom ]; then
30 $($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
32 image_file="$IMAGE_DIR/$IMAGE_NAME.$IMAGE_TYPE"
33 image_size="$(stat -L -c %s "$image_file")"
36 if [ ! -e "$image_file" ]; then
37 log_error "Image file \`$image_file' does not exit."
41 monitor="" #Empty if progress monitor support is disabled
42 if [ "$progress_monitor_support" = "yes" ]; then
43 monitor="$PROGRESS_MONITOR \
44 -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
47 # If the target device is not a real block device we'll first losetup it.
48 # This is needed for file disks.
49 if [ ! -b "$blockdev" ]; then
50 original_blockdev="$blockdev"
51 blockdev=$($LOSETUP -sf "$blockdev")
52 add_cleanup $LOSETUP -d "$blockdev"
58 format_disk0 "$blockdev" "$IMAGE_TYPE"
61 $INSTALL_MBR -p 1 -i n "$blockdev"
63 target="$(map_disk0 "$blockdev")-1" #the root device
64 add_cleanup unmap_disk0 "$blockdev"
65 snf_export_PROPERTY_ROOT_PARTITION=1
66 if [ "$IMAGE_TYPE" = "ntfsdump" ]; then
67 snf_export_PROPERTY_OSFAMILY="windows"
69 snf_export_PROPERTY_OSFAMILY="linux"
77 if [ "$IMAGE_TYPE" = "custom" ]; then
78 "$CURL" "$IMAGE_NAME" 2>/dev/null |\
79 $monitor dd bs=4M of="$target" oflag=direct
81 # dd the dump to its new home :-)
82 # Deploying an image file on a target block device is a streaming copy
83 # operation. Enable the direct I/O flag on the output fd to avoid polluting
84 # the host cache with useless data.
85 $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
88 # Create a floppy image
89 floppy=$(mktemp --tmpdir floppy.XXXXXX)
90 add_cleanup rm "$floppy"
92 snf_export_DEV=/dev/vda
93 snf_export_TYPE="$IMG_FORMAT"
94 snf_export_PASSWORD="$IMG_PASSWD"
95 snf_export_HOSTNAME="$instance"
96 if [ -n "$IMG_PROPERTIES" ]; then
97 snf_export_PROPERTIES="$IMG_PROPERTIES"
99 if [ -n "$IMG_PERSONALITY" ]; then
100 snf_export_PERSONALITY="$IMG_PERSONALITY"
103 create_floppy "$floppy"
105 # Invoke the helper vm to do the dirty job...
106 result_file=$(mktemp --tmpdir result.XXXXXX)
107 add_cleanup rm "$result_file"
109 snapshot=$(mktemp --tmpdir helperXXXXXX.img)
110 add_cleanup rm "$snapshot"
112 "$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
114 echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..."
115 $TIMELIMIT -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
116 kvm -runas "$HELPER_USER" -drive file="$snapshot" \
117 -drive file="$blockdev",format=raw,if=virtio,cache=none \
118 -boot c -serial stdio -serial file:"$result_file" \
119 -fda "$floppy" -vga none -nographic -parallel none -monitor null \
120 -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
121 -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \
122 2>&1 | sed 's|^|HELPER: |g'
123 echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished."
125 if [ $? -ne 0 ]; then
126 if [ $? -gt 128 ]; then
127 log_error "Helper was terminated. Did not finish on time."
132 # Read the first line. This will remove \r and \n chars
133 result=$(sed 's|\r||g' "$result_file" | head -1)
135 if [ "x$result" != "xSUCCESS" ]; then
136 log_error "Helper VM returned error"
146 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :