storage_backend_sheepdog.c 11.1 KB
Newer Older
1 2 3
/*
 * storage_backend_sheepdog.c: storage backend for Sheepdog handling
 *
4
 * Copyright (C) 2013-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * Copyright (C) 2012 Wido den Hollander
 * Copyright (C) 2012 Frank Spijkerman
 * Copyright (C) 2012 Sebastian Wiedenroth
 *
 * 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
20
 * License along with this library.  If not, see
O
Osier Yang 已提交
21
 * <http://www.gnu.org/licenses/>.
22 23 24 25 26 27 28 29
 *
 * Author: Wido den Hollander <wido@widodh.nl>
 *         Frank Spijkerman <frank.spijkerman@avira.com>
 *         Sebastian Wiedenroth <sebastian.wiedenroth@skylime.net>
 */

#include <config.h>

30
#include "virerror.h"
31 32
#include "storage_backend_sheepdog.h"
#include "storage_conf.h"
33
#include "vircommand.h"
34
#include "viralloc.h"
35
#include "virstring.h"
36
#include "storage_util.h"
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

#define VIR_FROM_THIS VIR_FROM_STORAGE

static int virStorageBackendSheepdogRefreshVol(virConnectPtr conn,
                                               virStoragePoolObjPtr pool,
                                               virStorageVolDefPtr vol);

void virStorageBackendSheepdogAddHostArg(virCommandPtr cmd,
                                         virStoragePoolObjPtr pool);

int
virStorageBackendSheepdogParseNodeInfo(virStoragePoolDefPtr pool,
                                       char *output)
{
    /* fields:
     * node id/total, size, used, use%, [total vdi size]
     *
     * example output:
     * 0 15245667872 117571104 0%
     * Total 15245667872 117571104 0% 20972341
     */

    const char *p, *next;

    pool->allocation = pool->capacity = pool->available = 0;

    p = output;
    do {
        char *end;

        if ((next = strchr(p, '\n')))
            ++next;
        else
70
            break;
71 72 73 74 75 76 77

        if (!STRPREFIX(p, "Total "))
            continue;

        p = p + 6;

        if (virStrToLong_ull(p, &end, 10, &pool->capacity) < 0)
78
            break;
79 80

        if ((p = end + 1) > next)
81
            break;
82 83

        if (virStrToLong_ull(p, &end, 10, &pool->allocation) < 0)
84
            break;
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

        pool->available = pool->capacity - pool->allocation;
        return 0;

    } while ((p = next));

    return -1;
}

void
virStorageBackendSheepdogAddHostArg(virCommandPtr cmd,
                                    virStoragePoolObjPtr pool)
{
    const char *address = "localhost";
    int port = 7000;
    if (pool->def->source.nhost > 0) {
101
        if (pool->def->source.hosts[0].name != NULL)
102
            address = pool->def->source.hosts[0].name;
103
        if (pool->def->source.hosts[0].port)
104 105 106 107 108 109 110 111
            port = pool->def->source.hosts[0].port;
    }
    virCommandAddArg(cmd, "-a");
    virCommandAddArgFormat(cmd, "%s", address);
    virCommandAddArg(cmd, "-p");
    virCommandAddArgFormat(cmd, "%d", port);
}

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
static int
virStorageBackendSheepdogAddVolume(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virStoragePoolObjPtr pool, const char *diskInfo)
{
    virStorageVolDefPtr vol = NULL;

    if (diskInfo == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing disk info when adding volume"));
        goto error;
    }

    if (VIR_ALLOC(vol) < 0 || VIR_STRDUP(vol->name, diskInfo) < 0)
        goto error;

    vol->type = VIR_STORAGE_VOL_NETWORK;

    if (virStorageBackendSheepdogRefreshVol(conn, pool, vol) < 0)
        goto error;

    if (VIR_EXPAND_N(pool->volumes.objs, pool->volumes.count, 1) < 0)
        goto error;

    pool->volumes.objs[pool->volumes.count - 1] = vol;

    return 0;

139
 error:
140 141 142 143 144 145 146 147 148 149 150 151 152 153
    virStorageVolDefFree(vol);
    return -1;
}

