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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/*
* storage_backend_zfs.c: storage backend for ZFS handling
*
* Copyright (C) 2014 Roman Bogorodskiy
*
* 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 "viralloc.h"
#include "virerror.h"
#include "virfile.h"
#include "storage_backend_zfs.h"
#include "virlog.h"
#include "virstring.h"
#include "storage_util.h"
#define VIR_FROM_THIS VIR_FROM_STORAGE
VIR_LOG_INIT("storage.storage_backend_zfs");
#define ZFS "zfs"
#define ZPOOL "zpool"
/*
* Some common flags of zfs and zpool commands we use:
* -H -- don't print headers and separate fields by tab
* -p -- show exact numbers instead of human-readable, i.e.
* for size, show just a number instead of 2G etc
*/
/**
* virStorageBackendZFSVolModeNeeded:
*
* Checks if it's necessary to specify 'volmode' (i.e. that
* we're working with BSD ZFS implementation).
*
* Returns 1 if 'volmode' is need, 0 if not needed, -1 on error
*/
static int
virStorageBackendZFSVolModeNeeded(void)
{
int ret = -1, exit_code = -1;
g_autofree char *error = NULL;
g_autoptr(virCommand) cmd = NULL;
/* 'zfs get' without arguments prints out
* usage information to stderr, including
* list of supported options, and exits with
* exit code 2
*/
cmd = virCommandNewArgList(ZFS, "get", NULL);
virCommandAddEnvString(cmd, "LC_ALL=C");
virCommandSetErrorBuffer(cmd, &error);
ret = virCommandRun(cmd, &exit_code);
if ((ret < 0) || (exit_code != 2)) {
VIR_WARN("Command 'zfs get' either failed "
"to run or exited with unexpected status");
return ret;
}
if (strstr(error, " volmode "))
return 1;
else
return 0;
}
static int
virStorageBackendZFSCheckPool(virStoragePoolObj *pool G_GNUC_UNUSED,
bool *isActive)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
g_autofree char *devpath = NULL;
devpath = g_strdup_printf("/dev/zvol/%s", def->source.name);
*isActive = virFileIsDir(devpath);
return 0;
}
static int
virStorageBackendZFSParseVol(virStoragePoolObj *pool,
virStorageVolDef *vol,
const char *volume_string)
{
int ret = -1;
char *vol_name;
bool is_new_vol = false;
virStorageVolDef *volume = NULL;
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
g_auto(GStrv) tokens = NULL;
char *tmp;
if (!(tokens = g_strsplit(volume_string, "\t", 0)))
return -1;
if (g_strv_length(tokens) != 3)
goto cleanup;
vol_name = tokens[0];
if ((tmp = strrchr(vol_name, '/')))
vol_name = tmp + 1;
if (vol == NULL)
volume = virStorageVolDefFindByName(pool, vol_name);
else
volume = vol;
if (volume == NULL) {
volume = g_new0(virStorageVolDef, 1);
is_new_vol = true;
volume->type = VIR_STORAGE_VOL_BLOCK;
volume->name = g_strdup(vol_name);
}
if (!volume->key)
volume->key = g_strdup(tokens[0]);
if (volume->target.path == NULL) {
volume->target.path = g_strdup_printf("%s/%s", def->target.path,
volume->name);
}
if (virStrToLong_ull(tokens[1], NULL, 10, &volume->target.capacity) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("malformed volsize reported"));
goto cleanup;
}
if (virStrToLong_ull(tokens[2], NULL, 10, &volume->target.allocation) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("malformed refreservation reported"));
goto cleanup;
}
if (volume->target.allocation < volume->target.capacity)
volume->target.sparse = true;
if (is_new_vol && virStoragePoolObjAddVol(pool, volume) < 0)
goto cleanup;
volume = NULL;
ret = 0;
cleanup:
if (is_new_vol)
virStorageVolDefFree(volume);
return ret;
}
static int
virStorageBackendZFSFindVols(virStoragePoolObj *pool,
virStorageVolDef *vol)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
size_t i;
g_auto(GStrv) lines = NULL;
g_autoptr(virCommand) cmd = NULL;
g_autofree char *volumes_list = NULL;
/**
* $ zfs list -Hp -t volume -o name,volsize -r test
* test/vol1 5368709120
* test/vol3 1073741824
* test/vol4 1572864000
* $
*
* Arguments description:
* -t volume -- we want to see only volumes
* -o name,volsize -- limit output to name and volume size
* -r -- we want to see all the childer of our pool
*/
cmd = virCommandNewArgList(ZFS,
"list", "-Hp",
"-t", "volume", "-r",
"-o", "name,volsize,refreservation",
def->source.name,
NULL);
virCommandSetOutputBuffer(cmd, &volumes_list);
if (virCommandRun(cmd, NULL) < 0)
return -1;
if (!(lines = g_strsplit(volumes_list, "\n", 0)))
return -1;
for (i = 0; lines[i]; i++) {
if (STREQ(lines[i], ""))
continue;
if (virStorageBackendZFSParseVol(pool, vol, lines[i]) < 0)
continue;
}
return 0;
}
static int
virStorageBackendZFSRefreshPool(virStoragePoolObj *pool G_GNUC_UNUSED)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
char *zpool_props = NULL;
size_t i;
g_autoptr(virCommand) cmd = NULL;
g_auto(GStrv) lines = NULL;
g_autofree char *name = g_strdup(def->source.name);
char *tmp;
/**
* $ zpool get -Hp health,size,free,allocated test
* test health ONLINE -
* test size 199715979264 -
* test free 198899976704 -
* test allocated 816002560 -
* $
*
* Here we just provide a list of properties we want to see
*/
if ((tmp = strchr(name, '/')))
*tmp = '\0';
cmd = virCommandNewArgList(ZPOOL,
"get", "-Hp",
"health,size,free,allocated",
name,
NULL);
virCommandSetOutputBuffer(cmd, &zpool_props);
if (virCommandRun(cmd, NULL) < 0)
goto cleanup;
if (!(lines = g_strsplit(zpool_props, "\n", 0)))
goto cleanup;
for (i = 0; lines[i]; i++) {
g_auto(GStrv) tokens = NULL;
char *prop_name;
if (STREQ(lines[i], ""))
continue;
if (!(tokens = g_strsplit(lines[i], "\t", 0)))
goto cleanup;
if (g_strv_length(tokens) != 4)
continue;
prop_name = tokens[1];
if (STREQ(prop_name, "free") || STREQ(prop_name, "size") ||
STREQ(prop_name, "allocated")) {
unsigned long long value;
if (virStrToLong_ull(tokens[2], NULL, 10, &value) < 0)
goto cleanup;
if (STREQ(prop_name, "free"))
def->available = value;
else if (STREQ(prop_name, "size"))
def->capacity = value;
else if (STREQ(prop_name, "allocated"))
def->allocation = value;
}
}
/* Obtain a list of volumes */
if (virStorageBackendZFSFindVols(pool, NULL) < 0)
goto cleanup;
cleanup:
VIR_FREE(zpool_props);
return 0;
}
static int
virStorageBackendZFSCreateVol(virStoragePoolObj *pool,
virStorageVolDef *vol)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
int volmode_needed = -1;
g_autoptr(virCommand) cmd = NULL;
if (vol->target.encryption != NULL) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
"%s", _("storage pool does not support encrypted "
"volumes"));
return -1;
}
vol->type = VIR_STORAGE_VOL_BLOCK;
VIR_FREE(vol->target.path);
vol->target.path = g_strdup_printf("%s/%s", def->target.path, vol->name);
vol->key = g_strdup(vol->target.path);
volmode_needed = virStorageBackendZFSVolModeNeeded();
if (volmode_needed < 0)
return -1;
/**
* $ zfs create -o volmode=dev -V 10240K test/volname
* $ zfs create -o volmode=dev -s -V 10240K test/volname
* $ zfs create -o volmode=dev -s -o refreservation=1024K -V 10240K test/volname
*
* -o volmode=dev -- we want to get volumes exposed as cdev
* devices. If we don't specify that zfs
* will lookup vfs.zfs.vol.mode sysctl value
* -s -- create a sparse volume
* -o refreservation -- reserve the specified amount of space
* -V -- tells to create a volume with the specified size
*/
cmd = virCommandNewArgList(ZFS, "create", NULL);
if (volmode_needed)
virCommandAddArgList(cmd, "-o", "volmode=dev", NULL);
if (vol->target.capacity != vol->target.allocation) {
virCommandAddArg(cmd, "-s");
if (vol->target.allocation > 0) {
virCommandAddArg(cmd, "-o");
virCommandAddArgFormat(cmd, "refreservation=%lluK",
VIR_DIV_UP(vol->target.allocation, 1024));
}
vol->target.sparse = true;
}
virCommandAddArg(cmd, "-V");
virCommandAddArgFormat(cmd, "%lluK",
VIR_DIV_UP(vol->target.capacity, 1024));
virCommandAddArgFormat(cmd, "%s/%s", def->source.name, vol->name);
if (virCommandRun(cmd, NULL) < 0)
return -1;
if (virStorageBackendZFSFindVols(pool, vol) < 0)
return -1;
return 0;
}
static int
virStorageBackendZFSDeleteVol(virStoragePoolObj *pool,
virStorageVolDef *vol,
unsigned int flags)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
g_autoptr(virCommand) destroy_cmd = NULL;
virCheckFlags(0, -1);
destroy_cmd = virCommandNewArgList(ZFS, "destroy", NULL);
virCommandAddArgFormat(destroy_cmd, "%s/%s",
def->source.name, vol->name);
return virCommandRun(destroy_cmd, NULL);
}
static int
virStorageBackendZFSBuildPool(virStoragePoolObj *pool,
unsigned int flags)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
size_t i;
g_autoptr(virCommand) cmd = NULL;
int ret = -1;
char *tmp;
virCheckFlags(0, -1);
tmp = strstr(def->source.name, "/");
if (tmp) {
cmd = virCommandNewArgList(ZFS, "create", "-o", "mountpoint=none",
def->source.name, NULL);
} else {
if (def->source.ndevice == 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
"%s", _("missing source devices"));
return -1;
}
cmd = virCommandNewArgList(ZPOOL, "create",
def->source.name, NULL);
for (i = 0; i < def->source.ndevice; i++)
virCommandAddArg(cmd, def->source.devices[i].path);
}
virObjectUnlock(pool);
ret = virCommandRun(cmd, NULL);
virObjectLock(pool);
return ret;
}
static int
virStorageBackendZFSDeletePool(virStoragePoolObj *pool,
unsigned int flags)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
g_autoptr(virCommand) cmd = NULL;
char *tmp;
virCheckFlags(0, -1);
tmp = strstr(def->source.name, "/");
if (tmp) {
cmd = virCommandNewArgList(ZFS, "destroy", "-r",
def->source.name, NULL);
} else {
cmd = virCommandNewArgList(ZPOOL, "destroy",
def->source.name, NULL);
}
return virCommandRun(cmd, NULL);
}
virStorageBackend virStorageBackendZFS = {
.type = VIR_STORAGE_POOL_ZFS,
.checkPool = virStorageBackendZFSCheckPool,
.refreshPool = virStorageBackendZFSRefreshPool,
.createVol = virStorageBackendZFSCreateVol,
.deleteVol = virStorageBackendZFSDeleteVol,
.buildPool = virStorageBackendZFSBuildPool,
.deletePool = virStorageBackendZFSDeletePool,
.uploadVol = virStorageBackendVolUploadLocal,
.downloadVol = virStorageBackendVolDownloadLocal,
};
int
virStorageBackendZFSRegister(void)
{
return virStorageBackendRegister(&virStorageBackendZFS);
}
|