f-10: code: add create and delete netns #10
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
473e1d0108
commit
454005d6ac
7 changed files with 107 additions and 0 deletions
5
internal/netns/create.go
Normal file
5
internal/netns/create.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package netns
|
||||
|
||||
func Create(name string) error {
|
||||
return create(name)
|
||||
}
|
||||
55
internal/netns/create_linux.go
Normal file
55
internal/netns/create_linux.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
//go:build linux
|
||||
|
||||
package netns
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func create(name string) error {
|
||||
base := "/var/run/netns"
|
||||
path := base + "/" + name
|
||||
|
||||
if err := os.MkdirAll(base, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// fichier cible
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.Close()
|
||||
|
||||
// sauvegarde du netns courant
|
||||
orig, err := os.Open("/proc/self/ns/net")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer orig.Close()
|
||||
|
||||
// nouveau netns
|
||||
if err := unix.Unshare(unix.CLONE_NEWNET); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// bind mount du netns courant vers /var/run/netns/<name>
|
||||
if err := unix.Mount(
|
||||
"/proc/self/ns/net",
|
||||
path,
|
||||
"",
|
||||
unix.MS_BIND,
|
||||
"",
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// revenir au netns original
|
||||
if err := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
5
internal/netns/create_other.go
Normal file
5
internal/netns/create_other.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//go:build !linux
|
||||
|
||||
package netns
|
||||
|
||||
func create(string) error { return nil }
|
||||
5
internal/netns/delete.go
Normal file
5
internal/netns/delete.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package netns
|
||||
|
||||
func Delete(name string) error {
|
||||
return delete(name)
|
||||
}
|
||||
18
internal/netns/delete_linux.go
Normal file
18
internal/netns/delete_linux.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
//go:build linux
|
||||
|
||||
package netns
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func delete(name string) error {
|
||||
path := "/var/run/netns/" + name
|
||||
|
||||
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Remove(path)
|
||||
}
|
||||
5
internal/netns/delete_other.go
Normal file
5
internal/netns/delete_other.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//go:build !linux
|
||||
|
||||
package netns
|
||||
|
||||
func delete(string) error { return nil }
|
||||
14
internal/netns/exist.go
Normal file
14
internal/netns/exist.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package netns
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func exist(name string) bool {
|
||||
_, err := os.Stat("/var/run/netns/" + name)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func Exist(name string) bool {
|
||||
return exist(name)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue