]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
vmspawn: Use host_device driver for --extra-drive block devices
authorNick Labich <nick@labich.org>
Fri, 27 Jun 2025 15:52:09 +0000 (11:52 -0400)
committerLennart Poettering <lennart@poettering.net>
Mon, 30 Jun 2025 08:08:30 +0000 (10:08 +0200)
Extra drives attached to VMs use the file driver for regular files and the
host_device driver for block devices.

Fixes https://github.com/systemd/systemd/issues/37986

src/vmspawn/vmspawn.c

index a717f3b0bd6968269d9a21d6d8f09e1c20e14e80..49833ec49fce03f522e7671f2a23fec9262222cd 100644 (file)
@@ -2013,6 +2013,8 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
         size_t i = 0;
         STRV_FOREACH(drive, arg_extra_drives) {
                 _cleanup_free_ char *escaped_drive = NULL;
+                const char *driver = NULL;
+                struct stat st;
 
                 r = strv_extend(&cmdline, "-blockdev");
                 if (r < 0)
@@ -2022,7 +2024,17 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
                 if (!escaped_drive)
                         return log_oom();
 
-                r = strv_extendf(&cmdline, "driver=raw,cache.direct=off,cache.no-flush=on,file.driver=file,file.filename=%s,node-name=vmspawn_extra_%zu", escaped_drive, i);
+                if (stat(*drive, &st) < 0)
+                        return log_error_errno(errno, "Failed to stat '%s': %m", *drive);
+
+                if (S_ISREG(st.st_mode))
+                        driver = "file";
+                else if (S_ISBLK(st.st_mode))
+                        driver = "host_device";
+                else
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Expected regular file or block device, not '%s': %m", *drive);
+
+                r = strv_extendf(&cmdline, "driver=raw,cache.direct=off,cache.no-flush=on,file.driver=%s,file.filename=%s,node-name=vmspawn_extra_%zu", driver, escaped_drive, i);
                 if (r < 0)
                         return log_oom();