blob: a831d8451018c22a0bead6e39fb7bf27a51a52d6 (
plain)
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
|
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Please run as root" 1>&2
exit 1
fi
rootdir=`dirname $(readlink -f "${BASH_SOURCE[0]}")`
source ${rootdir}/scripts/lib_nbd_img_handling
progname=$(basename $0)
function usage()
{
cat << HEREDOC
Usage:
Mount Image : $progname [--connect] <path to qcow2 image>
Mount Image : $progname [--connect-raw] <path to raw image>
Mount Image : $progname [--disconnect] <nbd device basename>
Mount Image : $progname [-m|--mount] <path to qcow2 image> <mount point>
Mount Image : $progname [-r|--mount-raw] <path to raw image> <mount point>
Umount Image: $progname [-u|--umount] <mount point>
Cleanup NBD : $progname [--cleanup]
arguments:
-h, --help show this help message and exit
-c, --cleanup cleanup orphaned device mappings
-C, --force-cleanup forcefully cleanup orphaned or still connected device mappings
--connect connect qcow2 image
--connect-raw connect raw image
--disconnect disconnect connected qcow2 image
-m, --mount mount qcow2 image
-r, --mount-raw mount raw image
-u, --umount umount image
./$progname --mount --image <your image> --mount-point <your path>
HEREDOC
}
CONNECT=0
DISCONNECT=0
MOUNT=0
RAW_IMAGE=0
UMOUNT=0
IMAGE=""
MOUNTPOINT=""
NBD_BASENAME=""
error_exit() {
echo "$@"
usage
exit
}
# As long as there is at least one more argument, keep looping
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
-h|--help)
usage
exit
;;
-c|--cleanup)
lib_nbd_cleanup
exit
;;
-C|--force-cleanup)
lib_force_nbd_cleanup
exit
;;
--connect-raw)
CONNECT=1
RAW_IMAGE=1
;;
--connect)
CONNECT=1
;;
--disconnect)
DISCONNECT=1
;;
-r|--mount-raw)
MOUNT=1
RAW_IMAGE=1
;;
-m|--mount)
MOUNT=1
;;
-u|--umount)
UMOUNT=1
;;
*)
if [ $CONNECT -eq 1 ]; then
if [ -z "$IMAGE" ]; then
IMAGE=$1
else
error_exit "Unknown connect option '$key'"
fi
elif [ $DISCONNECT -eq 1 ]; then
if [ -z "$NBD_BASENAME" ]; then
NBD_BASENAME=$1
else
error_exit "Unknown disconnect option '$key'"
fi
elif [ $MOUNT -eq 1 ]; then
if [ -z "$IMAGE" ]; then
IMAGE=$1
elif [ -z "$MOUNTPOINT" ]; then
MOUNTPOINT=$1
else
error_exit "Unknown mount option '$key'"
fi
elif [ $UMOUNT -eq 1 ]; then
if [ -z "$MOUNTPOINT" ]; then
MOUNTPOINT=$1
else
error_exit "Unknown unmount option '$key'"
fi
else
error_exit "Unknown option '$key'"
fi
;;
esac
# Shift after checking all the cases to get the next option
shift
done
CTR_MOUNTCONN=0
if [ "${MOUNT}" = "1" ]; then
let CTR_MOUNTCONN=${CTR_MOUNTCONN}+1
fi
if [ "${UMOUNT}" = "1" ]; then
let CTR_MOUNTCONN=${CTR_MOUNTCONN}+1
fi
if [ "${CONNECT}" = "1" ]; then
let CTR_MOUNTCONN=${CTR_MOUNTCONN}+1
fi
if [ "${DISCONNECT}" = "1" ]; then
let CTR_MOUNTCONN=${CTR_MOUNTCONN}+1
fi
if [ "${CTR_MOUNTCONN}" -gt 1 ]; then
usage
echo "Concurrent mount/connect options not possible."
exit
fi
if [ "${CONNECT}" = "1" ] && [ -z "${IMAGE}" ]; then
usage
echo "Can not connect to image. Image path missing."
exit
fi
if [ "${DISCONNECT}" = "1" ] && [ -z "${NBD_BASENAME}" ]; then
usage
echo "Can not disconnect nbd device. NBD basename missing."
exit
fi
if [ "${MOUNT}" = "1" ] && ([ -z "${IMAGE}" ] || [ -z "${MOUNTPOINT}" ]); then
usage
echo "Can not mount image. Image path and/or mount point missing."
exit
fi
if [ "${UMOUNT}" = "1" ] && [ -z "${MOUNTPOINT}" ]; then
usage
echo "Can not umount. Mount point parameter missing."
exit
fi
if [ -n "${MOUNTPOINT}" -a ! -d "${MOUNTPOINT}" ]; then
echo "Mountpoint ${MOUNTPOINT} not existing."
exit 1
fi
if [ -n "${IMAGE}" -a ! -f "${IMAGE}" ]; then
echo "Image ${IMAGE} not existing."
exit 1
fi
if [ "${CONNECT}" = "1" ]; then
nbd_dev=
if [ "${RAW_IMAGE}" = "1" ]; then
nbd_dev=$(lib_connect_blkdev2 "${IMAGE}" "raw")
else
nbd_dev=$(lib_connect_blkdev2 "${IMAGE}" "qcow2")
fi
echo ${nbd_dev}
elif [ "${DISCONNECT}" = "1" ]; then
lib_disconnect_blkdev "${NBD_BASENAME}"
elif [ "${MOUNT}" = "1" ]; then
nbd_dev=
if [ "${RAW_IMAGE}" = "1" ]; then
nbd_dev=$(lib_mount_image3 "${IMAGE}" "raw" "${MOUNTPOINT}")
else
nbd_dev=$(lib_mount_image3 "${IMAGE}" "qcow2" "${MOUNTPOINT}")
fi
echo ${nbd_dev}
elif [ "${UMOUNT}" = "1" ]; then
lib_umount_image1 "${MOUNTPOINT}"
fi
|