1
1
Fork 0

macvlan 설정 가능하도록 함

This commit is contained in:
Sangbum Kim 2017-04-24 00:07:11 +09:00
parent c8be99e787
commit 0508524148
3 changed files with 66 additions and 14 deletions

View File

@ -22,17 +22,16 @@ func main(){
renameInterface(link,"eth0") renameInterface(link,"eth0")
switch linkInterfaceType { if macAddr,err:= net.ParseMAC(getEnvWithDefault("MAC_ADDR","")) ;err == nil{
case "macvlan": renameMacAddress(link,macAddr)
macAddr,_ := net.ParseMAC(getEnv("IF_ADDR")) }
renameMacAddress(link,macAddr) if ipAddr,err:= netlink.ParseAddr(getEnvWithDefault("IP_ADDR","")) ; err ==nil{
case "ipvlan": netlink.AddrAdd(link,ipAddr)
ipAddr,_ := netlink.ParseAddr(getEnv("IP_ADDR")) }
gatewayAddr := net.ParseIP(getEnv("GATEWAY_ADDR")) netlink.LinkSetUp(link)
netlink.AddrAdd(link,ipAddr) if gatewayAddrStr:= getEnvWithDefault("GATEWAY_ADDR","");len(gatewayAddrStr)>0{
netlink.LinkSetUp(link) addRoute(net.ParseIP(gatewayAddrStr),link)
addRoute(gatewayAddr,link) }
}
} }
func readInitEnviron(){ func readInitEnviron(){

View File

@ -23,15 +23,39 @@ import (
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
type cmdList []string
func (i *cmdList) Set(value string) error {
*i = append(*i, value)
return nil
}
func (i *cmdList) String() string {
return ""
}
func (i *cmdList) IsCumulative() bool {
return true
}
func CmdList(s kingpin.Settings) (target *[]string) {
target = new([]string)
s.SetValue((*cmdList)(target))
return
}
var ( var (
app = kingpin.New("octopus-outside", "nspawn Bootstrapper.").Author("Sangbum Kim<sangbumkim@amuz.es>") app = kingpin.New("octopus-outside", "nspawn Bootstrapper.").Author("Sangbum Kim<sangbumkim@amuz.es>")
verbosePtr = app.Flag("verbose", "Enable verbose mode.").Short('v').Bool() verbosePtr = app.Flag("verbose", "Enable verbose mode.").Short('v').Bool()
clearPtr = app.Flag("clear", "clear os environment.").Short('p').Bool() clearPtr = app.Flag("clear", "clear os environment.").Short('p').Bool()
ifBindTypePtr = app.Flag("if-bind-type", "choose virtual ethernet bind type(ipvlan,macvlan).").Default("ipvlan").Short('b').Enum("ipvlan","macvlan")
macAddrPtr = app.Flag("mac-address", "Specify MAC address of macvlan mode. if not specify, use random.").Short('a').String()
nspawnPathPtr = app.Flag("nspawn-path", "systemd-nspawn location.").Short('s').String() nspawnPathPtr = app.Flag("nspawn-path", "systemd-nspawn location.").Short('s').String()
containerHomePtr = app.Flag("container-home", "container residence path").Default("/container").Short('d').String() containerHomePtr = app.Flag("container-home", "container residence path").Default("/container").Short('d').String()
configFileNamePtr = app.Flag("config-file", "config file name").Default("settings.yml").HintOptions("FILENAME").Short('c').String() configFileNamePtr = app.Flag("config-file", "config file name").Default("settings.yml").HintOptions("FILENAME").Short('c').String()
nodeNamePtr = app.Arg("nodeName", "nodename to spawn.").Required().String() nodeNamePtr = app.Arg("nodeName", "nodename to spawn.").Required().String()
bindIfNamePtr = app.Arg("bindInterface", "interface name to bind.").Required().String() bindIfNamePtr = app.Arg("bindInterface", "interface name to bind.").Required().String()
cmdListArgsPtr = CmdList(app.Arg("cmd","execute command rather than boot process"))
) )
type mountPoint struct { type mountPoint struct {
@ -40,6 +64,7 @@ type mountPoint struct {
Readonly bool `yaml:"readonly"` Readonly bool `yaml:"readonly"`
} }
func (mp mountPoint) Option(nodePath string) (option string, err error) { func (mp mountPoint) Option(nodePath string) (option string, err error) {
var mountPointPath string var mountPointPath string
if filepath.IsAbs(mp.Name){ if filepath.IsAbs(mp.Name){
@ -289,10 +314,13 @@ func main() {
_, err := app.Parse(os.Args[1:]) _, err := app.Parse(os.Args[1:])
var ( var (
verbose = *verbosePtr verbose = *verbosePtr
ifBindType = *ifBindTypePtr
macAddr = strings.TrimSpace(*macAddrPtr)
clear = *clearPtr clear = *clearPtr
nodeName = *nodeNamePtr nodeName = *nodeNamePtr
bindIfName = *bindIfNamePtr bindIfName = *bindIfNamePtr
configFileName = *configFileNamePtr configFileName = *configFileNamePtr
cmdListArgs = *cmdListArgsPtr
nspawnPath string nspawnPath string
progress = -1 progress = -1
) )
@ -371,13 +399,25 @@ func main() {
fmt.Printf(" - bind interface{%s} : valid\n", bindIfName) fmt.Printf(" - bind interface{%s} : valid\n", bindIfName)
fmt.Printf(" - default routing info : ip %s/%d link %s\n", gw.String(), cidr, linkname) fmt.Printf(" - default routing info : ip %s/%d link %s\n", gw.String(), cidr, linkname)
fmt.Printf(" - node{%s} IP address : %s\n", nodeName, nodeAddr.String()) fmt.Printf(" - node{%s} IP address : %s\n", nodeName, nodeAddr.String())
fmt.Printf(" - bind interface type : %s\n",ifBindType)
} }
option := []string{ option := []string{
"--quiet", "--quiet",
"--boot",
"--link-journal=try-host", "--link-journal=try-host",
"--notify-ready=true"} "--notify-ready=true"}
option = append(option, fmt.Sprintf("--network-ipvlan=%s", bindIfName)) switch ifBindType {
case "macvlan":
option = append(option, fmt.Sprintf("--network-macvlan=%s", bindIfName))
if len(macAddr)>0{
option = append(option, fmt.Sprintf("--setenv=MAC_ADDR=%s", macAddr))
}
case "ipvlan":
option = append(option, fmt.Sprintf("--network-ipvlan=%s", bindIfName))
default:
panic(errors.New("unknown bind type"))
}
option = append(option, fmt.Sprintf("--setenv=IF_TYPE=%s", ifBindType))
option = append(option, fmt.Sprintf("--setenv=IP_ADDR=%s/%d", nodeAddr.String(), cidr)) option = append(option, fmt.Sprintf("--setenv=IP_ADDR=%s/%d", nodeAddr.String(), cidr))
option = append(option, fmt.Sprintf("--setenv=GATEWAY_ADDR=%s", gw.String())) option = append(option, fmt.Sprintf("--setenv=GATEWAY_ADDR=%s", gw.String()))
option = append(option, fmt.Sprintf("--machine=%s", nodeName)) option = append(option, fmt.Sprintf("--machine=%s", nodeName))
@ -390,6 +430,19 @@ func main() {
option = append(option, mountOption) option = append(option, mountOption)
} }
} }
if len(cmdListArgs) == 0 {
if verbose {
fmt.Println("startup type: using systemd")
}
option = append(option, "--boot")
}else {
if verbose {
fmt.Println("startup type: using command")
}
for _, cmdArg := range cmdListArgs {
option = append(option, fmt.Sprintf(" %s",cmdArg))
}
}
var env []string var env []string
if clear { if clear {
env = []string{} env = []string{}

View File

@ -15,7 +15,7 @@ After=dnsmasq.service
After=pdnsd.service After=pdnsd.service
[Service] [Service]
ExecStart=/container/octopus-outside %I igb0 -v ExecStart=/container/octopus-outside %I igb0 -v -b macvlan
KillMode=mixed KillMode=mixed
Type=notify Type=notify
RestartForceExitStatus=133 RestartForceExitStatus=133