]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
vmspawn: search for machines when only passed -M/--machine=
authorSam Leonard <sam.leonard@codethink.co.uk>
Wed, 7 Feb 2024 14:51:21 +0000 (14:51 +0000)
committerSam Leonard <sam.leonard@codethink.co.uk>
Tue, 13 Feb 2024 12:31:03 +0000 (12:31 +0000)
src/vmspawn/vmspawn.c

index fc776746d01875fbcaeea3ce30ae639bfc5dd7cc..116f0143cde44d6f054988946267aaabc5863839 100644 (file)
@@ -12,6 +12,7 @@
 #include "chase.h"
 #include "dirent-util.h"
 #include "fd-util.h"
+#include "discover-image.h"
 #include "sd-daemon.h"
 #include "sd-event.h"
 #include "sd-id128.h"
@@ -1497,8 +1498,30 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
 static int determine_names(void) {
         int r;
 
-        if (!arg_directory && !arg_image)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine path, please use -D or -i.");
+        if (!arg_directory && !arg_image) {
+                if (arg_machine) {
+                        _cleanup_(image_unrefp) Image *i = NULL;
+
+                        r = image_find(IMAGE_MACHINE, arg_machine, NULL, &i);
+                        if (r == -ENOENT)
+                                return log_error_errno(r, "No image for machine '%s'.", arg_machine);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to find image for machine '%s': %m", arg_machine);
+
+                        if (IN_SET(i->type, IMAGE_RAW, IMAGE_BLOCK))
+                                r = free_and_strdup(&arg_image, i->path);
+                        else if (IN_SET(i->type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME))
+                                r = free_and_strdup(&arg_directory, i->path);
+                        else
+                                assert_not_reached();
+                        if (r < 0)
+                                return log_oom();
+                } else {
+                        r = safe_getcwd(&arg_directory);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to determine current directory: %m");
+                }
+        }
 
         if (!arg_machine) {
                 if (arg_directory && path_equal(arg_directory, "/")) {