virdevmapper.c 5.1 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
/*
 * virdevmapper.c: Functions for handling device mapper
 *
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * 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>

23
#ifdef __linux__
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# include <sys/sysmacros.h>
#endif

#ifdef WITH_DEVMAPPER
# include <libdevmapper.h>
#endif

#include "virdevmapper.h"
#include "internal.h"
#include "virthread.h"
#include "viralloc.h"
#include "virstring.h"

#ifdef WITH_DEVMAPPER
static void
J
Ján Tomko 已提交
39 40 41 42 43
virDevMapperDummyLogger(int level G_GNUC_UNUSED,
                        const char *file G_GNUC_UNUSED,
                        int line G_GNUC_UNUSED,
                        int dm_errno G_GNUC_UNUSED,
                        const char *fmt G_GNUC_UNUSED,
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
                        ...)
{
    return;
}

static int
virDevMapperOnceInit(void)
{
    /* Ideally, we would not need this. But libdevmapper prints
     * error messages to stderr by default. Sad but true. */
    dm_log_with_errno_init(virDevMapperDummyLogger);
    return 0;
}


59
VIR_ONCE_GLOBAL_INIT(virDevMapper);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84


static int
virDevMapperGetTargetsImpl(const char *path,
                           char ***devPaths_ret,
                           unsigned int ttl)
{
    struct dm_task *dmt = NULL;
    struct dm_deps *deps;
    struct dm_info info;
    char **devPaths = NULL;
    char **recursiveDevPaths = NULL;
    size_t i;
    int ret = -1;

    *devPaths_ret = NULL;

    if (virDevMapperInitialize() < 0)
        return ret;

    if (ttl == 0) {
        errno = ELOOP;
        return ret;
    }

85 86 87 88 89 90
    if (!(dmt = dm_task_create(DM_DEVICE_DEPS))) {
        if (errno == ENOENT || errno == ENODEV) {
            /* It's okay. Kernel is probably built without
             * devmapper support. */
            ret = 0;
        }
91
        return ret;
92
    }
93 94 95 96 97 98 99 100 101 102 103 104

    if (!dm_task_set_name(dmt, path)) {
        if (errno == ENOENT) {
            /* It's okay, @path is not managed by devmapper =>
             * not a devmapper device. */
            ret = 0;
        }
        goto cleanup;
    }

    dm_task_no_open_count(dmt);

105 106 107 108 109
    if (!dm_task_run(dmt)) {
        if (errno == ENXIO) {
            /* If @path = "/dev/mapper/control" ENXIO is returned. */
            ret = 0;
        }
110
        goto cleanup;
111
    }
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    if (!dm_task_get_info(dmt, &info))
        goto cleanup;

    if (!info.exists) {
        ret = 0;
        goto cleanup;
    }

    if (!(deps = dm_task_get_deps(dmt)))
        goto cleanup;

    if (VIR_ALLOC_N_QUIET(devPaths, deps->count + 1) < 0)
        goto cleanup;

    for (i = 0; i < deps->count; i++) {
128 129 130
        devPaths[i] = g_strdup_printf("/dev/block/%u:%u",
                                      major(deps->device[i]),
                                      minor(deps->device[i]));
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    }

    recursiveDevPaths = NULL;
    for (i = 0; i < deps->count; i++) {
        char **tmpPaths;

        if (virDevMapperGetTargetsImpl(devPaths[i], &tmpPaths, ttl - 1) < 0)
            goto cleanup;

        if (tmpPaths &&
            virStringListMerge(&recursiveDevPaths, &tmpPaths) < 0) {
            virStringListFree(tmpPaths);
            goto cleanup;
        }
    }

    if (virStringListMerge(&devPaths, &recursiveDevPaths) < 0)
        goto cleanup;

150
    *devPaths_ret = g_steal_pointer(&devPaths);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    ret = 0;
 cleanup:
    virStringListFree(recursiveDevPaths);
    virStringListFree(devPaths);
    dm_task_destroy(dmt);
    return ret;
}


/**
 * virDevMapperGetTargets:
 * @path: devmapper target
 * @devPaths: returned string list of devices
 *
 * For given @path figure out its targets, and store them in
 * @devPaths array. Note, @devPaths is a string list so it's NULL
 * terminated.
 *
 * If @path is not a devmapper device, @devPaths is set to NULL and
 * success is returned.
 *
 * If @path consists of yet another devmapper targets these are
 * consulted recursively.
 *
 * If we don't have permissions to talk to kernel, -1 is returned
 * and errno is set to EBADF.
 *
 * Returns 0 on success,
 *        -1 otherwise (with errno set, no libvirt error is
 *        reported)
 */
int
virDevMapperGetTargets(const char *path,
                       char ***devPaths)
{
    const unsigned int ttl = 32;

    /* Arbitrary limit on recursion level. A devmapper target can
     * consist of devices or yet another targets. If that's the
     * case, we have to stop recursion somewhere. */

    return virDevMapperGetTargetsImpl(path, devPaths, ttl);
}

#else /* ! WITH_DEVMAPPER */

int
J
Ján Tomko 已提交
198 199
virDevMapperGetTargets(const char *path G_GNUC_UNUSED,
                       char ***devPaths G_GNUC_UNUSED)
200 201 202 203 204
{
    errno = ENOSYS;
    return -1;
}
#endif /* ! WITH_DEVMAPPER */