Skip to content

Swap Setup

Use a swap file in btrfs

Swap file are more flexible in disk space and partition usage than swap partitions.

Source: btrfs docs and Arch Linux wiki.

The swapfile should not use COW.

cd /  # Or other dir for the swapfile
sudo mkdir -p /swap
sudo btrfs subvolume create /swap # Create a btrfs subvolume for the swap file
sudo btrfs filesystem mkswapfile --size 4G /swap/swapfile
sudo swapon /swap/swapfile

Or the following commands if btrfs filesystem mkswapfile command is not available.

cd /swap
sudo truncate -s 0 /swap/swapfile
sudo chattr +C /swap/swapfile
sudo fallocate -l 4G /swap/swapfile
sudo chmod 0600 /swap/swapfile
sudo mkswap /swap/swapfile
sudo swapon /swap/swapfile

Add the following line to /etc/fstab to mount the swap file on next boot.

/etc/fstab
/swap/swapfile none swap defaults 0 0

And one can see current activated swap by running

cat /proc/swaps

Use ZRAM

ZRAM is a RAM disk with on-the-fly disk compression, reducing physical disk use under high memory usage.

Install ZRAM in Ubuntu:

sudo apt install zram-config

edit /usr/bin/init-zram-swapping to change ZRAM options.

Comments