Monday, April 23, 2018

Extend / Reduce SWAP Partitions in Linux

Extend Swap Partition.

1) Determine the name of the swap logical volume using the "lvs" command:

root@ubuntu:~# lvs
  LV       VG   Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert
  motadata vg0  -wi-ao--- 71.52g
  root     vg0  -wi-ao--- 14.30g
  swap     vg0  -wi-ao---  7.63g
2) Turn off the swap partition:
root@ubuntu:~# swapoff /dev/mapper/vg0-swap
3) Resize the swap partition, adding 15GB more space to it:

root@ubuntu:~# lvresize -L +5G /dev/mapper/vg0-swap
  Extending logical volume swap to 12.63 GiB
  Logical volume swap successfully resized
4) Format the new swap space to make it usable:
root@ubuntu:~# mkswap /dev/mapper/vg0-swap
mkswap: /dev/mapper/vg0-swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 13242364 KiB
no label, UUID=a1ea414e-f650-4d11-8525-43ff01ed48d8
5) Turn the swap volume back on:

root@ubuntu:~# swapon /dev/mapper/vg0-swap

6) Use the free command to show your new swap space:
root@ubuntu:~# free -mh | grep -i swap
Swap: 12G 0B 12G

Reduce swap partition

1) Determine the name of the swap logical volume using the "lvs" command:

root@ubuntu:~# lvs
  LV            VG   Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert
  motadata  vg0  -wi-ao--- 71.52g
  root          vg0  -wi-ao--- 14.30g
  swap        vg0  -wi-ao--- 12.63g

2) Turn off the swap partition:
root@ubuntu:~# swapoff /dev/mapper/vg0-swap
3) Resize the swap partition, adding 15GB more space to it:

root@ubuntu:~# lvresize -L -5G /dev/mapper/vg0-swap
  WARNING: Reducing active logical volume to 7.63 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce swap? [y/n]: y
  Reducing logical volume swap to 7.63 GiB
  Logical volume swap successfully resized
4) Format the new swap space to make it usable:
root@ubuntu~#mkswap /dev/mapper/vg0-swap
mkswap: /dev/mapper/vg0-swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 7999484 KiB
no label, UUID=dbce9994-a5c1-4dcc-bacd-1a68ab437c4a
5) Turn the swap volume back on:

root@ubuntu:~# swapon /dev/mapper/vg0-swap

6) Use the free command to show your new swap space:
root@ubuntu:~# free -mh | grep -i swap
Swap: 7.6G 0B 7.6G

No comments:

Post a Comment