Skip to contentSkip to conversation

Add variable parsing framework for network related variables

PATCH In review Dennis Schridde 1 participant 3 messages Apr 11, 2013 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add variable parsing framework for network related variables

* 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(+)
modules.d/40network/net-lib.sh +56 -0
index f9b461b..caa3b79 100644
@@ -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
+}
Signature · 1 line
1.8.1.5

A message that is not in the archive (sent off-list or not yet received): 1365694853-7881-1-git-send-email-devurandom-hi6Y0CQ0nG0@public.gmane.org

#2 lore raw

PATCH 2/2 Provide a hostname fallback function, in case there is no executable of this name

---
 modules.d/40network/net-lib.sh | 5 +++++
 1 file changed, 5 insertions(+)
modules.d/40network/net-lib.sh +5 -0
index caa3b79..baeace4 100644
@@ -422,3 +422,8 @@ net_add_parse_vars() {
 		[ -f /tmp/40network.parse.finished."${jobid}" ] && rm -f /tmp/40network.parse.finished."${jobid}"
 	EOF
 }
+
+type hostname >/dev/null 2>&1 || \
+hostname() {
+	cat /proc/sys/kernel/hostname
+}
Signature · 1 line
1.8.1.5

A message that is not in the archive (sent off-list or not yet received): 1365694853-7881-2-git-send-email-devurandom-hi6Y0CQ0nG0@public.gmane.org

#4 lore raw

Re: [PATCH 2/2] [40network] Provide a hostname fallback function, in case there is no executable of this name

Hello!

I will submit further patches that require these, once these are in a state where they will be accepted.

Please note that you can also have a look at these patches on github:
https://github.com/devurandom/dracut

--Dennis