-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate DHClient in favor of systemd-networkd (#342)
* Deprecate DHClient in favor of systemd-networkd This is part of the ongoing work to deprecate DHClient. The main priority currently is utilizing systemd-networkd, since Ubuntu will be removing dhclient after their upcoming freeze at the end of January. The other network management services, such as wicked and NetworkManager will come later. Currently, this will do the following: + If dhclient is found on the system, we continue using dhclient for network interface configuration. + If not, then we use systemd-networkd. * Fix inconsistent routes on systemd-networkd
- Loading branch information
Showing
10 changed files
with
1,498 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package manager is responsible for detecting the current network manager service, and | ||
// writing and rolling back appropriate configurations for each network manager service. | ||
package manager | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/GoogleCloudPlatform/guest-agent/metadata" | ||
"github.com/GoogleCloudPlatform/guest-logging-go/logger" | ||
) | ||
|
||
var ( | ||
badMAC = make(map[string]net.Interface) | ||
) | ||
|
||
// interfaceNames extracts the names of the network interfaces from the provided list | ||
// of network interfaces. | ||
func interfaceNames(nics []metadata.NetworkInterfaces) ([]string, error) { | ||
var ifaces []string | ||
for _, ni := range nics { | ||
iface, err := GetInterfaceByMAC(ni.Mac) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ifaces = append(ifaces, iface.Name) | ||
} | ||
return ifaces, nil | ||
} | ||
|
||
// interfaceListsIpv4Ipv6 gets a list of interface names. The first list is a list of all | ||
// interfaces, and the second list consists of only interfaces that support IPv6. | ||
func interfaceListsIpv4Ipv6(nics []metadata.NetworkInterfaces) ([]string, []string) { | ||
var googleInterfaces []string | ||
var googleIpv6Interfaces []string | ||
|
||
for _, ni := range nics { | ||
iface, err := GetInterfaceByMAC(ni.Mac) | ||
if err != nil { | ||
if _, found := badMAC[ni.Mac]; !found { | ||
logger.Errorf("error getting interface: %s", err) | ||
badMAC[ni.Mac] = iface | ||
} | ||
continue | ||
} | ||
if ni.DHCPv6Refresh != "" { | ||
googleIpv6Interfaces = append(googleIpv6Interfaces, iface.Name) | ||
} | ||
googleInterfaces = append(googleInterfaces, iface.Name) | ||
} | ||
return googleInterfaces, googleIpv6Interfaces | ||
} | ||
|
||
// GetInterfaceByMAC gets the interface given the mac string. | ||
func GetInterfaceByMAC(mac string) (net.Interface, error) { | ||
hwaddr, err := net.ParseMAC(mac) | ||
if err != nil { | ||
return net.Interface{}, err | ||
} | ||
|
||
interfaces, err := net.Interfaces() | ||
if err != nil { | ||
return net.Interface{}, fmt.Errorf("failed to get interfaces: %v", err) | ||
} | ||
|
||
for _, iface := range interfaces { | ||
if iface.HardwareAddr.String() == hwaddr.String() { | ||
return iface, nil | ||
} | ||
} | ||
return net.Interface{}, fmt.Errorf("no interface found with MAC %s", mac) | ||
} |
Oops, something went wrong.