vmspawn: add --extra-drive=

This commit is contained in:
Sam Leonard
2024-02-21 10:22:40 +00:00
parent 018cc9eaf6
commit 1ec3218e82
2 changed files with 43 additions and 0 deletions
+9
View File
@@ -328,6 +328,15 @@
<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--extra-drive=</option><replaceable>PATH</replaceable></term>
<listitem><para>Takes a disk image or block device on the host and supplies it to the virtual machine as another drive.</para>
<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</varlistentry>
</variablelist>
</refsect2>
+34
View File
@@ -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)