You are not logged in.

#1 2025-11-20 20:22:45

RogerRabbit
Member
Registered: 2022-12-18
Posts: 78

Swap partition on, but hibernate to dedicated file

Hello,

I don't have any specific issue, but I think hibernation could work better on Linux, please help me understand if the following model would be achievable.

First of all, on a "normal" setup, you have fixed RAM + fixed dedicated SWAP, this causes at least two consequences regarding hibernation:
- the SWAP partition is not dedicated to hibernation, so you cannot (and actually don't want to) prevent it from filling up over time, leading to chances of a failed system hibernation (RAM + SWAP used space > available remaining SWAP space)
- if you enable compression, I believe SWAP data would be loaded to RAM in order to be compressed it and stored back to SWAP, causing long hibenartion state transition time

The model I'm thinking about is actually a simple file dedicated to hibernation.
A file on your system, that would not be used for swapping, with RAM pre-allocated size.
This would improve:
- hibernation consistency, the file is preallocated with RAM size, so you'll never EVER get OOM to hibernate
- hibernation state transition time, only RAM would need to be moved to the hibernation file, SWAP data can just remain in SWAP
- efficiency, as the space is preallocated, you wouln't need to compress RAM before storing it into the file (not sure about this one, I think with fast CPUs you actually gain some time compressing and storing on the fly)
Obviously the downside would be the hibernation file that will permanently eat the RAM size on the drive.

Does it make sense?
I searched for information, but could only find people wanting to hibernate without having SWAP enabled.

Offline

#2 2025-11-20 21:55:46

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 70,466

Re: Swap partition on, but hibernate to dedicated file

Obviously the downside would be the hibernation file that will permanently eat the RAM size on the drive.

No.

https://wiki.archlinux.org/title/Power_ … _initramfs
See the "… > /sys/power/resume*" stuff.
You can actually create a file on the fly, fix the bootloader config, tell the kernel where to write the image and that's that.
The most nasty part is actually to make sure to update the bootloader config.

I'll say that I don't really hibernate any more - booting from an nvme is so fast that you gain little to no benefit, you only have to make sure to either only S3 or save your work before shutting down smile

Offline

#3 2025-11-20 22:52:22

RogerRabbit
Member
Registered: 2022-12-18
Posts: 78

Re: Swap partition on, but hibernate to dedicated file

I actually know how to tell the bootloader where to resume from, because we had to do it before systemd started to manage it automatically from some update 1 or 2 years ago.

My issue is actually how to specify where to hibernate, by default it will automatically hibernate to swap, and I don't know ho to tell it to write it to specific file.

Also, the advantage of having a file with preallocated space is to be sure that the space is ALWAYS available (failed hibernation followed by battery outage can lead to data loss).

Offline

#4 2025-11-20 23:09:43

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 70,466

Re: Swap partition on, but hibernate to dedicated file

My issue is actually how to specify where to hibernate, by default it will automatically hibernate to swap, and I don't know ho to tell it to write it to specific file.

seth wrote:

https://wiki.archlinux.org/title/Power_ … _initramfs
See the "… > /sys/power/resume*" stuff.

By "nasty" i didn't mean "how" but "that" - you've to systematically automatically edit your bootloader config.

by default it will automatically hibernate to swap

You mean systemd does not respect the resume=… resume_offset=… parameters when selecting the hibernation location??

the advantage of having a file with preallocated space is to be sure that the space is ALWAYS available

Obviously the downside would be the hibernation file that will permanently eat the RAM size on the drive.

Those are the alternatives…

Offline

#5 2025-11-21 00:01:53

RogerRabbit
Member
Registered: 2022-12-18
Posts: 78

Re: Swap partition on, but hibernate to dedicated file

Ok I'm starting to understand,..

For a file I actually need to provide both resume and resume_offset and that's why it's tricky...
If I always keep the same file and not delete it, wouldn't the offset stick the same?
I'd still need to blank it at resume or else it would be used after a shutdown for a resume anyway...

Also, I tought It was needed to declare the file as SWAP...
Would I need to create the file and swap it on at hibernation time, and then swap it off at resume ?

Offline

#6 2025-11-21 07:48:34

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 70,466

Re: Swap partition on, but hibernate to dedicated file

If I always keep the same file and not delete it, wouldn't the offset stick the same?

Yes.

I'd still need to blank it at resume or else it would be used after a shutdown for a resume anyway...

Errr… wasn't that the point? To use the swap file?

Also, I tought It was needed to declare the file as SWAP...

https://wiki.archlinux.org/title/Swap#S … e_creation
You need to use mkswap, but w/ your original deliberations don't use swapon or the fstab since you're not actually using it as swap space

Would I need to create the file and swap it on at hibernation time, and then swap it off at resume ?

No, you're just gonna have a stale swap file on your disk.

Offline

Board footer

Powered by FluxBB