[PATCH v2 1/3] utils: Introduce functions for modprobe

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adds functions for various usages of modprobe

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/Makefile.am          |   1 +
 src/libvirt_private.syms |   7 +++
 src/util/virkmod.c       | 138 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virkmod.h       |  35 ++++++++++++
 4 files changed, 181 insertions(+)
 create mode 100644 src/util/virkmod.c
 create mode 100644 src/util/virkmod.h

diff --git a/src/Makefile.am b/src/Makefile.am
index abe0a51..b704045 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -125,6 +125,7 @@ UTIL_SOURCES =							\
 		util/virnetdevvportprofile.h util/virnetdevvportprofile.c \
 		util/virnetlink.c util/virnetlink.h		\
 		util/virnodesuspend.c util/virnodesuspend.h	\
+		util/virkmod.c util/virkmod.h                   \
 		util/virnuma.c util/virnuma.h			\
 		util/virobject.c util/virobject.h		\
 		util/virpci.c util/virpci.h			\
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 45f3117..11fc320 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1382,6 +1382,13 @@ virKeyFileLoadFile;
 virKeyFileNew;
 
 
+# util/virkmod.h
+virModprobeConfig;
+virModprobeLoad;
+virModprobeUnload;
+virModprobeUseBlacklist;
+
+
 # util/virlockspace.h
 virLockSpaceAcquireResource;
 virLockSpaceCreateResource;
diff --git a/src/util/virkmod.c b/src/util/virkmod.c
new file mode 100644
index 0000000..b8de8ea
--- /dev/null
+++ b/src/util/virkmod.c
@@ -0,0 +1,138 @@
+/*
+ * virkmod.c: helper APIs for managing kernel modules
+ *
+ * Copyright (C) 2014 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>
+#include "virkmod.h"
+#include "vircommand.h"
+
+static int
+doModprobe(const char *opts, const char *module, char **outbuf, char **errbuf)
+{
+    int ret = -1;
+    virCommandPtr cmd = NULL;
+
+    cmd = virCommandNew(MODPROBE);
+    if (opts)
+        virCommandAddArg(cmd, opts);
+    if (module)
+        virCommandAddArg(cmd, module);
+    if (outbuf)
+        virCommandSetOutputBuffer(cmd, outbuf);
+    if (errbuf)
+        virCommandSetErrorBuffer(cmd, errbuf);
+
+    if (virCommandRun(cmd, NULL) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+cleanup:
+    virCommandFree(cmd);
+    return ret;
+}
+
+/**
+ * virModprobeConfig:
+ *
+ * Get the current kernel module configuration
+ *
+ * Returns NULL on failure or a pointer to the output which
+ * must be VIR_FREE()'d by the caller
+ */
+char *
+virModprobeConfig(void)
+{
+    char *outbuf = NULL;
+
+    if (doModprobe("-c", NULL, &outbuf, NULL) < 0)
+        return NULL;
+
+    return outbuf;
+}
+
+
+/**
+ * virModprobeLoad:
+ * @module: Name of the module to load regardless of being on blacklist
+ *
+ * Attempts to load a kernel module
+ *
+ * returns NULL in case of success and the error buffer output from the
+ * virCommandRun() on failure.  The returned buffer must be VIR_FREE()
+ * by the caller
+ */
+char *
+virModprobeLoad(const char *module)
+{
+    char *errbuf = NULL;
+
+    if (doModprobe(NULL, module, NULL, &errbuf) < 0)
+        return errbuf;
+
+    VIR_FREE(errbuf);
+    return NULL;
+}
+
+
+/**
+ * virModprobeUseBlacklist:
+ * @module: Name of the module to load applying blacklist lookup
+ *
+ * Like ModprobeLoad, except adhere to the blacklist if present
+ *
+ * returns NULL in case of success and the error buffer output from the
+ * virCommandRun() on failure.  The returned buffer must be VIR_FREE()
+ * by the caller
+ */
+char *
+virModprobeUseBlacklist(const char *module)
+{
+    char *errbuf = NULL;
+
+    if (doModprobe("-b", module, NULL, &errbuf) < 0)
+        return errbuf;
+
+    VIR_FREE(errbuf);
+    return NULL;
+}
+
+
+/**
+ * virModprobeUnload:
+ * @module: Name of the module to unload
+ *
+ * Remove or unload a module
+ *
+ * returns NULL in case of success and the error buffer output from the
+ * virCommandRun() on failure.  The returned buffer must be VIR_FREE()
+ * by the caller
+ */
+char *
+virModprobeUnload(const char *module)
+{
+    char *errbuf = NULL;
+
+    if (doModprobe("-r", module, NULL, &errbuf) < 0)
+        return errbuf;
+
+    VIR_FREE(errbuf);
+    return NULL;
+}
diff --git a/src/util/virkmod.h b/src/util/virkmod.h
new file mode 100644
index 0000000..31b2663
--- /dev/null
+++ b/src/util/virkmod.h
@@ -0,0 +1,35 @@
+/*
+ * virkmod.h: helper APIs for managing kernel modprobe
+ *
+ * Copyright (C) 2014 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/>.
+ *
+ */
+
+#ifndef __VIR_KMOD_H__
+# define __VIR_KMOD_H__
+
+# include "internal.h"
+# include "viralloc.h"
+
+char *virModprobeConfig(void);
+char *virModprobeLoad(const char *)
+    ATTRIBUTE_NONNULL(1);
+char *virModprobeUseBlacklist(const char *);
+    ATTRIBUTE_NONNULL(1)
+char *virModprobeUnload(const char *)
+    ATTRIBUTE_NONNULL(1);
+#endif /* __VIR_KMOD_H__ */
-- 
1.8.4.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]