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 # Save stderr to a file
29 add_cleanup rm "$stderr"
30 exec 2> >(tee -a "$stderr" >&2)
32 monitor_pipe=$(mktemp -u)
33 mkfifo -m 600 "$monitor_pipe"
34 add_cleanup rm -f "$monitor_pipe"
36 if [ -n "$PROGRESS_MONITOR" ]; then
37 { sleep 1; $PROGRESS_MONITOR "$instance" < "$monitor_pipe" ; } &
40 sed -u 's|^|MONITOR MSG: |g' < "$monitor_pipe" &
44 # Create file descriptor to monitor_pipe
45 exec {MONITOR_FD}>${monitor_pipe}
46 add_cleanup close_fd ${MONITOR_FD}
48 # Ignore sigpipe signals. If progress monitor is dead and snf-image-host tries
49 # to output something to the opened pipe, then a sigpipe will be raised. If we
50 # do not catch this, the program will terminate.
53 trap "report_and_cleanup $(printf "%q" "$stderr")" EXIT
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."
62 image_size="$(stat -L -c %s "$image_file")"
67 # Treat it as local file from now on...
71 image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null"
72 image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
75 cmd_args="--db $(printf "%q" "${PITHOS_DB}") \
76 --data $(printf "%q" "${PITHOS_DATA}") \
77 $(printf "%q" "${IMAGE_NAME}")"
78 image_cmd="./pithcat $cmd_args"
79 image_size=$(./pithcat -s $cmd_args)
83 # If the target device is not a real block device we'll first losetup it.
84 # This is needed for file disks.
85 if [ ! -b "$blockdev" ]; then
86 original_blockdev="$blockdev"
87 blockdev=$($LOSETUP -sf "$blockdev")
88 add_cleanup $LOSETUP -d "$blockdev"
94 format_disk0 "$blockdev" "$IMAGE_TYPE"
97 $INSTALL_MBR -p 1 -i n "$blockdev"
99 target="$(map_disk0 "$blockdev")-1" #the root device
100 add_cleanup unmap_disk0 "$blockdev"
101 snf_export_PROPERTY_ROOT_PARTITION=1
102 if [ "$IMAGE_TYPE" = "ntfsdump" ]; then
103 snf_export_PROPERTY_OSFAMILY="windows"
105 snf_export_PROPERTY_OSFAMILY="linux"
112 log_error "Unknown Image format: \`$IMAGE_TYPE'"
117 log_info "Starting image copy..."
118 monitor="./copy-monitor.py -o $MONITOR_FD -r $image_size"
119 if [ "$BACKEND_TYPE" = "local" ]; then
120 # dd the dump to its new home :-)
121 # Deploying an image file on a target block device is a streaming copy
122 # operation. Enable the direct I/O flag on the output fd to avoid polluting
123 # the host cache with useless data.
124 $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
126 $image_cmd | $monitor dd bs=4M of="$target" oflag=direct
128 log_info "Image copy finished."
130 # Create a floppy image
131 floppy=$(mktemp --tmpdir floppy.XXXXXX)
132 add_cleanup rm "$floppy"
134 snf_export_TYPE="$IMG_FORMAT"
135 snf_export_PASSWORD="$IMG_PASSWD"
136 snf_export_HOSTNAME="$instance"
137 if [ -n "$IMG_PROPERTIES" ]; then
138 snf_export_PROPERTIES="$IMG_PROPERTIES"
140 if [ -n "$IMG_PERSONALITY" ]; then
141 snf_export_PERSONALITY="$IMG_PERSONALITY"
144 create_floppy "$floppy"
146 # Invoke the helper vm to do the dirty job...
147 jail=$(mktemp -d --tmpdir tmpfsXXXXXXX)
148 add_cleanup rmdir "$jail"
150 mount tmpfs -t tmpfs "$jail" -o size=1G
151 add_cleanup umount "$jail"
153 result_file=$(mktemp --tmpdir="$jail" result.XXXXXX)
154 add_cleanup rm "$result_file"
156 snapshot=$(mktemp --tmpdir="$jail" helperXXXXXX.img)
157 add_cleanup rm "$snapshot"
161 echo -n "$(date +%Y:%m:%d-%H:%M:%S.%N) " >&2
162 log_info "Starting customization VM..."
169 if [ $rc -ne 0 ]; then
170 if [ $rc -eq 124 ]; then
171 log_error "Image customization was terminated. Did not finish on time."
172 elif [ $rc -eq 137 ]; then # (128 + SIGKILL)
173 log_error "Image customization was killed. Did not finish on time."
174 elif [ $rc -eq 141 ]; then # (128 + SIGPIPE)
175 log_error "Image customization was terminated by a SIGPIPE."
176 log_error "Maybe progress monitor has died unexpectedly."
177 elif [ $rc -eq 125 ]; then
178 log_error "Internal Error. Image customization could not start."
179 log_error "timeout did not manage to run."
181 log_error "Image customization died unexpectedly (return code $rc)."
185 echo -n "$(date +%Y:%m:%d-%H:%M:%S.%N)" >&2
186 log_info "Customization VM finished."
191 if [ "x$result" != "xSUCCESS" ]; then
192 log_error "Image customization failed."
196 log_info "Image customization finished successfully."
204 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :