3
February
2022
15:46

Creating a swap file (SWAP) in Linux as a file in the root (/) of the disk

3 February 2022 15:46

In Windows, you can change the size of the paging file. which cannot be said about Linux - during the initial partitioning of the disk, a SWAP partition of a fixed size is created. This is not at all convenient, because... You cannot change the size of the paging file. The solution is to implement the swap file as a "swapfile" file in the root of the disk.

The original problem was indirectly related to the page file - the fsck check command was throwing an error "fsck: /etc/fstab: parsing error on line 11" and finished the job.

I had to delete line 11, which indicated the UUID of the disk where the paging file was located.
But you can't work without a swap file.

Team

free -m

should show that the swap file is in use (second line):

всего занято свободно общая буф./врем. доступно
Память: 2974 815 1039 54 1119 1943
Подкачка: 2047 0 2047

While solving this problem, I found an interesting way to improve the performance of Linux - creating a swap file as a file in "root" disk. The advantages of this method: more reliable operation (when connecting the main partition via fstab, swap is automatically connected) and the ability to easily change the size of the swap file. When using SSD drives, it does not matter at all, in terms of speed, where the paging file is physically located - at the beginning of the drive or at the end.

The commands are as follows:

1)Reboot the computer. In the initial menu of GRUB2, select "Advanced system settings"-"root",
enter the password and get into the superuser command line (#).

2) Create an empty file for the swap file in the root of the disk (for a SWAP file size of 2 GB, the command is as follows)
and assigning rights, enabling SWAP:

dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

3) In order for this file to be used when the system boots, edit the file /etc/fstab.

The old line related to SWAP should be deleted (CTRL+ K), and the following should be added instead:

/swapfile none swap defaults 0 0

4) I run the command to update the system kernel:

sudo update-initramfs -u

5) Reboot.

shutdown -rF now

Now in the system program "gparted" you can delete the "swap" partition: it is no longer used! We expand the main partition "/" or the home partition "/home" (Right mouse button - Resize | Move) using the free space from the former "swap" partition.


To fsck the entire working disk from which the operating system runs:

sudo touch /forcefsck

We send the computer to reboot. If there are problems in the EXT4 file system, it will be checked and fixed at the next startup:

shutdown -rF now

To change the SWAP size in the future, just boot into "Advanced recovery options", go to root again, delete the file

swapoff /swapfile
rm /swapfile

and repeat the commands from point 2 above, but with a different page file size, for example, for 4 GB:

dd if=/dev/zero of=swapfile bs=1M count=4096
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

Check:

free -m


Related publications