storage_backend_zfs.c 12.7 KB
Newer Older
R
Roman Bogorodskiy 已提交
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
/*
 * 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"
30
#include "storage_util.h"
R
Roman Bogorodskiy 已提交
31 32 33 34 35 36 37 38 39 40 41 42

#define VIR_FROM_THIS VIR_FROM_STORAGE

VIR_LOG_INIT("storage.storage_backend_zfs");

/*
 * 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
 */

43 44 45 46 47 48 49 50 51 52 53
/**
 * 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)
{
54
    int ret = -1, exit_code = -1;
55
    VIR_AUTOFREE(char *) error = NULL;
56
    VIR_AUTOPTR(virCommand) cmd = NULL;
57 58 59 60 61 62 63 64 65 66

    /* '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);

67 68
    ret = virCommandRun(cmd, &exit_code);
    if ((ret < 0) || (exit_code != 2)) {
69 70 71 72 73 74 75 76 77 78 79 80 81
        VIR_WARN("Command 'zfs get' either failed "
                 "to run or exited with unexpected status");
        goto cleanup;
    }

    if (strstr(error, " volmode "))
        ret = 1;
    else
        ret = 0;

 cleanup:
    return ret;
}
R
Roman Bogorodskiy 已提交
82 83

static int
J
Ján Tomko 已提交
84
virStorageBackendZFSCheckPool(virStoragePoolObjPtr pool G_GNUC_UNUSED,
R
Roman Bogorodskiy 已提交
85 86
                              bool *isActive)
{
87
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
88
    VIR_AUTOFREE(char *) devpath = NULL;
R
Roman Bogorodskiy 已提交
89 90

    if (virAsprintf(&devpath, "/dev/zvol/%s",
91
                    def->source.name) < 0)
R
Roman Bogorodskiy 已提交
92 93 94 95 96 97 98 99
        return -1;
    *isActive = virFileIsDir(devpath);

    return 0;
}

static int
virStorageBackendZFSParseVol(virStoragePoolObjPtr pool,
100 101
                             virStorageVolDefPtr vol,
                             const char *volume_string)
R
Roman Bogorodskiy 已提交
102 103 104 105 106
{
    int ret = -1;
    size_t count;
    char *vol_name;
    bool is_new_vol = false;
107
    virStorageVolDefPtr volume = NULL;
108
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
109 110
    VIR_AUTOSTRINGLIST tokens = NULL;
    VIR_AUTOSTRINGLIST name_tokens = NULL;
R
Roman Bogorodskiy 已提交
111

112
    if (!(tokens = virStringSplitCount(volume_string, "\t", 0, &count)))
R
Roman Bogorodskiy 已提交
113 114
        return -1;

R
Richard Laager 已提交
115
    if (count != 3)
R
Roman Bogorodskiy 已提交
116 117 118 119 120 121 122
        goto cleanup;

    if (!(name_tokens = virStringSplit(tokens[0], "/", 2)))
        goto cleanup;

    vol_name = name_tokens[1];

123 124 125 126
    if (vol == NULL)
        volume = virStorageVolDefFindByName(pool, vol_name);
    else
        volume = vol;
R
Roman Bogorodskiy 已提交
127

128 129
    if (volume == NULL) {
        if (VIR_ALLOC(volume) < 0)
R
Roman Bogorodskiy 已提交
130 131 132
            goto cleanup;

        is_new_vol = true;
133
        volume->type = VIR_STORAGE_VOL_BLOCK;
R
Roman Bogorodskiy 已提交
134

135
        if (VIR_STRDUP(volume->name, vol_name) < 0)
R
Roman Bogorodskiy 已提交
136 137 138
            goto cleanup;
    }

139
    if (!volume->key && VIR_STRDUP(volume->key, tokens[0]) < 0)
R
Roman Bogorodskiy 已提交
140 141
        goto cleanup;

142 143
    if (volume->target.path == NULL) {
        if (virAsprintf(&volume->target.path, "%s/%s",
144
                        def->target.path, volume->name) < 0)
R
Roman Bogorodskiy 已提交
145 146 147
            goto cleanup;
    }

148
    if (virStrToLong_ull(tokens[1], NULL, 10, &volume->target.capacity) < 0) {
R
Roman Bogorodskiy 已提交
149 150 151 152 153
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("malformed volsize reported"));
        goto cleanup;
    }

R
Richard Laager 已提交
154 155 156 157 158 159 160 161 162
    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;

163
    if (is_new_vol && virStoragePoolObjAddVol(pool, volume) < 0)
R
Roman Bogorodskiy 已提交
164
        goto cleanup;
165
    volume = NULL;
R
Roman Bogorodskiy 已提交
166 167 168

    ret = 0;
 cleanup:
169
    if (is_new_vol)
170
        virStorageVolDefFree(volume);
R
Roman Bogorodskiy 已提交
171 172 173 174
    return ret;
}

static int
175 176
virStorageBackendZFSFindVols(virStoragePoolObjPtr pool,
                             virStorageVolDefPtr vol)
R
Roman Bogorodskiy 已提交
177
{
178
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
R
Roman Bogorodskiy 已提交
179
    size_t i;
180
    VIR_AUTOSTRINGLIST lines = NULL;
181
    VIR_AUTOPTR(virCommand) cmd = NULL;
182
    VIR_AUTOFREE(char *) volumes_list = NULL;
R
Roman Bogorodskiy 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    /**
     * $ 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",
R
Richard Laager 已提交
199
                               "-o", "name,volsize,refreservation",
200
                               def->source.name,
R
Roman Bogorodskiy 已提交
201 202 203
                               NULL);
    virCommandSetOutputBuffer(cmd, &volumes_list);
    if (virCommandRun(cmd, NULL) < 0)
204
        return -1;
R
Roman Bogorodskiy 已提交
205 206

    if (!(lines = virStringSplit(volumes_list, "\n", 0)))
207
        return -1;
R
Roman Bogorodskiy 已提交
208 209 210 211 212

    for (i = 0; lines[i]; i++) {
        if (STREQ(lines[i], ""))
            continue;

213
        if (virStorageBackendZFSParseVol(pool, vol, lines[i]) < 0)
R
Roman Bogorodskiy 已提交
214 215 216 217 218 219 220
            continue;
    }

    return 0;
}

static int
J
Ján Tomko 已提交
221
virStorageBackendZFSRefreshPool(virStoragePoolObjPtr pool G_GNUC_UNUSED)
R
Roman Bogorodskiy 已提交
222
{
223
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
R
Roman Bogorodskiy 已提交
224 225
    char *zpool_props = NULL;
    size_t i;
226
    VIR_AUTOPTR(virCommand) cmd = NULL;
227 228
    VIR_AUTOSTRINGLIST lines = NULL;
    VIR_AUTOSTRINGLIST tokens = NULL;
R
Roman Bogorodskiy 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242

    /**
     * $ 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
     */
    cmd = virCommandNewArgList(ZPOOL,
                               "get", "-Hp",
                               "health,size,free,allocated",
243
                               def->source.name,
R
Roman Bogorodskiy 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
                               NULL);
    virCommandSetOutputBuffer(cmd, &zpool_props);
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

    if (!(lines = virStringSplit(zpool_props, "\n", 0)))
        goto cleanup;

    for (i = 0; lines[i]; i++) {
        size_t count;
        char *prop_name;

        if (STREQ(lines[i], ""))
            continue;

259
        virStringListFree(tokens);
R
Roman Bogorodskiy 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        if (!(tokens = virStringSplitCount(lines[i], "\t", 0, &count)))
            goto cleanup;

        if (count != 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"))
275
                def->available = value;
R
Roman Bogorodskiy 已提交
276
            else if (STREQ(prop_name, "size"))
277
                def->capacity = value;
R
Roman Bogorodskiy 已提交
278
            else if (STREQ(prop_name, "allocated"))
279
                def->allocation = value;
R
Roman Bogorodskiy 已提交
280 281 282 283
        }
    }

    /* Obtain a list of volumes */
