| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "capability-util.h" |
| 7 |
#include "efi-api.h" |
| 8 |
#include "fileio.h" |
| 9 |
#include "kmod-setup.h" |
| 10 |
#include "log.h" |
| 11 |
#include "module-util.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 |
static int match_modalias_recurse_dir_cb( |
| 19 |
RecurseDirEvent event, |
| 20 |
const char *path, |
| 21 |
int dir_fd, |
| 22 |
int inode_fd, |
| 23 |
const struct dirent *de, |
| 24 |
const struct statx *sx, |
| 25 |
void *userdata) { |
| 26 |
|
| 27 |
_cleanup_free_ char *alias = NULL; |
| 28 |
char **modaliases = ASSERT_PTR(userdata); |
| 29 |
int r; |
| 30 |
|
| 31 |
if (event != RECURSE_DIR_ENTRY) |
| 32 |
return RECURSE_DIR_CONTINUE; |
| 33 |
|
| 34 |
if (de->d_type != DT_REG) |
| 35 |
return RECURSE_DIR_CONTINUE; |
| 36 |
|
| 37 |
if (!streq(de->d_name, "modalias")) |
| 38 |
return RECURSE_DIR_CONTINUE; |
| 39 |
|
| 40 |
r = read_one_line_file(path, &alias); |
| 41 |
if (r < 0) { |
| 42 |
log_debug_errno(r, "Failed to read %s, ignoring: %m", path); |
| 43 |
return RECURSE_DIR_LEAVE_DIRECTORY; |
| 44 |
} |
| 45 |
|
| 46 |
if (startswith_strv(alias, modaliases)) |
| 47 |
return 1; |
| 48 |
|
| 49 |
return RECURSE_DIR_LEAVE_DIRECTORY; |
| 50 |
} |
| 51 |
|
| 52 |
static bool has_virtio_feature(const char *name, char **modaliases) { |
| 53 |
int r; |
| 54 |
|
| 55 |
/* Directory traversal might be slow, hence let's do a cheap check first if it's even worth it */ |
| 56 |
if (detect_vm() == VIRTUALIZATION_NONE) |
| 57 |
return false; |
| 58 |
|
| 59 |
r = recurse_dir_at( |
| 60 |
AT_FDCWD, |
| 61 |
"/sys/devices/pci0000:00", |
| 62 |
/* statx_mask= */ 0, |
| 63 |
/* n_depth_max= */ 3, |
| 64 |
RECURSE_DIR_ENSURE_TYPE, |
| 65 |
match_modalias_recurse_dir_cb, |
| 66 |
modaliases); |
| 67 |
if (r < 0) |
| 68 |
log_debug_errno(r, "Failed to determine whether host has %s device, ignoring: %m", name); |
| 69 |
|
| 70 |
return r > 0; |
| 71 |
} |
| 72 |
|
| 73 |
static bool has_virtio_rng(void) { |
| 74 |
return has_virtio_feature("virtio-rng", STRV_MAKE("pci:v00001AF4d00001005", "pci:v00001AF4d00001044")); |
| 75 |
} |
| 76 |
|
| 77 |
static bool has_virtio_pci(void) { |
| 78 |
return has_virtio_feature("virtio-pci", STRV_MAKE("pci:v00001AF4d")); |
| 79 |
} |
| 80 |
|
| 81 |
static bool may_have_virtio(void) { |
| 82 |
/* FIXME: strictly speaking, other virtio features, e.g. vsock, are independent of the virtio PCI device. */ |
| 83 |
return has_virtio_pci(); |
| 84 |
} |
| 85 |
|
| 86 |
static bool in_qemu(void) { |
| 87 |
return IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_QEMU); |
| 88 |
} |
| 89 |
|
| 90 |
static bool in_vmware(void) { |
| 91 |
return detect_vm() == VIRTUALIZATION_VMWARE; |
| 92 |
} |
| 93 |
|
| 94 |
static bool in_hyperv(void) { |
| 95 |
return detect_vm() == VIRTUALIZATION_MICROSOFT; |
| 96 |
} |
| 97 |
|
| 98 |
static bool may_have_vsock_loopback(void) { |
| 99 |
return may_have_virtio() || in_vmware(); |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
int kmod_setup(void) { |
| 104 |
#if HAVE_KMOD |
| 105 |
static const struct { |
| 106 |
const char *module; |
| 107 |
const char *path; |
| 108 |
bool warn_if_unavailable; |
| 109 |
bool warn_if_module; |
| 110 |
bool (*condition_fn)(void); |
| 111 |
} kmod_table[] = { |
| 112 |
/* This one we need to load explicitly, since auto-loading on use doesn't work |
| 113 |
* before udev created the ghost device nodes, and we need it earlier than that. */ |
| 114 |
{ "autofs4", "/sys/class/misc/autofs", true, false, NULL }, |
| 115 |
|
| 116 |
/* This one we need to load explicitly, since auto-loading of IPv6 is not done when |
| 117 |
* we try to configure ::1 on the loopback device. */ |
| 118 |
{ "ipv6", "/sys/module/ipv6", false, true, NULL }, |
| 119 |
|
| 120 |
/* virtio_rng would be loaded by udev later, but real entropy might be needed very early */ |
| 121 |
{ "virtio_rng", NULL, false, false, has_virtio_rng }, |
| 122 |
|
| 123 |
/* we want early logging to hvc consoles if possible, and make sure systemd-getty-generator |
| 124 |
* can rely on all consoles being probed already. */ |
| 125 |
{ "virtio_console", NULL, false, false, may_have_virtio }, |
| 126 |
|
| 127 |
/* Make sure we can send sd-notify messages over vsock as early as possible. */ |
| 128 |
{ "vmw_vsock_virtio_transport", NULL, false, false, may_have_virtio }, |
| 129 |
/* vsock_loopback provides VMADDR_CID_LOCAL and is not a hard dep of any transport module */ |
| 130 |
{ "vsock_loopback", "/sys/module/vsock_loopback", false, false, may_have_vsock_loopback }, |
| 131 |
{ "vmw_vsock_vmci_transport", NULL, false, false, in_vmware }, |
| 132 |
{ "hv_sock", NULL, false, false, in_hyperv }, |
| 133 |
|
| 134 |
/* We can't wait for specific virtiofs tags to show up as device nodes so we have to load the |
| 135 |
* virtiofs and virtio_pci modules early to make sure the virtiofs tags are found when |
| 136 |
* sysroot.mount is started. |
| 137 |
* |
| 138 |
* TODO: Remove these again once https://gitlab.com/virtio-fs/virtiofsd/-/issues/128 is |
| 139 |
* resolved and the kernel fix is widely available. */ |
| 140 |
{ "virtiofs", "/sys/module/virtiofs", false, false, may_have_virtio }, |
| 141 |
{ "virtio_pci", "/sys/module/virtio_pci", false, false, has_virtio_pci }, |
| 142 |
|
| 143 |
/* qemu_fw_cfg would be loaded by udev later, but we want to import credentials from it super early */ |
| 144 |
{ "qemu_fw_cfg", "/sys/firmware/qemu_fw_cfg", false, false, in_qemu }, |
| 145 |
|
| 146 |
/* dmi-sysfs is needed to import credentials from it super early */ |
| 147 |
{ "dmi-sysfs", "/sys/firmware/dmi/entries", false, false, NULL }, |
| 148 |
|
| 149 |
#if HAVE_TPM2 |
| 150 |
/* Make sure the tpm subsystem is available which ConditionSecurity=tpm2 depends on. */ |
| 151 |
{ "tpm", "/sys/class/tpmrm", false, false, efi_has_tpm2 }, |
| 152 |
#endif |
| 153 |
}; |
| 154 |
|
| 155 |
int r; |
| 156 |
|
| 157 |
if (have_effective_cap(CAP_SYS_MODULE) <= 0) |
| 158 |
return 0; |
| 159 |
|
| 160 |
_cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL; |
| 161 |
FOREACH_ELEMENT(kmod, kmod_table) { |
| 162 |
if (kmod->path && access(kmod->path, F_OK) >= 0) |
| 163 |
continue; |
| 164 |
|
| 165 |
if (kmod->condition_fn && !kmod->condition_fn()) |
| 166 |
continue; |
| 167 |
|
| 168 |
if (kmod->warn_if_module) |
| 169 |
log_debug("Your kernel apparently lacks built-in %s support. Might be " |
| 170 |
"a good idea to compile it in. We'll now try to work around " |
| 171 |
"this by loading the module...", kmod->module); |
| 172 |
|
| 173 |
if (!ctx) { |
| 174 |
r = module_setup_context(&ctx); |
| 175 |
if (r < 0) |
| 176 |
return log_error_errno(r, "Failed to initialize kmod context: %m"); |
| 177 |
} |
| 178 |
|
| 179 |
(void) module_load_and_warn(ctx, kmod->module, kmod->warn_if_unavailable); |
| 180 |
} |
| 181 |
|
| 182 |
#endif |
| 183 |
return 0; |
| 184 |
} |