1
1
Fork 0

added main source file

This commit is contained in:
Sangbum Kim 2016-03-01 01:33:34 +09:00
parent 44066dc126
commit abfef61405
2 changed files with 148 additions and 0 deletions

25
connet.service Normal file
View File

@ -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

123
main.go Normal file
View File

@ -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)
}