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