Skip to main content
21-03-2021

SwapSince a couple of years ago, Linux distributions use a swap file (to exchange with Ram memory when it is exhausted or to hibernate the system) instead of a swap partition. If, like I do, instead of clean installing version upgrading is used, it is possible that the system still uses the swap partition. In may case I use Ubuntu and both for the desktop and the laptop computers the first installation was done more than 3 years ago, and afterwards I upgraded the system several times. In this article it is shown how to convert a Swap partition to a a Swap file.

We first disable the swap:

sudo swapoff -a

Next, we create the swap file. The recommended size can be found in the table shown in this link:

https://help.ubuntu.com/community/SwapFaq

The following command will create the swapfile:

sudo fallocate -l 3G /swapfile

This creates a swap file of 3Gb, which has to be adjusted for each system. This creates the file in the root of the system (/) but it can be changed if not enough space is available at this partition.

Next, we restrict the file permisions:

sudo chmod 600 /swapfile

Only "root" will be able to write and read this file. We now dedicate this file to swap:

sudo mkswap /swapfile

Last, we have to assure that the swap file is mounted each time the system starts:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

The only remaining thing to do is to erase the old swap partition and reuse the freed space joining it to another partition or whatever is wanted, which we can do with gparted or any other of the available tools for this.

Finally, the reference to the swap partition has to be erased from the initramfs utility and update grub. First comment out (by adding # as first character) the line mentioning the swap partition in the following file:

/etc/initramfs-tools/conf.d/resume

and update grub and run initramfs:

update-grub

update-initramfs -u
Powered by Drupal

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer