root / snf-image-host / common.sh.in @ 97532278
History | View | Annotate | Download (9.9 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved. |
---|---|
2 |
# |
3 |
# Redistribution and use in source and binary forms, with or without |
4 |
# modification, are permitted provided that the following conditions |
5 |
# are met: |
6 |
# |
7 |
# 1. Redistributions of source code must retain the above copyright |
8 |
# notice, this list of conditions and the following disclaimer. |
9 |
# |
10 |
# 2. Redistributions in binary form must reproduce the above copyright |
11 |
# notice, this list of conditions and the following disclaimer in the |
12 |
# documentation and/or other materials provided with the distribution. |
13 |
# |
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 |
# SUCH DAMAGE. |
25 |
# |
26 |
# The views and conclusions contained in the software and documentation are |
27 |
# those of the authors and should not be interpreted as representing official |
28 |
# policies, either expressed or implied, of GRNET S.A. |
29 |
|
30 |
AWK="awk" |
31 |
KPARTX="kpartx" |
32 |
LOSETUP="losetup" |
33 |
SFDISK="sfdisk" |
34 |
QEMU_IMG="qemu-img" |
35 |
INSTALL_MBR="install-mbr" |
36 |
TIMELIMIT="timelimit" |
37 |
PROGRESS_MONITOR="snf-progress-monitor" |
38 |
|
39 |
progress_monitor_support="@progress_monitor_support@" |
40 |
|
41 |
CLEANUP=( ) |
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 "$@" >&2 |
51 |
} |
52 |
|
53 |
get_api5_arguments() { |
54 |
GETOPT_RESULT=$* |
55 |
# Note the quotes around `$TEMP': they are essential! |
56 |
eval set -- "$GETOPT_RESULT" |
57 |
while true; do |
58 |
case "$1" in |
59 |
-i|-n) instance=$2; shift 2;; |
60 |
|
61 |
-o) old_name=$2; shift 2;; |
62 |
|
63 |
-b) blockdev=$2; shift 2;; |
64 |
|
65 |
-s) swapdev=$2; shift 2;; |
66 |
|
67 |
--) shift; break;; |
68 |
|
69 |
*) log_error "Internal error!" >&2; exit 1;; |
70 |
esac |
71 |
done |
72 |
if [ -z "$instance" -o -z "$blockdev" ]; then |
73 |
log_error "Missing OS API Argument (-i, -n, or -b)" |
74 |
exit 1 |
75 |
fi |
76 |
if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then |
77 |
log_error "Missing OS API Argument -s (swapdev)" |
78 |
exit 1 |
79 |
fi |
80 |
if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then |
81 |
log_error "Missing OS API Argument -o (old_name)" |
82 |
exit 1 |
83 |
fi |
84 |
} |
85 |
|
86 |
get_api10_arguments() { |
87 |
if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then |
88 |
log_error "Missing OS API Variable:" |
89 |
log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)" |
90 |
exit 1 |
91 |
fi |
92 |
instance=$INSTANCE_NAME |
93 |
if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then |
94 |
log_error "At least one disk is needed" |
95 |
exit 1 |
96 |
fi |
97 |
if [ "$SCRIPT_NAME" = "export" ]; then |
98 |
if [ -z "$EXPORT_DEVICE" ]; then |
99 |
log_error "Missing OS API Variable EXPORT_DEVICE" |
100 |
fi |
101 |
blockdev=$EXPORT_DEVICE |
102 |
elif [ "$SCRIPT_NAME" = "import" ]; then |
103 |
if [ -z "$IMPORT_DEVICE" ]; then |
104 |
log_error "Missing OS API Variable IMPORT_DEVICE" |
105 |
fi |
106 |
blockdev=$IMPORT_DEVICE |
107 |
else |
108 |
blockdev=$DISK_0_PATH |
109 |
fi |
110 |
if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then |
111 |
log_error "Missing OS API Variable OLD_INSTANCE_NAME" |
112 |
fi |
113 |
old_name=$OLD_INSTANCE_NAME |
114 |
} |
115 |
|
116 |
get_api20_arguments() { |
117 |
get_api10_arguments |
118 |
if [ -z "$OSP_IMG_ID" ]; then |
119 |
log_error "Missing OS API Parameter: OSP_IMG_ID" |
120 |
exit 1 |
121 |
fi |
122 |
if [ -z "$OSP_IMG_FORMAT" ]; then |
123 |
log_error "Missing OS API Parameter: OSP_IMG_FORMAT" |
124 |
exit 1 |
125 |
fi |
126 |
if [ -z "$OSP_IMG_PASSWD" ]; then |
127 |
log_error "Missing OS API Parameter: OSP_IMG_PASSWD" |
128 |
exit 1 |
129 |
fi |
130 |
|
131 |
IMG_ID=$OSP_IMG_ID |
132 |
IMG_FORMAT=$OSP_IMG_FORMAT |
133 |
IMG_PASSWD=$OSP_IMG_PASSWD |
134 |
if [ -n "$OSP_IMG_PERSONALITY" ]; then |
135 |
IMG_PERSONALITY=$OSP_IMG_PERSONALITY |
136 |
fi |
137 |
} |
138 |
|
139 |
map_disk0() { |
140 |
blockdev="$1" |
141 |
filesystem_dev_base=$($KPARTX -l -p- $blockdev | \ |
142 |
grep -m 1 -- "-1.*$blockdev" | \ |
143 |
$AWK '{print $1}') |
144 |
if [ -z "$filesystem_dev_base" ]; then |
145 |
log_error "Cannot interpret kpartx output and get partition mapping" |
146 |
exit 1 |
147 |
fi |
148 |
$KPARTX -a -p- "$blockdev" > /dev/null |
149 |
filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}" |
150 |
if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then |
151 |
log_error "Can't find kpartx mapped partition:" \ |
152 |
"/dev/mapper/$filesystem_dev_base" |
153 |
exit 1 |
154 |
fi |
155 |
echo "$filesystem_dev" |
156 |
} |
157 |
|
158 |
unmap_disk0() { |
159 |
$KPARTX -d -p- "$1" |
160 |
} |
161 |
|
162 |
format_disk0() { |
163 |
local device="$1" |
164 |
local image_type="$2" |
165 |
|
166 |
declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" ) |
167 |
|
168 |
# The -f is needed, because we use an optimal alignment and sfdisk complains |
169 |
# about partitions not ending on clylinder boundary. |
170 |
local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device" |
171 |
|
172 |
$sfdisk_cmd > /dev/null <<EOF |
173 |
2048,,${part_id["$image_type"]},* |
174 |
EOF |
175 |
} |
176 |
|
177 |
create_floppy() { |
178 |
local img=$1 |
179 |
|
180 |
local target=$(mktemp -d) |
181 |
add_cleanup rmdir "$target" |
182 |
|
183 |
dd bs=512 count=2880 if=/dev/zero of="$img" |
184 |
mkfs.ext2 -F "$img" > /dev/null |
185 |
mount "$img" "$target" -o loop |
186 |
set | egrep ^snf_export_\\w+=|sed -e 's/^snf_export_/SNF_IMAGE_/' | \ |
187 |
while read line; do |
188 |
echo "export $line" >> "$target/rules" |
189 |
done |
190 |
umount "$target" |
191 |
} |
192 |
|
193 |
# this one is only to be called by create |
194 |
ganeti_os_main() { |
195 |
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then |
196 |
OS_API_VERSION=5 |
197 |
GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"` |
198 |
if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi |
199 |
get_api5_arguments $GETOPT_RESULT |
200 |
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then |
201 |
get_api10_arguments |
202 |
elif [ "$OS_API_VERSION" = "20" ]; then |
203 |
get_api20_arguments |
204 |
IMAGE_NAME=$IMG_ID |
205 |
IMAGE_TYPE=$IMG_FORMAT |
206 |
else |
207 |
log_error "Unknown OS API VERSION $OS_API_VERSION" |
208 |
exit 1 |
209 |
fi |
210 |
|
211 |
if [ -n "$OS_VARIANT" ]; then |
212 |
if [ ! -d "$VARIANTS_DIR" ]; then |
213 |
log_error "OS Variants directory $VARIANTS_DIR doesn't exist" |
214 |
exit 1 |
215 |
fi |
216 |
VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf" |
217 |
if [ -f "$VARIANT_CONFIG" ]; then |
218 |
. "$VARIANT_CONFIG" |
219 |
else |
220 |
if grep -qxF "$OS_VARIANT" variants.list; then |
221 |
log_error "ERROR: instance-image configuration error" |
222 |
log_error " Published variant $OS_VARIANT is missing its config" \ |
223 |
"file" |
224 |
log_error " Please create $VARIANT_CONFIG or unpublish the variant" |
225 |
log_error " (by removing $OS_VARIANT from variants.list)" |
226 |
else |
227 |
log_error "Unofficial variant $OS_VARIANT is unsupported" |
228 |
log_error "Most probably this is a user error, forcing a wrong name" |
229 |
log_error "To support this variant please create file" \ |
230 |
"$VARIANT_CONFIG" |
231 |
fi |
232 |
exit 1 |
233 |
fi |
234 |
fi |
235 |
|
236 |
} |
237 |
|
238 |
cleanup() { |
239 |
# if something fails here, it souldn't call cleanup again... |
240 |
trap - EXIT |
241 |
if [ ${#CLEANUP[*]} -gt 0 ]; then |
242 |
LAST_ELEMENT=$((${#CLEANUP[*]}-1)) |
243 |
REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0) |
244 |
for i in $REVERSE_INDEXES; do |
245 |
# If something fails here, it's better to retry it for a few times |
246 |
# before we give up with an error. This is needed for kpartx when |
247 |
# dealing with ntfs partitions mounted through fuse. umount is not |
248 |
# synchronous and may return while the partition is still busy. A |
249 |
# premature attempt to delete partition mappings through kpartx on a |
250 |
# device that hosts previously mounted ntfs partition may fail with |
251 |
# an `device-mapper: remove ioctl failed: Device or resource busy' |
252 |
# error. A sensible workaround for this is to wait for a while and |
253 |
# then try again. |
254 |
local cmd=${CLEANUP[$i]} |
255 |
$cmd || for interval in 0.25 0.5 1 2 4; do |
256 |
echo "Command $cmd failed!" |
257 |
echo "I'll wait for $interval secs and will retry..." |
258 |
sleep $interval |
259 |
$cmd && break |
260 |
done |
261 |
if [ "$?" != "0" ]; then |
262 |
echo "Giving Up..." |
263 |
exit 1; |
264 |
fi |
265 |
done |
266 |
fi |
267 |
} |
268 |
|
269 |
trap cleanup EXIT |
270 |
|
271 |
DEFAULT_FILE="@sysconfdir@/default/snf-image" |
272 |
if [ -f "$DEFAULT_FILE" ]; then |
273 |
. "$DEFAULT_FILE" |
274 |
fi |
275 |
|
276 |
: ${ARCH:="x86_64"} |
277 |
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"} |
278 |
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"} |
279 |
: ${HELPER_DIR:="@HELPER_DIR@"} |
280 |
: ${HELPER_IMG:="@HELPER_IMG@"} |
281 |
: ${HELPER_KERNEL:="@HELPER_KERNEL@"} |
282 |
: ${HELPER_INITRD:="@HELPER_INITRD@"} |
283 |
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"} |
284 |
: ${HELPER_SOFT_TIMEOUT:=15} |
285 |
: ${HELPER_HARD_TIMEOUT:=5} |
286 |
: ${HELPER_USER:="nobody"} |
287 |
: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"} |
288 |
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python"} |
289 |
|
290 |
|
291 |
SCRIPT_NAME=$(basename $0) |
292 |
|
293 |
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then |
294 |
VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID" |
295 |
VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE" |
296 |
else |
297 |
for dir in /lib/udev /sbin; do |
298 |
if [ -f $dir/vol_id -a -x $dir/vol_id ]; then |
299 |
VOL_ID="$dir/vol_id -u" |
300 |
VOL_TYPE="$dir/vol_id -t" |
301 |
fi |
302 |
done |
303 |
fi |
304 |
|
305 |
if [ -z "$VOL_ID" ]; then |
306 |
log_error "vol_id or blkid not found, please install udev or util-linux" |
307 |
exit 1 |
308 |
fi |
309 |
|
310 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |