[lxc-devel] [lxd/master] Fix path handling when running in the snap package

stgraber on Github lxc-bot at linuxcontainers.org
Tue Aug 22 05:32:15 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170822/317af9a8/attachment.bin>
-------------- next part --------------
From f368da1e0d63ea4944655e242e37bd575407f2d8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 22 Aug 2017 01:30:22 -0400
Subject: [PATCH 1/2] shared: Add wrapper to translate host paths
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>
---
 shared/util.go | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/shared/util.go b/shared/util.go
index 44a33c02a..d2e2526ca 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -100,6 +100,31 @@ func IsUnixSocket(path string) bool {
 	return (stat.Mode() & os.ModeSocket) == os.ModeSocket
 }
 
+// HostPath returns the host path for the provided path
+// On a normal system, this does nothing
+// When inside of a snap environment, returns the real path
+func HostPath(path string) string {
+	// Ignore relative paths
+	if len(path) == 0 || path[1] != os.PathSeparator {
+		return path
+	}
+
+	// Check if we're running in a snap package
+	snap := os.Getenv("SNAP")
+	if snap == "" {
+		return path
+	}
+
+	// Check if the path is already snap-aware
+	for _, prefix := range []string{"/snap", "/var/snap", "/var/lib/snapd"} {
+		if strings.HasPrefix(path, prefix) {
+			return path
+		}
+	}
+
+	return fmt.Sprintf("/var/lib/snapd/hostfs%s", path)
+}
+
 // VarPath returns the provided path elements joined by a slash and
 // appended to the end of $LXD_DIR, which defaults to /var/lib/lxd.
 func VarPath(path ...string) string {

From 83d933955a03b218d8c9b2a1a732340bda02b4bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 22 Aug 2017 01:30:42 -0400
Subject: [PATCH 2/2] Allow passing disk devices with the LXD snap
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #3660

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_lxc.go | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 07d972613..c9d27cb44 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1373,7 +1373,7 @@ func (c *containerLXC) initLXC() error {
 			networkidx++
 		} else if m["type"] == "disk" {
 			// Prepare all the paths
-			srcPath := m["source"]
+			srcPath := shared.HostPath(m["source"])
 			tgtPath := strings.TrimPrefix(m["path"], "/")
 			devName := fmt.Sprintf("disk.%s", strings.Replace(tgtPath, "/", "-", -1))
 			devPath := filepath.Join(c.DevicesPath(), devName)
@@ -1606,7 +1606,7 @@ func (c *containerLXC) startCommon() (string, error) {
 			// the storage volume, not the path where it is mounted.
 			// So do only check for the existence of m["source"]
 			// when m["pool"] is empty.
-			if m["pool"] == "" && m["source"] != "" && !shared.PathExists(m["source"]) {
+			if m["pool"] == "" && m["source"] != "" && !shared.PathExists(shared.HostPath(m["source"])) {
 				return "", fmt.Errorf("Missing source '%s' for disk '%s'", m["source"], name)
 			}
 		case "nic":
@@ -6209,7 +6209,7 @@ func (c *containerLXC) removeNetworkDevice(name string, m types.Device) error {
 // Disk device handling
 func (c *containerLXC) createDiskDevice(name string, m types.Device) (string, error) {
 	// Prepare all the paths
-	srcPath := m["source"]
+	srcPath := shared.HostPath(m["source"])
 	tgtPath := strings.TrimPrefix(m["path"], "/")
 	devName := fmt.Sprintf("disk.%s", strings.Replace(tgtPath, "/", "-", -1))
 	devPath := filepath.Join(c.DevicesPath(), devName)
@@ -6519,7 +6519,7 @@ func (c *containerLXC) getDiskLimits() (map[string]deviceBlockLimit, error) {
 		}
 
 		// Set the source path
-		source := m["source"]
+		source := shared.HostPath(m["source"])
 		if source == "" {
 			source = c.RootfsPath()
 		}


More information about the lxc-devel mailing list