From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 7E9CF91860 for ; Fri, 7 Oct 2022 16:30:19 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 668442704C for ; Fri, 7 Oct 2022 16:29:49 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Fri, 7 Oct 2022 16:29:46 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 8C11644847 for ; Fri, 7 Oct 2022 16:29:46 +0200 (CEST) From: Markus Frank To: pve-devel@lists.proxmox.com Date: Fri, 7 Oct 2022 16:29:20 +0200 Message-Id: <20221007142922.165009-1-m.frank@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.054 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [linux-kvm.org, gitlab.io, qemuserver.pm] Subject: [pve-devel] [PATCH qemu-server] feature #1027: virtio-9p & virtio-fs support X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Oct 2022 14:30:19 -0000 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 --- 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