Sunday 10 November 2019

How to Increase Swap Memory in Centos 7 (Using HDD Partition)




Linux distributions has the facility to reserve some amount of disk space to use as virtual memory when the amount of physical memory (RAM) is full. Swapping is a method when the RAM is fully utilized, inactive pages in the memory are moved to the swap space from the RAM to swap space.

This article describes How to increase swap memory in Centos 7 using HDD partition when the Linux machine is running out of swap space.

Step 1. Swap Space Check: Run the following command to check existing swap space:

[root@linuxcnf ~]# swapon -sh
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1048572 0       -2
[root@linuxcnf ~]#

[root@linuxcnf ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1838         140        1528           8         169        1549
Swap:          1023           0        1023
[root@linuxcnf ~]#

In the above output the existing swap size is around 1 GB. We will extend swap space from 1 GB to 2 GB.

Step 2. Prepare Swap Partition: I have added additional hard disk on the system to use swap. Run the following command to list attached hard disks. We will use the 2 GB hard disk /dev/sdb shown in the below output:

[root@linuxcnf ~]# fdisk -l|grep Disk
…………..
Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 sectors
…………..
[root@linuxcnf ~]#

Create a new partition size of 1 GB and change partition type to 82 (Linux swap / So) using fdisk command as shown below:

[root@linuxcnf ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xd7e3ca4b.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-4194303, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): +1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@linuxcnf ~]#

Check the list of partitions:

[root@linuxcnf ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd7e3ca4b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   82  Linux swap / Solaris
[root@linuxcnf ~]#

Step 3. Enabling Swap Space: Run the following command to make the partition as swap partition:

[root@linuxcnf ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=59b77b46-d20f-473c-b06a-14c801ae118d
[root@linuxcnf ~]#

Enable the swap partition as swap space:

[root@linuxcnf ~]# swapon /dev/sdb1
[root@linuxcnf ~]#

Add the below entry in /etc/fstab configuration file to auto mount swap space on reboot:

[root@linuxcnf ~]# vi /etc/fstab
………….
/dev/sdb1               swap                    swap    defaults        0 0
[root@linuxcnf ~]#

Save and exit from the file.

Step 4. Verify the Swap Space: Swap configuration is done now run the following commands to verify swap space:

[root@linuxcnf ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdb1                               partition       1048572 0       -3
/dev/dm-1                               partition       1048572 0       -2
[root@linuxcnf ~]#

[root@linuxcnf ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1838         141        1524           8         171        1548
Swap:          2047           0        2047
[root@linuxcnf ~]#

Done!!! Now Swap Space Increased from 1 GB to 2 GB.

No comments:

Post a Comment