Add new function part_num

With this function you get the partnum of a dev.
This commit is contained in:
Wolfgang Link
2016-12-19 15:15:36 +01:00
committed by Dietmar Maurer
parent 3524217d28
commit 3196c387d2

View 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;