]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/kmod-setup.c
core: align table
[thirdparty/systemd.git] / src / core / kmod-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "bus-util.h"
7 #include "capability-util.h"
8 #include "efi-api.h"
9 #include "fileio.h"
10 #include "kmod-setup.h"
11 #include "macro.h"
12 #include "recurse-dir.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "virt.h"
16
17 #if HAVE_KMOD
18 #include "module-util.h"
19
20 static void systemd_kmod_log(
21 void *data,
22 int priority,
23 const char *file, int line,
24 const char *fn,
25 const char *format,
26 va_list args) {
27
28 /* library logging is enabled at debug only */
29 DISABLE_WARNING_FORMAT_NONLITERAL;
30 log_internalv(LOG_DEBUG, 0, file, line, fn, format, args);
31 REENABLE_WARNING;
32 }
33
34 static int match_modalias_recurse_dir_cb(
35 RecurseDirEvent event,
36 const char *path,
37 int dir_fd,
38 int inode_fd,
39 const struct dirent *de,
40 const struct statx *sx,
41 void *userdata) {
42
43 _cleanup_free_ char *alias = NULL;
44 char **modaliases = ASSERT_PTR(userdata);
45 int r;
46
47 if (event != RECURSE_DIR_ENTRY)
48 return RECURSE_DIR_CONTINUE;
49
50 if (de->d_type != DT_REG)
51 return RECURSE_DIR_CONTINUE;
52
53 if (!streq(de->d_name, "modalias"))
54 return RECURSE_DIR_CONTINUE;
55
56 r = read_one_line_file(path, &alias);
57 if (r < 0) {
58 log_debug_errno(r, "Failed to read %s, ignoring: %m", path);
59 return RECURSE_DIR_LEAVE_DIRECTORY;
60 }
61
62 if (startswith_strv(alias, modaliases))
63 return 1;
64
65 return RECURSE_DIR_LEAVE_DIRECTORY;
66 }
67
68 static bool has_virtio_feature(const char *name, char **modaliases) {
69 int r;
70
71 /* Directory traversal might be slow, hence let's do a cheap check first if it's even worth it */
72 if (detect_vm() == VIRTUALIZATION_NONE)
73 return false;
74
75 r = recurse_dir_at(
76 AT_FDCWD,
77 "/sys/devices/pci0000:00",
78 /* statx_mask= */ 0,
79 /* n_depth_max= */ 3,
80 RECURSE_DIR_ENSURE_TYPE,
81 match_modalias_recurse_dir_cb,
82 modaliases);
83 if (r < 0)
84 log_debug_errno(r, "Failed to determine whether host has %s device, ignoring: %m", name);
85
86 return r > 0;
87 }
88
89 static bool has_virtio_rng(void) {
90 return has_virtio_feature("virtio-rng", STRV_MAKE("pci:v00001AF4d00001005", "pci:v00001AF4d00001044"));
91 }
92
93 static bool has_virtio_console(void) {
94 return has_virtio_feature("virtio-console", STRV_MAKE("virtio:d00000003v", "virtio:d0000000Bv"));
95 }
96
97 static bool has_virtio_vsock(void) {
98 return has_virtio_feature("virtio-vsock", STRV_MAKE("virtio:d00000013v"));
99 }
100
101 static bool has_virtiofs(void) {
102 return has_virtio_feature("virtiofs", STRV_MAKE("virtio:d0000001Av"));
103 }
104
105 static bool has_virtio_pci(void) {
106 return has_virtio_feature("virtio-pci", STRV_MAKE("pci:v00001AF4d"));
107 }
108
109 static bool in_qemu(void) {
110 return IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_QEMU);
111 }
112 #endif
113
114 int kmod_setup(void) {
115 #if HAVE_KMOD
116 static const struct {
117 const char *module;
118 const char *path;
119 bool warn_if_unavailable;
120 bool warn_if_module;
121 bool (*condition_fn)(void);
122 } kmod_table[] = {
123 /* This one we need to load explicitly, since auto-loading on use doesn't work
124 * before udev created the ghost device nodes, and we need it earlier than that. */
125 { "autofs4", "/sys/class/misc/autofs", true, false, NULL },
126
127 /* This one we need to load explicitly, since auto-loading of IPv6 is not done when
128 * we try to configure ::1 on the loopback device. */
129 { "ipv6", "/sys/module/ipv6", false, true, NULL },
130
131 /* This should never be a module */
132 { "unix", "/proc/net/unix", true, true, NULL },
133
134 #if HAVE_LIBIPTC
135 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
136 { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL },
137 #endif
138 /* virtio_rng would be loaded by udev later, but real entropy might be needed very early */
139 { "virtio_rng", NULL, false, false, has_virtio_rng },
140
141 /* we want early logging to hvc consoles if possible, and make sure systemd-getty-generator
142 * can rely on all consoles being probed already.*/
143 { "virtio_console", NULL, false, false, has_virtio_console },
144
145 /* Make sure we can send sd-notify messages over vsock as early as possible. */
146 { "vmw_vsock_virtio_transport", NULL, false, false, has_virtio_vsock },
147
148 /* We can't wait for specific virtiofs tags to show up as device nodes so we have to load the
149 * virtiofs and virtio_pci modules early to make sure the virtiofs tags are found when
150 * sysroot.mount is started.
151 *
152 * TODO: Remove these again once https://gitlab.com/virtio-fs/virtiofsd/-/issues/128 is
153 * resolved and the kernel fix is widely available. */
154 { "virtiofs", "/sys/module/virtiofs", false, false, has_virtiofs },
155 { "virtio_pci", "/sys/module/virtio_pci", false, false, has_virtio_pci },
156
157 /* qemu_fw_cfg would be loaded by udev later, but we want to import credentials from it super early */
158 { "qemu_fw_cfg", "/sys/firmware/qemu_fw_cfg", false, false, in_qemu },
159
160 /* dmi-sysfs is needed to import credentials from it super early */
161 { "dmi-sysfs", "/sys/firmware/dmi/entries", false, false, NULL },
162
163 #if HAVE_TPM2
164 /* Make sure the tpm subsystem is available which ConditionSecurity=tpm2 depends on. */
165 { "tpm", "/sys/class/tpmrm", false, false, efi_has_tpm2 },
166 #endif
167 };
168
169 if (have_effective_cap(CAP_SYS_MODULE) <= 0)
170 return 0;
171
172 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
173
174 FOREACH_ARRAY(kmod, kmod_table, ELEMENTSOF(kmod_table)) {
175 if (kmod->path && access(kmod->path, F_OK) >= 0)
176 continue;
177
178 if (kmod->condition_fn && !kmod->condition_fn())
179 continue;
180
181 if (kmod->warn_if_module)
182 log_debug("Your kernel apparently lacks built-in %s support. Might be "
183 "a good idea to compile it in. We'll now try to work around "
184 "this by loading the module...", kmod->module);
185
186 if (!ctx) {
187 ctx = kmod_new(NULL, NULL);
188 if (!ctx)
189 return log_oom();
190
191 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
192 kmod_load_resources(ctx);
193 }
194
195 (void) module_load_and_warn(ctx, kmod->module, kmod->warn_if_unavailable);
196 }
197
198 #endif
199 return 0;
200 }