static int
virStorageBackendSheepdogRefreshAllVol(virConnectPtr conn ATTRIBUTE_UNUSED,
                                       virStoragePoolObjPtr pool)
{
    int ret = -1;
    char *output = NULL;
    char **lines = NULL;
    char **cells = NULL;
    size_t i;

154
    virCommandPtr cmd = virCommandNewArgList(SHEEPDOGCLI, "vdi", "list", "-r", NULL);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    virStorageBackendSheepdogAddHostArg(cmd, pool);
    virCommandSetOutputBuffer(cmd, &output);
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

    lines = virStringSplit(output, "\n", 0);
    if (lines == NULL)
        goto cleanup;

    for (i = 0; lines[i]; i++) {
        const char *line = lines[i];
        if (line == NULL)
            break;

        cells = virStringSplit(line, " ", 0);

171 172
        if (cells != NULL &&
            virStringListLength((const char * const *)cells) > 2) {
173 174 175 176
            if (virStorageBackendSheepdogAddVolume(conn, pool, cells[1]) < 0)
                goto cleanup;
        }

177
        virStringListFree(cells);
178 179 180 181 182
        cells = NULL;
    }

    ret = 0;

183
 cleanup:
184
    virCommandFree(cmd);
185 186
    virStringListFree(lines);
    virStringListFree(cells);
187 188 189 190
    VIR_FREE(output);
    return ret;
}

191 192 193 194 195

static int
virStorageBackendSheepdogRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
                                     virStoragePoolObjPtr pool)
{
196
    int ret = -1;
197 198 199
    char *output = NULL;
    virCommandPtr cmd;

200
    cmd = virCommandNewArgList(SHEEPDOGCLI, "node", "info", "-r", NULL);
201 202
    virStorageBackendSheepdogAddHostArg(cmd, pool);
    virCommandSetOutputBuffer(cmd, &output);
203 204 205 206 207
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

    if (virStorageBackendSheepdogParseNodeInfo(pool->def, output) < 0)
        goto cleanup;
208

209
    ret = virStorageBackendSheepdogRefreshAllVol(conn, pool);
210
 cleanup:
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    virCommandFree(cmd);
    VIR_FREE(output);
    return ret;
}


static int
virStorageBackendSheepdogDeleteVol(virConnectPtr conn ATTRIBUTE_UNUSED,
                                   virStoragePoolObjPtr pool,
                                   virStorageVolDefPtr vol,
                                   unsigned int flags)
{

    virCheckFlags(0, -1);

226
    virCommandPtr cmd = virCommandNewArgList(SHEEPDOGCLI, "vdi", "delete", vol->name, NULL);
227 228 229 230 231 232 233 234 235 236 237 238 239 240
    virStorageBackendSheepdogAddHostArg(cmd, pool);
    int ret = virCommandRun(cmd, NULL);

    virCommandFree(cmd);
    return ret;
}


static int
virStorageBackendSheepdogCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED,
                                   virStoragePoolObjPtr pool,
                                   virStorageVolDefPtr vol)
{
    if (vol->target.encryption != NULL) {
241 242 243
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       "%s", _("storage pool does not support encrypted "
                               "volumes"));
244 245 246
        return -1;
    }

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    vol->type = VIR_STORAGE_VOL_NETWORK;

    VIR_FREE(vol->key);
    if (virAsprintf(&vol->key, "%s/%s",
                    pool->def->source.name, vol->name) == -1)
        return -1;

    VIR_FREE(vol->target.path);
    if (VIR_STRDUP(vol->target.path, vol->name) < 0)
        return -1;

    return 0;
}


static int
263
virStorageBackendSheepdogBuildVol(virConnectPtr conn ATTRIBUTE_UNUSED,
264 265 266 267 268
                                  virStoragePoolObjPtr pool,
                                  virStorageVolDefPtr vol,
                                  unsigned int flags)
{
    int ret = -1;
269
    virCommandPtr cmd = NULL;
270 271 272

    virCheckFlags(0, -1);

273 274 275 276 277 278
    if (!vol->target.capacity) {
        virReportError(VIR_ERR_NO_SUPPORT, "%s",
                       _("volume capacity required for this pool"));
        goto cleanup;
    }

279
    cmd = virCommandNewArgList(SHEEPDOGCLI, "vdi", "create", vol->name, NULL);
280
    virCommandAddArgFormat(cmd, "%llu", vol->target.capacity);
281
    virStorageBackendSheepdogAddHostArg(cmd, pool);
282 283
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;
284

285
    ret = 0;
286
 cleanup:
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    virCommandFree(cmd);
    return ret;
}


