From 25f05737851658d121ff5af678178deb42bafd22 Mon Sep 17 00:00:00 2001 From: Madhu Venugopal Date: Thu, 14 Jul 2016 21:25:52 -0700 Subject: [PATCH] Make osl sandbox basepath configurable via execroot. Signed-off-by: Madhu Venugopal --- libnetwork/config/config.go | 8 ++++++++ libnetwork/osl/namespace_linux.go | 19 +++++++++++++++---- libnetwork/osl/namespace_unsupported.go | 4 ++++ libnetwork/osl/namespace_windows.go | 4 ++++ libnetwork/osl/sandbox_freebsd.go | 4 ++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/libnetwork/config/config.go b/libnetwork/config/config.go index b14691e2f7..eb8afea8b4 100644 --- a/libnetwork/config/config.go +++ b/libnetwork/config/config.go @@ -11,6 +11,7 @@ import ( "github.com/docker/libnetwork/cluster" "github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/netlabel" + "github.com/docker/libnetwork/osl" ) // 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 func (c *Config) ProcessOptions(options ...Option) { for _, opt := range options { diff --git a/libnetwork/osl/namespace_linux.go b/libnetwork/osl/namespace_linux.go index 3dad60472a..d3f88191f0 100644 --- a/libnetwork/osl/namespace_linux.go +++ b/libnetwork/osl/namespace_linux.go @@ -6,6 +6,7 @@ import ( "net" "os" "os/exec" + "path/filepath" "runtime" "strconv" "strings" @@ -21,7 +22,7 @@ import ( "github.com/vishvananda/netns" ) -const prefix = "/var/run/docker/netns" +const defaultPrefix = "/var/run/docker" var ( once sync.Once @@ -30,6 +31,7 @@ var ( gpmWg sync.WaitGroup gpmCleanupPeriod = 60 * time.Second gpmChan = make(chan chan struct{}) + prefix = defaultPrefix ) // The networkNamespace type is the linux implementation of the Sandbox @@ -48,12 +50,21 @@ type networkNamespace struct { sync.Mutex } +// SetBasePath sets the base url prefix for the ns path +func SetBasePath(path string) { + prefix = path +} + func init() { reexec.Register("netns-create", reexecCreateNamespace) } +func basePath() string { + return filepath.Join(prefix, "netns") +} + func createBasePath() { - err := os.MkdirAll(prefix, 0755) + err := os.MkdirAll(basePath(), 0755) if err != nil { panic("Could not create net namespace path directory") } @@ -142,7 +153,7 @@ func GenerateKey(containerID string) string { indexStr string tmpkey string ) - dir, err := ioutil.ReadDir(prefix) + dir, err := ioutil.ReadDir(basePath()) if err != nil { return "" } @@ -172,7 +183,7 @@ func GenerateKey(containerID string) string { maxLen = len(containerID) } - return prefix + "/" + containerID[:maxLen] + return basePath() + "/" + containerID[:maxLen] } // NewSandbox provides a new sandbox instance created in an os specific way diff --git a/libnetwork/osl/namespace_unsupported.go b/libnetwork/osl/namespace_unsupported.go index dbd8d9d35d..e385046121 100644 --- a/libnetwork/osl/namespace_unsupported.go +++ b/libnetwork/osl/namespace_unsupported.go @@ -10,3 +10,7 @@ func GC() { func GetSandboxForExternalKey(path string, key string) (Sandbox, error) { return nil, nil } + +// SetBasePath sets the base url prefix for the ns path +func SetBasePath(path string) { +} diff --git a/libnetwork/osl/namespace_windows.go b/libnetwork/osl/namespace_windows.go index a735623a44..bfdca30bcb 100644 --- a/libnetwork/osl/namespace_windows.go +++ b/libnetwork/osl/namespace_windows.go @@ -37,3 +37,7 @@ func InitOSContext() func() { func SetupTestOSContext(t *testing.T) func() { return func() {} } + +// SetBasePath sets the base url prefix for the ns path +func SetBasePath(path string) { +} diff --git a/libnetwork/osl/sandbox_freebsd.go b/libnetwork/osl/sandbox_freebsd.go index 0222afe3d8..e5bc6278ee 100644 --- a/libnetwork/osl/sandbox_freebsd.go +++ b/libnetwork/osl/sandbox_freebsd.go @@ -38,3 +38,7 @@ func InitOSContext() func() { func SetupTestOSContext(t *testing.T) func() { return func() {} } + +// SetBasePath sets the base url prefix for the ns path +func SetBasePath(path string) { +}