Skip to content

Commit

Permalink
Generate udev rules files to rename devices
Browse files Browse the repository at this point in the history
Due to a systemd issue[1], using link files to rename interfaces
doesn't work as expected. Link files will not rename an interface
if it was already renamed, and interfaces are renamed in initrd.

However, rules files will cause a renaming, even if the interface
has been renamed in initrd.

So, if we have a driver and/or a mac address and set-name, generate
a udev rules file in /run/udev/rules.d/70-netplan-<interface>.rules

This is at least a temporary fix to LP: #1770082

[1] systemd/systemd#9006

Signed-off-by: Daniel Axtens <dja@axtens.net>
  • Loading branch information
daxtens committed May 17, 2018
1 parent 7afef6a commit 1f4b043
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 50 deletions.
37 changes: 36 additions & 1 deletion src/networkd.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,40 @@ write_network_file(net_definition* def, const char* rootdir, const char* path)
g_string_free_to_file(s, rootdir, path, ".network");
}

static void
write_rules_file(net_definition* def, const char* rootdir)
{
GString* s = NULL;
g_autofree char* path = g_strjoin(NULL, "run/udev/rules.d/70-netplan-", def->id, ".rules", NULL);

/* do we need to write a .rules file?
* It's only required for reliably setting the name of a physical device
* until systemd issue #9006 is resolved. */
if (def->type >= ND_VIRTUAL)
return;

if (!def->set_name || (!def->match.mac && !def->match.driver))
return;

/* build file contents */
s = g_string_sized_new(200);

g_string_append(s, "SUBSYSTEM==\"net\", ACTION==\"add\", ");

if (def->match.driver) {
g_string_append_printf(s,"DRIVERS==\"%s\", ", def->match.driver);
} else {
g_string_append(s, "DRIVERS==\"?*\", ");
}

if (def->match.mac)
g_string_append_printf(s, "ATTR{address}==\"%s\", ", def->match.mac);

g_string_append_printf(s, "NAME=\"%s\"\n", def->set_name);

g_string_free_to_file(s, rootdir, path, NULL);
}

static void
write_wpa_conf(net_definition* def, const char* rootdir)
{
Expand Down Expand Up @@ -443,9 +477,10 @@ write_networkd_conf(net_definition* def, const char* rootdir)
{
g_autofree char* path_base = g_strjoin(NULL, "run/systemd/network/10-netplan-", def->id, NULL);

/* We want this for all backends when renaming, as *.link files are
/* We want this for all backends when renaming, as *.link and *.rules files are
* evaluated by udev, not networkd itself or NetworkManager. */
write_link_file(def, rootdir, path_base);
write_rules_file(def, rootdir);

if (def->backend != BACKEND_NETWORKD) {
g_debug("networkd: definition %s is not for us (backend %i)", def->id, def->backend);
Expand Down
Loading

0 comments on commit 1f4b043

Please sign in to comment.