Add monitoring event for errors in create
[snf-image] / snf-image-host / create
1 #!/bin/bash
2
3 # Copyright (C) 2011 GRNET S.A. 
4 #
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.
9 #
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.
14 #
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
18 # 02110-1301, USA.
19
20 set -e
21 set -o pipefail
22
23 . common.sh
24
25 # Save stderr to a file
26 stderr=$(mktemp)
27 add_cleanup rm "$stderr"
28 exec 2> >(tee -a "$stderr" >&2)
29
30 monitor_pipe=$(mktemp -u)
31 mkfifo -m 600 "$monitor_pipe"
32 add_cleanup rm -f "$monitor_pipe"
33
34 if [ -n "$PROGRESS_MONITOR" ]; then
35     { cat "$monitor_pipe" | tee >( $PROGRESS_MONITOR ) ; } &
36 else
37     cat "$monitor_pipe" &
38 fi
39
40 # Create file descriptor to monitor_pipe
41 eval "exec ${MONITOR_FD}>${monitor_pipe}"
42 add_cleanup  close_fd ${MONITOR_FD}
43
44 trap "cleanup $(printf "%q" "$stderr")" EXIT
45
46 ganeti_os_main
47
48 case $BACKEND_TYPE in
49     local)
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."
53             exit 1
54         fi
55         image_size="$(stat -L -c %s "$image_file")"
56         ;;
57     network)
58         image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null"
59         image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
60         ;;
61     pithos)
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)
67         ;;
68 esac
69
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"
76 fi
77
78 case "$IMAGE_TYPE" in
79     ntfsdump|extdump)
80         # Create partitions
81         format_disk0 "$blockdev" "$IMAGE_TYPE"
82
83         # Install a new MBR
84         $INSTALL_MBR -p 1 -i n "$blockdev"
85
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"
91         else
92             snf_export_PROPERTY_OSFAMILY="linux"
93         fi
94         ;;
95     diskdump)
96         target="$blockdev"
97         ;;
98     *)
99         log_error "Unknown Image format: \`$IMAGE_TYPE'"
100         exit 1
101         ;;
102 esac
103
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
112 else
113     $image_cmd | $monitor dd bs=4M of="$target" oflag=direct
114 fi
115
116 # Create a floppy image
117 floppy=$(mktemp --tmpdir floppy.XXXXXX)
118 add_cleanup rm "$floppy"
119
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"
126 fi
127 if [ -n "$IMG_PERSONALITY" ]; then
128     snf_export_PERSONALITY="$IMG_PERSONALITY"
129 fi
130
131 create_floppy "$floppy"
132
133 # Invoke the helper vm to do the dirty job...
134 jail=$(mktemp -d --tmpdir tmpfsXXXXXXX)
135 add_cleanup rmdir "$jail"
136
137 mount tmpfs -t tmpfs "$jail" -o size=50M
138 add_cleanup umount "$jail"
139
140 result_file=$(mktemp --tmpdir="$jail" result.XXXXXX)
141 add_cleanup rm "$result_file"
142
143 snapshot=$(mktemp --tmpdir="$jail" helperXXXXXX.img)
144 add_cleanup rm "$snapshot"
145
146 "$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
147
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."
158
159 if [ $? -ne 0 ]; then
160     if [ $? -gt 128 ];  then
161         log_error "Helper was terminated. Did not finish on time."
162     fi
163     exit 1
164 fi
165
166 # Read the first line. This will remove \r and \n chars
167 result=$(sed 's|\r||g' "$result_file" | head -1)
168
169 if [ "x$result" != "xSUCCESS" ]; then
170     log_error "Helper VM returned error:"
171     log_error "$result"
172     exit 1
173 fi
174
175 # Execute cleanups
176 cleanup
177 trap - EXIT
178
179 exit 0
180
181 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :