storage_util.c 136.0 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
/*
 * storage_util.c: helper functions for the storage driver
 *
 * 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 <regex.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
27
#include <sys/statvfs.h>
28 29 30 31 32 33 34 35 36
#include <sys/param.h>
#include <dirent.h>
#include "dirname.h"
#ifdef __linux__
# include <sys/ioctl.h>
# include <linux/fs.h>
# ifndef FS_NOCOW_FL
#  define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
# endif
37 38 39 40 41
# define default_mount_opts "nodev,nosuid,noexec"
#elif defined(__FreeBSD__)
# define default_mount_opts "nosuid,noexec"
#else
# define default_mount_opts ""
42 43 44
#endif

#if WITH_BLKID
45
# include <blkid.h>
46 47 48 49 50 51
#endif

#if WITH_SELINUX
# include <selinux/selinux.h>
#endif

52 53 54
#ifdef FICLONE
# define REFLINK_IOC_CLONE FICLONE
#elif HAVE_LINUX_BTRFS_H
55
# include <linux/btrfs.h>
56 57 58 59
# define REFLINK_IOC_CLONE BTRFS_IOC_CLONE
#elif HAVE_XFS_XFS_H
# include <xfs/xfs.h>
# define REFLINK_IOC_CLONE XFS_IOC_CLONE
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#endif

#include "datatypes.h"
#include "virerror.h"
#include "viralloc.h"
#include "internal.h"
#include "secret_conf.h"
#include "secret_util.h"
#include "vircrypto.h"
#include "viruuid.h"
#include "virstoragefile.h"
#include "storage_util.h"
#include "virlog.h"
#include "virfile.h"
#include "virjson.h"
#include "virqemu.h"
#include "stat-time.h"
#include "virstring.h"
#include "virxml.h"
79
#include "virfdstream.h"
80 81 82 83 84 85 86 87 88 89 90 91

#define VIR_FROM_THIS VIR_FROM_STORAGE

VIR_LOG_INIT("storage.storage_util");

#define READ_BLOCK_SIZE_DEFAULT  (1024 * 1024)
#define WRITE_BLOCK_SIZE_DEFAULT (4 * 1024)

/*
 * Perform the O(1) btrfs clone operation, if possible.
 * Upon success, return 0.  Otherwise, return -1 and set errno.
 */
92
#ifdef REFLINK_IOC_CLONE
93
static inline int
94
reflinkCloneFile(int dest_fd, int src_fd)
95
{
96
    return ioctl(dest_fd, REFLINK_IOC_CLONE, src_fd);
97 98 99
}
#else
static inline int
100 101
reflinkCloneFile(int dest_fd ATTRIBUTE_UNUSED,
                 int src_fd ATTRIBUTE_UNUSED)
102 103 104 105 106 107
{
    errno = ENOTSUP;
    return -1;
}
#endif

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
static int ATTRIBUTE_NONNULL(2)
virStorageBackendCopyToFD(virStorageVolDefPtr vol,
                          virStorageVolDefPtr inputvol,
                          int fd,
                          unsigned long long *total,
                          bool want_sparse,
                          bool reflink_copy)
{
    int inputfd = -1;
    int amtread = -1;
    int ret = 0;
    size_t rbytes = READ_BLOCK_SIZE_DEFAULT;
    int wbytes = 0;
    int interval;
    char *zerobuf = NULL;
    char *buf = NULL;
    struct stat st;

    if ((inputfd = open(inputvol->target.path, O_RDONLY)) < 0) {
        ret = -errno;
        virReportSystemError(errno,
                             _("could not open input path '%s'"),
                             inputvol->target.path);
        goto cleanup;
    }

#ifdef __linux__
    if (ioctl(fd, BLKBSZGET, &wbytes) < 0)
        wbytes = 0;
#endif
    if ((wbytes == 0) && fstat(fd, &st) == 0)
        wbytes = st.st_blksize;
    if (wbytes < WRITE_BLOCK_SIZE_DEFAULT)
        wbytes = WRITE_BLOCK_SIZE_DEFAULT;

    if (VIR_ALLOC_N(zerobuf, wbytes) < 0) {
        ret = -errno;
        goto cleanup;
    }

    if (VIR_ALLOC_N(buf, rbytes) < 0) {
        ret = -errno;
        goto cleanup;
    }

    if (reflink_copy) {
155
        if (reflinkCloneFile(fd, inputfd) < 0) {
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
            ret = -errno;
            virReportSystemError(errno,
                                 _("failed to clone files from '%s'"),
                                 inputvol->target.path);
            goto cleanup;
        } else {
            VIR_DEBUG("btrfs clone finished.");
            goto cleanup;
        }
    }

    while (amtread != 0) {
        int amtleft;

        if (*total < rbytes)
            rbytes = *total;

        if ((amtread = saferead(inputfd, buf, rbytes)) < 0) {
            ret = -errno;
            virReportSystemError(errno,
                                 _("failed reading from file '%s'"),
                                 inputvol->target.path);
            goto cleanup;
        }
        *total -= amtread;

        /* Loop over amt read in 512 byte increments, looking for sparse
         * blocks */
        amtleft = amtread;
        do {
            interval = ((wbytes > amtleft) ? amtleft : wbytes);
            int offset = amtread - amtleft;

            if (want_sparse && memcmp(buf+offset, zerobuf, interval) == 0) {
                if (lseek(fd, interval, SEEK_CUR) < 0) {
                    ret = -errno;
                    virReportSystemError(errno,
                                         _("cannot extend file '%s'"),
                                         vol->target.path);
                    goto cleanup;
                }
            } else if (safewrite(fd, buf+offset, interval) < 0) {
                ret = -errno;
                virReportSystemError(errno,
                                     _("failed writing to file '%s'"),
                                     vol->target.path);
                goto cleanup;

            }
        } while ((amtleft -= interval) > 0);
    }

    if (fdatasync(fd) < 0) {
        ret = -errno;
        virReportSystemError(errno, _("cannot sync data to file '%s'"),
                             vol->target.path);
        goto cleanup;
    }


    if (VIR_CLOSE(inputfd) < 0) {
        ret = -errno;
        virReportSystemError(errno,
                             _("cannot close file '%s'"),
                             inputvol->target.path);
        goto cleanup;
    }
    inputfd = -1;

 cleanup:
    VIR_FORCE_CLOSE(inputfd);

    VIR_FREE(zerobuf);
    VIR_FREE(buf);

    return ret;
}

static int
235
storageBackendCreateBlockFrom(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
236 237 238
                              virStorageVolDefPtr vol,
                              virStorageVolDefPtr inputvol,
                              unsigned int flags)
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
{
    int fd = -1;
    int ret = -1;
    unsigned long long remain;
    struct stat st;
    gid_t gid;
    uid_t uid;
    mode_t mode;
    bool reflink_copy = false;

    virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA |
                  VIR_STORAGE_VOL_CREATE_REFLINK,
                  -1);

    if (flags & VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("metadata preallocation is not supported for block "
                         "volumes"));
        goto cleanup;
    }

    if (flags & VIR_STORAGE_VOL_CREATE_REFLINK)
        reflink_copy = true;

    if ((fd = open(vol->target.path, O_RDWR)) < 0) {
        virReportSystemError(errno,
                             _("cannot create path '%s'"),
                             vol->target.path);
        goto cleanup;
    }

    remain = vol->target.capacity;

    if (inputvol) {
        if (virStorageBackendCopyToFD(vol, inputvol, fd, &remain,
                                      false, reflink_copy) < 0)
            goto cleanup;
    }

    if (fstat(fd, &st) == -1) {
        virReportSystemError(errno, _("stat of '%s' failed"),
                             vol->target.path);
        goto cleanup;
    }
    uid = (vol->target.perms->uid != st.st_uid) ? vol->target.perms->uid
284
        : (uid_t)-1;
285
    gid = (vol->target.perms->gid != st.st_gid) ? vol->target.perms->gid
286 287
        : (gid_t)-1;
    if (((uid != (uid_t)-1) || (gid != (gid_t)-1))
288 289 290
        && (fchown(fd, uid, gid) < 0)) {
        virReportSystemError(errno,
                             _("cannot chown '%s' to (%u, %u)"),
291 292
                             vol->target.path, (unsigned int)uid,
                             (unsigned int)gid);
293 294 295
        goto cleanup;
    }

296
    mode = (vol->target.perms->mode == (mode_t)-1 ?
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
            VIR_STORAGE_DEFAULT_VOL_PERM_MODE : vol->target.perms->mode);
    if (fchmod(fd, mode) < 0) {
        virReportSystemError(errno,
                             _("cannot set mode of '%s' to %04o"),
                             vol->target.path, mode);
        goto cleanup;
    }
    if (VIR_CLOSE(fd) < 0) {
        virReportSystemError(errno,
                             _("cannot close file '%s'"),
                             vol->target.path);
        goto cleanup;
    }
    fd = -1;

    ret = 0;
 cleanup:
    VIR_FORCE_CLOSE(fd);

    return ret;
}

static int
createRawFile(int fd, virStorageVolDefPtr vol,
              virStorageVolDefPtr inputvol,
              bool reflink_copy)
{
    bool need_alloc = true;
    int ret = 0;
    unsigned long long pos = 0;

    /* If the new allocation is lower than the capacity of the original file,
     * the cloned volume will be sparse */
    if (inputvol &&
        vol->target.allocation < inputvol->target.capacity)
        need_alloc = false;

    /* Seek to the final size, so the capacity is available upfront
     * for progress reporting */
    if (ftruncate(fd, vol->target.capacity) < 0) {
        ret = -errno;
        virReportSystemError(errno,
                             _("cannot extend file '%s'"),
                             vol->target.path);
        goto cleanup;
    }

/* Avoid issues with older kernel's <linux/fs.h> namespace pollution. */
#if HAVE_FALLOCATE - 0
    /* Try to preallocate all requested disk space, but fall back to
     * other methods if this fails with ENOSYS or EOPNOTSUPP. If allocation
     * is 0 (or less than 0), then fallocate will fail with EINVAL.
     * NOTE: do not use posix_fallocate; posix_fallocate falls back
     * to writing zeroes block by block in case fallocate isn't
     * available, and since we're going to copy data from another
     * file it doesn't make sense to write the file twice. */
    if (vol->target.allocation && need_alloc) {
        if (fallocate(fd, 0, 0, vol->target.allocation) == 0) {
            need_alloc = false;
        } else if (errno != ENOSYS && errno != EOPNOTSUPP) {
            ret = -errno;
            virReportSystemError(errno,
                                 _("cannot allocate %llu bytes in file '%s'"),
                                 vol->target.allocation, vol->target.path);
            goto cleanup;
        }
    }
#endif

    if (inputvol) {
        unsigned long long remain = inputvol->target.capacity;
        /* allow zero blocks to be skipped if we've requested sparse
         * allocation (allocation < capacity) or we have already
         * been able to allocate the required space. */
        if ((ret = virStorageBackendCopyToFD(vol, inputvol, fd, &remain,
                                             !need_alloc, reflink_copy)) < 0)
            goto cleanup;

        /* If the new allocation is greater than the original capacity,
         * but fallocate failed, fill the rest with zeroes.
         */
        pos = inputvol->target.capacity - remain;
    }

    if (need_alloc && (vol->target.allocation - pos > 0)) {
        if (safezero(fd, pos, vol->target.allocation - pos) < 0) {
            ret = -errno;
            virReportSystemError(errno, _("cannot fill file '%s'"),
                                 vol->target.path);
            goto cleanup;
        }
    }

    if (fsync(fd) < 0) {
        ret = -errno;
        virReportSystemError(errno, _("cannot sync data to file '%s'"),
                             vol->target.path);
        goto cleanup;
    }

 cleanup:
    return ret;
}

401
static int
402
storageBackendCreateRaw(virStoragePoolObjPtr pool,
403 404 405
                        virStorageVolDefPtr vol,
                        virStorageVolDefPtr inputvol,
                        unsigned int flags)
406
{
407
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    int ret = -1;
    int fd = -1;
    int operation_flags;
    bool reflink_copy = false;
    mode_t open_mode = VIR_STORAGE_DEFAULT_VOL_PERM_MODE;
    bool created = false;

    virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA |
                  VIR_STORAGE_VOL_CREATE_REFLINK,
                  -1);

    if (flags & VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("metadata preallocation is not supported for raw "
                         "volumes"));
        goto cleanup;
    }

426
    if (virStorageSourceHasBacking(&vol->target)) {
427 428 429 430 431 432 433 434 435
        virReportError(VIR_ERR_NO_SUPPORT, "%s",
                       _("backing storage not supported for raw volumes"));
        goto cleanup;
    }

    if (flags & VIR_STORAGE_VOL_CREATE_REFLINK)
        reflink_copy = true;


436
    if (vol->target.encryption) {
437 438 439 440 441 442
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("storage pool does not support encrypted volumes"));
        goto cleanup;
    }

    operation_flags = VIR_FILE_OPEN_FORCE_MODE | VIR_FILE_OPEN_FORCE_OWNER;
443
    if (def->type == VIR_STORAGE_POOL_NETFS)
444 445
        operation_flags |= VIR_FILE_OPEN_FORK;

446
    if (vol->target.perms->mode != (mode_t)-1)
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
        open_mode = vol->target.perms->mode;

    if ((fd = virFileOpenAs(vol->target.path,
                            O_RDWR | O_CREAT | O_EXCL,
                            open_mode,
                            vol->target.perms->uid,
                            vol->target.perms->gid,
                            operation_flags)) < 0) {
        virReportSystemError(-fd,
                             _("Failed to create file '%s'"),
                             vol->target.path);
        goto cleanup;
    }
    created = true;

    if (vol->target.nocow) {
#ifdef __linux__
        int attr;

        /* Set NOCOW flag. This is an optimisation for btrfs.
         * The FS_IOC_SETFLAGS ioctl return value will be ignored since any
         * failure of this operation should not block the volume creation.
         */
        if (ioctl(fd, FS_IOC_GETFLAGS, &attr) < 0) {
            virReportSystemError(errno, "%s", _("Failed to get fs flags"));
        } else {
            attr |= FS_NOCOW_FL;
            if (ioctl(fd, FS_IOC_SETFLAGS, &attr) < 0) {
                virReportSystemError(errno, "%s",
                                     _("Failed to set NOCOW flag"));
            }
        }
#endif
    }

    if ((ret = createRawFile(fd, vol, inputvol, reflink_copy)) < 0)
        /* createRawFile already reported the exact error. */
        ret = -1;

 cleanup:
    if (ret < 0 && created)
        ignore_value(virFileRemove(vol->target.path,
                                   vol->target.perms->uid,
                                   vol->target.perms->gid));
    VIR_FORCE_CLOSE(fd);
    return ret;
}


static int
virStorageBackendCreateExecCommand(virStoragePoolObjPtr pool,
                                   virStorageVolDefPtr vol,
                                   virCommandPtr cmd)
{
501
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
502 503 504
    struct stat st;
    gid_t gid;
    uid_t uid;
505
    mode_t mode = (vol->target.perms->mode == (mode_t)-1 ?
506 507 508 509 510
                   VIR_STORAGE_DEFAULT_VOL_PERM_MODE :
                   vol->target.perms->mode);
    bool filecreated = false;
    int ret = -1;

511
    if ((def->type == VIR_STORAGE_POOL_NETFS)
512
        && (((geteuid() == 0)
513
             && (vol->target.perms->uid != (uid_t)-1)
514
             && (vol->target.perms->uid != 0))
515
            || ((vol->target.perms->gid != (gid_t)-1)
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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
                && (vol->target.perms->gid != getegid())))) {

        virCommandSetUID(cmd, vol->target.perms->uid);
        virCommandSetGID(cmd, vol->target.perms->gid);
        virCommandSetUmask(cmd, S_IRWXUGO ^ mode);

        if (virCommandRun(cmd, NULL) == 0) {
            /* command was successfully run, check if the file was created */
            if (stat(vol->target.path, &st) >= 0) {
                filecreated = true;

                /* seems qemu-img disregards umask and open/creates using 0644.
                 * If that doesn't match what we expect, then let's try to
                 * re-open the file and attempt to force the mode change.
                 */
                if (mode != (st.st_mode & S_IRWXUGO)) {
                    int fd = -1;
                    int flags = VIR_FILE_OPEN_FORK | VIR_FILE_OPEN_FORCE_MODE;

                    if ((fd = virFileOpenAs(vol->target.path, O_RDWR, mode,
                                            vol->target.perms->uid,
                                            vol->target.perms->gid,
                                            flags)) >= 0) {
                        /* Success - means we're good */
                        VIR_FORCE_CLOSE(fd);
                        ret = 0;
                        goto cleanup;
                    }
                }
            }
        }
    }

    if (!filecreated) {
        /* don't change uid/gid/mode if we retry */
        virCommandSetUID(cmd, -1);
        virCommandSetGID(cmd, -1);
        virCommandSetUmask(cmd, 0);

        if (virCommandRun(cmd, NULL) < 0)
            goto cleanup;
        if (stat(vol->target.path, &st) < 0) {
            virReportSystemError(errno,
                                 _("failed to create %s"), vol->target.path);
            goto cleanup;
        }
        filecreated = true;
    }

    uid = (vol->target.perms->uid != st.st_uid) ? vol->target.perms->uid
566
        : (uid_t)-1;
567
    gid = (vol->target.perms->gid != st.st_gid) ? vol->target.perms->gid
568 569
        : (gid_t)-1;
    if (((uid != (uid_t)-1) || (gid != (gid_t)-1))
570 571 572
        && (chown(vol->target.path, uid, gid) < 0)) {
        virReportSystemError(errno,
                             _("cannot chown %s to (%u, %u)"),
573 574
                             vol->target.path, (unsigned int)uid,
                             (unsigned int)gid);
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
        goto cleanup;
    }

    if (mode != (st.st_mode & S_IRWXUGO) &&
        chmod(vol->target.path, mode) < 0) {
        virReportSystemError(errno,
                             _("cannot set mode of '%s' to %04o"),
                             vol->target.path, mode);
        goto cleanup;
    }

    ret = 0;

 cleanup:
    if (ret < 0 && filecreated)
        virFileRemove(vol->target.path, vol->target.perms->uid,
                      vol->target.perms->gid);
    return ret;
}

/* Create ploop directory with ploop image and DiskDescriptor.xml
 * if function fails to create image file the directory will be deleted.*/
597
static int
598
storageBackendCreatePloop(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
599 600 601
                          virStorageVolDefPtr vol,
                          virStorageVolDefPtr inputvol,
                          unsigned int flags)
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
{
    int ret = -1;
    virCommandPtr cmd = NULL;
    char *create_tool = NULL;
    bool created = false;

    virCheckFlags(0, -1);

    if (inputvol && inputvol->target.format != VIR_STORAGE_FILE_PLOOP) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported input storage vol type %d"),
                       inputvol->target.format);
        return -1;
    }

617
    if (vol->target.encryption) {
618 619 620 621 622 623
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("encrypted ploop volumes are not supported with "
                         "ploop init"));
        return -1;
    }

624
    if (virStorageSourceHasBacking(&vol->target)) {
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("copy-on-write ploop volumes are not yet supported"));
        return -1;
    }

    create_tool = virFindFileInPath("ploop");
    if (!create_tool && !inputvol) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to find ploop, please install "
                               "ploop tools"));
        return -1;
    }

    if (!inputvol) {
        if ((virDirCreate(vol->target.path,
640
                          (vol->target.perms->mode == (mode_t)-1 ?
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
                           VIR_STORAGE_DEFAULT_VOL_PERM_MODE:
                           vol->target.perms->mode),
                          vol->target.perms->uid,
                          vol->target.perms->gid,
                          0)) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("error creating directory for ploop volume"));
            goto cleanup;
        }
        cmd = virCommandNewArgList(create_tool, "init", "-s", NULL);
        virCommandAddArgFormat(cmd, "%lluM", VIR_DIV_UP(vol->target.capacity,
                                                        (1024 * 1024)));
        virCommandAddArgList(cmd, "-t", "ext4", NULL);
        virCommandAddArgFormat(cmd, "%s/root.hds", vol->target.path);

    } else {
        vol->target.capacity = inputvol->target.capacity;
        cmd = virCommandNewArgList("cp", "-r", inputvol->target.path,
                                   vol->target.path, NULL);
    }
    created = true;
    ret = virCommandRun(cmd, NULL);
 cleanup:
    virCommandFree(cmd);
    VIR_FREE(create_tool);
    if (ret < 0 && created)
        virFileDeleteTree(vol->target.path);
    return ret;
}

671 672 673 674

static int
storagePloopResize(virStorageVolDefPtr vol,
                   unsigned long long capacity)
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
{
    int ret = -1;
    virCommandPtr cmd = NULL;
    char *resize_tool = NULL;

    resize_tool = virFindFileInPath("ploop");
    if (!resize_tool) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unable to find ploop, please install ploop tools"));
        return -1;
    }
    cmd = virCommandNewArgList(resize_tool, "resize", "-s", NULL);
    virCommandAddArgFormat(cmd, "%lluM", VIR_DIV_UP(capacity, (1024 * 1024)));

    virCommandAddArgFormat(cmd, "%s/DiskDescriptor.xml", vol->target.path);

    ret = virCommandRun(cmd, NULL);
    virCommandFree(cmd);
    VIR_FREE(resize_tool);
    return ret;
}


/* The _virStorageBackendQemuImgInfo separates the command line building from
 * the volume definition so that qemuDomainSnapshotCreateInactiveExternal can
 * use it without needing to deal with a volume.
 */
struct _virStorageBackendQemuImgInfo {
    int format;
704
    const char *type;
705
    const char *inputType;
706 707
    const char *path;
    unsigned long long size_arg;
708
    unsigned long long allocation;
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
    bool encryption;
    bool preallocate;
    const char *compat;
    virBitmapPtr features;
    bool nocow;

    const char *backingPath;
    int backingFormat;

    const char *inputPath;
    const char *inputFormatStr;
    int inputFormat;

    char *secretAlias;
};


static int
727
storageBackendCreateQemuImgOpts(virStorageEncryptionInfoDefPtr encinfo,
728 729
                                char **opts,
                                struct _virStorageBackendQemuImgInfo info)
730 731 732
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

733 734 735 736
    if (info.backingPath)
        virBufferAsprintf(&buf, "backing_fmt=%s,",
                          virStorageFileFormatTypeToString(info.backingFormat));

737
    if (encinfo)
738
        virQEMUBuildQemuImgKeySecretOpts(&buf, encinfo, info.secretAlias);
739 740 741 742 743 744

    if (info.preallocate) {
        if (info.size_arg > info.allocation)
            virBufferAddLit(&buf, "preallocation=metadata,");
        else
            virBufferAddLit(&buf, "preallocation=falloc,");
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    }

    if (info.nocow)
        virBufferAddLit(&buf, "nocow=on,");

    if (info.compat)
        virBufferAsprintf(&buf, "compat=%s,", info.compat);

    if (info.features && info.format == VIR_STORAGE_FILE_QCOW2) {
        if (virBitmapIsBitSet(info.features,
                              VIR_STORAGE_FILE_FEATURE_LAZY_REFCOUNTS)) {
            if (STREQ_NULLABLE(info.compat, "0.10")) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("lazy_refcounts not supported with compat"
                                 " level %s"),
                               info.compat);
                goto error;
            }
            virBufferAddLit(&buf, "lazy_refcounts,");
        }
    }

    virBufferTrim(&buf, ",", -1);

    if (virBufferCheckError(&buf) < 0)
        goto error;

    *opts = virBufferContentAndReset(&buf);
    return 0;

 error:
    virBufferFreeAndReset(&buf);
    return -1;
}


781
/* storageBackendCreateQemuImgCheckEncryption:
782
 * @format: format of file found
J
John Ferlan 已提交
783
 * @type: TypeToString of format.type
784 785 786 787 788 789 790
 * @vol: pointer to volume def
 *
 * Ensure the proper setup for encryption.
 *
 * Returns 0 on success, -1 on failure w/ error set
 */
static int
791 792 793
storageBackendCreateQemuImgCheckEncryption(int format,
                                           const char *type,
                                           virStorageVolDefPtr vol)
794 795 796
{
    virStorageEncryptionPtr enc = vol->target.encryption;

797
    if (format == VIR_STORAGE_FILE_RAW) {
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
        if (enc->format != VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported volume encryption format %d"),
                           vol->target.encryption->format);
            return -1;
        }
        if (enc->nsecrets > 1) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("too many secrets for luks encryption"));
            return -1;
        }
        if (enc->nsecrets == 0) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("no secret provided for luks encryption"));
            return -1;
        }
        if (!virCryptoHaveCipher(VIR_CRYPTO_CIPHER_AES256CBC)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("luks encryption usage requires encrypted "
                             "secret generation to be supported"));
            return -1;
        }
    } else {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("volume encryption unsupported with format %s"), type);
        return -1;
    }

    return 0;
}


static int
831
storageBackendCreateQemuImgSetInput(virStorageVolDefPtr inputvol,
832
                                    virStorageVolEncryptConvertStep convertStep,
833
                                    struct _virStorageBackendQemuImgInfo *info)
834
{
835 836 837 838 839 840
    if (convertStep != VIR_STORAGE_VOL_ENCRYPT_CREATE) {
        if (!(info->inputPath = inputvol->target.path)) {
            virReportError(VIR_ERR_INVALID_ARG, "%s",
                           _("missing input volume target path"));
            return -1;
        }
841 842 843 844 845
    }

    info->inputFormat = inputvol->target.format;
    if (inputvol->type == VIR_STORAGE_VOL_BLOCK)
        info->inputFormat = VIR_STORAGE_FILE_RAW;
846 847
    if (info->inputFormat == VIR_STORAGE_FILE_ISO)
        info->inputFormat = VIR_STORAGE_FILE_RAW;
848 849 850 851 852 853 854 855 856 857 858 859 860
    if (!(info->inputFormatStr =
          virStorageFileFormatTypeToString(info->inputFormat))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage vol type %d"),
                       info->inputFormat);
        return -1;
    }

    return 0;
}


static int
861 862 863 864
storageBackendCreateQemuImgSetBacking(virStoragePoolObjPtr pool,
                                      virStorageVolDefPtr vol,
                                      virStorageVolDefPtr inputvol,
                                      struct _virStorageBackendQemuImgInfo *info)
865
{
866
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    int accessRetCode = -1;
    char *absolutePath = NULL;

    if (info->format == VIR_STORAGE_FILE_RAW) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("cannot set backing store for raw volume"));
        return -1;
    }

    info->backingFormat = vol->target.backingStore->format;
    info->backingPath = vol->target.backingStore->path;

    if (info->preallocate) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("metadata preallocation conflicts with backing"
                         " store"));
        return -1;
    }

    /* XXX: Not strictly required: qemu-img has an option a different
     * backing store, not really sure what use it serves though, and it
     * may cause issues with lvm. Untested essentially.
     */
890
    if (inputvol && virStorageSourceHasBacking(&inputvol->target) &&
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
        STRNEQ_NULLABLE(inputvol->target.backingStore->path,
                        info->backingPath)) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("a different backing store cannot be specified."));
        return -1;
    }

    if (!virStorageFileFormatTypeToString(info->backingFormat)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage vol backing store type %d"),
                       info->backingFormat);
        return -1;
    }

    /* Convert relative backing store paths to absolute paths for access
     * validation.
     */
    if ('/' != *(info->backingPath) &&
909
        virAsprintf(&absolutePath, "%s/%s", def->target.path,
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
                    info->backingPath) < 0)
        return -1;
    accessRetCode = access(absolutePath ? absolutePath :
                           info->backingPath, R_OK);
    VIR_FREE(absolutePath);
    if (accessRetCode != 0) {
        virReportSystemError(errno,
                             _("inaccessible backing store volume %s"),
                             info->backingPath);
        return -1;
    }

    return 0;
}


static int
927
storageBackendCreateQemuImgSetOptions(virCommandPtr cmd,
928
                                      virStorageEncryptionInfoDefPtr encinfo,
929
                                      struct _virStorageBackendQemuImgInfo info)
930 931 932
{
    char *opts = NULL;

933
    if (info.format == VIR_STORAGE_FILE_QCOW2 && !info.compat)
934 935
        info.compat = "0.10";

936
    if (storageBackendCreateQemuImgOpts(encinfo, &opts, info) < 0)
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
        return -1;
    if (opts)
        virCommandAddArgList(cmd, "-o", opts, NULL);
    VIR_FREE(opts);

    return 0;
}


/* Add a secret object to the command line:
 *    --object secret,id=$secretAlias,file=$secretPath
 *
 *    NB: format=raw is assumed
 */
static int
952
storageBackendCreateQemuImgSecretObject(virCommandPtr cmd,
953 954
                                        const char *secretPath,
                                        const char *secretAlias)
955 956 957 958
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *commandStr = NULL;

959 960
    virBufferAsprintf(&buf, "secret,id=%s,file=", secretAlias);
    virQEMUBuildBufferEscapeComma(&buf, secretPath);
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

    if (virBufferCheckError(&buf) < 0) {
        virBufferFreeAndReset(&buf);
        return -1;
    }

    commandStr = virBufferContentAndReset(&buf);

    virCommandAddArgList(cmd, "--object", commandStr, NULL);

    VIR_FREE(commandStr);
    return 0;
}


976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
/* Add a --image-opts to the qemu-img resize command line:
 *    --image-opts driver=luks,file.filename=$volpath,key-secret=$secretAlias
 *
 *    NB: format=raw is assumed
 */
static int
storageBackendResizeQemuImgImageOpts(virCommandPtr cmd,
                                     const char *path,
                                     const char *secretAlias)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *commandStr = NULL;

    virBufferAsprintf(&buf, "driver=luks,key-secret=%s,file.filename=",
                      secretAlias);
    virQEMUBuildBufferEscapeComma(&buf, path);

    if (virBufferCheckError(&buf) < 0) {
        virBufferFreeAndReset(&buf);
        return -1;
    }

    commandStr = virBufferContentAndReset(&buf);

    virCommandAddArgList(cmd, "--image-opts", commandStr, NULL);

    VIR_FREE(commandStr);
    return 0;
}


1007 1008 1009 1010
static int
virStorageBackendCreateQemuImgSetInfo(virStoragePoolObjPtr pool,
                                      virStorageVolDefPtr vol,
                                      virStorageVolDefPtr inputvol,
1011
                                      virStorageVolEncryptConvertStep convertStep,
1012
                                      struct _virStorageBackendQemuImgInfo *info)
1013 1014 1015
{
    /* Treat output block devices as 'raw' format */
    if (vol->type == VIR_STORAGE_VOL_BLOCK)
1016
        info->format = VIR_STORAGE_FILE_RAW;
1017

1018 1019
    if (info->format == VIR_STORAGE_FILE_ISO)
        info->format = VIR_STORAGE_FILE_RAW;
1020

1021
    if (!(info->type = virStorageFileFormatTypeToString(info->format))) {
1022 1023
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage vol type %d"),
1024 1025
                       info->format);
        return -1;
1026 1027
    }

1028 1029 1030 1031 1032 1033 1034 1035 1036
    if (inputvol &&
        !(info->inputType =
          virStorageFileFormatTypeToString(inputvol->target.format))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown inputvol storage vol type %d"),
                       inputvol->target.format);
        return -1;
    }

1037
    if (info->preallocate && info->format != VIR_STORAGE_FILE_QCOW2) {
1038 1039
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("metadata preallocation only available with qcow2"));
1040
        return -1;
1041
    }
1042
    if (info->compat && info->format != VIR_STORAGE_FILE_QCOW2) {
1043 1044
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("compatibility option only available with qcow2"));
1045
        return -1;
1046
    }
1047
    if (info->features && info->format != VIR_STORAGE_FILE_QCOW2) {
1048 1049
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("format features only available with qcow2"));
1050
        return -1;
1051
    }
1052
    if (info->format == VIR_STORAGE_FILE_RAW && vol->target.encryption) {
1053
        if (vol->target.encryption->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
1054
            info->type = "luks";
1055 1056 1057
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Only luks encryption is supported for raw files"));
1058
            return -1;
1059 1060 1061 1062
        }
    }

    if (inputvol &&
1063
        storageBackendCreateQemuImgSetInput(inputvol, convertStep, info) < 0)
1064
        return -1;
1065

1066
    if (virStorageSourceHasBacking(&vol->target) &&
1067 1068
        storageBackendCreateQemuImgSetBacking(pool, vol, inputvol, info) < 0)
        return -1;
1069

1070 1071 1072 1073
    if (info->encryption &&
        storageBackendCreateQemuImgCheckEncryption(info->format, info->type,
                                                   vol) < 0)
        return -1;
1074 1075

    /* Size in KB */
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    info->size_arg = VIR_DIV_UP(vol->target.capacity, 1024);

    return 0;
}


/* Create a qemu-img virCommand from the supplied arguments */
virCommandPtr
virStorageBackendCreateQemuImgCmdFromVol(virStoragePoolObjPtr pool,
                                         virStorageVolDefPtr vol,
                                         virStorageVolDefPtr inputvol,
                                         unsigned int flags,
                                         const char *create_tool,
1089
                                         const char *secretPath,
1090
                                         const char *inputSecretPath,
1091
                                         virStorageVolEncryptConvertStep convertStep)
1092 1093 1094 1095 1096
{
    virCommandPtr cmd = NULL;
    struct _virStorageBackendQemuImgInfo info = {
        .format = vol->target.format,
        .type = NULL,
1097
        .inputType = NULL,
1098 1099 1100 1101 1102 1103 1104 1105 1106
        .path = vol->target.path,
        .allocation = vol->target.allocation,
        .encryption = !!vol->target.encryption,
        .preallocate = !!(flags & VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA),
        .compat = vol->target.compat,
        .features = vol->target.features,
        .nocow = vol->target.nocow,
        .secretAlias = NULL,
    };
1107
    virStorageEncryptionPtr enc = vol->target.encryption;
1108 1109
    char *inputSecretAlias = NULL;
    virStorageEncryptionPtr inputenc = inputvol ? inputvol->target.encryption : NULL;
1110
    virStorageEncryptionInfoDefPtr encinfo = NULL;
1111 1112 1113

    virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, NULL);

1114 1115 1116 1117 1118 1119 1120 1121 1122
    if (enc && (enc->format == VIR_STORAGE_ENCRYPTION_FORMAT_QCOW ||
                enc->format == VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT) &&
        (vol->target.format == VIR_STORAGE_FILE_QCOW ||
         vol->target.format == VIR_STORAGE_FILE_QCOW2)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("creation of qcow2 encrypted image is not supported"));
        goto error;
    }

1123 1124 1125 1126 1127 1128
    if (inputenc && inputenc->format != VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("encryption format of inputvol must be LUKS"));
        goto error;
    }

1129 1130
    if (virStorageBackendCreateQemuImgSetInfo(pool, vol, inputvol,
                                              convertStep, &info) < 0)
1131
        goto error;
1132 1133 1134

    cmd = virCommandNew(create_tool);

1135 1136 1137
    /* ignore the backing volume when we're converting a volume
     * including when we're doing a two step convert during create */
    if (info.inputPath || convertStep == VIR_STORAGE_VOL_ENCRYPT_CREATE)
1138 1139
        info.backingPath = NULL;

1140 1141 1142
    /* Converting to use encryption is a two step process - step 1 is to
     * create the image and step 2 is to convert it using special arguments */
    if (info.inputPath && convertStep == VIR_STORAGE_VOL_ENCRYPT_NONE)
1143
        virCommandAddArgList(cmd, "convert", "-f", info.inputFormatStr,
1144
                             "-O", info.type, NULL);
1145 1146 1147
    else if (info.inputPath && convertStep == VIR_STORAGE_VOL_ENCRYPT_CONVERT)
        virCommandAddArgList(cmd, "convert", "--image-opts", "-n",
                             "--target-image-opts", NULL);
1148
    else
1149
        virCommandAddArgList(cmd, "create", "-f", info.type, NULL);
1150 1151 1152 1153

    if (info.backingPath)
        virCommandAddArgList(cmd, "-b", info.backingPath, NULL);

1154
    if (enc) {
1155
        if (!secretPath) {
1156 1157 1158 1159
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("path to secret data file is required"));
            goto error;
        }
1160
        if (virAsprintf(&info.secretAlias, "%s_encrypt0", vol->name) < 0)
1161
            goto error;
1162
        if (storageBackendCreateQemuImgSecretObject(cmd, secretPath,
1163
                                                    info.secretAlias) < 0)
1164
            goto error;
1165
        encinfo = &enc->encinfo;
1166 1167
    }

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
    if (inputenc && convertStep == VIR_STORAGE_VOL_ENCRYPT_CONVERT) {
        if (!inputSecretPath) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("path to inputvol secret data file is required"));
            goto error;
        }
        if (virAsprintf(&inputSecretAlias, "%s_encrypt0",
                        inputvol->name) < 0)
            goto error;
        if (storageBackendCreateQemuImgSecretObject(cmd, inputSecretPath,
                                                    inputSecretAlias) < 0)
            goto error;
    }

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    if (convertStep != VIR_STORAGE_VOL_ENCRYPT_CONVERT) {
        if (storageBackendCreateQemuImgSetOptions(cmd, encinfo, info) < 0)
            goto error;
        if (info.inputPath)
            virCommandAddArg(cmd, info.inputPath);
        virCommandAddArg(cmd, info.path);
        if (!info.inputPath && (info.size_arg || !info.backingPath))
            virCommandAddArgFormat(cmd, "%lluK", info.size_arg);
    } else {
        /* source */
1192 1193 1194 1195 1196 1197 1198 1199
        if (inputenc)
            virCommandAddArgFormat(cmd,
                                   "driver=luks,file.filename=%s,key-secret=%s",
                                   info.inputPath, inputSecretAlias);
        else
            virCommandAddArgFormat(cmd, "driver=%s,file.filename=%s",
                                   info.inputType ? info.inputType : "raw",
                                   info.inputPath);
1200

1201
        /* dest */
1202 1203 1204 1205 1206 1207 1208 1209
        if (enc)
            virCommandAddArgFormat(cmd,
                                   "driver=%s,file.filename=%s,key-secret=%s",
                                   info.type, info.path, info.secretAlias);
        else
            virCommandAddArgFormat(cmd, "driver=%s,file.filename=%s",
                                   info.type, info.path);

1210 1211
    }
    VIR_FREE(info.secretAlias);
1212
    VIR_FREE(inputSecretAlias);
1213 1214

    return cmd;
1215 1216 1217

 error:
    VIR_FREE(info.secretAlias);
1218
    VIR_FREE(inputSecretAlias);
1219 1220
    virCommandFree(cmd);
    return NULL;
1221 1222 1223 1224
}


static char *
1225
storageBackendCreateQemuImgSecretPath(virStoragePoolObjPtr pool,
1226
                                      virStorageVolDefPtr vol)
1227 1228 1229 1230 1231 1232
{
    virStorageEncryptionPtr enc = vol->target.encryption;
    char *secretPath = NULL;
    int fd = -1;
    uint8_t *secret = NULL;
    size_t secretlen = 0;
1233
    virConnectPtr conn = NULL;
1234 1235 1236 1237 1238 1239 1240

    if (!enc) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("missing encryption description"));
        return NULL;
    }

1241 1242 1243 1244 1245 1246 1247
    if (enc->nsecrets != 1) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("A single <secret type='passphrase'...> "
                         "element is expected in encryption description"));
        return NULL;
    }

1248 1249
    conn = virGetConnectSecret();
    if (!conn)
1250 1251 1252 1253 1254 1255 1256
        return NULL;

    if (!(secretPath = virStoragePoolObjBuildTempFilePath(pool, vol)))
        goto cleanup;

    if ((fd = mkostemp(secretPath, O_CLOEXEC)) < 0) {
        virReportSystemError(errno, "%s",
1257
                             _("failed to open secret file for write"));
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
        goto error;
    }

    if (virSecretGetSecretString(conn, &enc->secrets[0]->seclookupdef,
                                 VIR_SECRET_USAGE_TYPE_VOLUME,
                                 &secret, &secretlen) < 0)
        goto error;

    if (safewrite(fd, secret, secretlen) < 0) {
        virReportSystemError(errno, "%s",
1268
                             _("failed to write secret file"));
1269 1270 1271 1272
        goto error;
    }
    VIR_FORCE_CLOSE(fd);

1273 1274
    if ((vol->target.perms->uid != (uid_t)-1) &&
        (vol->target.perms->gid != (gid_t)-1)) {
1275 1276 1277
        if (chown(secretPath, vol->target.perms->uid,
                  vol->target.perms->gid) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1278
                           _("failed to chown secret file"));
1279 1280 1281 1282 1283
            goto error;
        }
    }

 cleanup:
1284
    virObjectUnref(conn);
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    VIR_DISPOSE_N(secret, secretlen);
    VIR_FORCE_CLOSE(fd);

    return secretPath;

 error:
    unlink(secretPath);
    VIR_FREE(secretPath);
    goto cleanup;
}


1297 1298 1299 1300 1301 1302
static int
storageBackendDoCreateQemuImg(virStoragePoolObjPtr pool,
                              virStorageVolDefPtr vol,
                              virStorageVolDefPtr inputvol,
                              unsigned int flags,
                              const char *create_tool,
1303
                              const char *secretPath,
1304
                              const char *inputSecretPath,
1305
                              virStorageVolEncryptConvertStep convertStep)
1306 1307 1308 1309 1310 1311
{
    int ret;
    virCommandPtr cmd;

    cmd = virStorageBackendCreateQemuImgCmdFromVol(pool, vol, inputvol,
                                                   flags, create_tool,
1312 1313
                                                   secretPath, inputSecretPath,
                                                   convertStep);
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
    if (!cmd)
        return -1;

    ret = virStorageBackendCreateExecCommand(pool, vol, cmd);

    virCommandFree(cmd);

    return ret;
}


1325
static int
1326
storageBackendCreateQemuImg(virStoragePoolObjPtr pool,
1327 1328 1329
                            virStorageVolDefPtr vol,
                            virStorageVolDefPtr inputvol,
                            unsigned int flags)
1330 1331 1332 1333
{
    int ret = -1;
    char *create_tool;
    char *secretPath = NULL;
1334
    char *inputSecretPath = NULL;
1335
    virStorageVolEncryptConvertStep convertStep = VIR_STORAGE_VOL_ENCRYPT_NONE;
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346

    virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, -1);

    create_tool = virFindFileInPath("qemu-img");
    if (!create_tool) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("creation of non-raw file images is "
                         "not supported without qemu-img."));
        return -1;
    }

1347 1348
    if (vol->target.encryption &&
        !(secretPath = storageBackendCreateQemuImgSecretPath(pool, vol)))
1349 1350
        goto cleanup;

1351 1352 1353 1354 1355
    if (inputvol && inputvol->target.encryption &&
        !(inputSecretPath = storageBackendCreateQemuImgSecretPath(pool,
                                                                  inputvol)))
        goto cleanup;

1356 1357 1358
    /* Using an input file for encryption requires a multi-step process
     * to create an image of the same size as the inputvol and then to
     * convert the inputvol afterwards. */
1359
    if ((secretPath || inputSecretPath) && inputvol)
1360 1361 1362 1363 1364
        convertStep = VIR_STORAGE_VOL_ENCRYPT_CREATE;

    do {
        ret = storageBackendDoCreateQemuImg(pool, vol, inputvol, flags,
                                            create_tool, secretPath,
1365
                                            inputSecretPath, convertStep);
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381

        /* Failure to convert, attempt to delete what we created */
        if (ret < 0 && convertStep == VIR_STORAGE_VOL_ENCRYPT_CONVERT)
            ignore_value(virFileRemove(vol->target.path,
                                       vol->target.perms->uid,
                                       vol->target.perms->gid));

        if (ret < 0 || convertStep == VIR_STORAGE_VOL_ENCRYPT_NONE)
            goto cleanup;

        if (convertStep == VIR_STORAGE_VOL_ENCRYPT_CREATE)
            convertStep = VIR_STORAGE_VOL_ENCRYPT_CONVERT;
        else if (convertStep == VIR_STORAGE_VOL_ENCRYPT_CONVERT)
            convertStep = VIR_STORAGE_VOL_ENCRYPT_DONE;
    } while (convertStep != VIR_STORAGE_VOL_ENCRYPT_DONE);

1382 1383 1384 1385 1386
 cleanup:
    if (secretPath) {
        unlink(secretPath);
        VIR_FREE(secretPath);
    }
1387 1388 1389 1390
    if (inputSecretPath) {
        unlink(inputSecretPath);
        VIR_FREE(inputSecretPath);
    }
1391 1392 1393 1394
    VIR_FREE(create_tool);
    return ret;
}

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412

/**
 * virStorageBackendCreateVolUsingQemuImg
 * @pool: Storage Pool Object
 * @vol: Volume definition
 * @inputvol: Volume to use for creation
 * @flags: Flags for creation options
 *
 * A shim to storageBackendCreateQemuImg to allow other backends to
 * utilize qemu-img processing in order to create or alter the volume.
 *
 * NB: If a volume target format is not supplied (per usual for some
 * backends), temporarily adjust the format to be RAW. Once completed,
 * reset the format back to NONE.
 *
 * Returns: 0 on success, -1 on failure.
 */
int
1413
virStorageBackendCreateVolUsingQemuImg(virStoragePoolObjPtr pool,
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
                                       virStorageVolDefPtr vol,
                                       virStorageVolDefPtr inputvol,
                                       unsigned int flags)
{
    int ret = -1;
    bool changeFormat = false;

    if (vol->target.format == VIR_STORAGE_FILE_NONE) {
        vol->target.format = VIR_STORAGE_FILE_RAW;
        changeFormat = true;
    }

1426
    ret = storageBackendCreateQemuImg(pool, vol, inputvol, flags);
1427 1428 1429 1430 1431 1432 1433 1434

    if (changeFormat)
        vol->target.format = VIR_STORAGE_FILE_NONE;

    return ret;
}


1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
virStorageBackendBuildVolFrom
virStorageBackendGetBuildVolFromFunction(virStorageVolDefPtr vol,
                                         virStorageVolDefPtr inputvol)
{
    if (!inputvol)
        return NULL;

    /* If either volume is a non-raw file vol, or uses encryption,
     * we need to use an external tool for converting
     */
    if ((vol->type == VIR_STORAGE_VOL_FILE &&
         (vol->target.format != VIR_STORAGE_FILE_RAW ||
1447
          vol->target.encryption)) ||
1448 1449
        (inputvol->type == VIR_STORAGE_VOL_FILE &&
         (inputvol->target.format != VIR_STORAGE_FILE_RAW ||
1450
          inputvol->target.encryption))) {
1451
        return storageBackendCreateQemuImg;
1452 1453 1454
    }

    if (vol->type == VIR_STORAGE_VOL_PLOOP)
1455
        return storageBackendCreatePloop;
1456
    if (vol->type == VIR_STORAGE_VOL_BLOCK)
1457
        return storageBackendCreateBlockFrom;
1458
    else
1459
        return storageBackendCreateRaw;
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
}


struct diskType {
    int part_table_type;
    unsigned short offset;
    unsigned short length;
    unsigned long long magic;
};


static struct diskType const disk_types[] = {
    { VIR_STORAGE_POOL_DISK_LVM2, 0x218, 8, 0x31303020324D564CULL },
    { VIR_STORAGE_POOL_DISK_GPT,  0x200, 8, 0x5452415020494645ULL },
    { VIR_STORAGE_POOL_DISK_DVH,  0x0,   4, 0x41A9E50BULL },
    { VIR_STORAGE_POOL_DISK_MAC,  0x0,   2, 0x5245ULL },
    { VIR_STORAGE_POOL_DISK_BSD,  0x40,  4, 0x82564557ULL },
    { VIR_STORAGE_POOL_DISK_SUN,  0x1fc, 2, 0xBEDAULL },
    /*
     * NOTE: pc98 is funky; the actual signature is 0x55AA (just like dos), so
     * we can't use that.  At the moment I'm relying on the "dummy" IPL
     * bootloader data that comes from parted.  Luckily, the chances of running
     * into a pc98 machine running libvirt are approximately nil.
     */
    /*{ 0x1fe, 2, 0xAA55UL },*/
    { VIR_STORAGE_POOL_DISK_PC98, 0x0,   8, 0x314C5049000000CBULL },
    /*
     * NOTE: the order is important here; some other disk types (like GPT and
     * and PC98) also have 0x55AA at this offset.  For that reason, the DOS
     * one must be the last one.
     */
    { VIR_STORAGE_POOL_DISK_DOS,  0x1fe, 2, 0xAA55ULL },
    { -1,                         0x0,   0, 0x0ULL },
};


/*
 * virStorageBackendDetectBlockVolFormatFD
 * @target: target definition ptr of volume to update
 * @fd: fd of storage volume to update,
 * @readflags: VolReadErrorMode flags to handle read error after open
 *             is successful, but read is not.
 *
 * Returns 0 for success, -1 on a legitimate error condition, -2 if
 * the read error is desired to be ignored (along with appropriate
 * VIR_WARN of the issue).
 */
static int
virStorageBackendDetectBlockVolFormatFD(virStorageSourcePtr target,
                                        int fd,
                                        unsigned int readflags)
{
    size_t i;
    off_t start;
    unsigned char buffer[1024];
    ssize_t bytes;

    /* make sure to set the target format "unknown" to begin with */
    target->format = VIR_STORAGE_POOL_DISK_UNKNOWN;

    start = lseek(fd, 0, SEEK_SET);
    if (start < 0) {
        virReportSystemError(errno,
                             _("cannot seek to beginning of file '%s'"),
                             target->path);
        return -1;
    }
    bytes = saferead(fd, buffer, sizeof(buffer));
    if (bytes < 0) {
        if (readflags & VIR_STORAGE_VOL_READ_NOERROR) {
            VIR_WARN("ignoring failed saferead of file '%s'",
                     target->path);
            return -2;
        } else {
            virReportSystemError(errno,
                                 _("cannot read beginning of file '%s'"),
                                 target->path);
            return -1;
        }
    }

    for (i = 0; disk_types[i].part_table_type != -1; i++) {
        if (disk_types[i].offset + disk_types[i].length > bytes)
            continue;
        if (memcmp(buffer+disk_types[i].offset, &disk_types[i].magic,
            disk_types[i].length) == 0) {
            target->format = disk_types[i].part_table_type;
            break;
        }
    }

    if (target->format == VIR_STORAGE_POOL_DISK_UNKNOWN)
        VIR_DEBUG("cannot determine the target format for '%s'",
                  target->path);

    return 0;
}


/*
 * Allows caller to silently ignore files with improper mode
 *
 * Returns -1 on error. If VIR_STORAGE_VOL_OPEN_NOERROR is passed, we
 * return -2 if file mode is unexpected or the volume is a dangling
 * symbolic link.
 */
int
virStorageBackendVolOpen(const char *path, struct stat *sb,
                         unsigned int flags)
{
    int fd, mode = 0;
    char *base = last_component(path);
    bool noerror = (flags & VIR_STORAGE_VOL_OPEN_NOERROR);

    if (lstat(path, sb) < 0) {
        if (errno == ENOENT) {
            if (noerror) {
                VIR_WARN("ignoring missing file '%s'", path);
                return -2;
            }
            virReportError(VIR_ERR_NO_STORAGE_VOL,
                           _("no storage vol with matching path '%s'"),
                           path);
            return -1;
        }
        virReportSystemError(errno,
                             _("cannot stat file '%s'"),
                             path);
        return -1;
    }

    if (S_ISFIFO(sb->st_mode)) {
        if (noerror) {
            VIR_WARN("ignoring FIFO '%s'", path);
            return -2;
        }
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Volume path '%s' is a FIFO"), path);
        return -1;
    } else if (S_ISSOCK(sb->st_mode)) {
        if (noerror) {
            VIR_WARN("ignoring socket '%s'", path);
            return -2;
        }
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Volume path '%s' is a socket"), path);
        return -1;
    }

    /* O_NONBLOCK should only matter during open() for fifos and
     * sockets, which we already filtered; but using it prevents a
     * TOCTTOU race.  However, later on we will want to read() the
     * header from this fd, and virFileRead* routines require a
     * blocking fd, so fix it up after verifying we avoided a race.
     *
     * Use of virFileOpenAs allows this path to open a file using
     * the uid and gid as it was created in order to open. Since this
     * path is not using O_CREAT or O_TMPFILE, mode is meaningless.
     * Opening under user/group is especially important in an NFS
     * root-squash environment. If the target path isn't on shared
     * file system, the open will fail in the OPEN_FORK path.
     */
    if ((fd = virFileOpenAs(path, O_RDONLY|O_NONBLOCK|O_NOCTTY,
                            0, sb->st_uid, sb->st_gid,
                            VIR_FILE_OPEN_NOFORK|VIR_FILE_OPEN_FORK)) < 0) {
        if ((errno == ENOENT || errno == ELOOP) &&
            S_ISLNK(sb->st_mode) && noerror) {
            VIR_WARN("ignoring dangling symlink '%s'", path);
            return -2;
        }
        if (errno == ENOENT && noerror) {
            VIR_WARN("ignoring missing file '%s'", path);
            return -2;
        }
        if (errno == ENXIO && noerror) {
            VIR_WARN("ignoring missing fifo '%s'", path);
            return -2;
        }
        if ((errno == EACCES || errno == EPERM) && noerror) {
            VIR_WARN("ignoring permission error for '%s'", path);
            return -2;
        }

        virReportSystemError(errno, _("cannot open volume '%s'"), path);
        return -1;
    }

    if (fstat(fd, sb) < 0) {
        virReportSystemError(errno, _("cannot stat file '%s'"), path);
        VIR_FORCE_CLOSE(fd);
        return -1;
    }

    if (S_ISREG(sb->st_mode)) {
        mode = VIR_STORAGE_VOL_OPEN_REG;
    } else if (S_ISCHR(sb->st_mode)) {
        mode = VIR_STORAGE_VOL_OPEN_CHAR;
    } else if (S_ISBLK(sb->st_mode)) {
        mode = VIR_STORAGE_VOL_OPEN_BLOCK;
    } else if (S_ISDIR(sb->st_mode)) {
        mode = VIR_STORAGE_VOL_OPEN_DIR;

        if (STREQ(base, ".") ||
            STREQ(base, "..")) {
            VIR_FORCE_CLOSE(fd);
            if (noerror) {
                VIR_INFO("Skipping special dir '%s'", base);
                return -2;
            }
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Cannot use volume path '%s'"), path);
            return -1;
        }
    } else {
        VIR_FORCE_CLOSE(fd);
        if (noerror) {
            VIR_WARN("ignoring unexpected type for file '%s'", path);
            return -2;
        }
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected type for file '%s'"), path);
        return -1;
    }

    if (virSetBlocking(fd, true) < 0) {
        VIR_FORCE_CLOSE(fd);
        virReportSystemError(errno, _("unable to set blocking mode for '%s'"),
                             path);
        return -1;
    }

    if (!(mode & flags)) {
        VIR_FORCE_CLOSE(fd);
        if (noerror) {
            VIR_INFO("Skipping volume '%s'", path);
            return -2;
        }

        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected storage mode for '%s'"), path);
        return -1;
    }

    return fd;
}

/* virStorageIsPloop function checks whether given directory is ploop volume's
 * directory.
 */
1709 1710
static bool
storageBackendIsPloopDir(char *path)
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
{
    bool ret = false;
    char *root = NULL;
    char *desc = NULL;
    if (virAsprintf(&root, "%s/root.hds", path) < 0)
        return ret;
    if (!virFileExists(root))
        goto cleanup;
    if (virAsprintf(&desc, "%s/DiskDescriptor.xml", path) < 0)
        goto cleanup;
    if (!virFileExists(desc))
        goto cleanup;

    ret = true;
 cleanup:
    VIR_FREE(root);
    VIR_FREE(desc);
    return ret;
}

/* In case of ploop volumes, path to volume is the path to the ploop
 * directory. To get information about allocation, header information
 * and etc. we need to perform virStorageBackendVolOpen and
 * virStorageBackendUpdateVolTargetFd once again.
 */
1736 1737 1738
static int
storageBackendRedoPloopUpdate(virStorageSourcePtr target, struct stat *sb,
                              int *fd, unsigned int flags)
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
{
    char *path = NULL;
    int ret = -1;

    if (virAsprintf(&path, "%s/root.hds", target->path) < 0)
        return -1;
    VIR_FORCE_CLOSE(*fd);
    if ((*fd = virStorageBackendVolOpen(path, sb, flags)) < 0)
        goto cleanup;
    ret = virStorageBackendUpdateVolTargetInfoFD(target, *fd, sb);

 cleanup:

    VIR_FREE(path);
    return ret;
}

/*
1757
 * storageBackendUpdateVolTargetInfo
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
 * @voltype: Volume type
 * @target: target definition ptr of volume to update
 * @withBlockVolFormat: true if caller determined a block file
 * @openflags: various VolOpenCheckMode flags to handle errors on open
 * @readflags: VolReadErrorMode flags to handle read error after open
 *             is successful, but read is not.
 *
 * Returns 0 for success, -1 on a legitimate error condition, and -2
 * if the openflags used VIR_STORAGE_VOL_OPEN_NOERROR and some sort of
 * open error occurred. It is up to the caller to handle. A -2 may also
 * be returned if the caller passed a readflagsflag.
 */
1770 1771 1772 1773 1774 1775
static int
storageBackendUpdateVolTargetInfo(virStorageVolType voltype,
                                  virStorageSourcePtr target,
                                  bool withBlockVolFormat,
                                  unsigned int openflags,
                                  unsigned int readflags)
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
{
    int ret, fd = -1;
    struct stat sb;
    char *buf = NULL;
    ssize_t len = VIR_STORAGE_MAX_HEADER;

    if ((ret = virStorageBackendVolOpen(target->path, &sb, openflags)) < 0)
        goto cleanup;
    fd = ret;

    if ((ret = virStorageBackendUpdateVolTargetInfoFD(target, fd, &sb)) < 0)
        goto cleanup;

    if ((voltype == VIR_STORAGE_VOL_FILE || voltype == VIR_STORAGE_VOL_BLOCK) &&
        target->format != VIR_STORAGE_FILE_NONE) {
        if (S_ISDIR(sb.st_mode)) {
1792 1793 1794
            if (storageBackendIsPloopDir(target->path)) {
                if ((ret = storageBackendRedoPloopUpdate(target, &sb, &fd,
                                                         openflags)) < 0)
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
                    goto cleanup;
                target->format = VIR_STORAGE_FILE_PLOOP;
            } else {
                ret = 0;
                goto cleanup;
            }
        }

        if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
            virReportSystemError(errno, _("cannot seek to start of '%s'"), target->path);
            ret = -1;
            goto cleanup;
        }

        if ((len = virFileReadHeaderFD(fd, len, &buf)) < 0) {
            if (readflags & VIR_STORAGE_VOL_READ_NOERROR) {
                VIR_WARN("ignoring failed header read for '%s'",
                         target->path);
                ret = -2;
            } else {
                virReportSystemError(errno,
                                     _("cannot read header '%s'"),
                                     target->path);
                ret = -1;
            }
            goto cleanup;
        }

        if (virStorageSourceUpdateCapacity(target, buf, len, false) < 0) {
            ret = -1;
            goto cleanup;
        }
    }

    if (withBlockVolFormat) {
        if ((ret = virStorageBackendDetectBlockVolFormatFD(target, fd,
                                                           readflags)) < 0)
            goto cleanup;
    }

 cleanup:
    VIR_FORCE_CLOSE(fd);
    VIR_FREE(buf);
    return ret;
}

/*
 * virStorageBackendUpdateVolInfo
 * @vol: Pointer to a volume storage definition
 * @withBlockVolFormat: true if the caller determined a block file
 * @openflags: various VolOpenCheckMode flags to handle errors on open
 * @readflags: various VolReadErrorMode flags to handle errors on read
 *
 * Returns 0 for success, -1 on a legitimate error condition, and -2
 * if the openflags used VIR_STORAGE_VOL_OPEN_NOERROR and some sort of
 * open error occurred. It is up to the caller to handle.
 */
int
virStorageBackendUpdateVolInfo(virStorageVolDefPtr vol,
                               bool withBlockVolFormat,
                               unsigned int openflags,
                               unsigned int readflags)
{
    int ret;

1860 1861 1862 1863
    if ((ret = storageBackendUpdateVolTargetInfo(vol->type,
                                                 &vol->target,
                                                 withBlockVolFormat,
                                                 openflags, readflags)) < 0)
1864 1865
        return ret;

1866
    if (virStorageSourceHasBacking(&vol->target) &&
1867 1868 1869 1870 1871
        (ret = storageBackendUpdateVolTargetInfo(VIR_STORAGE_VOL_FILE,
                                                 vol->target.backingStore,
                                                 withBlockVolFormat,
                                                 VIR_STORAGE_VOL_OPEN_DEFAULT |
                                                 VIR_STORAGE_VOL_OPEN_NOERROR,
1872
                                                 readflags)) == -1)
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
        return ret;

    return 0;
}

/*
 * virStorageBackendUpdateVolTargetInfoFD:
 * @target: target definition ptr of volume to update
 * @fd: fd of storage volume to update, via virStorageBackendOpenVol*, or -1
 * @sb: details about file (must match @fd, if that is provided)
 *
 * Returns 0 for success, -1 on a legitimate error condition.
 */
int
virStorageBackendUpdateVolTargetInfoFD(virStorageSourcePtr target,
                                       int fd,
                                       struct stat *sb)
{
#if WITH_SELINUX
    security_context_t filecon = NULL;
#endif

    if (virStorageSourceUpdateBackingSizes(target, fd, sb) < 0)
        return -1;

    if (!target->perms && VIR_ALLOC(target->perms) < 0)
        return -1;
    target->perms->mode = sb->st_mode & S_IRWXUGO;
    target->perms->uid = sb->st_uid;
    target->perms->gid = sb->st_gid;

    if (!target->timestamps && VIR_ALLOC(target->timestamps) < 0)
        return -1;
    target->timestamps->atime = get_stat_atime(sb);
    target->timestamps->btime = get_stat_birthtime(sb);
    target->timestamps->ctime = get_stat_ctime(sb);
    target->timestamps->mtime = get_stat_mtime(sb);

1911 1912
    target->type = VIR_STORAGE_TYPE_FILE;

1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
    VIR_FREE(target->perms->label);

#if WITH_SELINUX
    /* XXX: make this a security driver call */
    if (fd >= 0) {
        if (fgetfilecon_raw(fd, &filecon) == -1) {
            if (errno != ENODATA && errno != ENOTSUP) {
                virReportSystemError(errno,
                                     _("cannot get file context of '%s'"),
                                     target->path);
                return -1;
            }
        } else {
            if (VIR_STRDUP(target->perms->label, filecon) < 0) {
                freecon(filecon);
                return -1;
            }
            freecon(filecon);
        }
    }
#endif

    return 0;
}

bool
virStorageBackendPoolPathIsStable(const char *path)
{
    if (path == NULL || STREQ(path, "/dev") || STREQ(path, "/dev/"))
        return false;

    if (!STRPREFIX(path, "/dev/"))
        return false;

    return true;
}

/*
 * Given a volume path directly in /dev/XXX, iterate over the
1952
 * entries in the directory def->target.path and find the
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
 * first symlink pointing to the volume path.
 *
 * If, the target.path is /dev/, then return the original volume
 * path.
 *
 * If no symlink is found, then return the original volume path
 *
 * Typically target.path is one of the /dev/disk/by-XXX dirs
 * with stable paths.
 *
 * If 'loop' is true, we use a timeout loop to give dynamic paths
 * a change to appear.
 */
char *
virStorageBackendStablePath(virStoragePoolObjPtr pool,
                            const char *devpath,
                            bool loop)
{
1971
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
1972 1973 1974 1975 1976 1977 1978 1979
    DIR *dh;
    struct dirent *dent;
    char *stablepath;
    int opentries = 0;
    int retry = 0;
    int direrr;

    /* Logical pools are under /dev but already have stable paths */
1980 1981
    if (def->type == VIR_STORAGE_POOL_LOGICAL ||
        !virStorageBackendPoolPathIsStable(def->target.path))
1982 1983 1984 1985 1986 1987 1988
        goto ret_strdup;

    /* We loop here because /dev/disk/by-{id,path} may not have existed
     * before we started this operation, so we have to give it some time to
     * get created.
     */
 reopen:
1989
    if (virDirOpenQuiet(&dh, def->target.path) < 0) {
1990 1991 1992 1993 1994 1995 1996
        opentries++;
        if (loop && errno == ENOENT && opentries < 50) {
            usleep(100 * 1000);
            goto reopen;
        }
        virReportSystemError(errno,
                             _("cannot read dir '%s'"),
1997
                             def->target.path);
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
        return NULL;
    }

    /* The pool is pointing somewhere like /dev/disk/by-path
     * or /dev/disk/by-id, so we need to check all symlinks in
     * the target directory and figure out which one points
     * to this device node.
     *
     * And it might need some time till the stable path shows
     * up, so add timeout to retry here.  Ignore readdir failures,
     * since we have a fallback.
     */
 retry:
    while ((direrr = virDirRead(dh, &dent, NULL)) > 0) {
        if (virAsprintf(&stablepath, "%s/%s",
2013
                        def->target.path, dent->d_name) < 0) {
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
            VIR_DIR_CLOSE(dh);
            return NULL;
        }

        if (virFileLinkPointsTo(stablepath, devpath)) {
            VIR_DIR_CLOSE(dh);
            return stablepath;
        }

        VIR_FREE(stablepath);
    }

    if (!direrr && loop && ++retry < 100) {
        usleep(100 * 1000);
        goto retry;
    }

    VIR_DIR_CLOSE(dh);

 ret_strdup:
    /* Couldn't find any matching stable link so give back
     * the original non-stable dev path
     */

    ignore_value(VIR_STRDUP(stablepath, devpath));

    return stablepath;
}

2043 2044
/* Common/Local File System/Directory Volume API's */
static int
2045
createFileDir(virStoragePoolObjPtr pool,
2046 2047 2048 2049
              virStorageVolDefPtr vol,
              virStorageVolDefPtr inputvol,
              unsigned int flags)
{
2050
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
    int err;

    virCheckFlags(0, -1);

    if (inputvol) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s",
                       _("cannot copy from volume to a directory volume"));
        return -1;
    }

2062
    if (virStorageSourceHasBacking(&vol->target)) {
2063 2064 2065 2066 2067 2068 2069
        virReportError(VIR_ERR_NO_SUPPORT, "%s",
                       _("backing storage not supported for directories volumes"));
        return -1;
    }


    if ((err = virDirCreate(vol->target.path,
2070
                            (vol->target.perms->mode == (mode_t)-1 ?
2071 2072 2073 2074
                             VIR_STORAGE_DEFAULT_VOL_PERM_MODE :
                             vol->target.perms->mode),
                            vol->target.perms->uid,
                            vol->target.perms->gid,
2075
                            (def->type == VIR_STORAGE_POOL_NETFS
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
                             ? VIR_DIR_CREATE_AS_UID : 0))) < 0) {
        return -1;
    }

    return 0;
}


/**
 * Set up a volume definition to be added to a pool's volume list, but
 * don't do any file creation or allocation. By separating the two processes,
 * we allow allocation progress reporting (by polling the volume's 'info'
 * function), and can drop the parent pool lock during the (slow) allocation.
 */
int
2091
virStorageBackendVolCreateLocal(virStoragePoolObjPtr pool,
2092 2093
                                virStorageVolDefPtr vol)
{
2094 2095
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);

2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
    if (vol->target.format == VIR_STORAGE_FILE_DIR)
        vol->type = VIR_STORAGE_VOL_DIR;
    else if (vol->target.format == VIR_STORAGE_FILE_PLOOP)
        vol->type = VIR_STORAGE_VOL_PLOOP;
    else
        vol->type = VIR_STORAGE_VOL_FILE;

    /* Volumes within a directory pools are not recursive; do not
     * allow escape to ../ or a subdir */
    if (strchr(vol->name, '/')) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume name '%s' cannot contain '/'"), vol->name);
        return -1;
    }

    VIR_FREE(vol->target.path);
    if (virAsprintf(&vol->target.path, "%s/%s",
2113
                    def->target.path, vol->name) < 0)
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
        return -1;

    if (virFileExists(vol->target.path)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("volume target path '%s' already exists"),
                       vol->target.path);
        return -1;
    }

    VIR_FREE(vol->key);
    return VIR_STRDUP(vol->key, vol->target.path);
}


static int
2129
storageBackendVolBuildLocal(virStoragePoolObjPtr pool,
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
                            virStorageVolDefPtr vol,
                            virStorageVolDefPtr inputvol,
                            unsigned int flags)
{
    virStorageBackendBuildVolFrom create_func;

    if (inputvol) {
        if (!(create_func =
              virStorageBackendGetBuildVolFromFunction(vol, inputvol)))
            return -1;
    } else if (vol->target.format == VIR_STORAGE_FILE_RAW &&
               vol->target.encryption == NULL) {
        create_func = storageBackendCreateRaw;
    } else if (vol->target.format == VIR_STORAGE_FILE_DIR) {
        create_func = createFileDir;
    } else if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
        create_func = storageBackendCreatePloop;
    } else {
        create_func = storageBackendCreateQemuImg;
    }

2151
    if (create_func(pool, vol, inputvol, flags) < 0)
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
        return -1;
    return 0;
}


/**
 * Allocate a new file as a volume. This is either done directly
 * for raw/sparse files, or by calling qemu-img for
 * special kinds of files
 */
int
2163
virStorageBackendVolBuildLocal(virStoragePoolObjPtr pool,
2164 2165 2166
                               virStorageVolDefPtr vol,
                               unsigned int flags)
{
2167
    return storageBackendVolBuildLocal(pool, vol, NULL, flags);
2168 2169 2170 2171 2172 2173 2174
}


/*
 * Create a storage vol using 'inputvol' as input
 */
int
2175
virStorageBackendVolBuildFromLocal(virStoragePoolObjPtr pool,
2176 2177 2178 2179
                                   virStorageVolDefPtr vol,
                                   virStorageVolDefPtr inputvol,
                                   unsigned int flags)
{
2180
    return storageBackendVolBuildLocal(pool, vol, inputvol, flags);
2181 2182 2183 2184 2185 2186 2187
}


/**
 * Remove a volume - no support for BLOCK and NETWORK yet
 */
int
2188
virStorageBackendVolDeleteLocal(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
2189 2190 2191 2192 2193
                                virStorageVolDefPtr vol,
                                unsigned int flags)
{
    virCheckFlags(0, -1);

2194
    switch ((virStorageVolType)vol->type) {
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
    case VIR_STORAGE_VOL_FILE:
    case VIR_STORAGE_VOL_DIR:
        if (virFileRemove(vol->target.path, vol->target.perms->uid,
                          vol->target.perms->gid) < 0) {
            /* Silently ignore failures where the vol has already gone away */
            if (errno != ENOENT) {
                if (vol->type == VIR_STORAGE_VOL_FILE)
                    virReportSystemError(errno,
                                         _("cannot unlink file '%s'"),
                                         vol->target.path);
                else
                    virReportSystemError(errno,
                                         _("cannot remove directory '%s'"),
                                         vol->target.path);
                return -1;
            }
        }
        break;
    case VIR_STORAGE_VOL_PLOOP:
        if (virFileDeleteTree(vol->target.path) < 0)
            return -1;
        break;
    case VIR_STORAGE_VOL_BLOCK:
    case VIR_STORAGE_VOL_NETWORK:
    case VIR_STORAGE_VOL_NETDIR:
    case VIR_STORAGE_VOL_LAST:
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("removing block or network volumes is not supported: %s"),
                       vol->target.path);
        return -1;
    }
    return 0;
}


/* storageBackendLoadDefaultSecrets:
 * @vol: volume being refreshed
 *
 * If the volume had a secret generated, we need to regenerate the
 * encryption secret information
 *
 * Returns 0 if no secret or secret setup was successful,
 * -1 on failures w/ error message set
 */
static int
2240
storageBackendLoadDefaultSecrets(virStorageVolDefPtr vol)
2241 2242 2243
{
    virSecretPtr sec;
    virStorageEncryptionSecretPtr encsec = NULL;
2244
    virConnectPtr conn = NULL;
2245 2246 2247 2248

    if (!vol->target.encryption || vol->target.encryption->nsecrets != 0)
        return 0;

2249 2250 2251 2252
    conn = virGetConnectSecret();
    if (!conn)
        return -1;

2253 2254 2255 2256 2257 2258 2259
    /* The encryption secret for qcow2 and luks volumes use the path
     * to the volume, so look for a secret with the path. If not found,
     * then we cannot generate the secret after a refresh (or restart).
     * This may be the case if someone didn't follow instructions and created
     * a usage string that although matched with the secret usage string,
     * didn't contain the path to the volume. We won't error in that case,
     * but we also cannot find the secret. */
2260 2261 2262 2263
    sec = virSecretLookupByUsage(conn, VIR_SECRET_USAGE_TYPE_VOLUME,
                                 vol->target.path);
    virObjectUnref(conn);
    if (!sec)
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
        return 0;

    if (VIR_ALLOC_N(vol->target.encryption->secrets, 1) < 0 ||
        VIR_ALLOC(encsec) < 0) {
        VIR_FREE(vol->target.encryption->secrets);
        virObjectUnref(sec);
        return -1;
    }

    vol->target.encryption->nsecrets = 1;
    vol->target.encryption->secrets[0] = encsec;

    encsec->type = VIR_STORAGE_ENCRYPTION_SECRET_TYPE_PASSPHRASE;
    encsec->seclookupdef.type = VIR_SECRET_LOOKUP_TYPE_UUID;
    virSecretGetUUID(sec, encsec->seclookupdef.u.uuid);
    virObjectUnref(sec);

    return 0;
}


/**
 * Update info about a volume's capacity/allocation
 */
int
2289
virStorageBackendVolRefreshLocal(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
                                 virStorageVolDefPtr vol)
{
    int ret;

    /* Refresh allocation / capacity / permissions info in case its changed */
    if ((ret = virStorageBackendUpdateVolInfo(vol, false,
                                              VIR_STORAGE_VOL_FS_OPEN_FLAGS,
                                              0)) < 0)
        return ret;

    /* Load any secrets if possible */
2301
    return storageBackendLoadDefaultSecrets(vol);
2302 2303 2304 2305
}


static int
2306
storageBackendResizeQemuImg(virStoragePoolObjPtr pool,
2307
                            virStorageVolDefPtr vol,
2308 2309 2310
                            unsigned long long capacity)
{
    int ret = -1;
2311
    char *img_tool = NULL;
2312
    virCommandPtr cmd = NULL;
2313 2314 2315
    const char *type;
    char *secretPath = NULL;
    char *secretAlias = NULL;
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
    virStorageEncryptionPtr enc = vol->target.encryption;

    if (enc && (enc->format == VIR_STORAGE_ENCRYPTION_FORMAT_QCOW ||
                enc->format == VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT) &&
        (vol->target.format == VIR_STORAGE_FILE_QCOW ||
         vol->target.format == VIR_STORAGE_FILE_QCOW2)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("resize of qcow2 encrypted image is not supported"));
        return -1;
    }
2326 2327 2328 2329 2330 2331 2332 2333

    img_tool = virFindFileInPath("qemu-img");
    if (!img_tool) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to find qemu-img"));
        return -1;
    }

2334 2335 2336 2337 2338 2339
    if (vol->target.encryption) {
        if (vol->target.format == VIR_STORAGE_FILE_RAW)
            type = "luks";
        else
            type = virStorageFileFormatTypeToString(vol->target.format);

2340
        storageBackendLoadDefaultSecrets(vol);
2341 2342

        if (storageBackendCreateQemuImgCheckEncryption(vol->target.format,
2343
                                                       type, vol) < 0)
2344 2345 2346
            goto cleanup;

        if (!(secretPath =
2347
              storageBackendCreateQemuImgSecretPath(pool, vol)))
2348 2349
            goto cleanup;

2350
        if (virAsprintf(&secretAlias, "%s_encrypt0", vol->name) < 0)
2351 2352 2353
            goto cleanup;
    }

2354 2355 2356 2357
    /* Round capacity as qemu-img resize errors out on sizes which are not
     * a multiple of 512 */
    capacity = VIR_ROUND_UP(capacity, 512);

2358 2359 2360
    cmd = virCommandNewArgList(img_tool, "resize", NULL);
    if (capacity < vol->target.capacity)
        virCommandAddArg(cmd, "--shrink");
2361
    if (!vol->target.encryption) {
2362
        virCommandAddArg(cmd, vol->target.path);
2363 2364 2365 2366 2367 2368 2369 2370 2371
    } else {
        if (storageBackendCreateQemuImgSecretObject(cmd, secretPath,
                                                    secretAlias) < 0)
            goto cleanup;

        if (storageBackendResizeQemuImgImageOpts(cmd, vol->target.path,
                                                 secretAlias) < 0)
            goto cleanup;
    }
2372 2373 2374 2375
    virCommandAddArgFormat(cmd, "%llu", capacity);

    ret = virCommandRun(cmd, NULL);

2376
 cleanup:
2377
    VIR_FREE(img_tool);
2378 2379 2380 2381 2382
    if (secretPath) {
        unlink(secretPath);
        VIR_FREE(secretPath);
    }
    VIR_FREE(secretAlias);
2383 2384 2385 2386 2387 2388 2389 2390 2391
    virCommandFree(cmd);
    return ret;
}


/**
 * Resize a volume
 */
int
2392
virStorageBackendVolResizeLocal(virStoragePoolObjPtr pool,
2393 2394 2395 2396
                                virStorageVolDefPtr vol,
                                unsigned long long capacity,
                                unsigned int flags)
{
2397 2398
    bool pre_allocate = flags & VIR_STORAGE_VOL_RESIZE_ALLOCATE;

2399 2400 2401
    virCheckFlags(VIR_STORAGE_VOL_RESIZE_ALLOCATE |
                  VIR_STORAGE_VOL_RESIZE_SHRINK, -1);

2402
    if (vol->target.format == VIR_STORAGE_FILE_RAW && !vol->target.encryption) {
2403
        return virStorageFileResize(vol->target.path, capacity, pre_allocate);
2404 2405 2406
    } else if (vol->target.format == VIR_STORAGE_FILE_RAW && vol->target.encryption) {
        if (pre_allocate) {
            virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
2407 2408
                           _("preallocate is only supported for an "
                             "unencrypted raw volume"));
2409 2410 2411
            return -1;
        }

2412
        return storageBackendResizeQemuImg(pool, vol, capacity);
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
    } else if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
        return storagePloopResize(vol, capacity);
    } else {
        if (pre_allocate) {
            virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                           _("preallocate is only supported for raw "
                             "type volume"));
            return -1;
        }

2423
        return storageBackendResizeQemuImg(pool, vol, capacity);
2424 2425 2426 2427
    }
}


2428 2429 2430 2431 2432 2433 2434
/*
 *  Check whether the ploop image has snapshots.
 *  return: -1 - failed to check
 *           0 - no snapshots
 *           1 - at least one snapshot
 */
static int
2435
storageBackendPloopHasSnapshots(char *path)
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
{
    virCommandPtr cmd = NULL;
    char *output = NULL;
    char *snap_tool = NULL;
    int ret = -1;

    snap_tool = virFindFileInPath("ploop");
    if (!snap_tool) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to find ploop, please install "
                               "ploop tools"));
        return ret;
    }

    cmd = virCommandNewArgList(snap_tool, "snapshot-list", NULL);
    virCommandAddArgFormat(cmd, "%s/DiskDescriptor.xml", path);
    virCommandSetOutputBuffer(cmd, &output);

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

    if (!strstr(output, "root.hds.")) {
        ret = 1;
        goto cleanup;
    }
    ret = 0;

 cleanup:
    VIR_FREE(output);
    virCommandFree(cmd);
    return ret;
}

2469

2470
int
2471
virStorageBackendVolUploadLocal(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
                                virStorageVolDefPtr vol,
                                virStreamPtr stream,
                                unsigned long long offset,
                                unsigned long long len,
                                unsigned int flags)
{
    char *path = NULL;
    char *target_path = vol->target.path;
    int ret = -1;
    int has_snap = 0;
2482
    bool sparse = flags & VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM;
2483

2484
    virCheckFlags(VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM, -1);
2485 2486 2487 2488 2489 2490
    /* if volume has target format VIR_STORAGE_FILE_PLOOP
     * we need to restore DiskDescriptor.xml, according to
     * new contents of volume. This operation will be perfomed
     * when volUpload is fully finished. */
    if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
        /* Fail if the volume contains snapshots or we failed to check it.*/
2491
        has_snap = storageBackendPloopHasSnapshots(vol->target.path);
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
        if (has_snap < 0) {
            goto cleanup;
        } else if (!has_snap) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("can't upload volume, all existing snapshots"
                             " will be lost"));
            goto cleanup;
        }

        if (virAsprintf(&path, "%s/root.hds", vol->target.path) < 0)
            return -1;
        target_path = path;
    }

    /* Not using O_CREAT because the file is required to already exist at
     * this point */
    ret = virFDStreamOpenBlockDevice(stream, target_path,
2509
                                     offset, len, sparse, O_WRONLY);
2510 2511 2512 2513 2514 2515 2516

 cleanup:
    VIR_FREE(path);
    return ret;
}

int
2517
virStorageBackendVolDownloadLocal(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
                                  virStorageVolDefPtr vol,
                                  virStreamPtr stream,
                                  unsigned long long offset,
                                  unsigned long long len,
                                  unsigned int flags)
{
    char *path = NULL;
    char *target_path = vol->target.path;
    int ret = -1;
    int has_snap = 0;
2528
    bool sparse = flags & VIR_STORAGE_VOL_DOWNLOAD_SPARSE_STREAM;
2529

2530
    virCheckFlags(VIR_STORAGE_VOL_DOWNLOAD_SPARSE_STREAM, -1);
2531
    if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
2532
        has_snap = storageBackendPloopHasSnapshots(vol->target.path);
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
        if (has_snap < 0) {
            goto cleanup;
        } else if (!has_snap) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("can't download volume, all existing snapshots"
                             " will be lost"));
            goto cleanup;
        }
        if (virAsprintf(&path, "%s/root.hds", vol->target.path) < 0)
            goto cleanup;
        target_path = path;
    }

    ret = virFDStreamOpenBlockDevice(stream, target_path,
2547
                                     offset, len, sparse, O_RDONLY);
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569

 cleanup:
    VIR_FREE(path);
    return ret;
}


/* If the volume we're wiping is already a sparse file, we simply
 * truncate and extend it to its original size, filling it with
 * zeroes.  This behavior is guaranteed by POSIX:
 *
 * http://www.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
 *
 * If fildes refers to a regular file, the ftruncate() function shall
 * cause the size of the file to be truncated to length. If the size
 * of the file previously exceeded length, the extra data shall no
 * longer be available to reads on the file. If the file previously
 * was smaller than this size, ftruncate() shall increase the size of
 * the file. If the file size is increased, the extended area shall
 * appear as if it were zero-filled.
 */
static int
2570 2571 2572
storageBackendVolZeroSparseFileLocal(const char *path,
                                     off_t size,
                                     int fd)
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
{
    if (ftruncate(fd, 0) < 0) {
        virReportSystemError(errno,
                             _("Failed to truncate volume with "
                               "path '%s' to 0 bytes"),
                             path);
        return -1;
    }

    if (ftruncate(fd, size) < 0) {
        virReportSystemError(errno,
                             _("Failed to truncate volume with "
                               "path '%s' to %ju bytes"),
                             path, (uintmax_t)size);
        return -1;
    }

    return 0;
}


static int
2595 2596 2597
storageBackendWipeLocal(const char *path,
                        int fd,
                        unsigned long long wipe_len,
2598 2599
                        size_t writebuf_length,
                        bool zero_end)
2600 2601 2602
{
    int ret = -1, written = 0;
    unsigned long long remaining = 0;
2603
    off_t size;
2604 2605 2606 2607 2608 2609
    size_t write_size = 0;
    char *writebuf = NULL;

    if (VIR_ALLOC_N(writebuf, writebuf_length) < 0)
        goto cleanup;

2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
    if (!zero_end) {
        if ((size = lseek(fd, 0, SEEK_SET)) < 0) {
            virReportSystemError(errno,
                                 _("Failed to seek to the start in volume "
                                   "with path '%s'"),
                                 path);
            goto cleanup;
        }
    } else {
        if ((size = lseek(fd, -wipe_len, SEEK_END)) < 0) {
            virReportSystemError(errno,
                                 _("Failed to seek to %llu bytes to the end "
                                   "in volume with path '%s'"),
                                 wipe_len, path);
            goto cleanup;
        }
2626 2627
    }

2628
    VIR_DEBUG("wiping start: %zd len: %llu", (ssize_t)size, wipe_len);
2629

2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
    remaining = wipe_len;
    while (remaining > 0) {

        write_size = (writebuf_length < remaining) ? writebuf_length : remaining;
        written = safewrite(fd, writebuf, write_size);
        if (written < 0) {
            virReportSystemError(errno,
                                 _("Failed to write %zu bytes to "
                                   "storage volume with path '%s'"),
                                 write_size, path);

            goto cleanup;
        }

        remaining -= written;
    }

    if (fdatasync(fd) < 0) {
        virReportSystemError(errno,
                             _("cannot sync data to volume with path '%s'"),
                             path);
        goto cleanup;
    }

    VIR_DEBUG("Wrote %llu bytes to volume with path '%s'", wipe_len, path);

    ret = 0;

 cleanup:
    VIR_FREE(writebuf);
    return ret;
}


static int
2665 2666
storageBackendVolWipeLocalFile(const char *path,
                               unsigned int algorithm,
2667 2668
                               unsigned long long allocation,
                               bool zero_end)
2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
{
    int ret = -1, fd = -1;
    const char *alg_char = NULL;
    struct stat st;
    virCommandPtr cmd = NULL;

    fd = open(path, O_RDWR);
    if (fd == -1) {
        virReportSystemError(errno,
                             _("Failed to open storage volume with path '%s'"),
                             path);
        goto cleanup;
    }

    if (fstat(fd, &st) == -1) {
        virReportSystemError(errno,
                             _("Failed to stat storage volume with path '%s'"),
                             path);
        goto cleanup;
    }

    switch ((virStorageVolWipeAlgorithm) algorithm) {
    case VIR_STORAGE_VOL_WIPE_ALG_ZERO:
        alg_char = "zero";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_NNSA:
        alg_char = "nnsa";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_DOD:
        alg_char = "dod";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_BSI:
        alg_char = "bsi";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_GUTMANN:
        alg_char = "gutmann";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_SCHNEIER:
        alg_char = "schneier";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_PFITZNER7:
        alg_char = "pfitzner7";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_PFITZNER33:
        alg_char = "pfitzner33";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_RANDOM:
        alg_char = "random";
        break;
    case VIR_STORAGE_VOL_WIPE_ALG_TRIM:
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("'trim' algorithm not supported"));
        goto cleanup;
    case VIR_STORAGE_VOL_WIPE_ALG_LAST:
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported algorithm %d"),
                       algorithm);
        goto cleanup;
    }

    VIR_DEBUG("Wiping file '%s' with algorithm '%s'", path, alg_char);

    if (algorithm != VIR_STORAGE_VOL_WIPE_ALG_ZERO) {
        cmd = virCommandNew(SCRUB);
        virCommandAddArgList(cmd, "-f", "-p", alg_char, path, NULL);

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

        ret = 0;
    } else {
        if (S_ISREG(st.st_mode) && st.st_blocks < (st.st_size / DEV_BSIZE)) {
2741
            ret = storageBackendVolZeroSparseFileLocal(path, st.st_size, fd);
2742
        } else {
2743 2744
            ret = storageBackendWipeLocal(path, fd, allocation, st.st_blksize,
                                          zero_end);
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
        }
        if (ret < 0)
            goto cleanup;
    }

 cleanup:
    virCommandFree(cmd);
    VIR_FORCE_CLOSE(fd);
    return ret;
}


static int
2758 2759
storageBackendVolWipePloop(virStorageVolDefPtr vol,
                           unsigned int algorithm)
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780
{
    virCommandPtr cmd = NULL;
    char *target_path = NULL;
    char *disk_desc = NULL;
    char *create_tool = NULL;

    int ret = -1;

    create_tool = virFindFileInPath("ploop");
    if (!create_tool) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unable to find ploop tools, please install them"));
        return -1;
    }

    if (virAsprintf(&target_path, "%s/root.hds", vol->target.path) < 0)
        goto cleanup;

    if (virAsprintf(&disk_desc, "%s/DiskDescriptor.xml", vol->target.path) < 0)
        goto cleanup;

2781
    if (storageBackendVolWipeLocalFile(target_path, algorithm,
2782
                                       vol->target.allocation, false) < 0)
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
        goto cleanup;

    if (virFileRemove(disk_desc, 0, 0) < 0) {
        virReportError(errno, _("Failed to delete DiskDescriptor.xml of volume '%s'"),
                       vol->target.path);
        goto cleanup;
    }
    if (virFileRemove(target_path, 0, 0) < 0) {
        virReportError(errno, _("failed to delete root.hds of volume '%s'"),
                       vol->target.path);
        goto cleanup;
    }

    cmd = virCommandNewArgList(create_tool, "init", "-s", NULL);

    virCommandAddArgFormat(cmd, "%lluM", VIR_DIV_UP(vol->target.capacity,
                                                    (1024 * 1024)));
    virCommandAddArgList(cmd, "-t", "ext4", NULL);
    virCommandAddArg(cmd, target_path);
    ret = virCommandRun(cmd, NULL);

 cleanup:
    VIR_FREE(disk_desc);
    VIR_FREE(target_path);
    VIR_FREE(create_tool);
    virCommandFree(cmd);
    return ret;
}


int
2814
virStorageBackendVolWipeLocal(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
                              virStorageVolDefPtr vol,
                              unsigned int algorithm,
                              unsigned int flags)
{
    int ret = -1;

    virCheckFlags(0, -1);

    VIR_DEBUG("Wiping volume with path '%s' and algorithm %u",
              vol->target.path, algorithm);

    if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
2827
        ret = storageBackendVolWipePloop(vol, algorithm);
2828
    } else {
2829
        ret = storageBackendVolWipeLocalFile(vol->target.path, algorithm,
2830
                                             vol->target.allocation, false);
2831 2832 2833 2834 2835 2836
    }

    return ret;
}


2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
/**
 * @pool: storage pool to build
 * @dir_create_flags: flags for directory creation
 *
 * Common code to build a directory based storage pool
 *
 * Returns 0 on success, -1 on failure
 */
int
virStorageBackendBuildLocal(virStoragePoolObjPtr pool)
{
2848
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
2849 2850 2851 2852 2853 2854 2855
    int ret = -1;
    char *parent = NULL;
    char *p = NULL;
    mode_t mode;
    bool needs_create_as_uid;
    unsigned int dir_create_flags;

2856
    if (VIR_STRDUP(parent, def->target.path) < 0)
2857 2858 2859 2860
        goto cleanup;
    if (!(p = strrchr(parent, '/'))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("path '%s' is not absolute"),
2861
                       def->target.path);
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
        goto cleanup;
    }

    if (p != parent) {
        /* assure all directories in the path prior to the final dir
         * exist, with default uid/gid/mode. */
        *p = '\0';
        if (virFileMakePath(parent) < 0) {
            virReportSystemError(errno, _("cannot create path '%s'"),
                                 parent);
            goto cleanup;
        }
    }

    dir_create_flags = VIR_DIR_CREATE_ALLOW_EXIST;
2877 2878
    needs_create_as_uid = (def->type == VIR_STORAGE_POOL_NETFS);
    mode = def->target.perms.mode;
2879

2880
    if (mode == (mode_t)-1 &&
2881
        (needs_create_as_uid || !virFileExists(def->target.path)))
2882 2883 2884 2885 2886 2887 2888
        mode = VIR_STORAGE_DEFAULT_POOL_PERM_MODE;
    if (needs_create_as_uid)
        dir_create_flags |= VIR_DIR_CREATE_AS_UID;

    /* Now create the final dir in the path with the uid/gid/mode
     * requested in the config. If the dir already exists, just set
     * the perms. */
2889
    if (virDirCreate(def->target.path,
2890
                     mode,
2891 2892
                     def->target.perms.uid,
                     def->target.perms.gid,
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912
                     dir_create_flags) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    VIR_FREE(parent);
    return ret;
}


/**
 * @conn connection to report errors against
 * @pool storage pool to delete
 *
 * Delete a directory based storage pool
 *
 * Returns 0 on success, -1 on error
 */
int
2913
virStorageBackendDeleteLocal(virStoragePoolObjPtr pool,
2914 2915
                             unsigned int flags)
{
2916 2917
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);

2918 2919 2920 2921
    virCheckFlags(0, -1);

    /* XXX delete all vols first ? */

2922
    if (rmdir(def->target.path) < 0) {
2923 2924
        virReportSystemError(errno,
                             _("failed to remove pool '%s'"),
2925
                             def->target.path);
2926 2927 2928 2929 2930 2931 2932
        return -1;
    }

    return 0;
}


2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
int
virStorageUtilGlusterExtractPoolSources(const char *host,
                                        const char *xml,
                                        virStoragePoolSourceListPtr list,
                                        virStoragePoolType pooltype)
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
    virStoragePoolSource *src = NULL;
2943
    char *volname = NULL;
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
    size_t i;
    int nnodes;
    int ret = -1;

    if (!(doc = virXMLParseStringCtxt(xml, _("(gluster_cli_output)"), &ctxt)))
        goto cleanup;

    if ((nnodes = virXPathNodeSet("//volumes/volume", ctxt, &nodes)) < 0)
        goto cleanup;

    for (i = 0; i < nnodes; i++) {
        ctxt->node = nodes[i];

        if (!(src = virStoragePoolSourceListNewSource(list)))
            goto cleanup;

2960
        if (!(volname = virXPathString("string(./name)", ctxt))) {
2961 2962 2963 2964 2965
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to extract gluster volume name"));
            goto cleanup;
        }

2966
        if (pooltype == VIR_STORAGE_POOL_NETFS) {
2967
            src->format = VIR_STORAGE_POOL_NETFS_GLUSTERFS;
2968
            VIR_STEAL_PTR(src->dir, volname);
2969 2970 2971
        } else if (pooltype == VIR_STORAGE_POOL_GLUSTER) {
            if (VIR_STRDUP(src->dir, "/") < 0)
                goto cleanup;
2972
            VIR_STEAL_PTR(src->name, volname);
2973 2974 2975 2976 2977
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("unsupported gluster lookup"));
            goto cleanup;
        }
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989

        if (VIR_ALLOC_N(src->hosts, 1) < 0)
            goto cleanup;
        src->nhost = 1;

        if (VIR_STRDUP(src->hosts[0].name, host) < 0)
            goto cleanup;
    }

    ret = nnodes;

 cleanup:
2990
    VIR_FREE(volname);
2991 2992 2993 2994 2995 2996 2997 2998
    VIR_FREE(nodes);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);

    return ret;
}


2999 3000 3001
/**
 * virStorageBackendFindGlusterPoolSources:
 * @host: host to detect volumes on
3002
 * @pooltype: type of the pool
3003 3004 3005 3006 3007
 * @list: list of storage pool sources to be filled
 * @report: report error if the 'gluster' cli tool is missing
 *
 * Looks up gluster volumes on @host and fills them to @list.
 *
3008 3009 3010 3011
 * @pooltype allows to influence the specific differences between netfs and
 * native gluster pools. Users should pass only VIR_STORAGE_POOL_NETFS or
 * VIR_STORAGE_POOL_GLUSTER.
 *
3012 3013 3014 3015
 * Returns number of volumes on the host on success, or -1 on error.
 */
int
virStorageBackendFindGlusterPoolSources(const char *host,
3016
                                        virStoragePoolType pooltype,
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
                                        virStoragePoolSourceListPtr list,
                                        bool report)
{
    char *glusterpath = NULL;
    char *outbuf = NULL;
    virCommandPtr cmd = NULL;
    int rc;

    int ret = -1;

3027
    if (!(glusterpath = virFindFileInPath("gluster"))) {
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052
        if (report) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("'gluster' command line tool not found"));
            return -1;
        } else {
            return 0;
        }
    }

    cmd = virCommandNewArgList(glusterpath,
                               "--xml",
                               "--log-file=/dev/null",
                               "volume", "info", "all", NULL);

    virCommandAddArgFormat(cmd, "--remote-host=%s", host);
    virCommandSetOutputBuffer(cmd, &outbuf);

    if (virCommandRun(cmd, &rc) < 0)
        goto cleanup;

    if (rc != 0) {
        ret = 0;
        goto cleanup;
    }

3053
    ret = virStorageUtilGlusterExtractPoolSources(host, outbuf, list, pooltype);
3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130

 cleanup:
    VIR_FREE(outbuf);
    virCommandFree(cmd);
    VIR_FREE(glusterpath);
    return ret;
}


#if WITH_BLKID

typedef enum {
    VIR_STORAGE_BLKID_PROBE_ERROR = -1,
    VIR_STORAGE_BLKID_PROBE_UNDEFINED, /* Nothing found */
    VIR_STORAGE_BLKID_PROBE_UNKNOWN,   /* Don't know libvirt fs/part type */
    VIR_STORAGE_BLKID_PROBE_MATCH,     /* Matches the on disk format */
    VIR_STORAGE_BLKID_PROBE_DIFFERENT, /* Format doesn't match on disk format */
} virStorageBackendBLKIDProbeResult;

/*
 * Utility function to probe for a file system on the device using the
 * blkid "superblock" (e.g. default) APIs.
 *
 * NB: In general this helper will handle the virStoragePoolFormatFileSystem
 *     format types; however, if called from the Disk path, the initial fstype
 *     check will fail forcing the usage of the ProbePart helper.
 *
 * Returns virStorageBackendBLKIDProbeResult enum
 */
static virStorageBackendBLKIDProbeResult
virStorageBackendBLKIDFindFS(blkid_probe probe,
                             const char *device,
                             const char *format)
{
    const char *fstype = NULL;

    /* Make sure we're doing a superblock probe from the start */
    blkid_probe_enable_superblocks(probe, true);
    blkid_probe_reset_superblocks_filter(probe);

    if (blkid_do_probe(probe) != 0) {
        VIR_INFO("No filesystem found on device '%s'", device);
        return VIR_STORAGE_BLKID_PROBE_UNDEFINED;
    }

    if (blkid_probe_lookup_value(probe, "TYPE", &fstype, NULL) == 0) {
        if (STREQ(fstype, format))
            return VIR_STORAGE_BLKID_PROBE_MATCH;

        return VIR_STORAGE_BLKID_PROBE_DIFFERENT;
    }

    if (blkid_known_fstype(format) == 0)
        return VIR_STORAGE_BLKID_PROBE_UNKNOWN;

    return VIR_STORAGE_BLKID_PROBE_ERROR;
}


/*
 * Utility function to probe for a partition on the device using the
 * blkid "partitions" APIs.
 *
 * NB: In general, this API will be validating the virStoragePoolFormatDisk
 *     format types.
 *
 * Returns virStorageBackendBLKIDProbeResult enum
 */
static virStorageBackendBLKIDProbeResult
virStorageBackendBLKIDFindPart(blkid_probe probe,
                               const char *device,
                               const char *format)
{
    const char *pttype = NULL;

    /* A blkid_known_pttype on "dvh" and "pc98" returns a failure;
     * however, the blkid_do_probe for "dvh" returns "sgi" and
3131 3132 3133
     * for "pc98" it returns "dos". Although "bsd" is recognized,
     * it seems that the parted created partition table is not being
     * properly recogized. Since each of these will cause problems
3134 3135
     * with startup comparison, let's just treat them as UNKNOWN causing
     * the caller to fallback to using PARTED */
3136
    if (STREQ(format, "dvh") || STREQ(format, "pc98") || STREQ(format, "bsd"))
3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
        return VIR_STORAGE_BLKID_PROBE_UNKNOWN;

    /* Make sure we're doing a partitions probe from the start */
    blkid_probe_enable_partitions(probe, true);
    blkid_probe_reset_partitions_filter(probe);

    if (blkid_do_probe(probe) != 0) {
        VIR_INFO("No partition found on device '%s'", device);
        return VIR_STORAGE_BLKID_PROBE_UNDEFINED;
    }

    if (blkid_probe_lookup_value(probe, "PTTYPE", &pttype, NULL) == 0) {
        if (STREQ(pttype, format))
            return VIR_STORAGE_BLKID_PROBE_MATCH;

        return VIR_STORAGE_BLKID_PROBE_DIFFERENT;
    }

    if (blkid_known_pttype(format) == 0)
        return VIR_STORAGE_BLKID_PROBE_UNKNOWN;

    return VIR_STORAGE_BLKID_PROBE_ERROR;
}


/*
 * @device: Path to device
 * @format: Desired format
 * @writelabel: True if desire to write the label
 *
 * Use the blkid_ APIs in order to get details regarding whether a file
 * system or partition exists on the disk already.
 *
 * Returns:
 *   -2: Force usage of PARTED for unknown types
 *   -1: An error was encountered, with error message set
 *    0: No file system found
 */
static int
virStorageBackendBLKIDFindEmpty(const char *device,
                                const char *format,
                                bool writelabel)
{
    int ret = -1;
    int rc;
    blkid_probe probe = NULL;

    VIR_DEBUG("Probe for existing filesystem/partition format %s on device %s",
              format, device);

    if (!(probe = blkid_new_probe_from_filename(device))) {
        virReportError(VIR_ERR_STORAGE_PROBE_FAILED,
                       _("Failed to create filesystem probe for device %s"),
                       device);
        return -1;
    }

    /* Look for something on FS, if it either doesn't recognize the
     * format type as a valid FS format type or it doesn't find a valid
     * format type on the device, then perform the same check using
     * partition probing. */
    rc = virStorageBackendBLKIDFindFS(probe, device, format);
    if (rc == VIR_STORAGE_BLKID_PROBE_UNDEFINED ||
        rc == VIR_STORAGE_BLKID_PROBE_UNKNOWN) {

        rc = virStorageBackendBLKIDFindPart(probe, device, format);
    }

    switch (rc) {
    case VIR_STORAGE_BLKID_PROBE_UNDEFINED:
        if (writelabel)
            ret = 0;
        else
            virReportError(VIR_ERR_STORAGE_PROBE_FAILED,
                           _("Device '%s' is unrecognized, requires build"),
                           device);
        break;

    case VIR_STORAGE_BLKID_PROBE_ERROR:
        virReportError(VIR_ERR_STORAGE_PROBE_FAILED,
                       _("Failed to probe for format type '%s'"), format);
        break;

    case VIR_STORAGE_BLKID_PROBE_UNKNOWN:
        ret = -2;
        break;

    case VIR_STORAGE_BLKID_PROBE_MATCH:
        if (writelabel)
            virReportError(VIR_ERR_STORAGE_POOL_BUILT,
                           _("Device '%s' already formatted using '%s'"),
                           device, format);
        else
            ret = 0;
        break;

    case VIR_STORAGE_BLKID_PROBE_DIFFERENT:
        if (writelabel)
            virReportError(VIR_ERR_STORAGE_POOL_BUILT,
                           _("Format of device '%s' does not match the "
                             "expected format '%s', forced overwrite is "
                             "necessary"),
                           device, format);
        else
            virReportError(VIR_ERR_OPERATION_INVALID,
                           _("Format of device '%s' does not match the "
                             "expected format '%s'"),
                           device, format);
        break;
    }

    if (ret == 0 && blkid_do_probe(probe) != 1) {
        virReportError(VIR_ERR_STORAGE_PROBE_FAILED, "%s",
                       _("Found additional probes to run, probing may "
                         "be incorrect"));
        ret = -1;
    }

    blkid_free_probe(probe);

    return ret;
}

