]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/netif-naming-scheme.c
Merge pull request #13748 from jwrdegoede/hwdb-updates2
[thirdparty/systemd.git] / src / shared / netif-naming-scheme.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "netif-naming-scheme.h"
5 #include "proc-cmdline.h"
6 #include "string-util.h"
7
8 static const NamingScheme naming_schemes[] = {
9 { "v238", NAMING_V238 },
10 { "v239", NAMING_V239 },
11 { "v240", NAMING_V240 },
12 { "v241", NAMING_V241 },
13 { "v243", NAMING_V243 },
14 /* … add more schemes here, as the logic to name devices is updated … */
15 };
16
17 static const NamingScheme* naming_scheme_from_name(const char *name) {
18 size_t i;
19
20 if (streq(name, "latest"))
21 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
22
23 for (i = 0; i < ELEMENTSOF(naming_schemes); i++)
24 if (streq(naming_schemes[i].name, name))
25 return naming_schemes + i;
26
27 return NULL;
28 }
29
30 const NamingScheme* naming_scheme(void) {
31 static const NamingScheme *cache = NULL;
32 _cleanup_free_ char *buffer = NULL;
33 const char *e, *k;
34
35 if (cache)
36 return cache;
37
38 /* Acquire setting from the kernel command line */
39 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer);
40
41 /* Also acquire it from an env var */
42 e = getenv("NET_NAMING_SCHEME");
43 if (e) {
44 if (*e == ':') {
45 /* If prefixed with ':' the kernel cmdline takes precedence */
46 k = buffer ?: e + 1;
47 } else
48 k = e; /* Otherwise the env var takes precedence */
49 } else
50 k = buffer;
51
52 if (k) {
53 cache = naming_scheme_from_name(k);
54 if (cache) {
55 log_info("Using interface naming scheme '%s'.", cache->name);
56 return cache;
57 }
58
59 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
60 }
61
62 cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME);
63 assert(cache);
64 log_info("Using default interface naming scheme '%s'.", cache->name);
65
66 return cache;
67 }