Skip to content

Commit b930a50

Browse files
author
Kamil Gierszewski
committed
test-framework: add detection for partitions on devices.
Signed-off-by: Kamil Gierszewski <kamil.gierszewski@huawei.com>
1 parent 1d75896 commit b930a50

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

storage_devices/disk.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# Copyright(c) 2019-2022 Intel Corporation
3-
# Copyright(c) 2024 Huawei Technologies Co., Ltd.
3+
# Copyright(c) 2024-2025 Huawei Technologies Co., Ltd.
44
# SPDX-License-Identifier: BSD-3-Clause
55
#
66

@@ -118,7 +118,7 @@ def __init__(
118118
self.serial_number = serial_number
119119
self.block_size = Unit(block_size)
120120
self.device_id = self.get_device_id()
121-
self.partitions = []
121+
self.partitions = self.discover_partitions()
122122
self.pci_address = None
123123

124124
@classmethod
@@ -139,6 +139,9 @@ def resolve_type(cls, disk_path):
139139
)
140140
return recognized_types[0]
141141

142+
def discover_partitions(self) -> list:
143+
return disk_tools.discover_partition(self)
144+
142145
def create_partitions(self, sizes: [], partition_table_type=PartitionTable.gpt):
143146
disk_tools.create_partitions(self, sizes, partition_table_type)
144147

@@ -154,7 +157,7 @@ def umount_all_partitions(self):
154157

155158
def remove_partitions(self):
156159
for part in self.partitions:
157-
if is_mounted(part.path):
160+
if is_mounted(part.device_id):
158161
part.unmount()
159162
if disk_tools.remove_partitions(self):
160163
self.partitions.clear()

storage_devices/partition.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#
22
# Copyright(c) 2019-2021 Intel Corporation
3+
# Copyright(c) 2025 Huawei Technologies Co., Ltd.
34
# SPDX-License-Identifier: BSD-3-Clause
45
#
56

67
from storage_devices.device import Device
78
from test_tools.disk_tools import get_partition_path
9+
from test_tools.fs_tools import readlink
810
from type_def.size import Size
911

1012

@@ -16,7 +18,11 @@ def __init__(self, parent_dev, type, number, begin: Size, end: Size):
1618
self.type = type
1719
self.begin = begin
1820
self.end = end
21+
self.device_id = self.get_device_id()
1922

2023
def __str__(self):
2124
return f"\tsystem path: {self.path}, size: {self.size}, type: {self.type}, " \
2225
f"parent device: {self.parent_device.path}\n"
26+
27+
def get_device_id(self):
28+
return readlink(self.path).split('/')[-1]

test_tools/disk_tools.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,57 @@ def get_partition_path(parent_dev, number):
5757
return f'{parent_dev}{id_separator}{number}'
5858

5959

60+
def discover_partition(device) -> list:
61+
cmd = (
62+
f"lsblk -ln -o NAME,TYPE {device.path} "
63+
+ r"""| awk '$2 == "part" { print "/dev/" $1 }'"""
64+
)
65+
66+
output = TestRun.executor.run(cmd).stdout
67+
partition_list = [line for line in output.split("\n") if line]
68+
69+
# needs to be placed here (circular dependency)
70+
from storage_devices.partition import Partition
71+
partitions_on_device_list = []
72+
73+
# the only tool "that I found out" that returns a value for the partition number field when the
74+
# partition is mounted is udevadm.
75+
for partition in partition_list:
76+
cmd = f"lsblk -n --bytes -o START,SIZE,LOG-SEC {partition}"
77+
output = TestRun.executor.run(cmd)
78+
if output.exit_code != 0:
79+
TestRun.LOGGER.warning(
80+
"Warning: Partition detection failed. "
81+
"The issue might be caused by an outdated version of `lsblk`. "
82+
"Please ensure you are using a version that supports the required "
83+
"features (version 2.34 or higher).")
84+
return []
85+
86+
cmd = f"udevadm info {partition} " + "-q property | grep PARTN= | awk -F= '{print $2}'"
87+
partition_number = int(TestRun.executor.run(cmd).stdout)
88+
89+
partition_start, partition_size, partition_leg_sec = output.stdout.split()
90+
part_number = int(partition_number)
91+
begin = Size(int(partition_start), Unit.Byte)
92+
end = Size(
93+
(
94+
(int(partition_size) / int(partition_leg_sec) + int(partition_start) - 1)
95+
),
96+
Unit.Byte,
97+
)
98+
partitions_on_device_list.append(
99+
Partition(
100+
parent_dev=device,
101+
type=PartitionType.logical,
102+
number=part_number,
103+
begin=begin,
104+
end=end,
105+
)
106+
)
107+
108+
return partitions_on_device_list
109+
110+
60111
def remove_parition(device, part_number):
61112
TestRun.LOGGER.info(f"Removing part {part_number} from {device.path}")
62113
cmd = f'parted --script {device.path} rm {part_number}'

0 commit comments

Comments
 (0)