284
    if (virStorageBackendZFSFindVols(pool, NULL) < 0)
R
Roman Bogorodskiy 已提交
285 286 287 288 289 290 291 292 293
        goto cleanup;

 cleanup:
    VIR_FREE(zpool_props);

    return 0;
}

static int
294
virStorageBackendZFSCreateVol(virStoragePoolObjPtr pool,
R
Roman Bogorodskiy 已提交
295 296
                              virStorageVolDefPtr vol)
{
297
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
R
Roman Bogorodskiy 已提交
298
    int ret = -1;
299
    int volmode_needed = -1;
300
    VIR_AUTOPTR(virCommand) cmd = NULL;
R
Roman Bogorodskiy 已提交
301

302 303 304 305 306 307 308
    if (vol->target.encryption != NULL) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       "%s", _("storage pool does not support encrypted "
                               "volumes"));
        return -1;
    }

R
Roman Bogorodskiy 已提交
309 310
    vol->type = VIR_STORAGE_VOL_BLOCK;

311
    VIR_FREE(vol->target.path);
R
Roman Bogorodskiy 已提交
312
    if (virAsprintf(&vol->target.path, "%s/%s",
313
                    def->target.path, vol->name) < 0)
R
Roman Bogorodskiy 已提交
314 315 316 317 318
        return -1;

    if (VIR_STRDUP(vol->key, vol->target.path) < 0)
        goto cleanup;

319 320 321
    volmode_needed = virStorageBackendZFSVolModeNeeded();
    if (volmode_needed < 0)
        goto cleanup;
