LVM: Extend an existing Logical Volume / Filesystem

LVM: Extend an existing Logical Volume / Filesystem

# Tested on RHEL 5, 6 & 7


# Concatenated volume
# ------------------------------------------------------------------------------------------

MNTPT=/testmntpt
SIZE=500                  # Size to add in Mbytes

VG=`mount | awk '{if ( $3 == "'$MNTPT'" ) print$1}' | cut -f4 -d'/' | cut -f1 -d '-'`
LV=`mount | awk '{if ( $3 == "'$MNTPT'" ) print$1}' | cut -f4 -d'/' | cut -f2 -d '-'`


df -h $MNTPT
   Filesystem            Size  Used Avail Use% Mounted on
   /dev/mapper/testvg-testlv
                         485M   11M  449M   3% /testmntpt
lvs | grep $LV
   testlv   testvg -wi-ao--- 500.00m



# First, verify if there is enough free space in the VG

vgdisplay $VG | grep Free
   Free  PE / Size       391 / 1.53 GiB


# Then, extend LV. Here size is given in Mbytes. To specify size in Logical Extents use '-l'

lvextend -L +$SIZE  /dev/$VG/$LV

# ** Size to be added to volume may be specified as a percentage of remaining free space of
# the volume group like this (in this case we take all available free space):
#
# lvextend -l +100%FREE -n /dev/$VG/$LV



lvs | grep $LV
   testlv   testvg -wi-ao--- 1012.00m


# Finally, extend filesystem

# For ext2/ext3 on RHEL 5 and ext2/ext3/ext4 on RHEL 6 & RHEL 7:

resize2fs -p /dev/$VG/$LV
   resize2fs 1.41.12 (17-May-2010)
   Filesystem at /dev/testvg/testlv is mounted on /testmntpt; on-line resizing required
   old desc_blocks = 2, new_desc_blocks = 4
   Performing an on-line resize of /dev/testvg/testlv to 1036288 (1k) blocks.
   The filesystem on /dev/testvg/testlv is now 1036288 blocks long.

# *** For ext4 F.S. on RHEL 5:
# resize4fs -p /dev/$VG/$LV

# *** For xfs F.S.:
# xfs_growfs -d /dev/$VG/$LV


df -h $MNTPT
   Filesystem            Size  Used Avail Use% Mounted on
   /dev/mapper/testvg-testlv
                         981M   11M  920M   2% /testmntpt



# Striped volume
# ------------------------------------------------------------------------------------------

MNTPT=/testmntpt
SIZE=256                  # Size to add in Mbytes

VG=`mount | awk '{if ( $3 == "'$MNTPT'" ) print$1}' | cut -f4 -d'/' | cut -f1 -d '-'`
LV=`mount | awk '{if ( $3 == "'$MNTPT'" ) print$1}' | cut -f4 -d'/' | cut -f2 -d '-'`



df -h $MNTPT
   Filesystem            Size  Used Avail Use% Mounted on
   /dev/mapper/testvg-testlv
                         504M   17M  462M   4% /testmntpt
lvs | grep $LV
   testlv   testvg -wi-ao--- 512.00m


# First, verify free space on volume group and disks

vgdisplay -v $VG
   [...]
   Free  PE / Size       388 / 1.52 GiB

   [...]
   --- Physical volumes ---
   PV Name               /dev/sde1                                   <--- Used
   PV UUID               DFfnXR-fjK3-V5Tz-oFHM-Py1T-rW4G-2DUz4f
   PV Status             allocatable
   Total PE / Free PE    129 / 65

   PV Name               /dev/sde2                                   <--- Used
   PV UUID               nQ3O7h-N8wH-FN9v-jmXu-5j5c-KZ25-IMUmGt
   PV Status             allocatable
   Total PE / Free PE    129 / 65

   PV Name               /dev/sde3
   PV UUID               CT1trl-90zb-afrY-bCAM-0Xas-3Shs-hLCw2m
   PV Status             allocatable
   Total PE / Free PE    129 / 129

   PV Name               /dev/sde4
   PV UUID               anN3BB-pafi-0ZE9-lpOw-Q0iL-EPiF-EQTUT0
   PV Status             allocatable
   Total PE / Free PE    129 / 129


# Then, extend LV. Here size is given in Mbytes. To specify size in Logical Extents use '-l'

