firecracker-netns/cmd/main.go

102 lines
2.0 KiB
Go

//go:build linux
package main
// inspired by https://github.com/firecracker-microvm/firectl/blob/main/main.go#L64
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.NewVirtualPairing(newNs, "wlp3s0")
if err != nil {
fmt.Println("new Veth error", err)
return 1
}
defer func(veth *netlink.PairLink) {
err = veth.DeleteVirtualPairing()
if err != nil {
fmt.Println("delete vethPair error", err)
}
}(vethPair)
err = netns.Set(newNs)
if err != nil {
fmt.Println("set guest ns error", err)
return 1
}
firstIpTapNetwork := net.IPv4(172, 16, 0, 1)
tapNetwork := net.IPNet{
IP: firstIpTapNetwork,
Mask: net.CIDRMask(30, 32),
}
tap, err := netlink.CreateTap(tapNetwork, vethPair.Guest.Link.Attrs().Name)
if err != nil {
fmt.Println("create tap in guest ns error", err)
return 1
}
defer func(tap *netlink.Tap) {
err := tap.DeleteTap()
if err != nil {
fmt.Println("delete tap error", err)
}
}(tap)
// 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())
}