Skip to content

Commit

Permalink
disable dhclient
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed Jun 22, 2020
1 parent c44a8e0 commit 9a6c26d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/hostman/hostinfo/hostbridge/hostbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"net"
"os"
"strconv"
"strings"
"syscall"

"yunion.io/x/jsonutils"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pkg/hostman/hostinfo/hostinfohelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 9a6c26d

Please sign in to comment.