# We'll try to keep the distribution regarding striping over disks. In this case I'm adding
# 256M and there is enough space on disks in use so it is not necessary to specify disks as
# LVM will be able to allocate extents directly on them:

lvextend -L +$SIZE  /dev/$VG/$LV
   Extending logical volume testlv to 768.00 MiB
   Logical volume testlv successfully resized

# Let's check what we did:

vgdisplay -v $VG | egrep "VG Name|Free|PV Name"
   VG Name               testvg
   Free  PE / Size       324 / 1.27 GiB
   VG Name                testvg
   PV Name               /dev/sde1     <--- Used
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde2     <--- Used
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde3
   Total PE / Free PE    129 / 129
   PV Name               /dev/sde4
   Total PE / Free PE    129 / 129

# Otherwise, if we want to add extents on specific disks, we may do it (Let's add 256
# additional MiB):

SIZE=256
DISKS="/dev/sde3 /dev/sde4"

lvextend -L +$SIZE  /dev/$VG/$LV $DISKS
   Extending logical volume testlv to 1.00 GiB
   Logical volume testlv successfully resized

vgdisplay -v $VG | egrep "VG Name|Free|PV Name"
   VG Name               testvg
   Free  PE / Size       260 / 1.02 GiB
   VG Name                testvg
   PV Name               /dev/sde1
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde2
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde3     <--- Used
   Total PE / Free PE    129 / 97
   PV Name               /dev/sde4     <--- Used
   Total PE / Free PE    129 / 97

# If we don't indicate disks, system will try to allocate extents on disks that were used
# during the last 'extend' operation:

SIZE=256

lvextend -L +$SIZE  /dev/$VG/$LV
   Extending logical volume testlv to 1.25 GiB
   Logical volume testlv successfully resized

vgdisplay -v $VG | egrep "VG Name|Free|PV Name"
   VG Name               testvg
   Free  PE / Size       196 / 784.00 MiB
   VG Name                testvg
   PV Name               /dev/sde1
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde2
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde3     <--- Used
   Total PE / Free PE    129 / 65
   PV Name               /dev/sde4     <--- Used
   Total PE / Free PE    129 / 65


# Finally, if there is no enough free space on disks that were used last, system will first
# fill them up and then will allocate extents on the other disks

SIZE=725

lvextend -L +$SIZE  /dev/$VG/$LV
   Extending logical volume testlv to 1.96 GiB
   Logical volume testlv successfully resized

vgdisplay -v $VG | egrep "VG Name|Free|PV Name"
   VG Name               testvg
   Free  PE / Size       14 / 56.00 MiB
   VG Name                testvg
   PV Name               /dev/sde1     <--- Used step 2
   Total PE / Free PE    129 / 7
   PV Name               /dev/sde2     <--- Used step 2
   Total PE / Free PE    129 / 7
   PV Name               /dev/sde3     <--- Used step 1
   Total PE / Free PE    129 / 0
   PV Name               /dev/sde4     <--- Used step 1
   Total PE / Free PE    129 / 0



# Let's see extent distribution:

lvdisplay -m $VG/$LV
   [...]
   --- Segments ---
   Logical extent 0 to 191:
     Type                striped
     Stripes             2
     Stripe size         64.00 KiB
     Stripe 0:
       Physical volume   /dev/sde1     <--- 1st extension
       Physical extents  0 to 95
     Stripe 1:
       Physical volume   /dev/sde2     <--- 1st extension
       Physical extents  0 to 95

   Logical extent 192 to 449:
     Type                striped
     Stripes             2
     Stripe size         64.00 KiB
     Stripe 0:
       Physical volume   /dev/sde3     <--- 2nd and 3rd extensions
       Physical extents  0 to 128
     Stripe 1:
       Physical volume   /dev/sde4     <--- 2nd and 3rd extensions
       Physical extents  0 to 128

   Logical extent 450 to 501:
     Type                striped
     Stripes             2
     Stripe size         64.00 KiB
     Stripe 0:
       Physical volume   /dev/sde1     <--- 4th extension
       Physical extents  96 to 121
     Stripe 1:
       Physical volume   /dev/sde2     <--- 4th extension
       Physical extents  96 to 121


# Anyway, try to maintain a "logical" distribution when extending a striped volume


lvs | grep $LV
   testlv   testvg -wi-a---- 1.96g


# Finally, extend filesystem

# For ext2/ext3 on RHEL 5 and ext2/ext3/ext4 on RHEL 6 & RHEL 7:

resize2fs -p /dev/$VG/$LV
   resize2fs 1.42.9 (28-Dec-2013)
   Filesystem at /dev/testvg/testlv is mounted on /testmntpt; on-line resizing required
   old_desc_blocks = 1, new_desc_blocks = 1
   The filesystem on /dev/testvg/testlv is now 514048 blocks long.

# *** For ext4 F.S. on RHEL 5:
# resize4fs -p /dev/$VG/$LV

# *** For xfs F.S.:
# xfs_growfs -d /dev/$VG/$LV

df -h $MNTPT
   Filesystem                 Size  Used Avail Use% Mounted on
   /dev/mapper/testvg-testlv  2.0G  788K  1.9G   1% /testmntpt



# Mirrored volume
# ------------------------------------------------------------------------------------------

MNTPT=/testmntpt
SIZE=128                  # Size to add in Mbytes

VG=`mount | awk '{if ( $3 == "'$MNTPT'" ) print$1}' | cut -f4 -d'/' | cut -f1 -d '-'`
LV=`mount | awk '{if ( $3 == "'$MNTPT'" ) print$1}' | cut -f4 -d'/' | cut -f2 -d '-'`



df -h $MNTPT
   Filesystem            Size  Used Avail Use% Mounted on
   /dev/mapper/testvg-testlv
                         248M   11M  226M   5% /testmntpt
lvs | grep $LV
   testlv   testvg mwi-aom-- 256.00m                         testlv_mlog   100.00


# First, verify free space on volume group and disks

vgdisplay -v $VG
   [...]
   Free  PE / Size       387 / 1.51 GiB

   [...]
   --- Physical volumes ---
   PV Name               /dev/sde1                                   <--- Used
   PV UUID               DFfnXR-fjK3-V5Tz-oFHM-Py1T-rW4G-2DUz4f
   PV Status             allocatable
   Total PE / Free PE    129 / 65

   PV Name               /dev/sde2                                   <--- Used
   PV UUID               nQ3O7h-N8wH-FN9v-jmXu-5j5c-KZ25-IMUmGt
   PV Status             allocatable
   Total PE / Free PE    129 / 64

   PV Name               /dev/sde3
   PV UUID               CT1trl-90zb-afrY-bCAM-0Xas-3Shs-hLCw2m
   PV Status             allocatable
   Total PE / Free PE    129 / 129

   PV Name               /dev/sde4
   PV UUID               anN3BB-pafi-0ZE9-lpOw-Q0iL-EPiF-EQTUT0
   PV Status             allocatable
   Total PE / Free PE    129 / 129


# Then, extend LV. Here size is given in Mbytes. To specify size in Logical Extents use '-l'

# We'll try to keep the distribution regarding mirroring over disks. In this case I'm adding
# 128M and there is enough space on disks in use so it is not necessary to specify disks as
# LVM will be able to allocate extents directly on them:

lvextend -L +$SIZE  /dev/$VG/$LV
   Extending 2 mirror images.
   Extending logical volume testlv to 384.00 MiB
   Logical volume testlv successfully resized

# Let's check what we did:

vgdisplay -v $VG | egrep "VG Name|Free|PV Name"
   VG Name               testvg
   Free  PE / Size       323 / 1.26 GiB
   VG Name                testvg
   PV Name               /dev/sde1     <--- Used
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde2     <--- Used
   Total PE / Free PE    129 / 32
   PV Name               /dev/sde3
   Total PE / Free PE    129 / 129
   PV Name               /dev/sde4
   Total PE / Free PE    129 / 129

# Otherwise, if we want to add extents on specific disks, we may do it (Let's add 256
# additional MiB):

SIZE=256
DISKS="/dev/sde3 /dev/sde4"

lvextend -L +$SIZE  /dev/$VG/$LV $DISKS
   Extending 2 mirror images.
   Extending logical volume testlv to 640.00 MiB
   Logical volume testlv successfully resized

vgdisplay -v $VG | egrep "VG Name|Free|PV Name"
   VG Name               testvg
   Free  PE / Size       195 / 780.00 MiB
   VG Name                testvg
   PV Name               /dev/sde1
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde2
   Total PE / Free PE    129 / 32
   PV Name               /dev/sde3     <--- Used
   Total PE / Free PE    129 / 65
   PV Name               /dev/sde4     <--- Used
   Total PE / Free PE    129 / 65


# If we don't indicate disks, system will try to allocate extents on disks that were used
# during the last 'extend' operation:


SIZE=250

lvextend -L +$SIZE  /dev/$VG/$LV
   Extending 2 mirror images.
   Extending logical volume testlv to 892.00 MiB
   Logical volume testlv successfully resized

vgdisplay -v $VG | egrep "VG Name|Free|PV Name"
   VG Name               testvg
   Free  PE / Size       69 / 276.00 MiB
   VG Name                testvg
   PV Name               /dev/sde1
   Total PE / Free PE    129 / 33
   PV Name               /dev/sde2
   Total PE / Free PE    129 / 32
   PV Name               /dev/sde3     <--- Used
   Total PE / Free PE    129 / 2
   PV Name               /dev/sde4     <--- Used
   Total PE / Free PE    129 / 2


# Finally, if there is no enough free space on disks that were used last, system will first
# fill them up and then will allocate extents on the other disks

SIZE=128

lvextend -L +$SIZE  /dev/$VG/$LV
   Extending 2 mirror images.
   Extending logical volume testlv to 1020.00 MiB
   Logical volume testlv successfully resized

vgdisplay -v $VG | egrep "VG Name|Free|PV Name"
  VG Name               testvg
  Free  PE / Size       5 / 20.00 MiB
  VG Name                testvg
  PV Name               /dev/sde1     <--- Used step 2
  Total PE / Free PE    129 / 3
  PV Name               /dev/sde2     <--- Used step 3
  Total PE / Free PE    129 / 2
  PV Name               /dev/sde3     <--- Used step 1
  Total PE / Free PE    129 / 0
  PV Name               /dev/sde4     <--- Used step 1
  Total PE / Free PE    129 / 0



# Anyway, try to maintain a "logical" distribution when extending a mirrored volume


lvs | grep $LV
   testlv   testvg mwi-aom-- 1020.00m                         testlv_mlog   100.00


# Finally, extend filesystem

# For ext2/ext3 on RHEL 5 and ext2/ext3/ext4 on RHEL 6 & RHEL 7:

resize2fs -p /dev/$VG/$LV
   resize2fs 1.41.12 (17-May-2010)
   Filesystem at /dev/testvg/testlv is mounted on /testmntpt; on-line resizing required
   old desc_blocks = 1, new_desc_blocks = 4
   Performing an on-line resize of /dev/testvg/testlv to 1044480 (1k) blocks.
   The filesystem on /dev/testvg/testlv is now 1044480 blocks long.

# *** For ext4 F.S. on RHEL 5:
# resize4fs -p /dev/$VG/$LV

# *** For xfs F.S.:
# xfs_growfs -d /dev/$VG/$LV


df -h $MNTPT
   Filesystem            Size  Used Avail Use% Mounted on
   /dev/mapper/testvg-testlv
                         988M   11M  926M   2% /testmntpt
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Using etckeeper with git
Viewed 6755 times since Sun, Jun 3, 2018
RHEL: Display swap/RAM size
Viewed 3187 times since Sat, Jun 2, 2018
Exclude multiple files and directories with rsync
Viewed 2400 times since Wed, Oct 31, 2018
Easily Monitor CPU Utilization in Linux Terminal With Stress Terminal UI
Viewed 3950 times since Thu, Apr 18, 2019
BIND for the Small LAN
Viewed 3310 times since Sun, May 20, 2018
Setup SSL Tunnel Using Stunnel on Ubuntu
Viewed 2422 times since Fri, Sep 28, 2018
red hat 7 tmpfiles service
Viewed 1741 times since Thu, Oct 11, 2018
Using grep to find string in files
Viewed 2075 times since Fri, May 15, 2020
Need to set up yum repository for locally-mounted DVD on Red Hat Enterprise Linux 7
Viewed 3000 times since Mon, Oct 29, 2018
linux manual tools
Viewed 2709 times since Fri, Sep 28, 2018