From 9ab9e3b4e06bf663efba0c322e8738c99cff2dad Mon Sep 17 00:00:00 2001 From: Robert Baldyga Date: Thu, 2 Jul 2026 14:55:57 +0200 Subject: [PATCH] Add support for /dev/disk/by-path/ devices Signed-off-by: Robert Baldyga --- test_tools/disk_finder.py | 25 +++++++++++++++---------- test_tools/disk_tools.py | 11 +++++++++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/test_tools/disk_finder.py b/test_tools/disk_finder.py index 16bc330..fcefcee 100644 --- a/test_tools/disk_finder.py +++ b/test_tools/disk_finder.py @@ -1,6 +1,7 @@ # # Copyright(c) 2019-2021 Intel Corporation # Copyright(c) 2024 Huawei Technologies Co., Ltd. +# Copyright(c) 2026 Unvertical # SPDX-License-Identifier: BSD-3-Clause # import os @@ -141,17 +142,21 @@ def __get_slaves(device_name: str): def resolve_to_by_id_link(path): - by_id_paths = TestRun.executor.run_expect_success("ls /dev/disk/by-id -1").stdout.splitlines() - dev_full_paths = [ - posixpath.join("/dev/disk/by-id", by_id_path) for by_id_path in by_id_paths - ] + link_dirs = ["/dev/disk/by-id", "/dev/disk/by-path"] + dev_target = readlink(posixpath.join("/dev", path)) - for full_path in dev_full_paths: - # handle exception for broken links + for link_dir in link_dirs: try: - if readlink(full_path) == readlink(posixpath.join("/dev", path)): - return full_path + links = TestRun.executor.run_expect_success(f"ls {link_dir} -1").stdout.splitlines() except CmdException: - continue + continue # directory may not exist if there are no links of this type + for link in links: + full_path = posixpath.join(link_dir, link) + # handle exception for broken links + try: + if readlink(full_path) == dev_target: + return full_path + except CmdException: + continue - raise ValueError(f'By-id device link not found for device {path}') + raise ValueError(f'By-id or by-path device link not found for device {path}') diff --git a/test_tools/disk_tools.py b/test_tools/disk_tools.py index b5967d6..dc2f370 100644 --- a/test_tools/disk_tools.py +++ b/test_tools/disk_tools.py @@ -314,6 +314,13 @@ def _is_by_id_path(path: str): return path in [posixpath.join(dev_by_id_dir, id_path.full_path) for id_path in by_id_paths] +def _is_by_path_path(path: str): + """check if given path already is proper by-path path""" + dev_by_path_dir = "/dev/disk/by-path" + by_path_paths = parse_ls_output(ls(dev_by_path_dir), dev_by_path_dir) + return path in [posixpath.join(dev_by_path_dir, p.full_path) for p in by_path_paths] + + def _is_dev_path_whitelisted(path: str): """check if given path is whitelisted""" whitelisted_paths = [ @@ -338,10 +345,10 @@ def validate_dev_path(path: str): if _is_dev_path_whitelisted(path): return path - if _is_by_id_path(path): + if _is_by_id_path(path) or _is_by_path_path(path): return path - raise ValueError(f'By-id device link {path} is broken.') + raise ValueError(f'Device link {path} is not a valid by-id or by-path link.') def get_block_device_names_list(exclude_list: List[int] = None) -> List[str]: