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