@@ -1033,7 +1033,7 @@ func (d *sDebianLikeRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics
10331033 cmds .WriteString (fmt .Sprintf (" up ip -6 route add %s/%d via %s || true\n " , r .Prefix , r .PrefixLen , r .Gateway ))
10341034 cmds .WriteString (fmt .Sprintf (" down ip -6 route del %s/%d via %s || true\n " , r .Prefix , r .PrefixLen , r .Gateway ))
10351035 }
1036- dnslist := netutils2 .GetNicDns (nicDesc )
1036+ dnslist , _ := netutils2 .GetNicDns (nicDesc )
10371037 if len (dnslist ) > 0 {
10381038 cmds .WriteString (fmt .Sprintf (" dns-nameservers %s\n " , strings .Join (dnslist , " " )))
10391039 dnss = append (dnss , dnslist ... )
@@ -1044,15 +1044,6 @@ func (d *sDebianLikeRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics
10441044 if nicDesc .Mtu > 0 {
10451045 cmds .WriteString (fmt .Sprintf (" mtu %d\n " , nicDesc .Mtu ))
10461046 }
1047- dnslist := netutils2 .GetNicDns (nicDesc )
1048- if len (dnslist ) > 0 {
1049- cmds .WriteString (fmt .Sprintf (" dns-nameservers %s\n " , strings .Join (dnslist , " " )))
1050- dnss = append (dnss , dnslist ... )
1051- if len (nicDesc .Domain ) > 0 {
1052- cmds .WriteString (fmt .Sprintf (" dns-search %s\n " , nicDesc .Domain ))
1053- domains = append (domains , nicDesc .Domain )
1054- }
1055- }
10561047 if len (nicDesc .TeamingSlaves ) > 0 {
10571048 cmds .WriteString (getNicTeamingConfigCmds (nicDesc .TeamingSlaves ))
10581049 }
@@ -1066,6 +1057,15 @@ func (d *sDebianLikeRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics
10661057 if len (nicDesc .Gateway6 ) > 0 && nicDesc .Ip == mainIp {
10671058 cmds .WriteString (fmt .Sprintf (" gateway %s\n " , nicDesc .Gateway6 ))
10681059 }
1060+ _ , dnslist := netutils2 .GetNicDns (nicDesc )
1061+ if len (dnslist ) > 0 {
1062+ cmds .WriteString (fmt .Sprintf (" dns-nameservers %s\n " , strings .Join (dnslist , " " )))
1063+ dnss = append (dnss , dnslist ... )
1064+ if len (nicDesc .Domain ) > 0 {
1065+ cmds .WriteString (fmt .Sprintf (" dns-search %s\n " , nicDesc .Domain ))
1066+ domains = append (domains , nicDesc .Domain )
1067+ }
1068+ }
10691069 cmds .WriteString ("\n " )
10701070 }
10711071 } else {
@@ -1327,8 +1327,24 @@ func (r *sRedhatLikeRootFs) PrepareFsForTemplate(rootFs IDiskPartition) error {
13271327 return r .CleanNetworkScripts (rootFs )
13281328}
13291329
1330+ func (r * sRedhatLikeRootFs ) cleanNetworkManagerConfigurations (rootFs IDiskPartition ) error {
1331+ networkPath := "/etc/NetworkManager/system-connections"
1332+ if ! rootFs .Exists (networkPath , false ) {
1333+ return nil
1334+ }
1335+ files := rootFs .ListDir (networkPath , false )
1336+ for _ , f := range files {
1337+ rootFs .Remove (filepath .Join (networkPath , f ), false )
1338+ }
1339+ return nil
1340+ }
1341+
13301342func (r * sRedhatLikeRootFs ) CleanNetworkScripts (rootFs IDiskPartition ) error {
13311343 networkPath := "/etc/sysconfig/network-scripts"
1344+ if ! rootFs .Exists (networkPath , false ) {
1345+ return r .cleanNetworkManagerConfigurations (rootFs )
1346+ }
1347+
13321348 files := rootFs .ListDir (networkPath , false )
13331349 for i := 0 ; i < len (files ); i ++ {
13341350 if strings .HasPrefix (files [i ], "ifcfg-" ) && files [i ] != "ifcfg-lo" {
@@ -1344,16 +1360,18 @@ func (r *sRedhatLikeRootFs) CleanNetworkScripts(rootFs IDiskPartition) error {
13441360
13451361func (r * sRedhatLikeRootFs ) RootSignatures () []string {
13461362 sig := r .sLinuxRootFs .RootSignatures ()
1347- return append ([]string {"/etc/sysconfig/network" , "/etc/ redhat-release" }, sig ... )
1363+ return append ([]string {"/etc/redhat-release" }, sig ... )
13481364}
13491365
13501366func (r * sRedhatLikeRootFs ) DeployHostname (rootFs IDiskPartition , hn , domain string ) error {
13511367 var sPath = "/etc/sysconfig/network"
1352- centosHn := ""
1353- centosHn += "NETWORKING=yes\n "
1354- centosHn += fmt .Sprintf ("HOSTNAME=%s\n " , getHostname (hn , domain ))
1355- if err := rootFs .FilePutContents (sPath , centosHn , false , false ); err != nil {
1356- return errors .Wrapf (err , "DeployHostname %s" , sPath )
1368+ if r .rootFs .Exists (sPath , false ) {
1369+ centosHn := ""
1370+ centosHn += "NETWORKING=yes\n "
1371+ centosHn += fmt .Sprintf ("HOSTNAME=%s\n " , getHostname (hn , domain ))
1372+ if err := rootFs .FilePutContents (sPath , centosHn , false , false ); err != nil {
1373+ return errors .Wrapf (err , "DeployHostname %s" , sPath )
1374+ }
13571375 }
13581376 if err := rootFs .FilePutContents ("/etc/hostname" , hn , false , false ); err != nil {
13591377 return errors .Wrapf (err , "DeployHostname %s" , "/etc/hostname" )
@@ -1422,17 +1440,47 @@ func (r *sRedhatLikeRootFs) isNetworkManagerEnabled(rootFs IDiskPartition) bool
14221440 return rootFs .Exists ("/etc/systemd/system/multi-user.target.wants/NetworkManager.service" , false )
14231441}
14241442
1425- func (r * sRedhatLikeRootFs ) deployNetworkingScripts (rootFs IDiskPartition , nics []* types.SServerNic , relInfo * deployapi.ReleaseInfo ) error {
1426- // remove all ifcfg-*
1427- const scriptPath = "/etc/sysconfig/network-scripts"
1443+ func (r * sRedhatLikeRootFs ) deployNetworkManagerConfigurations (rootFs IDiskPartition , nics []* types.SServerNic , relInfo * deployapi.ReleaseInfo ) error {
1444+ const scriptPath = "/etc/NetworkManager/system-connections"
1445+ if ! rootFs .Exists (scriptPath , false ) {
1446+ return errors .Wrap (errors .ErrNotSupported , "unsupported system, neither network-scripts nor NetworkManager" )
1447+ }
1448+
1449+ // remove all connections profiles
14281450 files := rootFs .ListDir (scriptPath , false )
14291451 for _ , f := range files {
1430- if strings .HasPrefix (f , "ifcfg-" ) && f != "ifcfg-lo" {
1431- log .Infof ("remove %s in %s" , f , scriptPath )
1432- rootFs .Remove (filepath .Join (scriptPath , f ), false )
1452+ log .Infof ("remove %s in %s" , f , scriptPath )
1453+ rootFs .Remove (filepath .Join (scriptPath , f ), false )
1454+ }
1455+
1456+ allNics , bondNics := convertNicConfigs (nics )
1457+ if len (bondNics ) > 0 {
1458+ err := r .enableBondingModule (rootFs , bondNics )
1459+ if err != nil {
1460+ return errors .Wrap (err , "enableBondingModule" )
14331461 }
14341462 }
1463+ // nicCnt := len(allNics) - len(bondNics)
14351464
1465+ mainNic := getMainNic (allNics )
1466+ var mainIp , mainIp6 string
1467+ if mainNic != nil {
1468+ mainIp = mainNic .Ip
1469+ mainIp6 = mainNic .Ip6
1470+ }
1471+ for i := range allNics {
1472+ nicDesc := allNics [i ]
1473+ profile := nicDescToNetworkManager (nicDesc , mainIp , mainIp6 )
1474+ var fn = fmt .Sprintf ("%s/%s.nmconnection" , scriptPath , nicDesc .Name )
1475+ if err := rootFs .FilePutContents (fn , profile , false , false ); err != nil {
1476+ return err
1477+ }
1478+ }
1479+
1480+ return nil
1481+ }
1482+
1483+ func (r * sRedhatLikeRootFs ) deployNetworkingScripts (rootFs IDiskPartition , nics []* types.SServerNic , relInfo * deployapi.ReleaseInfo ) error {
14361484 ver := strings .Split (relInfo .Version , "." )
14371485 iv , err := strconv .ParseInt (ver [0 ], 10 , 0 )
14381486 if err == nil && iv < 6 {
@@ -1443,6 +1491,21 @@ func (r *sRedhatLikeRootFs) deployNetworkingScripts(rootFs IDiskPartition, nics
14431491 if err != nil {
14441492 return errors .Wrap (err , "DeployNetworkingScripts" )
14451493 }
1494+
1495+ const scriptPath = "/etc/sysconfig/network-scripts"
1496+ if ! rootFs .Exists (scriptPath , false ) {
1497+ // NetworkManager is enabled, but no network-scripts directory, deploy NetworkManager configurations
1498+ return r .deployNetworkManagerConfigurations (rootFs , nics , relInfo )
1499+ }
1500+
1501+ // remove all ifcfg-*
1502+ files := rootFs .ListDir (scriptPath , false )
1503+ for _ , f := range files {
1504+ if strings .HasPrefix (f , "ifcfg-" ) && f != "ifcfg-lo" {
1505+ log .Infof ("remove %s in %s" , f , scriptPath )
1506+ rootFs .Remove (filepath .Join (scriptPath , f ), false )
1507+ }
1508+ }
14461509 // ToServerNics(nics)
14471510 allNics , bondNics := convertNicConfigs (nics )
14481511 if len (bondNics ) > 0 {
@@ -1495,7 +1558,7 @@ func (r *sRedhatLikeRootFs) deployNetworkingScripts(rootFs IDiskPartition, nics
14951558 cmds .WriteString ("\n " )
14961559 }
14971560 if len (nicDesc .TeamingSlaves ) > 0 {
1498- // bonding
1561+ // bonding master
14991562 cmds .WriteString (`BONDING_OPTS="mode=4 miimon=100"` )
15001563 cmds .WriteString ("\n " )
15011564 }
@@ -1532,15 +1595,6 @@ func (r *sRedhatLikeRootFs) deployNetworkingScripts(rootFs IDiskPartition, nics
15321595 cmds .WriteString ("\n " )
15331596 }
15341597 }
1535- if len (nicDesc .Ip6 ) > 0 {
1536- cmds .WriteString ("IPV6INIT=yes\n " )
1537- cmds .WriteString ("DHCPV6C=no\n " )
1538- cmds .WriteString ("IPV6_AUTOCONF=no\n " )
1539- cmds .WriteString (fmt .Sprintf ("IPV6ADDR=%s/%d\n " , nicDesc .Ip6 , nicDesc .Masklen6 ))
1540- if len (nicDesc .Gateway6 ) > 0 && nicDesc .Ip6 == mainIp6 {
1541- cmds .WriteString (fmt .Sprintf ("IPV6_DEFAULTGW=%s\n " , nicDesc .Gateway6 ))
1542- }
1543- }
15441598 routes4 := make ([]netutils2.SRouteInfo , 0 )
15451599 routes6 := make ([]netutils2.SRouteInfo , 0 )
15461600 routes4 , routes6 = netutils2 .AddNicRoutes (routes4 , routes6 , nicDesc , mainIp , mainIp6 , nicCnt )
@@ -1567,15 +1621,30 @@ func (r *sRedhatLikeRootFs) deployNetworkingScripts(rootFs IDiskPartition, nics
15671621 if err := rootFs .FilePutContents (fn , rtblStr , false , false ); err != nil {
15681622 return err
15691623 }
1570- dnslist := netutils2 .GetNicDns (nicDesc )
1571- if len (dnslist ) > 0 {
1572- cmds .WriteString ("PEERDNS=yes\n " )
1573- for i := 0 ; i < len (dnslist ); i ++ {
1574- cmds .WriteString (fmt .Sprintf ("DNS%d=%s\n " , i + 1 , dnslist [i ]))
1575- }
1576- if len (nicDesc .Domain ) > 0 {
1577- cmds .WriteString (fmt .Sprintf ("DOMAIN=%s\n " , nicDesc .Domain ))
1578- }
1624+ }
1625+ dns4list , dns6list := netutils2 .GetNicDns (nicDesc )
1626+ if len (dns4list )+ len (dns6list ) > 0 {
1627+ cmds .WriteString ("PEERDNS=yes\n " )
1628+ dnsIdx := 1
1629+ for i := 0 ; i < len (dns4list ); i ++ {
1630+ cmds .WriteString (fmt .Sprintf ("DNS%d=%s\n " , dnsIdx , dns4list [i ]))
1631+ dnsIdx += 1
1632+ }
1633+ for i := 0 ; i < len (dns6list ); i ++ {
1634+ cmds .WriteString (fmt .Sprintf ("DNS%d=%s\n " , dnsIdx , dns6list [i ]))
1635+ dnsIdx += 1
1636+ }
1637+ if len (nicDesc .Domain ) > 0 {
1638+ cmds .WriteString (fmt .Sprintf ("DOMAIN=%s\n " , nicDesc .Domain ))
1639+ }
1640+ }
1641+ if len (nicDesc .Ip6 ) > 0 {
1642+ cmds .WriteString ("IPV6INIT=yes\n " )
1643+ cmds .WriteString ("DHCPV6C=no\n " )
1644+ cmds .WriteString ("IPV6_AUTOCONF=no\n " )
1645+ cmds .WriteString (fmt .Sprintf ("IPV6ADDR=%s/%d\n " , nicDesc .Ip6 , nicDesc .Masklen6 ))
1646+ if len (nicDesc .Gateway6 ) > 0 && nicDesc .Ip6 == mainIp6 {
1647+ cmds .WriteString (fmt .Sprintf ("IPV6_DEFAULTGW=%s\n " , nicDesc .Gateway6 ))
15791648 }
15801649 }
15811650 }
@@ -1667,11 +1736,17 @@ func (r *sRedhatLikeRootFs) deployVlanNetworkingScripts(rootFs IDiskPartition, s
16671736 return err
16681737 }
16691738 }
1670- dnslist := netutils2 .GetNicDns (nicDesc )
1671- if len (dnslist ) > 0 {
1739+ dns4list , dns6list := netutils2 .GetNicDns (nicDesc )
1740+ if len (dns4list ) + len ( dns6list ) > 0 {
16721741 cmds .WriteString ("PEERDNS=yes\n " )
1673- for i := 0 ; i < len (dnslist ); i ++ {
1674- cmds .WriteString (fmt .Sprintf ("DNS%d=%s\n " , i + 1 , dnslist [i ]))
1742+ dnsIdx := 1
1743+ for i := 0 ; i < len (dns4list ); i ++ {
1744+ cmds .WriteString (fmt .Sprintf ("DNS%d=%s\n " , dnsIdx , dns4list [i ]))
1745+ dnsIdx += 1
1746+ }
1747+ for i := 0 ; i < len (dns6list ); i ++ {
1748+ cmds .WriteString (fmt .Sprintf ("DNS%d=%s\n " , dnsIdx , dns6list [i ]))
1749+ dnsIdx += 1
16751750 }
16761751 if len (nicDesc .Domain ) > 0 {
16771752 cmds .WriteString (fmt .Sprintf ("DOMAIN=%s\n " , nicDesc .Domain ))
0 commit comments