How to Extend a Linux LVM Partition with Unallocated Space: A Step-by-Step Guide


2 views

First, let's examine the current disk configuration. From parted /dev/sda print free, we can see there's 188GB of unallocated space at the end of the disk (between 111GB and 299GB). The existing LVM setup shows four volume groups (os, logs, mysql, and narcine) consuming about 93GB of space.

# Current PVs and VGs:
pvs
  PV         VG      Fmt  Attr PSize  PFree
  /dev/sda4  os      lvm2 a--  62.00g 4.00m
  /dev/sda5  logs    lvm2 a--  10.00g 4.00m
  /dev/sda6  mysql   lvm2 a--  20.00g 4.00m
  /dev/sda7  narcine lvm2 a--  11.00g 4.00m

We'll use parted to create a new partition in the unallocated space:

parted /dev/sda
(parted) mkpart primary 111GB 299GB
(parted) set  lvm on
(parted) quit

After creating the partition, you'll need to inform the kernel about the partition table change:

partprobe /dev/sda

Now we'll prepare the new partition for LVM:

pvcreate /dev/sda8  # Assuming it's the 8th partition

Let's extend the 'os' volume group with our new physical volume:

vgextend os /dev/sda8

Now we can extend the logical volume (in this case, os-root):

lvextend -l +100%FREE /dev/mapper/os-root

Finally, resize the ext4 filesystem to use the new space:

resize2fs /dev/mapper/os-root

After completing these steps, verify the changes:

df -h /dev/mapper/os-root
pvdisplay
lvdisplay

If you prefer to extend existing partitions rather than creating new ones, you could:

  1. Delete the last partition (/dev/sda7)
  2. Recreate it with a larger size
  3. Extend the physical volume with pvresize
# Example for /dev/sda7:
pvresize /dev/sda7 --setphysicalvolumesize 200G
  • Always backup important data before resizing partitions
  • Some filesystems (like XFS) require different resizing commands
  • For production systems, consider doing this during maintenance windows
  • If the partition is mounted, some operations might require unmounting first

If you encounter problems:

# Check for active LVM processes:
lvm lvmpolld --dump

# Debug partition table issues:
parted /dev/sda print
fdisk -l /dev/sda

# Verify LVM metadata:
vgcfgrestore --list os

When working with LVM on GPT-partitioned disks, it's crucial to first understand the current storage configuration. From your parted output, we can see:

Number  Start   End     Size    File system  Name     Flags
        111GB   299GB   188GB   Free Space

This shows significant unallocated space at the end of your disk that can be utilized for extending your logical volumes.

Here's the proper sequence to extend an LVM logical volume when you have free space on the physical disk:

  1. Create a new partition in the free space
  2. Add it as a physical volume to LVM
  3. Extend the volume group
  4. Extend the logical volume
  5. Resize the filesystem

First, create a new partition in the free space using parted:

parted /dev/sda
(parted) mkpart primary 111GB 299GB
(parted) set  lvm on
(parted) quit

Then update the kernel's partition table:

partprobe /dev/sda

Initialize the new partition as a physical volume:

pvcreate /dev/sdaX  # Replace X with your new partition number

Extend the volume group (VG) that needs more space. For example, to extend the 'os' VG:

vgextend os /dev/sdaX

Now extend the logical volume (LV) that needs more space. First check free space in the VG:

vgdisplay os

Then extend the LV (e.g., root):

lvextend -L +50G /dev/mapper/os-root

Finally, resize the filesystem. For ext4:

resize2fs /dev/mapper/os-root

For XFS (like your narcine volume):

xfs_growfs /dev/mapper/narcine-nartmp

The loop partition table you see in parted output for device mapper devices is normal - it indicates these are virtual block devices managed by LVM, not physical partitions.

  • Always ensure you have backups before resizing partitions
  • XFS can only be grown, not shrunk
  • Some filesystems require unmounting before resizing
  • Check alignment when creating new partitions on SSDs

Here's a bash script snippet that automates checking free space and extending a volume:

#!/bin/bash
VG="os"
LV="root"
PARTITION="/dev/sda8"

# Check free space in VG
FREE_PE=$(vgs --noheadings -o vg_free $VG | awk '{print $1}')

if [ $FREE_PE -gt 0 ]; then
    # Extend LV using all free space
    lvextend -l +100%FREE /dev/$VG/$LV
    resize2fs /dev/$VG/$LV
    echo "Successfully extended $VG/$LV"
else
    echo "No free space available in $VG"
fi