How to Increase SWAP Partition on Linux

  • by
Let's see the current size of SWAP.
[root@test ~]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       4095996 0       -1

We have 4GB in a partition for SWAP mounted at /dev/dm-1. (Note: This command is equivalent to swapon -s.)

There're two ways to increase the SWAP size of a host.
  1. Add a swap file.
  2. This could be the easiest way to do it. Please refer to the following post for more information.
    How to Enable a Swap File on CentOS 6.5
  3. Increase SWAP logical volume
  4. First, let's check the name of the logical volume of SWAP in this host.
    [root@test ~]# lvdisplay
    ...
      --- Logical volume ---
      LV Path                /dev/vg_test/lv_swap
      LV Name                lv_swap
      VG Name                vg_test
      LV UUID                219bOE-3aD7-UPX4-IS51-5QZd-X2nG-dLh1fY
      LV Write Access        read/write
      LV Creation host, time localhost.localdomain, 2015-07-29 22:47:16 +0800
      LV Status              available
      # open                 2
      LV Size                3.91 GiB
      Current LE             1000
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           252:1

    Next, let's see how much free space we have now.
    [root@test ~]# vgs
      VG           #PV #LV #SN Attr   VSize  VFree
      vg_test        1   3   0 wz--n- 79.51g 5.60g

    You can see there's 5.6GB left in the volume group vg_test. If there's no enough space for SWAP. you may reduce the size of other logical volumes to squeeze the space:
    How to Reduce Volume Size on Enterprise Linux

    Turn the SWAP volume off.
    [root@test ~]# swapoff /dev/vg_test/lv_swap
    Add more space (i.e. +2GB) to the SWAP volume.
    [root@test ~]# lvextend -L +2GB /dev/vg_test/lv_swap
      Size of logical volume vg_test/lv_swap changed from 3.91 GiB (1000 extents) to 5.91 GiB (1512 extents).
      Logical volume lv_swap successfully resized

    Reformat the SWAP volume.
    [root@test ~]# mkswap /dev/vg_test/lv_swap
    mkswap: /dev/vg_test/lv_swap: warning: don't erase bootbits sectors
            on whole disk. Use -f to force.
    Setting up swapspace version 1, size = 6193148 KiB
    no label, UUID=5c2765cf-9efe-44be-9fa0-679f185b2086

    Turn the SWAP volume on.
    [root@test ~]# swapon /dev/vg_test/lv_swap
    Let's see the result.
    [root@test ~]# cat /proc/swaps
    Filename                                Type            Size    Used    Priority
    /dev/dm-1                               partition       6193148 0       -1

Leave a Reply

Your email address will not be published. Required fields are marked *