diff --git a/pkg/hostman/hostinfo/hostbridge/hostbridge.go b/pkg/hostman/hostinfo/hostbridge/hostbridge.go index f63f76220d..4722badf38 100644 --- a/pkg/hostman/hostinfo/hostbridge/hostbridge.go +++ b/pkg/hostman/hostinfo/hostbridge/hostbridge.go @@ -31,7 +31,7 @@ import ( ) type IBridgeDriver interface { - ConfirmToConfig(bool, []string) (bool, error) + ConfirmToConfig() (bool, error) GetMac() string FetchConfig() Setup(IBridgeDriver) error @@ -46,6 +46,7 @@ type IBridgeDriver interface { CleanupConfig() SetupBridgeDev() error SetupInterface() error + PersistentMac() error GenerateIfupScripts(scriptPath string, nic jsonutils.JSONObject) error GenerateIfdownScripts(scriptPath string, nic jsonutils.JSONObject) error @@ -59,6 +60,8 @@ type SBaseBridgeDriver struct { bridge *netutils2.SNetInterface ip string inter *netutils2.SNetInterface + + drv IBridgeDriver } func NewBaseBridgeDriver(bridge, inter, ip string) (*SBaseBridgeDriver, error) { @@ -89,6 +92,10 @@ func (d *SBaseBridgeDriver) GetMac() string { return d.bridge.Mac } +func (d *SBaseBridgeDriver) PersistentMac() error { + return nil +} + func (d *SBaseBridgeDriver) BringupInterface() error { var infs = []*netutils2.SNetInterface{d.bridge} if d.inter != nil { @@ -106,8 +113,12 @@ func (d *SBaseBridgeDriver) BringupInterface() error { return nil } -func (d *SBaseBridgeDriver) ConfirmToConfig(exists bool, infs []string) (bool, error) { - if exists { +func (d *SBaseBridgeDriver) ConfirmToConfig() (bool, error) { + exist, err := d.drv.Exists() + if err != nil { + return false, err + } + if exist { d.bridge.FetchConfig() if len(d.ip) > 0 { if len(d.bridge.Addr) == 0 { @@ -136,6 +147,10 @@ func (d *SBaseBridgeDriver) ConfirmToConfig(exists bool, infs []string) (bool, e return false, fmt.Errorf("%s should have address in 169.254.0.0/16", d.bridge) } } + infs, err := d.drv.Interfaces() + if err != nil { + return false, err + } if d.inter != nil && !utils.IsInStringArray(d.inter.String(), infs) { log.Infof("Interface %s not in bridge...", d.inter) return false, nil diff --git a/pkg/hostman/hostinfo/hostbridge/linux_bridge.go b/pkg/hostman/hostinfo/hostbridge/linux_bridge.go index a08e137798..6bb06afd09 100644 --- a/pkg/hostman/hostinfo/hostbridge/linux_bridge.go +++ b/pkg/hostman/hostinfo/hostbridge/linux_bridge.go @@ -18,7 +18,9 @@ func NewLinuxBridgeDeriver(bridge, inter, ip string) (*SLinuxBridgeDriver, error if err != nil { return nil, err } - return &SLinuxBridgeDriver{*base}, nil + linuxBridgeDrv := &SLinuxBridgeDriver{*base} + linuxBridgeDrv.drv = linuxBridgeDrv + return linuxBridgeDrv, nil } func LinuxBridgePrepare() error { @@ -109,6 +111,14 @@ func (l *SLinuxBridgeDriver) SetupBridgeDev() error { return nil } +func (d *SLinuxBridgeDriver) PersistentMac() error { + output, err := procutils.NewCommand("ifconfig", d.bridge.String(), "hw", "ether", d.inter.Mac).Run() + if err != nil { + return fmt.Errorf("Linux bridge set mac address failed %s %s", output, err) + } + return nil +} + func (l *SLinuxBridgeDriver) RegisterHostlocalServer(mac, ip string) error { metadataPort := l.GetMetadataServerPort() metadataServerLoc := fmt.Sprintf("%s:%d", ip, metadataPort) diff --git a/pkg/hostman/hostinfo/hostbridge/ovs.go b/pkg/hostman/hostinfo/hostbridge/ovs.go index 48d3086a6e..d29a8c1f4f 100644 --- a/pkg/hostman/hostinfo/hostbridge/ovs.go +++ b/pkg/hostman/hostinfo/hostbridge/ovs.go @@ -315,5 +315,7 @@ func NewOVSBridgeDriver(bridge, inter, ip string) (*SOVSBridgeDriver, error) { if err != nil { return nil, err } - return &SOVSBridgeDriver{*base}, nil + ovsDrv := &SOVSBridgeDriver{*base} + ovsDrv.drv = ovsDrv + return ovsDrv, nil } diff --git a/pkg/hostman/hostinfo/hostinfohelper.go b/pkg/hostman/hostinfo/hostinfohelper.go index 1c30fda0d2..2ef8f38413 100644 --- a/pkg/hostman/hostinfo/hostinfohelper.go +++ b/pkg/hostman/hostinfo/hostinfohelper.go @@ -17,6 +17,7 @@ package hostinfo import ( "bufio" "context" + "fmt" "os" "regexp" "strconv" @@ -230,6 +231,9 @@ func (n *SNIC) ExitCleanup() { func NewNIC(desc string) (*SNIC, error) { nic := new(SNIC) data := strings.Split(desc, "/") + if len(data) < 3 { + return nil, fmt.Errorf("Parse nic conf %s failed, too short", desc) + } nic.Inter = data[0] nic.Bridge = data[1] if regutils.MatchIP4Addr(data[2]) { @@ -266,15 +270,7 @@ func NewNIC(desc string) (*SNIC, error) { return nil, err } - exist, err := nic.BridgeDev.Exists() - if err != nil { - return nil, err - } - infs, err := nic.BridgeDev.Interfaces() - if err != nil { - return nil, err - } - confirm, err := nic.BridgeDev.ConfirmToConfig(exist, infs) + confirm, err := nic.BridgeDev.ConfirmToConfig() if err != nil { log.Errorln(err) return nil, err @@ -289,6 +285,9 @@ func NewNIC(desc string) (*SNIC, error) { } else { log.Infof("Confirm to configuration!!") } + if err := nic.BridgeDev.PersistentMac(); err != nil { + return nil, err + } var dhcpRelay []string if nic.EnableDHCPRelay() {