File: tools-hv-Make-the-sample-hv_get_dhcp_info-script-mor.patch

package info (click to toggle)
linux 6.17.2-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,734,084 kB
  • sloc: ansic: 26,674,600; asm: 271,176; sh: 147,222; python: 75,910; makefile: 57,291; perl: 36,942; xml: 19,562; cpp: 5,894; yacc: 4,909; lex: 2,943; awk: 1,556; sed: 28; ruby: 25
file content (136 lines) | stat: -rw-r--r-- 4,392 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
From: Ben Hutchings <benh@debian.org>
Date: Sun, 15 Jun 2025 23:35:52 +0200
Subject: tools/hv: Make the sample hv_get_dhcp_info script more useful
Origin: https://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux.git/commit?id=29c75ddc2d1952528a1195b6ac52054c47c0ee89
Bug-Debian: https://bugs.debian.org/919350

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