storage_backend_logical.c 16.6 KB
Newer Older
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
/*
 * storage_backend_logvol.c: storage backend for logical volume handling
 *
 * Copyright (C) 2007-2008 Red Hat, Inc.
 * Copyright (C) 2007-2008 Daniel P. Berrange
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <regex.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

34
#include "internal.h"
35 36 37
#include "storage_backend_logical.h"
#include "storage_conf.h"
#include "util.h"
38
#include "memory.h"
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

#define PV_BLANK_SECTOR_SIZE 512

enum {
    VIR_STORAGE_POOL_LOGICAL_LVM2 = 0,
};


static int
virStorageBackendLogicalPoolFormatFromString(virConnectPtr conn,
                                             const char *format) {
    if (format == NULL)
        return VIR_STORAGE_POOL_LOGICAL_LVM2;

    if (STREQ(format, "lvm2"))
        return VIR_STORAGE_POOL_LOGICAL_LVM2;

    virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                          _("unsupported pool format %s"), format);
    return -1;
}

static const char *
virStorageBackendLogicalPoolFormatToString(virConnectPtr conn,
                                           int format) {
    switch (format) {
    case VIR_STORAGE_POOL_LOGICAL_LVM2:
        return "lvm2";
    }

    virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                          _("unsupported pool format %d"), format);
    return NULL;
}

static int
virStorageBackendLogicalSetActive(virConnectPtr conn,
                                  virStoragePoolObjPtr pool,
                                  int on)
{
    const char *cmdargv[4];

    cmdargv[0] = VGCHANGE;
    cmdargv[1] = on ? "-ay" : "-an";
    cmdargv[2] = pool->def->name;
    cmdargv[3] = NULL;

    if (virRun(conn, (char**)cmdargv, NULL) < 0)
        return -1;

    return 0;
}


static int
virStorageBackendLogicalMakeVol(virConnectPtr conn,
                                virStoragePoolObjPtr pool,
                                char **const groups,
                                void *data)
{
    virStorageVolDefPtr vol = NULL;
    unsigned long long offset, size, length;

    /* See if we're only looking for a specific volume */
    if (data != NULL) {
        vol = data;
        if (STRNEQ(vol->name, groups[0]))
            return 0;
    }

    /* Or filling in more data on an existing volume */
    if (vol == NULL)
        vol = virStorageVolDefFindByName(pool, groups[0]);

    /* Or a completely new volume */
    if (vol == NULL) {
115
        if (VIR_ALLOC(vol) < 0) {
J
Jim Meyering 已提交
116
            virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
117 118 119 120
            return -1;
        }

        if ((vol->name = strdup(groups[0])) == NULL) {
J
Jim Meyering 已提交
121
            virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
122 123 124 125 126 127 128 129 130
            return -1;
        }

        vol->next = pool->volumes;
        pool->volumes = vol;
        pool->nvolumes++;
    }

    if (vol->target.path == NULL) {
131 132
        if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
                        1 + strlen(vol->name) + 1) < 0) {
J
Jim Meyering 已提交
133
            virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
134 135 136 137 138 139 140 141 142
            return -1;
        }
        strcpy(vol->target.path, pool->def->target.path);
        strcat(vol->target.path, "/");
        strcat(vol->target.path, vol->name);
    }

    if (vol->key == NULL &&
        (vol->key = strdup(groups[1])) == NULL) {
J
Jim Meyering 已提交
143
        virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
144 145 146 147 148 149 150 151
        return -1;
    }

    if (virStorageBackendUpdateVolInfo(conn, vol, 1) < 0)
        return -1;


    /* Finally fill in extents information */
152 153
    if (VIR_REALLOC_N(vol->source.extents,
                      vol->source.nextent + 1) < 0) {
J
Jim Meyering 已提交
154
        virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("extents"));
155 156 157 158 159
        return -1;
    }

    if ((vol->source.extents[vol->source.nextent].path =
         strdup(groups[2])) == NULL) {
J
Jim Meyering 已提交
160
        virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("extents"));
