Skip to content

Commit 389adae

Browse files
committed
simplify cryptsetup status checks
Signed-off-by: Zen <[email protected]>
1 parent a42ccea commit 389adae

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/ugrd/crypto/cryptsetup.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = 'desultory'
2-
__version__ = '2.0.1'
2+
__version__ = '2.1.0'
33

44
from zenlib.util import check_dict
55

@@ -217,14 +217,14 @@ def open_crypt_device(self, name: str, parameters: dict) -> list[str]:
217217
self.logger.debug("[%s] Using key command: %s" % (name, parameters['key_command']))
218218
out_line, key_name = open_crypt_key(self, name, parameters)
219219
out += out_line
220-
cryptsetup_command = f' cryptsetup open --key-file {key_name}'
220+
cryptsetup_command = f'cryptsetup open --key-file {key_name}'
221221
elif 'key_file' in parameters:
222222
self.logger.debug("[%s] Using key file: %s" % (name, parameters['key_file']))
223223
_validate_crypysetup_key(self, parameters)
224-
cryptsetup_command = f' cryptsetup open --key-file {parameters["key_file"]}'
224+
cryptsetup_command = f'cryptsetup open --key-file {parameters["key_file"]}'
225225
else:
226226
# Set tries to 1 since it runs in the loop
227-
cryptsetup_command = ' cryptsetup open --tries 1'
227+
cryptsetup_command = 'cryptsetup open --tries 1'
228228

229229
# Add the header file if it exists
230230
if header_file := parameters.get('header_file'):
@@ -236,11 +236,10 @@ def open_crypt_device(self, name: str, parameters: dict) -> list[str]:
236236
self.logger.warning("Using --allow-discards can be a security risk.")
237237

238238
# Add the variable for the source device and mapped name
239-
cryptsetup_command += f' $CRYPTSETUP_SOURCE_{name} {name}'
240-
out += [cryptsetup_command]
239+
cryptsetup_command += f' "$CRYPTSETUP_SOURCE_{name}" {name}'
241240

242241
# Check if the device was successfully opened
243-
out += [' if [ $? -eq 0 ]; then',
242+
out += [f' if {cryptsetup_command}; then',
244243
f' einfo "Successfully opened device: {name}"',
245244
' break',
246245
' else',
@@ -263,8 +262,7 @@ def crypt_init(self) -> list[str]:
263262
out = [r'einfo "Unlocking LUKS volumes, ugrd.cryptsetup version: %s"' % __version__]
264263
for name, parameters in self['cryptsetup'].items():
265264
# Check if the volume is already open, if so, skip it
266-
out += [f'cryptsetup status {name} > /dev/null 2>&1',
267-
'if [ $? -eq 0 ]; then',
265+
out += [f'if cryptsetup status {name} > /dev/null 2>&1; then',
268266
f' ewarn "Device already open: {name}"',
269267
' return',
270268
'fi']
@@ -276,14 +274,12 @@ def crypt_init(self) -> list[str]:
276274
new_params.pop(parameter)
277275
except KeyError:
278276
pass
279-
out += [f'cryptsetup status {name}',
280-
'if [ $? -ne 0 ]; then',
277+
out += [f'if ! cryptsetup status {name} > /dev/null 2>&1; then',
281278
f' ewarn "Failed to open device using keys: {name}"']
282279
out += [f' {bash_line}' for bash_line in open_crypt_device(self, name, new_params)]
283280
out += ['fi']
284281
# Check that the device was successfully opened
285-
out += [f'cryptsetup status {name}',
286-
'if [ $? -ne 0 ]; then',
282+
out += [f'if ! cryptsetup status {name} > /dev/null 2>&1; then',
287283
f' rd_fail "Failed to open cryptsetup device: {name}"',
288284
'fi']
289285
return out

0 commit comments

Comments
 (0)