Skip to content

Commit

Permalink
devices: extract get_parent_path(), use for manage_devices_file()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvo5 committed Aug 26, 2024
1 parent 7149f1e commit 59ea396
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
22 changes: 16 additions & 6 deletions devices/org.osbuild.lvm2.lv
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions devices/test/test_lv.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 59ea396

Please sign in to comment.