Welcome to the Linux Foundation Forum!

[Lab 16.1] Swap files and openSUSE

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):

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

And then we can see the results:

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

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).

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • To remove and delete the swap file:

    1. opensuse:~ # swapoff /swap/swapfile
    2. opensuse:~ # btrfs subvolume delete /swap
    3. Delete subvolume 285 (no-commit): '//swap'
  • 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.

  • 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

  • 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.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training