161 162 163 164 165
        return -1;
    }

    if (virStrToLong_ull(groups[3], NULL, 10, &offset) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
166
                              "%s", _("malformed volume extent offset value"));
167 168 169 170
        return -1;
    }
    if (virStrToLong_ull(groups[4], NULL, 10, &length) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
171
                              "%s", _("malformed volume extent length value"));
172 173 174 175
        return -1;
    }
    if (virStrToLong_ull(groups[5], NULL, 10, &size) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
176
                              "%s", _("malformed volume extent size value"));
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
        return -1;
    }

    vol->source.extents[vol->source.nextent].start = offset * size;
    vol->source.extents[vol->source.nextent].end = (offset * size) + length;
    vol->source.nextent++;

    return 0;
}

static int
virStorageBackendLogicalFindLVs(virConnectPtr conn,
                                virStoragePoolObjPtr pool,
                                virStorageVolDefPtr vol)
{
    /*
     *  # lvs --separator : --noheadings --units b --unbuffered --nosuffix --options "lv_name,uuid,devices,seg_size,vg_extent_size" VGNAME
     *  RootLV:06UgP5-2rhb-w3Bo-3mdR-WeoL-pytO-SAa2ky:/dev/hda2(0):5234491392:33554432
     *  SwapLV:oHviCK-8Ik0-paqS-V20c-nkhY-Bm1e-zgzU0M:/dev/hda2(156):1040187392:33554432
     *  Test2:3pg3he-mQsA-5Sui-h0i6-HNmc-Cz7W-QSndcR:/dev/hda2(219):1073741824:33554432
     *  Test3:UB5hFw-kmlm-LSoX-EI1t-ioVd-h7GL-M0W8Ht:/dev/hda2(251):2181038080:33554432
     *  Test3:UB5hFw-kmlm-LSoX-EI1t-ioVd-h7GL-M0W8Ht:/dev/hda2(187):1040187392:33554432
     *
     * Pull out name & uuid, device, device extent start #, segment size, extent size.
     *
     * NB can be multiple rows per volume if they have many extents
203 204
     *
     * NB lvs from some distros (e.g. SLES10 SP2) outputs trailing ":" on each line
205 206
     */
    const char *regexes[] = {
207
        "^\\s*(\\S+):(\\S+):(\\S+)\\((\\S+)\\):(\\S+):([0-9]+):?\\s*$"
208 209 210 211 212 213 214 215 216 217 218
    };
    int vars[] = {
        6
    };
    const char *prog[] = {
        LVS, "--separator", ":", "--noheadings", "--units", "b",
        "--unbuffered", "--nosuffix", "--options",
        "lv_name,uuid,devices,seg_size,vg_extent_size",
        pool->def->name, NULL
    };

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    int exitstatus;

    if (virStorageBackendRunProgRegex(conn,
                                      pool,
                                      prog,
                                      1,
                                      regexes,
                                      vars,
                                      virStorageBackendLogicalMakeVol,
                                      vol,
                                      &exitstatus) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("lvs command failed"));
                              return -1;
    }

    if (exitstatus != 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("lvs command failed with exitstatus %d"),
                              exitstatus);
        return -1;
    }

    return 0;
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
}

static int
virStorageBackendLogicalRefreshPoolFunc(virConnectPtr conn ATTRIBUTE_UNUSED,
                                        virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
                                        char **const groups,
                                        void *data ATTRIBUTE_UNUSED)
{
    if (virStrToLong_ull(groups[0], NULL, 10, &pool->def->capacity) < 0)
        return -1;
    if (virStrToLong_ull(groups[1], NULL, 10, &pool->def->available) < 0)
        return -1;
    pool->def->allocation = pool->def->capacity - pool->def->available;

    return 0;
}


static int
virStorageBackendLogicalStartPool(virConnectPtr conn,
                                  virStoragePoolObjPtr pool)
{
    if (virStorageBackendLogicalSetActive(conn, pool, 1) < 0)
        return -1;

    return 0;
}


