62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
//go:build linux
|
|
|
|
package main
|
|
|
|
import (
|
|
"antoine-roux.tk/projects/go/firecracker-netns/internal/netns"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
)
|
|
|
|
func main() {
|
|
// Lock the OS Thread, so we don't accidentally switch namespaces
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
|
|
handle, err := netns.New()
|
|
if err != nil {
|
|
fmt.Println("new ns error", err)
|
|
}
|
|
|
|
defer func(handle *netns.NsHandle) {
|
|
err := handle.Close()
|
|
if err != nil {
|
|
fmt.Println("close ns error", err)
|
|
}
|
|
}(&handle)
|
|
|
|
err = netns.Set(handle)
|
|
if err != nil {
|
|
fmt.Println("set ns error", err)
|
|
}
|
|
|
|
// Do something with the network namespace
|
|
interfaces, _ := net.Interfaces()
|
|
fmt.Printf("Interfaces: %v\n", interfaces)
|
|
|
|
cmd := exec.Command("/bin/sh")
|
|
|
|
cmd.Stdin = os.Stdin
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Env = []string{"PS1=-[ns-process]- # "}
|
|
|
|
/* cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
Cloneflags: syscall.CLONE_NEWUTS,
|
|
}*/
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
fmt.Printf("Error running the /bin/sh command - %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
err = netns.Delete(handle)
|
|
if err != nil {
|
|
fmt.Println("delete ns error", err)
|
|
}
|
|
}
|