1 # Copyright (C) 2011 GRNET S.A.
2 # Copyright (C) 2007, 2008, 2009 Google Inc.
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.
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.
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
19 SNF_IMAGE_VERSION="@VERSION@"
26 INSTALL_MBR="install-mbr"
30 DATE="date -u" # Time in UTC
32 # Temporary use stderr as monitoring file descriptor.
33 # `create' will overwrite this
36 MSG_TYPE_ERROR="image-error"
37 MSG_TYPE_INFO="image-info"
45 for arg; do cmd+=$(printf "%q " "$arg"); done
54 echo "[WARNING] $*" >&2
64 report="$(./host-monitor.py info <<< "$*")"
65 eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
76 if [ ${#ERROR_MSGS[@]} -gt 0 ]; then
78 for err in "${ERROR_MSGS[@]}"; do
81 report="$(./host-monitor.py error <<< "$msg")"
83 report=$(./host-monitor.py error <<< "Internal Error: Image deployment failed.")
86 eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
89 get_api5_arguments() {
91 # Note the quotes around `$TEMP': they are essential!
92 eval set -- "$GETOPT_RESULT"
95 -i|-n) instance=$2; shift 2;;
97 -o) old_name=$2; shift 2;;
99 -b) blockdev=$2; shift 2;;
101 -s) swapdev=$2; shift 2;;
105 *) log_error "Internal error!" >&2; exit 1;;
108 if [ -z "$instance" -o -z "$blockdev" ]; then
109 log_error "Missing OS API Argument (-i, -n, or -b)"
112 if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then
113 log_error "Missing OS API Argument -s (swapdev)"
116 if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then
117 log_error "Missing OS API Argument -o (old_name)"
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)"
130 xen-hvm|xen-pvm) . xen-common.sh ;;
131 kvm) . kvm-common.sh ;;
132 *) log_error "Unsupported hypervisor: \`$HYPERVISTOR'"; exit 1;;
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"
140 if [ "$SCRIPT_NAME" = "export" ]; then
141 if [ -z "$EXPORT_DEVICE" ]; then
142 log_error "Missing OS API Variable EXPORT_DEVICE"
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"
151 blockdev=$IMPORT_DEVICE
153 blockdev=$DISK_0_PATH
155 if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
156 log_error "Missing OS API Variable OLD_INSTANCE_NAME"
159 old_name=$OLD_INSTANCE_NAME
162 get_api20_arguments() {
165 local required_osparams="IMG_ID IMG_FORMAT IMG_PASSWD"
166 local osparams="$required_osparams IMG_PROPERTIES IMG_PERSONALITY CONFIG_URL"
168 # Store OSP_VAR in VAR
169 for param in $osparams; do
170 eval $param=\"\$OSP_$param\"
173 if [ -n "$CONFIG_URL" ]; then
174 local config config_params
175 echo "Downloading configuration parameters from: \`$CONFIG_URL'" >&2
176 config=$($CURL -f "$CONFIG_URL")
177 config_params=$(./decode-config.py $osparams <<< "$config")
178 eval "$config_params"
181 for var in $required_osparams; do
182 if [ -z "${!var}" ]; then
183 log_error "Missing OS API Parameter: ${var}"
191 filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
192 grep -m 1 -- "-1.*$blockdev" | \
194 if [ -z "$filesystem_dev_base" ]; then
195 log_error "Cannot interpret kpartx output and get partition mapping"
198 $KPARTX -a -p- "$blockdev" > /dev/null
199 filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
200 if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
201 log_error "Can't find kpartx mapped partition:" \
202 "/dev/mapper/$filesystem_dev_base"
205 echo "$filesystem_dev"
214 local image_type="$2"
216 declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
218 # The -f is needed, because we use an optimal alignment and sfdisk complains
219 # about partitions not ending on clylinder boundary.
220 local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device"
222 $sfdisk_cmd > /dev/null <<EOF
223 2048,,${part_id["$image_type"]},*
233 add_cleanup rmdir "$target"
235 dd bs=512 count=2880 if=/dev/zero of="$img"
236 mkfs.ext2 -F "$img" > /dev/null
237 mount "$img" "$target" -o loop
238 set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
240 if [ -n "$UNATTEND" ]; then
241 if [ -f "$UNATTEND" ]; then
242 cat "$UNATTEND" > "$target/unattend.xml"
244 log_error "Unattend file: \`"$UNATTEND"' does not exist"
254 if [[ "$id" =~ ^pithos: ]]; then
256 elif [[ "$id" =~ ^pithosmap: ]]; then
258 elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
260 elif [ "$id" = "null" ]; then
270 if [ -d "$name" ]; then
274 local dname="${name%/*}"
275 local fname="${name##*/}"
277 if [ "x$dname" = "x" -a "${name:0:1}" = "/" ]; then
281 if [ -d "$dname" ]; then
282 (cd -- "$dname" && echo "${PWD%/}/$fname")
288 # this one is only to be called by create
290 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
292 GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
293 if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
294 get_api5_arguments $GETOPT_RESULT
295 elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
297 elif [ "$OS_API_VERSION" = "20" ]; then
300 IMAGE_TYPE="$IMG_FORMAT"
301 BACKEND_TYPE=$(get_backend_type $IMG_ID)
303 log_error "Unknown OS API VERSION $OS_API_VERSION"
307 if [ -n "$OS_VARIANT" ]; then
308 if [ ! -d "$VARIANTS_DIR" ]; then
309 log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
312 VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
313 if [ -f "$VARIANT_CONFIG" ]; then
316 if grep -qxF "$OS_VARIANT" variants.list; then
317 log_error "ERROR: instance-image configuration error"
318 log_error " Published variant $OS_VARIANT is missing its" \
320 log_error " Please create $VARIANT_CONFIG or unpublish the" \
322 log_error " (by removing $OS_VARIANT from variants.list)"
324 log_error "Unofficial variant $OS_VARIANT is unsupported"
325 log_error "Most probably this is a user error, forcing a" \
327 log_error "To support this variant please create file" \
340 # Create preferences.d for apt
341 mkdir -p "$target/etc/apt/preferences.d"
342 if [ -d "$MULTISTRAP_APTPREFDIR" ]; then
343 find "$MULTISTRAP_APTPREFDIR" -maxdepth 1 -type f -exec cp {} "$target/etc/apt/preferences.d" \;
346 # Create a policy-rc.d file to deny init script execution
347 mkdir -p "$target/usr/sbin"
348 cat > "$target/usr/sbin/policy-rc.d" <<EOF
352 chmod +x "$target/usr/sbin/policy-rc.d"
354 multistrap -d "$target" -f "$MULTISTRAP_CONFIG" 2>&1 | sed -u -e 's/^/MULTISTRAP: /g'
356 rm "$target/usr/sbin/policy-rc.d"
357 rm -rf "$target/etc/apt/preferences.d"
360 report_and_cleanup() {
366 "$@" &> /dev/null || true
372 if [ $rc -ne 0 ]; then
373 if [ $rc -eq 124 ]; then
374 log_error "Customization VM was terminated. Did not finish on time."
375 report_error "Image customization failed. Did not finish on time."
376 elif [ $rc -eq 137 ]; then # (128 + SIGKILL)
377 log_error "Customization VM was killed. Did not finish on time."
378 report_error "Image customization failed. Did not finish on time."
379 elif [ $rc -eq 141 ]; then # (128 + SIGPIPE)
380 log_error "Customization VM was terminated by a SIGPIPE."
381 log_error "Maybe progress monitor has died unexpectedly."
382 elif [ $rc -eq 125 ]; then
383 log_error "Internal Error. Image customization could not start."
384 log_error "timeout did not manage to run."
386 log_error "Customization VM died unexpectedly (return code $rc)."
390 report_info "Customization VM exited normally."
394 check_helper_result() {
397 if [ "x$result" != "xSUCCESS" ]; then
398 log_error "Image customization failed."
399 report_error "Image customization failed."
405 # if something fails here, it souldn't call cleanup again...
408 if [ ${#CLEANUP[*]} -gt 0 ]; then
409 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
410 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
411 for i in $REVERSE_INDEXES; do
412 # If something fails here, it's better to retry it for a few times
413 # before we give up with an error. This is needed for kpartx when
414 # dealing with ntfs partitions mounted through fuse. umount is not
415 # synchronous and may return while the partition is still busy. A
416 # premature attempt to delete partition mappings through kpartx on
417 # a device that hosts previously mounted ntfs partition may fail
418 # with errors like this one:
419 # `device-mapper: remove ioctl failed: Device or resource busy'
420 # A sensible workaround for this is to wait for a while and then
422 local cmd=${CLEANUP[$i]}
423 $cmd || for interval in 0.25 0.5 1 2 4; do
424 echo "Command $cmd failed!"
425 echo "I'll wait for $interval secs and will retry..."
429 if [ "$?" != "0" ]; then
439 DEFAULT_FILE="@sysconfdir@/default/snf-image"
440 if [ -f "$DEFAULT_FILE" ]; then
444 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
445 : ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
446 : ${IMAGE_DEBUG:="no"}
447 : ${VERSION_CHECK:="@VERSION_CHECK@"}
448 : ${HELPER_DIR:="@HELPER_DIR@"}
449 : ${HELPER_SIZE:="600"}
450 : ${HELPER_SOFT_TIMEOUT:=120}
451 : ${HELPER_HARD_TIMEOUT:=5}
452 : ${HELPER_USER:="nobody"}
453 : ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
454 : ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
455 : ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
456 : ${UNATTEND:="@UNATTEND@"}
457 : ${XEN_SCRIPTS_DIR="@sysconfdir@/xen/scripts"}
458 : ${MULTISTRAP_CONFIG:="@MULTISTRAP_CONFIG@"}
459 : ${MULTISTRAP_APTPREFDIR:="@MULTISTRAP_APTPREFDIR@"}
461 SCRIPT_NAME=$(basename $0)
463 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :