LVM: Reduce SWAP size by shrinking existing Logical Volume

LVM: Reduce SWAP size by shrinking existing Logical Volume

# Tested on RHEL 5, 6 & 7

# Check which is the swap volume and its size

root@<server>:/#> cat /etc/fstab | grep swap
/dev/rvg/swaplv         swap                    swap    defaults        0 0


root@<server>:/#> lvdisplay /dev/rvg/swaplv
  --- Logical volume ---
  LV Name                /dev/rvg/swaplv
  VG Name                rvg
  LV UUID                m0EThc-4Epd-Ecj3-lvPK-BfU0-D98H-1pgyRY
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                14.69 GB
  Current LE             470
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:5


root@<server>:/#> free -m
             total       used       free     shared    buffers     cached
Mem:         11239       4555       6683          0        422       3697
-/+ buffers/cache:        435      10803
Swap:        15039          0      15039                                     <----


root@<server>:/#> cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/mapper/rvg-swaplv                  partition       15400952        0       -4



# Disable swapping on LV to be resized (in our case, as we have a single LV allocated to
# swap, we could run command with '-a' flag so swapping is disabled on all known swap
# devices and files (as found in /proc/swaps or /etc/fstab)

# root@<server>:/#> swapoff -a

root@<server>:/#> swapoff /dev/rvg/swaplv



# Check that swapping has been disabled

root@<server>:/#> free
             total       used       free     shared    buffers     cached
Mem:      11508864    4657068    6851796          0     432316    3786148
-/+ buffers/cache:     438604   11070260
Swap:            0          0          0                                     <----


# Reduce swap Logical Volume (we will remove 1 GB)

root@<server>:/#> lvreduce -L -1GB /dev/rvg/swaplv
  WARNING: Reducing active logical volume to 13.69 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce swaplv? [y/n]: y
  Reducing logical volume swaplv to 13.69 GB
  Logical volume swaplv successfully resized



# Set up the new swap area

root@<server>:/#> mkswap /dev/rvg/swaplv
Setting up swapspace version 1, size = 14696837 kB



# ------------------------------------------------------------------------------------------
# Enable swapping on resized LV (in our case, as we have a single LV allocated to swap, we
# could run command with '-a' flag so swapping is enabled on all known swap devices and
# files (as found in /proc/swaps or /etc/fstab)

# root@<server>:/#> swapon -a

root@<server>:/#> swapon /dev/rvg/swaplv


# Check final size

root@<server>:/#> free
             total       used       free     shared    buffers     cached
Mem:      11508864    4664880    6843984          0     432320    3786160
-/+ buffers/cache:     446400   11062464
Swap:     14352376          0   14352376                                       <----
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
How to enable Proxy Settings for Yum Command on RHEL / CentOS Servers
Viewed 12258 times since Thu, Jul 19, 2018
ubuntu How to reset lost root password on Ubuntu 16.04 Xenial Xerus Linux
Viewed 8124 times since Tue, Dec 8, 2020
LVM: Move allocated PE between Physical Volumes
Viewed 3760 times since Sat, Jun 2, 2018
sed Delete / Remove ^M Carriage Return (Line Feed / CRLF) on Linux or Unix
Viewed 10038 times since Thu, Feb 7, 2019
Using renice and taskset to manage process priority and CPU affinity with Linux OEL 6.4
Viewed 3456 times since Mon, Feb 17, 2020
Deskshare TLS over Stunnel
Viewed 2658 times since Fri, Sep 28, 2018
Learn Linux System Auditing with Auditd Tool on CentOS/RHEL
Viewed 4107 times since Fri, Apr 5, 2019
CentOS / RHEL : How to move a Volume Group from one system to another
Viewed 3314 times since Mon, Jan 28, 2019
how to list all hard disks in linux from command line
Viewed 3257 times since Mon, Jan 28, 2019
RHEL: Getting/Setting hardware clock’s time
Viewed 2877 times since Sat, Jun 2, 2018