From c2d14440e1ef95666037703f2c8cc0389dba6bee Mon Sep 17 00:00:00 2001 From: Michael Zappa Date: Thu, 14 Apr 2022 09:26:24 -0600 Subject: [PATCH 1/3] support ns path Signed-off-by: Michael Zappa --- netns_linux.go | 52 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/netns_linux.go b/netns_linux.go index 6be5c55..02b6470 100644 --- a/netns_linux.go +++ b/netns_linux.go @@ -18,13 +18,8 @@ import ( // Deprecated: use syscall pkg instead (go >= 1.5 needed). const ( - CLONE_NEWUTS = 0x04000000 /* New utsname group? */ - CLONE_NEWIPC = 0x08000000 /* New ipcs */ - CLONE_NEWUSER = 0x10000000 /* New user namespace */ - CLONE_NEWPID = 0x20000000 /* New pid namespace */ - CLONE_NEWNET = 0x40000000 /* New network namespace */ - CLONE_IO = 0x80000000 /* Get io context */ - bindMountPath = "/run/netns" /* Bind mount path for named netns */ + CLONE_NEWNET = syscall.CLONE_NEWNET /* New network namespace */ + bindMountPath = "/run/netns" /* Bind mount path for named netns */ ) // Setns sets namespace using syscall. Note that this should be a method @@ -79,6 +74,37 @@ func NewNamed(name string) (NsHandle, error) { return newNs, nil } +// NewNamed creates a new named network namespace and returns a handle to it +func NewNamedWithDir(name, dir string) (NsHandle, error) { + if _, err := os.Stat(dir); os.IsNotExist(err) { + err = os.MkdirAll(dir, 0755) + if err != nil { + return None(), err + } + } + + newNs, err := New() + if err != nil { + return None(), err + } + + namedPath := path.Join(dir, name) + + f, err := os.OpenFile(namedPath, os.O_CREATE|os.O_EXCL, 0444) + if err != nil { + return None(), err + } + f.Close() + + nsPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()) + err = syscall.Mount(nsPath, namedPath, "bind", syscall.MS_BIND, "") + if err != nil { + return None(), err + } + + return newNs, nil +} + // DeleteNamed deletes a named network namespace func DeleteNamed(name string) error { namedPath := path.Join(bindMountPath, name) @@ -91,6 +117,18 @@ func DeleteNamed(name string) error { return os.Remove(namedPath) } +// DeleteNamed deletes a named network namespace +func DeleteNamedWithDir(name, dir string) error { + namedPath := path.Join(dir, name) + + err := syscall.Unmount(namedPath, syscall.MNT_DETACH) + if err != nil { + return err + } + + return os.Remove(namedPath) +} + // Get gets a handle to the current threads network namespace. func Get() (NsHandle, error) { return GetFromThread(os.Getpid(), unix.Gettid()) From f5da6ea82dfb378f7c2e4a00bdbdc9a7d68f87be Mon Sep 17 00:00:00 2001 From: Michael Zappa Date: Thu, 14 Apr 2022 09:32:11 -0600 Subject: [PATCH 2/3] remove duplicated code Signed-off-by: Michael Zappa --- netns_linux.go | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/netns_linux.go b/netns_linux.go index 02b6470..519e42a 100644 --- a/netns_linux.go +++ b/netns_linux.go @@ -16,7 +16,6 @@ import ( "golang.org/x/sys/unix" ) -// Deprecated: use syscall pkg instead (go >= 1.5 needed). const ( CLONE_NEWNET = syscall.CLONE_NEWNET /* New network namespace */ bindMountPath = "/run/netns" /* Bind mount path for named netns */ @@ -45,33 +44,7 @@ func New() (ns NsHandle, err error) { // NewNamed creates a new named network namespace and returns a handle to it func NewNamed(name string) (NsHandle, error) { - if _, err := os.Stat(bindMountPath); os.IsNotExist(err) { - err = os.MkdirAll(bindMountPath, 0755) - if err != nil { - return None(), err - } - } - - newNs, err := New() - if err != nil { - return None(), err - } - - namedPath := path.Join(bindMountPath, name) - - f, err := os.OpenFile(namedPath, os.O_CREATE|os.O_EXCL, 0444) - if err != nil { - return None(), err - } - f.Close() - - nsPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()) - err = syscall.Mount(nsPath, namedPath, "bind", syscall.MS_BIND, "") - if err != nil { - return None(), err - } - - return newNs, nil + return NewNamedWithDir(name, bindMountPath) } // NewNamed creates a new named network namespace and returns a handle to it @@ -107,14 +80,7 @@ func NewNamedWithDir(name, dir string) (NsHandle, error) { // DeleteNamed deletes a named network namespace func DeleteNamed(name string) error { - namedPath := path.Join(bindMountPath, name) - - err := syscall.Unmount(namedPath, syscall.MNT_DETACH) - if err != nil { - return err - } - - return os.Remove(namedPath) + return DeleteNamedWithDir(name, bindMountPath) } // DeleteNamed deletes a named network namespace From 93fbe2bdeb20e417f55ef0982db64e0c9dc9b785 Mon Sep 17 00:00:00 2001 From: Michael Zappa Date: Thu, 14 Apr 2022 09:53:24 -0600 Subject: [PATCH 3/3] support delete by path Signed-off-by: Michael Zappa --- netns_linux.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/netns_linux.go b/netns_linux.go index 519e42a..0431bdf 100644 --- a/netns_linux.go +++ b/netns_linux.go @@ -95,6 +95,15 @@ func DeleteNamedWithDir(name, dir string) error { return os.Remove(namedPath) } +func DeleteByPath(path string) error { + err := syscall.Unmount(path, syscall.MNT_DETACH) + if err != nil { + return err + } + + return os.Remove(path) +} + // Get gets a handle to the current threads network namespace. func Get() (NsHandle, error) { return GetFromThread(os.Getpid(), unix.Gettid())