Creating a swapfile for Linux machines running a btrfs filesystem

Feb 11, 2021
·
    ·
    • Linux
    • ram
    • swap
    • unix

    How I turned my computer from 4GB RAM to 8GB RAM without buying any hardware.
    Creating a swapfile for a Linux machine running a btrfs filesystem.

    I have an i5 Huawei Matebook with 4GB RAM. This isn’t the worst specs for most things I need to do day to day (save for something like maybe intense password cracking). However, I also run virtualbox on my computer to host an instance of Parrot OS that I use for other purposes and I have noticed performance issues which at times would force me to do a hard shutdown of the whole computer.

    Swap is a space on a disk that is used when a computer’s physical RAM is full. It’s a way to use harddisk space in order to simulate extra RAM on a computer. When a computer’s RAM is full, idle programs are copied over to the swapfile to free up RAM memory.

    When I initially set up my native Parrot OS on my computer, I was given the option to create separate partitions for a swap disk. The alternative setting was to settle for a swap file (I have since learnt that using a swap file is more desirable compared to a swap disk). Being the Linux noob that I was (and still probably am), I did not want to mess around too much with partitions on the hard disk.

    Only recently have I found out, by running the command swapon –show that a swapfile was not automatically created during the initial installation of Linux. A further obstacle presented itself when I was attempting to provision a swapfile in that the harddisk on my computer was using the btrfs filesystem (https://btrfs.wiki.kernel.org/index.php/Main_Page). At the point of enabling the swapfile, I was getting constant Invalid Argument errors.

    A bit of further research and I found out the required extra steps to configuring a swapfile. The steps needed to be taken to create a swapfile on a btrfs harddisk is:

    touch /swapfile
    

    We’ll create the swapfile at the root directory.

    chattr +C /swapfile
    

    This is the magic command that fixed the Invalid argument error I was getting when trying to enable my swapfile previously. This command is asking for COW (a btrfs-specific resource management technique) to be disabled.

    sudo fallocate -l 4G /swapfile
    

    In my case, I’m creating a swapfile of size 4GB

    chmod 0600 /swapfile
    

    Change the file permissions to be read and write only by root.

    mkswap /swapfile
    

    set up this directory as a swap area

    swapon /swapfile
    

    enable the swapfile

    swapon --show
    

    check that your swapfile is enabled and how much of it is being used at the moment

    Do not forget to set a swappiness value. Swappiness can be set between values of 0 to 100 and determine how often or how aggressively the computer system will make use of the swapfile. A lower value means the kernel avoids swapping as much as possible while a high value will allow the kernel to more liberally make use of the swapfile. By default swappiness is set to 60. There are circles of engineers discouraging the use of frequent read and write operations into SSD harddrives due to degradation concerns. While I disagree in terms of the degree of impact these read and write operations during swapping will actually cause, and have a high level of confidence in our modern harddrive technologies, I set my swappiness to a level of 10. (No need to use the space if you actually don’t need it right)

    To do this, add

    vm.swappiness = 10
    

    to /etc/sysctl.conf.

    This can also be temporarily set by using

    sudo sh -c 'echo 10 > /proc/sys/vm/swappiness'
    

    to temporarily change the swappiness value. The changes will not persist after reboot though.

    After setting up a swapfile, I have noticed increased performance and reliability. I have not had to perform a hard shutdown of my PC since. It’s never run as smoothly and has rid me of the frustration of having to reboot the VM, losing all work and worse yet when I have to reboot not only the VM but the entire system.

    Using a swapfile, I turned my machine with 4GB of RAM into a machine with 8GB of RAM, and I didn’t have to spend a single dollar on RAM sticks.