]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Diskmanage.pm
Diskmanage: refactor and rename get_parttype_info
[pve-storage.git] / PVE / Diskmanage.pm
index 2552addebcbea90c4d5d7152ca787344f222e89e..655a4b2ebd62a62de763bff25d067ed81cb522af 100644 (file)
@@ -17,7 +17,6 @@ my $ZPOOL = "/sbin/zpool";
 my $SGDISK = "/sbin/sgdisk";
 my $PVS = "/sbin/pvs";
 my $LVS = "/sbin/lvs";
-my $UDEVADM = "/bin/udevadm";
 my $LSBLK = "/bin/lsblk";
 
 sub verify_blockdev_path {
@@ -134,8 +133,8 @@ sub get_smart_data {
            } elsif (defined($type) && $type eq 'text') {
                $smartdata->{text} = '' if !defined $smartdata->{text};
                $smartdata->{text} .= "$line\n";
-               # extract wearout from nvme text, allow for decimal values
-               if ($line =~ m/Percentage Used:\s*(\d+(?:\.\d+)?)\%/i) {
+               # extract wearout from nvme/sas text, allow for decimal values
+               if ($line =~ m/Percentage Used(?: endurance indicator)?:\s*(\d+(?:\.\d+)?)\%/i) {
                    $smartdata->{wearout} = 100 - $1;
                }
            } elsif ($line =~ m/SMART Disabled/) {
@@ -157,7 +156,7 @@ sub get_smart_data {
     return $smartdata;
 }
 
-sub get_parttype_info() {
+sub get_lsblk_info() {
     my $cmd = [$LSBLK, '--json', '-o', 'path,parttype'];
     my $output = "";
     my $res = {};
@@ -174,30 +173,29 @@ sub get_parttype_info() {
     warn "$@\n" if $@;
     my $list = $parsed->{blockdevices} // [];
 
-    foreach my $dev (@$list) {
-       next if !($dev->{parttype});
-       my $type = $dev->{parttype};
-       $res->{$type} = [] if !defined($res->{$type});
-       push @{$res->{$type}}, $dev->{path};
-    }
+    $res = { map {
+       $_->{path} => { parttype => $_->{parttype} }
+    } @{$list} };
 
     return $res;
 }
 
 my $get_devices_by_partuuid = sub {
-    my ($parttype_map, $uuids, $res) = @_;
+    my ($lsblk_info, $uuids, $res) = @_;
 
     $res = {} if !defined($res);
 
-    foreach my $uuid (sort keys %$uuids) {
-       map { $res->{$_} = $uuids->{$uuid} } @{$parttype_map->{$uuid}};
+    foreach my $dev (sort keys %{$lsblk_info}) {
+       my $uuid = $lsblk_info->{$dev}->{parttype};
+       next if !defined($uuid) || !defined($uuids->{$uuid});
+       $res->{$dev} = $uuids->{$uuid};
     }
 
     return $res;
 };
 
 sub get_zfs_devices {
-    my ($parttype_map) = @_;
+    my ($lsblk_info) = @_;
     my $res = {};
 
     return {} if ! -x $ZPOOL;
@@ -225,13 +223,13 @@ sub get_zfs_devices {
     };
 
 
-    $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res);
+    $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res);
 
     return $res;
 }
 
 sub get_lvm_devices {
-    my ($parttype_map) = @_;
+    my ($lsblk_info) = @_;
     my $res = {};
     eval {
        run_command([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc => sub{
@@ -251,13 +249,13 @@ sub get_lvm_devices {
        "e6d6d379-f507-44c2-a23c-238f2a3df928" => 1,
     };
 
-    $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res);
+    $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res);
 
     return $res;
 }
 
 sub get_ceph_journals {
-    my ($parttype_map) = @_;
+    my ($lsblk_info) = @_;
     my $res = {};
 
     my $uuids = {
@@ -267,7 +265,7 @@ sub get_ceph_journals {
        'cafecafe-9b03-4f30-b4c6-b4b80ceff106' => 4, # block
     };
 
-    $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res);
+    $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res);
 
     return $res;
 }
@@ -312,7 +310,7 @@ sub get_udev_info {
     my $info = "";
     my $data = {};
     eval {
-       run_command([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc => sub {
+       run_command(['udevadm', 'info', '-p', $dev, '--query', 'all'], outfunc => sub {
            my ($line) = @_;
            $info .= "$line\n";
        });
@@ -480,19 +478,14 @@ sub get_disks {
        $mounted->{abs_path($mount->[0])} = $mount->[1];
     };
 
-    my $dev_is_mounted = sub {
-       my ($dev) = @_;
-       return $mounted->{$dev};
-    };
+    my $lsblk_info = get_lsblk_info();
 
-    my $parttype_map = get_parttype_info();
-
-    my $journalhash = get_ceph_journals($parttype_map);
+    my $journalhash = get_ceph_journals($lsblk_info);
     my $ceph_volume_infos = get_ceph_volume_infos();
 
-    my $zfshash = get_zfs_devices($parttype_map);
+    my $zfshash = get_zfs_devices($lsblk_info);
 
-    my $lvmhash = get_lvm_devices($parttype_map);
+    my $lvmhash = get_lvm_devices($lsblk_info);
 
     my $disk_regex = ".*";
     if (defined($disks)) {
@@ -502,7 +495,7 @@ sub get_disks {
            die "disks is not a string or array reference\n";
        }
        # we get cciss/c0d0 but need cciss!c0d0
-       map { s|cciss/|cciss!| } @$disks;
+       $_ =~ s|cciss/|cciss!| for @$disks;
 
        $disk_regex = "(?:" . join('|', @$disks) . ")";
     }
@@ -567,7 +560,7 @@ sub get_disks {
 
        $used = 'LVM' if $lvmhash->{$devpath};
 
-       $used = 'mounted' if &$dev_is_mounted($devpath);
+       $used = 'mounted' if $mounted->{$devpath};
 
        $used = 'ZFS' if $zfshash->{$devpath};
 
@@ -621,7 +614,7 @@ sub get_disks {
 
            $found_partitions = 1;
 
-           if (my $mp = &$dev_is_mounted("$partpath/$part")) {
+           if (my $mp = $mounted->{"$partpath/$part"}) {
                $found_mountpoints = 1;
                if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
                    $osdid = $1;
@@ -713,9 +706,12 @@ sub get_partnum {
 sub get_blockdev {
     my ($part_path) = @_;
 
-    my $dev = $1 if $part_path =~ m|^/dev/(.*)$|;
-    my $link = readlink "/sys/class/block/$dev";
-    my $block_dev = $1 if $link =~ m|([^/]*)/$dev$|;
+    my ($dev, $block_dev);
+    if ($part_path =~ m|^/dev/(.*)$|) {
+       $dev = $1;
+       my $link = readlink "/sys/class/block/$dev";
+       $block_dev = $1 if $link =~ m|([^/]*)/$dev$|;
+    }
 
     die "Can't parse parent device\n" if !defined($block_dev);
     die "No valid block device\n" if index($dev, $block_dev) == -1;