* [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script
@ 2025-06-15 23:04 Ben Hutchings
2025-06-15 23:05 ` [PATCH 1/2] tools/hv: Make the sample hv_get_dhcp_info script more useful Ben Hutchings
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Ben Hutchings @ 2025-06-15 23:04 UTC (permalink / raw)
To: linux-hyperv
[-- Attachment #1: Type: text/plain, Size: 714 bytes --]
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(-)
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 1/2] tools/hv: Make the sample hv_get_dhcp_info script more useful 2025-06-15 23:04 [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script Ben Hutchings @ 2025-06-15 23:05 ` Ben Hutchings 2025-06-15 23:06 ` [PATCH 2/2] tools/hv: Make network-scripts DHCP status check more specific Ben Hutchings 2025-07-09 22:44 ` [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script Wei Liu 2 siblings, 0 replies; 4+ messages in thread From: Ben Hutchings @ 2025-06-15 23:05 UTC (permalink / raw) To: linux-hyperv [-- Attachment #1: Type: text/plain, Size: 4209 bytes --] 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. Signed-off-by: Ben Hutchings <benh@debian.org> --- tools/hv/hv_get_dhcp_info.sh | 87 +++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/tools/hv/hv_get_dhcp_info.sh b/tools/hv/hv_get_dhcp_info.sh index 2f2a3c7df3de..310b16a2f734 100755 --- a/tools/hv/hv_get_dhcp_info.sh +++ b/tools/hv/hv_get_dhcp_info.sh @@ -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 [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 833 bytes --] ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] tools/hv: Make network-scripts DHCP status check more specific 2025-06-15 23:04 [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script Ben Hutchings 2025-06-15 23:05 ` [PATCH 1/2] tools/hv: Make the sample hv_get_dhcp_info script more useful Ben Hutchings @ 2025-06-15 23:06 ` Ben Hutchings 2025-07-09 22:44 ` [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script Wei Liu 2 siblings, 0 replies; 4+ messages in thread From: Ben Hutchings @ 2025-06-15 23:06 UTC (permalink / raw) To: linux-hyperv [-- Attachment #1: Type: text/plain, Size: 1066 bytes --] We 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. Signed-off-by: Ben Hutchings <benh@debian.org> --- tools/hv/hv_get_dhcp_info.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/hv/hv_get_dhcp_info.sh b/tools/hv/hv_get_dhcp_info.sh index 310b16a2f734..7815d55a9295 100755 --- a/tools/hv/hv_get_dhcp_info.sh +++ b/tools/hv/hv_get_dhcp_info.sh @@ -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 [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 833 bytes --] ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script 2025-06-15 23:04 [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script Ben Hutchings 2025-06-15 23:05 ` [PATCH 1/2] tools/hv: Make the sample hv_get_dhcp_info script more useful Ben Hutchings 2025-06-15 23:06 ` [PATCH 2/2] tools/hv: Make network-scripts DHCP status check more specific Ben Hutchings @ 2025-07-09 22:44 ` Wei Liu 2 siblings, 0 replies; 4+ messages in thread From: Wei Liu @ 2025-07-09 22:44 UTC (permalink / raw) To: Ben Hutchings; +Cc: linux-hyperv, Wei Liu On Mon, Jun 16, 2025 at 01:04:43AM +0200, Ben Hutchings wrote: > 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 Applied to hyperv-next. Thanks. ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-07-09 22:44 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-06-15 23:04 [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script Ben Hutchings 2025-06-15 23:05 ` [PATCH 1/2] tools/hv: Make the sample hv_get_dhcp_info script more useful Ben Hutchings 2025-06-15 23:06 ` [PATCH 2/2] tools/hv: Make network-scripts DHCP status check more specific Ben Hutchings 2025-07-09 22:44 ` [PATCH 0/2] tools/hv: Improve the sample hv_get_dhcp_info script Wei Liu
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox