81 lines
1.4 KiB
Go
81 lines
1.4 KiB
Go
//go:build linux
|
|
|
|
package main
|
|
|
|
import (
|
|
"antoine-roux.tk/projects/go/firecracker-netns/internal/netlink"
|
|
"antoine-roux.tk/projects/go/firecracker-netns/internal/netns"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
)
|
|
|
|
func setupEnv() int {
|
|
newNs, err := netns.New()
|
|
if err != nil {
|
|
fmt.Println("new ns error", err)
|
|
return 1
|
|
}
|
|
|
|
defer func(handle netns.NsHandle) {
|
|
err := handle.Close()
|
|
if err != nil {
|
|
fmt.Println("close ns error", err)
|
|
}
|
|
}(newNs)
|
|
|
|
defer func(ns netns.NsHandle) {
|
|
err := netns.Delete(ns)
|
|
if err != nil {
|
|
fmt.Println("delete ns error", err)
|
|
}
|
|
}(newNs)
|
|
|
|
vethPair, err := netlink.NewVethPair(newNs)
|
|
if err != nil {
|
|
fmt.Println("new Veth error", err)
|
|
return 1
|
|
}
|
|
|
|
defer func(veth *netlink.PairLink) {
|
|
err = veth.DeleteLink()
|
|
if err != nil {
|
|
fmt.Println("delete vethPair error", err)
|
|
}
|
|
}(vethPair)
|
|
|
|
err = netns.Set(newNs)
|
|
if err != nil {
|
|
return 1
|
|
}
|
|
|
|
// 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]- # "}
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
fmt.Printf("Error running the /bin/sh command - %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func main() {
|
|
// Lock the OS Thread, so we don't accidentally switch namespaces
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
|
|
os.Exit(setupEnv())
|
|
}
|