Friday, January 24, 2014

LVM - Resize a Logical Volume Partition in Linux

This is a short tutorial that explains how to resize (reduce/enlarge) a logical volume in Linux.


Logical volumes may be resized dynamically while preserving the data on the volume.

Before we start this exercise of resizing a logical volume, let us look at our LVM setup.

Hard disk 1 - /dev/sda - 2 GB
Volume group - vg_sda
Logical volume - lv_sda - size 2 GB

The address of the logical volume is - /dev/vg_sda/lv_sda

Logical volume is mounted at - /mnt/volume-a.

Reducing a logical volume

Task : Reduce the logical volume /dev/vg_sda/lv_sda from 2 GiB to 1 GiB size.

Reducing a logical volume involves following 3 steps namely -

  1. Unmount the logical volume (if it is mounted)
  2. Reduce the file system residing on the logical volume. And finally,
  3. Reduce the logical volume.

Step 1 - Unmount the logical volume


If the logical volume is not mounted anywhere, you can skip this step. 

Since our logical volume has been mounted at /mnt/volume-a, we have to unmount it.

# umount /mnt/volume-a

Step 2 - Reduce the file system residing on the logical volume


First run e2fsck to check the file system as follows -

# e2fsck -f /dev/vg_sda/lv_sda

Next resize the filesystem on the logical volume to 1 GiB size using the resize2fscommand.

# resize2fs /dev/vg_sda/lv_sda 1G
Resizing the filesystem on /dev/vg_sda/lv_sda to 262144 (4k) blocks.
The filesystem on /dev/vg_sda/lv_sda is now 262144 blocks long.

Step 3 - Reduce the logical volume


To reduce the size of a logical volume, we use the lvreduce LVM command.

Now lets reduce the logical volume /dev/vg_sda/lv_sda from 2 GiB to 1 GiB.

# lvreduce -L 1G /dev/vg_sda/lv_sda
    WARNING: Reducing active logical volume to 1.00 GiB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_sda? [y/n]: y
    Reducing logical volume lv_sda to 1.00 GiB
    Logical volume lv_sda successfully resized.
                                                            OR
# lvreduce -L -500M  /dev/vg_sda/lv_sda
    WARNING: Reducing active logical volume to 1.00 GiB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_sda? [y/n]: y
    Reducing logical volume lv_sda to 1.00 GiB
    Logical volume lv_sda successfully resized.               Note:This will decrease the logical volume 500 MB from the 2GB new lv is 1.5GB


You should be careful when reducing a logical volume's size, because data in the reduced part is lost.

Expanding a logical volume


Steps for expanding/growing a logical volume are exact opposite of those for shrinking /reducing the logical volume.(unmount the partion  is not required while expanding the size of logical volume)

This involves -

  1. Enlarge the logical volume. Then,
  2. Resize the filesystem to the new size of your logical volume.

Step 1: Enlarge the logical volume


lvextend is the LVM command you use to enlarge/expand a logical volume.

So to expand the logical volume to 2 GiB size, we run the following command -

# lvextend -L +1G /dev/vg_sda/lv_sda
    Extending logical volume lv_sda to 2.00 GiB
    Logical volume lv_sda successfully resized.
                                                                    
                                                              OR

# lvextend -L 2G /dev/vg_sda/lv_sda
    Extending logical volume lv_sda to 2.00 GiB
    Logical volume lv_sda successfully resized.

The '+' in '+1G' indicate the value is added to the actual size of the logical volume. Without the + sign, the value is taken as an absolute one.

Step 2: Resize the filesystem

First check the filesystem using e2fsck command -

# e2fsck -f /dev/vg_sda/lv_sda

Now resize the filesystem using the resize2fs command as follows -

# resize2fs /dev/vg_sda/lv_sda 2G
Resizing the filesystem on /dev/vg_sda/lv_sda to 523264 (4k) blocks.
The filesystem on /dev/vg_sda/lv_sda is now 523264 blocks long.

List of commands


To recap, following are the commands we have used in this tutorial.

lvreduce - Reduce the size of a logical volume.

lvextend - Extend the size of a logical volume.

e2fsck - Check the filesystem prior to making changes to it.

resize2fs - A tool to resize ext2 / ext3 / ext4 file systems.

No comments:

Post a Comment