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 E8E3190D45 for ; Fri, 31 Mar 2023 13:16:49 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8605627091 for ; Fri, 31 Mar 2023 13:16: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, 31 Mar 2023 13:16:47 +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 646A3431D3 for ; Fri, 31 Mar 2023 13:16:46 +0200 (CEST) From: Markus Frank To: pve-devel@lists.proxmox.com Date: Fri, 31 Mar 2023 13:16:14 +0200 Message-Id: <20230331111614.95981-7-m.frank@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230331111614.95981-1-m.frank@proxmox.com> References: <20230331111614.95981-1-m.frank@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.070 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy 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 Subject: [pve-devel] [PATCH qemu-server v3 6/6] 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, 31 Mar 2023 11:16:50 -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 Signed-off-by: Markus Frank --- PVE/API2/Qemu.pm | 20 +++++- PVE/QemuServer.pm | 158 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+), 1 deletion(-) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 587bb22..aaeefe2 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -639,6 +639,8 @@ my $check_vm_modify_config_perm = sub { # the user needs Disk and PowerMgmt privileges to change the vmstate # also needs privileges on the storage, that will be checked later $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk', 'VM.PowerMgmt' ]); + } elsif ($opt =~ m/^sharedfiles\d$/) { + # needs $param->{$opt} so checkout $check_vm_dir_perm } else { # catches hostpci\d+, args, lock, etc. # new options will be checked here @@ -649,6 +651,20 @@ my $check_vm_modify_config_perm = sub { return 1; }; +my $check_vm_dir_perm = sub { + my ($rpcenv, $authuser, $param) = @_; + + return 1 if $authuser eq 'root@pam'; + + foreach my $opt (keys %{$param}) { + if ($opt =~ m/^sharedfiles\d$/) { + my $sharedfiles = PVE::QemuServer::parse_sharedfiles($param->{$opt}); + $rpcenv->check_dir_perm($authuser, $sharedfiles->{dirid}, ['Map.Use']); + } + } + return 1; +}; + __PACKAGE__->register_method({ name => 'vmlist', path => '', @@ -875,7 +891,7 @@ __PACKAGE__->register_method({ &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage); &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]); - + &$check_vm_dir_perm($rpcenv, $authuser, $param); &$check_vm_create_serial_perm($rpcenv, $authuser, $vmid, $pool, $param); &$check_vm_create_usb_perm($rpcenv, $authuser, $vmid, $pool, $param); @@ -1576,6 +1592,8 @@ my $update_vm_api = sub { &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]); + &$check_vm_dir_perm($rpcenv, $authuser, $param); + &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param); my $updatefn = sub { diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index c1d0fd2..af47311 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -57,6 +57,7 @@ use PVE::QemuServer::Memory; use PVE::QemuServer::Monitor qw(mon_cmd); use PVE::QemuServer::PCI qw(print_pci_addr print_pcie_addr print_pcie_root_port parse_hostpci); use PVE::QemuServer::USB qw(parse_usb_device); +use PVE::DirConfig; my $have_sdn; eval { @@ -274,6 +275,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, + }, + dirid => { + type => 'string', + description => "dirid of directory you want to share with the guest VM", + format_description => "virtio-sharedfiles-dirid", + 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', @@ -832,6 +858,7 @@ while (my ($k, $v) = each %$confdesc) { my $MAX_USB_DEVICES = 14; my $MAX_NETS = 32; +my $MAX_SHAREDFILES = 10; my $MAX_SERIAL_PORTS = 4; my $MAX_PARALLEL_PORTS = 3; my $MAX_NUMA = 8; @@ -974,6 +1001,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 = { @@ -1035,6 +1068,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}; } @@ -1988,6 +2025,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) = @_; @@ -4105,6 +4152,44 @@ 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 dirid of a directory to share' if !$sharedfiles->{dirid}; + 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"; + } + + my $path = extract_dir_path($nodename, $sharedfiles->{dirid}); + + if ($sharedfiles->{type} eq 'virtio-9p') { + push @$devices, '-fsdev', "local,security_model=passthrough,id=fsdev$i" + .",path=$path"; + push @$devices, '-device', "virtio-9p-pci,id=fs9p$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}); @@ -4190,6 +4275,57 @@ sub config_to_command { return wantarray ? ($cmd, $vollist, $spice_port) : $cmd; } +sub extract_dir_path { + my ($nodename, $dirid) = @_; + my $dir_config = PVE::DirConfig::load_config($nodename); + my $path = $dir_config->{$dirid}; + if (!$path) { + die "Directory ID $dirid does not exist\n"; + } + if (! -e $path) { + die "Path $path does not exist\n"; + } + if ((-e $path) && (! -d $path)) { + die "Path $path exists but is not a directory\n" + } + return $path; +} + +sub start_virtiofs { + my ($vmid, $path, $fsid, $dirid) = @_; + + my $socket_path_root = "/var/run/virtiofsd"; + mkdir $socket_path_root; + my $socket_path = "$socket_path_root/vm$vmid-fs$fsid"; + unlink($socket_path); + my $socket = IO::Socket::UNIX->new( + Type => SOCK_STREAM, + Local => $socket_path, + Listen => 1, + ) or die "cannot create socket - $!\n"; + + my $flags = fcntl($socket, F_GETFD, 0) + or die "failed to get file descriptor flags: $!\n"; + fcntl($socket, F_SETFD, $flags & ~FD_CLOEXEC) + or die "failed to remove FD_CLOEXEC from file descriptor\n"; + + my $fd = $socket->fileno(); + + my $pid = fork(); + if ($pid == 0) { + # TODO replace with rust implementation in bookworm + run_command(['/usr/lib/kvm/virtiofsd', "--fd=$fd", + '-o', "source=$path", '-o', 'cache=always']); + POSIX::_exit(0); + } elsif (!defined($pid)) { + die "could not fork to start virtiofsd"; + } + + # return socket to keep it alive, + # so that qemu will wait for virtiofsd to start + return $socket; +} + sub check_rng_source { my ($source) = @_; @@ -5747,6 +5883,23 @@ sub vm_start_nolock { my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu, $params->{'pbs-backing'}); + my $nodename = nodename(); + my @sockets; + 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; + + my $path = extract_dir_path($nodename, $sharedfiles->{dirid}); + + if ($sharedfiles && $sharedfiles->{type} eq 'virtio-fs' && !$conf->{numa}) { + my $socket = start_virtiofs($vmid, $path, $i, $sharedfiles->{dirid}); + push @sockets, $socket; + } + } + my $migration_ip; my $get_migration_ip = sub { my ($nodename) = @_; @@ -6094,6 +6247,11 @@ sub vm_start_nolock { PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-start'); + foreach my $socket (@sockets) { + shutdown($socket, 2); + close($socket); + } + return $res; } -- 2.30.2