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
|
/*
* storage_backend_mpath.c: storage backend for multipath handling
*
* Copyright (C) 2009-2014 Red Hat, Inc.
* Copyright (C) 2009-2008 Dave Allan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <unistd.h>
#include <fcntl.h>
#include <libdevmapper.h>
#include "virerror.h"
#include "storage_conf.h"
#include "storage_backend.h"
#include "storage_backend_mpath.h"
#include "viralloc.h"
#include "virlog.h"
#include "virfile.h"
#include "virutil.h"
#include "storage_util.h"
#define VIR_FROM_THIS VIR_FROM_STORAGE
VIR_LOG_INIT("storage.storage_backend_mpath");
static int
virStorageBackendMpathNewVol(virStoragePoolObj *pool,
const int devnum,
const char *dev)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
g_autoptr(virStorageVolDef) vol = NULL;
vol = g_new0(virStorageVolDef, 1);
vol->type = VIR_STORAGE_VOL_BLOCK;
(vol->name) = g_strdup_printf("dm-%u", devnum);
vol->target.path = g_strdup_printf("/dev/%s", dev);
if (virStorageBackendUpdateVolInfo(vol, true,
VIR_STORAGE_VOL_OPEN_DEFAULT, 0) < 0) {
return -1;
}
/* XXX should use logical unit's UUID instead */
vol->key = g_strdup(vol->target.path);
if (virStoragePoolObjAddVol(pool, vol) < 0)
return -1;
def->capacity += vol->target.capacity;
def->allocation += vol->target.allocation;
vol = NULL;
return 0;
}
static int
virStorageBackendIsMultipath(const char *dev_name)
{
int ret = 0;
struct dm_task *dmt = NULL;
void *next = NULL;
uint64_t start, length;
char *target_type = NULL;
char *params = NULL;
dmt = dm_task_create(DM_DEVICE_TABLE);
if (dmt == NULL) {
ret = -1;
goto out;
}
if (dm_task_set_name(dmt, dev_name) == 0) {
ret = -1;
goto out;
}
dm_task_no_open_count(dmt);
if (!dm_task_run(dmt)) {
ret = -1;
goto out;
}
dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms);
if (STREQ_NULLABLE(target_type, "multipath"))
ret = 1;
out:
if (dmt != NULL)
dm_task_destroy(dmt);
return ret;
}
static int
virStorageBackendGetMinorNumber(const char *dev_name, uint32_t *minor)
{
int ret = -1;
struct dm_task *dmt;
struct dm_info info;
if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
goto out;
if (!dm_task_set_name(dmt, dev_name))
goto out;
if (!dm_task_run(dmt))
goto out;
if (!dm_task_get_info(dmt, &info))
goto out;
*minor = info.minor;
ret = 0;
out:
if (dmt != NULL)
dm_task_destroy(dmt);
return ret;
}
static int
virStorageBackendCreateVols(virStoragePoolObj *pool,
struct dm_names *names)
{
uint32_t minor = -1;
uint32_t next;
g_autofree char *map_device = NULL;
do {
int is_mpath = virStorageBackendIsMultipath(names->name);
if (is_mpath < 0)
return -1;
if (is_mpath == 1) {
map_device = g_strdup_printf("mapper/%s", names->name);
if (virStorageBackendGetMinorNumber(names->name, &minor) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to get %1$s minor number"),
names->name);
return -1;
}
if (virStorageBackendMpathNewVol(pool, minor, map_device) < 0)
return -1;
VIR_FREE(map_device);
}
/* Given the way libdevmapper returns its data, I don't see
* any way to avoid this series of casts. */
VIR_WARNINGS_NO_CAST_ALIGN
next = names->next;
names = (struct dm_names *)(((char *)names) + next);
VIR_WARNINGS_RESET
} while (next);
return 0;
}
static int
virStorageBackendGetMaps(virStoragePoolObj *pool)
{
int retval = 0;
struct dm_task *dmt = NULL;
struct dm_names *names = NULL;
if (!(dmt = dm_task_create(DM_DEVICE_LIST))) {
retval = 1;
goto out;
}
dm_task_no_open_count(dmt);
if (!dm_task_run(dmt)) {
retval = 1;
goto out;
}
if (!(names = dm_task_get_names(dmt))) {
retval = 1;
goto out;
}
if (!names->dev) {
/* No devices found */
goto out;
}
virStorageBackendCreateVols(pool, names);
out:
if (dmt != NULL)
dm_task_destroy(dmt);
return retval;
}
static int
virStorageBackendMpathCheckPool(virStoragePoolObj *pool G_GNUC_UNUSED,
bool *isActive)
{
*isActive = virFileExists("/dev/mapper") ||
virFileExists("/dev/mpath");
return 0;
}
static int
virStorageBackendMpathRefreshPool(virStoragePoolObj *pool)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
VIR_DEBUG("pool=%p", pool);
def->allocation = def->capacity = def->available = 0;
virWaitForDevices();
virStorageBackendGetMaps(pool);
return 0;
}
virStorageBackend virStorageBackendMpath = {
.type = VIR_STORAGE_POOL_MPATH,
.checkPool = virStorageBackendMpathCheckPool,
.refreshPool = virStorageBackendMpathRefreshPool,
.uploadVol = virStorageBackendVolUploadLocal,
.downloadVol = virStorageBackendVolDownloadLocal,
.wipeVol = virStorageBackendVolWipeLocal,
};
int
virStorageBackendMpathRegister(void)
{
return virStorageBackendRegister(&virStorageBackendMpath);
}
|