static int
virStorageBackendLogicalBuildPool(virConnectPtr conn,
                                  virStoragePoolObjPtr pool,
                                  unsigned int flags ATTRIBUTE_UNUSED)
{
    const char **vgargv;
    const char *pvargv[3];
    int n = 0, i, fd;
    char zeros[PV_BLANK_SECTOR_SIZE];

    memset(zeros, 0, sizeof(zeros));

    /* XXX multiple pvs */
285
    if (VIR_ALLOC_N(vgargv, 1) < 0) {
J
Jim Meyering 已提交
286
        virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("command line"));
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        return -1;
    }

    vgargv[n++] = VGCREATE;
    vgargv[n++] = pool->def->name;

    pvargv[0] = PVCREATE;
    pvargv[2] = NULL;
    for (i = 0 ; i < pool->def->source.ndevice ; i++) {
        /*
         * LVM requires that the first sector is blanked if using
         * a whole disk as a PV. So we just blank them out regardless
         * rather than trying to figure out if we're a disk or partition
         */
        if ((fd = open(pool->def->source.devices[i].path, O_WRONLY)) < 0) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("cannot open device %s"),
                                  strerror(errno));
            goto cleanup;
        }
307
        if (safewrite(fd, zeros, sizeof(zeros)) < 0) {
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
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("cannot clear device header %s"),
                                  strerror(errno));
            close(fd);
            goto cleanup;
        }
        if (close(fd) < 0) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("cannot close device %s"),
                                  strerror(errno));
            goto cleanup;
        }

        /*
         * Initialize the physical volume because vgcreate is not
         * clever enough todo this for us :-(
         */
        vgargv[n++] = pool->def->source.devices[i].path;
        pvargv[1] = pool->def->source.devices[i].path;
        if (virRun(conn, (char**)pvargv, NULL) < 0)
            goto cleanup;
    }

    vgargv[n++] = NULL;

    /* Now create the volume group itself */
    if (virRun(conn, (char**)vgargv, NULL) < 0)
        goto cleanup;

337
    VIR_FREE(vgargv);
338 339 340 341

    return 0;

 cleanup:
342
    VIR_FREE(vgargv);
343 344 345 346 347 348 349 350 351 352 353 354 355
    return -1;
}


static int
virStorageBackendLogicalRefreshPool(virConnectPtr conn,
                                    virStoragePoolObjPtr pool)
{
    /*
     *  # vgs --separator : --noheadings --units b --unbuffered --nosuffix --options "vg_size,vg_free" VGNAME
     *    10603200512:4328521728
     *
     * Pull out size & free
356 357
     *
     * NB vgs from some distros (e.g. SLES10 SP2) outputs trailing ":" on each line
358 359
     */
    const char *regexes[] = {
360
        "^\\s*(\\S+):([0-9]+):?\\s*$"
361 362 363 364 365 366 367 368 369
    };
    int vars[] = {
        2
    };
    const char *prog[] = {
        VGS, "--separator", ":", "--noheadings", "--units", "b", "--unbuffered",
        "--nosuffix", "--options", "vg_size,vg_free",
        pool->def->name, NULL
    };
370
    int exitstatus;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

    /* Get list of all logical volumes */
    if (virStorageBackendLogicalFindLVs(conn, pool, NULL) < 0) {
        virStoragePoolObjClearVols(pool);
        return -1;
    }

    /* Now get basic volgrp metadata */
    if (virStorageBackendRunProgRegex(conn,
                                      pool,
                                      prog,
                                      1,
                                      regexes,
                                      vars,
                                      virStorageBackendLogicalRefreshPoolFunc,
386 387 388 389 390 391 392
                                      NULL,
                                      &exitstatus) < 0) {
        virStoragePoolObjClearVols(pool);
        return -1;
    }

    if (exitstatus != 0) {
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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
        virStoragePoolObjClearVols(pool);
        return -1;
    }

    return 0;
}


/* XXX should we set LVM to inactive ? Probably not - it would
 * suck if this were your LVM root fs :-)
 */
