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

Wolfgang Link w.link at proxmox.com
Mon Dec 19 12:41:28 CET 2016


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

diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm
index 5d498ce..1171031 100644
--- a/PVE/Diskmanage.pm
+++ b/PVE/Diskmanage.pm
@@ -5,6 +5,8 @@ use warnings;
 use PVE::ProcFSTools;
 use Data::Dumper;
 use Cwd qw(abs_path);
+use File::stat;
+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 +518,34 @@ sub get_disks {
 
 }
 
+sub get_partnum {
+    my ($part_path) = @_;
+
+    my $stat = stat($part_path);
+    my $mode = $stat->mode;
+    my $rdev = $stat->rdev;
+
+    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;
+    if (-f "${partnum_path}partition") {
+	$partnum = file_read_firstline("${partnum_path}partition");
+
+	#untaint and ensure it is a int
+	if ($partnum =~ m/(\d+)/) {
+	    $partnum = $1;
+	    die "Part number: $partnum is not vailed\n" if $partnum > 128;
+	} else {
+	    die "Get no partnumber\n";
+	}
+    } else {
+	die "Unable to find correct Partition Number\n";
+    }
+
+    return $partnum;
+}
+
 1;
-- 
2.1.4





More information about the pve-devel mailing list