Using a swapfile
A swapfile has the same function of a swap partition. The advantage of swapfiles is that they they can be reallocated if need be. Whereas changing size of a partition is extremely difficult (except when using lvm). The performances of a swap file are the same as for a swap partition: Linux kernel ignores the filesystem and accesses the block contents directly on the disk.
- Allocate swapfile:
head -c {size (e.g. 4G)} < /dev/urandom > {swapfile (e.g. /swapfile)}. Man page forswaponsuggest avoiding usingfallocate, as some guides do. chmod 600 {swap file}mkswap {swapfile}: to format the swap file so that it can be used as swap- Add the swap file to
/etc/fstab: the line should look similar to{swapfile location (e.g. /swapfile)} none swap sw 0 0
Swapfile will be activated on boot. To activate it without rebooting, execute swapon {swapfile}
Removing/resizing a swapfile
To resize a swapfile: delete it and recreate it.
swapoff {swapfile}to deactivate the swaprm {swapfile}to delete the swap- Delete swapfile line from
/etc/fstab(unless you recreate it with the same name)
Setting up hibernation with a swapfile
For the system to be able to hibernate successfully when a swapfile is used, some steps have to e followed.
filefrag -v {swapfile}Look for the first big number which represent the physical offset of the swap file on the disk. e.g. in0: 0.. 0: 2193408.. 2193408: 1:it is2193408.- Edit
/etc/default/grubto add options toGRUB_CMDLINE_LINUX- Add
resume={partition containing the swap file}For an encrypted system it should be something like/dev/mapper/{root filesystem partition name} - Add
resume_offset={offset found earlier}
- Add
update-grub- Reboot the system so that all the parameters are loaded.
It is now possible to hibernate