From 55cdbccad3f6492b1e721154a45e5884044db76d Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Thu, 2 Jul 2020 13:56:10 +0200 Subject: rename_device: also support dracut-style kernel cmdline configuration There are 2 cases where this might be useful. * On machines with initrd build without dracut-network, you can now still use ifname option * On older kernels where double renaming was allowed, normal system replaced the name set by initrd. --- src/rename_device.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/rename_device.c b/src/rename_device.c index c39f447d..b67a536b 100644 --- a/src/rename_device.c +++ b/src/rename_device.c @@ -278,6 +278,46 @@ char *get_config_by_hwaddr(char *hwaddr, char *current) { return first; } +/* Let's check kernel cmdline and also process ifname= entries + * as they are documented in dracut.cmdline(7) + * Example: ifname=test:aa:bb:cc:dd:ee:ff + */ +char *get_cmdline_by_hwaddr(char *hwaddr) { + gchar *contents; + gchar **entries; + int i; + char *name = NULL; + g_file_get_contents("/proc/cmdline", &contents, NULL, NULL); + entries = g_strsplit(contents," ", 0); + for (i = 0; entries[i]; i++) { + char *c = (char *) entries[i]; + char *n; + + if (!g_str_has_prefix((gchar *)c,"ifname=")) + continue; + + c = strstr(c, "="); + if (c == NULL) + continue; + c++; + n=c; + + c = strstr(c, ":"); + if (c == NULL) + continue; + *c = '\0'; + c++; + + if (!strcasecmp(c, hwaddr)) { + name = strdup(n); + break; + } + } + g_free(contents); + g_strfreev(entries); + return name; +} + void take_lock() { int count = 0; int lockfd; @@ -343,7 +383,9 @@ int main(int argc, char **argv) { hw = get_hwaddr(src); if (!hw) goto out_unlock; - target = get_config_by_hwaddr(hw, src); + target = get_cmdline_by_hwaddr(hw); + if (!target) + target = get_config_by_hwaddr(hw, src); if (!target) goto out_unlock; -- cgit v1.2.1