]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/kmod-setup.c
kmod-setup: Add early loading for virtio_console
[thirdparty/systemd.git] / src / core / kmod-setup.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
11c3a4ee 2
cf0fbc49 3#include <unistd.h>
f84f9974 4
6c1f72f6 5#include "alloc-util.h"
d79acc30 6#include "bus-util.h"
cf0fbc49 7#include "capability-util.h"
0ae03a0f 8#include "efi-api.h"
6c1f72f6 9#include "fileio.h"
11c3a4ee 10#include "kmod-setup.h"
cf0fbc49 11#include "macro.h"
79a72b1b 12#include "recurse-dir.h"
6c1f72f6 13#include "string-util.h"
fa505db3 14#include "strv.h"
5c1d67af 15#include "virt.h"
11c3a4ee 16
349cc4a5 17#if HAVE_KMOD
8a8a4b6e
ZJS
18#include "module-util.h"
19
b4b87964
LP
20static 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
10223732 28 /* library logging is enabled at debug only */
bcfce235 29 DISABLE_WARNING_FORMAT_NONLITERAL;
79008bdd 30 log_internalv(LOG_DEBUG, 0, file, line, fn, format, args);
bcfce235 31 REENABLE_WARNING;
728beb28 32}
b4b87964 33
e1d8f702 34static int match_modalias_recurse_dir_cb(
79a72b1b
LP
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) {
6c1f72f6
HH
42
43 _cleanup_free_ char *alias = NULL;
e1d8f702 44 char **modaliases = ASSERT_PTR(userdata);
6c1f72f6
HH
45 int r;
46
79a72b1b
LP
47 if (event != RECURSE_DIR_ENTRY)
48 return RECURSE_DIR_CONTINUE;
6c1f72f6 49
79a72b1b
LP
50 if (de->d_type != DT_REG)
51 return RECURSE_DIR_CONTINUE;
6c1f72f6 52
79a72b1b
LP
53 if (!streq(de->d_name, "modalias"))
54 return RECURSE_DIR_CONTINUE;
6c1f72f6 55
79a72b1b
LP
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 }
6c1f72f6 61
e1d8f702 62 if (startswith_strv(alias, modaliases))
79a72b1b 63 return 1;
6c1f72f6 64
79a72b1b 65 return RECURSE_DIR_LEAVE_DIRECTORY;
6c1f72f6
HH
66}
67
68static bool has_virtio_rng(void) {
79a72b1b
LP
69 int r;
70
0a5d3c0b
LP
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
79a72b1b
LP
75 r = recurse_dir_at(
76 AT_FDCWD,
77 "/sys/devices/pci0000:00",
78 /* statx_mask= */ 0,
79 /* n_depth_max= */ 2,
80 RECURSE_DIR_ENSURE_TYPE,
e1d8f702
DDM
81 match_modalias_recurse_dir_cb,
82 STRV_MAKE("pci:v00001AF4d00001005", "pci:v00001AF4d00001044"));
79a72b1b
LP
83 if (r < 0)
84 log_debug_errno(r, "Failed to determine whether host has virtio-rng device, ignoring: %m");
85
86 return r > 0;
6c1f72f6 87}
5c1d67af 88
a93aaede
DDM
89static bool has_virtio_console(void) {
90 int r;
91
92 /* Directory traversal might be slow, hence let's do a cheap check first if it's even worth it */
93 if (detect_vm() == VIRTUALIZATION_NONE)
94 return false;
95
96 r = recurse_dir_at(
97 AT_FDCWD,
98 "/sys/devices/pci0000:00",
99 /* statx_mask= */ 0,
100 /* n_depth_max= */ 3,
101 RECURSE_DIR_ENSURE_TYPE,
102 match_modalias_recurse_dir_cb,
103 STRV_MAKE("virtio:d00000003v", "virtio:d0000000Bv"));
104 if (r < 0)
105 log_debug_errno(r, "Failed to determine whether host has virtio-console device, ignoring: %m");
106
107 return r > 0;
108}
109
5c1d67af
LP
110static bool in_qemu(void) {
111 return IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_QEMU);
112}
95441cf2 113#endif
6c1f72f6 114
7491e6e7 115int kmod_setup(void) {
349cc4a5 116#if HAVE_KMOD
c47fc1f0 117
7491e6e7
KS
118 static const struct {
119 const char *module;
120 const char *path;
85c67553
DM
121 bool warn_if_unavailable:1;
122 bool warn_if_module:1;
7491e6e7
KS
123 bool (*condition_fn)(void);
124 } kmod_table[] = {
6755bb55
ZJS
125 /* This one we need to load explicitly, since auto-loading on use doesn't work
126 * before udev created the ghost device nodes, and we need it earlier than that. */
a93aaede 127 { "autofs4", "/sys/class/misc/autofs", true, false, NULL },
7491e6e7 128
6755bb55
ZJS
129 /* This one we need to load explicitly, since auto-loading of IPv6 is not done when
130 * we try to configure ::1 on the loopback device. */
a93aaede 131 { "ipv6", "/sys/module/ipv6", false, true, NULL },
7491e6e7 132
6755bb55 133 /* This should never be a module */
a93aaede 134 { "unix", "/proc/net/unix", true, true, NULL },
7491e6e7 135
349cc4a5 136#if HAVE_LIBIPTC
1d308797 137 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
a93aaede 138 { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL },
a363680f 139#endif
6c1f72f6 140 /* virtio_rng would be loaded by udev later, but real entropy might be needed very early */
a93aaede
DDM
141 { "virtio_rng", NULL, false, false, has_virtio_rng },
142
143 /* we want early logging to hvc consoles if possible, and make sure systemd-getty-generator
144 * can rely on all consoles being probed already.*/
145 { "virtio_console", NULL, false, false, has_virtio_console },
5c1d67af
LP
146
147 /* qemu_fw_cfg would be loaded by udev later, but we want to import credentials from it super early */
a93aaede 148 { "qemu_fw_cfg", "/sys/firmware/qemu_fw_cfg", false, false, in_qemu },
8313a1a5
LN
149
150 /* dmi-sysfs is needed to import credentials from it super early */
a93aaede 151 { "dmi-sysfs", "/sys/firmware/dmi/entries", false, false, NULL },
0ae03a0f
DDM
152
153#if HAVE_TPM2
154 /* Make sure the tpm subsystem is available which ConditionSecurity=tpm2 depends on. */
a93aaede 155 { "tpm", "/sys/class/tpmrm", false, false, efi_has_tpm2 },
0ae03a0f 156#endif
7491e6e7 157 };
232ac0d6 158 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
14cb109d 159 unsigned i;
11c3a4ee 160
26c45a6c 161 if (have_effective_cap(CAP_SYS_MODULE) <= 0)
c47fc1f0
LP
162 return 0;
163
7491e6e7 164 for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
b43b8f7a 165 if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
7491e6e7
KS
166 continue;
167
b43b8f7a 168 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
11c3a4ee
LP
169 continue;
170
85c67553 171 if (kmod_table[i].warn_if_module)
7491e6e7
KS
172 log_debug("Your kernel apparently lacks built-in %s support. Might be "
173 "a good idea to compile it in. We'll now try to work around "
174 "this by loading the module...", kmod_table[i].module);
11c3a4ee 175
728beb28
TG
176 if (!ctx) {
177 ctx = kmod_new(NULL, NULL);
b4b87964
LP
178 if (!ctx)
179 return log_oom();
11c3a4ee 180
728beb28 181 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
728beb28
TG
182 kmod_load_resources(ctx);
183 }
11c3a4ee 184
81d7c696 185 (void) module_load_and_warn(ctx, kmod_table[i].module, kmod_table[i].warn_if_unavailable);
11c3a4ee
LP
186 }
187
f84f9974 188#endif
728beb28 189 return 0;
11c3a4ee 190}