Logical volumes may be resized dynamically while preserving the data on the volume.
This tutorial is a continuation of - How to create an LVM in Linux. If you haven't already, please read it before continuing.
Before we start this exercise of resizing a logical volume, let us look at our LVM setup.
Hard disk 1 - /dev/sda - 2 GiB
Volume group - vg_sda
Logical volume - lv_sda - size 2 GiB
The address of the logical volume is -
Logical volume is mounted at -
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 -
- Unmount the logical volume (if it is mounted)
- Reduce the file system residing on the logical volume. And finally,
- 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
e2fsckto 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
# 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
Now lets reduce the logical volume /dev/vg_sda/lv_sda from 2 GiB to 1 GiB.
# lvreduce --size 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.
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.
This involves -
- Enlarge the logical volume. Then,
- Resize the filesystem to the new size of your logical volume.
Step 1: Enlarge the logical volume
lvextendis 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 --size +1G /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.
Sometimes, using the
--sizeoption might give you an error. If that happens, you can use
--extentsoption instead as shown below.
# lvextend --extents +100%FREE /dev/vg_sda/lv_sda Extending logical volume lv_sda to 2.00 GiB Logical volume lv_sda successfully resized.
Step 2: Resize the filesystem
First check the filesystem using
# e2fsck -f /dev/vg_sda/lv_sda
Now resize the filesystem using the
resize2fscommand 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.