int
virStorageBackendSheepdogParseVdiList(virStorageVolDefPtr vol,
                                      char *output)
{
    /* fields:
     * current/clone/snapshot, name, id, size, used, shared, creation time, vdi id, [tag]
     *
     * example output:
     * s test 1 10 0 0 1336556634 7c2b25
     * s test 2 10 0 0 1336557203 7c2b26
     * = test 3 10 0 0 1336557216 7c2b27
     */

    int id;
    const char *p, *next;

308
    vol->target.allocation = vol->target.capacity = 0;
309 310 311 312 313 314 315 316 317 318 319 320 321

    p = output;
    do {
        char *end;

        if ((next = strchr(p, '\n')))
            ++next;

        /* ignore snapshots */
        if (*p != '=')
            continue;

        /* skip space */
322
        if (p + 2 < next)
323
            p += 2;
324
        else
325 326 327 328 329 330 331 332 333 334 335 336 337 338
            return -1;

        /* skip name */
        while (*p != '\0' && *p != ' ') {
            if (*p == '\\')
                ++p;
            ++p;
        }

        if (virStrToLong_i(p, &end, 10, &id) < 0)
            return -1;

        p = end + 1;

339
        if (virStrToLong_ull(p, &end, 10, &vol->target.capacity) < 0)
340 341 342 343
            return -1;

        p = end + 1;

344
        if (virStrToLong_ull(p, &end, 10, &vol->target.allocation) < 0)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
            return -1;

        return 0;
    } while ((p = next));

    return -1;
}

static int
virStorageBackendSheepdogRefreshVol(virConnectPtr conn ATTRIBUTE_UNUSED,
                                    virStoragePoolObjPtr pool,
                                    virStorageVolDefPtr vol)
{
    int ret;
    char *output = NULL;

361
    virCommandPtr cmd = virCommandNewArgList(SHEEPDOGCLI, "vdi", "list", vol->name, "-r", NULL);
362 363 364 365 366 367 368 369 370 371 372 373 374 375
    virStorageBackendSheepdogAddHostArg(cmd, pool);
    virCommandSetOutputBuffer(cmd, &output);
    ret = virCommandRun(cmd, NULL);

    if (ret < 0)
        goto cleanup;

    if ((ret = virStorageBackendSheepdogParseVdiList(vol, output)) < 0)
        goto cleanup;

    vol->type = VIR_STORAGE_VOL_NETWORK;

    VIR_FREE(vol->key);
    if (virAsprintf(&vol->key, "%s/%s",
376
                    pool->def->source.name, vol->name) == -1)
377 378 379
        goto cleanup;

    VIR_FREE(vol->target.path);
380
    ignore_value(VIR_STRDUP(vol->target.path, vol->name));
381
 cleanup:
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    virCommandFree(cmd);
    return ret;
}


static int
virStorageBackendSheepdogResizeVol(virConnectPtr conn ATTRIBUTE_UNUSED,
                                   virStoragePoolObjPtr pool,
                                   virStorageVolDefPtr vol,
                                   unsigned long long capacity,
                                   unsigned int flags)
{

    virCheckFlags(0, -1);

397
    virCommandPtr cmd = virCommandNewArgList(SHEEPDOGCLI, "vdi", "resize", vol->name, NULL);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    virCommandAddArgFormat(cmd, "%llu", capacity);
    virStorageBackendSheepdogAddHostArg(cmd, pool);
    int ret = virCommandRun(cmd, NULL);

    virCommandFree(cmd);
    return ret;

}



virStorageBackend virStorageBackendSheepdog = {
    .type = VIR_STORAGE_POOL_SHEEPDOG,

    .refreshPool = virStorageBackendSheepdogRefreshPool,
    .createVol = virStorageBackendSheepdogCreateVol,
414
    .buildVol = virStorageBackendSheepdogBuildVol,
415 416 417 418
    .refreshVol = virStorageBackendSheepdogRefreshVol,
    .deleteVol = virStorageBackendSheepdogDeleteVol,
    .resizeVol = virStorageBackendSheepdogResizeVol,
};