Skip to content

Commit 59b7d98

Browse files
committed
make the fail function more generic
Signed-off-by: Zen <[email protected]>
1 parent 2c64a8b commit 59b7d98

File tree

5 files changed

+31
-35
lines changed

5 files changed

+31
-35
lines changed

src/ugrd/base/base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ def do_switch_root(self) -> str:
105105
"fi"]
106106

107107

108+
def rd_fail(self) -> list[str]:
109+
""" Function for when the initramfs fails to function. """
110+
return ['if [ -n "$1" ]; then',
111+
' ewarn "Mount failed: $1"',
112+
'else',
113+
' ewarn "Mount failed"',
114+
'fi',
115+
'prompt_user "Press enter to display debug info."',
116+
r'einfo "Loaded modules:\n$(cat /proc/modules)"',
117+
r'einfo "Block devices:\n$(blkid)"',
118+
r'einfo "Mounts:\n$(mount)"',
119+
'if [ "$(readvar RECOVERY_SHELL)" == "1" ]; then',
120+
' einfo "Entering recovery shell"',
121+
' bash -l',
122+
'fi',
123+
'prompt_user "Press enter to restart init."',
124+
'if [ "$$" -eq 1 ]; then',
125+
' einfo "Restarting init"',
126+
' exec /init ; exit',
127+
'else',
128+
' ewarn "PID is not 1, exiting: $$"',
129+
' exit',
130+
'fi']
131+
132+
108133
def setvar(self) -> str:
109134
""" Returns a bash function that sets a variable in /run/vars/{name}. """
110135
return ['if check_var debug; then',

src/ugrd/base/base.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ autodetect_init = true
1818
"ugrd.base.base" = [ "export_init_target", "do_switch_root" ]
1919

2020
[imports.functions]
21-
"ugrd.base.base" = [ "check_var", "setvar", "readvar", "prompt_user", "edebug", "einfo", "ewarn", "_find_init" ]
21+
"ugrd.base.base" = [ "check_var", "setvar", "readvar", "prompt_user", "edebug", "einfo", "ewarn", "rd_fail", "_find_init" ]
2222

2323
[custom_parameters]
2424
init_target = "Path" # Specifies the location of the system init file

src/ugrd/crypto/cryptsetup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def get_crypt_sources(self) -> list[str]:
155155
source_cmd = f'export CRYPTSETUP_SOURCE_{name}=$(blkid --match-token "$SOURCE_TOKEN_{name}" --match-tag PARTUUID --output device)'
156156

157157
check_command = [f'if [ -z "$CRYPTSETUP_SOURCE_{name}" ]; then',
158-
f' _mount_fail "Unable to resolve device source for {name}"',
158+
f' rd_fail "Unable to resolve device source for {name}"',
159159
'else',
160160
f' einfo "Resolved device source: $CRYPTSETUP_SOURCE_{name}"',
161161
'fi']

src/ugrd/fs/mounts.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = 'desultory'
2-
__version__ = '4.2.2'
2+
__version__ = '4.3.0'
33

44
from pathlib import Path
55
from zenlib.util import check_dict, pretty_print
@@ -140,7 +140,7 @@ def _to_mount_cmd(self, mount: dict) -> str:
140140
if mount_type := mount.get('type'):
141141
mount_command += f" -t {mount_type}"
142142

143-
mount_command += f" || _mount_fail 'failed to mount: {mount['destination']}'"
143+
mount_command += f" || rd_fail 'Failed to mount: {mount['destination']}'"
144144

145145
out += [f" {mount_command}",
146146
'else',
@@ -391,7 +391,7 @@ def mount_base(self) -> list[str]:
391391
if mount.get('base_mount'):
392392
out += _to_mount_cmd(self, mount)
393393
out += ['mkdir -p /run/vars',
394-
f'einfo "Mounting base mounts, version: {__version__}"']
394+
f'einfo "Mounted base mounts, version: {__version__}"']
395395
return out
396396

397397

@@ -425,7 +425,7 @@ def mount_fstab(self) -> list[str]:
425425
else:
426426
out += ['prompt_user "Press enter once devices have settled."']
427427

428-
out += ["mount -a || _mount_fail 'failed to mount fstab'"]
428+
out += ["mount -a || rd_fail 'Failed to mount fstab'"]
429429
return out
430430

431431

@@ -480,29 +480,3 @@ def export_mount_info(self) -> None:
480480
f'setvar MOUNTS_ROOT_TYPE "{self["mounts"]["root"].get("type", "auto")}"',
481481
f'setvar MOUNTS_ROOT_OPTIONS "{",".join(self["mounts"]["root"]["options"])}"']
482482

483-
484-
def _mount_fail(self) -> list[str]:
485-
""" Generates init lines to run if the mount fails. """
486-
return ['if [ -n "$1" ]; then',
487-
' ewarn "Mount failed: $1"',
488-
'else',
489-
' ewarn "Mount failed"',
490-
'fi',
491-
'prompt_user "Press enter to display debug info."',
492-
r'einfo "Loaded modules:\n$(cat /proc/modules)"',
493-
r'einfo "Block devices:\n$(blkid)"',
494-
r'einfo "Mounts:\n$(mount)"',
495-
'if [ "$(readvar RECOVERY_SHELL)" == "1" ]; then',
496-
' einfo "Entering recovery shell"',
497-
' bash -l',
498-
'fi',
499-
'prompt_user "Press enter to restart init."',
500-
'if [ "$$" -eq 1 ]; then',
501-
' einfo "Restarting init"',
502-
' exec /init ; exit',
503-
'else',
504-
' ewarn "PID is not 1, exiting: $$"',
505-
' exit',
506-
'fi']
507-
508-

src/ugrd/fs/mounts.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ late_fstab = "/etc/fstab.late"
3838
[imports.init_mount_late]
3939
"ugrd.fs.mounts" = [ "mount_late" ]
4040

41-
[imports.functions]
42-
"ugrd.fs.mounts" = [ "_mount_fail" ]
43-
4441
[custom_parameters]
4542
mounts = "dict" # Add the mounts property, used to define the mounts to be made in the fstab
4643
late_mounts = "dict" # Like mounts, but run after the root is mounted

0 commit comments

Comments
 (0)