Shrinking a Logical Volume With LVM

In this article, and in this article, we showed how to install and extend logical volumes with LVM. Now, we want to shrink the filesystem we created and the logical volume, so we can remove the drive. [Note: Before you do anything involving your partitions or volumes, make a complete backup. Make sure to set up a test system with the same kernel and distribution to see how this stuff works. This article was written using our lab box. Read our terms of use. Note that this article was written using a Fedora RC 1 distribution and a generic 2.4.24 kernel.] As we do all of this, we have a text file on our logical volume called ruk.txt that we can use to somewhat test the integrity of our filesystem and volume:

[root@srv-1 root]# mount
/dev/sda1 on / type ext3 (rw)
none on /proc type proc (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sdb1 on /opt type ext3 (rw)
/dev/sdc1 on /usr/src type ext3 (rw)
/dev/sdd1 on /usr/local type ext3 (rw)
/dev/volgroup/logicalvol on /mnt type ext3 (rw)
[root@srv-1 root]# cat /mnt/ruk.txt
test
[root@srv-1 root]# umount /mnt

OK. We have data on our volume. Now, we want to shrink down the filesystem on our volume so we can remove one of the drives:

 
[root@srv-1 root]# lvmdiskscan
lvmdiskscan -- reading all disks / partitions (this may take a while...)
lvmdiskscan -- /dev/sda1  [       3.75 GB] Primary  LINUX native partition [0x83]
lvmdiskscan -- /dev/sda2  [     250.98 MB] Primary  LINUX swap partition [0x82]
lvmdiskscan -- /dev/sdb1  [          4 GB] Primary  LINUX native partition [0x83]
lvmdiskscan -- /dev/sdc1  [          4 GB] Primary  LINUX native partition [0x83]
lvmdiskscan -- /dev/sdd1  [          4 GB] Primary  LINUX native partition [0x83]
lvmdiskscan -- /dev/sde   [          4 GB] USED LVM whole disk
lvmdiskscan -- /dev/sdf   [          4 GB] USED LVM whole disk
lvmdiskscan -- /dev/sdg   [          4 GB] USED LVM whole disk
lvmdiskscan -- /dev/sdh   [          4 GB] free whole disk
lvmdiskscan -- 8 disks
lvmdiskscan -- 4 whole disks
lvmdiskscan -- 0 loop devices
lvmdiskscan -- 0 multiple devices
lvmdiskscan -- 0 network block devices
lvmdiskscan -- 5 partitions
lvmdiskscan -- 0 LVM physical volume partitions
[root@srv-1 root]# 
ot@srv-1 root]# e2fsck -f /dev/volgroup/logicalvol
e2fsck 1.34 (25-Jul-2003)
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/volgroup/logicalvol: 12/1441792 files (0.0% non-contiguous), 53460/2883584 blocks
[root@srv-1 root]#
ot@srv-1 root]# resize2fs /dev/volgroup/logicalvol 40000
resize2fs 1.34 (25-Jul-2003)
Resizing the filesystem on /dev/volgroup/logicalvol to 40000 (4k) blocks.
The filesystem on /dev/volgroup/logicalvol is now 40000 blocks long.
[root@srv-1 root]# 

Check our test file:

[root@srv-1 root]# mount /dev/volgroup/logicalvol /mnt
[root@srv-1 root]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1              3874108   1611852   2065460  44% /
none                   3874108   1611852   2065460  44% /dev/pts
/dev/sdb1              4127076    235028   3682404   6% /opt
/dev/sdc1              4127076    278956   3638476   8% /usr/src
/dev/sdd1              4127076     32828   3884604   1% /usr/local
/dev/volgroup/logicalvol
155872     32832    116640  22% /mnt
[root@srv-1 root]#
[root@srv-1 root]# ls /mnt
lost+found  ruk.txt
[root@srv-1 root]# cat /mnt/ruk.txt
test
[root@srv-1 root]# 
[root@srv-1 root]# umount /mnt
umount: /mnt: not mounted

Our test file is still complete after resizing the filesystem. Note that the logical volume is still the same size. Let’s shrink it by 8 Gigs:

[root@srv-1 root]# lvreduce -L -8G /dev/volgroup/logicalvol
lvreduce -- WARNING: reducing active logical volume to 3 GB
lvreduce -- THIS MAY DESTROY YOUR DATA (filesystem etc.)
lvreduce -- do you really want to reduce "/dev/volgroup/logicalvol"? [y/n]: y
lvreduce -- doing automatic backup of volume group "volgroup"
lvreduce -- logical volume "/dev/volgroup/logicalvol" successfully reduced
[root@srv-1 root]# mount /dev/volgroup/logicalvol /mnt
[root@srv-1 root]# ls /mnt
lost+found  ruk.txt
[root@srv-1 root]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1              3874108   1611996   2065316  44% /
none                   3874108   1611996   2065316  44% /dev/pts
/dev/sdb1              4127076    235028   3682404   6% /opt
/dev/sdc1              4127076    278956   3638476   8% /usr/src
/dev/sdd1              4127076     32828   3884604   1% /usr/local
/dev/volgroup/logicalvol
155872     32832    116640  22% /mnt
[root@srv-1 root]# 
ot@srv-1 root]# cat /mnt/ruk.txt
test
[root@srv-1 root]# 

Our file is still there, and the logical volume is shrunk down. Let’s remove /dev/sdg from the volume group:

[root@srv-1 root]# vgreduce volgroup /dev/sdg
vgreduce -- doing automatic backup of volume group "volgroup"
vgreduce -- volume group "volgroup" successfully reduced by physical volume:
vgreduce -- /dev/sdg
[root@srv-1 root]#

Let’s do a scan:

[root@srv-1 root]# pvscan
pvscan -- reading all physical volumes (this may take a while...)
pvscan -- ACTIVE   PV "/dev/sde" of VG "volgroup" [3.99 GB / 1016 MB free]
pvscan -- ACTIVE   PV "/dev/sdf" of VG "volgroup" [3.99 GB / 3.99 GB free]
pvscan -- inactive PV "/dev/sdg" is in no VG  [4 GB]
pvscan -- total: 3 [12 GB] / in use: 2 [8 GB] / in no VG: 1 [4 GB]
[root@srv-1 root]#

We are now ready to replace or reconfigure our logical volume.

There are six articles in this series:
Setting Up Logical Volume Manager
Extending a Logical Volume
Shrinking a Logical Volume With LVM
Adding a RAID1 Device to a Volume With LVM
Upgrading LVM To Version 2 and Patching The Linux Kernel
Finish Conversion And Expansion to Two RAID1 Devices With LVM