Swap space is used when the amount of physical memory (RAM) is full. If the system needs more memory and no more RAM is available, inactive pages in memory are moved to the swap space. Swap should not be considered as a replacement of RAM memory because swap space is on hard drives and I/O access to hard drivers is slower than l/O access memory.
The swap must be located on a dedicated swap partition and it is designed to help RAM memory not to replace it.
SWAP Partition
Using parted a swap partition can be created in order to be added to the existing swap (or just to create it for the first time):
#parted /deb/sdb
GNU parted 2.1
Using /dev/sdb
Welcome to GNU parted! Type ‘help’ to view qa list of commands.
(parted) mkpart
Partition type? Primary/extended? P
File system type? [Ext2]? Linux-swap
Start? 1
End? 512M
(parted) quit
Now /dev/sbd1 is a 512M swap partition
Next step is creating the swap filesystem on the swap partition:
#mkswap/dev/sdb1
Setting up swapspace version 1, size =498684 KiB
No label, UUID=81631aa8-8064-47fc-92ef-5eb3fa6e8b87
Once the swap filesystem has been created the final step is active the new swap space:
#free | grep Swap
Swap: 1736696 0 1736696
The initial swap size is 1736696 KB
#swapon/dev/sdb1
It activate the new swap
#free | grep Swap
Swap: 2235376 0 2235376
After the new swap (512 M) has been added the new swap size is 2.2G
Adding the corresponding line to /etc/fstab the system will activate automatically the swap on boot:
#echo “/dev/sdb1 swap swap defaults 0 0”>> /etc/fstab
LVM Swap
As normal partition an LV partition can be used to store a file system. Just create a LV for swap, create the swap filesystem on it and activate it as a standard swap partition:
#lvcreate –L512M –n VolGroup01 Swap VolGroup01
Logical volume “VolGroup01Swap” created
Create swap filesystem on LV VolGroup Swap:
#mkswap –f /dev/VolGroup01/VolGroup01Swap
Setting up swapspace version 1, size = 524284 KiB
No label,UUID=146df94a-4ddc-4e03-b7b7-879bb4db64ea
Configure the swap on/etc/fstab and activate it:
#echo “/dev/VolGroup01/VolGroup01Swap swap swap defaults 0 0”>> /etc/fstab
#swapon –a
It activates all swap partitions configured on/etc/fstab
#free | grep Swap
Swap: 2235376 0 2235376
After the new LV swap (512 M) has been added the new swap size is 2.2G
Extending Swap on LVM
As all LVM partitions, the swap LV partition can be extended/reduced. In this way the swap area can be modified via LVM:
Disable swap on the Swap-LV
#swapoff /dev/VolGroup01/VolGroup01swap
Resize the Swap-LV
# lvextended –L +256M /dev/VolGroup01/VolGroup01Swap
Extending logical volume VolGroup01Swap to 768.00 MiB
Logical volume VolGroup01Swap successfully resized
Recreate swap filesystem in the new extended Swap-LV:
#mkswap –f /dev/VolGroup01/VolGroup01Swap
Setting up swapspace version 1, size = 786428 KiB
No label, UUID=675d6278-72cd-457a-96ad-b18c81d25b3f
Activate the new swap on extended Swap-LV:
#swapon-a
Verify the new Swap
#free | grep Swap
Swap:2523120 0 2523120
After the swap-LV extension there are 2.5G of swap available.