#else /* #if WITH_BLKID */

static int
virStorageBackendBLKIDFindEmpty(const char *device ATTRIBUTE_UNUSED,
                                const char *format ATTRIBUTE_UNUSED,
                                bool writelabel ATTRIBUTE_UNUSED)
{
    return -2;
}

#endif /* #if WITH_BLKID */


#if WITH_STORAGE_DISK

typedef enum {
    VIR_STORAGE_PARTED_ERROR = -1,
    VIR_STORAGE_PARTED_MATCH,       /* Valid label found and matches format */
    VIR_STORAGE_PARTED_DIFFERENT,   /* Valid label found but not match format */
    VIR_STORAGE_PARTED_UNKNOWN,     /* No or unrecognized label */
    VIR_STORAGE_PARTED_NOPTTYPE,    /* Did not find the Partition Table type */
    VIR_STORAGE_PARTED_PTTYPE_UNK,  /* Partition Table type unknown*/
} virStorageBackendPARTEDResult;

/**
 * Check for a valid disk label (partition table) on device using
 * the PARTED command
 *
 * returns virStorageBackendPARTEDResult
 */
static virStorageBackendPARTEDResult
virStorageBackendPARTEDFindLabel(const char *device,
                                 const char *format)
{
    const char *const args[] = {
        device, "print", "--script", NULL,
    };
    virCommandPtr cmd = virCommandNew(PARTED);
    char *output = NULL;
    char *error = NULL;
    char *start, *end;
    int ret = VIR_STORAGE_PARTED_ERROR;

    virCommandAddArgSet(cmd, args);
    virCommandAddEnvString(cmd, "LC_ALL=C");
    virCommandSetOutputBuffer(cmd, &output);
    virCommandSetErrorBuffer(cmd, &error);

    /* if parted succeeds we have a valid partition table */
    ret = virCommandRun(cmd, NULL);
    if (ret < 0) {
        if ((output && strstr(output, "unrecognised disk label")) ||
            (error && strstr(error, "unrecognised disk label"))) {
            ret = VIR_STORAGE_PARTED_UNKNOWN;
        }
        goto cleanup;
    }

    /* Search for "Partition Table:" in the output. If not present,
     * then we cannot validate the partition table type.
     */
    if (!(start = strstr(output, "Partition Table: ")) ||
        !(end = strstr(start, "\n"))) {
        VIR_DEBUG("Unable to find tag in output: %s", output);
        ret = VIR_STORAGE_PARTED_NOPTTYPE;
        goto cleanup;
    }
    start += strlen("Partition Table: ");
    *end = '\0';

    /* on disk it's "msdos", but we document/use "dos" so deal with it here */
    if (STREQ(start, "msdos"))
        start += 2;

    /* Make sure we know about this type */
    if (virStoragePoolFormatDiskTypeFromString(start) < 0) {
        ret = VIR_STORAGE_PARTED_PTTYPE_UNK;
        goto cleanup;
    }

    /*  Does the on disk match what the pool desired? */
    if (STREQ(start, format))
        ret = VIR_STORAGE_PARTED_MATCH;
3343 3344
    else
        ret = VIR_STORAGE_PARTED_DIFFERENT;
3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475

 cleanup:
    virCommandFree(cmd);
    VIR_FREE(output);
    VIR_FREE(error);
    return ret;
}


/**
 * Determine whether the label on the disk is valid or in a known format
 * for the purpose of rewriting the label during build or being able to
 * start a pool on a device.
 *
 * When 'writelabel' is true, if we find a valid disk label on the device,
 * then we shouldn't be attempting to write as the volume may contain
 * data. Force the usage of the overwrite flag to the build command in
 * order to be certain. When the disk label is unrecognized, then it
 * should be safe to write.
 *
 * When 'writelabel' is false, only if we find a valid disk label on the
 * device should we allow the start since for this path we won't be
 * rewriting the label.
 *
 * Return: 0 if it's OK
 *         -1 if something's wrong
 */
static int
virStorageBackendPARTEDValidLabel(const char *device,
                                  const char *format,
                                  bool writelabel)
{
    int ret = -1;
    virStorageBackendPARTEDResult check;

    check = virStorageBackendPARTEDFindLabel(device, format);
    switch (check) {
    case VIR_STORAGE_PARTED_ERROR:
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Error checking for disk label, failed to get "
                         "disk partition information"));
        break;

    case VIR_STORAGE_PARTED_MATCH:
        if (writelabel)
            virReportError(VIR_ERR_OPERATION_INVALID,
                           _("Disk label already formatted using '%s'"),
                           format);
        else
            ret = 0;
        break;

    case VIR_STORAGE_PARTED_DIFFERENT:
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Known, but different label format present, "
                         "requires build --overwrite"));
        break;

    case VIR_STORAGE_PARTED_UNKNOWN:
        if (writelabel)
            ret = 0;
        else
            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                           _("Unrecognized disk label found, requires build"));
        break;

    case VIR_STORAGE_PARTED_NOPTTYPE:
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Unable to determine Partition Type, "
                         "requires build --overwrite"));
        break;

    case VIR_STORAGE_PARTED_PTTYPE_UNK:
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Unknown Partition Type, requires build --overwrite"));
        break;
    }

    return ret;
}

#else

static int
virStorageBackendPARTEDValidLabel(const char *device ATTRIBUTE_UNUSED,
                                  const char *format ATTRIBUTE_UNUSED,
                                  bool writelabel ATTRIBUTE_UNUSED)
{
    return -2;
}


#endif /* #if WITH_STORAGE_DISK */


/* virStorageBackendDeviceIsEmpty:
 * @devpath: Path to the device to check
 * @format: Desired format string
 * @writelabel: True if the caller expects to write the label
 *
 * Check if the @devpath has some sort of known file system using the
 * BLKID API if available.
 *
 * Returns true if the probe deems the device has nothing valid on it
 * or when we cannot check and we're not writing the label.
 *
 * Returns false if the probe finds something
 */
bool
virStorageBackendDeviceIsEmpty(const char *devpath,
                               const char *format,
                               bool writelabel)
{
    int ret;

    if ((ret = virStorageBackendBLKIDFindEmpty(devpath, format,
                                               writelabel)) == -2)
        ret = virStorageBackendPARTEDValidLabel(devpath, format, writelabel);

    if (ret == -2 && !writelabel)
        ret = 0;

    if (ret == -2) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Unable to probe '%s' for existing data, "
                         "forced overwrite is necessary"),
                       devpath);
    }

    return ret == 0;
}
3476 3477


3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
static int
storageBackendProbeTarget(virStorageSourcePtr target,
                          virStorageEncryptionPtr *encryption)
{
    int backingStoreFormat;
    int fd = -1;
    int ret = -1;
    int rc;
    virStorageSourcePtr meta = NULL;
    struct stat sb;

    if (encryption)
        *encryption = NULL;

    if ((rc = virStorageBackendVolOpen(target->path, &sb,
                                       VIR_STORAGE_VOL_FS_PROBE_FLAGS)) < 0)
        return rc; /* Take care to propagate rc, it is not always -1 */
    fd = rc;

    if (virStorageBackendUpdateVolTargetInfoFD(target, fd, &sb) < 0)
        goto cleanup;

    if (S_ISDIR(sb.st_mode)) {
        if (storageBackendIsPloopDir(target->path)) {
            if (storageBackendRedoPloopUpdate(target, &sb, &fd,
                                              VIR_STORAGE_VOL_FS_PROBE_FLAGS) < 0)
                goto cleanup;
        } else {
            target->format = VIR_STORAGE_FILE_DIR;
            ret = 0;
            goto cleanup;
        }
    }

    if (!(meta = virStorageFileGetMetadataFromFD(target->path,
                                                 fd,
                                                 VIR_STORAGE_FILE_AUTO,
                                                 &backingStoreFormat)))
        goto cleanup;

    if (meta->backingStoreRaw) {
        if (!(target->backingStore = virStorageSourceNewFromBacking(meta)))
            goto cleanup;

        target->backingStore->format = backingStoreFormat;

        /* XXX: Remote storage doesn't play nicely with volumes backed by
         * remote storage. To avoid trouble, just fake the backing store is RAW
         * and put the string from the metadata as the path of the target. */
        if (!virStorageSourceIsLocalStorage(target->backingStore)) {
            virStorageSourceFree(target->backingStore);

            if (VIR_ALLOC(target->backingStore) < 0)
                goto cleanup;

            target->backingStore->type = VIR_STORAGE_TYPE_NETWORK;
            target->backingStore->path = meta->backingStoreRaw;
            meta->backingStoreRaw = NULL;
            target->backingStore->format = VIR_STORAGE_FILE_RAW;
        }

        if (target->backingStore->format == VIR_STORAGE_FILE_AUTO) {
            if ((rc = virStorageFileProbeFormat(target->backingStore->path,
                                                -1, -1)) < 0) {
                /* If the backing file is currently unavailable or is
                 * accessed via remote protocol only log an error, fake the
                 * format as RAW and continue. Returning -1 here would
                 * disable the whole storage pool, making it unavailable for
                 * even maintenance. */
                target->backingStore->format = VIR_STORAGE_FILE_RAW;
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("cannot probe backing volume format: %s"),
                               target->backingStore->path);
            } else {
                target->backingStore->format = rc;
            }
        }
    }

    target->format = meta->format;

    /* Default to success below this point */
    ret = 0;

    if (meta->capacity)
        target->capacity = meta->capacity;

    if (encryption && meta->encryption) {
3566 3567 3568
        if (meta->encryption->payload_offset != -1)
            target->capacity -= meta->encryption->payload_offset * 512;

3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595
        *encryption = meta->encryption;
        meta->encryption = NULL;

        /* XXX ideally we'd fill in secret UUID here
         * but we cannot guarantee 'conn' is non-NULL
         * at this point in time :-(  So we only fill
         * in secrets when someone first queries a vol
         */
    }

    virBitmapFree(target->features);
    target->features = meta->features;
    meta->features = NULL;

    if (meta->compat) {
        VIR_FREE(target->compat);
        target->compat = meta->compat;
        meta->compat = NULL;
    }

 cleanup:
    VIR_FORCE_CLOSE(fd);
    virStorageSourceFree(meta);
    return ret;
}


3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
/**
 * virStorageBackendRefreshVolTargetUpdate:
 * @vol: Volume def that needs updating
 *
 * Attempt to probe the volume in order to get more details.
 *
 * Returns 0 on success, -2 to ignore failure, -1 on failure
 */
int
virStorageBackendRefreshVolTargetUpdate(virStorageVolDefPtr vol)
{
    int err;

    /* Real value is filled in during probe */
    vol->target.format = VIR_STORAGE_FILE_RAW;

    if ((err = storageBackendProbeTarget(&vol->target,
                                         &vol->target.encryption)) < 0) {
        if (err == -2) {
            return -2;
        } else if (err == -3) {
            /* The backing file is currently unavailable, its format is not
             * explicitly specified, the probe to auto detect the format
             * failed: continue with faked RAW format, since AUTO will
             * break virStorageVolTargetDefFormat() generating the line
             * <format type='...'/>. */
        } else {
            return -1;
        }
    }

    /* directory based volume */
    if (vol->target.format == VIR_STORAGE_FILE_DIR)
        vol->type = VIR_STORAGE_VOL_DIR;

    if (vol->target.format == VIR_STORAGE_FILE_PLOOP)
        vol->type = VIR_STORAGE_VOL_PLOOP;

3634
    if (virStorageSourceHasBacking(&vol->target)) {
3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647
        ignore_value(storageBackendUpdateVolTargetInfo(VIR_STORAGE_VOL_FILE,
                                                       vol->target.backingStore,
                                                       false,
                                                       VIR_STORAGE_VOL_OPEN_DEFAULT, 0));
        /* If this failed, the backing file is currently unavailable,
         * the capacity, allocation, owner, group and mode are unknown.
         * An error message was raised, but we just continue. */
    }

    return 0;
}


3648 3649 3650 3651 3652
/**
 * Iterate over the pool's directory and enumerate all disk images
 * within it. This is non-recursive.
 */
int
3653
virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
3654
{
3655
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
3656 3657 3658 3659 3660 3661 3662 3663 3664
    DIR *dir;
    struct dirent *ent;
    struct statvfs sb;
    struct stat statbuf;
    virStorageVolDefPtr vol = NULL;
    virStorageSourcePtr target = NULL;
    int direrr;
    int fd = -1, ret = -1;

3665
    if (virDirOpen(&dir, def->target.path) < 0)
3666 3667
        goto cleanup;

3668
    while ((direrr = virDirRead(dir, &ent, def->target.path)) > 0) {
3669 3670 3671
        int err;

        if (virStringHasControlChars(ent->d_name)) {
3672 3673
            VIR_WARN("Ignoring file '%s' with control characters under '%s'",
                     ent->d_name, def->target.path);
3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684
            continue;
        }

        if (VIR_ALLOC(vol) < 0)
            goto cleanup;

        if (VIR_STRDUP(vol->name, ent->d_name) < 0)
            goto cleanup;

        vol->type = VIR_STORAGE_VOL_FILE;
        if (virAsprintf(&vol->target.path, "%s/%s",
3685
                        def->target.path, vol->name) < 0)
3686 3687 3688 3689 3690
            goto cleanup;

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

3691
        if ((err = virStorageBackendRefreshVolTargetUpdate(vol)) < 0) {
3692 3693 3694 3695 3696 3697 3698
            if (err == -2) {
                /* Silently ignore non-regular files,
                 * eg 'lost+found', dangling symbolic link */
                virStorageVolDefFree(vol);
                vol = NULL;
                continue;
            }
3699
            goto cleanup;
3700 3701
        }

3702
        if (virStoragePoolObjAddVol(pool, vol) < 0)
3703
            goto cleanup;
3704
        vol = NULL;
3705 3706 3707 3708 3709 3710 3711 3712
    }
    if (direrr < 0)
        goto cleanup;
    VIR_DIR_CLOSE(dir);

    if (VIR_ALLOC(target))
        goto cleanup;

3713
    if ((fd = open(def->target.path, O_RDONLY)) < 0) {
3714 3715
        virReportSystemError(errno,
                             _("cannot open path '%s'"),
3716
                             def->target.path);
3717 3718 3719 3720 3721 3722
        goto cleanup;
    }

    if (fstat(fd, &statbuf) < 0) {
        virReportSystemError(errno,
                             _("cannot stat path '%s'"),
3723
                             def->target.path);
3724 3725 3726 3727 3728 3729 3730
        goto cleanup;
    }

    if (virStorageBackendUpdateVolTargetInfoFD(target, fd, &statbuf) < 0)
        goto cleanup;

    /* VolTargetInfoFD doesn't update capacity correctly for the pool case */
3731
    if (statvfs(def->target.path, &sb) < 0) {
3732 3733
        virReportSystemError(errno,
                             _("cannot statvfs path '%s'"),
3734
                             def->target.path);
3735 3736 3737
        goto cleanup;
    }

3738 3739 3740 3741 3742
    def->capacity = ((unsigned long long)sb.f_frsize *
                     (unsigned long long)sb.f_blocks);
    def->available = ((unsigned long long)sb.f_bfree *
                      (unsigned long long)sb.f_frsize);
    def->allocation = def->capacity - def->available;
3743

3744 3745 3746 3747 3748
    def->target.perms.mode = target->perms->mode;
    def->target.perms.uid = target->perms->uid;
    def->target.perms.gid = target->perms->gid;
    VIR_FREE(def->target.perms.label);
    if (VIR_STRDUP(def->target.perms.label, target->perms->label) < 0)
3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_DIR_CLOSE(dir);
    VIR_FORCE_CLOSE(fd);
    virStorageVolDefFree(vol);
    virStorageSourceFree(target);
    if (ret < 0)
        virStoragePoolObjClearVols(pool);
    return ret;
}


3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817
static char *
virStorageBackendSCSISerial(const char *dev)
{
    char *serial = NULL;
#ifdef WITH_UDEV
    virCommandPtr cmd = virCommandNewArgList(
        "/lib/udev/scsi_id",
        "--replace-whitespace",
        "--whitelisted",
        "--device", dev,
        NULL
        );

    /* Run the program and capture its output */
    virCommandSetOutputBuffer(cmd, &serial);
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;
#endif

    if (serial && STRNEQ(serial, "")) {
        char *nl = strchr(serial, '\n');
        if (nl)
            *nl = '\0';
    } else {
        VIR_FREE(serial);
        ignore_value(VIR_STRDUP(serial, dev));
    }

#ifdef WITH_UDEV
 cleanup:
    virCommandFree(cmd);
#endif

    return serial;
}


/*
 * Attempt to create a new LUN
 *
 * Returns:
 *
 *  0  => Success
 *  -1 => Failure due to some sort of OOM or other fatal issue found when
 *        attempting to get/update information about a found volume
 *  -2 => Failure to find a stable path, not fatal, caller can try another
 */
