[pve-devel] [PATCH v2 guest-common 1/1] Add foreach_passthrough_device

Filip Schauer f.schauer at proxmox.com
Tue Oct 24 14:55:54 CEST 2023


Add a function to iterate over passthrough devices of a provided
container config.

Signed-off-by: Filip Schauer <f.schauer at proxmox.com>
---
 src/PVE/AbstractConfig.pm | 44 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm
index a286b13..ed26e91 100644
--- a/src/PVE/AbstractConfig.pm
+++ b/src/PVE/AbstractConfig.pm
@@ -484,6 +484,50 @@ sub foreach_volume {
     return $class->foreach_volume_full($conf, undef, $func, @param);
 }
 
+sub foreach_passthrough_device {
+    my ($class, $conf, $func, @param) = @_;
+
+    foreach my $key (keys %$conf) {
+	next if $key !~ m/^dev(\d+)$/;
+
+	my $device = $class->parse_device($conf->{$key});
+
+	if (defined($device->{path})) {
+	    die "Device path $device->{path} does not start with /dev/\n"
+		if $device->{path} !~ m!^/dev/!;
+
+	    my $sanitized_path = substr($conf->{$key}, 1);
+	    die "Device /$sanitized_path does not exist\n" unless (-e "/$sanitized_path");
+
+	    $func->($key, $sanitized_path, @param);
+	} elsif (defined($device->{usbmapping})) {
+	    my $mapping = $device->{usbmapping};
+	    my $map_devices = PVE::Mapping::USB::find_on_current_node($mapping);
+
+	    die "USB device mapping not found for '$mapping'\n"
+		if !$map_devices || !scalar($map_devices->@*);
+
+	    die "More than one USB mapping per host not supported\n"
+		if scalar($map_devices->@*) > 1;
+
+	    eval {
+		PVE::Mapping::USB::assert_valid($mapping, $map_devices->[0]);
+	    };
+	    if (my $err = $@) {
+		die "USB Mapping invalid (hardware probably changed): $err\n";
+	    }
+
+	    my $map_device = $map_devices->[0];
+	    my $lsusb_output = `/usr/bin/lsusb -d $map_device->{id}`;
+	    my ($bus_id, $device_id) = $lsusb_output =~ /Bus (\d+) Device (\d+)/;
+
+	    $func->($key, "dev/bus/usb/$bus_id/$device_id", @param);
+	} else {
+	    die "Either path or usbmapping has to be defined for $key";
+	}
+    }
+}
+
 # $volume_map is a hash of 'old_volid' => 'new_volid' pairs.
 # This method replaces 'old_volid' by 'new_volid' throughout the config including snapshots, pending
 # changes, unused volumes and vmstate volumes.
-- 
2.39.2






More information about the pve-devel mailing list