]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
kmod-setup: Load virtiofs and virtio_pci early
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 28 Sep 2023 07:50:36 +0000 (09:50 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Thu, 28 Sep 2023 14:21:50 +0000 (15:21 +0100)
There's no way for us to wait for specific virtiofs tags to appear,
so we have to try and make sure that the tags are all available by
the time we try to mount any virtiofs tag. Let's try to do that by
loading the necessary modules as early as we can.

src/core/kmod-setup.c

index a99309f5c223de30ff11ca6907163ddfc75732b5..b8e3f7aadd5f11f7b004b56c025bef2417b4b4f5 100644 (file)
@@ -65,7 +65,7 @@ static int match_modalias_recurse_dir_cb(
         return RECURSE_DIR_LEAVE_DIRECTORY;
 }
 
-static bool has_virtio_rng(void) {
+static bool has_virtio_feature(const char *name, char **modaliases) {
         int r;
 
         /* Directory traversal might be slow, hence let's do a cheap check first if it's even worth it */
@@ -76,56 +76,34 @@ static bool has_virtio_rng(void) {
                         AT_FDCWD,
                         "/sys/devices/pci0000:00",
                         /* statx_mask= */ 0,
-                        /* n_depth_max= */ 2,
+                        /* n_depth_max= */ 3,
                         RECURSE_DIR_ENSURE_TYPE,
                         match_modalias_recurse_dir_cb,
-                        STRV_MAKE("pci:v00001AF4d00001005", "pci:v00001AF4d00001044"));
+                        modaliases);
         if (r < 0)
-                log_debug_errno(r, "Failed to determine whether host has virtio-rng device, ignoring: %m");
+                log_debug_errno(r, "Failed to determine whether host has %s device, ignoring: %m", name);
 
         return r > 0;
 }
 
-static bool has_virtio_console(void) {
-        int r;
-
-        /* Directory traversal might be slow, hence let's do a cheap check first if it's even worth it */
-        if (detect_vm() == VIRTUALIZATION_NONE)
-                return false;
-
-        r = recurse_dir_at(
-                        AT_FDCWD,
-                        "/sys/devices/pci0000:00",
-                        /* statx_mask= */ 0,
-                        /* n_depth_max= */ 3,
-                        RECURSE_DIR_ENSURE_TYPE,
-                        match_modalias_recurse_dir_cb,
-                        STRV_MAKE("virtio:d00000003v", "virtio:d0000000Bv"));
-        if (r < 0)
-                log_debug_errno(r, "Failed to determine whether host has virtio-console device, ignoring: %m");
+static bool has_virtio_rng(void) {
+        return has_virtio_feature("virtio-rng", STRV_MAKE("pci:v00001AF4d00001005", "pci:v00001AF4d00001044"));
+}
 
-        return r > 0;
+static bool has_virtio_console(void) {
+        return has_virtio_feature("virtio-console", STRV_MAKE("virtio:d00000003v", "virtio:d0000000Bv"));
 }
 
 static bool has_virtio_vsock(void) {
-        int r;
-
-        /* Directory traversal might be slow, hence let's do a cheap check first if it's even worth it */
-        if (detect_vm() == VIRTUALIZATION_NONE)
-                return false;
+        return has_virtio_feature("virtio-vsock", STRV_MAKE("virtio:d00000013v"));
+}
 
-        r = recurse_dir_at(
-                        AT_FDCWD,
-                        "/sys/devices/pci0000:00",
-                        /* statx_mask= */ 0,
-                        /* n_depth_max= */ 3,
-                        RECURSE_DIR_ENSURE_TYPE,
-                        match_modalias_recurse_dir_cb,
-                        STRV_MAKE("virtio:d00000013v"));
-        if (r < 0)
-                log_debug_errno(r, "Failed to determine whether host has virtio-vsock device, ignoring: %m");
+static bool has_virtiofs(void) {
+        return has_virtio_feature("virtiofs", STRV_MAKE("virtio:d0000001Av"));
+}
 
-        return r > 0;
+static bool has_virtio_pci(void) {
+        return has_virtio_feature("virtio-pci", STRV_MAKE("pci:v00001AF4d"));
 }
 
 static bool in_qemu(void) {
@@ -168,6 +146,15 @@ int kmod_setup(void) {
                 /* Make sure we can send sd-notify messages over vsock as early as possible. */
                 { "vmw_vsock_virtio_transport", NULL,                        false, false, has_virtio_vsock   },
 
+                /* We can't wait for specific virtiofs tags to show up as device nodes so we have to load the
+                 * virtiofs and virtio_pci modules early to make sure the virtiofs tags are found when
+                 * sysroot.mount is started.
+                 *
+                 * TODO: Remove these again once https://gitlab.com/virtio-fs/virtiofsd/-/issues/128 is
+                 * resolved and the kernel fix is widely available. */
+                { "virtiofs",                   "/sys/module/virtiofs",      false, false, has_virtiofs       },
+                { "virtio_pci",                 "/sys/module/virtio_pci",    false, false, has_virtio_pci     },
+
                 /* qemu_fw_cfg would be loaded by udev later, but we want to import credentials from it super early */
                 { "qemu_fw_cfg",                "/sys/firmware/qemu_fw_cfg", false, false, in_qemu            },