HowTo: Shrink size of ext4 LVM logical volume

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:


# e2fsck -f /dev/mapper/vg_d620-lv_example

        ###Results###
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:


# resize2fs -p /dev/mapper/vg_d620-lv_example 30G

           ###Output###
  1. 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.

 
# lvreduce -L 30G /dev/mapper/vg_d620-lv_example
  1. ### OUtput
  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.

             # e2fsck -f /dev/mapper/vg_d620-lv_example
       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

# resize2fs -p /dev/mapper/vg_d620-lv_example
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: 


# 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.
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

Cheapest Linux VPS Hosting in the world!

Access the best web hosting vps to enhance the efficiencyWeb hosting is an obligatory service if...

Virtual Hosting with Apache Linux Server

How to Setup Multiple Hosts on ApacheIt's the example to configure virtual hostings. Following...

Install MySQL for Database Server on CentOS VPS

 Install MySQL for Database Server. [root@www ~]# yum -y install mysql-server...

Add a HardDrive to Your Linux VPS

 This is an example to create a partition when you add a new hard drive. root@dlp:~#...

HowTo: Extend size of ext4 LVM logical volume

How to Manage and Use LVM (Logical Volume Management) in Ubuntu In...