How to use swap file instead of swap partition on Linux

This article explains how to transition from having a swap partition to swap files. If you do not need to disable any existing swap partition, but just create a swap file and activate it, just skip steps 1 and 2.
On the Ubuntu 18.04 desktop, I have a fairly large swap partition, and I want to use it for other purposes, and then move the swap to a file. Ubuntu 18.04 already uses swap files instead of swap partitions by default. However, I upgraded to the latest Ubuntu version instead of performing a fresh installation, so my system continues to use swap partitions. Therefore, I had to move the swap to a file myself, so the following instructions were tested on my Ubuntu 18.04 desktop. They should work on any Linux distribution.
It is important to mention You cannot use swap files with the BTRFS file system (Thanks to Isaac for mentioning this in the comments).
In addition, when using swap files, hibernation (to disk) will no longer be available. I can do this, but I can’t test it because I can’t switch to the swap file on my system before returning from hibernation, so I just gave up using hibernation. Also, by default, most Linux distributions use pause (to RAM) instead of hibernation (to disk) by default. If you need to use the swap file to enable sleep mode, please provide some information Here. Pause (to ram) is not affected by this.

How to move swap to files on Linux file system

1. Close the current swap partition.
To view the active swap partition, run:

swapon -s

In my case, the command output is as follows:

Filename    Type        Size       Used   Priority
/dev/sda5   partition   15624188   0      -2

Now, you can use the following command to shut down the current switching device:

sudo swapoff /dev/sdXX

Where /dev/sdXX Is the device listed swapon -s Command (under the “File Name” section- /dev/sda5 In the above example, make sure to replace it with swap partition 2. Remove old swap entries from /etc/fstab file.
To delete the old exchange entry, open /etc/fstab Use a text editor as the root file and delete the swap line. Don’t modify /etc/fstab file! Changing anything else in this file may prevent the system from starting!
You can use the Nano editor to open the file from the command line as follows:

sudo nano /etc/fstab

And delete the entry containing the swap partition information (you can also add # In front of it). For example, in my case, the exchange entry looks like this:

UUID=d1b17f9c-9c5e-4471-854a-3ccaf358c30b none swap sw 0 0

As you can see, the exchange entry should contain swap with sw -This way you can know which line to delete (or comment out).
then press Ctrl + O, Then Enter save document. To save the Nano editor after saving the file, press Ctrl + X.3. Create a swap file.
To create a 1GB swap file, use the following command:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

where:

  • /swapfile Is the path and name of the swap file. You can change it to something else.
  • Number behind count (1048576) is equal to 1GB. If you want to use a larger swap file, increase it. For example, if you want to use a 5GB swap file, multiply this number by 5 (so use 5242880 count= 5GB swap file value).

If you use another swap file name and path, make sure to use the name instead of /swapfile In all instructions below 4. Set the swap file permission to 600.
With this feature, other users will not be able to read your swap file, which may contain sensitive information.
To set the swap file permission to 600, use the following command:

sudo chmod 600 /swapfile

5. Format the newly created file as swap:

sudo mkswap /swapfile

6. Enable the newly created swap file:

sudo swapon /swapfile

To verify that the new swap file is used, run:

swapon -s

It should output the following:

Filename    Type   Size      Used   Priority
/swapfile   file   5242876   0      -2

7. Add the newly created swap file to /etc/fstab.
To use the new swap file every time you boot, you need to add it to /etc/fstab file. turn on /etc/fstab Use a text editor (as root), such as Nano:

sudo nano /etc/fstab

And add the following line in this file:

/swapfile none swap sw 0 0

To save the file (if you have used the Nano command line editor), press Ctrl + O, then press Enter. After saving the file, to exit the Nano editor, press Ctrl +X. Again, remember not to modify /etc/fstab file! Changing any other content in this file may prevent the system from booting! 8. For Linux distributions based on Ubuntu and Debian, this step is necessary (I’m not sure if others need to do the same).
You need to edit /etc/initramfs-tools/conf.d/resume File and comment out (add one # At the beginning of the line) RESUME=UUID=... line. In my case, not doing so will result in an additional startup time of about 15-20 seconds. of systemd-analyze blame The command did not provide any information about this happening, so I had to spend a lot of time to find out what caused the delay in the startup.
Fortunately, during the boot process, I noticed a short period of time showing the message “Give up waiting to suspend/resume device” Cause Since the UUID is not exchanged correctly /etc/initramfs-tools/conf.d/resume.
This file is used when resuming from hibernation. Since we no longer have a swap partition, it causes a delay in startup.
Comment out this line /etc/initramfs-tools/conf.d/resume, All you have to do is run the following command:

sudo sed -i 's/^RESUME=UUID/#RESUME=UUID/g' /etc/initramfs-tools/conf.d/resume

You also need to update the initramfs and then complete:

sudo update-initramfs -u

Source

Sidebar