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