root / snf-image-host / create @ 44038aae
History | View | Annotate | Download (6.2 kB)
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 |
{ sleep 1; $PROGRESS_MONITOR "$instance" < "$monitor_pipe" ; } & |
36 |
monitor_pid="$!" |
37 |
else |
38 |
cat "$monitor_pipe" & |
39 |
monitor_pid="$!" |
40 |
fi |
41 |
|
42 |
# Create file descriptor to monitor_pipe |
43 |
exec {MONITOR_FD}>${monitor_pipe} |
44 |
add_cleanup close_fd ${MONITOR_FD} |
45 |
|
46 |
# Ignore sigpipe signals. If progress monitor is dead and snf-image-host tries |
47 |
# to output something to the opened pipe, then a sigpipe will be raised. If we |
48 |
# do not catch this, the program will terminate. |
49 |
trap "" SIGPIPE |
50 |
|
51 |
trap "report_and_cleanup $(printf "%q" "$stderr") ${MONITOR_FD}" EXIT |
52 |
|
53 |
ganeti_os_main |
54 |
|
55 |
case $BACKEND_TYPE in |
56 |
local) |
57 |
image_file="$IMAGE_DIR/$(echo "$IMAGE_NAME" | sed 's/^file://').$IMAGE_TYPE" |
58 |
if [ ! -e "$image_file" ]; then |
59 |
log_error "Image file \`$image_file' does not exit." |
60 |
exit 1 |
61 |
fi |
62 |
image_size="$(stat -L -c %s "$image_file")" |
63 |
;; |
64 |
network) |
65 |
image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null" |
66 |
image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2) |
67 |
;; |
68 |
pithos) |
69 |
cmd_args="--db $(printf "%q" "${PITHOS_DB}") \ |
70 |
--data $(printf "%q" "${PITHOS_DATA}") \ |
71 |
$(printf "%q" "${IMAGE_NAME}")" |
72 |
image_cmd="./pithcat $cmd_args" |
73 |
image_size=$(./pithcat -s $cmd_args) |
74 |
;; |
75 |
esac |
76 |
|
77 |
# If the target device is not a real block device we'll first losetup it. |
78 |
# This is needed for file disks. |
79 |
if [ ! -b "$blockdev" ]; then |
80 |
original_blockdev="$blockdev" |
81 |
blockdev=$($LOSETUP -sf "$blockdev") |
82 |
add_cleanup $LOSETUP -d "$blockdev" |
83 |
fi |
84 |
|
85 |
case "$IMAGE_TYPE" in |
86 |
ntfsdump|extdump) |
87 |
# Create partitions |
88 |
format_disk0 "$blockdev" "$IMAGE_TYPE" |
89 |
|
90 |
# Install a new MBR |
91 |
$INSTALL_MBR -p 1 -i n "$blockdev" |
92 |
|
93 |
target="$(map_disk0 "$blockdev")-1" #the root device |
94 |
add_cleanup unmap_disk0 "$blockdev" |
95 |
snf_export_PROPERTY_ROOT_PARTITION=1 |
96 |
if [ "$IMAGE_TYPE" = "ntfsdump" ]; then |
97 |
snf_export_PROPERTY_OSFAMILY="windows" |
98 |
else |
99 |
snf_export_PROPERTY_OSFAMILY="linux" |
100 |
fi |
101 |
;; |
102 |
diskdump) |
103 |
target="$blockdev" |
104 |
;; |
105 |
*) |
106 |
log_error "Unknown Image format: \`$IMAGE_TYPE'" |
107 |
exit 1 |
108 |
;; |
109 |
esac |
110 |
|
111 |
monitor="./copy-monitor.py -o $MONITOR_FD -r $image_size" |
112 |
if [ "$BACKEND_TYPE" = "local" ]; then |
113 |
# dd the dump to its new home :-) |
114 |
# Deploying an image file on a target block device is a streaming copy |
115 |
# operation. Enable the direct I/O flag on the output fd to avoid polluting |
116 |
# the host cache with useless data. |
117 |
$monitor dd bs=4M if="$image_file" of="$target" oflag=direct |
118 |
else |
119 |
$image_cmd | $monitor dd bs=4M of="$target" oflag=direct |
120 |
fi |
121 |
|
122 |
# Create a floppy image |
123 |
floppy=$(mktemp --tmpdir floppy.XXXXXX) |
124 |
add_cleanup rm "$floppy" |
125 |
|
126 |
snf_export_DEV=/dev/vda |
127 |
snf_export_TYPE="$IMG_FORMAT" |
128 |
snf_export_PASSWORD="$IMG_PASSWD" |
129 |
snf_export_HOSTNAME="$instance" |
130 |
if [ -n "$IMG_PROPERTIES" ]; then |
131 |
snf_export_PROPERTIES="$IMG_PROPERTIES" |
132 |
fi |
133 |
if [ -n "$IMG_PERSONALITY" ]; then |
134 |
snf_export_PERSONALITY="$IMG_PERSONALITY" |
135 |
fi |
136 |
|
137 |
create_floppy "$floppy" |
138 |
|
139 |
# Invoke the helper vm to do the dirty job... |
140 |
jail=$(mktemp -d --tmpdir tmpfsXXXXXXX) |
141 |
add_cleanup rmdir "$jail" |
142 |
|
143 |
mount tmpfs -t tmpfs "$jail" -o size=50M |
144 |
add_cleanup umount "$jail" |
145 |
|
146 |
result_file=$(mktemp --tmpdir="$jail" result.XXXXXX) |
147 |
add_cleanup rm "$result_file" |
148 |
|
149 |
snapshot=$(mktemp --tmpdir="$jail" helperXXXXXX.img) |
150 |
add_cleanup rm "$snapshot" |
151 |
|
152 |
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot" |
153 |
|
154 |
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..." |
155 |
set +e |
156 |
$TIMEOUT -k "$HELPER_HARD_TIMEOUT" "$HELPER_SOFT_TIMEOUT" \ |
157 |
kvm -runas "$HELPER_USER" -drive file="$snapshot" \ |
158 |
-drive file="$blockdev",format=raw,if=virtio,cache=none \ |
159 |
-boot c -serial stdio -serial "file:$(printf "%q" "$result_file")" \ |
160 |
-serial "file:$(printf "%q" "$monitor_pipe")" \ |
161 |
-fda "$floppy" -vga none -nographic -parallel none -monitor null \ |
162 |
-kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \ |
163 |
-append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \ |
164 |
2>&1 | sed 's|^|HELPER: |g' |
165 |
rc=$? |
166 |
set -e |
167 |
if [ $rc -ne 0 ]; then |
168 |
if [ $rc -eq 124 ]; then |
169 |
log_error "Helper VM was terminated. Did not finish on time." |
170 |
elif [ $rc -eq 137 ]; then # (128 + SIGKILL) |
171 |
log_error "Helper VM was killed. Did not finish on time." |
172 |
elif [ $rc -eq 141 ]; then # (128 + SIGPIPE) |
173 |
log_error "Helper was killed by a SIGPIPE signal." |
174 |
log_error "Maybe progress monitor has died unexpectedly." |
175 |
elif [ $rc -eq 125 ]; then |
176 |
log_error "timeout did not manage to run." |
177 |
else |
178 |
log_error "KVM returned an error (return code $rc)." |
179 |
fi |
180 |
exit 1 |
181 |
else |
182 |
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished." |
183 |
fi |
184 |
|
185 |
# Do not report errors after this. If the result is not "SUCCESS" then the |
186 |
# helper VM should have reported the error. |
187 |
trap cleanup EXIT |
188 |
|
189 |
# Read the first line. This will remove \r and \n chars |
190 |
result=$(sed 's|\r||g' "$result_file" | head -1) |
191 |
|
192 |
if [ "x$result" != "xSUCCESS" ]; then |
193 |
log_error "Helper VM returned error:" |
194 |
log_error "$result" |
195 |
exit 1 |
196 |
fi |
197 |
|
198 |
# Execute cleanups |
199 |
cleanup |
200 |
trap - EXIT |
201 |
|
202 |
exit 0 |
203 |
|
204 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |