Bladeren bron

Make osl sandbox basepath configurable via execroot.

Signed-off-by: Madhu Venugopal <madhu@docker.com>
Madhu Venugopal 9 jaren geleden
bovenliggende
commit
25f0573785

+ 8 - 0
libnetwork/config/config.go

@@ -11,6 +11,7 @@ import (
 	"github.com/docker/libnetwork/cluster"
 	"github.com/docker/libnetwork/cluster"
 	"github.com/docker/libnetwork/datastore"
 	"github.com/docker/libnetwork/datastore"
 	"github.com/docker/libnetwork/netlabel"
 	"github.com/docker/libnetwork/netlabel"
+	"github.com/docker/libnetwork/osl"
 )
 )
 
 
 // Config encapsulates configurations of various Libnetwork components
 // Config encapsulates configurations of various Libnetwork components
@@ -197,6 +198,13 @@ func OptionDataDir(dataDir string) Option {
 	}
 	}
 }
 }
 
 
+// OptionExecRoot function returns an option setter for exec root folder
+func OptionExecRoot(execRoot string) Option {
+	return func(c *Config) {
+		osl.SetBasePath(execRoot)
+	}
+}
+
 // ProcessOptions processes options and stores it in config
 // ProcessOptions processes options and stores it in config
 func (c *Config) ProcessOptions(options ...Option) {
 func (c *Config) ProcessOptions(options ...Option) {
 	for _, opt := range options {
 	for _, opt := range options {

+ 15 - 4
libnetwork/osl/namespace_linux.go

@@ -6,6 +6,7 @@ import (
 	"net"
 	"net"
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
+	"path/filepath"
 	"runtime"
 	"runtime"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
@@ -21,7 +22,7 @@ import (
 	"github.com/vishvananda/netns"
 	"github.com/vishvananda/netns"
 )
 )
 
 
-const prefix = "/var/run/docker/netns"
+const defaultPrefix = "/var/run/docker"
 
 
 var (
 var (
 	once             sync.Once
 	once             sync.Once
@@ -30,6 +31,7 @@ var (
 	gpmWg            sync.WaitGroup
 	gpmWg            sync.WaitGroup
 	gpmCleanupPeriod = 60 * time.Second
 	gpmCleanupPeriod = 60 * time.Second
 	gpmChan          = make(chan chan struct{})
 	gpmChan          = make(chan chan struct{})
+	prefix           = defaultPrefix
 )
 )
 
 
 // The networkNamespace type is the linux implementation of the Sandbox
 // The networkNamespace type is the linux implementation of the Sandbox
@@ -48,12 +50,21 @@ type networkNamespace struct {
 	sync.Mutex
 	sync.Mutex
 }
 }
 
 
+// SetBasePath sets the base url prefix for the ns path
+func SetBasePath(path string) {
+	prefix = path
+}
+
 func init() {
 func init() {
 	reexec.Register("netns-create", reexecCreateNamespace)
 	reexec.Register("netns-create", reexecCreateNamespace)
 }
 }
 
 
+func basePath() string {
+	return filepath.Join(prefix, "netns")
+}
+
 func createBasePath() {
 func createBasePath() {
-	err := os.MkdirAll(prefix, 0755)
+	err := os.MkdirAll(basePath(), 0755)
 	if err != nil {
 	if err != nil {
 		panic("Could not create net namespace path directory")
 		panic("Could not create net namespace path directory")
 	}
 	}
@@ -142,7 +153,7 @@ func GenerateKey(containerID string) string {
 			indexStr string
 			indexStr string
 			tmpkey   string
 			tmpkey   string
 		)
 		)
-		dir, err := ioutil.ReadDir(prefix)
+		dir, err := ioutil.ReadDir(basePath())
 		if err != nil {
 		if err != nil {
 			return ""
 			return ""
 		}
 		}
@@ -172,7 +183,7 @@ func GenerateKey(containerID string) string {
 		maxLen = len(containerID)
 		maxLen = len(containerID)
 	}
 	}
 
 
-	return prefix + "/" + containerID[:maxLen]
+	return basePath() + "/" + containerID[:maxLen]
 }
 }
 
 
 // NewSandbox provides a new sandbox instance created in an os specific way
 // NewSandbox provides a new sandbox instance created in an os specific way

+ 4 - 0
libnetwork/osl/namespace_unsupported.go

@@ -10,3 +10,7 @@ func GC() {
 func GetSandboxForExternalKey(path string, key string) (Sandbox, error) {
 func GetSandboxForExternalKey(path string, key string) (Sandbox, error) {
 	return nil, nil
 	return nil, nil
 }
 }
+
+// SetBasePath sets the base url prefix for the ns path
+func SetBasePath(path string) {
+}

+ 4 - 0
libnetwork/osl/namespace_windows.go

@@ -37,3 +37,7 @@ func InitOSContext() func() {
 func SetupTestOSContext(t *testing.T) func() {
 func SetupTestOSContext(t *testing.T) func() {
 	return func() {}
 	return func() {}
 }
 }
+
+// SetBasePath sets the base url prefix for the ns path
+func SetBasePath(path string) {
+}

+ 4 - 0
libnetwork/osl/sandbox_freebsd.go

@@ -38,3 +38,7 @@ func InitOSContext() func() {
 func SetupTestOSContext(t *testing.T) func() {
 func SetupTestOSContext(t *testing.T) func() {
 	return func() {}
 	return func() {}
 }
 }
+
+// SetBasePath sets the base url prefix for the ns path
+func SetBasePath(path string) {
+}