Announcement

Collapse
No announcement yet.

Enlarging a BTRFS swap file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [System] Enlarging a BTRFS swap file

    How do I enlarge my BTRFS swap file as the kernel keeps killing steam due to low memory. My SSD is 2 TB?
    Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

    http://www.kubuntu.org/getkubuntu

    #2
    AI Overview

    You must disable, delete, and create a new swap file to increase its size, as there is no command to directly resize an existing btrfs swap file. First, disable the current swap file, then create a new one of the desired size, format it, and activate it.

    Step-by-step instructions

    Disable the swap file

    Turn off the current swap file using this command:

    sudo swapoff /swapfile

    Delete the old swap file

    Remove the old file to free up space:

    sudo rm /swapfile

    Create a new swap file

    Create a new, larger file. Replace 1024 with your desired size in megabytes (e.g., 4096 for a 4GB file):

    sudo fallocate -l 4G /swapfile

    Alternatively, you can use dd:

    sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

    Set permissions

    Set the correct permissions for the new swap file:

    sudo chmod 600 /swapfile

    Format the new file as swap

    Format the file to be used as a swap area:

    sudo mkswap /swapfile

    Activate the new swap file

    Turn on the new swap file:

    sudo swapon /swapfile

    Verify the change

    Check if the new swap file is active and has the correct size:

    sudo swapon -s

    Make the change permanent (optional but recommended)

    To ensure the new swap file is mounted after a reboot, add it to your /etc/fstab file.
    Open the file with a text editor: sudo nano /etc/fstab
    Add the following line to the file:

    /swapfile swap swap defaults 0 0

    Save the file and exit the editor.​
    Windows no longer obstruct my view.
    Using Kubuntu Linux since March 23, 2007.
    "It is a capital mistake to theorize before one has data." - Sherlock Holmes

    Comment


      #3
      My vanilla-ish 25.10 fresh install swap file was created in @swap; OP if you don't find it in /swapfile you might mount the btrfs top level and look for it there.
      Regards, John Little

      Comment


        #4
        Mount your root file system (I'll use /mnt). Then;

        Code:
        sudo swapoff -a
        sudo rm /mnt/@swap/swapfile
        sudo btrfs fi mkswapfile --size 4g /mnt/@swap/swapfile
        sudo swapon​
        The above command list assumes you have a "normal" (not custom or modified) installation.

        Change 4g to something larger if you want.
        Last edited by oshunluvr; Nov 18, 2025, 02:25 PM. Reason: corrected command

        Please Read Me

        Comment


          #5
          Originally posted by oshunluvr View Post
          Mount your root file system (I'll use /mnt). Then;

          Code:
          sudo swapoff
          sudo rm /mnt/@swap/swapfile
          sudo btrfs fi mkswapfile --size 4g /mnt/@swap/swapfile
          sudo swapon​
          The above command list assumes you have a "normal" (not custom or modified) installation.

          Change 4g to something larger if you want.
          steve7233@doctortux:~$ sudo swapoff
          [sudo: authenticate] Password:
          swapoff: bad usage
          Try 'swapoff --help' for more information.
          steve7233@doctortux:~$


          I tried to figure it out:

          steve7233@doctortux:~$ swapoff --help

          Usage:
          swapoff [options] [<spec>]

          Disable devices and files for paging and swapping.

          Options:
          -a, --all disable all swaps from /proc/swaps
          -v, --verbose verbose mode

          -h, --help display this help
          -V, --version display version

          The <spec> parameter:
          -L <label> LABEL of device to be used
          -U <uuid> UUID of device to be used
          LABEL=<label> LABEL of device to be used
          UUID=<uuid> UUID of device to be used
          <device> name of device to be used
          <file> name of file to be used

          For more details see swapoff(8).
          steve7233@doctortux:~$ sudo swapoff -a
          [sudo: authenticate] Password:
          steve7233@doctortux:~$ sudo rm /mnt@swap/swapfile
          rm: cannot remove '/mnt@swap/swapfile': No such file or directory
          steve7233@doctortux:~$ sudo btrfs fi mkswapfile --size 64g /mnt/@swap/swapfile
          ERROR: cannot create new swapfile: No such file or directory
          steve7233@doctortux:~$




          Last edited by steve7233; Nov 18, 2025, 02:22 PM.
          Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

          http://www.kubuntu.org/getkubuntu

          Comment


            #6
            You left out the / on the rm command.

            Why don't you do "ls" and see whats in there,

            Please Read Me

            Comment


              #7
              You also didn't mount the root file system at /mnt

              Please Read Me

              Comment


                #8
                Originally posted by oshunluvr View Post
                You left out the / on the rm command.

                Why don't you do "ls" and see whats in there,
                steve7233@doctortux:~$ ls
                DesktopDocumentsDownloadsMusicPicturesPublicTemplatesVideossnapstl
                steve7233@doctortux:~$


                Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

                http://www.kubuntu.org/getkubuntu

                Comment


                  #9
                  I just got up recently and my brain was firing all its neurons yet. I just realized I can't do this from the installed system. I have to use a USB stick.
                  Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

                  http://www.kubuntu.org/getkubuntu

                  Comment


                    #10
                    I don't have a 25.10 or 25.04 USB stick, but I do have a 24.04 USB stick. Can I use the 24.04 USB stick or make a 25.10 startup disk, or do I have to download the full 25.10 and make a USB stick from that?
                    Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

                    http://www.kubuntu.org/getkubuntu

                    Comment


                      #11
                      Originally posted by steve7233 View Post
                      I just got up recently and my brain was firing all its neurons yet. I just realized I can't do this from the installed system. I have to use a USB stick.
                      Normally You should only need a USB stick for the classical method with a Swap Partition.
                      In your case you should be able to do it from your running Kubuntu. You have btrfs and a Swap File.

                      Comment


                        #12
                        Originally posted by INVICTRA View Post

                        Normally You should only need a USB stick for the classical method with a Swap Partition.
                        In your case you should be able to do it from your running Kubuntu. You have btrfs and a Swap File.
                        How does that work?
                        Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

                        http://www.kubuntu.org/getkubuntu

                        Comment


                          #13
                          I thought I had figured it out, but as you can see, /home and swap are both 21%. How do I make it permanent once I've sorted it out?
                          steve7233@doctortux:~$ sudo swapon
                          [sudo: authenticate] Password:
                          steve7233@doctortux:~$ df
                          Filesystem 1K-blocks Used Available Use% Mounted
                          on
                          tmpfs 393808 1948 391860 1% /run
                          /dev/sda1 1953513560 403191348 1549107932 21% /
                          tmpfs 1969024 517536 1451488 27% /dev/sh
                          m
                          tmpfs 5120 12 5108 1% /run/lo
                          ck
                          tmpfs 1024 0 1024 0% /run/cr
                          edentials/systemd-journald.service
                          tmpfs 1024 0 1024 0% /run/cr
                          edentials/systemd-resolved.service
                          /dev/sda1 1953513560 403191348 1549107932 21% /swap
                          /dev/sda1 1953513560 403191348 1549107932 21% /home
                          tmpfs 1969024 10124 1958900 1% /tmp
                          tmpfs 393804 88 393716 1% /run/us
                          er/1000
                          /dev/sda1 1953513560 403191348 1549107932 21% /mnt
                          steve7233@doctortux:~$

                          Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

                          http://www.kubuntu.org/getkubuntu

                          Comment


                            #14
                            I got a little further:
                            steve7233@doctortux:~$ sudo chmod 600 /mnt/@swap
                            steve7233@doctortux:~$ sudo swapon /mnt/@swap
                            swapon: /mnt/@swap: read swap header failed
                            steve7233@doctortux:~$


                            Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

                            http://www.kubuntu.org/getkubuntu

                            Comment


                              #15
                              Ubuntu and Kubuntu do it a bit different I think. Find out if your swapfile is at /@swap/swapfile or /swap/swapfile or deleted. Start with:
                              Code:
                              swapon --show
                              ​If no output then swapfile is already deleted. In a new Kubuntu 25.10 the file is located in /swap/swapfile
                              Check your fstab text file if it is the same there.
                              Click image for larger version  Name:	Screenshot_20251120_233554.png Views:	0 Size:	451.9 KB ID:	689402

















































                              Turn off swap if it is still there:
                              Code:
                              sudo swapoff /swap/swapfile
                              ​Remove the file.
                              Code:
                              sudo rm /swap/swapfile



                              To create a new swapfile located in folder /swap/ that is mounted from the btrfs subvolume @swap for kubuntu:
                              Code:
                              sudo fallocate -l 16G /swap/swapfile
                              Code:
                              sudo chattr +C /swap/swapfile
                              Code:
                              sudo chmod 600 /swap/swapfile
                              Code:
                              sudo mkswap /swap/swapfile
                              Code:
                              sudo swapon /swap/swapfile

                              chattr +C is recommended to use on swapfile in btrfs filesystems.
                              Last edited by INVICTRA; Nov 20, 2025, 04:59 PM.

                              Comment

                              Users Viewing This Topic

                              Collapse

                              There are 0 users viewing this topic.

                              Working...
                              X