From 063ac404f09c02571f03f06fdadea519f7b746e2 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Mon, 22 Jun 2020 21:27:31 +0800 Subject: [PATCH] disable dhclient --- pkg/hostman/hostinfo/hostbridge/hostbridge.go | 32 +++++++++++++++++++ pkg/hostman/hostinfo/hostinfohelper.go | 3 ++ 2 files changed, 35 insertions(+) diff --git a/pkg/hostman/hostinfo/hostbridge/hostbridge.go b/pkg/hostman/hostinfo/hostbridge/hostbridge.go index ee27f03a9f..d3671e8fcf 100644 --- a/pkg/hostman/hostinfo/hostbridge/hostbridge.go +++ b/pkg/hostman/hostinfo/hostbridge/hostbridge.go @@ -18,6 +18,8 @@ import ( "fmt" "net" "os" + "strconv" + "strings" "syscall" "yunion.io/x/jsonutils" @@ -48,6 +50,7 @@ type IBridgeDriver interface { SetupBridgeDev() error SetupInterface() error PersistentMac() error + DisableDHCPClient() error GenerateIfupScripts(scriptPath string, nic jsonutils.JSONObject) error GenerateIfdownScripts(scriptPath string, nic jsonutils.JSONObject) error @@ -327,6 +330,35 @@ func (d *SBaseBridgeDriver) WarmupConfig() error { return nil } +func (d *SBaseBridgeDriver) DisableDHCPClient() error { + if d.inter != nil { + filename := fmt.Sprintf("/var/run/dhclient-%s.pid", d.inter.String()) + if !fileutils2.Exists(filename) { + return nil + } + s, err := fileutils2.FileGetContents(filename) + if err != nil { + return errors.Wrap(err, "get dhclient pid") + } + pid, err := strconv.Atoi(strings.TrimSpace(s)) + if err != nil { + return errors.Wrap(err, "convert pid str to int") + } + if fileutils2.Exists(fmt.Sprintf("/proc/%d/cmdline", pid)) { + cmdline, err := fileutils2.FileGetContents(fmt.Sprintf("/proc/%d/cmdline", pid)) + if err != nil { + return errors.Wrap(err, "get proc cmdline") + } + if strings.Contains(cmdline, "dhclient") { + // kill process + p, _ := os.FindProcess(pid) + return p.Kill() + } + } + } + return nil +} + func NewDriver(bridgeDriver, bridge, inter, ip string) (IBridgeDriver, error) { if bridgeDriver == DRV_OPEN_VSWITCH { return NewOVSBridgeDriver(bridge, inter, ip) diff --git a/pkg/hostman/hostinfo/hostinfohelper.go b/pkg/hostman/hostinfo/hostinfohelper.go index b17d6072c8..a1767ab478 100644 --- a/pkg/hostman/hostinfo/hostinfohelper.go +++ b/pkg/hostman/hostinfo/hostinfohelper.go @@ -349,6 +349,9 @@ func NewNIC(desc string) (*SNIC, error) { if err := nic.BridgeDev.PersistentMac(); err != nil { return nil, err } + if err := nic.BridgeDev.DisableDHCPClient(); err != nil { + return nil, errors.Wrap(err, "disable dhcp client") + } var dhcpRelay []string if nic.EnableDHCPRelay() {