From abfef614052fae15a46825a0c4c17180b8cd54b8 Mon Sep 17 00:00:00 2001 From: Sangbum Kim Date: Tue, 1 Mar 2016 01:33:34 +0900 Subject: [PATCH] added main source file --- connet.service | 25 ++++++++++ main.go | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 connet.service create mode 100644 main.go diff --git a/connet.service b/connet.service new file mode 100644 index 0000000..2573f25 --- /dev/null +++ b/connet.service @@ -0,0 +1,25 @@ + +[Unit] +Description=MACVLAN setup +#Documentation=man:systemd-networkd.service(8) +ConditionCapability=CAP_NET_ADMIN +DefaultDependencies=no +# dbus.service can be dropped once on kdbus, and systemd-udevd.service can be +# dropped once tuntap is moved to netlink +After=systemd-udevd.service dbus.service network-pre.target systemd-sysusers.service systemd-sysctl.service +Before=network.target multi-user.target shutdown.target +Conflicts=shutdown.target +Wants=network.target + + +[Service] +# the VT is cleared by TTYVTDisallocate +RemainAfterExit=yes +#Environment=IP_ADDR= GATEWAY_ADDR= +EnvironmentFile=/proc/1/environ +ExecStart=/connet +Type=oneshot +[Install] +WantedBy=multi-user.target + + diff --git a/main.go b/main.go new file mode 100644 index 0000000..6c37a0a --- /dev/null +++ b/main.go @@ -0,0 +1,123 @@ +package main + +import ( + "github.com/vishvananda/netlink" + "io/ioutil" + "strings" + "net" + "fmt" + "os" + "regexp" +) + +var env map[string]string=make(map[string]string) + +func main(){ + readInitEnviron() + + macAddr,_ := net.ParseMAC(getEnv("IF_ADDR")) +// fpAddr,_ := netlink.ParseAddr(getEnv("IP_ADDR")) +// gatewayAddr := net.ParseIP(getEnv("GATEWAY_ADDR")) + + linkInterfaceType:=getEnvWithDefault("IF_TYPE","macvlan") + + link:=findFirstInterface(linkInterfaceType) + + renameInterface(link,"eth0") + + renameMacAddress(link,macAddr) + +/* + #netlink.AddrAdd(link,ipAddr) + + #netlink.LinkSetUp(link) + + #addRoute(gatewayAddr,link) +*/ +} + +func readInitEnviron(){ + envFilePath:="/proc/1/environ" + content, err := ioutil.ReadFile(envFilePath) + kvParseRegex,_:=regexp.Compile("^([^=]+)=(.+)$") + if err != nil { + panic(fmt.Sprintf("environ file %s not existed!",envFilePath)) + } + lines := strings.Split(string(content), "\x00") + for _,element := range lines { + result_slice := kvParseRegex.FindStringSubmatch(element) + if len(result_slice) < 2 { + continue + }else{ + env[result_slice[1]]=result_slice[2] + } + } +} +func renameMacAddress(link netlink.Link,new_name net.HardwareAddr){ + err:=netlink.LinkSetHardwareAddr(link,new_name) + if(err != nil){ + panic(fmt.Sprintf("cannot rename mac address %s -> %s",link.Attrs().HardwareAddr,new_name)) + } else { + fmt.Printf("rename mac address %s -> %s\n",link.Attrs().HardwareAddr,new_name) + } + +} + +func renameInterface(link netlink.Link,new_name string) { + err:=netlink.LinkSetName(link,new_name) + if(err != nil){ + panic(fmt.Sprintf("cannot rename interface %s -> %s",link.Attrs().Name,new_name)) + } else { + fmt.Printf("rename interface %s -> %s\n",link.Attrs().Name,new_name) + } +} + +func findFirstInterface(if_type string) netlink.Link { + links,_:=netlink.LinkList() + for _,element := range links { + if(element.Type() == if_type){ + fmt.Println(element.Attrs().Name) + return element + } + } + panic(fmt.Sprintf("cannot get type: %s interface",if_type)) +} + +func getEnv(variable string) string { + envValue,ok:=env[variable] + if ok { + return envValue + } + envValue=os.Getenv(variable) + + if(envValue == ""){ + panic(fmt.Sprintf("cannot get ${%s}",variable)) + } else{ + return envValue + } +} + +func getEnvWithDefault(variable string, ret_is_empty string) string { + envValue,ok:=env[variable] + if ok { + return envValue + } + envValue=os.Getenv(variable) + + if(envValue == ""){ + return ret_is_empty + } else{ + return envValue + } +} + +func addRoute(gateway net.IP,link netlink.Link){ + default_network := &net.IPNet{ + IP: net.IPv4(0, 0, 0, 0), + Mask: net.CIDRMask(0, 32), + } + + route := &netlink.Route{LinkIndex: link.Attrs().Index, Dst: default_network, Gw:gateway} + + netlink.RouteAdd(route) +}