Skip to content

Commit 7f1566a

Browse files
committed
Make the root mount return a warning if that device is not detected
Signed-off-by: Zen <[email protected]>
1 parent c1c3500 commit 7f1566a

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The original goal of this project was to create an initramfs suitable for decryp
1616
- Allows for late insertion of a smartcard
1717
- Can fail back to plain password entry
1818
* Key entry over serial
19-
* Automatic CPIO generation
19+
* Automatic CPIO generation (gen_init_cpio)
2020

2121
## Usage
2222

@@ -350,6 +350,8 @@ Modules can load other modules using the `modules` directive, be careful conside
350350

351351
If a module depends on another module, it can be added to the `mod_depends` list in the module config. A `ValueError` will be thrown if the module is not present.
352352

353+
`_module_name` can be set within a module for logging purposes, it is verified to be accurate when imported but optional.
354+
353355
#### imports
354356

355357
The most powerful part of a module is the `imports` directive.

ugrd/fs/mounts.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,32 +173,47 @@ def _get_mounts_source(self, mount):
173173
self.logger.warning("Unable to find mount source for: %s" % mount)
174174

175175

176-
def _get_lsblk_info(self, mount, output_fields="NAME,UUID,PARTUUID,LABEL"):
176+
def _get_blkid_info(self, device):
177177
"""
178-
Gets the lsblk info for a mountpoint
178+
Gets the blkid info for a device
179179
"""
180-
from json import loads, JSONDecodeError
180+
self.logger.debug("Getting blkid info for: %s" % device)
181181

182-
self.logger.debug("Getting lsblk info for: %s" % mount)
183-
184-
mount_info = self._run(['lsblk', '--json', '--output', output_fields, str(mount)])
185-
try:
186-
mount_info = loads(mount_info.stdout)
187-
except JSONDecodeError:
188-
self.logger.warning("Unable to parse lsblk info for: %s" % mount)
182+
mount_info = self._run(['blkid', str(device)]).stdout.decode().strip()
183+
if not mount_info:
184+
self.logger.warning("Unable to find blkid info for: %s" % device)
189185
return None
190186

191-
self.logger.debug("Found lsblk info: %s" % mount_info)
187+
self.logger.debug("Found blkid info: %s" % mount_info)
192188
return mount_info
193189

190+
194191
def mount_root(self):
195192
"""
196193
Mounts the root partition.
197194
Warns if the root partition isn't found on the current system.
198195
"""
199-
# root_source = self.config_dict['mounts']['root']['source']
196+
root_source = self.config_dict['mounts']['root']['source']
200197
host_root_dev = _get_mounts_source(self, '/')
201-
lsblk_info = _get_lsblk_info(self, host_root_dev)
198+
199+
# If the root device is a string, check that it's the same path as the host root mount
200+
if isinstance(root_source, str):
201+
if root_source != host_root_dev:
202+
self.logger.warning("Root device mismatch. Expected: %s, Found: %s" % (root_source, host_root_dev))
203+
elif isinstance(root_source, dict):
204+
# If the root device is a dict, check that the uuid, partuuid, or label matches the host root mount
205+
if blkid_info := _get_blkid_info(self, host_root_dev):
206+
# Unholy for-else, breaks if the uuid, partuuid, or label matches, otherwise warns
207+
for key, value in root_source.items():
208+
search_str = f"{key.upper()}=\"{value}\""
209+
if value in blkid_info:
210+
self.logger.debug("Found root device match: %s" % search_str)
211+
break
212+
else:
213+
self.logger.warning("Configuration root device not found on host system. Expected: %s" % root_source)
214+
self.logger.warning("Host system root device info: %s" % blkid_info)
215+
else:
216+
self.logger.warning("Unable to find blkid info for: %s" % root_source)
202217

203218
root_path = self.config_dict['mounts']['root']['destination']
204219

ugrd/initramfs_generator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _run_func(self, function, external=False, return_output=True):
9696
self.logger.debug("[%s] Function returned string: %s" % (function.__name__, function_output))
9797
return [function_output]
9898
else:
99-
self.logger.debug("[%s] Function returned output: %s" % (function.__name__, function_output))
99+
self.logger.debug("[%s] Function returned output: %s" % (function.__name__, pretty_print(function_output)))
100100
return function_output
101101
else:
102102
self.logger.debug("[%s] Function returned no output" % function.__name__)
@@ -268,7 +268,7 @@ def _write(self, file_name, contents, chmod_mask=0o644, in_build_dir=True):
268268
self.logger.info("Deleting file: %s" % file_path)
269269
file_path.unlink()
270270

271-
self.logger.debug("[%s] Writing contents:\n%s" % (file_path, pretty_print(contents)))
271+
self.logger.debug("[%s] Writing contents:\n%s" % (file_path, contents))
272272
with open(file_path, 'w') as file:
273273
file.writelines("\n".join(contents))
274274

@@ -325,7 +325,7 @@ def _rotate_old(self, file_name: Path, sequence=0):
325325
# If the cycle count is not set, attempt to clean
326326
if not self.old_count:
327327
if self.clean:
328-
self.logger.info("Deleting file: %s" % file_name)
328+
self.logger.warning("Deleting file: %s" % file_name)
329329
file_name.unlink()
330330
return
331331
else:
@@ -345,7 +345,7 @@ def _rotate_old(self, file_name: Path, sequence=0):
345345
if sequence >= self.old_count:
346346
# Clean the last file in the sequence if clean is enabled
347347
if self.clean:
348-
self.logger.info("Deleting old file: %s" % target_file)
348+
self.logger.warning("Deleting old file: %s" % target_file)
349349
target_file.unlink()
350350
else:
351351
self.logger.debug("Cycle limit reached")
@@ -392,7 +392,7 @@ def _run(self, args):
392392
"""
393393
Runs a command, returns the object
394394
"""
395-
self.logger.debug("Running command: %s" % args)
395+
self.logger.debug("Running command: %s" % ' '.join(args))
396396
cmd = run(args, capture_output=True)
397397
if cmd.returncode != 0:
398398
self.logger.error("Failed to run command: %s" % cmd.args)

0 commit comments

Comments
 (0)