Skip to content

Commit d5ed3f8

Browse files
committed
updated custom_init mechanism, added soruce to custom_init
Signed-off-by: Zen <[email protected]>
1 parent 2171ca3 commit d5ed3f8

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

readme.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -540,47 +540,42 @@ Finally, like the standard init build, the `init_final` is written to the main `
540540
"ugrd.base.console" = [ "custom_init" ]
541541
```
542542

543-
The custom init works by creating an `init_main` file and returning a config line which will execute that file in a getty session.
544-
This `init_main` file contains everything that would be in the standard init file, but without the `init_pre` and `init_final` portions.
543+
The `custom_init` function should return a tuple with the line used to call the custom init file, and the contents of it.
545544

546545

547546
```
548-
549-
def custom_init(self):
547+
def custom_init(self) -> str:
550548
"""
551-
init override
549+
init override for the console module.
550+
Write the main init runlevels to the self.config_dict['_custom_init_file'] file.
551+
Returns the output of console_init which is the command to start agetty.
552552
"""
553553
custom_init_contents = [self.config_dict['shebang'],
554554
f"# Console module version v{__version__}"]
555555
custom_init_contents += self.generate_init_main()
556556
557-
self._write("init_main.sh", custom_init_contents, 0o755)
558-
559-
return console_init(self)
557+
return console_init(self), custom_init_contents
560558
561559
562-
def console_init(self):
560+
def console_init(self) -> str:
563561
"""
564-
start agetty
562+
Start agetty on the primary console.
563+
Tell it to execute teh _custom_init_file
564+
If the console is a serial port, set the baud rate.
565565
"""
566566
name = self.config_dict['primary_console']
567-
out_str = f"agetty --autologin root --login-program /init_main.sh {name}"
568-
569567
console = self.config_dict['console'][name]
570568
571-
if console.get('local'):
572-
out_str += " -L"
569+
out_str = f"agetty --autologin root --login-program {self.config_dict['_custom_init_file']}"
573570
574571
console_type = console.get('type', 'tty')
575572
576573
if console_type != 'tty':
577-
baud_rate = console['baud']
578-
out_str += f" {console_type} {baud_rate}"
579-
else:
580-
out_str += f" {console_type}"
574+
# This differs from usage in the man page but seems to work?
575+
out_str += f" --local-line {console['baud']}"
576+
577+
out_str += f" {name} {console_type}"
581578
582579
return out_str
583580
```
584581

585-
586-
`self._write(file_name, contents, chmod)` should be used instead of manually writing to a file.

ugrd/base/console.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "desultory"
2-
__version__ = "0.4.2"
2+
__version__ = "0.5.0"
33

44

55
def custom_init(self) -> str:
@@ -12,8 +12,7 @@ def custom_init(self) -> str:
1212
f"# Console module version v{__version__}"]
1313
custom_init_contents += self.generate_init_main()
1414

15-
self._write(self.config_dict['_custom_init_file'], custom_init_contents, 0o755)
16-
return console_init(self)
15+
return console_init(self), custom_init_contents
1716

1817

1918
def console_init(self) -> str:

ugrd/initramfs_dict.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ def _process_imports(self, import_type: str, import_value: dict) -> None:
115115
self.logger.log(5, "Creating import type: %s" % import_type)
116116
self['imports'][import_type] = NoDupFlatList(log_bump=10, logger=self.logger, _log_init=False)
117117

118+
if import_type == 'custom_init':
119+
if self['imports']['custom_init']:
120+
raise ValueError("Custom init function already defined: %s" % self['imports']['custom_init'])
121+
else:
122+
self['imports']['custom_init'] = function_list[0]
123+
self.logger.info("Registered custom init function: %s" % function_list[0].__name__)
124+
continue
125+
126+
if import_type == 'funcs':
127+
for function in function_list:
128+
if function.__name__ in self['imports']['funcs']:
129+
raise ValueError("Function '%s' already registered" % function.__name__)
130+
118131
self['imports'][import_type] += function_list
119132
self.logger.debug("[%s] Updated import functions: %s" % (import_type, function_list))
120133

@@ -123,11 +136,6 @@ def _process_imports(self, import_type: str, import_value: dict) -> None:
123136
self['custom_processing'][function.__name__] = function
124137
self.logger.debug("Registered config processing function: %s" % function.__name__)
125138

126-
if import_type == 'funcs':
127-
for function in function_list:
128-
if function.__name__ in self['imports']['funcs']:
129-
raise ValueError("Function '%s' already registered" % function.__name__)
130-
131139
@handle_plural
132140
def _process_mod_depends(self, module: str) -> None:
133141
"""

ugrd/initramfs_generator.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ def generate_init(self) -> None:
215215

216216
if self.config_dict['imports'].get('custom_init'):
217217
init += ["\n\n# !!custom_init"]
218-
init.extend(self._run_hook('custom_init'))
218+
init_line, custom_init = self.config_dict['imports']['custom_init'](self)
219+
init.append(init_line)
219220
else:
220221
init.extend(self.generate_init_main())
221222

@@ -226,6 +227,12 @@ def generate_init(self) -> None:
226227
init_funcs = self.generate_init_funcs()
227228
self._write('init_funcs.sh', init_funcs, 0o755)
228229
init.insert(2, "source /init_funcs.sh")
230+
if custom_init:
231+
custom_init.insert(2, "source /init_funcs.sh")
232+
233+
if custom_init:
234+
self._write('_custom_init_file', custom_init, 0o755)
235+
init += custom_init
229236

230237
self._write('init', init, 0o755)
231238
self.logger.debug("Final config:\n%s" % pretty_print(self.config_dict))

0 commit comments

Comments
 (0)