disks: factor out stripping of /dev and cleanup vicinity

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht
2021-06-02 13:10:10 +02:00
parent 2829e6a853
commit 70dc70984a

View File

@ -22,7 +22,6 @@ my $LSBLK = "/bin/lsblk";
sub check_bin { sub check_bin {
my ($path) = @_; my ($path) = @_;
return -x $path; return -x $path;
} }
@ -848,25 +847,26 @@ sub append_partition {
return $partition; return $partition;
} }
my sub strip_dev :prototype($) {
my ($devpath) = @_;
$devpath =~ s|^/dev/||;
return $devpath;
}
# Check if a disk or any of its partitions has a holder. # Check if a disk or any of its partitions has a holder.
# Can also be called with a partition. # Can also be called with a partition.
# Expected to be called with a result of verify_blockdev_path(). # Expected to be called with a result of verify_blockdev_path().
sub has_holder { sub has_holder {
my ($devpath) = @_; my ($devpath) = @_;
my $sysdir = "/sys/class/block/"; my $dev = strip_dev($devpath);
my $dev = $devpath; return $devpath if !dir_is_empty("/sys/class/block/${dev}/holders");
$dev =~ s|^/dev/||;
return $devpath if !dir_is_empty("${sysdir}/${dev}/holders");
my $found; my $found;
dir_glob_foreach("/sys/block/${dev}", "${dev}.+", sub { dir_glob_foreach("/sys/block/${dev}", "${dev}.+", sub {
my ($part) = @_; my ($part) = @_;
$found = "/dev/${part}" if !dir_is_empty("/sys/class/block/${part}/holders");
$found = "/dev/${part}" if !dir_is_empty("${sysdir}/${part}/holders");
}); });
return $found; return $found;
@ -882,14 +882,11 @@ sub is_mounted {
return $devpath if $mounted->{$devpath}; return $devpath if $mounted->{$devpath};
my $dev = $devpath; my $dev = strip_dev($devpath);
$dev =~ s|^/dev/||;
my $found; my $found;
dir_glob_foreach("/sys/block/${dev}", "${dev}.+", sub { dir_glob_foreach("/sys/block/${dev}", "${dev}.+", sub {
my ($part) = @_; my ($part) = @_;
my $partpath = "/dev/${part}"; my $partpath = "/dev/${part}";
$found = $partpath if $mounted->{$partpath}; $found = $partpath if $mounted->{$partpath};