8 March 2014

Extend Swap logical volume in Linux

There are times that you need to extend swap partition in your Linux server, here is the step by step:

1. Check current swap size:

# swapon -s
 Filename                           Type            Size    Used    Priority
 /dev/mapper/vg00-lv_swap           partition       8388604 0       -1


or via lvm commands:

# lvdisplay /dev/vg00/lv_swap
  --- Logical volume ---
  LV Name                /dev/vg00/lv_swap
  VG Name                vg00
  LV UUID                8RTlA8-6R64-N3rz-1kcX-TJHX-yLca-sxxbl8
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                8.00 GiB
  Current LE             2048
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     1024
  Block device           253:4


2. Check if there is enough space available in the VG that contains the swap logical volume. In this case there is 10.87GB free space available:

# vgdisplay /dev/vg00
  --- Volume group ---
  VG Name               vg00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                8
  Open LV               8
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               39.87 GiB
  PE Size               4.00 MiB
  Total PE              10206
  Alloc PE / Size       7424 / 29.00 GiB
  Free  PE / Size       2782 / 10.87 GiB
  VG UUID               QjIFPZ-R71S-LY2s-7ysg-10QH-mFGP-lzeDHD

If you don't have enough free size in your volume group, then you need to extend it by adding additional physical disk to it.


3. Proceed to disable swap:

#swapoff -v /dev/vg00/lv_swap
swapoff on /dev/vg00/lv_swap


4. Extend the swap logical volume to desired size:

# lvextend -L16G /dev/vg00/lv_swap
 Extending logical volume lv_swap to 16.00 GiB
 Logical volume lv_swap successfully resized


5. Format new swap partition after resizing its logical volume:

# mkswap /dev/vg00/lv_swap
mkswap: /dev/vg00/lv_swap: warning: don't erase bootbits sectors on whole disk. Use -f to force.
Setting up swapspace version 1, size = 16777212 KiB
no label, UUID=a234f87f-3e19-4f61-868e-9ee2cf574b25


6. Enable swap back:

# swapon -va
swapon on /dev/mapper/vg00-lv_swap
swapon: /dev/mapper/vg00-lv_swap: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/vg00-lv_swap: pagesize=4096, swapsize=17179869184, devsize=17179869184


7. Check new swap size:

# swapon -s
Filename                          Type           Size      Used    Priority
/dev/mapper/vg00-lv_swap          partition      16777212   0      -1

or via free commnad:

# free -m
             total       used       free     shared    buffers     cached
Mem:         64305       2046      62258          0        157       1153
-/+ buffers/cache:        735      63569
Swap:        16383          0      16383





No comments:

Post a Comment