Skip to content

Commit 7711a44

Browse files
authored
Add resume_swapfile
1 parent bb798e2 commit 7711a44

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/ugrd/fs/resume_swapfile.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
__version__ = "0.4.2"
2+
3+
4+
def handle_resume_swapfile(self) -> None:
5+
"""Returns a shell script handling resume from hibernation.
6+
Checks that /sys/power/resume is writable, resume= is set, and noresume is not set, if so,
7+
checks if UUID= or PARTUUID= or LABEL= is in the resume var,
8+
and tries to use blkid to find the resume device.
9+
If the specified device exists, writes resume device to /sys/power/resume.
10+
In the event of failure, it prints an error message, a list of block devuices, then runs rd_fail.
11+
12+
13+
Resuming or failing to do so is potentially dangerous.
14+
If the system was hibernated, and fails to resume, it will be in an inconsistent state.
15+
If the system is freshly booted, it will not be able to resume, as there is no hibernation image.
16+
Distinguising between a fresh boot and missing/borked hibernation image is not possible at run time.
17+
"""
18+
return [
19+
"resumeval=$(readvar resume)", # read the cmdline resume var
20+
"offset=$(readvar resume_offset)",
21+
'if ! check_var noresume && [ -n "$resumeval" ] && [ -w /sys/power/resume ]; then',
22+
' if echo "$resumeval" | grep -q "UUID=" ||', # resolve uuid to device
23+
' echo "$resumeval" | grep -q "PARTUUID=" ||', # or resolve partuuid to device
24+
' echo "$resumeval" | grep -q "LABEL=" ; then', # or resolve label to device
25+
' resume=$(blkid -t "$resumeval" -o device)',
26+
" else",
27+
" resume=$resumeval",
28+
" fi",
29+
' if [ ! -n "$offset" ] || [ "$offset" -le 0 ]; then',
30+
' ewarn "resume_offset not set, resume from swapfile likely to fail"',
31+
" fi",
32+
' if [ -e "$resume" ]; then', # Check if the resume device exists
33+
' einfo "Resuming from: $resume position $offset"',
34+
' printf "%s" "$resume" > /sys/power/resume', # Attempt to resume
35+
' ewarn "Failed to resume from: $resume position $offset"',
36+
" else",
37+
' ewarn "Resume device not found: $resume)"', # Warn if the resume device does not exist
38+
r' eerror "Block devices:\n$(blkid)"',
39+
' eerror "If you wish to continue booting, remove the resume= kernel parameter."',
40+
''' eerror " or run 'setvar noresume 1' from the recovery shell (set ugrd_recovery) to skip resuming."''',
41+
' rd_fail "Failed to resume from $(readvar resume)."',
42+
" fi",
43+
"fi",
44+
]

src/ugrd/fs/resume_swapfile.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmdline_strings = [ "resume" ]
2+
3+
[imports.init_main]
4+
"ugrd.fs.resume_swapfile" = [ "handle_resume_swapfile" ]
5+
6+
[import_order.before]
7+
handle_resume_swapfile = "mount_fstab"
8+
9+
[import_order.after]
10+
handle_resume_swapfile = "crypt_init"

0 commit comments

Comments
 (0)