Skip to content

Commit 1f92632

Browse files
authored
Merge pull request #200 from desultory/dev
improve dm source detection, allow cryptsetup device validation toggle
2 parents d7e4668 + bafdc16 commit 1f92632

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/ugrd/crypto/cryptsetup.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "desultory"
2-
__version__ = "4.0.2"
2+
__version__ = "4.0.3"
33

44
from pathlib import Path
55

@@ -145,12 +145,18 @@ def _get_dm_info(self, mapped_name: str) -> dict:
145145
def _get_dm_slave_info(self, device_info: dict) -> (str, dict):
146146
"""Gets the device mapper slave information for a particular device."""
147147
slave_source = device_info["slaves"][0]
148-
try:
149-
slave_device = f"/dev/{slave_source}"
150-
return slave_device, self["_blkid_info"][slave_device]
151-
except KeyError:
152-
slave_device = f"/dev/mapper/{slave_source}"
153-
return slave_device, self["_blkid_info"][slave_device]
148+
slave_name = self["_vblk_info"].get(slave_source, {}).get("name")
149+
search_paths = ["/dev/", "/dev/mapper/"]
150+
151+
for path in search_paths:
152+
slave_device = path + slave_source
153+
if slave_device in self["_blkid_info"]:
154+
return slave_device, self["_blkid_info"][slave_device]
155+
if slave_name:
156+
slave_device = path + slave_name
157+
if slave_device in self["_blkid_info"]:
158+
return slave_device, self["_blkid_info"][slave_device]
159+
raise AutodetectError("No slave device information found for: %s" % device_info)
154160

155161

156162
def _read_cryptsetup_header(self, mapped_name: str, slave_device: str = None) -> dict:
@@ -253,7 +259,7 @@ def _validate_cryptsetup_header(self, mapped_name: str) -> None:
253259
self["check_included_or_mounted"] = cryptsetup_info["header_file"] # Add the header file to the check list
254260

255261

256-
@contains("hostonly", "Skipping cryptsetup device check.", log_level=30)
262+
@contains("validate", "Skipping cryptsetup device check.", log_level=30)
257263
def _validate_cryptsetup_device(self, mapped_name) -> None:
258264
"""Validates a cryptsetup device against the device mapper information,
259265
blkid information, and cryptsetup information.
@@ -450,7 +456,7 @@ def open_crypt_dev(self) -> str:
450456
The second argument is a key file, if it exists.
451457
This key file may be a named pipe, previously created by the key_command.
452458
"""
453-
out = """
459+
out = """
454460
crypt_device="$(get_crypt_dev "$1")"
455461
header="$(readvar CRYPTSETUP_HEADER_"$1")"
456462
@@ -469,11 +475,14 @@ def open_crypt_dev(self) -> str:
469475
"""
470476
if self["cryptsetup_trim"]:
471477
out += 'cryptsetup_args="$cryptsetup_args --allow-discards"'
472-
return out + """
478+
return (
479+
out
480+
+ """
473481
cryptsetup_args="$cryptsetup_args $crypt_device $1"
474482
edebug "[$1] cryptsetup args: $cryptsetup_args"
475483
$cryptsetup_args
476484
"""
485+
)
477486

478487

479488
def _open_crypt_dev(self, name: str, parameters: dict) -> list[str]:

0 commit comments

Comments
 (0)