1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
9 #include "capability-util.h"
11 #include "kmod-setup.h"
13 #include "string-util.h"
17 #include "module-util.h"
19 static void systemd_kmod_log(
22 const char *file
, int line
,
27 /* library logging is enabled at debug only */
28 DISABLE_WARNING_FORMAT_NONLITERAL
;
29 log_internalv(LOG_DEBUG
, 0, file
, line
, fn
, format
, args
);
33 static int has_virtio_rng_nftw_cb(
35 const struct stat
*sb
,
39 _cleanup_free_
char *alias
= NULL
;
42 if ((FTW_D
== tflag
) && (ftwbuf
->level
> 2))
43 return FTW_SKIP_SUBTREE
;
48 if (!endswith(fpath
, "/modalias"))
51 r
= read_one_line_file(fpath
, &alias
);
53 return FTW_SKIP_SIBLINGS
;
55 if (startswith(alias
, "pci:v00001AF4d00001005"))
58 if (startswith(alias
, "pci:v00001AF4d00001044"))
61 return FTW_SKIP_SIBLINGS
;
64 static bool has_virtio_rng(void) {
65 return (nftw("/sys/devices/pci0000:00", has_virtio_rng_nftw_cb
, 64, FTW_MOUNT
|FTW_PHYS
|FTW_ACTIONRETVAL
) == FTW_STOP
);
69 int kmod_setup(void) {
75 bool warn_if_unavailable
:1;
76 bool warn_if_module
:1;
77 bool (*condition_fn
)(void);
79 /* This one we need to load explicitly, since auto-loading on use doesn't work
80 * before udev created the ghost device nodes, and we need it earlier than that. */
81 { "autofs4", "/sys/class/misc/autofs", true, false, NULL
},
83 /* This one we need to load explicitly, since auto-loading of IPv6 is not done when
84 * we try to configure ::1 on the loopback device. */
85 { "ipv6", "/sys/module/ipv6", false, true, NULL
},
87 /* This should never be a module */
88 { "unix", "/proc/net/unix", true, true, NULL
},
91 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
92 { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL
},
94 /* virtio_rng would be loaded by udev later, but real entropy might be needed very early */
95 { "virtio_rng", NULL
, false, false, has_virtio_rng
},
97 _cleanup_(kmod_unrefp
) struct kmod_ctx
*ctx
= NULL
;
100 if (have_effective_cap(CAP_SYS_MODULE
) == 0)
103 for (i
= 0; i
< ELEMENTSOF(kmod_table
); i
++) {
104 if (kmod_table
[i
].path
&& access(kmod_table
[i
].path
, F_OK
) >= 0)
107 if (kmod_table
[i
].condition_fn
&& !kmod_table
[i
].condition_fn())
110 if (kmod_table
[i
].warn_if_module
)
111 log_debug("Your kernel apparently lacks built-in %s support. Might be "
112 "a good idea to compile it in. We'll now try to work around "
113 "this by loading the module...", kmod_table
[i
].module
);
116 ctx
= kmod_new(NULL
, NULL
);
120 kmod_set_log_fn(ctx
, systemd_kmod_log
, NULL
);
121 kmod_load_resources(ctx
);
124 (void) module_load_and_warn(ctx
, kmod_table
[i
].module
, kmod_table
[i
].warn_if_unavailable
);