Friday, June 1, 2012

HowTo: Shrink size of ext4 LVM logical volume

LVM, the Logical Volume Manager, is extremely flexible and provides numerous advantages over standard partitions. One of those advantages consists in resizing logical volumes.

In this post I'll go over the required steps to reduce the size of an LVM logical volume formatted as an ext4 filesystem. This is achieved can be achieved in a few steps:
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 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    The 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 GiB
    Logical 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.

6 comments:

Anonymous said...

Very helpful blog

Anonymous said...

Great Howto, worked like a charm, no steps left out. Thank you :)

Anonymous said...

Outstanding how-to. Just followed it and everything went exactly as described. Thanks!

Anonymous said...

nice. works for me (red hat 7.0)

tangram said...

Yes it should work on RH 7.0 as well as in any other distro that uses LVM2.

Thanks for the feedback. Appreciated.

Anonymous said...

Thanks