[lxc-devel] [lxd/master] Only load kernel modules if not loaded

stgraber on Github lxc-bot at linuxcontainers.org
Tue Oct 18 21:13:30 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161018/29274beb/attachment.bin>
-------------- next part --------------
From 89c1be5869838d2751cf5ea84fcad13764ada1fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 18 Oct 2016 17:12:13 -0400
Subject: [PATCH] Only load kernel modules if not loaded
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_lxc.go | 8 ++++----
 lxd/main.go          | 2 +-
 lxd/storage_zfs.go   | 2 +-
 lxd/util.go          | 8 ++++++++
 4 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 5a7ff65..7170548 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1111,9 +1111,9 @@ func (c *containerLXC) startCommon() (string, error) {
 	if kernelModules != "" {
 		for _, module := range strings.Split(kernelModules, ",") {
 			module = strings.TrimPrefix(module, " ")
-			out, err := exec.Command("modprobe", module).CombinedOutput()
+			err := loadModule(module)
 			if err != nil {
-				return "", fmt.Errorf("Failed to load kernel module '%s': %s", module, out)
+				return "", fmt.Errorf("Failed to load kernel module '%s': %s", module, err)
 			}
 		}
 	}
@@ -2572,9 +2572,9 @@ func (c *containerLXC) Update(args containerArgs, userRequested bool) error {
 			} else if key == "linux.kernel_modules" && value != "" {
 				for _, module := range strings.Split(value, ",") {
 					module = strings.TrimPrefix(module, " ")
-					out, err := exec.Command("modprobe", module).CombinedOutput()
+					err := loadModule(module)
 					if err != nil {
-						return fmt.Errorf("Failed to load kernel module '%s': %s", module, out)
+						return fmt.Errorf("Failed to load kernel module '%s': %s", module, err)
 					}
 				}
 			} else if key == "limits.disk.priority" {
diff --git a/lxd/main.go b/lxd/main.go
index 95f71fe..e288faf 100644
--- a/lxd/main.go
+++ b/lxd/main.go
@@ -621,7 +621,7 @@ func cmdInit() error {
 	// Detect zfs
 	out, err := exec.LookPath("zfs")
 	if err == nil && len(out) != 0 && !runningInUserns {
-		_ = shared.RunCommand("modprobe", "zfs")
+		_ = loadModule("zfs")
 
 		err := shared.RunCommand("zpool", "list")
 		if err == nil {
diff --git a/lxd/storage_zfs.go b/lxd/storage_zfs.go
index 36d7819..8d59b57 100644
--- a/lxd/storage_zfs.go
+++ b/lxd/storage_zfs.go
@@ -52,7 +52,7 @@ func (s *storageZfs) Init(config map[string]interface{}) (storage, error) {
 	err = s.zfsCheckPool(s.zfsPool)
 	if err != nil {
 		if shared.PathExists(shared.VarPath("zfs.img")) {
-			_ = exec.Command("modprobe", "zfs").Run()
+			_ = loadModule("zfs")
 
 			output, err := exec.Command("zpool", "import",
 				"-d", shared.VarPath(), s.zfsPool).CombinedOutput()
diff --git a/lxd/util.go b/lxd/util.go
index a496f82..cce5ffb 100644
--- a/lxd/util.go
+++ b/lxd/util.go
@@ -57,3 +57,11 @@ func etagCheck(r *http.Request, data interface{}) error {
 
 	return nil
 }
+
+func loadModule(module string) error {
+	if shared.PathExists(fmt.Sprintf("/sys/module/%s", module)) {
+		return nil
+	}
+
+	return shared.RunCommand("modprobe", module)
+}


More information about the lxc-devel mailing list