]> git.proxmox.com Git - pve-storage.git/commitdiff
Add new function part_num
authorWolfgang Link <w.link@proxmox.com>
Mon, 19 Dec 2016 14:15:36 +0000 (15:15 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 22 Dec 2016 11:12:47 +0000 (12:12 +0100)
With this function you get the partnum of a dev.

PVE/Diskmanage.pm

index 5d498ce4f801e62b799a54d0ab19b98282de564c..e4821d49f16e8235efb21be5ed8f5cf82b3f83ef 100644 (file)
@@ -5,6 +5,7 @@ use warnings;
 use PVE::ProcFSTools;
 use Data::Dumper;
 use Cwd qw(abs_path);
+use Fcntl ':mode';
 
 use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
 
@@ -516,4 +517,31 @@ sub get_disks {
 
 }
 
+sub get_partnum {
+    my ($part_path) = @_;
+
+    my ($mode, $rdev) = (stat($part_path))[2,6];
+
+    next if !$mode || !S_ISBLK($mode) || !$rdev;
+    my $major = int($rdev / 0x100);
+    my $minor = $rdev % 0x100;
+    my $partnum_path = "/sys/dev/block/$major:$minor/";
+
+    my $partnum;
+
+    $partnum = file_read_firstline("${partnum_path}partition");
+
+    die "Partition does not exists\n" if !defined($partnum);
+
+    #untaint and ensure it is a int
+    if ($partnum =~ m/(\d+)/) {
+       $partnum = $1;
+       die "Partition number $partnum is invalid\n" if $partnum > 128;
+    } else {
+       die "Failed to get partition number\n";
+    }
+
+    return $partnum;
+}
+
 1;