RedHat OS uses swap as a type of “Virtual Memory “in case your physical memory begins to run low. This capacity can be helpful for the system that don’t have enough physical memory ( RAM ).
You can define swap on your existing file system wherever you got free space instead of arranging a separate partition.

Follow the steps to achieve these settings.

`

Steps-1

Check the free space available on your existing storage.

[root@test /]# df –h

Output

Filesystem Size Used Avail Use{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be} Mounted on
/dev/mapper/vg_test-lv_root
7.5G 3.5G 4.8G 47{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be} /
tmpfs 495M 260K 495M 1{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be} /dev/shm
/dev/sda1 485M 33M 428M 8{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be} /boot

I have 4.8G available on my root ‘/’.

Step-2
Create swap on ‘/’ (root)

We will create 1G file using ‘dd’ command to create an output file size which is desirable.

dd if=/dev/zero of=/swap1Gfile bs=1024 count=1048576

Output:

1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 34.4907 s, 31.1 MB/s

Note: “count” calculations based on 1024×1024
It will create a 1 GB file on root partition.

Step-3

Now we have ‘/swap1Gfile’ swap on root it’s time to overlay swap file system on it.
let’s do it .

mkswap /swap1Gfile

Output :

mkswap: /swap1Gfile: warning: don’t erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=630c36e3-4a50-4a9b-99a5-08b849813e10

Step-4

Using ‘swapon’ we will Active swap to perform as an additional virtual memory in case system kernel is in panic situation just cause of shortage of RAM.

swapon -v /swap1Gfile

Output :

swapon on /swap1Gfile
swapon: /swap1Gfile: insecure permissions 0644, 0600 suggested.
swapon: /swap1Gfile: found swap signature: version 1, page-size 4, same byte order
swapon: /swap1Gfile: pagesize=4096, swapsize=1073741824, devsize=1073741824

Step-5
Verify it’s loaded or not

swapon -s

Output :

Filename Type Size Used Priority
/dev/dm-1 partition 2031608 0 -1
/swap1Gfile file 1048568 0 -2

Step-6
 

 

For making this changes permanent lets add this swap file as a swap storage into the ‘/etc/fstab’ file to survive on reboot.

Add the below line in your ‘/etc’fstab’ file.

/swap1Gfile swap swap defaults 0 0

Step-7
Once off swap partitions in order to read ‘/etc/fstab’ changes.

swapoff -a

Use this below command to read swap partitions from ‘/etc/fstab’ file.

swapon -a

Verify it works or not

swapon -s

Output :

Filename Type Size Used Priority
/dev/dm-1 partition 2031608 0 -1
/swap1Gfile file 1048568 0 -2

Let me know if you have any questions below in Comments Section .

Similar Posts