2023-04-26 11:05:32 +00:00
|
|
|
A swapfile, when active, is a file-backed swap area. It is supported since kernel 5.0.
|
2024-02-16 08:37:55 +00:00
|
|
|
Use :manref:`swapon(8)` to activate it, until then (respectively again after deactivating it
|
|
|
|
with :manref:`swapoff(8)`) it's just a normal file (with NODATACOW set), for which the special
|
2023-04-26 11:05:32 +00:00
|
|
|
restrictions for active swapfiles don't apply.
|
2023-04-20 23:59:20 +00:00
|
|
|
|
|
|
|
There are some limitations of the implementation in BTRFS and Linux swap
|
|
|
|
subsystem:
|
2022-01-11 22:56:25 +00:00
|
|
|
|
|
|
|
* filesystem - must be only single device
|
|
|
|
* filesystem - must have only *single* data profile
|
2023-04-26 11:05:32 +00:00
|
|
|
* subvolume - cannot be snapshotted if it contains any active swapfiles
|
|
|
|
* swapfile - must be preallocated (i.e. no holes)
|
|
|
|
* swapfile - must be NODATACOW (i.e. also NODATASUM, no compression)
|
2022-01-11 22:56:25 +00:00
|
|
|
|
|
|
|
The limitations come namely from the COW-based design and mapping layer of
|
|
|
|
blocks that allows the advanced features like relocation and multi-device
|
|
|
|
filesystems. However, the swap subsystem expects simpler mapping and no
|
2022-12-06 19:44:39 +00:00
|
|
|
background changes of the file block location once they've been assigned to
|
2024-07-30 15:31:30 +00:00
|
|
|
swap. The constraints mentioned above (single device and single profile) are
|
|
|
|
related to the swapfile itself, i.e. the extents and their placement. It is
|
|
|
|
possible to create swapfile on multi-device filesystem as long as the extents
|
|
|
|
are on one device but this cannot be affected by user and depends on free space
|
|
|
|
fragmentation and available unused space for new chunks.
|
2022-01-11 22:56:25 +00:00
|
|
|
|
|
|
|
With active swapfiles, the following whole-filesystem operations will skip
|
|
|
|
swapfile extents or may fail:
|
|
|
|
|
2023-04-26 11:05:32 +00:00
|
|
|
* balance - block groups with extents of any active swapfiles are skipped and
|
2023-04-20 23:59:20 +00:00
|
|
|
reported, the rest will be processed normally
|
2022-01-11 22:56:25 +00:00
|
|
|
* resize grow - unaffected
|
2023-04-26 11:05:32 +00:00
|
|
|
* resize shrink - works as long as the extents of any active swapfiles are
|
2023-04-20 23:59:20 +00:00
|
|
|
outside of the shrunk range
|
2023-04-26 11:05:32 +00:00
|
|
|
* device add - if the new devices do not interfere with any already active swapfiles
|
|
|
|
this operation will work, though no new swapfile can be activated
|
2023-04-20 23:59:20 +00:00
|
|
|
afterwards
|
2022-01-11 22:56:25 +00:00
|
|
|
* device delete - if the device has been added as above, it can be also deleted
|
|
|
|
* device replace - ditto
|
|
|
|
|
|
|
|
When there are no active swapfiles and a whole-filesystem exclusive operation
|
2022-12-07 20:00:25 +00:00
|
|
|
is running (e.g. balance, device delete, shrink), the swapfiles cannot be
|
2022-01-11 22:56:25 +00:00
|
|
|
temporarily activated. The operation must finish first.
|
|
|
|
|
|
|
|
To create and activate a swapfile run the following commands:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# truncate -s 0 swapfile
|
|
|
|
# chattr +C swapfile
|
|
|
|
# fallocate -l 2G swapfile
|
|
|
|
# chmod 0600 swapfile
|
|
|
|
# mkswap swapfile
|
|
|
|
# swapon swapfile
|
|
|
|
|
2022-12-06 19:44:39 +00:00
|
|
|
Since version 6.1 it's possible to create the swapfile in a single command
|
|
|
|
(except the activation):
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2023-03-16 10:39:15 +00:00
|
|
|
# btrfs filesystem mkswapfile --size 2G swapfile
|
2022-12-06 19:44:39 +00:00
|
|
|
# swapon swapfile
|
|
|
|
|
2022-01-11 22:56:25 +00:00
|
|
|
Please note that the UUID returned by the *mkswap* utility identifies the swap
|
|
|
|
"filesystem" and because it's stored in a file, it's not generally visible and
|
|
|
|
usable as an identifier unlike if it was on a block device.
|
|
|
|
|
2023-06-28 17:55:08 +00:00
|
|
|
Once activated the file will appear in :file:`/proc/swaps`:
|
2022-01-11 22:56:25 +00:00
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
# cat /proc/swaps
|
|
|
|
Filename Type Size Used Priority
|
|
|
|
/path/swapfile file 2097152 0 -2
|
|
|
|
|
|
|
|
The swapfile can be created as one-time operation or, once properly created,
|
2023-04-26 23:48:47 +00:00
|
|
|
activated on each boot by the :command:`swapon -a` command (usually started by the
|
2022-01-11 22:56:25 +00:00
|
|
|
service manager). Add the following entry to */etc/fstab*, assuming the
|
|
|
|
filesystem that provides the */path* has been already mounted at this point.
|
|
|
|
Additional mount options relevant for the swapfile can be set too (like
|
|
|
|
priority, not the BTRFS mount options).
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
/path/swapfile none swap defaults 0 0
|
|
|
|
|
2023-04-20 23:59:20 +00:00
|
|
|
From now on the subvolume with the active swapfile cannot be snapshotted until
|
2023-04-26 23:48:47 +00:00
|
|
|
the swapfile is deactivated again by :command:`swapoff`. Then the swapfile is a
|
2023-04-20 23:59:20 +00:00
|
|
|
regular file and the subvolume can be snapshotted again, though this would prevent
|
|
|
|
another activation any swapfile that has been snapshotted. New swapfiles (not
|
|
|
|
snapshotted) can be created and activated.
|
|
|
|
|
|
|
|
Otherwise, an inactive swapfile does not affect the containing subvolume. Activation
|
|
|
|
creates a temporary in-memory status and prevents some file operations, but is
|
|
|
|
not stored permanently.
|
|
|
|
|
2022-12-06 19:44:39 +00:00
|
|
|
Hibernation
|
|
|
|
-----------
|
|
|
|
|
|
|
|
A swapfile can be used for hibernation but it's not straightforward. Before
|
|
|
|
hibernation a resume offset must be written to file */sys/power/resume_offset*
|
|
|
|
or the kernel command line parameter *resume_offset* must be set.
|
|
|
|
|
|
|
|
The value is the physical offset on the device. Note that **this is not the same
|
2023-04-26 23:48:47 +00:00
|
|
|
value that** :command:`filefrag` **prints as physical offset!**
|
2022-12-06 19:44:39 +00:00
|
|
|
|
|
|
|
Btrfs filesystem uses mapping between logical and physical addresses but here
|
|
|
|
the physical can still map to one or more device-specific physical block
|
|
|
|
addresses. It's the device-specific physical offset that is suitable as resume
|
|
|
|
offset.
|
|
|
|
|
2023-06-28 17:55:08 +00:00
|
|
|
Since version 6.1 there's a command :ref:`btrfs inspect-internal map-swapfile<man-inspect-map-swapfile>`
|
|
|
|
that will print the device physical offset and the adjusted value for
|
|
|
|
:file:`/sys/power/resume_offset`. Note that the value is divided by page size, i.e.
|
|
|
|
it's not the offset itself.
|
2022-12-06 19:44:39 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# btrfs filesystem mkswapfile swapfile
|
|
|
|
# btrfs inspect-internal map-swapfile swapfile
|
|
|
|
Physical start: 811511726080
|
|
|
|
Resume offset: 198122980
|
|
|
|
|
|
|
|
For scripting and convenience the option *-r* will print just the offset:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# btrfs inspect-internal map-swapfile -r swapfile
|
|
|
|
198122980
|
|
|
|
|
2023-06-28 17:55:08 +00:00
|
|
|
The command :command:`map-swapfile` also verifies all the requirements, i.e. no holes,
|
2022-12-06 19:44:39 +00:00
|
|
|
single device, etc.
|
|
|
|
|
2022-07-09 11:30:37 +00:00
|
|
|
|
|
|
|
Troubleshooting
|
|
|
|
---------------
|
|
|
|
|
|
|
|
If the swapfile activation fails please verify that you followed all the steps
|
2023-04-26 23:48:47 +00:00
|
|
|
above or check the system log (e.g. :command:`dmesg` or :command:`journalctl`) for more
|
2022-07-09 11:30:37 +00:00
|
|
|
information.
|
|
|
|
|
2023-04-26 23:48:47 +00:00
|
|
|
Notably, the :command:`swapon` utility exits with a message that does not say what
|
2022-07-09 11:30:37 +00:00
|
|
|
failed:
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
# swapon /path/swapfile
|
|
|
|
swapon: /path/swapfile: swapon failed: Invalid argument
|
|
|
|
|
|
|
|
The specific reason is likely to be printed to the system log by the btrfs
|
|
|
|
module:
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
# journalctl -t kernel | grep swapfile
|
|
|
|
kernel: BTRFS warning (device sda): swapfile must have single data profile
|