147b37
From c66b76761528f6294849cacaa8a99c49967d0070 Mon Sep 17 00:00:00 2001
147b37
Message-Id: <c66b76761528f6294849cacaa8a99c49967d0070@dist-git>
147b37
From: Michal Privoznik <mprivozn@redhat.com>
147b37
Date: Mon, 9 Apr 2018 15:46:49 +0200
147b37
Subject: [PATCH] util: Introduce virDevMapperGetTargets
147b37
MIME-Version: 1.0
147b37
Content-Type: text/plain; charset=UTF-8
147b37
Content-Transfer-Encoding: 8bit
147b37
147b37
https://bugzilla.redhat.com/show_bug.cgi?id=1564996
147b37
147b37
This helper fetches dependencies for given device mapper target.
147b37
147b37
At the same time, we need to provide a dummy log function because
147b37
by default libdevmapper prints out error messages to stderr which
147b37
we need to suppress.
147b37
147b37
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
147b37
(cherry picked from commit fd9d1e686db64fa9481b9eab4dabafa46713e2cf)
147b37
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
147b37
147b37
Conflicts: src/util/Makefile.inc.am: src/Makefile split is not backported
147b37
  yet (9cd0bdd1a178a76dbce4)
147b37
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
147b37
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
147b37
Reviewed-by: Ján Tomko <jtomko@redhat.com>
147b37
---
147b37
 src/Makefile.am          |   1 +
147b37
 src/libvirt_private.syms |   4 +
147b37
 src/util/virdevmapper.c  | 199 +++++++++++++++++++++++++++++++++++++++
147b37
 src/util/virdevmapper.h  |  31 ++++++
147b37
 4 files changed, 235 insertions(+)
147b37
 create mode 100644 src/util/virdevmapper.c
147b37
 create mode 100644 src/util/virdevmapper.h
147b37
147b37
diff --git a/src/Makefile.am b/src/Makefile.am
147b37
index 6b24a47afe..2130859045 100644
147b37
--- a/src/Makefile.am
147b37
+++ b/src/Makefile.am
147b37
@@ -111,6 +111,7 @@ UTIL_SOURCES =							\
147b37
 		util/virconf.c util/virconf.h			\
147b37
 		util/vircrypto.c util/vircrypto.h		\
147b37
 		util/virdbus.c util/virdbus.h util/virdbuspriv.h	\
147b37
+		util/virdevmapper.c util/virdevmapper.h \
147b37
 		util/virdnsmasq.c util/virdnsmasq.h		\
147b37
 		util/virebtables.c util/virebtables.h		\
147b37
 		util/virendian.h				\
147b37
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
147b37
index 15cc83931a..5bacafb462 100644
147b37
--- a/src/libvirt_private.syms
147b37
+++ b/src/libvirt_private.syms
147b37
@@ -1625,6 +1625,10 @@ virDBusMessageUnref;
147b37
 virDBusSetSharedBus;
147b37
 
147b37
 
147b37
+# util/virdevmapper.h
147b37
+virDevMapperGetTargets;
147b37
+
147b37
+
147b37
 # util/virdnsmasq.h
147b37
 dnsmasqAddDhcpHost;
147b37
 dnsmasqAddHost;
147b37
diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c
147b37
new file mode 100644
147b37
index 0000000000..d2c25af003
147b37
--- /dev/null
147b37
+++ b/src/util/virdevmapper.c
147b37
@@ -0,0 +1,199 @@
147b37
+/*
147b37
+ * virdevmapper.c: Functions for handling device mapper
147b37
+ *
147b37
+ * Copyright (C) 2018 Red Hat, Inc.
147b37
+ *
147b37
+ * This library is free software; you can redistribute it and/or
147b37
+ * modify it under the terms of the GNU Lesser General Public
147b37
+ * License as published by the Free Software Foundation; either
147b37
+ * version 2.1 of the License, or (at your option) any later version.
147b37
+ *
147b37
+ * This library is distributed in the hope that it will be useful,
147b37
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
147b37
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147b37
+ * Lesser General Public License for more details.
147b37
+ *
147b37
+ * You should have received a copy of the GNU Lesser General Public
147b37
+ * License along with this library.  If not, see
147b37
+ * <http://www.gnu.org/licenses/>.
147b37
+ *
147b37
+ * Authors:
147b37
+ *     Michal Privoznik <mprivozn@redhat.com>
147b37
+ */
147b37
+
147b37
+#include <config.h>
147b37
+
147b37
+#ifdef MAJOR_IN_MKDEV
147b37
+# include <sys/mkdev.h>
147b37
+#elif MAJOR_IN_SYSMACROS
147b37
+# include <sys/sysmacros.h>
147b37
+#endif
147b37
+
147b37
+#ifdef WITH_DEVMAPPER
147b37
+# include <libdevmapper.h>
147b37
+#endif
147b37
+
147b37
+#include "virdevmapper.h"
147b37
+#include "internal.h"
147b37
+#include "virthread.h"
147b37
+#include "viralloc.h"
147b37
+#include "virstring.h"
147b37
+
147b37
+#ifdef WITH_DEVMAPPER
147b37
+static void
147b37
+virDevMapperDummyLogger(int level ATTRIBUTE_UNUSED,
147b37
+                        const char *file ATTRIBUTE_UNUSED,
147b37
+                        int line ATTRIBUTE_UNUSED,
147b37
+                        int dm_errno ATTRIBUTE_UNUSED,
147b37
+                        const char *fmt ATTRIBUTE_UNUSED,
147b37
+                        ...)
147b37
+{
147b37
+    return;
147b37
+}
147b37
+
147b37
+static int
147b37
+virDevMapperOnceInit(void)
147b37
+{
147b37
+    /* Ideally, we would not need this. But libdevmapper prints
147b37
+     * error messages to stderr by default. Sad but true. */
147b37
+    dm_log_with_errno_init(virDevMapperDummyLogger);
147b37
+    return 0;
147b37
+}
147b37
+
147b37
+
147b37
+VIR_ONCE_GLOBAL_INIT(virDevMapper)
147b37
+
147b37
+
147b37
+static int
147b37
+virDevMapperGetTargetsImpl(const char *path,
147b37
+                           char ***devPaths_ret,
147b37
+                           unsigned int ttl)
147b37
+{
147b37
+    struct dm_task *dmt = NULL;
147b37
+    struct dm_deps *deps;
147b37
+    struct dm_info info;
147b37
+    char **devPaths = NULL;
147b37
+    char **recursiveDevPaths = NULL;
147b37
+    size_t i;
147b37
+    int ret = -1;
147b37
+
147b37
+    *devPaths_ret = NULL;
147b37
+
147b37
+    if (virDevMapperInitialize() < 0)
147b37
+        return ret;
147b37
+
147b37
+    if (ttl == 0) {
147b37
+        errno = ELOOP;
147b37
+        return ret;
147b37
+    }
147b37
+
147b37
+    if (!(dmt = dm_task_create(DM_DEVICE_DEPS)))
147b37
+        return ret;
147b37
+
147b37
+    if (!dm_task_set_name(dmt, path)) {
147b37
+        if (errno == ENOENT) {
147b37
+            /* It's okay, @path is not managed by devmapper =>
147b37
+             * not a devmapper device. */
147b37
+            ret = 0;
147b37
+        }
147b37
+        goto cleanup;
147b37
+    }
147b37
+
147b37
+    dm_task_no_open_count(dmt);
147b37
+
147b37
+    if (!dm_task_run(dmt))
147b37
+        goto cleanup;
147b37
+
147b37
+    if (!dm_task_get_info(dmt, &info))
147b37
+        goto cleanup;
147b37
+
147b37
+    if (!info.exists) {
147b37
+        ret = 0;
147b37
+        goto cleanup;
147b37
+    }
147b37
+
147b37
+    if (!(deps = dm_task_get_deps(dmt)))
147b37
+        goto cleanup;
147b37
+
147b37
+    if (VIR_ALLOC_N_QUIET(devPaths, deps->count + 1) < 0)
147b37
+        goto cleanup;
147b37
+
147b37
+    for (i = 0; i < deps->count; i++) {
147b37
+        if (virAsprintfQuiet(&devPaths[i], "/dev/block/%u:%u",
147b37
+                             major(deps->device[i]),
147b37
+                             minor(deps->device[i])) < 0)
147b37
+            goto cleanup;
147b37
+    }
147b37
+
147b37
+    recursiveDevPaths = NULL;
147b37
+    for (i = 0; i < deps->count; i++) {
147b37
+        char **tmpPaths;
147b37
+
147b37
+        if (virDevMapperGetTargetsImpl(devPaths[i], &tmpPaths, ttl - 1) < 0)
147b37
+            goto cleanup;
147b37
+
147b37
+        if (tmpPaths &&
147b37
+            virStringListMerge(&recursiveDevPaths, &tmpPaths) < 0) {
147b37
+            virStringListFree(tmpPaths);
147b37
+            goto cleanup;
147b37
+        }
147b37
+    }
147b37
+
147b37
+    if (virStringListMerge(&devPaths, &recursiveDevPaths) < 0)
147b37
+        goto cleanup;
147b37
+
147b37
+    VIR_STEAL_PTR(*devPaths_ret, devPaths);
147b37
+    ret = 0;
147b37
+ cleanup:
147b37
+    virStringListFree(recursiveDevPaths);
147b37
+    virStringListFree(devPaths);
147b37
+    dm_task_destroy(dmt);
147b37
+    return ret;
147b37
+}
147b37
+
147b37
+
147b37
+/**
147b37
+ * virDevMapperGetTargets:
147b37
+ * @path: devmapper target
147b37
+ * @devPaths: returned string list of devices
147b37
+ *
147b37
+ * For given @path figure out its targets, and store them in
147b37
+ * @devPaths array. Note, @devPaths is a string list so it's NULL
147b37
+ * terminated.
147b37
+ *
147b37
+ * If @path is not a devmapper device, @devPaths is set to NULL and
147b37
+ * success is returned.
147b37
+ *
147b37
+ * If @path consists of yet another devmapper targets these are
147b37
+ * consulted recursively.
147b37
+ *
147b37
+ * If we don't have permissions to talk to kernel, -1 is returned
147b37
+ * and errno is set to EBADF.
147b37
+ *
147b37
+ * Returns 0 on success,
147b37
+ *        -1 otherwise (with errno set, no libvirt error is
147b37
+ *        reported)
147b37
+ */
147b37
+int
147b37
+virDevMapperGetTargets(const char *path,
147b37
+                       char ***devPaths)
147b37
+{
147b37
+    const unsigned int ttl = 32;
147b37
+
147b37
+    /* Arbitrary limit on recursion level. A devmapper target can
147b37
+     * consist of devices or yet another targets. If that's the
147b37
+     * case, we have to stop recursion somewhere. */
147b37
+
147b37
+    return virDevMapperGetTargetsImpl(path, devPaths, ttl);
147b37
+}
147b37
+
147b37
+#else /* ! WITH_DEVMAPPER */
147b37
+
147b37
+int
147b37
+virDevMapperGetTargets(const char *path ATTRIBUTE_UNUSED,
147b37
+                       char ***devPaths ATTRIBUTE_UNUSED)
147b37
+{
147b37
+    errno = ENOSYS;
147b37
+    return -1;
147b37
+}
147b37
+#endif /* ! WITH_DEVMAPPER */
147b37
diff --git a/src/util/virdevmapper.h b/src/util/virdevmapper.h
147b37
new file mode 100644
147b37
index 0000000000..34d6655e77
147b37
--- /dev/null
147b37
+++ b/src/util/virdevmapper.h
147b37
@@ -0,0 +1,31 @@
147b37
+/*
147b37
+ * virdevmapper.h: Functions for handling device mapper
147b37
+ *
147b37
+ * Copyright (C) 2018 Red Hat, Inc.
147b37
+ *
147b37
+ * This library is free software; you can redistribute it and/or
147b37
+ * modify it under the terms of the GNU Lesser General Public
147b37
+ * License as published by the Free Software Foundation; either
147b37
+ * version 2.1 of the License, or (at your option) any later version.
147b37
+ *
147b37
+ * This library is distributed in the hope that it will be useful,
147b37
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
147b37
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147b37
+ * Lesser General Public License for more details.
147b37
+ *
147b37
+ * You should have received a copy of the GNU Lesser General Public
147b37
+ * License along with this library.  If not, see
147b37
+ * <http://www.gnu.org/licenses/>.
147b37
+ *
147b37
+ * Authors:
147b37
+ *     Michal Privoznik <mprivozn@redhat.com>
147b37
+ */
147b37
+
147b37
+#ifndef __VIR_DEVMAPPER_H__
147b37
+# define __VIR_DEVMAPPER_H__
147b37
+
147b37
+int
147b37
+virDevMapperGetTargets(const char *path,
147b37
+                       char ***devPaths);
147b37
+
147b37
+#endif /* __VIR_DEVMAPPER_H__ */
147b37
-- 
147b37
2.17.0
147b37