The value of DO_SYNC property should be "yes"
[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 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     log_warning "Unsupported IMAGE_DEBUG value: \`$IMAGE_DEBUG'"
32 fi
33
34 monitor_pipe=$(mktemp -u)
35 mkfifo -m 600 "$monitor_pipe"
36 add_cleanup rm -f "$monitor_pipe"
37
38 if [ -n "$PROGRESS_MONITOR" ]; then
39     { sleep 1; $PROGRESS_MONITOR "$instance" < "$monitor_pipe" ; } &
40     monitor_pid="$!"
41 else
42     sed -u 's|^|[MONITOR] |g' < "$monitor_pipe" &
43     monitor_pid="$!"
44 fi
45
46 # Create file descriptor to monitor_pipe
47 exec {MONITOR_FD}>${monitor_pipe}
48 add_cleanup  close_fd ${MONITOR_FD}
49
50 # Ignore sigpipe signals. If progress monitor is dead and snf-image tries to
51 # output something to the opened pipe, then a sigpipe will be raised. If we do
52 # not catch this, the program will terminate.
53 trap "" SIGPIPE
54
55 trap report_and_cleanup EXIT
56
57 echo "Processing image with ID: \`$IMG_ID' and type: \`$IMAGE_TYPE'" >&2
58
59 case $BACKEND_TYPE in
60     local)
61         if [[ "$IMAGE_NAME" =~ ^local:// ]]; then
62             IMAGE_NAME="${IMAGE_NAME:8}"
63         elif [[ "$IMAGE_NAME" =~ ^file:// ]]; then
64             IMAGE_NAME="${IMAGE_NAME:7}"
65             log_warning "The file:// back-end identifier is deprecated and" \
66                         "will be removed in the future. Use local:// instead."
67         fi
68
69         canonical_image_dir="$(canonicalize "$IMAGE_DIR")"
70         if [ ! -d "$canonical_image_dir" ]; then
71             log_error "The IMAGE_DIR directory: \`$IMAGE_DIR' does not exist."
72             report_error "Unable to retrieve image file."
73         fi
74
75         image_file="$IMAGE_DIR/$IMAGE_NAME"
76         if [ ! -e "$image_file" ]; then
77             if [ -e "$image_file.$IMAGE_TYPE" ] ; then
78                 image_file="$image_file.$IMAGE_TYPE"
79                 log_warning "The \`.$IMAGE_TYPE' extension is missing from" \
80                     "the local back-end id. This id form is deprecated and" \
81                     " will be remove in the future."
82             else
83                 log_error "Image file \`$image_file' does not exist."
84                 report_error "Unable to retrieve image file."
85                 exit 1
86             fi
87         fi
88
89         canonical_image_file="$(canonicalize "$image_file")"
90
91         if [[ "$canonical_image_file" != "$canonical_image_dir"* ]]; then
92             log_error "Image ID points to a file outside the image directory: \`$IMAGE_DIR'"
93             report_error "Invalid image ID"
94             exit 1
95         fi
96
97         image_size="$(stat -L -c %s "$image_file")"
98         ;;
99     null)
100         image_file=/dev/null
101         image_size=0
102         # Treat it as local file from now on...
103         BACKEND_TYPE="local"
104         ;;
105     network)
106         image_cmd="$CURL $(printf "%q" "$IMAGE_NAME")"
107         image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
108         ;;
109     pithos)
110         # For security reasons pass the database url to pithcat as an
111         # environmental variable.
112         export PITHCAT_INPUT_DB="$PITHOS_DB"
113         export PITHCAT_INPUT_DATA="$PITHOS_DATA"
114         cmd_args="$(printf "%q" "${IMAGE_NAME}")"
115         image_cmd="./pithcat $cmd_args"
116         image_size=$(./pithcat -s  $cmd_args)
117         ;;
118 esac
119
120 # If the target device is not a real block device we'll first losetup it.
121 # This is needed for file disks.
122 if [ ! -b "$blockdev" ]; then
123     original_blockdev="$blockdev"
124     blockdev=$($LOSETUP -sf "$blockdev")
125     add_cleanup $LOSETUP -d "$blockdev"
126 fi
127
128 case "$IMAGE_TYPE" in
129     ntfsdump|extdump)
130         # Create partitions
131         format_disk0 "$blockdev" "$IMAGE_TYPE"
132
133         # Install a new MBR
134         $INSTALL_MBR -p 1 -i n "$blockdev"
135
136         target="$(map_disk0 "$blockdev")-1" #the root device
137         add_cleanup unmap_disk0 "$blockdev"
138         snf_export_PROPERTY_ROOT_PARTITION=1
139         if [ "$IMAGE_TYPE" = "ntfsdump" ]; then
140             snf_export_PROPERTY_OSFAMILY="windows"
141         else
142             snf_export_PROPERTY_OSFAMILY="linux"
143         fi
144         ;;
145     diskdump)
146         target="$blockdev"
147         ;;
148     *)
149         log_error "Unknown Image format: \`$IMAGE_TYPE'"
150         report_error "Unknown Image Format"
151         exit 1
152         ;;
153 esac
154
155 report_info "Starting image copy..."
156 monitor="./copy-monitor.py -o $MONITOR_FD -r $image_size"
157 if [ "$BACKEND_TYPE" = "local" ]; then
158     # dd the dump to its new home :-)
159     # Deploying an image file on a target block device is a streaming copy
160     # operation. Enable the direct I/O flag on the output fd to avoid polluting
161     # the host cache with useless data.
162     $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
163 else
164     $image_cmd | $monitor dd bs=4M of="$target" oflag=direct
165 fi
166 report_info "Image copy finished."
167
168 # Create a floppy image
169 floppy=$(mktemp --tmpdir floppy.XXXXXX)
170 add_cleanup rm "$floppy"
171
172 snf_export_TYPE="$IMG_FORMAT"
173 snf_export_PASSWORD="$IMG_PASSWD"
174 snf_export_HOSTNAME="$instance"
175 if [ -n "$IMG_PROPERTIES" ]; then
176     snf_export_PROPERTIES="$IMG_PROPERTIES"
177 fi
178 if [ -n "$IMG_PERSONALITY" ]; then
179     snf_export_PERSONALITY="$IMG_PERSONALITY"
180 fi
181
182 snf_export_DEV=$(get_img_dev)
183
184 create_floppy "$floppy"
185
186 launch_helper "$blockdev" "$floppy"
187
188 report_info "Image customization finished successfully."
189
190 # Execute cleanups
191 cleanup
192 trap - EXIT
193
194 exit 0
195
196 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :