diff --git a/storage_devices/disk.py b/storage_devices/disk.py index 8f3a179..afe3800 100644 --- a/storage_devices/disk.py +++ b/storage_devices/disk.py @@ -1,6 +1,6 @@ # # Copyright(c) 2019-2022 Intel Corporation -# Copyright(c) 2024 Huawei Technologies Co., Ltd. +# Copyright(c) 2024-2025 Huawei Technologies Co., Ltd. # SPDX-License-Identifier: BSD-3-Clause # @@ -118,7 +118,7 @@ def __init__( self.serial_number = serial_number self.block_size = Unit(block_size) self.device_id = self.get_device_id() - self.partitions = [] + self.partitions = self.discover_partitions() self.pci_address = None @classmethod @@ -139,6 +139,9 @@ def resolve_type(cls, disk_path): ) return recognized_types[0] + def discover_partitions(self) -> list: + return disk_tools.discover_partition(self) + def create_partitions(self, sizes: [], partition_table_type=PartitionTable.gpt): disk_tools.create_partitions(self, sizes, partition_table_type) @@ -154,7 +157,7 @@ def umount_all_partitions(self): def remove_partitions(self): for part in self.partitions: - if is_mounted(part.path): + if is_mounted(part.device_id): part.unmount() if disk_tools.remove_partitions(self): self.partitions.clear() diff --git a/storage_devices/partition.py b/storage_devices/partition.py index fd83ace..ff4278b 100644 --- a/storage_devices/partition.py +++ b/storage_devices/partition.py @@ -1,10 +1,12 @@ # # Copyright(c) 2019-2021 Intel Corporation +# Copyright(c) 2025 Huawei Technologies Co., Ltd. # SPDX-License-Identifier: BSD-3-Clause # from storage_devices.device import Device from test_tools.disk_tools import get_partition_path +from test_tools.fs_tools import readlink from type_def.size import Size @@ -16,7 +18,11 @@ def __init__(self, parent_dev, type, number, begin: Size, end: Size): self.type = type self.begin = begin self.end = end + self.device_id = self.get_device_id() def __str__(self): return f"\tsystem path: {self.path}, size: {self.size}, type: {self.type}, " \ f"parent device: {self.parent_device.path}\n" + + def get_device_id(self): + return readlink(self.path).split('/')[-1] diff --git a/test_tools/disk_tools.py b/test_tools/disk_tools.py index 5588d05..879a025 100644 --- a/test_tools/disk_tools.py +++ b/test_tools/disk_tools.py @@ -57,6 +57,53 @@ def get_partition_path(parent_dev, number): return f'{parent_dev}{id_separator}{number}' +def discover_partition(device) -> list: + cmd = ( + f"lsblk -ln -o NAME,TYPE {device.path} " + r"""| awk '$2 == "part" { print "/dev/" $1 }'""" + ) + + output = TestRun.executor.run(cmd).stdout + partition_list = [line for line in output.split("\n") if line] + if not partition_list: + return [] + + device_name = re.search(r"^(/dev/\S+)(?=\d+)", partition_list[0]).group() + if "nvme" in partition_list[0]: + device_name = device_name[:-1] + + # needs to be placed here (circular dependency) + from storage_devices.partition import Partition + + partitions_on_device_list = [] + + cmd = f"fdisk -l --bytes {device_name}" + r"| grep -E '^/dev/[a-z]+[0-9]+'" + output = TestRun.executor.run(cmd).stdout.split("\n") + + for partition in partition_list: + disk_params = [ + x for x in next(param for param in output if partition in param).split(" ") if x + ] + partition_number = int(re.search(r"(\d+)(?!.*\d)", disk_params[0]).group()) + + part_number = int(partition_number) + begin = Size(int(disk_params[1]), Unit.Byte) + end = Size( + (int(disk_params[2])), + Unit.Byte, + ) + partitions_on_device_list.append( + Partition( + parent_dev=device, + type=PartitionType.logical, + number=part_number, + begin=begin, + end=end, + ) + ) + + return partitions_on_device_list + + def remove_parition(device, part_number): TestRun.LOGGER.info(f"Removing part {part_number} from {device.path}") cmd = f'parted --script {device.path} rm {part_number}'