Skip to content

Commit 21c42db

Browse files
committed
Merge the crypttab systemd reload fix into the closed LUKS branch
Include #688 so the role's crypttab entry is visible to systemd before #686 inspects the generated unit. Keep the original reload commit in the history of both branches. Assisted-by: OpenCode using model GPT-6.2 Astra Signed-off-by: Bata Roland Krisztián <batarolandkrisztian@gmail.com>
2 parents a527ac0 + 0f52571 commit 21c42db

3 files changed

Lines changed: 78 additions & 5 deletions

File tree

‎tasks/main-blivet.yml‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,6 @@
245245
loop_control:
246246
loop_var: mount_info
247247

248-
- name: Tell systemd to refresh its view of /etc/fstab
249-
systemd:
250-
daemon_reload: true
251-
when: blivet_output['mounts'] | length > 0
252-
253248
#
254249
# Manage /etc/crypttab
255250
#
@@ -258,6 +253,7 @@
258253
path: /etc/crypttab
259254
register: __storage_crypttab
260255

256+
# Assisted by OpenCode using model GPT-6.1 Sol
261257
- name: Manage /etc/crypttab to account for changes we just made
262258
lineinfile:
263259
path: /etc/crypttab
@@ -274,6 +270,12 @@
274270
loop: "{{ blivet_output.crypts }}"
275271
loop_control:
276272
loop_var: entry
273+
register: __storage_crypttab_update
274+
275+
- name: Tell systemd to refresh its view of /etc/fstab and /etc/crypttab
276+
systemd:
277+
daemon_reload: true
278+
when: blivet_output['mounts'] | length > 0 or __storage_crypttab_update is changed
277279

278280
#
279281
# Update facts since we may have changed system state.

‎tests/tests_luks2.yml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@
107107
encryption_password: yabbadabbadoo
108108
encryption_luks_version: luks2
109109

110+
# Assisted by OpenCode using model GPT-6.1 Sol
111+
- name: Verify crypttab was loaded during the first role invocation
112+
when: ansible_facts['service_mgr'] == 'systemd'
113+
block:
114+
- name: Derive the new cryptsetup unit name
115+
command:
116+
argv:
117+
- systemd-escape
118+
- --template=systemd-cryptsetup@.service
119+
- "{{ blivet_output.crypts[0].name }}"
120+
register: storage_test_cryptsetup_unit
121+
changed_when: false
122+
123+
- name: Inspect the new cryptsetup unit without reloading systemd
124+
command:
125+
argv:
126+
- systemctl
127+
- show
128+
- "{{ storage_test_cryptsetup_unit.stdout }}"
129+
- --property=LoadState,SourcePath
130+
register: storage_test_cryptsetup_state
131+
changed_when: false
132+
133+
- name: Require the generated crypttab unit before any role rerun
134+
assert:
135+
that:
136+
- "'LoadState=loaded' in storage_test_cryptsetup_state.stdout_lines"
137+
- "'SourcePath=/etc/crypttab' in storage_test_cryptsetup_state.stdout_lines"
138+
110139
- name: Verify role results
111140
include_tasks: verify-role-results.yml
112141

‎tests/unit/test_crypttab_reload.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# SPDX-License-Identifier: MIT
2+
# Assisted by OpenCode using model GPT-6.1 Sol
3+
"""Crypttab changes must reach systemd during the same role invocation."""
4+
import os
5+
import unittest
6+
7+
import jinja2
8+
import yaml
9+
from ansible.plugins.test.core import TestModule as AnsibleTestModule
10+
11+
12+
class TestCrypttabReload(unittest.TestCase):
13+
@classmethod
14+
def setUpClass(cls):
15+
root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
16+
with open(os.path.join(root, 'tasks', 'main-blivet.yml')) as stream:
17+
cls.tasks = yaml.safe_load(stream)
18+
cls.crypttab_index = next(i for i, task in enumerate(cls.tasks)
19+
if task['name'] == 'Manage /etc/crypttab to account for changes we just made')
20+
cls.reload_indices = [i for i, task in enumerate(cls.tasks)
21+
if task.get('systemd', {}).get('daemon_reload')]
22+
cls.env = jinja2.Environment(undefined=jinja2.StrictUndefined)
23+
cls.env.tests.update(AnsibleTestModule().tests())
24+
25+
def test_final_reload_follows_crypttab(self):
26+
self.assertEqual(len(self.reload_indices), 2)
27+
self.assertLess(self.reload_indices[0], self.crypttab_index)
28+
self.assertGreater(self.reload_indices[-1], self.crypttab_index)
29+
30+
def test_crypttab_changes_are_registered(self):
31+
self.assertEqual(self.tasks[self.crypttab_index]['register'], '__storage_crypttab_update')
32+
33+
def test_reload_covers_mounts_and_crypttab_only_changes(self):
34+
reload_task = self.tasks[self.reload_indices[-1]]
35+
condition = self.env.compile_expression(reload_task['when'])
36+
for mounts, changed, expected in [([], False, False), ([], True, True),
37+
([{}], False, True), ([{}], True, True)]:
38+
self.assertEqual(condition(**{'blivet_output': dict(mounts=mounts),
39+
'__storage_crypttab_update': dict(changed=changed)}), expected)
40+
41+
def test_reload_respects_check_mode(self):
42+
self.assertNotIn('check_mode', self.tasks[self.reload_indices[-1]])

0 commit comments

Comments
 (0)