summaryrefslogtreecommitdiffstats
path: root/src/vmspawn
diff options
context:
space:
mode:
authorSam Leonard <sam.leonard@codethink.co.uk>2024-02-14 17:40:40 +0100
committerSam Leonard <sam.leonard@codethink.co.uk>2024-02-21 11:22:40 +0100
commit1ec3218e825bccf6189226121fc26e5c577db5e6 (patch)
treedfbe978dd8af072ddcaef38b502c95c75f84a6d3 /src/vmspawn
parentvmspawn: correctly escape ',' in certain values passed to qemu (diff)
downloadsystemd-1ec3218e825bccf6189226121fc26e5c577db5e6.tar.xz
systemd-1ec3218e825bccf6189226121fc26e5c577db5e6.zip
vmspawn: add --extra-drive=
Diffstat (limited to 'src/vmspawn')
-rw-r--r--src/vmspawn/vmspawn.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c
index fa6c5e2b3e..dd72f24002 100644
--- a/src/vmspawn/vmspawn.c
+++ b/src/vmspawn/vmspawn.c
@@ -86,6 +86,7 @@ static char *arg_forward_journal = NULL;
static bool arg_runtime_directory_created = false;
static bool arg_privileged = false;
static char **arg_kernel_cmdline_extra = NULL;
+static char **arg_extra_drives = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
@@ -99,6 +100,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_initrds, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_runtime_mounts, runtime_mount_context_done);
STATIC_DESTRUCTOR_REGISTER(arg_forward_journal, freep);
STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline_extra, strv_freep);
+STATIC_DESTRUCTOR_REGISTER(arg_extra_drives, strv_freep);
static int help(void) {
_cleanup_free_ char *link = NULL;
@@ -144,6 +146,7 @@ static int help(void) {
" Mount a file or directory from the host into the VM\n"
" --bind-ro=SOURCE[:TARGET]\n"
" Mount a file or directory, but read-only\n"
+ " --extra-drive=PATH Adds an additional disk to the virtual machine\n"
"\n%3$sIntegration:%4$s\n"
" --forward-journal=FILE|DIR\n"
" Forward the VM's journal to the host\n"
@@ -180,6 +183,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_NETWORK_USER_MODE,
ARG_BIND,
ARG_BIND_RO,
+ ARG_EXTRA_DRIVE,
ARG_SECURE_BOOT,
ARG_PRIVATE_USERS,
ARG_FORWARD_JOURNAL,
@@ -209,6 +213,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "network-user-mode", no_argument, NULL, ARG_NETWORK_USER_MODE },
{ "bind", required_argument, NULL, ARG_BIND },
{ "bind-ro", required_argument, NULL, ARG_BIND_RO },
+ { "extra-drive", required_argument, NULL, ARG_EXTRA_DRIVE },
{ "secure-boot", required_argument, NULL, ARG_SECURE_BOOT },
{ "private-users", required_argument, NULL, ARG_PRIVATE_USERS },
{ "forward-journal", required_argument, NULL, ARG_FORWARD_JOURNAL },
@@ -356,6 +361,19 @@ static int parse_argv(int argc, char *argv[]) {
arg_settings_mask |= SETTING_BIND_MOUNTS;
break;
+ case ARG_EXTRA_DRIVE: {
+ _cleanup_free_ char *drive_path = NULL;
+
+ r = parse_path_argument(optarg, /* suppress_root= */ false, &drive_path);
+ if (r < 0)
+ return r;
+
+ r = strv_consume(&arg_extra_drives, TAKE_PTR(drive_path));
+ if (r < 0)
+ return log_oom();
+ break;
+ }
+
case ARG_SECURE_BOOT:
r = parse_tristate(optarg, &arg_secure_boot);
if (r < 0)
@@ -1259,6 +1277,22 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
return log_oom();
}
+ STRV_FOREACH(drive, arg_extra_drives) {
+ _cleanup_free_ char *escaped_drive = NULL;
+
+ r = strv_extend(&cmdline, "-drive");
+ if (r < 0)
+ return log_oom();
+
+ escaped_drive = escape_qemu_value(*drive);
+ if (!escaped_drive)
+ return log_oom();
+
+ r = strv_extendf(&cmdline, "format=raw,cache=unsafe,file=%s", escaped_drive);
+ if (r < 0)
+ return log_oom();
+ }
+
if (kernel) {
r = strv_extend_many(&cmdline, "-kernel", kernel);
if (r < 0)