Skip to content

Commit 98f52af

Browse files
committed
improve logging, bash autocomplete, docs. remove required_modules
Signed-off-by: Zen <[email protected]>
1 parent 29f499b commit 98f52af

File tree

8 files changed

+35
-25
lines changed

8 files changed

+35
-25
lines changed

completion/ugrd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _ugrd_autocomplete() {
99
args+=" ${option}"
1010
done <<< "$(ugrd --dump_args)"
1111

12+
args+=" $(ls)"
1213
COMPREPLY=()
1314
case "${prev}" in
1415
--config|-c)
@@ -20,7 +21,11 @@ _ugrd_autocomplete() {
2021
return 0
2122
;;
2223
*)
23-
mapfile -t COMPREPLY < <(compgen -W "${args}" -- "${cur}")
24+
if [[ "${cur}" == -* ]]; then
25+
mapfile -t COMPREPLY < <(compgen -W "${args}" -- "${cur}")
26+
return 0
27+
fi
28+
mapfile -t COMPREPLY < <(compgen -f -- "${cur}")
2429
return 0
2530
;;
2631
esac

readme.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ An initramfs environment will be generated at `build_dir` (`/tmp/initramfs/`).
118118

119119
The initramfs will be packed to `out_dir` (`/tmp/initramfs_out`) and named `out_file` (`ugrd.cpio`).
120120

121+
## Config information
122+
123+
The final config dict can be printed with `--print-config`.
124+
125+
A list of functions by runlevel can be printed with `--print-init`.
126+
127+
121128
### Embedding the initramfs image into the kernel
122129

123130
The `build_dir`can be embedded into the Linux kernel with:
@@ -209,7 +216,7 @@ Verbose information about what what is being moved into the initramfs build dire
209216

210217
`_build_log_level` can be manually set to any log level. It is incremented by 10 when `build_logging` is enabled, with a minimum of 20.
211218

212-
This can be enabled at runtime with `--build-logging` or disabled wit `--no-build-logging`
219+
This can be enabled at runtime with `--build-logging` or disabled with `--no-build-logging`
213220

214221
The output can be logged to a file instead of stdout by specifying a log file with `--log-file`
215222

src/ugrd/base/core.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ old_count = 1
1010

1111
binaries = [ "/bin/bash" ]
1212

13-
required_parameters = [ "clean", "old_count", "build_dir", "out_dir", "file_owner", "validate", "hostonly"]
14-
1513
[nodes.console]
1614
mode = 0o644
1715
major = 5

src/ugrd/fs/cpio.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = 'desultory'
2-
__version__ = '2.5.1'
2+
__version__ = '2.6.0'
33

44

55
from pycpio import PyCPIO
@@ -31,11 +31,17 @@ def make_cpio(self) -> None:
3131

3232
def _process_out_file(self, out_file):
3333
""" Processes the out_file configuration option. """
34+
from pathlib import Path
35+
if Path(out_file).is_dir():
36+
self.logger.info("Specified out_file is a directory, setting out_dir: %s" % out_file)
37+
self['out_dir'] = out_file
38+
return
39+
3440
if out_file.startswith('./'):
3541
from pathlib import Path
3642
self.logger.debug("Relative out_file path detected: %s" % out_file)
3743
self['out_dir'] = Path('.').resolve()
38-
self.logger.info("out_dir resolve to: %s" % self['out_dir'])
44+
self.logger.info("Resolved out_dir to: %s" % self['out_dir'])
3945
out_file = Path(out_file[2:])
4046

4147
dict.__setitem__(self, 'out_file', out_file)

src/ugrd/fs/cpio.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
out_file = "ugrd.cpio"
22
mknod_cpio = true
33

4-
required_parameters = ['out_file']
5-
64
[imports.config_processing]
75
"ugrd.fs.cpio" = [ "_process_out_file" ]
86

97
[imports.pack]
108
"ugrd.fs.cpio" = [ "make_cpio" ]
119

1210
[custom_parameters]
13-
out_file = "str" # The name of the cpio file to create.
11+
out_file = "str" # The name of the cpio file to create.
1412
mknod_cpio = "bool" # When enabled, mknod is not used to create device nodes, they are just created in the cpio.

src/ugrd/initramfs_dict.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
__author__ = "desultory"
3-
__version__ = "1.2.0"
3+
__version__ = "1.3.0"
44

55
from tomllib import load, TOMLDecodeError
66
from pathlib import Path
@@ -24,7 +24,6 @@ class InitramfsConfigDict(dict):
2424
"""
2525
builtin_parameters = {'modules': NoDupFlatList, # A list of the names of modules which have been loaded, mostly used for dependency checking
2626
'imports': dict, # A dict of functions to be imported into the initramfs, under their respective hooks
27-
'required_parameters': NoDupFlatList, # A list of parameters which must be set before the initramfs can be generated
2827
'custom_parameters': dict, # Custom parameters loaded from imports
2928
'custom_processing': dict, # Custom processing functions which will be run to validate and process parameters
3029
'_processing': dict} # A dict of queues containing parameters which have been set before the type was known
@@ -222,18 +221,8 @@ def verify_mask(self) -> None:
222221
runlevel.remove(function)
223222
self.logger.warning("[%s] Masking import: %s" % (mask_hook, function.__name__))
224223

225-
def verify_required_parameters(self) -> None:
226-
""" Verifies that all required parameters are set """
227-
for parameter in self['required_parameters']:
228-
if parameter not in self:
229-
self.logger.error(self)
230-
raise KeyError("Required parameter not found in config: %s" % parameter)
231-
232-
self.logger.debug("Verified required parameters: %s" % self['required_parameters'])
233-
234224
def validate(self) -> None:
235225
""" Validate config """
236-
self.verify_required_parameters()
237226
if self['_processing']:
238227
self.logger.critical("Unprocessed config values: %s" % ', '.join(list(self['_processing'].keys())))
239228
self.verify_mask()

src/ugrd/initramfs_generator.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from tomllib import load
32
from typing import Union
43
from pathlib import Path
@@ -66,8 +65,6 @@ def get(self, item, default=None):
6665
def __getattr__(self, item):
6766
""" Allows access to the config dict via the InitramfsGenerator object. """
6867
if item not in self.__dict__ and item != 'config_dict':
69-
if item not in self.config_dict['required_parameters']:
70-
self.logger.warning("Accessing non-required parameter through getattr -> config_dict: %s" % item)
7168
return self[item]
7269
return super().__getattr__(item)
7370

@@ -184,7 +181,7 @@ def generate_init(self) -> None:
184181
if self.included_functions:
185182
init_funcs = self.generate_init_funcs()
186183
self._write('init_funcs.sh', init_funcs, 0o755)
187-
self.logger.info("Included functions: %s" % list(self.included_functions.keys()))
184+
self.logger.info("Included functions: %s" % ', '.join(list(self.included_functions.keys())))
188185
init.insert(3, "source init_funcs.sh")
189186
if self['imports'].get('custom_init'):
190187
custom_init.insert(2, f"echo 'Starting custom init, UGRD v{__version__}'")

src/ugrd/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ def main():
2626
{'flags': ['--autodetect-root-luks'], 'action': 'store_true', 'help': 'autodetect LUKS volumes under the root partition'},
2727
{'flags': ['--no-autodetect-root-luks'], 'action': 'store_false', 'help': 'do not autodetect root LUKS volumes', 'dest': 'autodetect_root_luks'},
2828
{'flags': ['--print-config'], 'action': 'store_true', 'help': 'print the final config dict'},
29+
{'flags': ['--print-init'], 'action': 'store_true', 'help': 'print the final init structure'},
2930
{'flags': ['out_file'], 'action': 'store', 'help': 'set the output image location', 'nargs': '?'}]
3031

3132
args, logger = get_args_n_logger(package=__package__, description='MicrogRAM disk initramfs generator', arguments=arguments, drop_default=True)
3233
kwargs = get_kwargs_from_args(args, logger=logger)
3334
kwargs.pop('print_config', None) # This is not a valid kwarg for InitramfsGenerator
35+
kwargs.pop('print_init', None) # This is not a valid kwarg for InitramfsGenerator
3436

3537
logger.debug(f"Using the following kwargs: {kwargs}")
3638
generator = InitramfsGenerator(**kwargs)
@@ -46,6 +48,14 @@ def main():
4648
if 'print_config' in args and args.print_config:
4749
print(generator.config_dict)
4850

51+
if 'print_init' in args and args.print_init:
52+
for runlevel in ['init_pre', *generator.init_types, 'init_final']:
53+
if runlevel not in generator.imports:
54+
continue
55+
print({runlevel} + ":")
56+
for func in generator.imports[runlevel]:
57+
print(f" {func.__name__}")
58+
4959

5060
if __name__ == '__main__':
5161
main()

0 commit comments

Comments
 (0)