Skip to content

Instantly share code, notes, and snippets.

@CMGS
Created May 15, 2015 14:36
Show Gist options
  • Save CMGS/4f60fd8da2014eed0dd8 to your computer and use it in GitHub Desktop.
Save CMGS/4f60fd8da2014eed0dd8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net"
"os"
"syscall"
"github.com/docker/libcontainer/netlink"
"github.com/docker/libcontainer/system"
)
func set(f *os.File) {
system.Setns(f.Fd(), syscall.CLONE_NEWNET)
ifc, _ := net.InterfaceByName("mt2")
ip, ipn, _ := net.ParseCIDR("10.222.7.101/16")
fmt.Println("set ip", ifc, netlink.NetworkLinkAddIp(ifc, ip, ipn))
fmt.Println("up link", ifc, netlink.NetworkLinkUp(ifc))
defer fmt.Println("close", f.Close())
}
func main() {
netlink.NetworkLinkAddMacVlan("em1", "mt2", "bridge")
ifc, _ := net.InterfaceByName("mt2")
fmt.Println("ifc in host", ifc)
netlink.NetworkSetNsPid(ifc, 19474)
f, _ := os.OpenFile("/proc/19474/ns/net", os.O_RDONLY, 0)
set(f)
ifc, _ = net.InterfaceByName("mt2")
fmt.Println("check outside", ifc)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment