Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions storage_devices/disk.py
Original file line number Diff line number Diff line change
@@ -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
#

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions storage_devices/partition.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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]
47 changes: 47 additions & 0 deletions test_tools/disk_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Comment on lines +61 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove (redundant)


# 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]+'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cmd = f"fdisk -l --bytes {device_name}" + r"| grep -E '^/dev/[a-z]+[0-9]+'"
cmd = f"fdisk -l --bytes {device.path} | grep ^/dev/"

output = TestRun.executor.run(cmd).stdout.split("\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check here if stdout is empty (and return if so)


for partition in partition_list:
disk_params = [
x for x in next(param for param in output if partition in param).split(" ") if x
]
Comment on lines +83 to +85
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

part_params = re.split("\s+", line)

partition_number = int(re.search(r"(\d+)(?!.*\d)", disk_params[0]).group())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
partition_number = int(re.search(r"(\d+)(?!.*\d)", disk_params[0]).group())
partition_number = int(re.search(r"(\d+)$", disk_params[0]).group())


part_number = int(partition_number)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's already cast to int

begin = Size(int(disk_params[1]), Unit.Byte)
end = Size(
(int(disk_params[2])),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(int(disk_params[2])),
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe set device.partitions instead of returning the 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}'
Expand Down