]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-scripts: Add function ip_prefix_iface()
authorMartin Schwenke <mschwenke@ddn.com>
Wed, 8 Apr 2026 23:40:55 +0000 (09:40 +1000)
committerMartin Schwenke <martins@samba.org>
Thu, 16 Apr 2026 23:09:33 +0000 (23:09 +0000)
Reimplement ip_maskbits_iface() using the ip -brief option.  Do less
parsing, no longer extract maskbits but return whole prefix.

Retain ip_maskbits_iface() for backward compatibility in case custom
event scripts are using it.

Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: John Mulligan <jmulligan@redhat.com>
ctdb/config/events/legacy/10.interface.script
ctdb/config/functions

index a23bac4bb0792c28f854c5d93c7297d8bf1fafdd..67bc6cd4fd1873cdac15615193f6e3acfa95da26 100755 (executable)
@@ -64,14 +64,14 @@ get_iface_ip_maskbits()
 
        # Intentional word splitting here
        # shellcheck disable=SC2046
-       set -- $(ip_maskbits_iface "$ip")
+       set -- $(ip_prefix_iface "$ip")
        if [ -z "$1" ]; then
                echo "WARNING: Unable to determine interface for IP ${ip}"
                iface=""
                return
        fi
 
-       maskbits="$1"
+       maskbits="${1#*/}"
        iface="$2"
 
        if [ "$iface" != "$_iface_in" ]; then
index 4e7265fed206d3419e7726a44082c5ce6caabf3b..c0035815c7370fa0a6b633377459ff6f0b0b375a 100755 (executable)
@@ -738,16 +738,20 @@ delete_ip_from_iface()
        ip_addr_del "${_ip}/${_maskbits}" "$_iface"
 }
 
+# If the given IP is hosted then print 2 items: prefix and iface
+ip_prefix_iface()
+{
+       _addr="$1"
+
+       ip -brief addr show to "$_addr" 2>/dev/null | awk '{ print $3, $1 }'
+}
+
 # If the given IP is hosted then print 2 items: maskbits and iface
 ip_maskbits_iface()
 {
        _addr="$1"
 
-       ip addr show to "$_addr" 2>/dev/null |
-               awk 'NR == 1 { iface = $2; sub(":$", "", iface) ;
-                      sub("@.*", "", iface) }
-             $1 ~ /inet/ { mask = $2; sub(".*/", "", mask);
-                           print mask, iface }'
+       ip_prefix_iface "$_addr" | sed -e 's|[^/]*/||'
 }
 
 drop_ip()
@@ -756,12 +760,11 @@ drop_ip()
 
        # Intentional word splitting here
        # shellcheck disable=SC2046
-       set -- $(ip_maskbits_iface "$_addr")
+       set -- $(ip_prefix_iface "$_addr")
        if [ -n "$1" ]; then
-               _maskbits="$1"
+               _prefix="$1"
                _iface="$2"
 
-               _prefix="${_addr}/${_maskbits}"
                echo "Removing public address ${_prefix} from device ${_iface}"
                ip_addr_del "$_prefix" "$_iface" >/dev/null 2>&1
        fi