LVM: Reduce an existing Logical Volume / Filesystem

LVM: Reduce an existing Logical Volume / Filesystem

# Tested on RHEL 5, 6 & 7

# *** It is not possible to reduce an xfs filesystem so this recipe is valid only for
#     ext2/ext3/ext4 filesystems.

# *** As of February 2016, new implementation of LVM didn't permit reducing a RAID volume:
#     "Unable to reduce RAID LV - operation not implemented."

# Just in case, I recommended to backup up all important data before reducing
a F.S.

MNTPT=/testmntpt
SIZE=128M               # Final size, in Mbytes. Without "M" to specify size in Bytes.

VG=`mount | awk '{if ( $3 == "'$MNTPT'" ) print$1}' | cut -f4 -d'/' | cut -f1 -d '-'`
LV=`mount | awk '{if ( $3 == "'$MNTPT'" ) print$1}' | cut -f4 -d'/' | cut -f2 -d '-'`



df -h $MNTPT
   Filesystem            Size  Used Avail Use% Mounted on
   /dev/mapper/testvg-testlv
                         788M   17M  731M   3% /testmntpt

lvs | grep $LV
   testlv   testvg mwi-aom-- 800.00m                     testlv_mlog   100.00


umount $MNTPT

# First, we check the file system. 'e2fsck' is used to check the ext2/ext3/ext4 family of
# file systems. '-f' option force checking even if the file system seems clean. This step
# is mandatory.

e2fsck -f /dev/$VG/$LV
   e2fsck 1.39 (29-May-2006)
   Pass 1: Checking inodes, blocks, and sizes
   Pass 2: Checking directory structure
   Pass 3: Checking directory connectivity
   Pass 4: Checking reference counts
   Pass 5: Checking group summary information
   /dev/testvg/testlv: 11/102592 files (9.1% non-contiguous), 7532/204800 blocks

# Then, we reduce the size of the filesystem

resize2fs -p /dev/$VG/$LV $SIZE
   resize2fs 1.39 (29-May-2006)
   Resizing the filesystem on /dev/testvg/testlv to 32768 (4k) blocks.
   Begin pass 3 (max = 7)
   Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   The filesystem on /dev/testvg/testlv is now 32768 blocks long.


# And finally, we reduce the size of the logical volume

lvreduce -L $SIZE /dev/$VG/$LV
     WARNING: Reducing active logical volume to 128.00 MB
     THIS MAY DESTROY YOUR DATA (filesystem etc.)
   Do you really want to reduce testlv? [y/n]: y
     Reducing logical volume testlv to 128.00 MB
     Logical volume testlv successfully resized




lvs | grep $LV
  testlv   testvg mwi-ao 128.00M
                     testlv_mlog   100.00


mount  /dev/$VG/$LV  $MNTPT


df -h $MNTPT
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/testvg-testlv
                      127M   17M  104M  14% /testmntpt
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
RHEL: Getting/Setting hardware clock’s time
Viewed 2925 times since Sat, Jun 2, 2018
How to use yum command on CentOS/RHEL
Viewed 10870 times since Wed, Oct 17, 2018
How to manage Linux password expiry with the chage command
Viewed 11115 times since Tue, Sep 11, 2018
tcpdump
Viewed 8994 times since Fri, Jul 27, 2018
Improve security with polyinstantiation
Viewed 13115 times since Fri, May 15, 2020
Linux Kernel /etc/sysctl.conf Security Hardening
Viewed 23440 times since Fri, Aug 3, 2018
Linux - Cannot login from remote console but can access via ssh
Viewed 4982 times since Fri, Jun 8, 2018
SPRAWDZONA KONFIGURACJA RSYSLOG I LOGROTATE, JAKO ZEWNĘTRZNEGO SERWERA SYSLOG
Viewed 3696 times since Fri, Nov 30, 2018
Cron YUM How to use yum-cron to automatically update RHEL/CentOS Linux
Viewed 2368 times since Fri, Oct 26, 2018
LVM basic
Viewed 2131 times since Sat, Jun 2, 2018