root / common.sh.in @ 77449e7c
History | View | Annotate | Download (8.1 kB)
1 | 79224631 | Lance Albertson | # |
---|---|---|---|
2 | 79224631 | Lance Albertson | |
3 | 79224631 | Lance Albertson | # Copyright (C) 2007, 2008, 2009 Google Inc. |
4 | 79224631 | Lance Albertson | # |
5 | 79224631 | Lance Albertson | # This program is free software; you can redistribute it and/or modify |
6 | 79224631 | Lance Albertson | # it under the terms of the GNU General Public License as published by |
7 | 79224631 | Lance Albertson | # the Free Software Foundation; either version 2 of the License, or |
8 | 79224631 | Lance Albertson | # (at your option) any later version. |
9 | 79224631 | Lance Albertson | # |
10 | 79224631 | Lance Albertson | # This program is distributed in the hope that it will be useful, but |
11 | 79224631 | Lance Albertson | # WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | 79224631 | Lance Albertson | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | 79224631 | Lance Albertson | # General Public License for more details. |
14 | 79224631 | Lance Albertson | # |
15 | 79224631 | Lance Albertson | # You should have received a copy of the GNU General Public License |
16 | 79224631 | Lance Albertson | # along with this program; if not, write to the Free Software |
17 | 79224631 | Lance Albertson | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | 79224631 | Lance Albertson | # 02110-1301, USA. |
19 | 79224631 | Lance Albertson | |
20 | 8d0132dc | Lance Albertson | AWK="@AWK@" |
21 | 8d0132dc | Lance Albertson | DUMP="@DUMP@" |
22 | 8d0132dc | Lance Albertson | LOSETUP="@LOSETUP@" |
23 | 8d0132dc | Lance Albertson | KPARTX="@KPARTX@" |
24 | 8d0132dc | Lance Albertson | SFDISK="@SFDISK@" |
25 | ab712029 | Lance Albertson | QEMU_IMG="@QEMU_IMG@" |
26 | 8d0132dc | Lance Albertson | MKDIR_P="@MKDIR_P@" |
27 | 8d0132dc | Lance Albertson | |
28 | 79224631 | Lance Albertson | CLEANUP=( ) |
29 | 79224631 | Lance Albertson | |
30 | 79224631 | Lance Albertson | log_error() { |
31 | 79224631 | Lance Albertson | echo "$@" >&2 |
32 | 79224631 | Lance Albertson | } |
33 | 79224631 | Lance Albertson | |
34 | e7a2d1ad | Lance Albertson | debug() { |
35 | e7a2d1ad | Lance Albertson | [ "$IMAGE_DEBUG" == "1" ] && $@ || : |
36 | e7a2d1ad | Lance Albertson | } |
37 | e7a2d1ad | Lance Albertson | |
38 | 79224631 | Lance Albertson | get_api5_arguments() { |
39 | 79224631 | Lance Albertson | GETOPT_RESULT=$* |
40 | 79224631 | Lance Albertson | # Note the quotes around `$TEMP': they are essential! |
41 | 79224631 | Lance Albertson | eval set -- "$GETOPT_RESULT" |
42 | 79224631 | Lance Albertson | while true; do |
43 | 79224631 | Lance Albertson | case "$1" in |
44 | 79224631 | Lance Albertson | -i|-n) instance=$2; shift 2;; |
45 | 79224631 | Lance Albertson | |
46 | 79224631 | Lance Albertson | -o) old_name=$2; shift 2;; |
47 | 79224631 | Lance Albertson | |
48 | 79224631 | Lance Albertson | -b) blockdev=$2; shift 2;; |
49 | 79224631 | Lance Albertson | |
50 | 79224631 | Lance Albertson | -s) swapdev=$2; shift 2;; |
51 | 79224631 | Lance Albertson | |
52 | 79224631 | Lance Albertson | --) shift; break;; |
53 | 79224631 | Lance Albertson | |
54 | 79224631 | Lance Albertson | *) log_error "Internal error!" >&2; exit 1;; |
55 | 79224631 | Lance Albertson | esac |
56 | 79224631 | Lance Albertson | done |
57 | 79224631 | Lance Albertson | if [ -z "$instance" -o -z "$blockdev" ]; then |
58 | 79224631 | Lance Albertson | log_error "Missing OS API Argument (-i, -n, or -b)" |
59 | 79224631 | Lance Albertson | exit 1 |
60 | 79224631 | Lance Albertson | fi |
61 | 79224631 | Lance Albertson | if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then |
62 | 79224631 | Lance Albertson | log_error "Missing OS API Argument -s (swapdev)" |
63 | 79224631 | Lance Albertson | exit 1 |
64 | 79224631 | Lance Albertson | fi |
65 | 79224631 | Lance Albertson | if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then |
66 | 79224631 | Lance Albertson | log_error "Missing OS API Argument -o (old_name)" |
67 | 79224631 | Lance Albertson | exit 1 |
68 | 79224631 | Lance Albertson | fi |
69 | 79224631 | Lance Albertson | } |
70 | 79224631 | Lance Albertson | |
71 | 79224631 | Lance Albertson | get_api10_arguments() { |
72 | 79224631 | Lance Albertson | if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then |
73 | 79224631 | Lance Albertson | log_error "Missing OS API Variable:" |
74 | 79224631 | Lance Albertson | log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)" |
75 | 79224631 | Lance Albertson | exit 1 |
76 | 79224631 | Lance Albertson | fi |
77 | 79224631 | Lance Albertson | instance=$INSTANCE_NAME |
78 | 79224631 | Lance Albertson | if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then |
79 | 79224631 | Lance Albertson | log_error "At least one disk is needed" |
80 | 79224631 | Lance Albertson | exit 1 |
81 | 79224631 | Lance Albertson | fi |
82 | 79224631 | Lance Albertson | if [ "$SCRIPT_NAME" = "export" ]; then |
83 | 79224631 | Lance Albertson | if [ -z "$EXPORT_DEVICE" ]; then |
84 | 79224631 | Lance Albertson | log_error "Missing OS API Variable EXPORT_DEVICE" |
85 | 79224631 | Lance Albertson | fi |
86 | 79224631 | Lance Albertson | blockdev=$EXPORT_DEVICE |
87 | 79224631 | Lance Albertson | elif [ "$SCRIPT_NAME" = "import" ]; then |
88 | 79224631 | Lance Albertson | if [ -z "$IMPORT_DEVICE" ]; then |
89 | 79224631 | Lance Albertson | log_error "Missing OS API Variable IMPORT_DEVICE" |
90 | 79224631 | Lance Albertson | fi |
91 | 79224631 | Lance Albertson | blockdev=$IMPORT_DEVICE |
92 | 79224631 | Lance Albertson | else |
93 | 79224631 | Lance Albertson | blockdev=$DISK_0_PATH |
94 | 79224631 | Lance Albertson | fi |
95 | 79224631 | Lance Albertson | if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then |
96 | 79224631 | Lance Albertson | log_error "Missing OS API Variable OLD_INSTANCE_NAME" |
97 | 79224631 | Lance Albertson | fi |
98 | 79224631 | Lance Albertson | old_name=$OLD_INSTANCE_NAME |
99 | 79224631 | Lance Albertson | } |
100 | 79224631 | Lance Albertson | |
101 | f6f223b8 | Lance Albertson | get_os_type() { |
102 | f6f223b8 | Lance Albertson | if [ -e ${TARGET}/etc/redhat-release ] ; then |
103 | f6f223b8 | Lance Albertson | OS_TYPE="redhat" |
104 | f6f223b8 | Lance Albertson | elif [ -e ${TARGET}/etc/debian_version ] ; then |
105 | f6f223b8 | Lance Albertson | OS_TYPE="debian" |
106 | f6f223b8 | Lance Albertson | elif [ -e ${TARGET}/etc/gentoo-release ] ; then |
107 | f6f223b8 | Lance Albertson | OS_TYPE="gentoo" |
108 | f6f223b8 | Lance Albertson | elif [ -e ${TARGET}/etc/SuSE-release ] ; then |
109 | f6f223b8 | Lance Albertson | OS_TYPE="suse" |
110 | f6f223b8 | Lance Albertson | fi |
111 | f6f223b8 | Lance Albertson | } |
112 | f6f223b8 | Lance Albertson | |
113 | 79224631 | Lance Albertson | format_disk0() { |
114 | 77449e7c | Lance Albertson | local sfdisk_cmd="$SFDISK -uM -H 255 -S 63 --quiet --Linux \"$1\"" |
115 | 77449e7c | Lance Albertson | if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then |
116 | e21b8802 | Lance Albertson | # Create three partitions: |
117 | e21b8802 | Lance Albertson | # 1 - 100MB /boot, bootable |
118 | e21b8802 | Lance Albertson | # 2 - Size of Memory, swap |
119 | e21b8802 | Lance Albertson | # 3 - Rest |
120 | 77449e7c | Lance Albertson | $sfdisk_cmd <<EOF |
121 | 22b570d6 | Lance Albertson | ,100,L,* |
122 | 240140b7 | Lance Albertson | ,$INSTANCE_BE_memory,S |
123 | 22b570d6 | Lance Albertson | ,,L |
124 | 79224631 | Lance Albertson | EOF |
125 | 77449e7c | Lance Albertson | elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then |
126 | e21b8802 | Lance Albertson | # Create two partitions: |
127 | e21b8802 | Lance Albertson | # 1 - 100MB /boot, bootable |
128 | e21b8802 | Lance Albertson | # 2 - Rest |
129 | 77449e7c | Lance Albertson | $sfdisk_cmd <<EOF |
130 | e21b8802 | Lance Albertson | ,100,L,* |
131 | e21b8802 | Lance Albertson | ,,L |
132 | e21b8802 | Lance Albertson | EOF |
133 | 77449e7c | Lance Albertson | elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then |
134 | 77449e7c | Lance Albertson | # Create two partitions: |
135 | 77449e7c | Lance Albertson | # 1 - Size of Memory, swap |
136 | 77449e7c | Lance Albertson | # 2 - Rest |
137 | 77449e7c | Lance Albertson | $sfdisk_cmd <<EOF |
138 | 77449e7c | Lance Albertson | ,$INSTANCE_BE_memory,S |
139 | 77449e7c | Lance Albertson | ,,L |
140 | 77449e7c | Lance Albertson | EOF |
141 | 77449e7c | Lance Albertson | elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then |
142 | 77449e7c | Lance Albertson | # Create two partitions: |
143 | 77449e7c | Lance Albertson | # 1 - Whole |
144 | 77449e7c | Lance Albertson | $sfdisk_cmd <<EOF |
145 | 77449e7c | Lance Albertson | ,,L |
146 | 77449e7c | Lance Albertson | EOF |
147 | e21b8802 | Lance Albertson | fi |
148 | 79224631 | Lance Albertson | } |
149 | 79224631 | Lance Albertson | |
150 | cc927a76 | Lance Albertson | mkfs_disk0() { |
151 | cc927a76 | Lance Albertson | # Format / |
152 | 77449e7c | Lance Albertson | mkfs.ext3 -Fq -L / $root_dev |
153 | 77449e7c | Lance Albertson | # Format /boot |
154 | 77449e7c | Lance Albertson | if [ -n "${boot_dev}" ] ; then |
155 | 77449e7c | Lance Albertson | mkfs.ext3 -Fq -L /boot $boot_dev |
156 | 77449e7c | Lance Albertson | fi |
157 | 77449e7c | Lance Albertson | # Format swap |
158 | 77449e7c | Lance Albertson | if [ -n "${swap_dev}" ] ; then |
159 | cc927a76 | Lance Albertson | # Format swap |
160 | 70054ca5 | Lance Albertson | mkswap -f $swap_dev |
161 | cc927a76 | Lance Albertson | fi |
162 | cc927a76 | Lance Albertson | } |
163 | cc927a76 | Lance Albertson | |
164 | b05b1ab6 | Lance Albertson | mount_disk0() { |
165 | 77449e7c | Lance Albertson | local target=$1 |
166 | 3173a4bc | Lance Albertson | mount $root_dev $target |
167 | 3173a4bc | Lance Albertson | CLEANUP+=("umount $target") |
168 | 77449e7c | Lance Albertson | if [ -n "${boot_dev}" ] ; then |
169 | 77449e7c | Lance Albertson | $MKDIR_P $target/boot |
170 | 77449e7c | Lance Albertson | mount $boot_dev $target/boot |
171 | 77449e7c | Lance Albertson | CLEANUP+=("umount $target/boot") |
172 | 77449e7c | Lance Albertson | fi |
173 | b05b1ab6 | Lance Albertson | } |
174 | b05b1ab6 | Lance Albertson | |
175 | 79224631 | Lance Albertson | map_disk0() { |
176 | 77449e7c | Lance Albertson | blockdev="$1" |
177 | 77449e7c | Lance Albertson | filesystem_dev_base=`$KPARTX -l -p- $blockdev | \ |
178 | 77449e7c | Lance Albertson | grep -m 1 -- "-1.*$blockdev" | \ |
179 | 77449e7c | Lance Albertson | $AWK '{print $1}'` |
180 | 77449e7c | Lance Albertson | if [ -z "$filesystem_dev_base" ]; then |
181 | 77449e7c | Lance Albertson | log_error "Cannot interpret kpartx output and get partition mapping" |
182 | 77449e7c | Lance Albertson | exit 1 |
183 | 77449e7c | Lance Albertson | fi |
184 | 77449e7c | Lance Albertson | $KPARTX -a -p- $blockdev > /dev/null |
185 | 77449e7c | Lance Albertson | filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}" |
186 | 77449e7c | Lance Albertson | if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then |
187 | 77449e7c | Lance Albertson | log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base" |
188 | 77449e7c | Lance Albertson | exit 1 |
189 | 77449e7c | Lance Albertson | fi |
190 | 77449e7c | Lance Albertson | if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then |
191 | 77449e7c | Lance Albertson | boot_dev="${filesystem_base_dev}-1" |
192 | 77449e7c | Lance Albertson | swap_dev="${filesystem_base_dev}-2" |
193 | 77449e7c | Lance Albertson | root_dev="${filesystem_base_dev}-3" |
194 | 77449e7c | Lance Albertson | elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then |
195 | 77449e7c | Lance Albertson | boot_dev="${filesystem_base_dev}-1" |
196 | 77449e7c | Lance Albertson | root_dev="${filesystem_base_dev}-2" |
197 | 77449e7c | Lance Albertson | elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then |
198 | 77449e7c | Lance Albertson | swap_dev="${filesystem_base_dev}-1" |
199 | 77449e7c | Lance Albertson | root_dev="${filesystem_base_dev}-2" |
200 | 77449e7c | Lance Albertson | elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then |
201 | 77449e7c | Lance Albertson | root_dev="${filesystem_base_dev}-1" |
202 | 77449e7c | Lance Albertson | fi |
203 | 77449e7c | Lance Albertson | echo "$filesystem_dev" |
204 | 79224631 | Lance Albertson | } |
205 | 79224631 | Lance Albertson | |
206 | 79224631 | Lance Albertson | unmap_disk0() { |
207 | 8d0132dc | Lance Albertson | $KPARTX -d -p- $1 |
208 | 79224631 | Lance Albertson | } |
209 | 79224631 | Lance Albertson | |
210 | 79224631 | Lance Albertson | cleanup() { |
211 | 79224631 | Lance Albertson | if [ ${#CLEANUP[*]} -gt 0 ]; then |
212 | 79224631 | Lance Albertson | LAST_ELEMENT=$((${#CLEANUP[*]}-1)) |
213 | 79224631 | Lance Albertson | REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0) |
214 | 79224631 | Lance Albertson | for i in $REVERSE_INDEXES; do |
215 | 79224631 | Lance Albertson | ${CLEANUP[$i]} |
216 | 79224631 | Lance Albertson | done |
217 | 79224631 | Lance Albertson | fi |
218 | 79224631 | Lance Albertson | } |
219 | 79224631 | Lance Albertson | |
220 | 79224631 | Lance Albertson | trap cleanup EXIT |
221 | 79224631 | Lance Albertson | |
222 | c4661256 | Lance Albertson | DEFAULT_FILE="@DEFAULTS_DIR@/ganeti-instance-image" |
223 | 79224631 | Lance Albertson | if [ -f "$DEFAULT_FILE" ]; then |
224 | 79224631 | Lance Albertson | . "$DEFAULT_FILE" |
225 | 79224631 | Lance Albertson | fi |
226 | 79224631 | Lance Albertson | |
227 | 79224631 | Lance Albertson | # note: we don't set a default mirror since debian and ubuntu have |
228 | 79224631 | Lance Albertson | # different defaults, and it's better to use the default |
229 | 79224631 | Lance Albertson | |
230 | 79224631 | Lance Albertson | # only if the user want to specify a mirror in the defaults file we |
231 | 79224631 | Lance Albertson | # will use it, this declaration is to make sure the variable is set |
232 | fcce5224 | Lance Albertson | : ${CDINSTALL:="yes"} |
233 | e21b8802 | Lance Albertson | : ${SWAP:="yes"} |
234 | 134c11c3 | Lance Albertson | : ${IMAGE_NAME:=""} |
235 | b05b1ab6 | Lance Albertson | : ${IMAGE_TYPE:="qemu"} |
236 | 79224631 | Lance Albertson | : ${ARCH:=""} |
237 | f5aa7725 | Lance Albertson | : ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image.d"} |
238 | f5aa7725 | Lance Albertson | : ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"} |
239 | 2e0988ec | Lance Albertson | : ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"} |
240 | 611fa6b0 | Lance Albertson | : ${IMAGE_DEBUG:="0"} |
241 | f7959feb | Lance Albertson | |
242 | 79224631 | Lance Albertson | SCRIPT_NAME=$(basename $0) |
243 | 77449e7c | Lance Albertson | KERNEL_PATH="$INSTANCE_HV_kernel_path" |
244 | 79224631 | Lance Albertson | |
245 | 103d261e | Lance Albertson | if [ -f /sbin/blkid -a -x /sbin/blkid ]; then |
246 | 103d261e | Lance Albertson | VOL_ID="/sbin/blkid -o value -s UUID" |
247 | 103d261e | Lance Albertson | VOL_TYPE="/sbin/blkid -o value -s TYPE" |
248 | 103d261e | Lance Albertson | else |
249 | 103d261e | Lance Albertson | for dir in /lib/udev /sbin; do |
250 | 103d261e | Lance Albertson | if [ -f $dir/vol_id -a -x $dir/vol_id ]; then |
251 | 103d261e | Lance Albertson | VOL_ID="$dir/vol_id -u" |
252 | 103d261e | Lance Albertson | VOL_TYPE="$dir/vol_id -t" |
253 | 103d261e | Lance Albertson | fi |
254 | 103d261e | Lance Albertson | done |
255 | 103d261e | Lance Albertson | fi |
256 | 103d261e | Lance Albertson | |
257 | 103d261e | Lance Albertson | if [ -z "$VOL_ID" ]; then |
258 | 103d261e | Lance Albertson | log_error "vol_id or blkid not found, please install udev or util-linux" |
259 | 103d261e | Lance Albertson | exit 1 |
260 | 103d261e | Lance Albertson | fi |
261 | 103d261e | Lance Albertson | |
262 | 103d261e | Lance Albertson | |
263 | 79224631 | Lance Albertson | if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then |
264 | 79224631 | Lance Albertson | OS_API_VERSION=5 |
265 | 79224631 | Lance Albertson | GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"` |
266 | 79224631 | Lance Albertson | if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi |
267 | 79224631 | Lance Albertson | get_api5_arguments $GETOPT_RESULT |
268 | 79224631 | Lance Albertson | elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then |
269 | 79224631 | Lance Albertson | get_api10_arguments |
270 | 79224631 | Lance Albertson | else |
271 | 79224631 | Lance Albertson | log_error "Unknown OS API VERSION $OS_API_VERSION" |
272 | 79224631 | Lance Albertson | exit 1 |
273 | 79224631 | Lance Albertson | fi |
274 | 79224631 | Lance Albertson | |
275 | 79224631 | Lance Albertson | if [ -n "$OS_VARIANT" ]; then |
276 | 79224631 | Lance Albertson | if [ ! -d "$VARIANTS_DIR" ]; then |
277 | 79224631 | Lance Albertson | log_error "OS Variants directory $VARIANTS_DIR doesn't exist" |
278 | 79224631 | Lance Albertson | exit 1 |
279 | 79224631 | Lance Albertson | fi |
280 | 79224631 | Lance Albertson | VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf" |
281 | 79224631 | Lance Albertson | if [ -f "$VARIANT_CONFIG" ]; then |
282 | 79224631 | Lance Albertson | . "$VARIANT_CONFIG" |
283 | 79224631 | Lance Albertson | else |
284 | 79224631 | Lance Albertson | if grep -qxF "$OS_VARIANT" variants.list; then |
285 | f5aa7725 | Lance Albertson | log_error "ERROR: instance-image configuration error" |
286 | 79224631 | Lance Albertson | log_error " Published variant $OS_VARIANT is missing its config file" |
287 | 79224631 | Lance Albertson | log_error " Please create $VARIANT_CONFIG or unpublish the variant" |
288 | 79224631 | Lance Albertson | log_error " (by removing $OS_VARIANT from variants.list)" |
289 | 79224631 | Lance Albertson | else |
290 | 79224631 | Lance Albertson | log_error "Unofficial variant $OS_VARIANT is unsupported" |
291 | 79224631 | Lance Albertson | log_error "Most probably this is a user error, forcing a wrong name" |
292 | 79224631 | Lance Albertson | log_error "To support this variant please create file $VARIANT_CONFIG" |
293 | 79224631 | Lance Albertson | fi |
294 | 79224631 | Lance Albertson | exit 1 |
295 | 79224631 | Lance Albertson | fi |
296 | 79224631 | Lance Albertson | fi |