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
24 INSTALL_MBR="install-mbr"
28 network_backend_support="@network_backend_support@"
30 # Use file descriptors in the range 3-9. File descriptors below 3 are used for
31 # standard input, output, and error, the ones above 9 may be used by the shell
35 ERROR_TYPE="ganeti-error"
42 for arg; do cmd+=$(printf "%q " "$arg"); done
59 local id=$(sed 's/"/\\"/g' <<< "$INSTANCE_NAME")
60 local type="$ERROR_TYPE"
64 for err in "${ERROR_MSGS[@]}"; do
65 msg+="\"$(sed 's/"/\\"/g' <<< "$err")\","
67 if [ ${#msg} -gt 1 ]; then
68 # remove last comma (,)
73 local stderr="$(cat "$error_file" | sed 's/"/\\"/g')"
75 report="{\"id\":\"$id\","
76 report+="\"type\":\"$type\"," \
77 report+="\"timestamp\":$(date +%s)," \
78 report+="\"location\":\"$location\"," \
79 report+="\"messages\":$msg," \
80 report+="\"stderr\":\"$stderr\"}"
82 eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
85 get_api5_arguments() {
87 # Note the quotes around `$TEMP': they are essential!
88 eval set -- "$GETOPT_RESULT"
91 -i|-n) instance=$2; shift 2;;
93 -o) old_name=$2; shift 2;;
95 -b) blockdev=$2; shift 2;;
97 -s) swapdev=$2; shift 2;;
101 *) log_error "Internal error!" >&2; exit 1;;
104 if [ -z "$instance" -o -z "$blockdev" ]; then
105 log_error "Missing OS API Argument (-i, -n, or -b)"
108 if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then
109 log_error "Missing OS API Argument -s (swapdev)"
112 if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then
113 log_error "Missing OS API Argument -o (old_name)"
118 get_api10_arguments() {
119 if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
120 log_error "Missing OS API Variable:"
121 log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
124 instance=$INSTANCE_NAME
125 if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
126 log_error "At least one disk is needed"
129 if [ "$SCRIPT_NAME" = "export" ]; then
130 if [ -z "$EXPORT_DEVICE" ]; then
131 log_error "Missing OS API Variable EXPORT_DEVICE"
133 blockdev=$EXPORT_DEVICE
134 elif [ "$SCRIPT_NAME" = "import" ]; then
135 if [ -z "$IMPORT_DEVICE" ]; then
136 log_error "Missing OS API Variable IMPORT_DEVICE"
138 blockdev=$IMPORT_DEVICE
140 blockdev=$DISK_0_PATH
142 if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
143 log_error "Missing OS API Variable OLD_INSTANCE_NAME"
145 old_name=$OLD_INSTANCE_NAME
148 get_api20_arguments() {
150 if [ -z "$OSP_IMG_ID" ]; then
151 log_error "Missing OS API Parameter: OSP_IMG_ID"
154 if [ -z "$OSP_IMG_FORMAT" ]; then
155 log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
158 if [ -z "$OSP_IMG_PASSWD" ]; then
159 log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
164 IMG_FORMAT=$OSP_IMG_FORMAT
165 IMG_PASSWD=$OSP_IMG_PASSWD
166 if [ -n "$OSP_IMG_PROPERTIES" ]; then
167 IMG_PROPERTIES="$OSP_IMG_PROPERTIES"
169 if [ -n "$OSP_IMG_PERSONALITY" ]; then
170 IMG_PERSONALITY="$OSP_IMG_PERSONALITY"
176 filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
177 grep -m 1 -- "-1.*$blockdev" | \
179 if [ -z "$filesystem_dev_base" ]; then
180 log_error "Cannot interpret kpartx output and get partition mapping"
183 $KPARTX -a -p- "$blockdev" > /dev/null
184 filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
185 if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
186 log_error "Can't find kpartx mapped partition:" \
187 "/dev/mapper/$filesystem_dev_base"
190 echo "$filesystem_dev"
199 local image_type="$2"
201 declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
203 # The -f is needed, because we use an optimal alignment and sfdisk complains
204 # about partitions not ending on clylinder boundary.
205 local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device"
207 $sfdisk_cmd > /dev/null <<EOF
208 2048,,${part_id["$image_type"]},*
215 local target=$(mktemp -d)
216 add_cleanup rmdir "$target"
218 dd bs=512 count=2880 if=/dev/zero of="$img"
219 mkfs.ext2 -F "$img" > /dev/null
220 mount "$img" "$target" -o loop
221 set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
223 if [ -n "$UNATTEND" ]; then
224 if [ -f "$UNATTEND" ]; then
225 cat "$UNATTEND" > "$target/unattend.xml"
227 log_error "Unattend file: \`"$UNATTEND"' does not exist"
236 if [[ "$id" =~ ^pithos: ]]; then
238 elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
239 if [ "$network_backend_support" = "yes" ]; then
242 log_error "Retrieving images from the network is not supported."
250 # this one is only to be called by create
252 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
254 GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
255 if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
256 get_api5_arguments $GETOPT_RESULT
257 elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
259 elif [ "$OS_API_VERSION" = "20" ]; then
262 IMAGE_TYPE="$IMG_FORMAT"
263 BACKEND_TYPE=$(get_backend_type $IMG_ID)
265 log_error "Unknown OS API VERSION $OS_API_VERSION"
269 if [ -n "$OS_VARIANT" ]; then
270 if [ ! -d "$VARIANTS_DIR" ]; then
271 log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
274 VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
275 if [ -f "$VARIANT_CONFIG" ]; then
278 if grep -qxF "$OS_VARIANT" variants.list; then
279 log_error "ERROR: instance-image configuration error"
280 log_error " Published variant $OS_VARIANT is missing its" \
282 log_error " Please create $VARIANT_CONFIG or unpublish the" \
284 log_error " (by removing $OS_VARIANT from variants.list)"
286 log_error "Unofficial variant $OS_VARIANT is unsupported"
287 log_error "Most probably this is a user error, forcing a" \
289 log_error "To support this variant please create file" \
301 echo "Debootstraping to create a new root filesystem:"
303 # Create a policy-rc.d file to deny init script execution
304 mkdir -p "$target/usr/sbin"
305 cat > "$target/usr/sbin/policy-rc.d" <<EOF
309 chmod +x "$target/usr/sbin/policy-rc.d"
311 debootstrap --arch $(dpkg --print-architecture) \
312 --include "$HELPER_EXTRA_PKGS" --variant=minbase stable "$target" \
313 "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
315 # Save the package list
316 chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
318 rm "$target/usr/sbin/policy-rc.d"
320 # remove the downloaded debs, as they are no longer needed
321 find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
324 local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
325 tar cf "$tmp_cache" --one-file-system -C "$target" . || \
326 { rm "$tmp_cache"; false; }
327 # Overwrite the default cache file. Not the user specified if present.
328 mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
332 # Carefull this should be the first command in the function. We want to
333 # store the last exit code to see if cleanup was triggered by an abnormal
334 # termination of the script.
338 # if something fails here, it souldn't call cleanup again...
341 if [ $rc -ne 0 -a -f "$err_file" ]; then
342 report_error "$err_file"
345 if [ ${#CLEANUP[*]} -gt 0 ]; then
346 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
347 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
348 for i in $REVERSE_INDEXES; do
349 # If something fails here, it's better to retry it for a few times
350 # before we give up with an error. This is needed for kpartx when
351 # dealing with ntfs partitions mounted through fuse. umount is not
352 # synchronous and may return while the partition is still busy. A
353 # premature attempt to delete partition mappings through kpartx on
354 # a device that hosts previously mounted ntfs partition may fail
355 # with errors like this one:
356 # `device-mapper: remove ioctl failed: Device or resource busy'
357 # A sensible workaround for this is to wait for a while and then
359 local cmd=${CLEANUP[$i]}
360 $cmd || for interval in 0.25 0.5 1 2 4; do
361 echo "Command $cmd failed!"
362 echo "I'll wait for $interval secs and will retry..."
366 if [ "$?" != "0" ]; then
376 DEFAULT_FILE="@sysconfdir@/default/snf-image"
377 if [ -f "$DEFAULT_FILE" ]; then
381 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
382 : ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
383 : ${HELPER_DIR:="@HELPER_DIR@"}
384 : ${HELPER_IMG:="@HELPER_IMG@"}
385 : ${HELPER_KERNEL:="@HELPER_KERNEL@"}
386 : ${HELPER_INITRD:="@HELPER_INITRD@"}
387 : ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
388 : ${HELPER_SOFT_TIMEOUT:=15}
389 : ${HELPER_HARD_TIMEOUT:=5}
390 : ${HELPER_USER:="nobody"}
391 : ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
392 : ${HELPER_CACHE_PKGS:="@HELPER_DIR@/packages"}
393 : ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw,util-linux"}
394 : ${HELPER_MIRROR:=""}
395 : ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
396 : ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
397 : ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
398 : ${UNATTEND:="@UNATTEND@"}
400 SCRIPT_NAME=$(basename $0)
402 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :