From 3196c387d2e5d62a82676585f09dc505f77f61f0 Mon Sep 17 00:00:00 2001 From: Wolfgang Link Date: Mon, 19 Dec 2016 15:15:36 +0100 Subject: [PATCH] Add new function part_num With this function you get the partnum of a dev. --- PVE/Diskmanage.pm | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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.39.2