This repository has been archived by the owner on Aug 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qemu-nbd
executable file
·362 lines (311 loc) · 10 KB
/
qemu-nbd
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/sh
# Copyright 2015 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
usage() {
cat 1>&2 <<EOF
Invalid usage. Usage:
$DRIVER init
$DRIVER waitforattach <json params>
$DRIVER mountdevice <mount dir> <mount device> <json params>
$DRIVER unmountdevice <mount dir>
EOF
exit 1
}
err() {
printf "$*" 1>&2
}
log() {
printf "$*" >&1
}
getdevice() {
# Find and connect new nbd device
if [ -z "${NBDDEVICE}" ]; then
local NBDNUMBERS="$(ls -1 /dev/nbd[0-9]* | grep -o [0-9]* | sort -h)"
for i in $NBDNUMBERS; do
if ! lsblk "/dev/nbd$i" 1>/dev/null 2>/dev/null && ! ps x | grep -q "\[jbd2/nbd$i-8\]" && mkfifo /run/lock/qemu-nbd-nbd$i 2> /dev/null; then
local NBDDEVICE="/dev/nbd$i"
break
fi
done
fi
echo "$NBDDEVICE"
}
dofilesystem() {
[ "$FSTYPE" = "" ] && FSTYPE="ext4"
[ "$MMP_INTERVAL" = null ] && MMP_INTERVAL=5
[ "$FSCK_OPTIONS" = null ] && FSCK_OPTIONS="a"
# Calculate multimount protection options
case "$MMP" in false|no|0)
if [ "${FSTYPE}" = "ext4" ]; then
MMP_OPT="-O mmp -E mmp_update_interval=${MMP_INTERVAL}"
fi
;; esac
VOLFSTYPE=`blkid -o udev "$1" 2>/dev/null|grep "ID_FS_TYPE"|cut -d"=" -f2`
if [ "${VOLFSTYPE}" = "" ]; then
# Make filesystem
mkfs -t "${FSTYPE}" ${MMP_OPT} "${1}" >/dev/null 2>&1
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failure\", \"message\": \"Failed to create fs ${FSTYPE} on device ${1}\"}"
exit 1
fi
else
# Do filesystem check
case "$FSCK" in false|no|0)
fsck -${FSCK_OPTIONS} ${1} 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failure\", \"message\": \"Fsck -${FSCK_OPTIONS} check for ${1} was failed\"}"
exit 1
fi
;; esac
fi
}
attach() {
log "{\"status\": \"Success\"}"
exit 0
}
detach() {
log "{\"status\": \"Success\"}"
exit 0
}
waitforattach() {
FSTYPE=$(echo "$1" | jq -r '.["kubernetes.io/fsType"]')
MMP=$(echo "$1" | jq -r '.mmp')
SIZE=$(echo "$1" | jq -r '.size' | tr '[:lower:]' '[:upper:]')
FSCK=$(echo "$1" | jq -r '.fsck')
MMP_INTERVAL=$(echo "$1" | jq -r '.mmpUpdateInterval')
FSCK_OPTIONS=$(echo "$1" | jq -r '.fsckOptions')
ALLOCATE=$(echo "$1" | jq -r '.allocate')
# Get driver specific options
if [ "$DRIVER" = "sheepdog" ]; then
for OPTION in prealloc hyper copies address address port block_size_shift; do
VALUE="$(echo "$1" | jq -r ".$OPTION")"
export "$(echo $OPTION | tr '[:lower:]' '[:upper:]')=$VALUE"
if [ "$VALUE" != "null" ]; then
export DOG_CREATE_OPTS="$DOG_CREATE_OPTS --${OPTION}=${VALUE}"
fi
done
VDINAME=$(echo $1 | jq -r '.vdiname')
else
SHARE=$(echo "$1" | jq -r '.share')
FILE=$(echo "$1" | jq -r '.file')
if [ "${SHARE}" != "null" ]; then
FILE="${SHARE}/${FILE}"
fi
fi
if [ "$DRIVER" != "sheepdog" ]; then
IMAGE_URL="$FILE"
elif [ "$ADDRESS" != null ] && [ "$PORT" = null ]; then
IMAGE_URL="sheepdog://${ADDRESS}/${VDINAME}"
elif [ "$ADDRESS" != null ] && [ "$PORT" != null ]; then
IMAGE_URL="sheepdog://${ADDRESS}:${PORT}/${VDINAME}"
elif [ "$ADDRESS" = null ] && [ "$PORT" != null ]; then
IMAGE_URL="sheepdog://:${PORT}/${VDINAME}"
else
IMAGE_URL="sheepdog:${VDINAME}"
fi
# Check is SHARE mountpoint
if [ "$DRIVER" != "sheepdog" ] && [ "${SHARE}" != "null" ]; then
if ! mountpoint -q "${SHARE}"; then
err "{ \"status\": \"Failed\", \"message\": \"${SHARE} is not mounted\"}"
exit 0
fi
fi
# Check is image exist
if [ "$DRIVER" != "sheepdog" ]; then
if [ ! -f "${FILE}" ]; then
IMAGE_EXIST=0
else
IMAGE_EXIST=1
fi
else
QEMU_IMG_INFO_STDERR="$(qemu-img info "${IMAGE_URL}" 2>&1 1>/dev/null)"
if echo "$QEMU_IMG_INFO_STDERR" | grep -q 'No vdi found'; then
IMAGE_EXIST=0
elif [ -n "$QEMU_IMG_INFO_STDERR" ]; then
err "{ \"status\": \"Failure\", \"message\": \"${QEMU_IMG_INFO_STDERR}\"}"
exit 1
else
IMAGE_EXIST=1
fi
fi
# Allocate image
if [ "$IMAGE_EXIST" = 0 ]; then
if [ -z "${SIZE}" ]; then
err "{\"status\": \"Failure\", \"message\": \"${IMAGE_URL} does not exist\"}"
exit 1
fi
if [ "$DRIVER" != "sheepdog" ]; then
mkdir -p "$(dirname "${FILE}")"
truncate -s ${SIZE} "${FILE}"
if [ $? -ne 0 ]; then
err "{\"status\": \"Failure\", \"message\": \"Can not create file ${FILE}\"}"
exit 1
fi
else
dog vdi create $DOG_CREATE_OPTS "$VDINAME" "$SIZE"
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failure\", \"message\": \"Failed to create vdi ${IMAGE_URL}\"}"
exit 1
fi
fi
fi
# Do filesystem check
if [ "$DRIVER" != "sheepdog" ]; then
dofilesystem "$FILE"
fi
# Skip device setting when loop
if [ "$DRIVER" = "loop" ]; then
log "{\"status\": \"Success\", \"device\":\"${FILE}\"}"
exit 0
fi
# Check for qemu binaries
for binary in qemu-nbd qemu-img; do
if ! command -v $binary >/dev/null 2>&1; then
err "{ \"status\": \"Failure\", \"message\": \"'$binary' binary not found. Please install qemu-utils package before using this driver\"}"
exit 1
fi
done
# Load nbd module
modprobe nbd 2> /dev/null
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failure\", \"message\": \"Failed to load nbd module\"}"
exit 1
fi
# Check is already attached
NBDDEVICE="$(ps aux | sed -n "s|.*qemu-nbd -t -f raw -c \(/dev/nbd[0-9]\+\) ${IMAGE_URL}\$|\1|p" | head -n1)"
if [ ! -z "${NBDDEVICE}" ]; then
log "{\"status\": \"Success\", \"device\":\"${NBDDEVICE}\"}"
exit 0
fi
# Get free nbd device
NBDDEVICE="$(getdevice)"
if [ -z "${NBDDEVICE}" ]; then
err "{\"status\": \"Failure\", \"message\": \"Free nbd device was not found\"}"
exit 1
fi
# Setup nbd device
qemu-nbd -t -f raw -c "${NBDDEVICE}" "${IMAGE_URL}" 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failure\", \"message\": \"Failed to setup nbd device "${NBDDEVICE}" from ${FILE}\"}"
exit 1
fi
# Do filesystem check
if [ "$DRIVER" = "sheepdog" ]; then
dofilesystem "$NBDDEVICE"
fi
log "{\"status\": \"Success\", \"device\":\"${NBDDEVICE}\"}"
exit 0
}
mountdevice() {
MNTPATH=$1
DEVICE=$2
MOUNT_OPTIONS=$(echo $3 | jq -r '.mount_options')
[ "$MOUNT_OPTIONS" = null ] && MOUNT_OPTIONS=""
if [ "$DRIVER" = "loop" ]; then
MOUNT_OPTIONS="loop,$MOUNTOPTIONS"
fi
# Create mountpoint
mkdir -p "${MNTPATH}" 1>/dev/null 2>/dev/null
# Check is already mounted
if mountpoint -q "${MNTPATH}"; then
log "{\"status\": \"Success\"}"
exit 0
fi
# Mount device
mount -o "${MOUNT_OPTIONS}" "${DEVICE}" "${MNTPATH}" 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failure\", \"message\": \"Failed to mount device ${DEVICE} at ${MNTPATH}\"}"
exit 1
fi
log "{\"status\": \"Success\"}"
exit 0
}
unmountdevice() {
MNTPATH=$1
DEVICE=$(mount | awk "\$3 == \"$(readlink -f $MNTPATH)\" {print \$1}" | head -n 1)
if [ ! -d ${MNTPATH} ]; then
log "{\"status\": \"Success\"}"
exit 0
fi
if ! mountpoint -q "${MNTPATH}"; then
log "{\"status\": \"Success\"}"
exit 0
fi
umount ${MNTPATH} 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failed\", \"message\": \"Failed to unmount volume at ${MNTPATH}\"}"
exit 1
fi
if [ "$DRIVER" != "loop" ]; then
sleep 1
qemu-nbd -d ${DEVICE} 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
err "{ \"status\": \"Failure\", \"message\": \"Failed to detach device ${DEVICE}\"}"
exit 1
fi
fi
log "{\"status\": \"Success\"}"
exit 0
}
PATH="$PATH:$(dirname "$(readlink -f "$0")")"
DRIVER="$(basename $0)"
OPERATION=$1
# enable debug
#echo "$DRIVER $@" >> /tmp/flex-qemu-nbd.log
case "${DRIVER}" in
loop|qemu-nbd|nbd|sheepdog)
true
;;
*)
err "{ \"status\": \"Failure\", \"message\": \"'${DRIVER}' wrong name for driver\"}"
exit 1
;;
esac
if ! command -v jq >/dev/null 2>&1; then
err "{ \"status\": \"Failure\", \"message\": \"'jq' binary not found. Please install jq package before using this driver\"}"
exit 1
fi
if [ "$OPERATION" = "init" ]; then
# Disable kernel panic on oops due:
# http://lkml.iu.edu/hypermail/linux/kernel/1509.2/04313.html
(echo 0 > /proc/sys/kernel/panic_on_oops) 2>/dev/null || true
log "{\"status\":\"Success\",\"capabilities\":{\"attach\":true}}"
exit 0
fi
if [ $# -lt 2 ]; then
usage
fi
shift
case "$OPERATION" in
attach)
attach $*
;;
detach)
detach $*
;;
waitforattach)
waitforattach $*
;;
mountdevice)
mountdevice $*
;;
unmountdevice)
unmountdevice $*
;;
*)
log "{ \"status\": \"Not supported\" }"
exit 0
;;
esac
exit 1