tools/hv: Improve the sample hv_get_dhcp_info script
Subsystems (2)
cover tools/hv: Improve the sample hv_get_dhcp_info script
The sample hv_get_dhcp_info script was originally supposed to be replaced by downstream distributions, but:
- Network Manager and systemd-networkd are used across many distributions
- Debian's ifupdown is not only used in Debian derivatives but also Alpine and Void Linux
This adds support for all of those.
The check for DHCP in network-scripts configuration files was also quite lax. This makes the regex a bit more strict.
Ben.
Ben Hutchings (2):
tools/hv: Make the sample hv_get_dhcp_info script more useful tools/hv: Make network-scripts DHCP status check more specific
tools/hv/hv_get_dhcp_info.sh | 87 +++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 12 deletions(-)
PATCH 1/2 tools/hv: Make the sample hv_get_dhcp_info script more useful
Currently the sample hv_get_dhcp_info script only supports the old Red Hat network-scripts configuration format and leaves everything else to downstream distributions.
However, Network Manager and systemd-networkd are available across many distributions, so it makes more sense to implement support for them here.
Debian's ifupdown is also used in several distributions that are not Debian derivatives, so I think it makes sense to implement support for that here too.
Extend the script to support all of these:
- Add a report function that reports the status based on the result of the previous command
- Add a function for each configuration system that checks whether that system in use for the given interface, and:
- If so, checks and reports the DHCP status
- If not, returns failure
- Call each of those functions, exiting once one of them succeeds, with a final fallback to reporting 'Disabled'
The network-scripts check is placed last, because it only checks a file and not the actual interface state and so is the least reliable check.
--- tools/hv/hv_get_dhcp_info.sh | 87 +++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 12 deletions(-)
tools/hv/hv_get_dhcp_info.sh +75 -12
@@ -12,18 +12,81 @@ # that DHCP is enabled on the interface. If DHCP is not enabled, # the script prints the string "Disabled" to stdout. # -# Each Distro is expected to implement this script in a distro specific -# fashion. For instance, on Distros that ship with Network Manager enabled, -# this script can be based on the Network Manager APIs for retrieving DHCP -# information. +# Distributions may need to adapt or replace this script for their +# preferred network configuration system. -if_file="/etc/sysconfig/network-scripts/ifcfg-"$1 +# Report status based on result of previous command +report() { + if [ $? -eq 0 ]; then + echo "Enabled" + else + echo "Disabled" + fi +} -dhcp=$(grep "dhcp" $if_file 2>/dev/null) +check_network_manager() { + local conn_name -if [ "$dhcp" != "" ]; -then -echo "Enabled" -else -echo "Disabled" -fi + # Check that the interface has a configured connection, and get + # its name + if conn_name="$(nmcli -g GENERAL.CONNECTION device show "$1" 2>/dev/null)" && + [ "$conn_name" ]; then + # Check whether the connection enables DHCPv4 + test "$(nmcli -g ipv4.method connection show "$conn_name")" = auto + report + else + return 1 + fi +} + +check_systemd_networkd() { + local status + + # Check that the interface is managed by networkd + if status="$(networkctl status --json=short -- "$1" 2>/dev/null)" && + ! printf '%s' "$status" | + grep -qE '"AdministrativeState":"unmanaged"'; then + # Check for DHCPv4 client state in the interface status + printf '%s' "$status" | grep -q '"DHCPv4Client":' + report + else + return 1 + fi +} + +check_ifupdown() { + local conf_name + + # Check that a configuration has been applied to the interface + if command -v ifquery >/dev/null && + conf_name="$(ifquery --state -- "$1" | sed 's/[^=]*=//')" && + [ "$conf_name" ]; then + # Check whether that configuration enables DHCPv4. + # Unfortunately ifquery does not expose the method name, so we + # have to grep through the configuration file(s) and make an + # assumption about which are included. + find /etc/network/interfaces /etc/network/interfaces.d \ + -type f -regex '.*/[a-zA-Z0-9_-]+$' -print | + xargs grep -qE '^\s*iface\s+'"$conf_name"'\s+inet\s+dhcp(\s|$)' + report + else + return 1 + fi +} + +check_network_scripts() { + local if_file="/etc/sysconfig/network-scripts/ifcfg-"$1 + + if [ -f "$if_file" ]; then + grep -q dhcp "$if_file" + report + else + return 1 + fi +} + +check_network_manager "$1" || +check_systemd_networkd "$1" || +check_ifupdown "$1" || +check_network_scripts "$1" || +report
PATCH 2/2 tools/hv: Make network-scripts DHCP status check more specific
In reply to Ben HutchingsWe currently look for the string 'dhcp' in a network-scripts configuration file, but this could potentially be found in a comment line. The variable that controls whether DHCPv4 is used is BOOTPROTO, so check for that variable name as well as the value 'dhcp'.
Also quote the interface name when constructing the configuration filename, just in case it contains a special character.
--- tools/hv/hv_get_dhcp_info.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/hv/hv_get_dhcp_info.sh +2 -2
@@ -75,10 +75,10 @@ check_ifupdown() { } check_network_scripts() { - local if_file="/etc/sysconfig/network-scripts/ifcfg-"$1 + local if_file="/etc/sysconfig/network-scripts/ifcfg-$1" if [ -f "$if_file" ]; then - grep -q dhcp "$if_file" + grep -q '^\s*BOOTPROTO=.*dhcp' "$if_file" report else return 1
Quoted 10 lines · Ben Hutchings
The sample
hv_get_dhcp_infoscript was originally supposed to be replaced by downstream distributions, but:- Network Manager and systemd-networkd are used across many distributions
- Debian's ifupdown is not only used in Debian derivatives but also Alpine and Void LinuxThis adds support for all of those.
Quoted 8 lines
The check for DHCP in network-scripts configuration files was also quite lax. This makes the regex a bit more strict.
Ben.
Ben Hutchings (2):
tools/hv: Make the samplehv_get_dhcp_infoscript more usefultools/hv: Make network-scripts DHCP status check more specific
Applied to hyperv-next. Thanks.