How To Shrink LVM Partition or File System

Linux Redhat 1 Comment

Situation where you need to shrink lvm partition or file system are rare due to the plenty of storage space we have nowadays, but first let’s clarify one thing, the correct term is this : some time we need to shrink, reduce or resize an LV (logical volume) file system, which can be an ext3, ext4 or other.
So here is the actual scenario: We have two PV (Physical volume) grouped into one VG (volume group), on this volume group, an LV was created to host a small mysql database.

The database size represent, for example 1% of the total available space, and the database administrator estimated that it will not grow for no more than 25% per year, which in our case, is a total waste of resource.

Here is the actual configuration of the LVM, volume group and the physical volume:

Physical volume

[root@host581 ~]# pvs
  PV         VG       Fmt  Attr PSize   PFree
  /dev/sda2  VolGroup lvm2 a--    7.51g    0
  /dev/sdc   vgdata   lvm2 a--  100.00m    0
  /dev/sdd   vgdata   lvm2 a--  100.00m    0

Volume group

[root@host581 ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  VolGroup   1   2   0 wz--n-   7.51g    0
  vgdata     2   1   0 wz--n- 200.00m    0

Used space:

[root@host581 ~]# df -h /data
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vgdata-lv_mysql
                      194M   26M  159M  14% /data

As we can see the logical volume is oversized for this database, so let’s shrink lvm partition /dev/mapper/vgdata-lv_mysql, or file system if you prefer:

1- First unmount the file system

[root@host581 ~]# umount /data
[root@host581 ~]#

2- Check the file system for any errors or inconsistency:

[root@host581 ~]# e2fsck -f /dev/mapper/vgdata-lv_mysql
e2fsck 1.41.12 (17-May-2010)
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/mapper/vgdata-lv_mysql: 12/51200 files (0.0% non-contiguous), 32110/204800 blocks


3- Resize the file system

[root@host581 ~]# resize2fs -p /dev/mapper/vgdata-lv_mysql 100m
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vgdata-lv_mysql to 102400 (1k) blocks.
Begin pass 3 (max = 25)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/vgdata-lv_mysql is now 102400 blocks long.


4- Reduce the lvm partition size

[root@host581 ~]# lvreduce -L 100M  /dev/mapper/vgdata-lv_mysql
  WARNING: Reducing active logical volume to 100.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_mysql? [y/n]: y
  Reducing logical volume lv_mysql to 100.00 MiB
  Logical volume lv_mysql successfully resized

5- Mount the file system

[root@host581 ~]# mount /dev/mapper/vgdata-lv_mysql /data
[root@host581 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      6.5G  721M  5.5G  12% /
tmpfs                 246M     0  246M   0% /dev/shm
/dev/sda1             485M   31M  429M   7% /boot
/dev/mapper/vgdata-lv_mysql
                       97M   26M   67M  28% /data

We need then to move any physical extent to the disk that we want to keep, this suppose that we have a volume group with the default allocation policy, as shown here on Attr column (letter n), which mean normal:

[root@host581 ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  VolGroup   1   2   0 wz--n-   7.51g      0
  vgdata     2   1   0 wz--n- 200.00m 100.00m

The attributes also control the behaviour of our logical volume partition, which can be seen here with the “i” in the attributes which mean inherit from our VG:

[root@host581 ~]# lvs
  LV       VG       Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lv_root  VolGroup -wi-ao   6.54g
  lv_swap  VolGroup -wi-ao 992.00m
  lv_mysql vgdata   -wi-ao 100.00m

These allocation policy control how data is distributed between your physical volume.

6- Move any physical extents to the disk that we want to keep.

[root@host581 ~]# pvmove -v /dev/sdd /dev/sdc

7- Reduce the volume group to only contain one physical disk

[root@host581 ~]# vgreduce -v vgdata /dev/sdd
    Finding volume group "vgdata"
    Using physical volume(s) on command line
    Archiving volume group "vgdata" metadata (seqno 3).
    Removing "/dev/sdd" from volume group "vgdata"
    Creating volume group backup "/etc/lvm/backup/vgdata" (seqno 4).
  Removed "/dev/sdd" from volume group "vgdata"

8- Retire the disk from the machine and remove any reference to it, you can have small variation on the disk name if you want to shrink lvm partition based on LUN not local disk.

[root@host581 ]# pvremove /dev/sdd
  Labels on physical volume "/dev/sdd" successfully wiped
[root@host581 ]# rm -r /dev/sdd
[root@host581 ]# ls -l /dev/disk//by-path/pci-0000\:00\:0*
lrwxrwxrwx. 1 root root  9 Feb  3 05:16 /dev/disk//by-path/pci-0000:00:01.1-scsi-0:0:0:0 -> ../../sr0
lrwxrwxrwx. 1 root root  9 Feb  3 05:16 /dev/disk//by-path/pci-0000:00:0d.0-scsi-0:0:0:0 -> ../../sda
lrwxrwxrwx. 1 root root 10 Feb  3 05:16 /dev/disk//by-path/pci-0000:00:0d.0-scsi-0:0:0:0-part1 -> ../../sda1
lrwxrwxrwx. 1 root root 10 Feb  3 05:16 /dev/disk//by-path/pci-0000:00:0d.0-scsi-0:0:0:0-part2 -> ../../sda2
lrwxrwxrwx. 1 root root  9 Feb  3 05:16 /dev/disk//by-path/pci-0000:00:0d.0-scsi-1:0:0:0 -> ../../sdb
lrwxrwxrwx. 1 root root  9 Feb  3 09:44 /dev/disk//by-path/pci-0000:00:0d.0-scsi-2:0:0:0 -> ../../sdc
lrwxrwxrwx. 1 root root  9 Feb  3 10:16 /dev/disk//by-path/pci-0000:00:0d.0-scsi-3:0:0:0 -> ../../sdd
[root@host581 ]# rm -r /dev/disk//by-path/pci-0000:00:0d.0-scsi-3:0:0:0

9- Remove the device from the SCSI subsystem
[root@host581 ]# echo 1 > /sys/block/sdd/device/delete

If we examine the output from dmesg, we can see that ths disk was removed:

....
sd 5:0:0:0: [sdd] Synchronizing SCSI cache
sd 5:0:0:0: [sdd] Stopping disk
....

Our lvm partition is reduced to only one disk, and the file system reflect this as shown here

shrink lvm partition result

As a best practice, if you need to shrink lvm partition or file system, be sure to have a backup of your data before, not matter how confident you are with your skills.

Find this useful! Be Sociable, Share your Knowledge!

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading...

One thought on “How To Shrink LVM Partition or File System

  • Seb

    Very helpful, thanks for sharing.
    I was looking for a way to shrink my lvm but the majority of tutorials doesn’t involve retiring a physical volume.

Leave a Reply

Your email address will not be published. Required fields are marked *