Logical Volume Manager(LVM) allows the Linux kernel to manage large disk drives effectively. This allows users to create partitions from more than one disk and allows them to extend the filesystem size online within a few seconds.
In this article, we will see, how we can extend and resize an LVM partition without losing data.
If you are using LVM on your Linux, here are the steps to extend your LVM partition online without any data loss.
Note - these commands must be run with root privilege.
1) Extend LVM using existing disk
In this scenario, we have an unused partition on existing storage and we want to use it to extend another LVM partition.
a. Check volume groups details
First, we will use vgdisplay command to see the detail of our Volume Groups (next we call it VG).
We can see that there is 49.21 GB Free Size.
To see the more detail of our VG, we can use add
-v parameter to vgdisplay command.
# vgdisplay -v
b. Extend storage
/dev/vg_devmachine/data has 24.41 GB. Let say we want to add 10 GB more to
/data partition. To extend a logical volume you simply tell the lvextend command how much you want to increase the size (+10 below) with
# lvextend -L+10G /dev/vg_devmachine/data
Note for RHEL 3 Only: We need to unmount the partition before activate the change.
# unmount /dev/VolGroup00/LogVol00
For this example, we can unmount
/data partition by typing:
# unmount /dev/vg_devmachine/data
Then we need to activate the change. We will use resize2fs command. This command is used to resize ext2/ext3/ext4 file system online.
# resize2fs /dev/vg_devmachine/data
Note: On RHEL 4 you can use ext2online command to resize the partition.
# ext2online /dev/mapper/vg_devmachine-data
But on RHEL 5 above, the command is replaced by resize2fs command.
Now to check it, run
vgdisplay -v again, and highlight the
We now see that the LV size grow into 34.41 GB.
If you are using RHEL 3, don't forget to mount your partition again.
Extend storage with a new disk
In this scenario, we have added new storage which is
/dev/sdb with 100 GB capacity into Linux system. Then, we want to use it to extend existing LVM partition. The command to extend LVM partition to another disk remains the same. But when we add more physical storage into the system, then we need to make the OS (Linux) know more about it.
a. Create a physical partition
When we add a new disk, we need it to be recognized by the system to make some operations. There are some partition tools on the internet such as fdisk and parted. In this article, we will use fdisk because it is available in most Linux distribution. The first thing to do is to create a new partition
# fdisk /dev/sdb
n to create new partition.
Let us say, we want to create 2 new partitions. Press
1 to create new primary partition. Put the size as 50G.
Repeat the operation for the second partition. press
2 to create the second primary partition. For the size (at question Last cylinder ...), you don't need to enter a value, it will consider the rest of space as the example below
Once it is finished, commit the change by pressing
Now if we do
fdisk -l, we will see
/dev/sdb2 partition appear.
b. Create Physical Volume (PV) as the base of LVM
After we finish creating physical partition, the next step is to create Physical Volume (PV). PV is used as the host of LVM. A physical disk is divided into one or more physical volumes (PV)
# pvcreate /dev/sdb1 /dev/sdb2
If you find no error, you can check the result using pvscan command.
We now see two empty PV appear. Empty means that the PV’s does not belong to any Volume Group.
c. Extend the Volume Group (VG)
Logical volume groups (VG) are created by combining one or more PV. We have already a VG called vg_devmachine. Its capacity is 95.11 GB and 39.21 GB of free space. To extend vg_devmachine, we can use vgextend command.
# vgextend vg_devmachine /dev/sdb1
Check it via vgdisplay. You will see that vg_devmachine size is increasing.
d. Extend the LVM partition
Now in this stage, we will use the same command at the previous one. Let say we want to extend
/home partition. Adding it more 30 GB.
Again we check it using lvdisplay command first to see the current capacity.
To add more 30G into
/home partition, we need to extend the partition:
# lvextend -L+30G /dev/vg_devmachine/home
Then we need to activate the change so it will take effect:
# resize2fs /dev/vg_devmachine/home
How to Resize/Shrink LVM
Before shrinking the volume make sure you have taken the backup of the mount point. Shrinking volume has a chance of data corruption. To reduce the size of a logical volume, first unmount the file system. You can then use the lvreduce command to shrink the volume. After shrinking the volume, remount the file system. The example given in this post applies to centos 7.
In our example, the logical volume named
/dev/vg-01/lv_stripe has 1Gb size. We want to reduce the LV size to 800MB. Note down the following points before proceeding with the shrinking of filesystem.
- Make sure the current disk usage of the filesystem is less than the size to which you are going to reduce the logical volume.
- Always take a backup of filesystem data before doing any size change in LVM as a simple mistake in command can cause filesystem corruption and hence loss of data.
In order to reduce the LV size to 800MB, we need to follow the below steps.
- Unmount the filesystem (if its root volume which requires booting into a Live CD to complete the work)
- Use fsck command to check the filesystem before resizing it.
- Resize the filesystem to 800MB before reducing the LV size
- Reduce the size of the logical Volume 800MB.
- Mount the filesystem
Now, we can go through each step mentioned above.
1) Unmounting filesystem
In the example below, we have a logical volume
/dev/vg-01/lv_stripe mounted on the mount point.
/mnt/lv_stripe We can check if the volume is mounted with
df -hP command
# df -hP Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 18G 5.2G 13G 30% / devtmpfs 897M 0 897M 0% /dev tmpfs 912M 156K 912M 1% /dev/shm tmpfs 912M 9.0M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 497M 189M 309M 38% /boot tmpfs 183M 24K 183M 1% /run/user/1000 /dev/mapper/vg--01-lv_stripe 1008M 55M 902M 6% /mnt/lv_stripe
You can see the last line, the logical volume is mounted. Before running fsck on the filesystem, it should be unmounted. You can unmount the filesystem
/mnt/lv_stripe as follows.
# umount /mnt/lv_stripe
Check with the df command
# df -hP Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 18G 5.2G 13G 30% / devtmpfs 897M 0 897M 0% /dev tmpfs 912M 156K 912M 1% /dev/shm tmpfs 912M 9.0M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 497M 189M 309M 38% /boot tmpfs 183M 24K 183M 1% /run/user/1000
2) Performing filesystem check
Before proceeding with reducing filesystem, fsck command should be done in order to avoid inconsistency of filesystem data. We will force checking even if the file system seems clean with
-f option. This can be done as follows.
# e2fsck -f /dev/vg-01/lv_stripe e2fsck 1.42.9 (28-Dec-2013) 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/vg-01/lv_stripe: 14/65536 files (7.1% non-contiguous), 18146/262144 blocks
This operation must pass in every 5 steps of file-system check if not there might be some issue with your filesystem.
3) Resizing filesystem
Before reducing the size of the Logical Volume, we need to reduce the filesystem in it. The command resize2fs can be used for this as follows. We will reduce the filesystem to 800MB. We will use
-p option to print out a percentage completion bar for each resize2fs operation during an offline resize.
# resize2fs -p /dev/vg-01/lv_stripe 800M resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg-01/lv_stripe to 204800 (4k) blocks. Begin pass 3 (max = 8) Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The filesystem on /dev/vg-01/lv_stripe is now 204800 blocks long.
4) Reducing the LV size
After shrinking the residing filesystem, we can proceed with reducing LV as follows.
# lvreduce -L 800M /dev/vg-01/lv_stripe WARNING: Reducing active logical volume to 800.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv_stripe? [y/n]: y Size of logical volume vg-01/lv_stripe changed from 1.00 GiB (256 extents) to 800.00 MiB (200 extents). Logical volume lv_stripe successfully resized.
This will reduce the logical volume size to 800MB. Note that when resizing the logical volume, use the exact size (800MB). We can check the new size of the logical volume
# lvdisplay /dev/vg-01/lv_stripe --- Logical volume --- LV Path /dev/vg-01/lv_stripe LV Name lv_stripe VG Name vg-01 LV UUID moX8R0-ME2Q-UFHd-HLIP-rmij-SmTm-3Zupem LV Write Access read/write LV Creation host, time centos7-srv, 2017-05-01 16:02:21 +0100 LV Status available # open 0 LV Size 800.00 MiB Current LE 200 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3
You can see the
LV Size 800.00 Mib line which indicates the new size. You can also mount the filesystem and check it using the command
df -Ph command
# mount /dev/vg-01/lv_stripe /mnt/lv_stripe/
# df -Ph Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 18G 5.2G 13G 30% / devtmpfs 897M 0 897M 0% /dev tmpfs 912M 156K 912M 1% /dev/shm tmpfs 912M 9.0M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 497M 189M 309M 38% /boot tmpfs 183M 20K 183M 1% /run/user/1000 /dev/mapper/vg--01-lv_linear 9.7G 3.2M 9.2G 1% /mnt/lv_linear /dev/mapper/vg--01-lv_mirror 992M 2.8M 938M 1% /mnt/lv_mirror /dev/mapper/vg--01-lv_stripe 786M 55M 692M 8% /mnt/lv_stripe
Note that we resize the filesystem with resize2fs command before reducing the logical volume with lvreduce command. Resize2fs makes the filesystem use only the first size bytes of the storage. It does this by moving both filesystem metadata and your data around. After it completes, there will be unused storage at the end of the block device (logical volume), unused by the filesystem. Lvextend and lvreduce commands change the size of the logical volume. They can additionally change the size of the filesystem if given the
-r option that calls resize2fs for you, which is probably the right way to go, especially with reducing. Accidentally giving the wrong size to lvreduce is an unfortunately easy way to lose data;
-r prevents this (by ensuring that resize2fs is told the same size).
So you can bypass resize2fs step by using directly lvreduce with
-r option. For example, we will reduce our logical volume by following the steps above without resize2fs step and by ending with
lvreduce -r command as below:
# umount /mnt/lv_stripe/
# e2fsck -f /dev/vg-01/lv_stripe e2fsck 1.42.9 (28-Dec-2013) 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/vg-01/lv_stripe: 14/57344 files (7.1% non-contiguous), 17567/204800 blocks
# lvreduce -r -L 500M /dev/vg-01/lv_stripe Rounding size (125 extents) up to stripe boundary size for segment (126 extents) fsck from util-linux 2.23.2 /dev/mapper/vg--01-lv_stripe: clean, 14/57344 files, 17567/204800 blocks resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/mapper/vg--01-lv_stripe to 129024 (4k) blocks. The filesystem on /dev/mapper/vg--01-lv_stripe is now 129024 blocks long. Size of logical volume vg-01/lv_stripe changed from 800.00 MiB (200 extents) to 504.00 MiB (126 extents). Logical volume lv_stripe successfully resized.
Now let's check the new size of our logical volume
# lvdisplay /dev/vg-01/lv_stripe | grep "Size" LV Size 504.00 MiB
You can see a new size. We didn't need to resize the filesystem.
Using LVM partition, it gives you the flexibility about your storage. When your current partition is running out of space, you can easily resize the partition on-the-fly. You notice that we didn't need to unmount the partition to extend lvm and unmount is only required when you need to shrink the lvm. One disadvantage is if the physical disk has a problem, it will affect the volume groups and all the LVM partitions created.