Btrfs is a modern copy on write (COW) file system for Linux aimed at implementing advanced features while also focusing on fault tolerance, repair and easy administration.
Timeshift for Linux is an application that provides functionality similar to the System Restore feature in Windows and the Time Machine tool in macOS. This details how to setup and use Timeshift with Btrfs.
- Endeavour OS: Snapshotting with Timeshift
- No space left, despite having 200GB free space (BTRFS install)
-
Install the
timeshiftpackage usingyay. -
Install the
timeshift-autosnappackage usingyayto automatically create a snapshot every pre-upgrade. -
Install the
grub-btrfspackage usingyayto allow booting into snapshots through Grub. -
Complete the setup through the Timeshift wizard:
-
Authenticate (as it requires
sudo). -
Select BTRFS as Snapshot Type.
-
Select the main btrfs partition as the Snapshot Location.
-
Select the desired Snapshot Levels. Default options are fine.
-
Leave the Include @home subvolume in backups option unchecked.
-
Click Finish.
-
-
Create the first snapshot by clicking the Create button.
-
Start and Enable the system's
cronservice (i.e.cronie.service) so that scheduled backups work. -
Regenerate a new GRUB config so that
grub-btrfsworks as intended.
-
timeshift-autosnapshould only keep 3 older snapshots by default according to its config,/etc/timeshift-autosnap.conf. -
If it is not working reliably, it is recommended for you to use the Timeshift desktop application to delete older snapshots manually from time to time.
If you are using a Btrfs filesystem, it is recommended for you to "balance" your Btrfs volumes on a monthly basis in order to ensure that the free space available on your device is properly unallocated.
Warning
Leaving your Btrfs volume unbalanced for too long might cause you No space left errors in the long run.
-
Install the
btrfs-assistantapplication usingyay. -
Launch the
btrfs-assistantapplication. You will be asked to authenticate (as it requiresrootpermissions). -
Click the Start button in the Balance section to balance your Btrfs volumes.
This guide covers topics pertaining to the addition or removal of swap files as virtual memory on a system running BTRFS.
This details how to add and enable a swap file on a system running BTRFS:
-
First, verify that swap is currently disabled on the system:
-
First method:
inxi -j
Sample output:
Swap: Alert: No swap data was found. -
Second method:
swapon --show
This command should not return any output.
-
-
Ensure there is not a BTRFS subvolume for the swap file already:
sudo btrfs subvolume list /
Sample output:
ID 257 gen 5349724 top level 5 path @home ID 258 gen 5349702 top level 5 path @cache ID 259 gen 5349717 top level 5 path @log ID 457 gen 5349724 top level 5 path @ -
Determine the UUID of the BTRFS filesystem on the system:
sudo btrfs filesystem show /
Sample output:
Label: 'endeavouros' uuid: bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg Total devices 1 FS bytes used 290.02GiB devid 1 size 446.15GiB used 315.07GiB path /dev/sdb2Based on this sample output, the UUID of the BTRFS filesystem is
bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg. -
Create a BTRFS subvolume to store the swap file:
-
Mount the BTRFS filesystem root temporarily to a mountpoint (i.e.
/mnt):sudo mount UUID=<uuid> /mnt
Replace
<uuid>with the UUID of the BTRFS filesystem (i.e.bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg). For example:sudo mount UUID=bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg /mnt
This is done so we could access the top level where other BTRFS subvolumes are created on the system.
-
Create the swap BTRFS subvolume on the mountpoint (i.e.
/mnt):sudo btrfs subvolume create /mnt/@swap
-
Ensure the swap BTRFS subvolume has been created:
sudo btrfs subvolume list / | grep swapSample output:
ID 260 gen 344318 top level 5 path @swap -
Unmount the BTRFS filesystem root from the mountpoint (i.e.
/mnt):sudo umount /mnt
-
-
Mount the swap BTRFS subvolume on the system (temporarily) and create the swap file:
-
Create a directory where the swap BTRFS subvolume should be mounted to (i.e.
/swap):sudo mkdir /swap
-
Mount the swap BTRFS subvolume to the designated mountpoint (i.e.
/swap):sudo mount -o subvol=/@swap,noatime UUID=<uuid> /swap
Replace
<uuid>with the UUID of the BTRFS filesystem (i.e.bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg). For example:sudo mount -o subvol=/@swap,noatime UUID=bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg /swapVerify that it's mounted:
TARGET SOURCE FSTYPE OPTIONS /swap /dev/sdb2[/@swap] btrfs rw,noatime,compress=zstd:3,ssd,discard=async,space_cache=v2,subvolid=260,subvol=/@swap -
Create the appropriately sized swap file on the mountpoint (i.e.
/swap):sudo btrfs filesystem mkswapfile --size <size>g --uuid clear /swap/swapfile
Replace
<size>with the desired size of the swap file in gigabytes. If in doubt, allocate the size of your total system memory, or half of it. For example, for 16GB of swap:sudo btrfs filesystem mkswapfile --size 16g --uuid clear /swap/swapfile
-
Activate the swap file (i.e.
/swap/swapfile) as swap space on the system:sudo swapon /swap/swapfile
-
-
Verify that swap has been enabled on the system:
-
First method:
inxi -j
Sample output:
Swap: ID-1: swap-1 type: file size: 16 GiB used: 10.2 MiB (0.1%) file: /swap/swapfile -
Second method:
swapon --show
Sample output:
NAME TYPE SIZE USED PRIO /swap/swapfile file 16G 10.5M -2
-
-
Mount the swap BTRFS subvolume on the system and activate the swap file on boot automatically:
-
Update the system's
fstabfile:sudo nano /etc/fstab
-
Add the following line to the
fstabfile, with the rest of the existing entries, to mount the swap BTRFS subvolume on boot to the designated mountpoint (i.e./swap):UUID=<uuid> /var/log btrfs subvol=/@log,noatime,compress=zstd 0 0 + UUID=<uuid> /swap btrfs subvol=/@swap,noatime 0 0Replace
<uuid>with the UUID of the BTRFS filesystem (i.e.bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg). For example:UUID=bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg /swap btrfs subvol=/@swap,noatime 0 0 -
Add the following line to the
fstabfile to activate the swap file we had created as swap space on boot (i.e./swap/swapfile):UUID=bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg /swap btrfs subvol=/@swap,noatime 0 0 + /swap/swapfile swap swap defaultsSample entry:
/swap/swapfile swap swap defaults -
Upon next boot, the swap BTRFS subvolume will be mounted and the swap file will be activated accordingly.
-
Note
This guide assumes that you have previously enabled swap on the system, and have not disabled it.
This details how to resize a swap file on a system running BTRFS:
-
First, verify that swap is currently enabled on the system:
-
First method:
inxi -j
Sample output:
Swap: ID-1: swap-1 type: file size: 16 GiB used: 10.2 MiB (0.1%) file: /swap/swapfile -
Second method:
swapon --show
Sample output:
NAME TYPE SIZE USED PRIO /swap/swapfile file 16G 10.5M -2
-
-
Disable the swap file (i.e.
/swap/swapfile) on the system, temporarily:sudo swapoff /swap/swapfile
-
Delete the current swap file (i.e.
/swap/swapfile):sudo rm /swap/swapfile
-
Create the replacement, appropriately sized swap file (i.e.
/swap/swapfile):sudo btrfs filesystem mkswapfile --size <size>g --uuid clear /swap/swapfile
For example, for 32GB of swap space:
sudo btrfs filesystem mkswapfile --size 32g --uuid clear /swap/swapfile
Sample output:
create swapfile /swap/swapfile size 32.00GiB (34359738368) -
Activate the swap file (i.e.
/swap/swapfile) as swap space on the system:sudo swapon /swap/swapfile
-
Verify that swap has been enabled on the system:
-
First method:
inxi -j
Sample output:
Swap: ID-1: swap-1 type: file size: 32 GiB used: 983.3 MiB (3.0%) file: /swap/swapfile -
Second method:
swapon --show
Sample output:
NAME TYPE SIZE USED PRIO /swap/swapfile file 32G 987.1M -2
-
This details how to disable and remove a swap device on a system running BTRFS:
-
First, verify that swap is indeed currently enabled on the system:
-
First method:
inxi -j
Sample output:
Swap: ID-1: swap-1 type: file size: 512 MiB used: 511.8 MiB (100.0%) file: /swap/swapfile -
Second method:
swapon --show
Sample output:
NAME TYPE SIZE USED PRIO /swap/swapfile file 512M 511.8M -2Take note of the name of the swap device on the system (i.e.
/swap/swapfile).
-
-
Disable the (file-based) swap device (i.e.
/swap/swapfile):sudo swapoff /swap/swapfile
-
Unmount the swap subvolume from its mountpoint (i.e.
/swap):sudo umount /swap
(Optional) You may also delete the mountpoint as it is no longer needed:
sudo rm -r /swap
-
Delete the swap BTRFS subvolume (i.e.
@swap) using the Btrfs Assistant application:-
Launch the Btrfs Assistant application. Install the
btrfs-assistantpackage usingyayif you do not have it already. -
Navigate to the Subvolumes tab.
-
From the list of subvolumes, locate and highlight the swap BTRFS subvolume (i.e.
@swap) by selecting it. -
Click the Delete button.
-
When prompted to confirm, click the Yes button.
-
-
Prevent the system from attempting to mount the deleted swap BTRFS subvolume and activate the swap file on boot:
-
Update the system's
fstabfile:sudo nano /etc/fstab
-
Remove or comment the following line that mounts the swap BTRFS subvolume to the designated mountpoint (i.e.
/swap):- UUID=<uuid> /swap btrfs subvol=/@swap,noatime 0 0 + #UUID=<uuid> /swap btrfs subvol=/@swap,noatime 0 0
Replace
<uuid>with the UUID of the BTRFS filesystem (i.e.bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg). For example:UUID=bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg /swap btrfs subvol=/@swap,noatime 0 0 -
Add the following line to the
fstabfile to activate the swap file we had created as swap space on boot (i.e./swap/swapfile):- /swap/swapfile swap swap defaults + #/swap/swapfile swap swap defaults
-
Sample entries after they have been disabled by commenting them:
#UUID=bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg /swap btrfs subvol=/@swap,noatime 0 0 #/swap/swapfile swap swap defaults
-
-
Reboot the system and reuse the same step to verify that swap has been disabled.