]>
Commit | Line | Data |
---|---|---|
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" |
6c1f72f6 | 8 | #include "fileio.h" |
11c3a4ee | 9 | #include "kmod-setup.h" |
cf0fbc49 | 10 | #include "macro.h" |
79a72b1b | 11 | #include "recurse-dir.h" |
6c1f72f6 | 12 | #include "string-util.h" |
5c1d67af | 13 | #include "virt.h" |
11c3a4ee | 14 | |
349cc4a5 | 15 | #if HAVE_KMOD |
8a8a4b6e ZJS |
16 | #include "module-util.h" |
17 | ||
b4b87964 LP |
18 | static void systemd_kmod_log( |
19 | void *data, | |
20 | int priority, | |
21 | const char *file, int line, | |
22 | const char *fn, | |
23 | const char *format, | |
24 | va_list args) { | |
25 | ||
10223732 | 26 | /* library logging is enabled at debug only */ |
bcfce235 | 27 | DISABLE_WARNING_FORMAT_NONLITERAL; |
79008bdd | 28 | log_internalv(LOG_DEBUG, 0, file, line, fn, format, args); |
bcfce235 | 29 | REENABLE_WARNING; |
728beb28 | 30 | } |
b4b87964 | 31 | |
79a72b1b LP |
32 | static int has_virtio_rng_recurse_dir_cb( |
33 | RecurseDirEvent event, | |
34 | const char *path, | |
35 | int dir_fd, | |
36 | int inode_fd, | |
37 | const struct dirent *de, | |
38 | const struct statx *sx, | |
39 | void *userdata) { | |
6c1f72f6 HH |
40 | |
41 | _cleanup_free_ char *alias = NULL; | |
42 | int r; | |
43 | ||
79a72b1b LP |
44 | if (event != RECURSE_DIR_ENTRY) |
45 | return RECURSE_DIR_CONTINUE; | |
6c1f72f6 | 46 | |
79a72b1b LP |
47 | if (de->d_type != DT_REG) |
48 | return RECURSE_DIR_CONTINUE; | |
6c1f72f6 | 49 | |
79a72b1b LP |
50 | if (!streq(de->d_name, "modalias")) |
51 | return RECURSE_DIR_CONTINUE; | |
6c1f72f6 | 52 | |
79a72b1b LP |
53 | r = read_one_line_file(path, &alias); |
54 | if (r < 0) { | |
55 | log_debug_errno(r, "Failed to read %s, ignoring: %m", path); | |
56 | return RECURSE_DIR_LEAVE_DIRECTORY; | |
57 | } | |
6c1f72f6 HH |
58 | |
59 | if (startswith(alias, "pci:v00001AF4d00001005")) | |
79a72b1b | 60 | return 1; |
6c1f72f6 HH |
61 | |
62 | if (startswith(alias, "pci:v00001AF4d00001044")) | |
79a72b1b | 63 | return 1; |
6c1f72f6 | 64 | |
79a72b1b | 65 | return RECURSE_DIR_LEAVE_DIRECTORY; |
6c1f72f6 HH |
66 | } |
67 | ||
68 | static bool has_virtio_rng(void) { | |
79a72b1b LP |
69 | int r; |
70 | ||
71 | r = recurse_dir_at( | |
72 | AT_FDCWD, | |
73 | "/sys/devices/pci0000:00", | |
74 | /* statx_mask= */ 0, | |
75 | /* n_depth_max= */ 2, | |
76 | RECURSE_DIR_ENSURE_TYPE, | |
77 | has_virtio_rng_recurse_dir_cb, | |
78 | NULL); | |
79 | if (r < 0) | |
80 | log_debug_errno(r, "Failed to determine whether host has virtio-rng device, ignoring: %m"); | |
81 | ||
82 | return r > 0; | |
6c1f72f6 | 83 | } |
5c1d67af LP |
84 | |
85 | static bool in_qemu(void) { | |
86 | return IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_QEMU); | |
87 | } | |
95441cf2 | 88 | #endif |
6c1f72f6 | 89 | |
7491e6e7 | 90 | int kmod_setup(void) { |
349cc4a5 | 91 | #if HAVE_KMOD |
c47fc1f0 | 92 | |
7491e6e7 KS |
93 | static const struct { |
94 | const char *module; | |
95 | const char *path; | |
85c67553 DM |
96 | bool warn_if_unavailable:1; |
97 | bool warn_if_module:1; | |
7491e6e7 KS |
98 | bool (*condition_fn)(void); |
99 | } kmod_table[] = { | |
6755bb55 ZJS |
100 | /* This one we need to load explicitly, since auto-loading on use doesn't work |
101 | * before udev created the ghost device nodes, and we need it earlier than that. */ | |
85c67553 | 102 | { "autofs4", "/sys/class/misc/autofs", true, false, NULL }, |
7491e6e7 | 103 | |
6755bb55 ZJS |
104 | /* This one we need to load explicitly, since auto-loading of IPv6 is not done when |
105 | * we try to configure ::1 on the loopback device. */ | |
85c67553 | 106 | { "ipv6", "/sys/module/ipv6", false, true, NULL }, |
7491e6e7 | 107 | |
6755bb55 | 108 | /* This should never be a module */ |
85c67553 | 109 | { "unix", "/proc/net/unix", true, true, NULL }, |
7491e6e7 | 110 | |
349cc4a5 | 111 | #if HAVE_LIBIPTC |
1d308797 | 112 | /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */ |
85c67553 | 113 | { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL }, |
a363680f | 114 | #endif |
6c1f72f6 HH |
115 | /* virtio_rng would be loaded by udev later, but real entropy might be needed very early */ |
116 | { "virtio_rng", NULL, false, false, has_virtio_rng }, | |
5c1d67af LP |
117 | |
118 | /* qemu_fw_cfg would be loaded by udev later, but we want to import credentials from it super early */ | |
119 | { "qemu_fw_cfg", "/sys/firmware/qemu_fw_cfg", false, false, in_qemu }, | |
7491e6e7 | 120 | }; |
232ac0d6 | 121 | _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL; |
14cb109d | 122 | unsigned i; |
11c3a4ee | 123 | |
c47fc1f0 LP |
124 | if (have_effective_cap(CAP_SYS_MODULE) == 0) |
125 | return 0; | |
126 | ||
7491e6e7 | 127 | for (i = 0; i < ELEMENTSOF(kmod_table); i++) { |
b43b8f7a | 128 | if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0) |
7491e6e7 KS |
129 | continue; |
130 | ||
b43b8f7a | 131 | if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn()) |
11c3a4ee LP |
132 | continue; |
133 | ||
85c67553 | 134 | if (kmod_table[i].warn_if_module) |
7491e6e7 KS |
135 | log_debug("Your kernel apparently lacks built-in %s support. Might be " |
136 | "a good idea to compile it in. We'll now try to work around " | |
137 | "this by loading the module...", kmod_table[i].module); | |
11c3a4ee | 138 | |
728beb28 TG |
139 | if (!ctx) { |
140 | ctx = kmod_new(NULL, NULL); | |
b4b87964 LP |
141 | if (!ctx) |
142 | return log_oom(); | |
11c3a4ee | 143 | |
728beb28 | 144 | kmod_set_log_fn(ctx, systemd_kmod_log, NULL); |
728beb28 TG |
145 | kmod_load_resources(ctx); |
146 | } | |
11c3a4ee | 147 | |
81d7c696 | 148 | (void) module_load_and_warn(ctx, kmod_table[i].module, kmod_table[i].warn_if_unavailable); |
11c3a4ee LP |
149 | } |
150 | ||
f84f9974 | 151 | #endif |
728beb28 | 152 | return 0; |
11c3a4ee | 153 | } |