Check in MountImage task if /etc/fstab is present
[snf-image] / snf-image-host / common.sh.in
1 # Copyright (C) 2011 GRNET S.A. 
2 # Copyright (C) 2007, 2008, 2009 Google Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA.
18
19 SNF_IMAGE_VERSION="@VERSION@"
20
21 AWK="awk"
22 LOSETUP="losetup"
23 KPARTX="kpartx"
24 SFDISK="sfdisk"
25 QEMU_IMG="qemu-img"
26 INSTALL_MBR="install-mbr"
27 TIMEOUT="timeout"
28 CURL="curl"
29 TAR="tar"
30 DATE="date -u" # Time in UTC
31
32 # Temporary use stderr as monitoring file descriptor.
33 # `create' will overwrite this
34 MONITOR_FD="2"
35
36 MSG_TYPE_ERROR="image-error"
37 MSG_TYPE_INFO="image-info"
38
39 CLEANUP=( )
40 ERROR_MSGS=( )
41
42
43 add_cleanup() {
44     local cmd=""
45     for arg; do cmd+=$(printf "%q " "$arg"); done
46     CLEANUP+=("$cmd")
47 }
48
49 log_error() {
50     echo "[ERROR] $*" >&2
51 }
52
53 log_warning() {
54     echo "[WARNING] $*" >&2
55 }
56
57 report_error() {
58     ERROR_MSGS+=("$@")
59 }
60
61 report_info() {
62     local report
63     echo "[INFO] $*" >&2
64     report="$(./host-monitor.py info <<< "$*")"
65     eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
66 }
67
68
69 close_fd() {
70     local fd="$1"
71     exec {fd}>&-
72 }
73
74 send_errors() {
75     local report=""
76     if [ ${#ERROR_MSGS[@]} -gt 0 ]; then
77         local msg=""
78         for err in "${ERROR_MSGS[@]}"; do
79             msg+="$(echo "$err")"
80         done
81         report="$(./host-monitor.py error <<< "$msg")"
82     else
83         report=$(./host-monitor.py error <<< "Internal Error: Image deployment failed.")
84     fi
85
86     eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
87 }
88
89 get_api5_arguments() {
90     GETOPT_RESULT=$*
91     # Note the quotes around `$TEMP': they are essential!
92     eval set -- "$GETOPT_RESULT"
93     while true; do
94         case "$1" in
95             -i|-n) instance=$2; shift 2;;
96
97             -o) old_name=$2; shift 2;;
98
99             -b) blockdev=$2; shift 2;;
100
101             -s) swapdev=$2; shift 2;;
102
103             --) shift; break;;
104
105             *)  log_error "Internal error!" >&2; exit 1;;
106         esac
107     done
108     if [ -z "$instance" -o -z "$blockdev" ]; then
109         log_error "Missing OS API Argument (-i, -n, or -b)"
110         exit 1
111     fi
112     if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
113         log_error "Missing OS API Argument -s (swapdev)"
114         exit 1
115     fi
116     if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
117         log_error "Missing OS API Argument -o (old_name)"
118         exit 1
119     fi
120 }
121
122 get_api10_arguments() {
123     if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
124         log_error "Missing OS API Variable:"
125         log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
126         exit 1
127     fi
128
129     case $HYPERVISOR in
130         xen-hvm|xen-pvm) . xen-common.sh ;;
131         kvm) . kvm-common.sh ;;
132         *) log_error "Unsupported hypervisor: \`$HYPERVISTOR'"; exit 1;;
133     esac
134
135     instance=$INSTANCE_NAME
136     if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
137         log_error "At least one disk is needed"
138         exit 1
139     fi
140     if [ "$SCRIPT_NAME" = "export" ]; then
141         if [ -z "$EXPORT_DEVICE" ]; then
142             log_error "Missing OS API Variable EXPORT_DEVICE"
143             exit 1
144         fi
145         blockdev=$EXPORT_DEVICE
146     elif [ "$SCRIPT_NAME" = "import" ]; then
147         if [ -z "$IMPORT_DEVICE" ]; then
148             log_error "Missing OS API Variable IMPORT_DEVICE"
149             exit 1
150         fi
151         blockdev=$IMPORT_DEVICE
152     else
153         blockdev=$DISK_0_PATH
154     fi
155     if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
156         log_error "Missing OS API Variable OLD_INSTANCE_NAME"
157         exit 1
158     fi
159     old_name=$OLD_INSTANCE_NAME
160 }
161
162 get_api20_arguments() {
163     get_api10_arguments
164
165     if [ "$SCRIPT_NAME" = "create" ]; then
166         local required_osparams="IMG_ID IMG_FORMAT IMG_PASSWD"
167         local osparams="$required_osparams IMG_PROPERTIES IMG_PERSONALITY CONFIG_URL"
168
169         # Store OSP_VAR in VAR
170         for param in $osparams; do
171             eval $param=\"\$OSP_$param\"
172         done
173
174         if [ -n "$CONFIG_URL" ]; then
175             local config config_params
176             echo "Downloading configuration parameters from: \`$CONFIG_URL'" >&2
177             config=$($CURL -f "$CONFIG_URL")
178             config_params=$(./decode-config.py $osparams <<< "$config")
179             eval "$config_params"
180         fi
181
182         for var in $required_osparams; do
183             if [ -z "${!var}" ]; then
184                  log_error "Missing OS API Parameter: ${var}"
185                  exit 1
186             fi
187         done
188     fi
189 }
190
191 map_disk0() {
192     blockdev="$1"
193     filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
194                             grep -m 1 -- "-1.*$blockdev" | \
195                             $AWK '{print $1}')
196     if [ -z "$filesystem_dev_base" ]; then
197         log_error "Cannot interpret kpartx output and get partition mapping"
198         exit 1
199     fi
200     $KPARTX -a -p- "$blockdev" > /dev/null
201     filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
202     if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
203         log_error "Can't find kpartx mapped partition:" \
204                                             "/dev/mapper/$filesystem_dev_base"
205         exit 1
206     fi
207     echo "$filesystem_dev"
208 }
209
210 unmap_disk0() {
211     $KPARTX -d -p- "$1"
212 }
213
214 format_disk0() {
215     local device="$1"
216     local image_type="$2"
217
218     declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
219
220     # The -f is needed, because we use an optimal alignment and sfdisk complains
221     # about partitions not ending on clylinder boundary.
222     local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device"
223
224     $sfdisk_cmd > /dev/null <<EOF
225 2048,,${part_id["$image_type"]},*
226 EOF
227 }
228
229 create_floppy() {
230     local img target
231
232     img=$1
233
234     target=$(mktemp -d)
235     add_cleanup rmdir "$target"
236
237     dd bs=512 count=2880 if=/dev/zero of="$img"
238     mkfs.ext2 -F "$img" > /dev/null
239     mount "$img" "$target" -o loop
240     set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
241         > "$target/rules"
242     if [ -n "$UNATTEND" ]; then
243         if [ -f "$UNATTEND" ]; then
244             cat "$UNATTEND" > "$target/unattend.xml"
245         else
246             log_error "Unattend file: \`"$UNATTEND"' does not exist"
247             exit 1
248         fi
249     fi
250     umount "$target"
251 }
252
253 get_backend_type() {
254     local id=$1
255
256     if [[ "$id" =~ ^pithos: ]]; then
257         echo "pithos"
258     elif [[ "$id" =~ ^pithosmap: ]]; then
259         echo "pithos"
260     elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
261         echo "network"
262     elif [ "$id" = "null" ]; then
263         echo "null"
264     else
265         echo "local"
266     fi
267 }
268
269 canonicalize() {
270     local name="$1"
271
272     if [ -d "$name" ]; then
273         name="$name/"
274     fi
275
276     local dname="${name%/*}"
277     local fname="${name##*/}"
278
279     if [ "x$dname" = "x" -a "${name:0:1}" = "/" ]; then
280         dname="/"
281     fi
282
283     if [ -d "$dname" ]; then
284         (cd -- "$dname" && echo "${PWD%/}/$fname")
285     else
286         echo
287     fi
288 }
289
290 # this one is only to be called by create
291 ganeti_os_main() {
292     if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
293         OS_API_VERSION=5
294         GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
295         if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
296         get_api5_arguments $GETOPT_RESULT
297     elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
298         get_api10_arguments
299     elif [ "$OS_API_VERSION" = "20" ]; then
300         get_api20_arguments
301         IMAGE_NAME="$IMG_ID"
302         IMAGE_TYPE="$IMG_FORMAT"
303         BACKEND_TYPE=$(get_backend_type $IMG_ID)
304     else
305         log_error "Unknown OS API VERSION $OS_API_VERSION"
306         exit 1
307     fi
308     
309     if [ -n "$OS_VARIANT" ]; then
310         if [ ! -d "$VARIANTS_DIR" ]; then
311             log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
312             exit 1
313         fi
314         VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
315         if [ -f "$VARIANT_CONFIG" ]; then
316             . "$VARIANT_CONFIG"
317         else
318             if grep -qxF "$OS_VARIANT" variants.list; then
319                 log_error "ERROR: instance-image configuration error"
320                 log_error "  Published variant $OS_VARIANT is missing its" \
321                     "config file"
322                 log_error "  Please create $VARIANT_CONFIG or unpublish the" \
323                     "variant"
324                 log_error "  (by removing $OS_VARIANT from variants.list)"
325             else
326                 log_error "Unofficial variant $OS_VARIANT is unsupported"
327                 log_error "Most probably this is a user error, forcing a" \
328                     "wrong name"
329                 log_error "To support this variant please create file" \
330                     "$VARIANT_CONFIG"
331             fi
332             exit 1
333         fi
334     fi
335 }
336
337 do_multistrap() {
338    local target="$1"
339    local cache="$2"
340    local pkgs="$3"
341
342     # Create preferences.d for apt
343     mkdir -p "$target/etc/apt/preferences.d"
344     if [ -d "$MULTISTRAP_APTPREFDIR" ]; then
345         find "$MULTISTRAP_APTPREFDIR" -maxdepth 1 -type f -exec cp {} "$target/etc/apt/preferences.d" \;
346     fi
347
348     # Create a policy-rc.d file to deny init script execution
349     mkdir -p "$target/usr/sbin"
350     cat > "$target/usr/sbin/policy-rc.d" <<EOF
351 #!/bin/sh
352 exit 101
353 EOF
354     chmod +x "$target/usr/sbin/policy-rc.d"
355
356    multistrap -d "$target" -f "$MULTISTRAP_CONFIG" 2>&1 | sed -u -e 's/^/MULTISTRAP: /g'
357
358    rm "$target/usr/sbin/policy-rc.d"
359    rm -rf "$target/etc/apt/preferences.d"
360 }
361
362 report_and_cleanup() {
363     send_errors
364     cleanup
365 }
366
367 suppress_errors() {
368     "$@" &> /dev/null || true
369 }
370
371 check_helper_rc() {
372     local rc=$1
373
374     if [ $rc -ne 0 ]; then
375         if [ $rc -eq 124 ];  then
376             log_error "Customization VM was terminated. Did not finish on time."
377             report_error "Image customization failed. Did not finish on time."
378         elif [ $rc -eq 137 ]; then # (128 + SIGKILL)
379             log_error "Customization VM was killed. Did not finish on time."
380             report_error "Image customization failed. Did not finish on time."
381         elif [ $rc -eq 141 ]; then # (128 + SIGPIPE)
382             log_error "Customization VM was terminated by a SIGPIPE."
383             log_error "Maybe progress monitor has died unexpectedly."
384         elif [ $rc -eq 125 ]; then
385             log_error "Internal Error. Image customization could not start."
386             log_error "timeout did not manage to run."
387         else
388             log_error "Customization VM died unexpectedly (return code $rc)."
389         fi
390         exit 1
391     else
392         report_info "Customization VM exited normally."
393     fi
394 }
395
396 check_helper_result() {
397    local result=$1
398
399     if [ "x$result" != "xSUCCESS" ]; then
400         log_error "Image customization failed."
401         report_error "Image customization failed."
402         exit 1
403     fi
404 }
405
406 cleanup() {
407     # if something fails here, it souldn't call cleanup again...
408     trap - EXIT
409
410     if [ ${#CLEANUP[*]} -gt 0 ]; then
411         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
412         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
413         for i in $REVERSE_INDEXES; do
414             # If something fails here, it's better to retry it for a few times
415             # before we give up with an error. This is needed for kpartx when
416             # dealing with ntfs partitions mounted through fuse. umount is not
417             # synchronous and may return while the partition is still busy. A
418             # premature attempt to delete partition mappings through kpartx on
419             # a device that hosts previously mounted ntfs partition may fail
420             # with errors like this one:
421             # `device-mapper: remove ioctl failed: Device or resource busy'
422             # A sensible workaround for this is to wait for a while and then
423             # retry it.
424             local cmd=${CLEANUP[$i]}
425             $cmd || for interval in 0.25 0.5 1 2 4; do
426             echo "Command $cmd failed!"
427             echo "I'll wait for $interval secs and will retry..."
428             sleep $interval
429             $cmd && break
430         done
431         if [ "$?" != "0" ]; then
432             echo "Giving Up..."
433             exit 1;
434         fi
435     done
436   fi
437 }
438
439 trap cleanup EXIT
440
441 DEFAULT_FILE="@sysconfdir@/default/snf-image"
442 if [ -f "$DEFAULT_FILE" ]; then
443     . "$DEFAULT_FILE"
444 fi
445
446 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
447 : ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
448 : ${IMAGE_DEBUG:="no"}
449 : ${VERSION_CHECK:="@VERSION_CHECK@"}
450 : ${HELPER_DIR:="@HELPER_DIR@"}
451 : ${HELPER_SIZE:="600"}
452 : ${HELPER_SOFT_TIMEOUT:=120}
453 : ${HELPER_HARD_TIMEOUT:=5}
454 : ${HELPER_USER:="nobody"}
455 : ${HELPER_MEMORY:="256"}
456 : ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
457 : ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
458 : ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
459 : ${UNATTEND:="@UNATTEND@"}
460 : ${XEN_SCRIPTS_DIR="@sysconfdir@/xen/scripts"}
461 : ${MULTISTRAP_CONFIG:="@MULTISTRAP_CONFIG@"}
462 : ${MULTISTRAP_APTPREFDIR:="@MULTISTRAP_APTPREFDIR@"}
463
464 SCRIPT_NAME=$(basename $0)
465
466 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :