[lxc-devel] [lxd/master] Feature request : #3125

qq690388648 on Github lxc-bot at linuxcontainers.org
Wed Mar 29 16:08:09 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 324 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170329/64ba0724/attachment.bin>
-------------- next part --------------
From 8ff7a1a0b87dd1745a8d1ef3fad8239a5a17c270 Mon Sep 17 00:00:00 2001
From: linan <690388648 at qq.com>
Date: Thu, 30 Mar 2017 00:00:19 +0800
Subject: [PATCH] Add `source` attribute for `unix-block` to make it renamable.
 Signed-off-by: sungengze  sungengze1 at huawei.com

---
 lxd/container.go     | 14 +++++++++----
 lxd/container_lxc.go | 55 +++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/lxd/container.go b/lxd/container.go
index 8ed673e..ee0926a 100644
--- a/lxd/container.go
+++ b/lxd/container.go
@@ -80,6 +80,8 @@ func containerValidDeviceConfigKey(t, k string) bool {
 			return true
 		case "mode":
 			return true
+		case "source":
+			return true
 		case "path":
 			return true
 		case "uid":
@@ -329,16 +331,20 @@ func containerValidDevices(devices types.Devices, profile bool, expanded bool) e
 			}
 
 		} else if shared.StringInSlice(m["type"], []string{"unix-char", "unix-block"}) {
-			if m["path"] == "" {
-				return fmt.Errorf("Unix device entry is missing the required \"path\" property.")
+			if m["source"] == "" && m["path"] == "" {
+				return fmt.Errorf("Unix device entry is missing the required \"source\" and \"path\" property.")
 			}
 
 			if m["major"] == "" || m["minor"] == "" {
-				if !shared.PathExists(m["path"]) {
+				srcPath, exist := m["source"]
+				if !exist {
+					srcPath = m["path"]
+				}
+				if !shared.PathExists(srcPath) {
 					return fmt.Errorf("The device path doesn't exist on the host and major/minor wasn't specified.")
 				}
 
-				dType, _, _, err := deviceGetAttributes(m["path"])
+				dType, _, _, err := deviceGetAttributes(srcPath)
 				if err != nil {
 					return err
 				}
diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 79753e6..5db7909 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1184,13 +1184,21 @@ func (c *containerLXC) initLXC() error {
 		m := c.expandedDevices[k]
 		if shared.StringInSlice(m["type"], []string{"unix-char", "unix-block"}) {
 			// Prepare all the paths
-			srcPath := m["path"]
-			tgtPath := strings.TrimPrefix(srcPath, "/")
-			devName := fmt.Sprintf("unix.%s", strings.Replace(tgtPath, "/", "-", -1))
+			srcPath, exist := m["source"]
+			if !exist {
+				srcPath = m["path"]
+			}
+			relativeSrcPath := strings.TrimPrefix(srcPath, "/")
+			devName := fmt.Sprintf("unix.%s", strings.Replace(relativeSrcPath, "/", "-", -1))
 			devPath := filepath.Join(c.DevicesPath(), devName)
+			tgtPath, exist := m["path"]
+			if !exist {
+				tgtPath = m["source"]
+			}
+			relativeTgtPath := strings.TrimPrefix(tgtPath, "/")
 
 			// Set the bind-mount entry
-			err = lxcSetConfigItem(cc, "lxc.mount.entry", fmt.Sprintf("%s %s none bind,create=file", devPath, tgtPath))
+			err = lxcSetConfigItem(cc, "lxc.mount.entry", fmt.Sprintf("%s %s none bind,create=file", devPath, relativeTgtPath))
 			if err != nil {
 				return err
 			}
@@ -1515,8 +1523,12 @@ func (c *containerLXC) startCommon() (string, error) {
 				return "", fmt.Errorf("Missing parent '%s' for nic '%s'", m["parent"], name)
 			}
 		case "unix-char", "unix-block":
-			if m["path"] != "" && m["major"] == "" && m["minor"] == "" && !shared.PathExists(m["path"]) {
-				return "", fmt.Errorf("Missing source '%s' for device '%s'", m["path"], name)
+			srcPath, exist := m["source"]
+			if !exist {
+				srcPath = m["path"]
+			}
+			if m["path"] != "" && m["major"] == "" && m["minor"] == "" && !shared.PathExists(srcPath) {
+				return "", fmt.Errorf("Missing source '%s' for device '%s'", srcPath, name)
 			}
 		}
 	}
@@ -5243,9 +5255,12 @@ func (c *containerLXC) createUnixDevice(m types.Device) ([]string, error) {
 	var major, minor int
 
 	// Our device paths
-	srcPath := m["path"]
-	tgtPath := strings.TrimPrefix(srcPath, "/")
-	devName := fmt.Sprintf("unix.%s", strings.Replace(tgtPath, "/", "-", -1))
+	srcPath, exist := m["source"]
+	if !exist {
+		srcPath = m["path"]
+	}
+	relativeSrcPath := strings.TrimPrefix(srcPath, "/")
+	devName := fmt.Sprintf("unix.%s", strings.Replace(relativeSrcPath, "/", "-", -1))
 	devPath := filepath.Join(c.DevicesPath(), devName)
 
 	// Extra checks for nesting
@@ -5371,7 +5386,7 @@ func (c *containerLXC) createUnixDevice(m types.Device) ([]string, error) {
 		}
 	}
 
-	return []string{devPath, tgtPath}, nil
+	return []string{devPath, relativeSrcPath}, nil
 }
 
 func (c *containerLXC) insertUnixDevice(m types.Device) error {
@@ -5457,9 +5472,12 @@ func (c *containerLXC) removeUnixDevice(m types.Device) error {
 	}
 
 	// Figure out the paths
-	srcPath := m["path"]
-	tgtPath := strings.TrimPrefix(srcPath, "/")
-	devName := fmt.Sprintf("unix.%s", strings.Replace(tgtPath, "/", "-", -1))
+	srcPath, exist := m["source"]
+	if !exist {
+		srcPath = m["path"]
+	}
+	relativeSrcPath := strings.TrimPrefix(srcPath, "/")
+	devName := fmt.Sprintf("unix.%s", strings.Replace(relativeSrcPath, "/", "-", -1))
 	devPath := filepath.Join(c.DevicesPath(), devName)
 
 	// Check if we've been passed major and minor numbers already.
@@ -5504,13 +5522,18 @@ func (c *containerLXC) removeUnixDevice(m types.Device) error {
 	}
 
 	// Remove the bind-mount from the container
-	if c.FileExists(tgtPath) == nil {
-		err = c.removeMount(m["path"])
+	tgtPath, exist := m ["path"]
+	if !exist {
+		tgtPath = m["source"]
+	}
+	relativeTgtPath := strings.TrimPrefix(tgtPath, "/")
+	if c.FileExists(relativeTgtPath) == nil {
+		err = c.removeMount(tgtPath)
 		if err != nil {
 			return fmt.Errorf("Error unmounting the device: %s", err)
 		}
 
-		err = c.FileRemove(tgtPath)
+		err = c.FileRemove(relativeTgtPath)
 		if err != nil {
 			return fmt.Errorf("Error removing the device: %s", err)
 		}


More information about the lxc-devel mailing list