root / common.sh.in @ 79224631
History | View | Annotate | Download (6.1 kB)
1 |
# |
---|---|
2 |
|
3 |
# Copyright (C) 2007, 2008, 2009 Google Inc. |
4 |
# |
5 |
# This program is free software; you can redistribute it and/or modify |
6 |
# it under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 2 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# This program is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
# General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 |
# 02110-1301, USA. |
19 |
|
20 |
CLEANUP=( ) |
21 |
|
22 |
log_error() { |
23 |
echo "$@" >&2 |
24 |
} |
25 |
|
26 |
get_api5_arguments() { |
27 |
GETOPT_RESULT=$* |
28 |
# Note the quotes around `$TEMP': they are essential! |
29 |
eval set -- "$GETOPT_RESULT" |
30 |
while true; do |
31 |
case "$1" in |
32 |
-i|-n) instance=$2; shift 2;; |
33 |
|
34 |
-o) old_name=$2; shift 2;; |
35 |
|
36 |
-b) blockdev=$2; shift 2;; |
37 |
|
38 |
-s) swapdev=$2; shift 2;; |
39 |
|
40 |
--) shift; break;; |
41 |
|
42 |
*) log_error "Internal error!" >&2; exit 1;; |
43 |
esac |
44 |
done |
45 |
if [ -z "$instance" -o -z "$blockdev" ]; then |
46 |
log_error "Missing OS API Argument (-i, -n, or -b)" |
47 |
exit 1 |
48 |
fi |
49 |
if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then |
50 |
log_error "Missing OS API Argument -s (swapdev)" |
51 |
exit 1 |
52 |
fi |
53 |
if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then |
54 |
log_error "Missing OS API Argument -o (old_name)" |
55 |
exit 1 |
56 |
fi |
57 |
} |
58 |
|
59 |
get_api10_arguments() { |
60 |
if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then |
61 |
log_error "Missing OS API Variable:" |
62 |
log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)" |
63 |
exit 1 |
64 |
fi |
65 |
instance=$INSTANCE_NAME |
66 |
if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then |
67 |
log_error "At least one disk is needed" |
68 |
exit 1 |
69 |
fi |
70 |
if [ "$SCRIPT_NAME" = "export" ]; then |
71 |
if [ -z "$EXPORT_DEVICE" ]; then |
72 |
log_error "Missing OS API Variable EXPORT_DEVICE" |
73 |
fi |
74 |
blockdev=$EXPORT_DEVICE |
75 |
elif [ "$SCRIPT_NAME" = "import" ]; then |
76 |
if [ -z "$IMPORT_DEVICE" ]; then |
77 |
log_error "Missing OS API Variable IMPORT_DEVICE" |
78 |
fi |
79 |
blockdev=$IMPORT_DEVICE |
80 |
else |
81 |
blockdev=$DISK_0_PATH |
82 |
fi |
83 |
if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then |
84 |
log_error "Missing OS API Variable OLD_INSTANCE_NAME" |
85 |
fi |
86 |
old_name=$OLD_INSTANCE_NAME |
87 |
} |
88 |
|
89 |
format_disk0() { |
90 |
# Create one big partition, and make it bootable |
91 |
# some versions of sfdisk need manual specification of |
92 |
# head/sectors for devices such as drbd which don't |
93 |
# report geometry |
94 |
sfdisk -H 255 -S 63 --quiet --Linux "$1" <<EOF |
95 |
0,,L,* |
96 |
EOF |
97 |
} |
98 |
|
99 |
map_disk0() { |
100 |
blockdev="$1" |
101 |
filesystem_dev_base=`kpartx -l -p- $blockdev | \ |
102 |
grep -m 1 -- "-1.*$blockdev" | \ |
103 |
awk '{print $1}'` |
104 |
if [ -z "$filesystem_dev_base" ]; then |
105 |
log_error "Cannot interpret kpartx output and get partition mapping" |
106 |
exit 1 |
107 |
fi |
108 |
kpartx -a -p- $blockdev > /dev/null |
109 |
filesystem_dev="/dev/mapper/$filesystem_dev_base" |
110 |
if [ ! -b "$filesystem_dev" ]; then |
111 |
log_error "Can't find kpartx mapped partition: $filesystem_dev" |
112 |
exit 1 |
113 |
fi |
114 |
echo "$filesystem_dev" |
115 |
} |
116 |
|
117 |
unmap_disk0() { |
118 |
kpartx -d -p- $1 |
119 |
} |
120 |
|
121 |
cleanup() { |
122 |
if [ ${#CLEANUP[*]} -gt 0 ]; then |
123 |
LAST_ELEMENT=$((${#CLEANUP[*]}-1)) |
124 |
REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0) |
125 |
for i in $REVERSE_INDEXES; do |
126 |
${CLEANUP[$i]} |
127 |
done |
128 |
fi |
129 |
} |
130 |
|
131 |
trap cleanup EXIT |
132 |
|
133 |
DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap" |
134 |
if [ -f "$DEFAULT_FILE" ]; then |
135 |
. "$DEFAULT_FILE" |
136 |
fi |
137 |
|
138 |
# note: we don't set a default mirror since debian and ubuntu have |
139 |
# different defaults, and it's better to use the default |
140 |
|
141 |
# only if the user want to specify a mirror in the defaults file we |
142 |
# will use it, this declaration is to make sure the variable is set |
143 |
: ${MIRROR:=""} |
144 |
: ${PROXY:=""} |
145 |
: ${SUITE:="lenny"} |
146 |
: ${ARCH:=""} |
147 |
: ${EXTRA_PKGS:=""} |
148 |
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-debootstrap.d"} |
149 |
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-debootstrap/variants"} |
150 |
: ${GENERATE_CACHE:="yes"} |
151 |
: ${CLEAN_CACHE:="14"} # number of days to keep a cache file |
152 |
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then |
153 |
DEFAULT_PARTITION_STYLE="none" |
154 |
else |
155 |
DEFAULT_PARTITION_STYLE="msdos" |
156 |
fi |
157 |
: ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style |
158 |
|
159 |
CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap" |
160 |
|
161 |
SCRIPT_NAME=$(basename $0) |
162 |
|
163 |
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then |
164 |
VOL_ID="/sbin/blkid -o value -s UUID" |
165 |
VOL_TYPE="/sbin/blkid -o value -s TYPE" |
166 |
else |
167 |
for dir in /lib/udev /sbin; do |
168 |
if [ -f $dir/vol_id -a -x $dir/vol_id ]; then |
169 |
VOL_ID="$dir/vol_id -u" |
170 |
VOL_TYPE="$dir/vol_id -t" |
171 |
fi |
172 |
done |
173 |
fi |
174 |
|
175 |
if [ -z "$VOL_ID" ]; then |
176 |
log_error "vol_id or blkid not found, please install udev or util-linux" |
177 |
exit 1 |
178 |
fi |
179 |
|
180 |
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then |
181 |
OS_API_VERSION=5 |
182 |
GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"` |
183 |
if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi |
184 |
get_api5_arguments $GETOPT_RESULT |
185 |
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then |
186 |
get_api10_arguments |
187 |
else |
188 |
log_error "Unknown OS API VERSION $OS_API_VERSION" |
189 |
exit 1 |
190 |
fi |
191 |
|
192 |
if [ -n "$OS_VARIANT" ]; then |
193 |
if [ ! -d "$VARIANTS_DIR" ]; then |
194 |
log_error "OS Variants directory $VARIANTS_DIR doesn't exist" |
195 |
exit 1 |
196 |
fi |
197 |
VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf" |
198 |
if [ -f "$VARIANT_CONFIG" ]; then |
199 |
. "$VARIANT_CONFIG" |
200 |
else |
201 |
if grep -qxF "$OS_VARIANT" variants.list; then |
202 |
log_error "ERROR: instance-debootstrap configuration error" |
203 |
log_error " Published variant $OS_VARIANT is missing its config file" |
204 |
log_error " Please create $VARIANT_CONFIG or unpublish the variant" |
205 |
log_error " (by removing $OS_VARIANT from variants.list)" |
206 |
else |
207 |
log_error "Unofficial variant $OS_VARIANT is unsupported" |
208 |
log_error "Most probably this is a user error, forcing a wrong name" |
209 |
log_error "To support this variant please create file $VARIANT_CONFIG" |
210 |
fi |
211 |
exit 1 |
212 |
fi |
213 |
fi |
214 |
|