Welcome to the Linux Foundation Forum!

[Lab 16.1] Swap files and openSUSE

albertgoma
albertgoma Posts: 9
edited March 2024 in LFS207 Class Forum

Due to the fact that openSUSE's default filesystem is BTRFS, it doesn't allow creating swap files in the conventional way. The following must be done instead (As seen on the ArchWiki):

opensuse:~ # btrfs subvolume create /swap
Create subvolume '//swap'
opensuse:~ # btrfs filesystem mkswapfile --size 1g --uuid clear /swap/swapfile
create swapfile /swap/swapfile size 1.00GiB (1073741824)
opensuse:~ # swapon /swap
/swap

And then we can see the results:

opensuse:~ # cat /proc/swaps 
Filename                Type        Size        Used        Priority
...
/swap/swapfile                          file        1048572     0       -3
...

I assume the creation of a dedicated subvolume for the swap file must be to prevent it from being accidentally snapshotted alongside with other parts of the filesystem (i.e. by tools such as zypper and YaST when used with Snapper, such as in the default install).

Comments

  • To remove and delete the swap file:

    opensuse:~ # swapoff /swap/swapfile
    opensuse:~ # btrfs subvolume delete /swap
    Delete subvolume 285 (no-commit): '//swap'
    
  • luisviveropena
    luisviveropena Posts: 1,286

    Hi @albertgoma,

    Thanks for the information!

    Due to the fact that openSUSE's default filesystem is BTRFS, it doesn't allow creating swap files in the >conventional way.

    I checked on the openSUSE documentation, and that's right for the root partition. You always can create another partition for specific purposes, including swap, with another filesystem.

    I let this here for reference:

    https://doc.opensuse.org/documentation/leap/archive/42.3/reference/html/book.opensuse.reference/cha.advdisk.html

    Regards,
    Luis.

  • arilou
    arilou Posts: 1
    edited October 13

    The command
    swapon /swap
    has to be changed to
    swapon /swap/swapfile
    to make it work. To make that swap space permanent, it has to be added to /etc/fstab
    /swap/swapfile none swap defaults 0 0

  • luisviveropena
    luisviveropena Posts: 1,286

    Hi @arilou,

    The command
    swapon /swap
    has to be changed to
    swapon /swap/swapfile

    That's not correct, in fact it can be anywhere in the filesystem (except on /tmp probably). The important thing is that you need to apply the 'mkwap swpfile' giving the complete path, or run the command in the same directory of the file.

    I did a test case on openSUSE Leap 16 (as this post was originally created for openSUSE), just to see if there is any specific consideration (and there is, but not in the location of the file), and it worked on /swapfile, look:

    opensuse:/ # pwd
    /
    opensuse:/ # touch swpfile
    opensuse:/ # chattr +C swpfile
    opensuse:/ # dd if=/dev/zero of=swpfile bs=1M count=1024
    1024+0 records in
    1024+0 records out
    1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.08878 s, 986 MB/s
    opensuse:/ #
    opensuse:/ # mkswap swpfile
    mkswap: swpfile: insecure permissions 0644, fix with: chmod 0600 swpfile
    Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
    no label, UUID=b662da04-cc9c-4b08-b941-91e0f8c0df01
    opensuse:/ #
    opensuse:/ # chmod 0600 swpfile
    opensuse:/ # swapon swpfile
    opensuse:/ #
    opensuse:/ # free -h
    total used free shared buff/cache available
    Mem: 3.8Gi 1.2Gi 653Mi 8.5Mi 2.2Gi 2.7Gi
    Swap: 1.0Gi 0B 1.0Gi

    Regards,
    Luis.

Categories

Upcoming Training