all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
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	[thread overview]
Message-ID: <20261118065302.3033937-26-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261118065302.3033937-1-t.lamprecht@proxmox.com>

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 <t.lamprecht@proxmox.com>
---
 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');




  parent reply	other threads:[~2026-11-18  6:57 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  6:52 [PATCH v6 cluster,storage,docs,manager 00/42] SAN: cluster-wide multipath, SAN views and a guided setup for shared LVM Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 cluster 01/42] cfs: observe the multipath configuration files Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 02/42] diskmanage: collect disk transport type from lsblk Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 03/42] diskmanage: qualify NVMe over fabrics transport Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 04/42] diskmanage: optionally include iSCSI session devices in disk enumeration Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 05/42] diskmanage: collect native NVMe multipath path state Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 06/42] iscsi: factor out session device map from device list Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 07/42] multipath: add a library around multipathd Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 08/42] diskmanage: add helper to list multipath devices Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 09/42] multipath: add a cluster-wide configuration Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 10/42] iscsi: add an option to manage storage LUNs with multipath Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 11/42] lvm: activate a missing base volume before using the volume group Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 12/42] multipath: apply the configuration on every node Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 13/42] multipath: publish per-node status Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 14/42] api: add endpoints for the cluster-wide multipath configuration Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 15/42] api: multipath: add the cluster-wide health status Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 16/42] diskmanage: look up the requested disks by name Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 17/42] diskmanage: read the udev data of all disks with one call Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 18/42] api: scan: add a SAN LUN inventory Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 19/42] pvesm: expose the " Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 20/42] api: add node-level iSCSI status Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 21/42] disks: lvm: drop unused imports Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 22/42] disks: lvm: allow volume groups on SAN LUNs and multipath maps Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 23/42] disks: lvm: refuse to destroy the volume group of a shared storage Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 storage 24/42] disks: allow wiping a SAN LUN that no storage uses Thomas Lamprecht
2026-11-18  6:52 ` Thomas Lamprecht [this message]
2026-11-18  6:52 ` [PATCH v6 docs 26/42] storage: document the cluster-wide multipath management Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 docs 27/42] storage: multipath: describe the use shown for an open LUN Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 28/42] pvestatd: apply the multipath configuration and report the node status Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 29/42] report: add the managed multipath configuration Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 30/42] api: cluster: add the multipath endpoints Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 31/42] api: nodes: expose the iSCSI status Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 32/42] ui: dc: add a SAN view Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 33/42] ui: storage: iscsi: add the multipath option Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 34/42] ui: lvm: enable shared when a base storage is selected Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 35/42] ui: node: add SAN LUNs panel Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 36/42] ui: node: san: wipe a LUN that no storage uses Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 37/42] ui: node: san: show iSCSI targets and sessions Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 38/42] ui: dc: san: add a guided setup for shared LVM on iSCSI Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 39/42] ui: dc: san: add a guided setup for shared LVM on Fibre Channel Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 40/42] ui: storage: point the iSCSI and LVM add dialogs to the SAN view Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 41/42] ui: san: show the guest or process that has a LUN open Thomas Lamprecht
2026-11-18  6:52 ` [PATCH v6 manager 42/42] ui: node: san: add a column for the iSCSI target of a LUN Thomas Lamprecht
2026-11-18 20:28 ` superseded: [PATCH v6 cluster,storage,docs,manager 00/42] SAN: cluster-wide multipath, SAN views and a guided setup for shared LVM Thomas Lamprecht

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=20261118065302.3033937-26-t.lamprecht@proxmox.com \
    --to=t.lamprecht@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal