How to extend and resize LVM partitions in Linux

The Logical Volume Manager (LVM) allows the Linux kernel to efficiently manage large disk drives. This allows users to create partitions from multiple disks and allows them to expand the file system size online in seconds.

In this article, we will see how to expand and resize LVM partitions without losing data.

If you use LVM on Linux, follow the steps below to expand the LVM partition online without losing any data.

Note-These commands must be run with root privileges.

table of Contents

  • plan 1:
  • 1) Extend LVM with an existing disk
  • Scenario 2:
  • Expand storage with new disks
  • How to adjust / shrink LVM
  • 1) Unmount the file system
  • 2) Perform a file system check
  • 3) Adjust the file system size
  • 4) Reduce LV size
  • in conclusion

plan 1:

1) Extend LVM with an existing disk

In this case, we have an unused partition on the existing storage and we want to use it to extend another LVM partition.

One. Check volume group details

First, we will use the vgdisplay command to view the details of the volume group (hereafter called VG).

# vgdisplay

Display volume group details

We can see that there is a usable size of 49.21 GB.

To see more details of our VG, we can use add -v Parameters of the vgdisplay command.

# vgdisplay -v

VG more details

b. Extended storage

of /dev/vg_devmachine/data Has 24.41 GB. Let’s say we want to add 10 GB of storage /data Divided. To expand a logical volume, you just need to tell the lvextend command how much you want to increase the size (+10 below) -L parameter.

# lvextend -L+10G /dev/vg_devmachine/data

Extended LVM

Instructions for RHEL 3 only: Before activating the changes, we need to unmount the partition first.

# unmount /dev/VolGroup00/LogVol00

For this example, we can uninstall /data Partition by typing:

# unmount /dev/vg_devmachine/data

Then we need to activate the changes. We will use the resize2fs command. This command is used to resize the ext2 / ext3 / ext4 file system online.

# resize2fs /dev/vg_devmachine/data

note: On RHEL 4, you can use the ext2online command to resize partitions.

# ext2online /dev/mapper/vg_devmachine-data

But on RHEL 5 above, this command is replaced by the resize2fs command.

Activate changes

Check it now and run vgdisplay -v Again and highlight /dev/vg_devmachine/data.

Expanded new VG display

Now we see that the LV size has increased to 34.41 GB.

If you are using RHEL 3, don’t forget to mount the partition again.

Scenario 2:

Expand storage with new disks

In this case, we added new storage /dev/sdb Linux system with 100 GB capacity. We will then use it to extend an existing LVM partition. The command to extend the LVM partition to another disk remains the same. However, as we add more physical storage to the system, we need to make the OS (Linux) more aware of it.

One. Create a physical partition

When adding a new disk, we need the system to recognize it for some operations. There are some partitioning tools on the Internet, such as fdisk and parted. In this article, we will use fdisk because fdisk is available in most Linux distributions. The first thing to do is to create a new partition

# fdisk /dev/sdb

Create new partition

Press n Create a new partition.

Create new partition

Suppose we want to create 2 new partitions. Press p with 1 Create a new primary partition. Put in a size of 50G.

Repeat for the second partition. Press p with 2 Create a second primary partition. For the size (in the last cylinder …) you don’t need to enter a value, it will consider the rest of the space, as shown in the following example

Specify the size

When done, press Submit Changes w Button.

Commit changes

Now if we do fdisk -l, we will see /dev/sdb1 with /dev/sdb2 The partition appears.

New partition appears

b. Creating a physical volume (PV) as the basis for LVM

After the physical partition is created, the next step is to create the physical volume (PV). PV is used as the host of LVM. Physical disk is divided into one or more physical volumes (PV)

# pvcreate /dev/sdb1 /dev/sdb2

Create PV

If no errors are found, you can use the pvscan command to check the results.

# pvscan

Scan for available PV

Now we see two empty PVs appear. A blank indicates that the PV does not belong to any volume group.

C. Extended Volume Group (VG)

A logical volume group (VG) is created by combining one or more PVs. We already have 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 the vgextend command.

# vgextend vg_devmachine /dev/sdb1

Extended volume group

Check with vgdisplay. You will see that the size of vg_devmachine is increasing.

d. Extended LVM partition

Now, at this stage, we will use the same command in the previous command. Suppose we want to expand /home Divided. Add more 30 GB.

Again, we first check it using the lvdisplay command to see the current capacity.

# lvdisplay

Display / home LV

Add more 30G to it /home Partition, we need to extend the partition:

# lvextend -L+30G /dev/vg_devmachine/home

Extended / home partition

We then need to activate the changes for them to take effect:

# resize2fs /dev/vg_devmachine/home

Activate changes

How to adjust / shrink LVM

Before shrinking the volume, make sure that you have backed up the mount point. Shrinking a volume can damage data. To reduce the size of a logical volume, first unmount the file system. You can then use the lvreduce command to reduce the volume. After shrinking the volume, remount the file system. The examples given in this article apply to centos 7.

In our example, the logical volume is named /dev/vg-01/lv_stripe Available in 1Gb size. We want to reduce the LV size to 800MB. Before continuing to shrink the file system, note the following points.

  • Ensure that the current disk usage of the file system is less than the size of the logical volume to be reduced.
  • Before making any size changes in LVM, always back up your file system data, as simple command errors can cause file system corruption and therefore data loss.

In order to reduce the LV size to 800MB, we need to perform the following steps.

  • Unmount the file system (if its root volume needs to be booted into the Live CD to get the job done)
  • Before resizing, use the fsck command to check the file system.
  • Adjust the file system size to 800MB before reducing the LV size
  • Reduce the size of the logical volume by 800MB.
  • Mount the file system

Now we can complete each of the above steps.

1) Unmount the file system

In the example below we have a logical volume /dev/vg-01/lv_stripe Mount on the mounting point./mnt/lv_stripe We can check if the volume is mounted 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 a file system, you should unmount it. You can unmount the file system /mnt/lv_stripe as follows.

# umount /mnt/lv_stripe

Check with 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) Perform a file system check

Before continuing to reduce the file system, you should execute the fsck command to avoid inconsistent file system data. We force a check even if the file system looks clean -f Options. 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 must be done in every 5 steps of the file system check, otherwise the file system may be fine.

Note: fsck.ext4 is now recommended for new versions of Ubuntu because it uses the ext4 file system by default.

3) Adjust the file system size

Before reducing the size of a logical volume, we need to reduce the file system in it. The command resize2fs can be used for this purpose, as shown below. We reduced the file system to 800MB. We will use -p Option to print a percentage completion bar for each resize2fs operation when resizing offline.

# 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) Reduce LV size

After shrinking the resident file system, we can reduce the 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 reduces the logical volume size to 800MB. Note that when resizing logical volumes, 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 LV Size 800.00 Mib Indicates a new size row. You can also mount the file system and check with the following 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:

Note that before using the lvreduce command to reduce the logical volume, we first use the resize2fs command to resize the file system. Resize2fs causes the file system to use only the first size byte stored. It does this by moving file system metadata and data. Upon completion, there will be unused storage at the end of the block device (logical volume) and the file system will be unused. The Lvextend and lvreduce commands change the size of the logical volume. They can also change the size of the file system if given the following commands. -r This may be the right approach for you to call the option of resize2fs, especially when reducing. Accidentally assigning the wrong size to lvreduce is a convenient way to lose data. -r Prevent this from happening (by making sure to tell resize2fs the same size).

So you can use lvreduce directly with -r Options. For example, we will follow the steps above (without using the resize2fs step) and start with lvreduce -r The command is as follows:

# 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 the logical volume

# lvdisplay /dev/vg-01/lv_stripe | grep "Size"
 LV Size 504.00 MiB

You can see a new size. We don’t need to resize the file system.

in conclusion

With LVM partitioning, it gives you flexibility about storage. When the current partition space is insufficient, you can adjust the partition size on the fly. You will notice that we do not need to unmount the partition to expand the lvm, and only need to unload if you need to shrink the lvm. One disadvantage is that if something goes wrong with the physical disk, it will affect the volume group and all the LVM partitions created.

Also read:

  • How to configure LVM (pvcreate, lvcreate, vgcreate) in Linux
Sidebar