How to Extend and Reduce LVM (Logical Volume Management)

January 5, 2021
()

How to Extend and Reduce LVM (Logical Volume Management)


In the previous blog, we saw How to Configure LVM, click here to read more. In this blog we are going to demonstrate How to Extend and Reduce LVM with pvs command you can see here, we still have 20Gb free space in disk /dev/sdb1, We have created two mount points /u01 with filesystem ext4 and /u02 with filesystem xfs.

[root@test-machine02 /]# pvs
  PV         VG  Fmt  Attr PSize   PFree
  /dev/sda2  ol  lvm2 a--  <49.00g   4.00m
  /dev/sdb1  vg1 lvm2 a--  <50.00g <20.00g
[root@test-machine02 /]#
[root@test-machine02 /]# df -Th /u01 /u02
Filesystem                Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lvu01_vg1 ext4   15G   41M   14G   1% /u01
/dev/mapper/vg1-lvu02_vg1 xfs    15G   33M   15G   1% /u02
[root@test-machine02 /]#


As we have mentioned earlier that we will add more storage to LVM without any downtime to illustrate that we will add 10 Gb of space to each mount point i.e. u01 & u02 using LVEXTEND command.


We will add 10Gb of space in the /u01 mount point. After completing the below steps, you will observe that /u01 mount point space increased from 15GB to 25 GB and all operations we did online, without unmounting /u01.

[root@test-machine02 vg1]# lvextend -L +10G /dev/mapper/vg1-lvu01_vg1
  Size of logical volume vg1/lvu01_vg1 changed from 15.00 GiB (3840 extents) to 25.00 GiB (6400 extents).
  Logical volume vg1/lvu01_vg1 successfully resized.
[root@test-machine02 vg1]#
[root@test-machine02 vg1]# resize2fs  /dev/mapper/vg1-lvu01_vg1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vg1-lvu01_vg1 is mounted on /u01; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 4
The filesystem on /dev/mapper/vg1-lvu01_vg1 is now 6553600 blocks long.

[root@test-machine02 vg1]#
[root@test-machine02 vg1]# df -Th /u01
Filesystem                Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lvu01_vg1 ext4   25G   44M   24G   1% /u01
[root@test-machine02 vg1]#


Now we will add 9Gb of space to /u02 mount point. Please note filesystem /u02 is xfs so resize2fs command will not work on xfs filesystem, resize2fs work only for ext3 and ext4 filesystem. Instead of resize2fs, we will use the command xfs_growfs. If you attempt to execute the command using resize2fs on xfs filesystem will receive the below errors.

[root@test-machine02 vg1]# lvextend -L +9G /dev/mapper/vg1-lvu02_vg1
  Size of logical volume vg1/lvu02_vg1 changed from 15.00 GiB (3840 extents) to 24.00 GiB (6144 extents).
  Logical volume vg1/lvu02_vg1 successfully resized.
[root@test-machine02 vg1]#

[root@test-machine02 vg1]# resize2fs /dev/mapper/vg1-lvu02_vg1
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/vg1-lvu02_vg1
Couldn't find valid filesystem superblock.

[root@test-machine02 vg1]# xfs_growfs /u02
meta-data=/dev/mapper/vg1-lvu02_vg1 isize=256    agcount=4, agsize=983040 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0 spinodes=0 rmapbt=0
         =                       reflink=0
data     =                       bsize=4096   blocks=3932160, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 3932160 to 6291456
[root@test-machine02 vg1]#

[root@test-machine02 vg1]# df -Th /u02
Filesystem                Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lvu02_vg1 xfs    24G   33M   24G   1% /u02


on the grounds that we’ve got completed how to Extend the LVM partition, So now we can perform, How we will Reduce the LVM partition. To reduce the partition first, we need to unmount the partition. Simply recall without un-mounting we cannot use e2fsck command. If you try to use e2fsck on a mounted partition you will you will acquire the underneath errors.


In the below demonstration, we can try to reduce /u01 having ext4 filesystem from 25Gb to 15Gb.

[root@test-machine02 ~]# df -Th /u01
Filesystem                Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lvu01_vg1 ext4   25G   44M   24G   1% /u01

[root@test-machine02 ~]# e2fsck -f /dev/mapper/vg1-lvu01_vg1
e2fsck 1.42.9 (28-Dec-2013)
/dev/mapper/vg1-lvu01_vg1 is mounted.
e2fsck: Cannot continue, aborting.

[root@test-machine02 ~]# umount /u01

[root@test-machine02 ~]#  e2fsck -f /dev/mapper/vg1-lvu01_vg1
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/mapper/vg1-lvu01_vg1: 11/1638400 files (0.0% non-contiguous), 146871/6553600 blocks

[root@test-machine02 ~]# resize2fs  /dev/mapper/vg1-lvu01_vg1 15g
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg1-lvu01_vg1 to 3932160 (4k) blocks.
The filesystem on /dev/mapper/vg1-lvu01_vg1 is now 3932160 blocks long.

[root@test-machine02 ~]# lvreduce -L 15g /dev/mapper/vg1-lvu01_vg1
  WARNING: Reducing active logical volume to 15.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lvu01_vg1? [y/n]: y
  Size of logical volume vg1/lvu01_vg1 changed from 25.00 GiB (6400 extents) to 15.00 GiB (3840 extents).
  Logical volume vg1/lvu01_vg1 successfully resized.

[root@test-machine02 ~]# mount /dev/mapper/vg1-lvu01_vg1 /u01

[root@test-machine02 ~]# df -Th /u01
Filesystem                Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lvu01_vg1 ext4   15G   41M   14G   1% /u01
[root@test-machine02 ~]#


Please note xfs filesystem doesn’t support e2fsck & resize2fs command. It is currently not possible to shrink or reduce an xfs filesystem. If you try to execute the command using e2fsck & resize2fs on xfs file system you will get hold of the under below errors.

[root@test-machine02 ~]# df -Th /u02
Filesystem                Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lvu02_vg1 xfs    24G   33M   24G   1% /u02

[root@test-machine02 ~]# umount /u02

[root@test-machine02 ~]#  e2fsck -f /dev/mapper/vg1-lvu02_vg1
e2fsck 1.42.9 (28-Dec-2013)
ext2fs_open2: Bad magic number in super-block
e2fsck: Superblock invalid, trying backup blocks...
e2fsck: Bad magic number in super-block while trying to open /dev/mapper/vg1-lvu02_vg1

The superblock could not be read or does not describe a correct ext2
filesystem.  If the device is valid and it really contains an ext2
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193 <device>


[root@test-machine02 ~]# resize2fs /dev/mapper/vg1-lvu02_vg1 15g
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/vg1-lvu02_vg1
Couldn't find valid filesystem superblock.



Click here for Step by Step Install Oracle Enterprise Linux (OEL) 7
Click here for How to Upgrade Oracle Enterprise Linux (OEL) 7.7 to 7.9
Click here for How to Configure LVM (Logical Volume Management) in Oracle Enterprise Linux (OEL)
Click here for Step by Step manually creation of an Oracle 19c Database on Linux 7

 

This document is only for learning purpose and always validate in the LAB environment first before applying in the LIVE environment.


Hope so you like this article!
Please share your valuable feedback/comments/subscribe and follow us below and don’t forget to click on the bell icon to get the most recent update.
Click here to understand more about our pursuit.

 

Loading

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Hello and welcome to DBsGuru,I’m Jamsher Khan working as Senior Oracle DBA based in KSA-Jeddah, I have working experience in Oracle DBA, SQL Server, MySql, PostgreSQL, Linux, Golden Gate, ODA.Thanks for the visits!Share Learn Grow!

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *