]> git.proxmox.com Git - pve-storage.git/commitdiff
Diskmanage: introduce usage helper
authorFabian Ebner <f.ebner@proxmox.com>
Tue, 26 Jan 2021 11:45:23 +0000 (12:45 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Sat, 6 Feb 2021 12:52:20 +0000 (13:52 +0100)
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@proxmox.com>
PVE/Diskmanage.pm

index fbdd3304d6793ad57b134d80361d6c6aec3c7e76..431316122bf0078ffaeb3c754a4fa5d117221f5d 100644 (file)
@@ -565,22 +565,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
@@ -614,10 +598,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
@@ -625,6 +605,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 {
@@ -634,32 +636,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}) {
@@ -673,16 +664,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;