-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic-pv-provisioner
executable file
·323 lines (274 loc) · 7.9 KB
/
static-pv-provisioner
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/bash
#
# Note: if you change HOSTROOTMOUNT variable, you'll have to update the volume's path in the daemonset.yaml
#
HOSTROOTMOUNT=/host
function syntax() {
>&2 echo "Syntax: $0 -A <add|remove|list-local> ..."
}
function get_ts() {
TS=$(date +"%Y-%m-%d %H:%M:%S")
}
function prefixed() {
get_ts
echo "[$TS] [provisioner] "$*
}
function error() {
>&2 prefixed "ERROR: "$*
}
function info() {
>&2 prefixed "INFO: "$*
}
function debug() {
if [ "$DEBUG" = "true" ] ; then
>&2 prefixed "DEBUG: "$*
fi
}
#
# Just in case we need to sanitize the LV name
#
function sanitize_lv_name() {
_PVNAME="$1"
echo "$XPVNAME"
}
#
# Units in K8s and LV use different syntax. See:
# https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-memory
# https://linux.die.net/man/8/lvcreate
#
function sanitize_lv_size() {
_VOLSIZE="$1"
units=$(echo "$_VOLSIZE" | sed 's,^[[:digit:]]\+,,g')
value=$(echo "$_VOLSIZE" | sed 's,^\([[:digit:]]\+\).*$,\1,g')
_SANITIZED=""
case "$units" in
Ti) _SANITIZED=T ;;
Gi) _SANITIZED=G ;;
Mi) _SANITIZED=M ;;
Ki) _SANITIZED=M ;;
esac
echo $value$_SANITIZED
}
function sanitize_systemd_unit_filename() {
local XMOUNTPATH="$1"
echo $XMOUNTPATH | sed 's,-,\\x2d,g; s,/,-,g; s,^-,,g'
}
function defer_mount_systemd() {
local XDEVFILE="$1"
local XMOUNTPATH="$2"
local XFSTYPE="$3"
local XMOUNTOPTS="$4"
local SDFILENAME=$(sanitize_systemd_unit_filename "$XMOUNTPATH")
debug "defer_mount_systemd: Going to create: $HOSTROOTMOUNT/etc/systemd/system/${SDFILENAME}.mount"
cat > "$HOSTROOTMOUNT/etc/systemd/system/${SDFILENAME}.mount" <<EOF
#
# This file is generated by Kubernetes portavita.net/k8s-local-pv provisioner
#
[Unit]
Description=Mount for Kubernetes PersistentVolume
Conflicts=umount.target
[Mount]
What=${XDEVFILE}
Where=${XMOUNTPATH}
Type=${XFSTYPE}
Options=${XMOUNTOPTS}
[Install]
WantedBy=multi-user.target
EOF
[ "$DEBUG" = "true" ] && cat "$HOSTROOTMOUNT/etc/systemd/system/${SDFILENAME}.mount"
info "defer_mount_systemd: Mounting on $XMOUNTPATH (via systemd)"
run_cmd systemctl daemon-reload
run_cmd systemctl start ${SDFILENAME}.mount
run_cmd systemctl enable ${SDFILENAME}.mount
}
function defer_unmount_systemd() {
local XMOUNTPATH="$1"
local SDFILENAME=$(sanitize_systemd_unit_filename "$XMOUNTPATH")
info "defer_unmount_systemd: Un-mounting $XMOUNTPATH (via systemd)"
run_cmd systemctl stop ${SDFILENAME}.mount
run_cmd systemctl disable ${SDFILENAME}.mount
run_cmd systemctl daemon-reload
debug "defer_mount_systemd: Removing $HOSTROOTMOUNT/etc/systemd/system/${SDFILENAME}.mount"
rm -f "$HOSTROOTMOUNT/etc/systemd/system/${SDFILENAME}.mount"
}
function get_mount_path() {
echo "$MOUNTBASEPATH/$XPVNAME"
}
function provisioner_add() {
local XPVNAME="$1"
local XVGNAME="$2"
local XFSTYPE="$3"
local XMOUNTOPTS="$4"
local XVOLSIZE="$5"
local XFSOPTS="$6"
local XCRYPTSECRET="$7"
if [ -n "$XCRYPTSECRET" ] ; then
info "WARNING - Encryption was requested, but it's not supported (yet). Ignoring."
fi
XMOUNTPATH=$(get_mount_path)
if [ -z "$(vgdisplay -s 2>/dev/null | cut -d '"' -f 2 | grep $XVGNAME)" ] ; then
error "No VG found with name: $XVGNAME"
exit 127
fi
_LVNAME=$(sanitize_lv_name "$XPVNAME")
DETECT_LV=$(detect_lv "$XVGNAME" "$_LVNAME")
if [ -n "$DETECT_LV" ] ; then
info "Found an LV already provisioned with name '$DETECT_LV'. Skipping lvcreate"
else
LVSIZE=$(sanitize_lv_size "$XVOLSIZE")
run_cmd lvcreate -W y -Z y --yes -n "$_LVNAME" -L "$LVSIZE" "$XVGNAME" # --addtag k8s-local-pv
fi
LV_DEV_NAME="/dev/$XVGNAME/$_LVNAME"
DETECT_FSTYPE=$(run_cmd_ignore_rc blkid -o value -s TYPE "$LV_DEV_NAME")
if [ "$DETECT_FSTYPE" = "$XFSTYPE" ] ; then
info "Found filesystem of type '$DETECT_FSTYPE' already there. Skipping mkfs..."
else
if [ -n "$XFSOPTS" ] ; then
run_cmd mkfs -t "$XFSTYPE" $XFSOPTS "$LV_DEV_NAME"
else
run_cmd mkfs -t "$XFSTYPE" "$LV_DEV_NAME"
fi
fi
DETECT_MOUNT=$(detect_mount "$LV_DEV_NAME")
if [ -n "$DETECT_MOUNT" ] ; then
info "Found '$LV_DEV_NAME' already mounted on '$DETECT_MOUNT'"
else
defer_mount_systemd "$LV_DEV_NAME" "$XMOUNTPATH" "$XFSTYPE" "$XMOUNTOPTS"
fi
}
function run_cmd_ignore_rc() {
debug "[CMD] "$*
if [ "$DRYRUN" != "true" ] ; then
chroot $HOSTROOTMOUNT $*
fi
}
function run_cmd() {
run_cmd_ignore_rc $*
if [ $? -ne 0 ] ; then
error "Error executing: "$*
exit 126
fi
}
function detect_mount() {
local XLV_DEV_NAME="$1"
# The mount table refers to the devicemapper common path, which we don't have, so let's get it:
LV_DM_NAME=$(run_cmd_ignore_rc dmsetup info "$XLV_DEV_NAME" | grep ^Name: | awk '{print $2}')
run_cmd_ignore_rc grep "^/dev/mapper/${LV_DM_NAME}[[:space:]]" /proc/mounts | awk '{print $2;}'
}
function detect_lv() {
local XVGNAME="$1"
local XLVNAME="$2"
lvdisplay -C 2>/dev/null | tail -n +2 | awk "\$2~/$XVGNAME/{print \$1}" | awk "/^$XLVNAME\$/"
}
function provisioner_remove() {
XPVNAME="$1"
XVGNAME="$2"
XMOUNTPATH=$(get_mount_path)
_LVNAME=$(sanitize_lv_name "$XPVNAME")
LV_DEV_NAME="/dev/$XVGNAME/$_LVNAME"
DETECT_MOUNT=$(detect_mount "$LV_DEV_NAME")
if [ -z "$DETECT_MOUNT" ] ; then
info "Did not find a mount on '$DETECT_MOUNT'. Skipping umount"
else
defer_unmount_systemd "$XMOUNTPATH"
fi
DETECT_LV=$(detect_lv "$XVGNAME" "$_LVNAME")
if [ -z "$DETECT_LV" ] ; then
info "Did not find an LV with name: $DETECT_LV. skipping lvremove"
else
# we could some shredding here.... but :-/
run_cmd lvremove -f /dev/$XVGNAME/$_LVNAME
fi
DETECT_LV=$(run_cmd_ignore_rc find $MOUNTBASEPATH -maxdepth 1 -type d | grep $XMOUNTPATH)
if [ -n "$DETECT_LV" ] ; then
run_cmd "rmdir $XMOUNTPATH"
fi
info "Finished operations on mountpoint '$XMOUNTPATH'."
}
function provisioner_list_local() {
info "Searching for local mountpoints, found:"
run_cmd_ignore_rc find $MOUNTBASEPATH -maxdepth 1 -type d 2>/dev/null | sed "s,^$MOUNTBASEPATH,,g; /^$/ d; s,^/,,g;"
}
DEBUG="false"
CRYPTSECRET=""
DRYRUN="false"
MOUNTBASEPATH="/mnt/pv"
while getopts ":O:T:V:vA:P:N:E:S:F:" opt; do
case $opt in
A)
ACTION="$OPTARG"
debug "ACTION: $ACTION"
;;
E)
CRYPTSECRET="$OPTARG"
debug "CRYPTSECRET: $CRYPTSECRET"
;;
F)
FSOPTS="$OPTARG"
debug "FSOPTS: $FSOPTS"
;;
N)
PVNAME="$OPTARG"
debug "PVNAME: $PVNAME"
;;
O)
MOUNTOPTS="$OPTARG"
debug "MOUNTOPTS: $MOUNTOPTS"
;;
P)
MOUNTBASEPATH="$OPTARG"
debug "MOUNTBASEPATH: $MOUNTBASEPATH"
;;
S)
VOLSIZE="$OPTARG"
debug "VOLSIZE: $VOLSIZE"
;;
T)
FSTYPE="$OPTARG"
debug "FSTYPE: $FSTYPE"
;;
V)
VGNAME="$OPTARG"
debug "VGNAME: $VGNAME"
;;
v)
DEBUG="true"
debug "DEBUG: $DEBUG"
;;
?)
error "Invalid option: -$OPTARG"
syntax
exit 1
;;
:)
error "Option -$OPTARG requires an argument."
syntax
exit 1
;;
esac
done
case "$ACTION" in
add)
if [ -z "$PVNAME" -o -z "$MOUNTOPTS" -o -z "$FSTYPE" -o -z "$VGNAME" -o -z "$VOLSIZE" ] ; then
error "Insufficient parameters."
syntax
exit 1
fi
provisioner_add "$PVNAME" "$VGNAME" "$FSTYPE" "$MOUNTOPTS" "$VOLSIZE" "$FSOPTS" "$CRYPTVOL"
;;
remove)
if [ -z "$PVNAME" -o -z "$VGNAME" ] ; then
error "Insufficient parameters."
syntax
exit 1
fi
provisioner_remove "$PVNAME" "$VGNAME"
;;
list-local)
provisioner_list_local
;;
*)
error "Invalid action: $ACTION. Aborting.."
syntax
exit 1
esac