R
Roman Bogorodskiy 已提交
322 323
    /**
     * $ zfs create -o volmode=dev -V 10240K test/volname
R
Richard Laager 已提交
324 325
     * $ zfs create -o volmode=dev -s -V 10240K test/volname
     * $ zfs create -o volmode=dev -s -o refreservation=1024K -V 10240K test/volname
R
Roman Bogorodskiy 已提交
326 327 328 329
     *
     * -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
R
Richard Laager 已提交
330 331
     * -s -- create a sparse volume
     * -o refreservation -- reserve the specified amount of space
R
Roman Bogorodskiy 已提交
332 333
     * -V -- tells to create a volume with the specified size
     */
334 335 336
    cmd = virCommandNewArgList(ZFS, "create", NULL);
    if (volmode_needed)
        virCommandAddArgList(cmd, "-o", "volmode=dev", NULL);
R
Richard Laager 已提交
337 338 339 340 341 342 343 344 345
    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;
    }
346
    virCommandAddArg(cmd, "-V");
R
Roman Bogorodskiy 已提交
347 348
    virCommandAddArgFormat(cmd, "%lluK",
                           VIR_DIV_UP(vol->target.capacity, 1024));
349
    virCommandAddArgFormat(cmd, "%s/%s", def->source.name, vol->name);
R
Roman Bogorodskiy 已提交
350 351 352 353

    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

354
    if (virStorageBackendZFSFindVols(pool, vol) < 0)
R
Roman Bogorodskiy 已提交
355 356 357 358 359 360 361 362 363
        goto cleanup;

    ret = 0;
 cleanup:
    return ret;

}

static int
364
virStorageBackendZFSDeleteVol(virStoragePoolObjPtr pool,
R
Roman Bogorodskiy 已提交
365 366 367
                              virStorageVolDefPtr vol,
                              unsigned int flags)
{
368
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
369
    VIR_AUTOPTR(virCommand) destroy_cmd = NULL;
R
Roman Bogorodskiy 已提交
370 371 372

    virCheckFlags(0, -1);

J
John Ferlan 已提交
373 374
    destroy_cmd = virCommandNewArgList(ZFS, "destroy", NULL);

R
Roman Bogorodskiy 已提交
375
    virCommandAddArgFormat(destroy_cmd, "%s/%s",
376
                           def->source.name, vol->name);
R
Roman Bogorodskiy 已提交
377

378
    return virCommandRun(destroy_cmd, NULL);
R
Roman Bogorodskiy 已提交
379 380
}

381
static int
382
virStorageBackendZFSBuildPool(virStoragePoolObjPtr pool,
383 384
                              unsigned int flags)
{
385
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
386
    size_t i;
387
    VIR_AUTOPTR(virCommand) cmd = NULL;
388
    int ret = -1;
389 390 391

    virCheckFlags(0, -1);

392
    if (def->source.ndevice == 0) {
393 394 395 396 397 398
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       "%s", _("missing source devices"));
        return -1;
    }

    cmd = virCommandNewArgList(ZPOOL, "create",
399
                               def->source.name, NULL);
400

401 402
    for (i = 0; i < def->source.ndevice; i++)
        virCommandAddArg(cmd, def->source.devices[i].path);
403

404 405 406 407 408
    virObjectUnlock(pool);
    ret = virCommandRun(cmd, NULL);
    virObjectLock(pool);

    return ret;
409 410 411
}

static int
412
virStorageBackendZFSDeletePool(virStoragePoolObjPtr pool,
413 414
                               unsigned int flags)
{
415
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
416
    VIR_AUTOPTR(virCommand) cmd = NULL;
417 418 419 420

    virCheckFlags(0, -1);

    cmd = virCommandNewArgList(ZPOOL, "destroy",
421
                               def->source.name, NULL);
422

423
    return virCommandRun(cmd, NULL);
424
}
R
Roman Bogorodskiy 已提交
425 426 427 428 429 430 431 432

virStorageBackend virStorageBackendZFS = {
    .type = VIR_STORAGE_POOL_ZFS,

    .checkPool = virStorageBackendZFSCheckPool,
    .refreshPool = virStorageBackendZFSRefreshPool,
    .createVol = virStorageBackendZFSCreateVol,
    .deleteVol = virStorageBackendZFSDeleteVol,
433 434
    .buildPool = virStorageBackendZFSBuildPool,
    .deletePool = virStorageBackendZFSDeletePool,
435 436
    .uploadVol = virStorageBackendVolUploadLocal,
    .downloadVol = virStorageBackendVolDownloadLocal,
R
Roman Bogorodskiy 已提交
437
};
438 439 440 441 442 443 444


int
virStorageBackendZFSRegister(void)
{
    return virStorageBackendRegister(&virStorageBackendZFS);
}