From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 870AF1FF0C0 for ; Wed, 18 Nov 2026 07:57:00 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 3CDFE2168C; Wed, 18 Nov 2026 07:54:12 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH v6 storage 25/42] api: scan: report the process that has a SAN LUN open Date: Wed, 18 Nov 2026 07:52:42 +0100 Message-ID: <20261118065302.3033937-26-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118065302.3033937-1-t.lamprecht@proxmox.com> References: <20261118065302.3033937-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794984788565 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.179 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 6PSNDUW23H5BAHWNGXYDT73CFKZWY35W X-Message-ID-Hash: 6PSNDUW23H5BAHWNGXYDT73CFKZWY35W X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The LUN inventory shows a LUN as unused if no storage references it and it has no signature. A guest can still have such a LUN as disk, by its device path or from a third-party storage plugin. Wiping the LUN or creating a volume group on it is then refused, as both check for a process that has the device open. But the list gave no hint of that. Report a process that has the LUN open for writing, also through another path of the LUN, and the virtual machine that the control group of the process names. A process that only reads does not count: the status query of an LVM storage reads its physical volumes every few seconds, so their LUNs would show up as in use at times. The list needs one pass over the open files of all processes. That took 2 ms on an idle lab node and 106 ms with 1000 more processes that have 64 files open each. Signed-off-by: Thomas Lamprecht --- src/PVE/API2/Storage/Scan.pm | 14 ++++++++++ src/PVE/CLI/pvesm.pm | 2 +- src/PVE/Storage/SAN.pm | 17 ++++++++++++- src/test/run_san_lun_scan_tests.pl | 41 ++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/PVE/API2/Storage/Scan.pm b/src/PVE/API2/Storage/Scan.pm index 0e7667c8..586bfe53 100644 --- a/src/PVE/API2/Storage/Scan.pm +++ b/src/PVE/API2/Storage/Scan.pm @@ -527,6 +527,20 @@ __PACKAGE__->register_method({ type => 'string', optional => 1, }, + 'used-by-process' => { + description => "A process of the node that has the device of the LUN open" + . " for writing, as PID followed by the command name in parentheses. A" + . " guest shows up like this if it has the LUN as disk by its device" + . " path or from a third-party storage plugin.", + type => 'string', + optional => 1, + }, + 'used-by-guest' => { + description => + "The ID of the virtual machine that this process belongs to.", + type => 'integer', + optional => 1, + }, wwid => { description => "The LUN WWID.", type => 'string', diff --git a/src/PVE/CLI/pvesm.pm b/src/PVE/CLI/pvesm.pm index 347fbffa..7b9d035b 100755 --- a/src/PVE/CLI/pvesm.pm +++ b/src/PVE/CLI/pvesm.pm @@ -745,7 +745,7 @@ our $cmddef = { my ($data, $schema, $options) = @_; my $columns = [qw(devpath name size transport state paths-active paths-expected multipath - usage vgname used-by-storage wwid)]; + usage vgname used-by-storage used-by-guest used-by-process wwid)]; PVE::CLIFormatter::print_api_result($data, $schema, $columns, $options); }, $PVE::RESTHandler::standard_output_options, diff --git a/src/PVE/Storage/SAN.pm b/src/PVE/Storage/SAN.pm index 3d681fbb..c7717415 100644 --- a/src/PVE/Storage/SAN.pm +++ b/src/PVE/Storage/SAN.pm @@ -42,7 +42,7 @@ sub scan_luns($cfg) { PVE::Multipath::Config::wwids_for_node($mpcfg, PVE::INotify::nodename())->@*; my %listed = map { $_ => 1 } PVE::Multipath::list_etc_multipath_wwids()->@*; my $derived = PVE::Multipath::Generator::derived_wwids($cfg, $ids); - my (%storage, %expected, %vg_owner, %target_owner, %base_owner, %targets); + my (%storage, %expected, %vg_owner, %target_owner, %base_owner, %targets, %paths); for my $sid (sort keys %$derived) { for my $wwid (keys $derived->{$sid}->{wwids}->%*) { @@ -105,6 +105,7 @@ sub scan_luns($cfg) { $row->{$key} //= $value if defined($value) && $value ne 'unknown'; } for my $member (map { basename($_) } ($disk->{slaves} // [$disk->{devpath}])->@*) { + $paths{$wwid}->{"/dev/$member"} = 1; my $session = $sessions->{$member} // next; $targets{$wwid}->{ $session->{target} } = 1; $row->{'iscsi-target'} //= $session->{target}; @@ -146,7 +147,21 @@ sub scan_luns($cfg) { $add->($disk); } $add->($maps->{$_}, 1) for sort keys %$maps; + # A guest can have a LUN as disk by its device path or from a third-party storage plugin, also + # by one of its paths. The scans of LVM and udev only read a device, and are gone again a + # moment later. + my %devices = map { $_ => 1 } (map { $_->{devpath} } values %rows), + map { keys %$_ } values %paths; + my $openers = PVE::Diskmanage::device_openers([sort keys %devices], writable => 1); for my $row (values %rows) { + my ($opener) = map { $openers->{$_} // () } abs_path($row->{devpath}) // $row->{devpath}, + sort keys(($paths{ $row->{wwid} } // {})->%*); + if ($opener) { + $row->{'used-by-process'} = "$opener->{pid} ($opener->{command})"; + # a virtual machine runs in a control group of its own that is named after its ID + my $cgroup = file_read_firstline("/proc/$opener->{pid}/cgroup") // ''; + $row->{'used-by-guest'} = int($1) if $cgroup =~ m{^0::/qemu\.slice/(\d+)\.scope$}; + } if (defined($row->{'paths-active'})) { $row->{state} = PVE::Multipath::derive_health( $row->{'paths-active'}, diff --git a/src/test/run_san_lun_scan_tests.pl b/src/test/run_san_lun_scan_tests.pl index abfdfa28..1794af26 100755 --- a/src/test/run_san_lun_scan_tests.pl +++ b/src/test/run_san_lun_scan_tests.pl @@ -58,7 +58,9 @@ $scan->redefine( $code->($_) for grep { /^$regex$/ } qw(sda sdb sdc sdd sde sdf dm-0 loop0 nvme0n1); }, ); +my ($openers, @opener_query) = ({}); $disk->mock( + device_openers => sub { @opener_query = @_; return $openers }, get_disks => sub { ok($_[3], 'SAN inventory opts into iSCSI'); @listed = sort $_[0]->@*; @@ -113,6 +115,45 @@ is($row{'3600plain'}->{'used-by-storage'}, 'direct', 'a storage serving the LUNs my %plain = map { $_->{wwid} => $_ } PVE::Storage::SAN::scan_luns($cfg)->@*; is($plain{'3600plain'}->{'used-by-storage'}, undef, 'a connection storage does not'); } +{ + # a process has a map open by the device it resolves to + $scan->mock(abs_path => sub { $_[0] eq '/dev/mapper/lun' ? '/dev/dm-0' : $_[0] }); + my $cgroup = '0::/qemu.slice/100.scope'; + $scan->mock( + file_read_firstline => sub { + return $_[0] eq '/proc/4242/cgroup' ? $cgroup : PVE::Tools::file_read_firstline(@_); + }, + ); + local $openers->{'/dev/dm-0'} = { pid => 4242, command => 'kvm' }; + my %open = map { $_->{wwid} => $_ } PVE::Storage::SAN::scan_luns($cfg)->@*; + is($open{'3600map'}->{'used-by-process'}, '4242 (kvm)', 'a process with the map open'); + is($open{'3600map'}->{'used-by-guest'}, 100, 'that belongs to a virtual machine'); + ok(!exists($open{'3600plain'}->{'used-by-process'}), 'no process for the other LUN'); + my ($devices, %param) = @opener_query; + is_deeply( + [sort @$devices], + [qw(/dev/mapper/lun /dev/sdb /dev/sdc /dev/sdd /dev/sdf)], + 'asked once for all LUNs and their paths', + ); + ok($param{writable}, 'a process that only reads a LUN does not use it'); + { + local $openers->{'/dev/sdc'} = { pid => 4243, command => 'kvm' }; + local $openers->{'/dev/sdf'} = { pid => 4244, command => 'kvm' }; + my %path = map { $_->{wwid} => $_ } PVE::Storage::SAN::scan_luns($cfg)->@*; + is($path{'3600plain'}->{'used-by-process'}, '4243 (kvm)', 'a process with another path'); + is($path{'3600map'}->{'used-by-process'}, '4242 (kvm)', 'the map before its paths'); + } + my $schema = PVE::API2::Storage::Scan->map_method_by_name('sanlunscan')->{returns}; + eval { PVE::JSONSchema::validate([values %open], $schema) }; + is($@, '', 'rows with a process match the public schema'); + + $cgroup = '0::/system.slice/100.scope'; + ($open{'3600map'}) = grep { $_->{wwid} eq '3600map' } PVE::Storage::SAN::scan_luns($cfg)->@*; + is($open{'3600map'}->{'used-by-process'}, '4242 (kvm)', 'a process of no guest'); + ok(!exists($open{'3600map'}->{'used-by-guest'}), 'has no guest ID'); + $scan->unmock('file_read_firstline'); + $scan->mock(abs_path => sub { $_[0] }); +} ok(!exists($row{'3600plain'}->{state}), 'single path devices have no multipath health'); is($row{'3600map'}->{state}, 'degraded', 'final state counts usable paths'); is($row{'3600map'}->{multipath}, 'manual', 'explicit map');