• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to extend and reduce Swap Space on LVM2 Logical Volume

by admin

By default, all Linux(RHEL, CentOS, Fedora, and Ubuntu) like operating system uses all available space during installation. If this is the case with your system, then a swap partition will be created on LVM and if you want to resize the swap space then you must first add a new physical volume to the volume group used by the swap space.

After adding additional storage to the swap space’s volume group, it is now possible to extend it. To do so, perform the following steps (assuming /dev/VolGroup/lv_swap is the volume you want to extend by 2 GB):

The output of Free Command before extend:

free before extend

Steps to extend swap space on an LVM2 logical volume

Step 1: Disable swapping for the associated logical volume:

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

Step 2: Resize the LVM2 logical volume by 2 GB.

# lvresize /dev/VolGroup/lv_swap -L +2G 
Extending logical volume lv_swap to 3.97 GiB 
Logical volume lv_swap successfully resized

Step 3: Format the new swap space.

# mkswap /dev/VolGroup/lv_swap 
mkswap: /dev/VolGroup/lv_swap: warning: don't erase bootbits sectors 
on whole disk. Use -f to force. 
Setting up swapspace version 1, size = 4161532 KiB 
no label, UUID=14df63cb-5e3b-42c3-911d-2016fb771804

Step 4: Enable the extended logical volume.

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

To test if the logical volume was successfully extended, use cat /proc/swaps or free to inspect the swap space.

free after extend

Steps to reduce swap on LVM2 logical volume

To reduce an LVM2 swap logical volume (assuming /dev/VolGroup/lv_swap is the volume you want to reduce by 512 MB):

The output of Free Command before reduction:

free before reduce

Step 1: Disable swapping for the associated logical volume:

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

Step 2: Reduce the LVM2 logical volume by 512 MB:

# lvreduce /dev/VolGroup/lv_swap -L -512M 
WARNING: Reducing active logical volume to 3.47 GiB 
THIS MAY DESTROY YOUR DATA (filesystem etc.) 
Do you really want to reduce lv_swap? [y/n]: y 
Reducing logical volume lv_swap to 3.47 GiB 
Logical volume lv_swap successfully resized

Step 3: Format the new swap space.

# mkswap /dev/VolGroup/lv_swap 
mkswap: /dev/VolGroup/lv_swap: warning: don't erase bootbits sectors 
on whole disk. Use -f to force. 
Setting up swapspace version 1, size = 3637244 KiB 
no label, UUID=7f8f11de-5bc3-4b9c-b558-471fc540fa9b

Step 4: Enable the resized logical volume.

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

To test if the swap’s logical volume size was successfully reduced, use cat /proc/swaps or free command to inspect the swap space.

free after reduce

Note: We can also create a temporary swapfile on a filesystem with dd & mkswap prior to doing swapoff, for cases where ram+swap usage exceeds ram size. +Later on swapoff from that temp swapfile & removing it of course.

Filed Under: Linux

Some more articles you might also be interested in …

  1. RHEL 7 – RHCSA Notes (Cheat Sheets)
  2. zcat Command Examples in Linux
  3. grub2-mkconfig: command not found
  4. CentOS / RHEL 7 : How to remove rescue image using grubby
  5. How to Setup SSH keys for “passwordless” ssh login in Linux
  6. setpci command – configure PCI device
  7. mktemp: command not found
  8. How to obtain virtual/physical CPU information in Oracle VM (XEN)
  9. sox: command not found
  10. compsize Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • aws ec2: CLI for AWS EC2 (Command Examples)
  • aws cur – Create, query, and delete AWS usage report definitions (Command Examples)
  • aws configure – Manage configuration for the AWS CLI (Command Examples)
  • aws cognito-idp: Manage Amazon Cognito user pool and its users and groups using the CLI

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright