From 64310bf4dfc4a77a2cc32b2801cd734973232d99 Mon Sep 17 00:00:00 2001 From: wanyaoqi <18528551+wanyaoqi@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:40:46 +0800 Subject: [PATCH] fix(host-deployer): change netdev prefix to "en" on nics has different driver (#18330) Signed-off-by: wanyaoqi --- pkg/baremetal/agent.go | 2 +- pkg/hostman/guestfs/fsdriver/linux.go | 54 +++++++++++++++++----- pkg/hostman/guestfs/fsdriver/nicteaming.go | 7 +-- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/pkg/baremetal/agent.go b/pkg/baremetal/agent.go index 888d5ca807..ba078cad9b 100644 --- a/pkg/baremetal/agent.go +++ b/pkg/baremetal/agent.go @@ -58,7 +58,7 @@ func newBaremetalAgent() (*SBaremetalAgent, error) { return nil, err } // set guest fs NetDevPrefix - fsdriver.NetDevPrefix = "en" + fsdriver.NetDevPrefix = fsdriver.NetDevPrefixEN return agent, nil } diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index 63304f5c8d..1b7e1f7e45 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -55,9 +55,31 @@ const ( ) var ( - NetDevPrefix = "eth" + NetDevPrefix = "eth" + NetDevPrefixEN = "en" ) +func GetNetDevPrefix(nics []*types.SServerNic) string { + if NicsHasDifferentDriver(nics) { + return NetDevPrefixEN + } else { + return NetDevPrefix + } +} + +func NicsHasDifferentDriver(nics []*types.SServerNic) bool { + m := make(map[string]int) + for i := 0; i < len(nics); i++ { + if _, ok := m[nics[i].Driver]; !ok { + m[nics[i].Driver] = 1 + } + } + if len(m) > 1 { + return true + } + return false +} + type sLinuxRootFs struct { *sGuestRootFsDriver } @@ -357,6 +379,9 @@ func (l *sLinuxRootFs) DeployFstabScripts(rootFs IDiskPartition, disks []*deploy } func (l *sLinuxRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics []*types.SServerNic) error { + netDevPrefix := GetNetDevPrefix(nics) + log.Infof("netdev prefix: %s", netDevPrefix) + udevPath := "/etc/udev/rules.d/" if rootFs.Exists(udevPath, false) { rules := rootFs.ListDir(udevPath, false) @@ -376,7 +401,7 @@ func (l *sLinuxRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics []*ty mac := nic.Mac nicRules += fmt.Sprintf(`ATTR{address}=="%s", ATTR{type}=="1", `, strings.ToLower(mac)) idx := nic.Index - nicRules += fmt.Sprintf("NAME=\"%s%d\"\n", NetDevPrefix, idx) + nicRules += fmt.Sprintf("NAME=\"%s%d\"\n", netDevPrefix, idx) } if err := rootFs.FilePutContents(path.Join(udevPath, "70-persistent-net.rules"), nicRules, false, false); err != nil { return err @@ -431,6 +456,7 @@ func (l *sLinuxRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics []*ty } func (l *sLinuxRootFs) DeployStandbyNetworkingScripts(rootFs IDiskPartition, nics, nicsStandby []*types.SServerNic) error { + var netDevPrefix = GetNetDevPrefix(nicsStandby) var udevPath = "/etc/udev/rules.d/" var nicRules string for _, nic := range nicsStandby { @@ -440,7 +466,7 @@ func (l *sLinuxRootFs) DeployStandbyNetworkingScripts(rootFs IDiskPartition, nic mac := nic.Mac nicRules += fmt.Sprintf(`ATTR{address}=="%s", ATTR{type}=="1", `, strings.ToLower(mac)) idx := nic.Index - nicRules += fmt.Sprintf(`NAME="%s%d"\n`, NetDevPrefix, idx) + nicRules += fmt.Sprintf(`NAME="%s%d"\n`, netDevPrefix, idx) } } if err := rootFs.FilePutContents(path.Join(udevPath, "70-persistent-net.rules"), nicRules, true, false); err != nil { @@ -1228,13 +1254,14 @@ func (r *sRedhatLikeRootFs) DeployHostname(rootFs IDiskPartition, hn, domain str } func (r *sRedhatLikeRootFs) Centos5DeployNetworkingScripts(rootFs IDiskPartition, nics []*types.SServerNic) error { + var netDevPrefix = GetNetDevPrefix(nics) var udevPath = "/etc/udev/rules.d/" if rootFs.Exists(udevPath, false) { var nicRules = "" for _, nic := range nics { nicRules += `KERNEL=="*", ` nicRules += fmt.Sprintf(`SYSFS{address}=="%s", `, strings.ToLower(nic.Mac)) - nicRules += fmt.Sprintf("NAME=\"%s%d\"\n", NetDevPrefix, nic.Index) + nicRules += fmt.Sprintf("NAME=\"%s%d\"\n", netDevPrefix, nic.Index) } return rootFs.FilePutContents(path.Join(udevPath, "60-net.rules"), nicRules, false, false) @@ -1411,15 +1438,16 @@ func (r *sRedhatLikeRootFs) DeployStandbyNetworkingScripts(rootFs IDiskPartition if err := r.sLinuxRootFs.DeployStandbyNetworkingScripts(rootFs, nics, nicsStandby); err != nil { return err } + var netDevPrefix = GetNetDevPrefix(nics) for _, nic := range nicsStandby { var cmds string if len(nic.NicType) == 0 || nic.NicType != "ipmi" { - cmds += fmt.Sprintf("DEVICE=%s%d\n", NetDevPrefix, nic.Index) - cmds += fmt.Sprintf("NAME=%s%d\n", NetDevPrefix, nic.Index) + cmds += fmt.Sprintf("DEVICE=%s%d\n", netDevPrefix, nic.Index) + cmds += fmt.Sprintf("NAME=%s%d\n", netDevPrefix, nic.Index) cmds += fmt.Sprintf("HWADDR=%s\n", nic.Mac) cmds += fmt.Sprintf("MACADDR=%s\n", nic.Mac) cmds += "ONBOOT=no\n" - var fn = fmt.Sprintf("/etc/sysconfig/network-scripts/ifcfg-%s%d", NetDevPrefix, nic.Index) + var fn = fmt.Sprintf("/etc/sysconfig/network-scripts/ifcfg-%s%d", netDevPrefix, nic.Index) if err := rootFs.FilePutContents(fn, cmds, false, false); err != nil { return err } @@ -1714,18 +1742,19 @@ func (l *SGentooRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics []*t cmds = "" ) + var netDevPrefix = GetNetDevPrefix(nics) // Ref https://wiki.gentoo.org/wiki/Netifrc for _, nic := range nics { nicIndex := nic.Index if nic.Virtual { - cmds += fmt.Sprintf(`config_%s%d="`, NetDevPrefix, nicIndex) + cmds += fmt.Sprintf(`config_%s%d="`, netDevPrefix, nicIndex) cmds += fmt.Sprintf("%s netmask 255.255.255.255", netutils2.PSEUDO_VIP) cmds += `"\n` } else { - cmds += fmt.Sprintf(`config_%s%d="dhcp"\n`, NetDevPrefix, nicIndex) + cmds += fmt.Sprintf(`config_%s%d="dhcp"\n`, netDevPrefix, nicIndex) } if nic.Mtu > 0 { - cmds += fmt.Sprintf(`mtu_%s%d="%d"\n`, NetDevPrefix, nicIndex, nic.Mtu) + cmds += fmt.Sprintf(`mtu_%s%d="%d"\n`, netDevPrefix, nicIndex, nic.Mtu) } } if err := rootFs.FilePutContents(fn, cmds, false, false); err != nil { @@ -1733,7 +1762,7 @@ func (l *SGentooRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics []*t } for _, nic := range nics { nicIndex := nic.Index - netname := fmt.Sprintf("net.%s%d", NetDevPrefix, nicIndex) + netname := fmt.Sprintf("net.%s%d", netDevPrefix, nicIndex) procutils.NewCommand("ln", "-s", "net.lo", fmt.Sprintf("%s/etc/init.d/%s", rootFs.GetMountPath(), netname)).Run() procutils.NewCommand("chroot", @@ -1974,8 +2003,9 @@ func (d *SCoreOsRootFs) DeployHosts(rootFs IDiskPartition, hostname, domain stri } func (d *SCoreOsRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics []*types.SServerNic) error { + var netDevPrefix = GetNetDevPrefix(nics) for _, nic := range nics { - name := fmt.Sprintf("%s%d", NetDevPrefix, nic.Index) + name := fmt.Sprintf("%s%d", netDevPrefix, nic.Index) cont := "[Match]\n" cont += "Name=" + name + "\n" cont += "\n[Network]\n" diff --git a/pkg/hostman/guestfs/fsdriver/nicteaming.go b/pkg/hostman/guestfs/fsdriver/nicteaming.go index d2ba0683ff..69763116c3 100644 --- a/pkg/hostman/guestfs/fsdriver/nicteaming.go +++ b/pkg/hostman/guestfs/fsdriver/nicteaming.go @@ -81,6 +81,7 @@ func convertNicConfigs(nics []*types.SServerNic) ([]*types.SServerNic, []*types. allNics := make([]*types.SServerNic, 0) bondNics := make([]*types.SServerNic, 0) + var netDevPrefix = GetNetDevPrefix(nics) for i := range nics { // skip nics without mac if len(nics[i].Mac) == 0 { @@ -94,7 +95,7 @@ func convertNicConfigs(nics []*types.SServerNic) ([]*types.SServerNic, []*types. if teamNic == nil { // no teaming nic nnic := nics[i] - nnic.Name = fmt.Sprintf("%s%d", NetDevPrefix, nnic.Index) + nnic.Name = fmt.Sprintf("%s%d", netDevPrefix, nnic.Index) allNics = append(allNics, nnic) continue } @@ -102,11 +103,11 @@ func convertNicConfigs(nics []*types.SServerNic) ([]*types.SServerNic, []*types. master := nics[i] nnic := *nics[i] tnic := *teamNic - nnic.Name = fmt.Sprintf("%s%d", NetDevPrefix, nnic.Index) + nnic.Name = fmt.Sprintf("%s%d", netDevPrefix, nnic.Index) nnic.TeamingMaster = master nnic.Ip = "" nnic.Gateway = "" - tnic.Name = fmt.Sprintf("%s%d", NetDevPrefix, tnic.Index) + tnic.Name = fmt.Sprintf("%s%d", netDevPrefix, tnic.Index) tnic.TeamingMaster = master tnic.Ip = "" tnic.Gateway = ""