How to increase swap size on CentOS 8 / RHEL 8
Swap space is the second type of
memory in modern Linux systems. The primary function of swap space is to
substitute disk space for RAM memory when real RAM fills up and more space is
needed. Linux uses swap space to increase the amount of virtual memory
available to a host. It can use one or more dedicated swap partitions or a swap
file on a regular filesystem or logical volume.
Now we are going to check:
• Current
swap space in the system.
• How to
identify swap volumes in the system.
• How to
increase the size of swap volume.
• How to
add one more swap device to system.
This documentation is based on the test done in a CentOS
4.18.0-80.el8.x86_64 machine. The same procedure can be applied on RHEL 8 as
well. We follow the LVM method to manage the volumes here though a normal
partition also can be used for swap. We will see both the options here when we
add another device as a swap volume.
How to check the current swap size in the system?
We can see the current swap size
either by using the command “free”
or fetching the details from file “/proc/meminfo“. Some examples given below.
[root@labsrv1 ~]# free -h
total used free shared
buff/cache available
Mem: 981Mi 759Mi 82Mi 13Mi 139Mi 76Mi
Swap:
511Mi 0.0Ki 511Mi
[root@labsrv1 ~]#
[root@labsrv1 ~]# cat /proc/meminfo |grep Swap
SwapCached: 4 kB
SwapTotal: 524284 kB
SwapFree: 524016 kB
[root@labsrv1 ~]#
The above commands output show that the system has 512MB
space allocated for swap and at this point only 4KB of that is in use and rest
of the size is free (SwapTotal:
524284 kB).
Sometimes we see that the swap
utilization is more, and we need to allocate more space to swap device to
allocate more pages. Either we extend the existing swap volume (in case of LVM)
or add a new volume to swap. We will see both the options later. If we want to
extend the existing swap volume size, we need to identify the device allocated
for swapping
How to identify the swap volumes in the system?
Some set of commands like “swapon”, “lsblk” etc. will give us the details to identify the swap
volumes. Let’s see how to identify the device from these commands. Please look
at the highlighted areas in the below output.
[root@labsrv1 dev]# swapon -s
Filename Type Size Used
Priority
/dev/dm-1 partition 524284
268 -2
[root@labsrv1 dev]#
Ok. Above command shows that our swap device is “/dev/dm-1”.
[root@labsrv1 dev]# ls -l /dev/dm-1
brw-rw---- 1 root disk
253, 1 Dec 29 22:52 /dev/dm-1
[root@labsrv1 dev]#
This command output shows that the device “dev/dm-1” has major and minor numbers
as 253 and 1.
[root@labsrv1 dev]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0
8G 0 disk
├─vda1 252:1 0
500M 0 part /boot
└─vda2 252:2 0
7.5G 0 part
├─rootvg-rootlv 253:0 0
7G 0 lvm /
└─rootvg-swaplv
253:1 0
512M 0 lvm [SWAP]
vdb 252:16 0
4G 0 disk
[root@labsrv1 dev]#
This command output shows that the numbers 253 and 1 belongs
to the volume “swaplv” in “rootvg”. So our swap device is “/dev/rootvg/swaplv”. If you want to
confirm this see the output from below command.
[root@labsrv1 dev]# ls -l /dev/rootvg/swaplv
lrwxrwxrwx 1 root root
7 Dec 29 22:52 /dev/rootvg/swaplv
-> ../dm-1
[root@labsrv1 dev]#
Since the swap device to be enabled at the system reboot,
the entries for swap device will be available in “/etc/fstab” file as well as below.
[root@labsrv1 ~]# cat
/etc/fstab |grep swap
/dev/mapper/rootvg-swaplv swap swap defaults 0 0
[root@labsrv1 ~]#
We can see that, as we figured out earlier the swap device
is “swaplv” in “rootvg”.
How to increase the size of swap volume?
Now we have the details of swap volumes. Let’s see how to
increase the size of it. To increase the size, you need to:
a. Increase
the size of swap device (if it is an LV).
b. Disable
the swap in that volume.
c. Make
swap FS on the device once again.
d. Enable
the swap o the device.
>> First step, to increase the swap LV size by “lvextend” command. In our lab we have
swap device with 512MB size, and we have 4GB free space available in the “rootvg”. So, we are good to increase
the swap LV size easily. In case you do not have enough free space in the
system, you may need to extend the VG by adding an additional disk. You may
need to refer the LVM guide for extending VG by adding a new disk.
For your reference I have given the output of “lvs” and “vgs” command to see the size of LV and free space in VG.
[root@labsrv1 ~]# lvs
LV
VG Attr LSize
Pool Origin Data% Meta% Move Log Cpy%Sync Convert
rootlv rootvg -wi-ao---- 7.00g
swaplv rootvg -wi-ao---- 512.00m
[root@labsrv1 ~]#
[root@labsrv1 ~]# vgs
VG
#PV #LV #SN Attr VSize VFree
rootvg
2 2 0 wz--n- 11.50g 4.00g
[root@labsrv1 ~]#
Since we have enough free space in the system, I am going to
add one more 512MB to the swap LV.
[root@labsrv1 ~]# lvextend -L +512M /dev/rootvg/swaplv
Size of logical volume rootvg/swaplv changed
from 512.00 MiB (128 extents) to 1.00 GiB (256 extents).
Logical volume rootvg/swaplv successfully
resized.
[root@labsrv1 ~]#
Now the size of swap LV increased to 1GB.
[root@labsrv1 ~]# lvs
LV
VG Attr LSize Pool Origin Data% Meta%
Move Log Cpy%Sync Convert
rootlv rootvg -wi-ao---- 7.00g
swaplv rootvg -wi-ao---- 1.00g
[root@labsrv1 ~]#
Still you will see the size of swap as 512MB by “free” command as we did not increase
the swap size but only the swap LV size.
>> Second step, to disable the existing swap volume
for which the size of LV we increased. Before disabling the swap, please make
sure that the system is swapping very minimal by using that device as an
important factor to be considered especially when you are running the system
with a single swap device.
[root@labsrv1 ~]# swapoff -v /dev/rootvg/swaplv
swapoff
/dev/rootvg/swaplv
[root@labsrv1 ~]#
[root@labsrv1 ~]# free -h
total used free shared
buff/cache available
Mem: 981Mi 730Mi 86Mi 13Mi 164Mi 99Mi
Swap: 0B 0B 0B
[root@labsrv1 ~]#
After disabling the swap, as in the above output we can see
that the swap space size reduced to 0B. This again depends on the configuration of your
system. If you have more than one swap devices configured in your system and
you disabled only one device then it will show the size of remaining device in
instead of 0B.
>> Third step, to recreate the swap formatting in the
entire LV. Earlier we had it only on first 512MB and now we need to have the
same on remaining 512MB too.
[root@labsrv1 ~]# mkswap /dev/rootvg/swaplv
mkswap:
/dev/rootvg/swaplv: warning: wiping old swap signature.
Setting up swapspace
version 1, size = 1024 MiB (1073737728 bytes)
no label,
UUID=b1243731-c803-4dce-9c54-e29d0c7436aa
[root@labsrv1 ~]#
Last step, to re-enable the swapping.
[root@labsrv1 ~]# swapon -v /dev/rootvg/swaplv
swapon:
/dev/mapper/rootvg-swaplv: found signature [pagesize=4096, signature=swap]
swapon:
/dev/mapper/rootvg-swaplv: pagesize=4096, swapsize=1073741824,
devsize=1073741824
swapon
/dev/mapper/rootvg-swaplv
[root@labsrv1 ~]#
Now you will see the swap space increased as 1G as below.
[root@labsrv1 ~]# free -h
total used free shared
buff/cache available
Mem: 981Mi 731Mi 85Mi 13Mi 164Mi 98Mi
Swap:
1.0Gi 0B 1.0Gi
[root@labsrv1 ~]#
Since the system is configured with LVM, it was quite easy
for us to increase the size. Suppose we have whole disk configuration in the
system, then what do we do? Yes, it is possible to add a partition as swap
device. We are going to see that in the next step.
How to add one more swap device for the system?
Two ways to add:
a. Add a
whole disk or disk partition as swap
device
b. Add a
new LV as swap device.
Let’s see both the ways to add swap device. We are planning
to add 1GB device with each method which are whole disk or disk partition and
LV device.
Add a whole disk
or disk partition as swap device:
Before that lets have an agreement of the current size and
number of the swap device.
[root@labsrv1 ~]# free -h
total used free shared
buff/cache available
Mem: 981Mi 730Mi 85Mi 13Mi 164Mi 98Mi
Swap:
1.0Gi 0B
1.0Gi
[root@labsrv1 ~]#
[root@labsrv1 ~]# swapon -s
Filename Type Size Used
Priority
/dev/dm-1 partition 1048572 0 -2
[root@labsrv1 ~]#
Current swap space
: 1GB
Number of swap
devices : 1
Expected swap size
after new device addition : 2GB
Expected number of
swap devices : 2
I have added one 1GB disk to the system. The name of the device is “/dev/vdc” as shown below.
[root@labsrv1 ~]# fdisk -l|grep vd
Disk /dev/vda: 8 GiB,
8589934592 bytes, 16777216 sectors
/dev/vda1 *
2048 1026047 1024000
500M 83 Linux
/dev/vda2 1026048 16777215 15751168 7.5G 8e Linux LVM
Disk /dev/vdb: 4 GiB,
4294967296 bytes, 8388608 sectors
Disk /dev/vdc: 1 GiB, 1073741824 bytes,
2097152 sectors
[root@labsrv1 ~]#
• Create
swap format in /dev/vdc.
[root@labsrv1 ~]# mkswap /dev/vdc
Setting up swapspace
version 1, size = 1024 MiB (1073737728 bytes)
no label,
UUID=ab8f7372-4473-47a7-92ca-3119179a5fee
[root@labsrv1 ~]#
• Enable
new swap device.
[root@labsrv1 ~]# swapon -v /dev/vdc
swapon: /dev/vdc:
found signature [pagesize=4096, signature=swap]
swapon: /dev/vdc:
pagesize=4096, swapsize=1073741824, devsize=1073741824
swapon /dev/vdc
[root@labsrv1 ~]#
Current swap size is 2GB.
[root@labsrv1 ~]# free -h
total used free shared
buff/cache available
Mem: 981Mi 729Mi 75Mi 12Mi 176Mi 101Mi
Swap:
2.0Gi 8.0Mi 2.0Gi
[root@labsrv1 ~]#
Number of devices for swap are 2 now.
[root@labsrv1 ~]# swapon -s
Filename Type Size Used
Priority
/dev/dm-1 partition 1048572 8192 -2
/dev/vdc partition 1048572 0 -3
[root@labsrv1 ~]#
Since we added a new device, we need to add the
corresponding entries in “/etc/fstab”
file to get the swap enabled at system reboot for this device too. Add the
below entry in “/etc/fstab”
file.
/dev/vdc swap
defaults 0 0
Add a new LV as swap
device:
Current swap space
: 2GB
Number of swap
devices : 2
Expected swap size
after new device addition : 3GB
Expected number of
swap devices : 3
• Create a
new LV with 1GB size. The new LV name is “swaplv2”.
[root@labsrv1 ~]# lvcreate -L 1GB -n swaplv2 rootvg
Logical volume "swaplv2" created.
[root@labsrv1 ~]#
The new LV “swplv2” is mapping to “/dev/dm-2” as shown in
the below output. We expect ”/dev/dm-2“ as the third swap device.
[root@labsrv1 ~]# ls -l /dev/rootvg/swaplv2
lrwxrwxrwx 1 root root
7 Dec 30 00:14 /dev/rootvg/swaplv2 -> ../dm-2
[root@labsrv1 ~]#
• Create
swap format on /dev/rootvg/swaplv2.
[root@labsrv1 ~]# mkswap /dev/rootvg/swaplv2
Setting up swapspace
version 1, size = 1024 MiB (1073737728 bytes)
no label,
UUID=a75a8d74-61f8-49da-8a45-d216344487d2
[root@labsrv1 ~]#
• Enable
the swap device /dev/rootvg/swaplv2.
[root@labsrv1 ~]# swapon -v /dev/rootvg/swaplv2
swapon: /dev/mapper/rootvg-swaplv2:
found signature [pagesize=4096, signature=swap]
swapon:
/dev/mapper/rootvg-swaplv2: pagesize=4096, swapsize=1073741824,
devsize=1073741824
swapon
/dev/mapper/rootvg-swaplv2
[root@labsrv1 ~]#
Now we can see our system is running with 3GB swap space.
[root@labsrv1 ~]# free -h
total used free shared
buff/cache available
Mem: 981Mi 731Mi 67Mi 12Mi 182Mi 99Mi
Swap: 3.0Gi
8.0Mi 3.0Gi
[root@labsrv1 ~]#
Number of swap devices became 3.
[root@labsrv1 ~]# swapon -s
Filename Type Size Used
Priority
/dev/dm-1 partition 1048572 8192 -2
/dev/vdc partition 1048572 0 -3
/dev/dm-2 partition 1048572 0 -4
[root@labsrv1 ~]#
As I mentioned above. Add the below entry to “/etc/fstab” file to bring this swap
device enabled during system reboot.
/dev/rootvg/swaplv2
swap defaults 0 0
That is all about swap device configuration. If you found
this useful or you have any suggestions/questions, then please leave a comment
below. You may get in touch with us by an email too.
Bet365 Bet Credits | T&C's Apply - Thakasino
ReplyDeleteBet365 Sign Up Bonus. Bet Credits. bet365 T&Cs 샌즈카지노 Apply. Bet365 Sign Up Bonus. Bet365 Promo Codes. Promo 다파벳 Codes For Dec 2021. Use code MAXBONUS for more info.