#if 0
static int
virStorageBackendLogicalStopPool(virConnectPtr conn,
                                 virStoragePoolObjPtr pool)
{
    if (virStorageBackendLogicalSetActive(conn, pool, 0) < 0)
        return -1;

    return 0;
}
#endif

static int
virStorageBackendLogicalDeletePool(virConnectPtr conn,
                                   virStoragePoolObjPtr pool,
                                   unsigned int flags ATTRIBUTE_UNUSED)
{
    const char *cmdargv[] = {
        VGREMOVE, "-f", pool->def->name, NULL
    };

    if (virRun(conn, (char**)cmdargv, NULL) < 0)
        return -1;

    /* XXX clear the PVs too ? ie pvremove ? probably ought to */

    return 0;
}


static int
virStorageBackendLogicalDeleteVol(virConnectPtr conn,
                                  virStoragePoolObjPtr pool,
                                  virStorageVolDefPtr vol,
                                  unsigned int flags);


static int
virStorageBackendLogicalCreateVol(virConnectPtr conn,
                                  virStoragePoolObjPtr pool,
                                  virStorageVolDefPtr vol)
{
    int fd = -1;
    char size[100];
    const char *cmdargv[] = {
        LVCREATE, "--name", vol->name, "-L", size,
        pool->def->target.path, NULL
    };

    snprintf(size, sizeof(size)-1, "%lluK", vol->capacity/1024);
    size[sizeof(size)-1] = '\0';

    if (virRun(conn, (char**)cmdargv, NULL) < 0)
        return -1;

    if ((fd = open(vol->target.path, O_RDONLY)) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("cannot read path '%s': %s"),
                              vol->target.path, strerror(errno));
        goto cleanup;
    }

    /* We can only chown/grp if root */
    if (getuid() == 0) {
        if (fchown(fd, vol->target.perms.uid, vol->target.perms.gid) < 0) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("cannot set file owner '%s': %s"),
                                  vol->target.path, strerror(errno));
            goto cleanup;
        }
    }
    if (fchmod(fd, vol->target.perms.mode) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("cannot set file mode '%s': %s"),
                              vol->target.path, strerror(errno));
        goto cleanup;
    }

    if (close(fd) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("cannot close file '%s': %s"),
                              vol->target.path, strerror(errno));
        goto cleanup;
    }
    fd = -1;

    /* Fill in data about this new vol */
    if (virStorageBackendLogicalFindLVs(conn, pool, vol) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("cannot find newly created volume '%s': %s"),
                              vol->target.path, strerror(errno));
        goto cleanup;
    }

    return 0;

 cleanup:
    if (fd != -1)
        close(fd);
    virStorageBackendLogicalDeleteVol(conn, pool, vol, 0);
    return -1;
}

static int
virStorageBackendLogicalDeleteVol(virConnectPtr conn,
                                  virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
                                  virStorageVolDefPtr vol,
                                  unsigned int flags ATTRIBUTE_UNUSED)
{
    const char *cmdargv[] = {
        LVREMOVE, "-f", vol->target.path, NULL
    };

    if (virRun(conn, (char**)cmdargv, NULL) < 0)
        return -1;

    return 0;
}


virStorageBackend virStorageBackendLogical = {
    .type = VIR_STORAGE_POOL_LOGICAL,

    .startPool = virStorageBackendLogicalStartPool,
    .buildPool = virStorageBackendLogicalBuildPool,
    .refreshPool = virStorageBackendLogicalRefreshPool,
#if 0
    .stopPool = virStorageBackendLogicalStopPool,
#endif
    .deletePool = virStorageBackendLogicalDeletePool,
    .createVol = virStorageBackendLogicalCreateVol,
    .deleteVol = virStorageBackendLogicalDeleteVol,

    .poolOptions = {
        .formatFromString = virStorageBackendLogicalPoolFormatFromString,
        .formatToString = virStorageBackendLogicalPoolFormatToString,
    },

    .volType = VIR_STORAGE_VOL_BLOCK,
};