Besides the RAM there is a so called Swap, which is a virtual memory, where RAM content (pages) could be swapped-in in case there is not enough RAM available anymore. This swap is located on the disc and since disc reads and writes are slower than reading from RAM, accessing memory pages there will result in a delay. In addition to filesystem creation, Linux administrators also has to pay close attention to swap usage and needs. The post describes 3 ways to add swap space to Linux systems:
1. adding swap partition
2. adding swap volume
3. adding swap file
1. Adding swap partition
1. Run the fdisk or parted utility to identify disks with sufficient unused (unpartitioned) disk space. Create a new partition of type swap (82). For example:
# fdisk /dev/sdb ..... Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-652, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +1G
Make sure you modify the partition type to 82 (Linux swap / Solaris) and save the partition table at the end.
Command (m for help): t Selected partition 1 Hex code (type L to list codes): 82 Changed system type of partition 1 to 82 (Linux swap / Solaris) Command (m for help): w The partition table has been altered!
2. Run the command mkswap against the device/partition created earlier using fdisk/parted. Optionally -L can be used to set LABEL on the swap partition.
# mkswap -L swap1 /dev/sdb1 Setting up swapspace version 1, size = 1060252 KiB LABEL=swap1, UUID=f0a884c9-59b3-47d2-9f06-5884f6a33806
3. Modify the /etc/fstab file to mount the new swap device on boot.
vi /etc/fstab /dev/sdb1 swap swap defaults 0 0
4. Run the swapon command to enable all swap devices listed in the /etc/fstab file and verify the added swap using “swap -s”.
# swapon -a # swapon -s Filename Type Size Used Priority /dev/sdb1 partition 1060248 0 -1
1. Adding swap volume
1. We can also add LVM volume as a swap space. To do so we need to create a volume in any one of the existing volume groups.
# vgs VG #PV #LV #SN Attr VSize VFree vg_geeklab 1 1 0 wz--n- 19.51g 1.94g
# lvcreate -L 1G -n lv_swap vg_geeklab Logical volume "lv_swap" created
# ll /dev/mapper/vg_geeklab-lv_swap lrwxrwxrwx. 1 root root 7 Oct 22 19:36 /dev/mapper/vg_geeklab-lv_swap -> ../dm-1
2. Run the mkswap command against the volume and optionally label it using the -L option.
mkswap -L swap /dev/mapper/vg_geeklab-lv_swap mkswap: /dev/mapper/vg_geeklab-lv_swap: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 1048572 KiB LABEL=swap, UUID=6d228d34-b67e-4e38-82d4-f068c7bc097e
4. To make the swap entry persist across reboots, make an entry into the /etc/fstab file.
# vi /etc/fstab /dev/mapper/vg_geeklab-lv_swap swap swap defaults 0 0
5. Finallt, enable the swap using the swapon command which in turn reads the /etc/fstab file for swap entries.
# swapon -a # swapon -s Filename Type Size Used Priority /dev/dm-1 partition 1048568 0 -1
3. Adding swap file
1. In case if there is no free space left on disk to create new partition, we can also use file as swap. To do so, use dd command to create a file of required size.
# dd if=/dev/zero of=/root/swapfile01 bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 6.73334 s, 159 MB/s
2. I’ve created the swap file in /root directory to make it more secure as normal user can’t enter that directory. To make is more secure apply appropriate permissions to the file.
# chmod 600 /root/swapfile01
3. Run mkswap command to convert the file to a swap file.
# mkswap -L swap01 /root/swapfile01 Setting up swapspace version 1, size = 1048572 KiB LABEL=swap01, UUID=ba5f7163-43f6-4001-84c1-2fca3935301f
4. Modify the /etc/fstab file to add the new swap file.
# vi /etc/fstab /root/swapfile01 swap swap defaults 0 0
5. Run the swapon command to enable all swap devices listed in the /etc/fstab file.
# swapon -a # swapon -s Filename Type Size Used Priority /swap/swapfile01 file 1048568 0 -1
Removing swap
In case you want to remove added swap, you can do it with swapoff command. Make sure you remove the respective swap entry from the /etc/fstab file after disabling the swap.
# swapoff -a ### this will remove all swap(s) mentioned in .etc.fstab # swapoff /dev/sdb1 ### to remove individual swap
How to check swap usage
To check swap usage, you can use any one of the utilities below:
1. free
# free total used free shared buffers cached Mem: 1004608 934036 70572 0 30124 769640 -/+ buffers/cache: 134272 870336 Swap: 1060248 0 1060248
2. /proc/meminfo
# grep Swap /proc/meminfo SwapCached: 0 kB SwapTotal: 1060248 kB SwapFree: 1060248 kB
3. top
# top ....... Mem: 1004608k total, 934780k used, 69828k free, 30124k buffers Swap: 1060248k total, 0k used, 1060248k free, 770156k cached .......
4. vmstat
# vmstat procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 69828 30124 770156 0 0 40 403 23 22 0 1 99 0 0