devices: extract get_parent_path(), use for manage_devices_file()

This commit extract a helper `get_parent_path()` that is unit
tested and also uses this generated parent_path for the call
to manage_devices_file to be consistent with the exiting behavior
of only including the device that actually contains the VG.
This commit is contained in:
Michael Vogt 2024-08-26 09:38:34 +02:00 committed by Achilleas Koutsou
parent 0695911d5c
commit a2fed3e0d8
2 changed files with 28 additions and 6 deletions

View file

@ -53,6 +53,20 @@ SCHEMA = """
"""
def get_parent_path(parent: str, options: Dict) -> str:
""" get_parent_path returns the full path to the block device for parent
Note that the options can influence the behavior via the vg partition
number option.
"""
assert not parent.startswith("/")
parent_path = os.path.join("/dev", parent)
part = options.get("vg_partnum")
if part:
parent_path += f"p{part}"
return parent_path
class LVService(devices.DeviceService):
def __init__(self, args):
@ -175,20 +189,16 @@ class LVService(devices.DeviceService):
return major, minor
def open(self, devpath: str, parent: str, tree: str, options: Dict):
def open(self, devpath: str, parent: str, tree: str, options: Dict) -> Dict:
lv = options["volume"]
assert not parent.startswith("/")
parent_path = os.path.join("/dev", parent)
parent_path = get_parent_path(parent, options)
# Add the device to a lvm devices file on supported systems
self.manage_devices_file(parent_path)
# Find the volume group that belongs to the device specified
# via `parent`
part = options.get("vg_partnum")
if part:
parent_path += f"p{part}"
vg = self.volume_group_for_device(parent_path)
# Activate the logical volume

12
devices/test/test_lv.py Normal file
View file

@ -0,0 +1,12 @@
import pytest
DEVICES_NAME = "org.osbuild.lvm2.lv"
@pytest.mark.parametrize("parent,options,expected_parent_path", [
("loop2", {}, "/dev/loop2"),
("loop1", {"vg_partnum": 2}, "/dev/loop1p2"),
])
def test_lvm2_lv_get_parent_path(devices_module, parent, options, expected_parent_path):
pp = devices_module.get_parent_path(parent, options)
assert pp == expected_parent_path