Announcement

Collapse
No announcement yet.

Using Swap file with BTRFS

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Using Swap file with BTRFS

    While researching another topic I came across this page, which deals with btrfs swapfiles, which I haven't kept up on:
    https://www.jwillikers.com/btrfs-swapfile

    After reading that article I went to the BTRFS wiki
    https://btrfs.wiki.kernel.org/index....APFILE_SUPPORT
    SWAPFILE SUPPORT

    The swapfile is supported since kernel 5.0. Use swapon(8) to activate the swapfile. There are some limitations of the implementation in btrfs and linux swap subystem:
    • filesystem - must be only single device
    • swapfile - the containing subvolume cannot be snapshotted
    • swapfile - must be preallocated
    • swapfile - must be nodatacow (ie. also nodatasum)
    • swapfile - must not be compressed


    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 background changes of the file blocks once they?ve been attached to swap.

    With active swapfiles, the following whole-filesystem operations will skip swapfile extents or may fail:
    • balance - block groups with swapfile extents are skipped and reported, the rest will be processed normally
    • resize grow - unaffected
    • resize shrink - works as long as the extents are outside of the shrunk range
    • device add - a new device does not interfere with existing swapfile and this operation will work, though no new swapfile can be activated afterwards
    • 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 is running (ie. balance, device delete, shrink), the swapfiles cannot be temporarily activated. The operation must finish first.
    # truncate -s 0 swapfile
    # chattr +C swapfile
    # fallocate -l 2G swapfile
    # chmod 0600 swapfile
    # mkswap swapfile
    # swapon swapfile
    I may give it a try to see how well it supports suspension or hibernation.
    https://www.how2shout.com/linux/how-...s-focal-fossa/
    "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
    – John F. Kennedy, February 26, 1962.

    #2
    Well, I gave it the old college try.
    I wanted to put the swapfile inside a BTRFS subvolume so that it wouldn't (and couldn't) be snapshotted when I made a / snapshot.

    However, I could not get the swapfile to mount during boot.

    And, step 5 in JWilliker's howto
    • Add the subvolume to /etc/fstab.
      ➜ echo (df --output=source / \
      | tail -n 1)" /swap btrfs defaults,noatime,subvol=swap 0 0" \
      | sudo tee -a /etc/fstab
      /dev/mapper/sda2_crypt /swap btrfs defaults,noatime,subvol=swap 0 0
    for obvious reasons.
    I could manually create a swapfile within a /mnt/@swap subvvolume, size it to 16GB, turn compression off, use noatime, mount it to /swap and turn on the swapfile manually.
    Adding a line in fstab to mount the swap file always failed with the swapfile not being accessible during boot up.
    So, I took an F for the exercise and moved on.
    "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
    – John F. Kennedy, February 26, 1962.

    Comment


      #3
      Hello. Putting together instructions from JWillikers page and modifying them with recommendations I found at
      https://askubuntu.com/questions/1206...pfile-on-btrfs + changing the nomenclature to @swap, to comply with Ubuntu standards + using dd instead of fallocate to allocate space to the swapfile as it can create file-system 'holes' which are incompatible with swapfile usage + omitting the propriety set to "compression none" as btrfs now disables compression when CoW is disabled, the working solution looks like this:

      Code:
      fish
      Mount the root Btrfs filesystem to create a subvolume:
      Code:
      sudo mount (df --output=source / | tail -n 1) /mnt
      Create a dedicated Btrfs subvolume for @swap in order to exclude the swapfile from snapshots:
      Code:
      sudo btrfs subvolume create /mnt/@swap
      Set the appropriate permissions on the swap subvolume so that only the owner, the root user in this case, has access to the subvolume:
      Code:
      sudo chmod 700 /mnt/@swap
      Create a directory at where the swap subvolume will be mounted:
      Code:
      sudo mkdir /mnt/@/swap
      Add the subvolume to /etc/fstab:
      Code:
      echo (df --output=source / \
        | tail -n 1)" /swap btrfs defaults,noatime,subvol=@swap 0 0" \
        | sudo tee -a /etc/fstab
      Now mount the /swap subvolume according to the rules you have just added in fstab:
      Code:
      sudo mount /swap
      Create an empty swapfile within the swap subvolume:
      Code:
      sudo truncate -s 0 /swap/swapfile​
      Disable Copy-on-Write for the swapfile:
      Code:
      sudo chattr +C /swap/swapfile​
      Check RAM size with:
      Code:
      free -h | awk 'NR == 2 {print $2}'
      Allocate the file with as much space as there is RAM on the system (4G as an example):
      Code:
      sudo dd if=/dev/zero of=/swap/swapfile​ bs=1M count=4096
      Only allow access to the swapfile by its owner, the root user, to prevent snooping:
      Code:
      sudo chmod 600 /swap/swapfile
      Initialize the swapfile:
      Code:
      sudo mkswap /swap/swapfile
      Enable the swapfile:
      Code:
      sudo swapon /swap/swapfile
      Add the swapfile to /etc/fstab so that systemd will initialize it automatically when booting the system:
      Code:
      echo "/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
      Activate the new fstab settings:
      Code:
      systemctl daemon-reload' to reload​
      Verify there are no errors in /etc/fstab.
      Code:
      sudo findmnt --verify --verbose
      Verify there are no errors with swap:
      Code:
      sudo swapon -s​
      Set a lower swappiness in an attempt to improve performance:
      Code:
      echo vm.swappiness=10 | sudo tee /etc/sysctl.d/99-swappiness.conf vm.swappiness=10


      To create an encrypted swapfile (and general instructions for encrypted + compressed btrfs install) follow this tutorial: https://mutschler.dev/linux/ubuntu-b...ion-b-swapfile
      Last edited by PrivacyZen; Dec 27, 2022, 08:46 AM.

      Comment


        #4
        I'm just not sure /etc/fstab really is error-free, as:
        Code:
        sudo findmnt --verify --verbose
        returns a warning under "none" category:
        Code:
        [W] non-bind mount source /swap/swapfile is a directory or regular file
        ...any ideas of what this means?
        Last edited by PrivacyZen; Dec 19, 2022, 03:57 PM.

        Comment


          #5
          That looks like there's "$" missing in a few places, for example
          Originally posted by PrivacyZen View Post
          Code:
          sudo mount (df --output=source / | tail -n 1) /mnt
          bash: syntax error near unexpected token `('
          I think there should be "$" before the "(".
          Regards, John Little

          Comment


            #6
            Thanks for your reply, but the syntax is fish shell one (I edited now the instructions to make it more clear) copied from JWillikers page and works fine. The only warning I got is that /swap/swapfile is a directory or regular file, but I found out it's a non-issue as indeed swapfile is a file:
            https://serverfault.com/questions/10...r-file-warning
            so everything works as it should.

            Comment

            Working...
            X