How to Extend LVM Partition Using Unallocated Space in CentOS 7 – Step-by-Step Guide for System Administrators


2 views

When examining the storage configuration on this CentOS 7 server, we observe an inefficient disk allocation:

NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0  4.5G  0 part
  ├─centos-root 253:0    0    4G  0 lvm  /
  └─centos-swap 253:1    0  512M  0 lvm  [SWAP]

The key issue here is that while the physical disk has 20GB capacity, only 4.5GB is allocated to the LVM partition (sda2), leaving approximately 15.5GB as unallocated space.

First, let's confirm the available free space on the physical disk:

# fdisk -l /dev/sda
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b5b9d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    10485759     4729856   8e  Linux LVM

We'll create a new partition in the unallocated space and set it as LVM type:

# fdisk /dev/sda
Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 
First sector (10485760-41943039, default 10485760): 
Last sector, +sectors or +size{K,M,G} (10485760-41943039, default 41943039): 
Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Command (m for help): w

Now let's incorporate the new partition into our LVM setup:

# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  VG Size               15.50 GiB
  PE Size               4.00 MiB
  Total PE              3967
  Alloc PE / Size       1152 / 4.50 GiB
  Free  PE / Size       2815 / 11.00 GiB

Finally, we'll expand the root filesystem:

# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from 4.00 GiB (1024 extents) to 15.50 GiB (3967 extents).
  Logical volume root successfully resized.

# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=262144 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=1048576, 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 1048576 to 4052608

Confirm the new space is available:

# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   16G  1.1G   15G   7% /

For ext4 filesystems, the resize command differs:

# resize2fs /dev/centos/root
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/centos/root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/centos/root is now 4063232 blocks long.

Remember to always backup critical data before performing storage operations, especially on production systems.


From your lsblk output, I can see you have a 20GB disk (/dev/sda) with:

sda1 - 500MB /boot partition
sda2 - 4.5GB LVM physical volume containing:
  centos-root - 4GB root filesystem
  centos-swap - 512MB swap space

The remaining ~15GB is unallocated space on the disk. We'll need to:

  1. Create a new partition in the unallocated space
  2. Add it to the LVM physical volume
  3. Extend the logical volume
  4. Resize the filesystem
  5. First, let's create a new partition using fdisk:

    fdisk /dev/sda

    Then follow these commands in fdisk:

    n  # new partition
    p  # primary partition
    3  # partition number
    # Accept default first sector
    # Accept default last sector
    t  # change partition type
    3  # select partition 3
    8e # set to Linux LVM
    w  # write changes

    After creating the partition, run:

    partprobe /dev/sda

    Now create a physical volume from the new partition:

    pvcreate /dev/sda3

    Add the new physical volume to your existing volume group (centos):

    vgextend centos /dev/sda3

    Extend the root filesystem (assuming you want to add all available space):

    lvextend -l +100%FREE /dev/centos/root

    Finally, resize the filesystem (for ext4):

    resize2fs /dev/centos/root

    Or for xfs:

    xfs_growfs /

    Check the new size with:

    df -h

    And verify the LVM configuration:

    vgs
    lvs

    If you prefer to extend sda2 instead of creating sda3, you would need to:

    1. Delete sda2 (after backing up data)
    2. Create a new sda2 covering all space
    3. Recreate the LVM setup

    This method is more complex and requires downtime.

    • Always backup important data before modifying partitions
    • This process can be done on a running system for LVM extensions
    • For physical partition changes, consider booting from a live CD
    • XFS filesystems can only be grown, not shrunk