[pve-devel] [PATCH storage 07/14] Diskmanage: introduce usage helper

Fabian Ebner f.ebner at proxmox.com
Tue Jan 26 12:45:23 CET 2021


Note that this is a slight behavior change, because now the first
partition's usage which is not simply 'partition' will become the disk's
usage. Previously, if any partition was 'mounted', it would become the disk's
usage, then 'LVM', 'ZFS', etc.

A partitions usage defaults to 'partition' if nothing more specific can be
found, and is never treated as unused for now.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 PVE/Diskmanage.pm | 71 +++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 40 deletions(-)

diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm
index a06f8ae..32789f2 100644
--- a/PVE/Diskmanage.pm
+++ b/PVE/Diskmanage.pm
@@ -566,22 +566,6 @@ sub get_disks {
 	    };
 	}
 
-	my $used;
-
-	$used = 'LVM' if $lvmhash->{$devpath};
-
-	$used = 'mounted' if $mounted->{$devpath};
-
-	$used = 'ZFS' if $zfshash->{$devpath};
-
-	if (defined($lsblk_info->{$devpath})) {
-	    my $fstype = $lsblk_info->{$devpath}->{fstype};
-	    if (defined($fstype)) {
-		$used = $fstype;
-		$used .= ' (mounted)' if $mounted->{$devpath};
-	    }
-	}
-
 	# we replaced cciss/ with cciss! above
 	# but in the result we need cciss/ again
 	# because the caller might want to check the
@@ -615,10 +599,6 @@ sub get_disks {
 	my $db_count = 0;
 	my $wal_count = 0;
 
-	my $found_lvm;
-	my $found_mountpoints;
-	my $found_zfs;
-	my $found_dm;
 	my $partpath = $devpath;
 
 	# remove part after last / to
@@ -626,6 +606,28 @@ sub get_disks {
 	# e.g. from /dev/cciss/c0d0 get /dev/cciss
 	$partpath =~ s/\/[^\/]+$//;
 
+	my $determine_usage = sub {
+	    my ($devpath, $sysdir, $is_partition) = @_;
+
+	    return 'LVM' if $lvmhash->{$devpath};
+	    return 'ZFS' if $zfshash->{$devpath};
+
+	    my $info = $lsblk_info->{$devpath} // {};
+	    my $fstype = $info->{fstype};
+	    if (defined($fstype)) {
+		return "${fstype} (mounted)" if $mounted->{$devpath};
+		return "${fstype}";
+	    }
+	    return 'mounted' if $mounted->{$devpath};
+
+	    return if !$is_partition;
+
+	    # for devices, this check is done explicitly later
+	    return 'Device Mapper' if !dir_is_empty("$sysdir/holders");
+
+	    return 'partition';
+	};
+
 	my $partitions = {};
 
 	dir_glob_foreach("$sysdir", "$dev.+", sub {
@@ -635,32 +637,21 @@ sub get_disks {
 	    $partitions->{$part}->{gpt} = $data->{gpt};
 	    $partitions->{$part}->{size} =
 		get_sysdir_size("$sysdir/$part") // 0;
+	    $partitions->{$part}->{used} =
+		$determine_usage->("$partpath/$part", "$sysdir/$part", 1);
 
 	    if (my $mp = $mounted->{"$partpath/$part"}) {
-		$found_mountpoints = 1;
 		if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
 		    $osdid = $1;
 		}
 	    }
 
-	    if ($lvmhash->{"$partpath/$part"}) {
-		$found_lvm = 1;
-	    }
-
-	    if ($zfshash->{"$partpath/$part"}) {
-		$found_zfs = 1;
-	    }
-
 	    if (my $journal_part = $journalhash->{"$partpath/$part"}) {
 		$journal_count++ if $journal_part == 1;
 		$db_count++ if $journal_part == 2;
 		$wal_count++ if $journal_part == 3;
 		$bluestore = 1 if $journal_part == 4;
 	    }
-
-	    if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm)  {
-		$found_dm = 1;
-	    }
 	});
 
 	if (my $ceph_volume = $ceph_volume_infos->{$devpath}) {
@@ -674,16 +665,16 @@ sub get_disks {
 	    }
 	}
 
-	$used = 'mounted' if $found_mountpoints && !$used;
-	$used = 'LVM' if $found_lvm && !$used;
-	$used = 'ZFS' if $found_zfs && !$used;
-	$used = 'Device Mapper' if $found_dm && !$used;
-	$used = 'partitions' if scalar(keys %{$partitions}) && !$used;
-
+	my $used = $determine_usage->($devpath, $sysdir, 0);
+	foreach my $part (sort keys %{$partitions}) {
+	    next if $partitions->{$part}->{used} eq 'partition';
+	    $used //= $partitions->{$part}->{used};
+	}
+	$used //= 'partitions' if scalar(keys %{$partitions});
 	# multipath, software raid, etc.
 	# this check comes in last, to show more specific info
 	# if we have it
-	$used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders");
+	$used //= 'Device Mapper' if !dir_is_empty("$sysdir/holders");
 
 	$disklist->{$dev}->{used} = $used if $used;
 	$disklist->{$dev}->{osdid} = $osdid;
-- 
2.20.1





More information about the pve-devel mailing list