From cb057e21c549d9377169c56c15e3e66586c8fa7c Mon Sep 17 00:00:00 2001 From: Fabian Ebner Date: Fri, 23 Apr 2021 12:14:54 +0200 Subject: [PATCH] diskmanage: add has_holder method Signed-off-by: Fabian Ebner --- PVE/Diskmanage.pm | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm index 70677ea..07bcef2 100644 --- a/PVE/Diskmanage.pm +++ b/PVE/Diskmanage.pm @@ -848,6 +848,30 @@ sub append_partition { return $partition; } +# Check if a disk or any of its partitions has a holder. +# Can also be called with a partition. +# Expected to be called with a result of verify_blockdev_path(). +sub has_holder { + my ($devpath) = @_; + + my $sysdir = "/sys/class/block/"; + + my $dev = $devpath; + $dev =~ s|^/dev/||; + + return $devpath if !dir_is_empty("${sysdir}/${dev}/holders"); + + my $found; + + dir_glob_foreach("/sys/block/${dev}", "${dev}.+", sub { + my ($part) = @_; + + $found = "/dev/${part}" if !dir_is_empty("${sysdir}/${part}/holders"); + }); + + return $found; +} + # Basic check if a disk or any of its partitions is mounted. # Can also be called with a partition. # Expected to be called with a result of verify_blockdev_path().