static int
virStorageBackendSCSINewLun(virStoragePoolObjPtr pool,
                            uint32_t host ATTRIBUTE_UNUSED,
                            uint32_t bus,
                            uint32_t target,
                            uint32_t lun,
                            const char *dev)
{
3818
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829
    virStorageVolDefPtr vol = NULL;
    char *devpath = NULL;
    int retval = -1;

    /* Check if the pool is using a stable target path. The call to
     * virStorageBackendStablePath will fail if the pool target path
     * isn't stable and just return the strdup'd 'devpath' anyway.
     * This would be indistinguishable to failing to find the stable
     * path to the device if the virDirRead loop to search the
     * target pool path for our devpath had failed.
     */
3830 3831 3832
    if (!virStorageBackendPoolPathIsStable(def->target.path) &&
        !(STREQ(def->target.path, "/dev") ||
          STREQ(def->target.path, "/dev/"))) {
3833 3834
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unable to use target path '%s' for dev '%s'"),
3835
                       NULLSTR(def->target.path), dev);
3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
        goto cleanup;
    }

    if (VIR_ALLOC(vol) < 0)
        goto cleanup;

    vol->type = VIR_STORAGE_VOL_BLOCK;

    /* 'host' is dynamically allocated by the kernel, first come,
     * first served, per HBA. As such it isn't suitable for use
     * in the volume name. We only need uniqueness per-pool, so
     * just leave 'host' out
     */
    if (virAsprintf(&(vol->name), "unit:%u:%u:%u", bus, target, lun) < 0)
        goto cleanup;

    if (virAsprintf(&devpath, "/dev/%s", dev) < 0)
        goto cleanup;

    VIR_DEBUG("Trying to create volume for '%s'", devpath);

    /* Now figure out the stable path
     *
     * XXX this method is O(N) because it scans the pool target
     * dir every time its run. Should figure out a more efficient
     * way of doing this...
     */
    if ((vol->target.path = virStorageBackendStablePath(pool,
                                                        devpath,
                                                        true)) == NULL)
        goto cleanup;

    if (STREQ(devpath, vol->target.path) &&
3869 3870
        !(STREQ(def->target.path, "/dev") ||
          STREQ(def->target.path, "/dev/"))) {
3871 3872

        VIR_DEBUG("No stable path found for '%s' in '%s'",
3873
                  devpath, def->target.path);
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887

        retval = -2;
        goto cleanup;
    }

    /* Allow a volume read failure to ignore or skip this block file */
    if ((retval = virStorageBackendUpdateVolInfo(vol, true,
                                                 VIR_STORAGE_VOL_OPEN_DEFAULT,
                                                 VIR_STORAGE_VOL_READ_NOERROR)) < 0)
        goto cleanup;

    if (!(vol->key = virStorageBackendSCSISerial(vol->target.path)))
        goto cleanup;

3888 3889
    def->capacity += vol->target.capacity;
    def->allocation += vol->target.allocation;
3890

3891
    if (virStoragePoolObjAddVol(pool, vol) < 0)
3892 3893
        goto cleanup;
    vol = NULL;
3894

3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
    retval = 0;

 cleanup:
    virStorageVolDefFree(vol);
    VIR_FREE(devpath);
    return retval;
}



static int
getNewStyleBlockDevice(const char *lun_path,
                       const char *block_name ATTRIBUTE_UNUSED,
                       char **block_device)
{
    char *block_path = NULL;
    DIR *block_dir = NULL;
    struct dirent *block_dirent = NULL;
    int retval = -1;
    int direrr;

    if (virAsprintf(&block_path, "%s/block", lun_path) < 0)
        goto cleanup;

    VIR_DEBUG("Looking for block device in '%s'", block_path);

    if (virDirOpen(&block_dir, block_path) < 0)
        goto cleanup;

    while ((direrr = virDirRead(block_dir, &block_dirent, block_path)) > 0) {
        if (VIR_STRDUP(*block_device, block_dirent->d_name) < 0)
            goto cleanup;

        VIR_DEBUG("Block device is '%s'", *block_device);

        break;
    }

    if (direrr < 0)
        goto cleanup;

    retval = 0;

 cleanup:
    VIR_DIR_CLOSE(block_dir);
    VIR_FREE(block_path);
    return retval;
}


static int
getOldStyleBlockDevice(const char *lun_path ATTRIBUTE_UNUSED,
                       const char *block_name,
                       char **block_device)
{
    char *blockp = NULL;
    int retval = -1;

    /* old-style; just parse out the sd */
    if (!(blockp = strrchr(block_name, ':'))) {
        /* Hm, wasn't what we were expecting; have to give up */
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to parse block name %s"),
                       block_name);
        goto cleanup;
    } else {
        blockp++;
        if (VIR_STRDUP(*block_device, blockp) < 0)
            goto cleanup;

        VIR_DEBUG("Block device is '%s'", *block_device);
    }

    retval = 0;
 cleanup:
    return retval;
}


/*
 * Search a device entry for the "block" file
 *
 * Returns
 *
 *   0 => Found it
 *   -1 => Fatal error
 *   -2 => Didn't find in lun_path directory
 */
static int
getBlockDevice(uint32_t host,
               uint32_t bus,
               uint32_t target,
               uint32_t lun,
               char **block_device)
{
    char *lun_path = NULL;
    DIR *lun_dir = NULL;
    struct dirent *lun_dirent = NULL;
    int retval = -1;
    int direrr;

    *block_device = NULL;

    if (virAsprintf(&lun_path, "/sys/bus/scsi/devices/%u:%u:%u:%u",
                    host, bus, target, lun) < 0)
        goto cleanup;

    if (virDirOpen(&lun_dir, lun_path) < 0)
        goto cleanup;

    while ((direrr = virDirRead(lun_dir, &lun_dirent, lun_path)) > 0) {
        if (STRPREFIX(lun_dirent->d_name, "block")) {
            if (strlen(lun_dirent->d_name) == 5) {
                if (getNewStyleBlockDevice(lun_path,
                                           lun_dirent->d_name,
                                           block_device) < 0)
                    goto cleanup;
            } else {
                if (getOldStyleBlockDevice(lun_path,
                                           lun_dirent->d_name,
                                           block_device) < 0)
                    goto cleanup;
            }
            break;
        }
    }
    if (direrr < 0)
        goto cleanup;
    if (!*block_device) {
        retval = -2;
        goto cleanup;
    }

    retval = 0;

 cleanup:
    VIR_DIR_CLOSE(lun_dir);
    VIR_FREE(lun_path);
    return retval;
}


/* Function to check if the type file in the given sysfs_path is a
 * Direct-Access device (i.e. type 0).  Return -1 on failure, type of
 * the device otherwise.
 */
static int
getDeviceType(uint32_t host,
              uint32_t bus,
              uint32_t target,
              uint32_t lun,
              int *type)
{
    char *type_path = NULL;
    char typestr[3];
    char *gottype, *p;
    FILE *typefile;
    int retval = 0;

    if (virAsprintf(&type_path, "/sys/bus/scsi/devices/%u:%u:%u:%u/type",
                    host, bus, target, lun) < 0)
        goto out;

    typefile = fopen(type_path, "r");
    if (typefile == NULL) {
        virReportSystemError(errno,
                             _("Could not find typefile '%s'"),
                             type_path);
        /* there was no type file; that doesn't seem right */
        retval = -1;
        goto out;
    }

    gottype = fgets(typestr, 3, typefile);
    VIR_FORCE_FCLOSE(typefile);

    if (gottype == NULL) {
        virReportSystemError(errno,
                             _("Could not read typefile '%s'"),
                             type_path);
        /* we couldn't read the type file; have to give up */
        retval = -1;
        goto out;
    }

    /* we don't actually care about p, but if you pass NULL and the last
     * character is not \0, virStrToLong_i complains
     */
    if (virStrToLong_i(typestr, &p, 10, type) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Device type '%s' is not an integer"),
                       typestr);
        /* Hm, type wasn't an integer; seems strange */
        retval = -1;
        goto out;
    }

    VIR_DEBUG("Device type is %d", *type);

 out:
    VIR_FREE(type_path);
    return retval;
}


/*
 * Process a Logical Unit entry from the scsi host device directory
 *
 * Returns:
 *
 *  0  => Found a valid entry
 *  -1 => Some sort of fatal error
 *  -2 => non-fatal error or a non-disk entry
 */
static int
processLU(virStoragePoolObjPtr pool,
          uint32_t host,
          uint32_t bus,
          uint32_t target,
          uint32_t lun)
{
    int retval = -1;
    int device_type;
    char *block_device = NULL;

    VIR_DEBUG("Processing LU %u:%u:%u:%u",
              host, bus, target, lun);

    if (getDeviceType(host, bus, target, lun, &device_type) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to determine if %u:%u:%u:%u is a Direct-Access LUN"),
                       host, bus, target, lun);
        return -1;
    }

    /* We don't create volumes for devices other than disk and cdrom
     * devices, but finding a device that isn't one of those types
     * isn't an error, either. */
    if (!(device_type == VIR_STORAGE_DEVICE_TYPE_DISK ||
          device_type == VIR_STORAGE_DEVICE_TYPE_ROM))
        return -2;

    VIR_DEBUG("%u:%u:%u:%u is a Direct-Access LUN",
              host, bus, target, lun);

    if ((retval = getBlockDevice(host, bus, target, lun, &block_device)) < 0) {
        VIR_DEBUG("Failed to find block device for this LUN");
        return retval;
    }

    retval = virStorageBackendSCSINewLun(pool, host, bus, target, lun,
                                         block_device);
    if (retval < 0) {
        VIR_DEBUG("Failed to create new storage volume for %u:%u:%u:%u",
                  host, bus, target, lun);
        goto cleanup;
    }

    VIR_DEBUG("Created new storage volume for %u:%u:%u:%u successfully",
              host, bus, target, lun);

 cleanup:
    VIR_FREE(block_device);
    return retval;
}


int
virStorageBackendSCSIFindLUs(virStoragePoolObjPtr pool,
                              uint32_t scanhost)
{
4166
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
4167 4168 4169 4170 4171 4172 4173 4174 4175 4176
    int retval = 0;
    uint32_t bus, target, lun;
    const char *device_path = "/sys/bus/scsi/devices";
    DIR *devicedir = NULL;
    struct dirent *lun_dirent = NULL;
    char devicepattern[64];
    int found = 0;

    VIR_DEBUG("Discovering LUs on host %u", scanhost);

J
John Ferlan 已提交
4177
    virWaitForDevices();
4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207

    if (virDirOpen(&devicedir, device_path) < 0)
        return -1;

    snprintf(devicepattern, sizeof(devicepattern), "%u:%%u:%%u:%%u\n", scanhost);

    while ((retval = virDirRead(devicedir, &lun_dirent, device_path)) > 0) {
        int rc;

        if (sscanf(lun_dirent->d_name, devicepattern,
                   &bus, &target, &lun) != 3) {
            continue;
        }

        VIR_DEBUG("Found possible LU '%s'", lun_dirent->d_name);

        rc = processLU(pool, scanhost, bus, target, lun);
        if (rc == -1) {
            retval = -1;
            break;
        }
        if (rc == 0)
            found++;
    }

    VIR_DIR_CLOSE(devicedir);

    if (retval < 0)
        return -1;

4208
    VIR_DEBUG("Found %d LUs for pool %s", found, def->name);
4209 4210 4211

    return found;
}
4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233


/*
 * @path: Path to the device to initialize
 * @size: Size to be cleared
 *
 * Zero out possible partition table information for the specified
 * bytes from the start of the @path and from the end of @path
 *
 * Returns 0 on success, -1 on failure with error message set
 */
int
virStorageBackendZeroPartitionTable(const char *path,
                                    unsigned long long size)
{
    if (storageBackendVolWipeLocalFile(path, VIR_STORAGE_VOL_WIPE_ALG_ZERO,
                                       size, false) < 0)
        return -1;

    return storageBackendVolWipeLocalFile(path, VIR_STORAGE_VOL_WIPE_ALG_ZERO,
                                          size, true);
}
4234 4235


4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268
/**
 * virStorageBackendFileSystemGetPoolSource
 * @pool: storage pool object pointer
 *
 * Allocate/return a string representing the FS storage pool source.
 * It is up to the caller to VIR_FREE the allocated string
 */
char *
virStorageBackendFileSystemGetPoolSource(virStoragePoolObjPtr pool)
{
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
    char *src = NULL;

    if (def->type == VIR_STORAGE_POOL_NETFS) {
        if (def->source.format == VIR_STORAGE_POOL_NETFS_CIFS) {
            if (virAsprintf(&src, "//%s/%s",
                            def->source.hosts[0].name,
                            def->source.dir) < 0)
                return NULL;
        } else {
            if (virAsprintf(&src, "%s:%s",
                            def->source.hosts[0].name,
                            def->source.dir) < 0)
                return NULL;
        }
    } else {
        if (VIR_STRDUP(src, def->source.devices[0].path) < 0)
            return NULL;
    }
    return src;
}


4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289
static void
virStorageBackendFileSystemMountAddOptions(virCommandPtr cmd,
                                           const char *providedOpts)
{
    VIR_AUTOFREE(char *) mountOpts = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (*default_mount_opts != '\0')
        virBufferAsprintf(&buf, "%s,", default_mount_opts);

    if (providedOpts)
        virBufferAsprintf(&buf, "%s,", providedOpts);

    virBufferTrim(&buf, ",", -1);
    mountOpts = virBufferContentAndReset(&buf);

    if (mountOpts)
        virCommandAddArgList(cmd, "-o", mountOpts, NULL);
}


4290 4291 4292 4293 4294 4295
static void
virStorageBackendFileSystemMountNFSArgs(virCommandPtr cmd,
                                        const char *src,
                                        virStoragePoolDefPtr def)
{
    virCommandAddArgList(cmd, src, def->target.path, NULL);
4296
    virStorageBackendFileSystemMountAddOptions(cmd, NULL);
4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307
}


static void
virStorageBackendFileSystemMountGlusterArgs(virCommandPtr cmd,
                                            const char *src,
                                            virStoragePoolDefPtr def)
{
    const char *fmt;

    fmt = virStoragePoolFormatFileSystemNetTypeToString(def->source.format);
4308 4309
    virCommandAddArgList(cmd, "-t", fmt, src, def->target.path, NULL);
    virStorageBackendFileSystemMountAddOptions(cmd, "direct-io-mode=1");
4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320
}


static void
virStorageBackendFileSystemMountCIFSArgs(virCommandPtr cmd,
                                         const char *src,
                                         virStoragePoolDefPtr def)
{
    const char *fmt;

    fmt = virStoragePoolFormatFileSystemNetTypeToString(def->source.format);
4321 4322
    virCommandAddArgList(cmd, "-t", fmt, src, def->target.path, NULL);
    virStorageBackendFileSystemMountAddOptions(cmd, "guest");
4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337
}


static void
virStorageBackendFileSystemMountDefaultArgs(virCommandPtr cmd,
                                            const char *src,
                                            virStoragePoolDefPtr def)
{
    const char *fmt;

    if (def->type == VIR_STORAGE_POOL_FS)
        fmt = virStoragePoolFormatFileSystemTypeToString(def->source.format);
    else
        fmt = virStoragePoolFormatFileSystemNetTypeToString(def->source.format);
    virCommandAddArgList(cmd, "-t", fmt, src, def->target.path, NULL);
4338
    virStorageBackendFileSystemMountAddOptions(cmd, NULL);
4339 4340 4341
}


4342
virCommandPtr
4343 4344
virStorageBackendFileSystemMountCmd(const char *cmdstr,
                                    virStoragePoolDefPtr def,
4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
                                    const char *src)
{
    /* 'mount -t auto' doesn't seem to auto determine nfs (or cifs),
     *  while plain 'mount' does. We have to craft separate argvs to
     *  accommodate this */
    bool netauto = (def->type == VIR_STORAGE_POOL_NETFS &&
                    def->source.format == VIR_STORAGE_POOL_NETFS_AUTO);
    bool glusterfs = (def->type == VIR_STORAGE_POOL_NETFS &&
                      def->source.format == VIR_STORAGE_POOL_NETFS_GLUSTERFS);
    bool cifsfs = (def->type == VIR_STORAGE_POOL_NETFS &&
                   def->source.format == VIR_STORAGE_POOL_NETFS_CIFS);
    virCommandPtr cmd = NULL;

4358
    cmd = virCommandNew(cmdstr);
4359
    if (netauto)
4360
        virStorageBackendFileSystemMountNFSArgs(cmd, src, def);
4361
    else if (glusterfs)
4362
        virStorageBackendFileSystemMountGlusterArgs(cmd, src, def);
4363
    else if (cifsfs)
4364
        virStorageBackendFileSystemMountCIFSArgs(cmd, src, def);
4365
    else
4366
        virStorageBackendFileSystemMountDefaultArgs(cmd, src, def);
4367 4368
    return cmd;
}
4369 4370 4371


virCommandPtr
4372 4373
virStorageBackendLogicalChangeCmd(const char *cmdstr,
                                  virStoragePoolDefPtr def,
4374 4375
                                  bool on)
{
4376
    return virCommandNewArgList(cmdstr,
4377 4378 4379 4380
                                on ? "-aly" : "-aln",
                                def->source.name,
                                NULL);
}