[pve-devel] [PATCH V2 pve-storage 1/2] Add new function part_num

Wolfgang Link w.link at proxmox.com
Mon Dec 19 15:15:36 CET 2016


With this function you get the partnum of a dev.
---
 PVE/Diskmanage.pm | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

Remove Module use File::stat.
Fix typo.
rewrite if condition.

diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm
index 5d498ce..e4821d4 100644
--- a/PVE/Diskmanage.pm
+++ b/PVE/Diskmanage.pm
@@ -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;
-- 
2.1.4





More information about the pve-devel mailing list