public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support
@ 2022-10-07 14:29 Markus Frank
  2022-10-07 14:29 ` [pve-devel] [PATCH pve-manager] added options to add virtio-9p & virtio-fs fileshare to the config Markus Frank
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Markus Frank @ 2022-10-07 14:29 UTC (permalink / raw)
  To: pve-devel

adds support for sharing directorys with a guest vm.

virtio-9p can be simply started with qemu.
virtio-fs needs virtiofsd to be started before qemu.

Signed-off-by: Markus Frank <m.frank@proxmox.com>
---
I chose MAX_SHAREDFILES to be 10, because I think it is more than enough.

 PVE/QemuServer.pm | 113 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 113 insertions(+)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 4e85dd0..580133b 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -272,6 +272,31 @@ my $rng_fmt = {
     },
 };
 
+my $sharedfiles_fmt = {
+    type => {
+	type => 'string',
+	default_key => 1,
+	enum => ['virtio-9p', 'virtio-fs'],
+	description => "sharedfiles via"
+	    ." virtio-9p (https://www.linux-kvm.org/page/9p_virtio)"
+	    ." or virtio-fs (https://virtio-fs.gitlab.io/howto-qemu.html)",
+	format_description => "virtio-sharedfiles-type",
+	optional => 1,
+    },
+    path => {
+	type => 'string',
+	description => "path you want to share with the guest VM",
+	format_description => "virtio-sharedfiles-path",
+	optional => 1,
+    },
+    tag => {
+	type => 'string',
+	description => "tag name for mounting in the guest VM",
+	format_description => "virtio-sharedfiles-tag",
+	optional => 1,
+    },
+};
+
 my $meta_info_fmt = {
     'ctime' => {
 	type => 'integer',
@@ -826,6 +851,7 @@ while (my ($k, $v) = each %$confdesc) {
 
 my $MAX_USB_DEVICES = 5;
 my $MAX_NETS = 32;
+my $MAX_SHAREDFILES = 10;
 my $MAX_SERIAL_PORTS = 4;
 my $MAX_PARALLEL_PORTS = 3;
 my $MAX_NUMA = 8;
@@ -968,6 +994,12 @@ my $netdesc = {
     description => "Specify network devices.",
 };
 
+my $sharedfilesdesc = {
+    optional => 1,
+    type => 'string', format => $sharedfiles_fmt,
+    description => "share files between host and guest",
+};
+
 PVE::JSONSchema::register_standard_option("pve-qm-net", $netdesc);
 
 my $ipconfig_fmt = {
@@ -1029,6 +1061,10 @@ for (my $i = 0; $i < $MAX_NETS; $i++)  {
     $confdesc_cloudinit->{"ipconfig$i"} = $ipconfigdesc;
 }
 
+for (my $i = 0; $i < $MAX_SHAREDFILES; $i++)  {
+    $confdesc->{"sharedfiles$i"} = $sharedfilesdesc;
+}
+
 foreach my $key (keys %$confdesc_cloudinit) {
     $confdesc->{$key} = $confdesc_cloudinit->{$key};
 }
@@ -1933,6 +1969,16 @@ sub parse_net {
     return $res;
 }
 
+sub parse_sharedfiles {
+    my ($value) = @_;
+
+    return if !$value;
+    my $res = eval { parse_property_string($sharedfiles_fmt, $value) };
+
+    warn $@ if $@;
+    return $res;
+}
+
 # ipconfigX ip=cidr,gw=ip,ip6=cidr,gw6=ip
 sub parse_ipconfig {
     my ($data) = @_;
@@ -4022,6 +4068,45 @@ sub config_to_command {
 	push @$devices, '-device', $netdevicefull;
     }
 
+    my $onevirtiofs = 0;
+    for (my $i = 0; $i < $MAX_SHAREDFILES; $i++) {
+	my $sharedfilesstr = "sharedfiles$i";
+
+	next if !$conf->{$sharedfilesstr};
+	my $sharedfiles = parse_sharedfiles($conf->{$sharedfilesstr});
+	next if !$sharedfiles;
+
+	die $sharedfilesstr.' needs a type (virtio-9p or virtio-fs)' if !$sharedfiles->{type};
+	die $sharedfilesstr.' needs a path to a directory to share' if !$sharedfiles->{path};
+	die $sharedfilesstr.' needs a mount tag' if !$sharedfiles->{tag};
+
+	if ($sharedfiles->{type} eq 'virtio-fs' && $conf->{numa}) {
+	    die "disable numa to use virtio-fs or use virtio-9p instead";
+	}
+
+	mkdir $sharedfiles->{path};
+
+	if ($sharedfiles->{type} eq 'virtio-9p') {
+	    push @$devices, '-fsdev', 'local,security_model=passthrough,id=fsdev'.$i
+		.',path='.$sharedfiles->{path};
+	    push @$devices, '-device', 'virtio-9p-pci,id=fs'.$i.',fsdev=fsdev'.$i
+		.',mount_tag='.$sharedfiles->{tag};
+	}
+	if ($sharedfiles->{type} eq 'virtio-fs') {
+	    push @$devices, '-chardev', 'socket,id=virtfs'.$i
+		.',path=/var/run/virtiofsd/vm'.$vmid.'-fs'.$i;
+	    push @$devices, '-device', 'vhost-user-fs-pci,queue-size=1024,chardev=virtfs'.$i
+		.',tag='.$sharedfiles->{tag};
+	    $onevirtiofs = 1;
+	}
+    }
+
+    if ($onevirtiofs) {
+	push @$devices, '-object', 'memory-backend-file,id=mem,'
+	    .'size='.$conf->{memory}.'M,mem-path=/dev/shm,share=on';
+	push @$devices, '-numa', 'node,memdev=mem';
+    }
+
     if ($conf->{ivshmem}) {
 	my $ivshmem = parse_property_string($ivshmem_fmt, $conf->{ivshmem});
 
@@ -4107,6 +4192,22 @@ sub config_to_command {
     return wantarray ? ($cmd, $vollist, $spice_port) : $cmd;
 }
 
+sub start_virtiofs {
+    my ($vmid, $path, $fsid) = @_;
+    # virtiofsd does not run in background until vhost-user connects
+    # to the socket, so it has to be started in a fork or with a tool
+    # like daemonize
+
+    my $pid = fork();
+    if ($pid == 0) {
+	run_command('/usr/lib/kvm/virtiofsd --daemonize --socket-path=/var/run/virtiofsd/'
+	    .'vm'.$vmid.'-fs'.$fsid.' -o source='.$path.' -o cache=always');
+	POSIX::_exit(0);
+    } elsif (!defined($pid)) {
+        die "could not fork to start virtiofsd";
+    }
+}
+
 sub check_rng_source {
     my ($source) = @_;
 
@@ -5497,6 +5598,18 @@ sub vm_start_nolock {
     my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid,
 	$conf, $defaults, $forcemachine, $forcecpu, $params->{'pbs-backing'});
 
+    for (my $i = 0; $i < $MAX_SHAREDFILES; $i++) {
+	my $sharedfilesstr = "sharedfiles$i";
+
+	next if !$conf->{$sharedfilesstr};
+	my $sharedfiles = parse_sharedfiles($conf->{$sharedfilesstr});
+	next if !$sharedfiles;
+
+	if ($sharedfiles && $sharedfiles->{type} eq 'virtio-fs' && !$conf->{numa}) {
+	    start_virtiofs($vmid, $sharedfiles->{path}, $i);
+	}
+    }
+
     my $migration_ip;
     my $get_migration_ip = sub {
 	my ($nodename) = @_;
-- 
2.30.2





^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pve-devel] [PATCH pve-manager] added options to add virtio-9p & virtio-fs fileshare to the config
  2022-10-07 14:29 [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support Markus Frank
@ 2022-10-07 14:29 ` Markus Frank
  2022-10-07 14:29 ` [pve-devel] [PATCH pve-docs] added shared filesystem doc for virtio-fs & virtio-9p Markus Frank
  2022-10-10 12:17 ` [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support Fabian Grünbichler
  2 siblings, 0 replies; 4+ messages in thread
From: Markus Frank @ 2022-10-07 14:29 UTC (permalink / raw)
  To: pve-devel

Signed-off-by: Markus Frank <m.frank@proxmox.com>
---
 www/manager6/Makefile                |   1 +
 www/manager6/Utils.js                |  12 ++-
 www/manager6/qemu/HardwareView.js    |  18 +++++
 www/manager6/qemu/SharedfilesEdit.js | 106 +++++++++++++++++++++++++++
 4 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100644 www/manager6/qemu/SharedfilesEdit.js

diff --git a/www/manager6/Makefile b/www/manager6/Makefile
index d16770b1..dd907d36 100644
--- a/www/manager6/Makefile
+++ b/www/manager6/Makefile
@@ -209,6 +209,7 @@ JSSRC= 							\
 	qemu/Config.js					\
 	qemu/CreateWizard.js				\
 	qemu/DisplayEdit.js				\
+	qemu/SharedfilesEdit.js				\
 	qemu/HDEdit.js					\
 	qemu/HDEfi.js					\
 	qemu/HDTPM.js					\
diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js
index 7ca6a271..e3fb40e5 100644
--- a/www/manager6/Utils.js
+++ b/www/manager6/Utils.js
@@ -1569,7 +1569,17 @@ Ext.define('PVE.Utils', {
 	}
     },
 
-    hardware_counts: { net: 32, usb: 5, hostpci: 16, audio: 1, efidisk: 1, serial: 4, rng: 1, tpmstate: 1 },
+    hardware_counts: {
+	net: 32,
+	usb: 5,
+	hostpci: 16,
+	audio: 1,
+	efidisk: 1,
+	serial: 4,
+	rng: 1,
+	tpmstate: 1,
+	sharedfiles: 10,
+    },
 
     cleanEmptyObjectKeys: function(obj) {
 	for (const propName of Object.keys(obj)) {
diff --git a/www/manager6/qemu/HardwareView.js b/www/manager6/qemu/HardwareView.js
index 6e9d03b4..530934ce 100644
--- a/www/manager6/qemu/HardwareView.js
+++ b/www/manager6/qemu/HardwareView.js
@@ -233,6 +233,16 @@ Ext.define('PVE.qemu.HardwareView', {
 		header: gettext('Network Device') + ' (' + confid +')',
 	    };
 	}
+	for (let i = 0; i < PVE.Utils.hardware_counts.sharedfiles; i++) {
+	    let confid = "sharedfiles" + i.toString();
+	    rows[confid] = {
+		group: 18,
+		order: i,
+		iconCls: 'hdd-o',
+		editor: 'PVE.qemu.SharedfilesEdit',
+		header: gettext('Shared FS') + ' (' + confid +')',
+	    };
+	}
 	rows.efidisk0 = {
 	    group: 20,
 	    iconCls: 'hdd-o',
@@ -578,6 +588,7 @@ Ext.define('PVE.qemu.HardwareView', {
 	    me.down('#addRng').setDisabled(noSysConsolePerm || isAtLimit('rng'));
 	    efidisk_menuitem.setDisabled(noVMConfigDiskPerm || isAtLimit('efidisk'));
 	    me.down('#addTpmState').setDisabled(noSysConsolePerm || isAtLimit('tpmstate'));
+	    me.down('#addFileshare').setDisabled(noSysConsolePerm || isAtLimit('sharedfiles'));
 	    me.down('#addCloudinitDrive').setDisabled(noSysConsolePerm || hasCloudInit);
 
 	    if (!rec) {
@@ -718,6 +729,13 @@ Ext.define('PVE.qemu.HardwareView', {
 				disabled: !caps.nodes['Sys.Console'],
 				handler: editorFactory('RNGEdit'),
 			    },
+			    {
+				text: gettext("Shared Filesystem"),
+				itemId: 'addFileshare',
+				iconCls: 'fa fa-fw fa-hdd-o black',
+				disabled: !caps.nodes['Sys.Console'],
+				handler: editorFactory('SharedfilesEdit'),
+			    },
 			],
 		    }),
 		},
diff --git a/www/manager6/qemu/SharedfilesEdit.js b/www/manager6/qemu/SharedfilesEdit.js
new file mode 100644
index 00000000..7baf90d3
--- /dev/null
+++ b/www/manager6/qemu/SharedfilesEdit.js
@@ -0,0 +1,106 @@
+Ext.define('PVE.qemu.SharedfilesInputPanel', {
+    extend: 'Proxmox.panel.InputPanel',
+    xtype: 'pveSharedfilesInputPanel',
+    onlineHelp: 'qm_sharedfiles',
+
+    insideWizard: false,
+
+    onGetValues: function(values) {
+	var me = this;
+	var confid = me.confid;
+	var params = {};
+	params[confid] = PVE.Parser.printPropertyString(values, 'type');
+	console.log(params);
+	return params;
+    },
+
+    setSharedfiles: function(confid, data) {
+	var me = this;
+	me.confid = confid;
+	me.sharedfiles = data;
+	me.setValues(me.sharedfiles);
+    },
+    items: [
+	{
+	    name: 'type',
+	    xtype: 'proxmoxKVComboBox',
+	    fieldLabel: gettext('Shared FS Type'),
+	    comboItems: [['virtio-9p', 'virtio-9p'], ['virtio-fs', 'virtio-fs']],
+	    allowBlank: false,
+	},
+	{
+	    xtype: 'proxmoxtextfield',
+	    emptyText: '/path/to/shared/dir',
+	    fieldLabel: gettext('shared path'),
+	    name: 'path',
+	    allowBlank: false,
+	},
+	{
+	    xtype: 'proxmoxtextfield',
+	    emptyText: 'tag name',
+	    fieldLabel: gettext('tag'),
+	    name: 'tag',
+	    allowBlank: false,
+	},
+    ],
+
+    initComponent: function() {
+	var me = this;
+
+	me.sharedfiles = {};
+	me.confid = 'sharedfiles0';
+	me.callParent();
+    },
+});
+
+Ext.define('PVE.qemu.SharedfilesEdit', {
+    extend: 'Proxmox.window.Edit',
+
+    subject: gettext('Filesystem Passthrough'),
+
+    initComponent: function() {
+	var me = this;
+
+	me.isCreate = !me.confid;
+
+	var ipanel = Ext.create('PVE.qemu.SharedfilesInputPanel', {
+	    confid: me.confid,
+	    isCreate: me.isCreate,
+	});
+
+	Ext.applyIf(me, {
+	    subject: gettext('Sharedfiles 1'),
+	    items: ipanel,
+	});
+
+	me.callParent();
+
+	me.load({
+	    success: function(response) {
+	        me.conf = response.result.data;
+	        var i, confid;
+	        if (!me.isCreate) {
+		    var value = me.conf[me.confid];
+		    console.log(value);
+		    var sharedfiles = PVE.Parser.parsePropertyString(value, "type");
+		    console.log(sharedfiles);
+		    if (!sharedfiles) {
+			Ext.Msg.alert(gettext('Error'), 'Unable to parse network options');
+			me.close();
+			return;
+		    }
+		    ipanel.setSharedfiles(me.confid, sharedfiles);
+		} else {
+		    for (i = 0; i < PVE.Utils.hardware_counts.sharedfiles; i++) {
+		        confid = 'sharedfiles' + i.toString();
+		        if (!Ext.isDefined(me.conf[confid])) {
+			    me.confid = confid;
+			    break;
+			}
+		    }
+		    ipanel.setSharedfiles(me.confid, {});
+		}
+	    },
+	});
+    },
+});
-- 
2.30.2





^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pve-devel] [PATCH pve-docs] added shared filesystem doc for virtio-fs & virtio-9p
  2022-10-07 14:29 [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support Markus Frank
  2022-10-07 14:29 ` [pve-devel] [PATCH pve-manager] added options to add virtio-9p & virtio-fs fileshare to the config Markus Frank
@ 2022-10-07 14:29 ` Markus Frank
  2022-10-10 12:17 ` [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support Fabian Grünbichler
  2 siblings, 0 replies; 4+ messages in thread
From: Markus Frank @ 2022-10-07 14:29 UTC (permalink / raw)
  To: pve-devel

Signed-off-by: Markus Frank <m.frank@proxmox.com>
---
 qm.adoc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/qm.adoc b/qm.adoc
index 4d0c7c4..bd0e2f3 100644
--- a/qm.adoc
+++ b/qm.adoc
@@ -909,6 +909,56 @@ recommended to always use a limiter to avoid guests using too many host
 resources. If desired, a value of '0' for `max_bytes` can be used to disable
 all limits.
 
+[[qm_sharedfiles]]
+Shared Filesystems
+~~~~~~~~~~~~~~~~~~
+
+9pfs (virtio-9p)
+^^^^^^^^^^^^^^^^
+
+QEMU's 9pfs uses the Plan 9 Filesystem Protocol to share a directory on the host
+with a guest VM.
+
+To share a directory with 9p, run the following command:
+
+----
+qm set <vmid> -sharedfiles0 virtio-9p,path=<directory>,tag=<mount tag>
+----
+
+To mount QEMU's 9pfs in a guest VM with the Linux kernel 9p driver, run the
+following command:
+
+----
+mount -t 9p -o trans=virtio,version=9p2000.L <mount tag> <mount point>
+----
+
+https://www.linux-kvm.org/page/9p_virtio
+
+https://wiki.qemu.org/Documentation/9psetup
+
+virtio-fs
+^^^^^^^^^
+
+Virtio-fs is a shared file system, that enables sharing between host and 
+guest VM while taking advantage of the locality of virtual machines and the
+hypervisor to get a higher throughput than 9p.
+Numa must be disabled to use virtio-fs.
+
+To share a directory with virtio-fs, run the following command:
+
+----
+qm set <vmid> -sharedfiles0 virtio-fs,path=<directory>,tag=<mount tag>
+----
+
+To mount virtio-fs in a guest VM with the Linux kernel virtiofs driver, run the
+following command:
+
+----
+mount -t virtiofs <mount tag> <mount point>
+----
+
+https://virtio-fs.gitlab.io/howto-qemu.html
+
 [[qm_bootorder]]
 Device Boot Order
 ~~~~~~~~~~~~~~~~~
-- 
2.30.2





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support
  2022-10-07 14:29 [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support Markus Frank
  2022-10-07 14:29 ` [pve-devel] [PATCH pve-manager] added options to add virtio-9p & virtio-fs fileshare to the config Markus Frank
  2022-10-07 14:29 ` [pve-devel] [PATCH pve-docs] added shared filesystem doc for virtio-fs & virtio-9p Markus Frank
@ 2022-10-10 12:17 ` Fabian Grünbichler
  2 siblings, 0 replies; 4+ messages in thread
From: Fabian Grünbichler @ 2022-10-10 12:17 UTC (permalink / raw)
  To: Proxmox VE development discussion

On October 7, 2022 4:29 pm, Markus Frank wrote:
> adds support for sharing directorys with a guest vm.
> 
> virtio-9p can be simply started with qemu.
> virtio-fs needs virtiofsd to be started before qemu.
> 
> Signed-off-by: Markus Frank <m.frank@proxmox.com>
> ---
> I chose MAX_SHAREDFILES to be 10, because I think it is more than enough.

famous last words ;)

> 
>  PVE/QemuServer.pm | 113 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 113 insertions(+)
> 
> diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
> index 4e85dd0..580133b 100644
> --- a/PVE/QemuServer.pm
> +++ b/PVE/QemuServer.pm
> @@ -272,6 +272,31 @@ my $rng_fmt = {
>      },
>  };
>  
> +my $sharedfiles_fmt = {
> +    type => {
> +	type => 'string',
> +	default_key => 1,
> +	enum => ['virtio-9p', 'virtio-fs'],
> +	description => "sharedfiles via"
> +	    ." virtio-9p (https://www.linux-kvm.org/page/9p_virtio)"
> +	    ." or virtio-fs (https://virtio-fs.gitlab.io/howto-qemu.html)",
> +	format_description => "virtio-sharedfiles-type",
> +	optional => 1,
> +    },
> +    path => {
> +	type => 'string',
> +	description => "path you want to share with the guest VM",
> +	format_description => "virtio-sharedfiles-path",
> +	optional => 1,

this should have more restrictions than being an arbitrary string.

ideally we could take this opportunity and implement some sort of 
"defined host source paths" feature that we can re-use for bind mounts 
as well:

- admin defines dirs on the host that are eligibly for mounting into 
  guests
- admin gives access via an ACL
- user can then use this object as bind mount/shared dir source without 
  requiring root access

(and not allow arbitrary paths here at all?)

> +    },
> +    tag => {
> +	type => 'string',
> +	description => "tag name for mounting in the guest VM",
> +	format_description => "virtio-sharedfiles-tag",
> +	optional => 1,
> +    },
> +};
> +
>  my $meta_info_fmt = {
>      'ctime' => {
>  	type => 'integer',
> @@ -826,6 +851,7 @@ while (my ($k, $v) = each %$confdesc) {
>  
>  my $MAX_USB_DEVICES = 5;
>  my $MAX_NETS = 32;
> +my $MAX_SHAREDFILES = 10;
>  my $MAX_SERIAL_PORTS = 4;
>  my $MAX_PARALLEL_PORTS = 3;
>  my $MAX_NUMA = 8;
> @@ -968,6 +994,12 @@ my $netdesc = {
>      description => "Specify network devices.",
>  };
>  
> +my $sharedfilesdesc = {
> +    optional => 1,
> +    type => 'string', format => $sharedfiles_fmt,
> +    description => "share files between host and guest",
> +};
> +
>  PVE::JSONSchema::register_standard_option("pve-qm-net", $netdesc);
>  
>  my $ipconfig_fmt = {
> @@ -1029,6 +1061,10 @@ for (my $i = 0; $i < $MAX_NETS; $i++)  {
>      $confdesc_cloudinit->{"ipconfig$i"} = $ipconfigdesc;
>  }
>  
> +for (my $i = 0; $i < $MAX_SHAREDFILES; $i++)  {
> +    $confdesc->{"sharedfiles$i"} = $sharedfilesdesc;
> +}
> +
>  foreach my $key (keys %$confdesc_cloudinit) {
>      $confdesc->{$key} = $confdesc_cloudinit->{$key};
>  }
> @@ -1933,6 +1969,16 @@ sub parse_net {
>      return $res;
>  }
>  
> +sub parse_sharedfiles {
> +    my ($value) = @_;
> +
> +    return if !$value;
> +    my $res = eval { parse_property_string($sharedfiles_fmt, $value) };
> +
> +    warn $@ if $@;
> +    return $res;
> +}
> +
>  # ipconfigX ip=cidr,gw=ip,ip6=cidr,gw6=ip
>  sub parse_ipconfig {
>      my ($data) = @_;
> @@ -4022,6 +4068,45 @@ sub config_to_command {
>  	push @$devices, '-device', $netdevicefull;
>      }
>  
> +    my $onevirtiofs = 0;
> +    for (my $i = 0; $i < $MAX_SHAREDFILES; $i++) {
> +	my $sharedfilesstr = "sharedfiles$i";
> +
> +	next if !$conf->{$sharedfilesstr};
> +	my $sharedfiles = parse_sharedfiles($conf->{$sharedfilesstr});
> +	next if !$sharedfiles;
> +
> +	die $sharedfilesstr.' needs a type (virtio-9p or virtio-fs)' if !$sharedfiles->{type};
> +	die $sharedfilesstr.' needs a path to a directory to share' if !$sharedfiles->{path};

should check whether path exists and is a directory

> +	die $sharedfilesstr.' needs a mount tag' if !$sharedfiles->{tag};
> +
> +	if ($sharedfiles->{type} eq 'virtio-fs' && $conf->{numa}) {
> +	    die "disable numa to use virtio-fs or use virtio-9p instead";
> +	}
> +
> +	mkdir $sharedfiles->{path};
> +
> +	if ($sharedfiles->{type} eq 'virtio-9p') {
> +	    push @$devices, '-fsdev', 'local,security_model=passthrough,id=fsdev'.$i
> +		.',path='.$sharedfiles->{path};

not sure about the encoding here on the qemu side, but this right now 
allows injecting ',' followed by other fsdev options, so likely we want 
to escape/encode and have more checks on the path.

> +	    push @$devices, '-device', 'virtio-9p-pci,id=fs'.$i.',fsdev=fsdev'.$i
> +		.',mount_tag='.$sharedfiles->{tag};
> +	}
> +	if ($sharedfiles->{type} eq 'virtio-fs') {
> +	    push @$devices, '-chardev', 'socket,id=virtfs'.$i
> +		.',path=/var/run/virtiofsd/vm'.$vmid.'-fs'.$i;
> +	    push @$devices, '-device', 'vhost-user-fs-pci,queue-size=1024,chardev=virtfs'.$i
> +		.',tag='.$sharedfiles->{tag};
> +	    $onevirtiofs = 1;
> +	}
> +    }
> +
> +    if ($onevirtiofs) {
> +	push @$devices, '-object', 'memory-backend-file,id=mem,'
> +	    .'size='.$conf->{memory}.'M,mem-path=/dev/shm,share=on';
> +	push @$devices, '-numa', 'node,memdev=mem';
> +    }
> +
>      if ($conf->{ivshmem}) {
>  	my $ivshmem = parse_property_string($ivshmem_fmt, $conf->{ivshmem});
>  
> @@ -4107,6 +4192,22 @@ sub config_to_command {
>      return wantarray ? ($cmd, $vollist, $spice_port) : $cmd;
>  }
>  
> +sub start_virtiofs {
> +    my ($vmid, $path, $fsid) = @_;
> +    # virtiofsd does not run in background until vhost-user connects
> +    # to the socket, so it has to be started in a fork or with a tool
> +    # like daemonize
> +
> +    my $pid = fork();
> +    if ($pid == 0) {
> +	run_command('/usr/lib/kvm/virtiofsd --daemonize --socket-path=/var/run/virtiofsd/'
> +	    .'vm'.$vmid.'-fs'.$fsid.' -o source='.$path.' -o cache=always');

run_command like that is pretty much always wrong ;) please use

run_command(['usr/lib/kvm/virtiofsd', '--daemonize', ...])

and extract stuff into variables as needed if it gets too long/..

> +	POSIX::_exit(0);
> +    } elsif (!defined($pid)) {
> +        die "could not fork to start virtiofsd";
> +    }
> +}
> +
>  sub check_rng_source {
>      my ($source) = @_;
>  
> @@ -5497,6 +5598,18 @@ sub vm_start_nolock {
>      my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid,
>  	$conf, $defaults, $forcemachine, $forcecpu, $params->{'pbs-backing'});
>  
> +    for (my $i = 0; $i < $MAX_SHAREDFILES; $i++) {
> +	my $sharedfilesstr = "sharedfiles$i";
> +
> +	next if !$conf->{$sharedfilesstr};
> +	my $sharedfiles = parse_sharedfiles($conf->{$sharedfilesstr});
> +	next if !$sharedfiles;
> +
> +	if ($sharedfiles && $sharedfiles->{type} eq 'virtio-fs' && !$conf->{numa}) {
> +	    start_virtiofs($vmid, $sharedfiles->{path}, $i);
> +	}
> +    }
> +
>      my $migration_ip;
>      my $get_migration_ip = sub {
>  	my ($nodename) = @_;
> -- 
> 2.30.2
> 
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 
> 
> 




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-10-10 12:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-07 14:29 [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support Markus Frank
2022-10-07 14:29 ` [pve-devel] [PATCH pve-manager] added options to add virtio-9p & virtio-fs fileshare to the config Markus Frank
2022-10-07 14:29 ` [pve-devel] [PATCH pve-docs] added shared filesystem doc for virtio-fs & virtio-9p Markus Frank
2022-10-10 12:17 ` [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support Fabian Grünbichler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal