public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Markus Frank <m.frank@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH guest-common v16 2/12] add dir mapping section config
Date: Fri,  4 Apr 2025 18:08:37 +0200	[thread overview]
Message-ID: <20250404160847.489922-3-m.frank@proxmox.com> (raw)
In-Reply-To: <20250404160847.489922-1-m.frank@proxmox.com>

Adds a config file for directories by using a 'map' property string for
each node mapping.

example config:
```
some-dir-id
	map node=node1,path=/path/to/share/
	map node=node2,path=/different/location/
```

Signed-off-by: Markus Frank <m.frank@proxmox.com>
---
v16:
* renamed verify_path to pve_verify_path
* find_on_current_node: fixed undefined values error
  * die if the directory id does not exist
  * die if there is no directory mapping for the node

 src/Makefile           |   1 +
 src/PVE/Mapping/Dir.pm | 198 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 199 insertions(+)
 create mode 100644 src/PVE/Mapping/Dir.pm

diff --git a/src/Makefile b/src/Makefile
index cbc40c1..030e7f7 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -15,6 +15,7 @@ install: PVE
 	install -m 0644 PVE/StorageTunnel.pm ${PERL5DIR}/PVE/
 	install -m 0644 PVE/Tunnel.pm ${PERL5DIR}/PVE/
 	install -d ${PERL5DIR}/PVE/Mapping
+	install -m 0644 PVE/Mapping/Dir.pm ${PERL5DIR}/PVE/Mapping/
 	install -m 0644 PVE/Mapping/PCI.pm ${PERL5DIR}/PVE/Mapping/
 	install -m 0644 PVE/Mapping/USB.pm ${PERL5DIR}/PVE/Mapping/
 	install -d ${PERL5DIR}/PVE/VZDump
