Shrinking LVM logical volume with ext3/ext4 filesystem

… the required steps to reduce the size of an LVM logical volume formatted as an ext4 filesystem…

1) Unmount the logical volume (or boot into a live CD if the logical volume contains the root filesystem)
2) Check the filesystem for errors
3) Shrink the filesystem to the desired size
4) Reduce the size of the underlying logical volume
5) Check if the resulting logical volume and filesystem are ok
6) Re-mount the logical volume

To illustrate the procedure assume a volume group name vg_d620 which contains the lv_example logical group. The objective will be to shrink the lv_example logical group that is formatted with ext4 to 30G.

1) Unmount the logical volume

Change to the superuser and unmount the logical volume filesystem that is to be resized:

  1. $ su
  2. # umount /dev/mapper/vg_d620-lv_example

2) Check the filesystem for errors

e2fsck checks a Linux ext2/ext3/ext4 filesystem for errors, in this case the -f switch is used to force the check even if the filesystem appears to be clean:

  1. # e2fsck -f /dev/mapper/vg_d620-lv_example
  2. 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/vg_d620-lv_example: 11448/3678208 files (1.5% non-contiguous), 4768046/14704640 blocks

3) Shrink the filesystem to the desired size

resize2fs is used to shrink our unmounted filesystem located on vol_d620-lv_example. The -p switch is prints out percentage completion bars for the resize operation. Here the ext4 filesystem is reduced to the desired filesystem final size, in this case I want it to be of 30 gigabytes:

  1. # resize2fs -p /dev/mapper/vg_d620-lv_example 30G
  2. resize2fs 1.41.12 (17-May-2010)
    Resizing the filesystem on /dev/mapper/vg_d620-lv_example to 7864320 (4k) blocks.
    Begin pass 2 (max = 16894)
    Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    Begin pass 3 (max = 449)
    Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    Begin pass 4 (max = 1866)
    Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXThe filesystem on /dev/mapper/vg_d620-lv_example is now 7864320 blocks long.

4) Reduce the size of the underlying logical volume

Having shrunk the ext4 filesystem it is time to reduce the logical volume size accordingly. To achieve this the lvreduce tool is employed. The -L switch specifies final size of the logical volume which should match the size of the ext4 filesystem.

  1. # lvreduce -L 30G /dev/mapper/vg_d620-lv_example
  2. WARNING: Reducing active logical volume to 30.00 GiB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce lv_example? [y/n]: y
    Reducing logical volume lv_example to 30.00 GiBLogical volume lv_example successfully resized

5) Check if the resulting logical volume and filesystem are ok

Everything should have proceeded as planned however let’s verify things. e2fsck and resize2fs are used verify the new filesystem, respectively. Notice that this time the resize2fs doesn’t specify any size, the goal here is to have the filesystem match the size of the logical volume.

  1. # e2fsck -f /dev/mapper/vg_d620-lv_example
  2. 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/vg_d620-lv_example: 11448/1966080 files (1.5% non-contiguous), 4658570/7864320 blocks
  3. # resize2fs -p /dev/mapper/vg_d620-lv_example
  4. resize2fs 1.41.12 (17-May-2010)The filesystem is already 7864320 blocks long. Nothing to do!

6) Re-mount the logical volume

Finally, mount the updated logical volume:

  1. # mount /dev/mapper/vg_d620-lv_example /mnt/example

It should be noted that if in step 4 e2fsck fails because the partition is tool small lvextend can be used to extend the logical volume until e2fsck completes with success.

Further information on lvm, lvreduce, lvextend,e2fsck and resize2fs can be obtained in the associated man pages.

——————–

Shrinking a Logical Volume With LVM

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

Source : http://www.softpanorama.org/Commercial_linuxes/LVM/shrinking_a_logical_volume.shtml

This entry was posted in Linux and tagged , . Bookmark the permalink.

Leave a Reply

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