* Provides net_add_parse_vars function to install net_parse_vars hook when an 
interface comes online
* Provides net_parse_vars function to search and replace network related 
variables in a string
---
 modules.d/40network/net-lib.sh | 56 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh
index f9b461b..caa3b79 100644
--- a/modules.d/40network/net-lib.sh
+++ b/modules.d/40network/net-lib.sh
@@ -366,3 +366,59 @@ linkup() {
      && wait_for_if_up $1 2>/dev/null
 }
 
+# net_parse_vars STRING IFUP
+# parse vars in STRING
+# IFUP may contain the interface that came online, triggering this hook
+net_parse_vars() {
+       local cur="$1" ifup="$2" var val iface
+
+       var="${cur#*%\{}"
+       var="${var%%\}*}"
+
+       while [ "${var}" != "${cur}" ] ; do
+               case "${var}" in
+                       mac.*)
+                               iface="${var##mac.}"
+                               [ "${ifup}" = "${iface}" ] || exit
+                               val="$(ip link show dev "${iface}" | sed -nre 
'/ether/s/^.*ether ([0-9a-f:]*) .*$/\1/' -e 's/://')"
+                               ;;
+                       hostname)
+                               val="$(hostname)"
+                               [ "${val}" = '(none)' ] && die 'No hostname 
(yet)'
+                               ;;
+                       *)
+                               die "Unsupported cmdline variable '${var}'"
+                               ;;
+               esac
+
+               cur="$(echo "${cur}" | sed "s/%{${var}}/${val}/")"
+               unset val
+
+               var="${cur#*%\{}"
+               var="${var%%\}*}"
+       done
+
+       echo "${cur}"
+}
+
+# net_add_parse_vars STRING LIB FUNCTION
+# add initqueue/online hook to parse vars in STRING
+# execute FUNCTION from LIB afterwards
+net_add_parse_vars() {
+       local string="$1" lib="$2" func="$3" jobid="$(cat 
/proc/sys/kernel/random/uuid)"
+
+       cat <<- EOF > 
"${hookdir}"/initqueue/online/40network.parse."${jobid}".sh
+               . /lib/"${lib}".sh
+
+               string="\$(net_parse_vars "${string}" "\$@")"
+               ${func} "\${string}"
+               unset string
+
+               > /tmp/40network.parse.finished."${jobid}"
+       EOF
+
+       cat <<- EOF > 
"${hookdir}"/initqueue/finished/40network.parse.wait."${jobid}".sh
+               echo "Waiting for nfs_parse_vars (${jobid}) to complete ..."
+               [ -f /tmp/40network.parse.finished."${jobid}" ] && rm -f 
/tmp/40network.parse.finished."${jobid}"
+       EOF
+}
-- 
1.8.1.5

--
To unsubscribe from this list: send the line "unsubscribe initramfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to