diff --git a/src/PVE/Mapping/Dir.pm b/src/PVE/Mapping/Dir.pm
new file mode 100644
index 0000000..2373908
--- /dev/null
+++ b/src/PVE/Mapping/Dir.pm
@@ -0,0 +1,198 @@
+package PVE::Mapping::Dir;
+
+use strict;
+use warnings;
+
+use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_lock_file cfs_write_file);
+use PVE::INotify;
+use PVE::JSONSchema qw(get_standard_option parse_property_string);
+use PVE::SectionConfig;
+
+use base qw(PVE::SectionConfig);
+
+my $FILENAME = 'mapping/dir.cfg';
+
+cfs_register_file($FILENAME,
+    sub { __PACKAGE__->parse_config(@_); },
+    sub { __PACKAGE__->write_config(@_); });
+
+
+# so we don't have to repeat the type every time
+sub parse_section_header {
+    my ($class, $line) = @_;
+
+    if ($line =~ m/^(\S+)\s*$/) {
+	my $id = $1;
+	my $errmsg = undef; # set if you want to skip whole section
+	eval { PVE::JSONSchema::pve_verify_configid($id) };
+	$errmsg = $@ if $@;
+	my $config = {}; # to return additional attributes
+	return ('dir', $id, $errmsg, $config);
+    }
+    return undef;
+}
+
+sub format_section_header {
+    my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
+
+    return "$sectionId\n";
+}
+
+sub type {
+    return 'dir';
+}
+
+# temporary path format that also disallows commas and equal signs
+# TODO: Remove this when property_string supports quotation of properties
+PVE::JSONSchema::register_format('pve-storage-path-in-property-string', \&pve_verify_path);
+sub pve_verify_path {
+    my ($path, $noerr) = @_;
+
+    if ($path !~ m|^/[^;,=\(\)]+|) {
+	return undef if $noerr;
+	die "Value does not look like a valid absolute path."
+	    ." These symbols are currently not allowed in path: ;,=()\n";
+    }
+    return $path;
+}
+
+my $map_fmt = {
+    node => get_standard_option('pve-node'),
+    path => {
+	description => "Absolute directory path that should be shared with the guest.",
+	type => 'string',
+	format => 'pve-storage-path-in-property-string',
+    },
+};
+
+my $defaultData = {
+    propertyList => {
+	id => {
+	    type => 'string',
+	    description => "The ID of the directory mapping",
+	    format => 'pve-configid',
+	},
+	description => {
+	    type => 'string',
+	    description => "Description of the directory mapping",
+	    optional => 1,
+	    maxLength => 4096,
+	},
+	map => {
+	    type => 'array',
+	    description => 'A list of maps for the cluster nodes.',
+	    optional => 1,
+	    items => {
+		type => 'string',
+		format => $map_fmt,
+	    },
+	},
+    },
+};
+
+sub private {
+    return $defaultData;
+}
+
+sub options {
+    return {
+	description => { optional => 1 },
+	map => {},
+    };
+}
+
+sub assert_valid {
+    my ($dir_cfg) = @_;
+
+    my $path = $dir_cfg->{path};
+
+    pve_verify_path($path);
+
+    if (! -e $path) {
+	die "Path $path does not exist\n";
+    } elsif (! -d $path) {
+	die "Path $path exists, but is not a directory\n";
+    }
+
+    return 1;
+};
+
+sub assert_valid_map_list {
+    my ($map_list) = @_;
+
+    my $nodename = PVE::INotify::nodename();
+
+    my %count;
+    for my $map (@$map_list) {
+	my $entry = parse_property_string($map_fmt, $map);
+	if ($entry->{node} eq $nodename) {
+	    assert_valid($entry);
+	}
+	$count{$entry->{node}}++;
+    }
+    for my $node (keys %count) {
+	if ($count{$node} > 1) {
+	    die "Node '$node' is specified $count{$node} times.\n";
+	}
+    }
+}
+
+sub config {
+    return cfs_read_file($FILENAME);
+}
+
+sub lock_dir_config {
+    my ($code, $errmsg) = @_;
+
+    cfs_lock_file($FILENAME, undef, $code);
+    if (my $err = $@) {
+	$errmsg ? die "$errmsg: $err" : die $err;
+    }
+}
+
+sub write_dir_config {
+    my ($cfg) = @_;
+
+    cfs_write_file($FILENAME, $cfg);
+}
+
+sub find_on_current_node {
+    my ($id) = @_;
+
+    my $cfg = config();
+    my $node = PVE::INotify::nodename();
+
+    my $node_mapping = get_node_mapping($cfg, $id, $node);
+    if (!$node_mapping) {
+	die "Directory ID $id does not exist.\n";
+    }
+    if (@{$node_mapping} > 1) {
+	die "More than than one directory mapping for node $node.\n";
+    } elsif (@{$node_mapping} == 0) {
+	die "No directory mapping for node $node.\n";
+    }
+
+    return $node_mapping->[0];
+}
+
+sub get_node_mapping {
+    my ($cfg, $id, $nodename) = @_;
+
+    return undef if !defined($cfg->{ids}->{$id});
+
+    my $res = [];
+    my $mapping_list = $cfg->{ids}->{$id}->{map};
+    for my $map (@{$mapping_list}) {
+	my $entry = eval { parse_property_string($map_fmt, $map) };
+	warn $@ if $@;
+	if ($entry && $entry->{node} eq $nodename) {
+	    push $res->@*, $entry;
+	}
+    }
+    return $res;
+}
+
+PVE::Mapping::Dir->register();
+PVE::Mapping::Dir->init();
+
+1;
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2025-04-04 16:10 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-04 16:08 [pve-devel] [PATCH cluster/guest-common/docs/qemu-server/manager v16 0/12] virtiofs Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH cluster v16 1/12] add mapping/dir.cfg for resource mapping Markus Frank
2025-04-04 17:03   ` [pve-devel] applied: " Thomas Lamprecht
2025-04-04 16:08 ` Markus Frank [this message]
2025-04-07  9:58   ` [pve-devel] applied: [PATCH guest-common v16 2/12] add dir mapping section config Thomas Lamprecht
2025-04-04 16:08 ` [pve-devel] [PATCH docs v16 3/12] add doc section for the shared filesystem virtio-fs Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH qemu-server v16 4/12] control: add virtiofsd as runtime dependency for qemu-server Markus Frank
2025-04-07 10:43   ` Thomas Lamprecht
2025-04-07 10:59     ` Markus Frank
2025-04-07 12:00       ` Thomas Lamprecht
2025-04-07 12:03         ` Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH qemu-server v16 5/7] fix #1027: virtio-fs support Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH qemu-server v16 6/12] migration: check_local_resources for virtiofs Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH qemu-server v16 7/12] disable snapshot (with RAM) and hibernate with virtio-fs devices Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH manager v16 08/12] api: add resource map api endpoints for directories Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH manager v16 09/12] ui: add edit window for dir mappings Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH manager v16 10/12] ui: add resource mapping view for directories Markus Frank
2025-04-07  9:51   ` Filip Schauer
2025-04-04 16:08 ` [pve-devel] [PATCH manager v16 11/12] ui: form: add selector for directory mappings Markus Frank
2025-04-04 16:08 ` [pve-devel] [PATCH manager v16 12/12] ui: add options to add virtio-fs to qemu config Markus Frank
2025-04-07 12:03   ` Filip Schauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250404160847.489922-3-m.frank@proxmox.com \
    --to=m.frank@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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