Linux – Why can’t a file as swap be used for hibernation in Linux

hibernatelinuxswap

I was trying to hibernate my Fedora 27 system temporarily using a swap file and failed. Answers in the following question also say that a dedicated swap partition must be used to hibernate the system and a swap file won't work.

Why does Linux use a swap partition rather than a file?

I'm using an ext3 file system in which I created the swap file. What is stopping it being used for hibernation?

Best Answer

As said here (link provided by @don_crissti), the system must locate the swap file's header, but in order to do this the filesystem that contains the swap file must be mounted, and a journaled filesystem (as ext3 is) cannot be mounted during resume from disk.

Quoting from the document:

In order to use a swap file with swsusp, you need to:

1) Create the swap file and make it active, eg.

# dd if=/dev/zero of=<swap_file_path> bs=1024 count=<swap_file_size_in_k>

# mkswap <swap_file_path>

# swapon <swap_file_path>

2) Use an application that will bmap the swap file with the help of the FIBMAP ioctl and determine the location of the file's swap header, as the offset, in <PAGE_SIZE> units, from the beginning of the partition which holds the swap file.

3) Add the following parameters to the kernel command line:

resume=<swap_file_partition> resume_offset=<swap_file_offset>

where <swap_file_partition> is the partition on which the swap file is located and <swap_file_offset> is the offset of the swap header determined by the application in 2) (of course, this step may be carried out automatically by the same application that determines the swap file's header offset using the FIBMAP ioctl)

OR

Use a userland suspend application that will set the partition and offset with the help of the SNAPSHOT_SET_SWAP_AREA ioctl described in Documentation/power/userland-swsusp.txt (this is the only method to suspend to a swap file allowing the resume to be initiated from an initrd or initramfs image).

Related Question