root / snf-image-host / common.sh.in @ 5cbb15b9
History | View | Annotate | Download (9.6 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 |
log_error() { |
44 |
echo "$@" >&2 |
45 |
} |
46 |
|
47 |
get_api5_arguments() { |
48 |
GETOPT_RESULT=$* |
49 |
# Note the quotes around `$TEMP': they are essential! |
50 |
eval set -- "$GETOPT_RESULT" |
51 |
while true; do |
52 |
case "$1" in |
53 |
-i|-n) instance=$2; shift 2;; |
54 |
|
55 |
-o) old_name=$2; shift 2;; |
56 |
|
57 |
-b) blockdev=$2; shift 2;; |
58 |
|
59 |
-s) swapdev=$2; shift 2;; |
60 |
|
61 |
--) shift; break;; |
62 |
|
63 |
*) log_error "Internal error!" >&2; exit 1;; |
64 |
esac |
65 |
done |
66 |
if [ -z "$instance" -o -z "$blockdev" ]; then |
67 |
log_error "Missing OS API Argument (-i, -n, or -b)" |
68 |
exit 1 |
69 |
fi |
70 |
if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then |
71 |
log_error "Missing OS API Argument -s (swapdev)" |
72 |
exit 1 |
73 |
fi |
74 |
if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then |
75 |
log_error "Missing OS API Argument -o (old_name)" |
76 |
exit 1 |
77 |
fi |
78 |
} |
79 |
|
80 |
get_api10_arguments() { |
81 |
if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then |
82 |
log_error "Missing OS API Variable:" |
83 |
log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)" |
84 |
exit 1 |
85 |
fi |
86 |
instance=$INSTANCE_NAME |
87 |
if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then |
88 |
log_error "At least one disk is needed" |
89 |
exit 1 |
90 |
fi |
91 |
if [ "$SCRIPT_NAME" = "export" ]; then |
92 |
if [ -z "$EXPORT_DEVICE" ]; then |
93 |
log_error "Missing OS API Variable EXPORT_DEVICE" |
94 |
fi |
95 |
blockdev=$EXPORT_DEVICE |
96 |
elif [ "$SCRIPT_NAME" = "import" ]; then |
97 |
if [ -z "$IMPORT_DEVICE" ]; then |
98 |
log_error "Missing OS API Variable IMPORT_DEVICE" |
99 |
fi |
100 |
blockdev=$IMPORT_DEVICE |
101 |
else |
102 |
blockdev=$DISK_0_PATH |
103 |
fi |
104 |
if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then |
105 |
log_error "Missing OS API Variable OLD_INSTANCE_NAME" |
106 |
fi |
107 |
old_name=$OLD_INSTANCE_NAME |
108 |
} |
109 |
|
110 |
get_api20_arguments() { |
111 |
get_api10_arguments |
112 |
if [ -z "$OSP_IMG_ID" ]; then |
113 |
log_error "Missing OS API Parameter: OSP_IMG_ID" |
114 |
exit 1 |
115 |
fi |
116 |
if [ -z "$OSP_IMG_FORMAT" ]; then |
117 |
log_error "Missing OS API Parameter: OSP_IMG_FORMAT" |
118 |
exit 1 |
119 |
fi |
120 |
if [ -z "$OSP_IMG_PASSWD" ]; then |
121 |
log_error "Missing OS API Parameter: OSP_IMG_PASSWD" |
122 |
exit 1 |
123 |
fi |
124 |
|
125 |
IMG_ID=$OSP_IMG_ID |
126 |
IMG_FORMAT=$OSP_IMG_FORMAT |
127 |
IMG_PASSWD=$OSP_IMG_PASSWD |
128 |
if [ -n "$OSP_IMG_PERSONALITY" ]; then |
129 |
IMG_PERSONALITY=$OSP_IMG_PERSONALITY |
130 |
fi |
131 |
} |
132 |
|
133 |
map_disk0() { |
134 |
blockdev="$1" |
135 |
filesystem_dev_base=$($KPARTX -l -p- $blockdev | \ |
136 |
grep -m 1 -- "-1.*$blockdev" | \ |
137 |
$AWK '{print $1}') |
138 |
if [ -z "$filesystem_dev_base" ]; then |
139 |
log_error "Cannot interpret kpartx output and get partition mapping" |
140 |
exit 1 |
141 |
fi |
142 |
$KPARTX -a -p- $blockdev > /dev/null |
143 |
filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}" |
144 |
if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then |
145 |
log_error "Can't find kpartx mapped partition:" \ |
146 |
"/dev/mapper/$filesystem_dev_base" |
147 |
exit 1 |
148 |
fi |
149 |
echo "$filesystem_dev" |
150 |
} |
151 |
|
152 |
unmap_disk0() { |
153 |
$KPARTX -d -p- $1 |
154 |
} |
155 |
|
156 |
format_disk0() { |
157 |
local device="$1" |
158 |
local image_type="$2" |
159 |
|
160 |
declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" ) |
161 |
|
162 |
# The -f is needed, because we use an optimal alignment and sfdisk complains |
163 |
# about partitions not ending on clylinder boundary. |
164 |
local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device" |
165 |
|
166 |
$sfdisk_cmd > /dev/null <<EOF |
167 |
2048,,${part_id["$image_type"]},* |
168 |
EOF |
169 |
} |
170 |
|
171 |
create_floppy() { |
172 |
local img=$1 |
173 |
|
174 |
local target=$(mktemp -d) || exit 1 |
175 |
CLEANUP+=("rmdir $target") |
176 |
|
177 |
dd bs=512 count=2880 if=/dev/zero of=$img |
178 |
mkfs.ext2 -F $img > /dev/null |
179 |
mount $img $target -o loop |
180 |
set | egrep ^snf_export_\\w+=|sed -e 's/^snf_export_/SNF_IMAGE_/' | \ |
181 |
while read line; do |
182 |
echo "export $line" >> $target/rules |
183 |
done |
184 |
umount $target |
185 |
} |
186 |
|
187 |
# this one is only to be called by create |
188 |
ganeti_os_main() { |
189 |
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then |
190 |
OS_API_VERSION=5 |
191 |
GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"` |
192 |
if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi |
193 |
get_api5_arguments $GETOPT_RESULT |
194 |
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then |
195 |
get_api10_arguments |
196 |
elif [ "$OS_API_VERSION" = "20" ]; then |
197 |
get_api20_arguments |
198 |
IMAGE_NAME=$IMG_ID |
199 |
IMAGE_TYPE=$IMG_FORMAT |
200 |
else |
201 |
log_error "Unknown OS API VERSION $OS_API_VERSION" |
202 |
exit 1 |
203 |
fi |
204 |
|
205 |
if [ -n "$OS_VARIANT" ]; then |
206 |
if [ ! -d "$VARIANTS_DIR" ]; then |
207 |
log_error "OS Variants directory $VARIANTS_DIR doesn't exist" |
208 |
exit 1 |
209 |
fi |
210 |
VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf" |
211 |
if [ -f "$VARIANT_CONFIG" ]; then |
212 |
. "$VARIANT_CONFIG" |
213 |
else |
214 |
if grep -qxF "$OS_VARIANT" variants.list; then |
215 |
log_error "ERROR: instance-image configuration error" |
216 |
log_error " Published variant $OS_VARIANT is missing its config" \ |
217 |
"file" |
218 |
log_error " Please create $VARIANT_CONFIG or unpublish the variant" |
219 |
log_error " (by removing $OS_VARIANT from variants.list)" |
220 |
else |
221 |
log_error "Unofficial variant $OS_VARIANT is unsupported" |
222 |
log_error "Most probably this is a user error, forcing a wrong name" |
223 |
log_error "To support this variant please create file" \ |
224 |
"$VARIANT_CONFIG" |
225 |
fi |
226 |
exit 1 |
227 |
fi |
228 |
fi |
229 |
|
230 |
} |
231 |
|
232 |
cleanup() { |
233 |
# if something fails here, it souldn't call cleanup again... |
234 |
trap - EXIT |
235 |
if [ ${#CLEANUP[*]} -gt 0 ]; then |
236 |
LAST_ELEMENT=$((${#CLEANUP[*]}-1)) |
237 |
REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0) |
238 |
for i in $REVERSE_INDEXES; do |
239 |
# If something fails here, it's better to retry it for a few times |
240 |
# before we give up with an error. This is needed for kpartx when |
241 |
# dealing with ntfs partitions mounted through fuse. umount is not |
242 |
# synchronous and may return while the partition is still busy. A |
243 |
# premature attempt to delete partition mappings through kpartx on a |
244 |
# device that hosts previously mounted ntfs partition may fail with |
245 |
# an `device-mapper: remove ioctl failed: Device or resource busy' |
246 |
# error. A sensible workaround for this is to wait for a while and |
247 |
# then try again. |
248 |
local cmd=${CLEANUP[$i]} |
249 |
$cmd || for interval in 0.25 0.5 1 2 4; do |
250 |
echo "Command $cmd failed!" |
251 |
echo "I'll wait for $interval secs and will retry..." |
252 |
sleep $interval |
253 |
$cmd && break |
254 |
done |
255 |
if [ "$?" != "0" ]; then |
256 |
echo "Giving Up..." |
257 |
exit 1; |
258 |
fi |
259 |
done |
260 |
fi |
261 |
} |
262 |
|
263 |
trap cleanup EXIT |
264 |
|
265 |
DEFAULT_FILE="@sysconfdir@/default/snf-image" |
266 |
if [ -f "$DEFAULT_FILE" ]; then |
267 |
. "$DEFAULT_FILE" |
268 |
fi |
269 |
|
270 |
: ${ARCH:="x86_64"} |
271 |
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"} |
272 |
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"} |
273 |
: ${HELPER_DIR:="@HELPER_DIR@"} |
274 |
: ${HELPER_IMG:="@HELPER_IMG@"} |
275 |
: ${HELPER_KERNEL:="@HELPER_KERNEL@"} |
276 |
: ${HELPER_INITRD:="@HELPER_INITRD@"} |
277 |
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"} |
278 |
: ${HELPER_SOFT_TIMEOUT=120} |
279 |
: ${HELPER_HARD_TIMEOUT=15} |
280 |
: ${HELPER_USER="nobody"} |
281 |
|
282 |
SCRIPT_NAME=$(basename $0) |
283 |
|
284 |
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then |
285 |
VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID" |
286 |
VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE" |
287 |
else |
288 |
for dir in /lib/udev /sbin; do |
289 |
if [ -f $dir/vol_id -a -x $dir/vol_id ]; then |
290 |
VOL_ID="$dir/vol_id -u" |
291 |
VOL_TYPE="$dir/vol_id -t" |
292 |
fi |
293 |
done |
294 |
fi |
295 |
|
296 |
if [ -z "$VOL_ID" ]; then |
297 |
log_error "vol_id or blkid not found, please install udev or util-linux" |
298 |
exit 1 |
299 |
fi |
300 |
|
301 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |