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"
29 network_backend_support="@network_backend_support@"
35 for arg; do cmd+=$(printf "%q " "$arg"); done
43 get_api5_arguments() {
45 # Note the quotes around `$TEMP': they are essential!
46 eval set -- "$GETOPT_RESULT"
49 -i|-n) instance=$2; shift 2;;
51 -o) old_name=$2; shift 2;;
53 -b) blockdev=$2; shift 2;;
55 -s) swapdev=$2; shift 2;;
59 *) log_error "Internal error!" >&2; exit 1;;
62 if [ -z "$instance" -o -z "$blockdev" ]; then
63 log_error "Missing OS API Argument (-i, -n, or -b)"
66 if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then
67 log_error "Missing OS API Argument -s (swapdev)"
70 if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then
71 log_error "Missing OS API Argument -o (old_name)"
76 get_api10_arguments() {
77 if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
78 log_error "Missing OS API Variable:"
79 log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
82 instance=$INSTANCE_NAME
83 if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
84 log_error "At least one disk is needed"
87 if [ "$SCRIPT_NAME" = "export" ]; then
88 if [ -z "$EXPORT_DEVICE" ]; then
89 log_error "Missing OS API Variable EXPORT_DEVICE"
91 blockdev=$EXPORT_DEVICE
92 elif [ "$SCRIPT_NAME" = "import" ]; then
93 if [ -z "$IMPORT_DEVICE" ]; then
94 log_error "Missing OS API Variable IMPORT_DEVICE"
96 blockdev=$IMPORT_DEVICE
100 if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
101 log_error "Missing OS API Variable OLD_INSTANCE_NAME"
103 old_name=$OLD_INSTANCE_NAME
106 get_api20_arguments() {
108 if [ -z "$OSP_IMG_ID" ]; then
109 log_error "Missing OS API Parameter: OSP_IMG_ID"
112 if [ -z "$OSP_IMG_FORMAT" ]; then
113 log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
116 if [ -z "$OSP_IMG_PASSWD" ]; then
117 log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
122 IMG_FORMAT=$OSP_IMG_FORMAT
123 IMG_PASSWD=$OSP_IMG_PASSWD
124 if [ -n "$OSP_IMG_PROPERTIES" ]; then
125 IMG_PROPERTIES="$OSP_IMG_PROPERTIES"
127 if [ -n "$OSP_IMG_PERSONALITY" ]; then
128 IMG_PERSONALITY="$OSP_IMG_PERSONALITY"
134 filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
135 grep -m 1 -- "-1.*$blockdev" | \
137 if [ -z "$filesystem_dev_base" ]; then
138 log_error "Cannot interpret kpartx output and get partition mapping"
141 $KPARTX -a -p- "$blockdev" > /dev/null
142 filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
143 if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
144 log_error "Can't find kpartx mapped partition:" \
145 "/dev/mapper/$filesystem_dev_base"
148 echo "$filesystem_dev"
157 local image_type="$2"
159 declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
161 # The -f is needed, because we use an optimal alignment and sfdisk complains
162 # about partitions not ending on clylinder boundary.
163 local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device"
165 $sfdisk_cmd > /dev/null <<EOF
166 2048,,${part_id["$image_type"]},*
173 local target=$(mktemp -d)
174 add_cleanup rmdir "$target"
176 dd bs=512 count=2880 if=/dev/zero of="$img"
177 mkfs.ext2 -F "$img" > /dev/null
178 mount "$img" "$target" -o loop
179 set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
181 if [ -n "$UNATTEND" ]; then
182 if [ -f "$UNATTEND" ]; then
183 cat "$UNATTEND" > "$target/unattend.xml"
185 log_error "Unattend file: \`"$UNATTEND"' does not exist"
194 if [[ "$id" =~ ^pithos: ]]; then
196 elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
197 if [ "$network_backend_support" = "yes" ]; then
200 log_error "Retrieving images from the network is not supported."
208 # this one is only to be called by create
210 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
212 GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
213 if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
214 get_api5_arguments $GETOPT_RESULT
215 elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
217 elif [ "$OS_API_VERSION" = "20" ]; then
220 IMAGE_TYPE="$IMG_FORMAT"
221 BACKEND_TYPE=$(get_backend_type $IMG_ID)
223 log_error "Unknown OS API VERSION $OS_API_VERSION"
227 if [ -n "$OS_VARIANT" ]; then
228 if [ ! -d "$VARIANTS_DIR" ]; then
229 log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
232 VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
233 if [ -f "$VARIANT_CONFIG" ]; then
236 if grep -qxF "$OS_VARIANT" variants.list; then
237 log_error "ERROR: instance-image configuration error"
238 log_error " Published variant $OS_VARIANT is missing its" \
240 log_error " Please create $VARIANT_CONFIG or unpublish the" \
242 log_error " (by removing $OS_VARIANT from variants.list)"
244 log_error "Unofficial variant $OS_VARIANT is unsupported"
245 log_error "Most probably this is a user error, forcing a" \
247 log_error "To support this variant please create file" \
259 echo "Debootstraping to create a new root filesystem:"
261 # Create a policy-rc.d file to deny init script execution
262 mkdir -p "$target/usr/sbin"
263 cat > "$target/usr/sbin/policy-rc.d" <<EOF
267 chmod +x "$target/usr/sbin/policy-rc.d"
269 debootstrap --arch $(dpkg --print-architecture) \
270 --include "$HELPER_EXTRA_PKGS" --variant=minbase stable "$target" \
271 "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
273 # Save the package list
274 chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
276 rm "$target/usr/sbin/policy-rc.d"
278 # remove the downloaded debs, as they are no longer needed
279 find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
282 local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
283 tar cf "$tmp_cache" --one-file-system -C "$target" . || \
284 { rm "$tmp_cache"; false; }
285 # Overwrite the default cache file. Not the user specified if present.
286 mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
290 # if something fails here, it souldn't call cleanup again...
292 if [ ${#CLEANUP[*]} -gt 0 ]; then
293 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
294 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
295 for i in $REVERSE_INDEXES; do
296 # If something fails here, it's better to retry it for a few times
297 # before we give up with an error. This is needed for kpartx when
298 # dealing with ntfs partitions mounted through fuse. umount is not
299 # synchronous and may return while the partition is still busy. A
300 # premature attempt to delete partition mappings through kpartx on
301 # a device that hosts previously mounted ntfs partition may fail
302 # with errors like this one:
303 # `device-mapper: remove ioctl failed: Device or resource busy'
304 # A sensible workaround for this is to wait for a while and then
306 local cmd=${CLEANUP[$i]}
307 $cmd || for interval in 0.25 0.5 1 2 4; do
308 echo "Command $cmd failed!"
309 echo "I'll wait for $interval secs and will retry..."
313 if [ "$?" != "0" ]; then
323 DEFAULT_FILE="@sysconfdir@/default/snf-image"
324 if [ -f "$DEFAULT_FILE" ]; then
328 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
329 : ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
330 : ${HELPER_DIR:="@HELPER_DIR@"}
331 : ${HELPER_IMG:="@HELPER_IMG@"}
332 : ${HELPER_KERNEL:="@HELPER_KERNEL@"}
333 : ${HELPER_INITRD:="@HELPER_INITRD@"}
334 : ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
335 : ${HELPER_SOFT_TIMEOUT:=15}
336 : ${HELPER_HARD_TIMEOUT:=5}
337 : ${HELPER_USER:="nobody"}
338 : ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
339 : ${HELPER_CACHE_PKGS:="@HELPER_DIR@/packages"}
340 : ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw,util-linux"}
341 : ${HELPER_MIRROR:=""}
342 : ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
343 : ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
344 : ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
345 : ${UNATTEND:="@UNATTEND@"}
347 SCRIPT_NAME=$(basename $0)
349 if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
350 VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
351 VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
353 for dir in /lib/udev /sbin; do
354 if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
355 VOL_ID="$dir/vol_id -u"
356 VOL_TYPE="$dir/vol_id -t"
361 if [ -z "$VOL_ID" ]; then
362 log_error "vol_id or blkid not found, please install udev or util-linux"
366 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :