From 547939e45e9a2a7ae152a228a7f0f89e6af70a09 Mon Sep 17 00:00:00 2001 From: Swati Sehgal Date: Wed, 13 Mar 2024 13:48:37 +0000 Subject: [PATCH] Add RelocatableSysFS IsBlockDeviceHidden Based on cadvisor code: https://github.com/google/cadvisor/blob/v0.48.1/utils/sysfs/sysfs.go#L218-L236 Signed-off-by: Swati Sehgal --- pkg/machineinformer/relocatablesysfs.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/machineinformer/relocatablesysfs.go b/pkg/machineinformer/relocatablesysfs.go index e70aec95..f287e7a4 100644 --- a/pkg/machineinformer/relocatablesysfs.go +++ b/pkg/machineinformer/relocatablesysfs.go @@ -61,6 +61,7 @@ const ( nodeDirPattern = "node*[0-9]" nodeDistance = "distance" + hidden = "hidden" //HugePagesNrFile name of nr_hugepages file in sysfs HugePagesNrFile = "nr_hugepages" @@ -93,6 +94,27 @@ func (fs RelocatableSysFs) GetDistances(nodePath string) (string, error) { return strings.TrimSpace(string(nodeDistances)), err } +// See: https://github.com/google/cadvisor/blob/v0.48.1/utils/sysfs/sysfs.go#L218-L236 +func (fs RelocatableSysFs) IsBlockDeviceHidden(name string) (bool, error) { + // See: https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-block + // https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git + // - c8487d854ba5 ("lsblk: Ignore hidden devices") + path := filepath.Join(fs.root, blockDir, name, hidden) + hidden, err := os.ReadFile(path) + if err != nil && os.IsNotExist(err) { + // older OS may not have /hidden sysfs entry, so for sure + // it is not a hidden device... + return false, nil + } + if err != nil { + return false, fmt.Errorf("failed to read %s: %w", path, err) + } + if string(hidden) == "1" { + return true, nil + } + return false, nil +} + func (fs RelocatableSysFs) GetNodesPaths() ([]string, error) { pathPattern := filepath.Join(fs.root, nodeDir, nodeDirPattern) return filepath.Glob(pathPattern)