Tutoriel Linux

LVM under Linux: Extending a volume without changing the layer

Débutant6 min de lecture
À retenirLinux n'est pas réservé aux experts. Le bon point de départ : une distribution accessible, une sauvegarde propre et quelques commandes comprises.

You have expanded a virtual disk, but df -h It always displays the same size. With LVM, this is normal if only the physical layer has changed. The disk, partition, physical volume, volume group, logical volume, and file system are different elements.

The order lvextend Therefore, it doesn’t solve every problem on its own. Before using it, you must locate the free space and identify the file system you want to expand. I also advise you to check your backup before proceeding. LVM makes it easy to extend a volume, but it doesn’t protect your data from device errors.

Tux expands a logical volume from multiple disks using LVM
LVM separates the physical disks, the volume group, and the logical volume to be expanded.

Identify the LVM layers before changing the size

A typical LVM storage system follows this chain:

  • a disk or a partition, for example /dev/nvme0n1p3 ;
  • A physical volume (PV) created on this device;
  • A volume group (VG) which brings together one or more PVs;
  • A logical volume (LV) cut from the VG;
  • an ext4, XFS or other file system, mounted in a folder.

Start by displaying the entire chain. Do not replace any names until you have linked the mount point to the correct LV and PV:

lsblk -o NAME,PATH,SIZE,TYPE,FSTYPE,MOUNTPOINTS,MODEL
findmnt -no SOURCE,FSTYPE,TARGET /srv/data
sudo pvs -o pv_name,pv_size,pv_free,vg_name
sudo vgs -o vg_name,vg_size,vg_free
sudo lvs -a -o lv_name,vg_name,lv_size,lv_path,devices
df -hT /srv/data

In the following examples, the group is called vgdata, the logical volume lvdata and the mounting point /srv/dataUse the values ​​reported by your machine. The guide on lsblk under Linux will help you if the directory tree mixes partitions, encryption and logical volumes.

Back up LVM data and metadata

A copy of the LVM metadata does not replace a file backup. First, verify that important data exists on another storage medium and is recoverable. Then, back up the volume group configuration.

sudo vgcfgbackup -f /root/vgdata-$(date +%F-%H%M).conf vgdata
sudo lvs -a -o lv_name,vg_name,lv_size,segtype,devices
sudo pvs -o pv_name,pv_uuid,pv_size,pv_free,vg_name

Also, keep the output of these commands in your intervention report. On a remote server hosting the root directory or critical data, keep a second SSH session open and verify access to the provider’s console. An extension typically doesn’t require a reboot, but an error in the partition or the encryption layer could cut off your access.

Case 1: The volume group already has free space

Look at the column VFree of vgs. If vgdata If you already have at least 10 GB of free space, you can test the LV extension without writing metadata:

sudo vgs -o vg_name,vg_size,vg_free
sudo lvextend --test -L +10G /dev/vgdata/lvdata

The mode --test It avoids writing metadata, but it doesn’t simulate every side effect. Double-check the path and size, then run the actual extension:

sudo lvextend -L +10G /dev/vgdata/lvdata
sudo lvs -o lv_name,vg_name,lv_size,lv_path,devices /dev/vgdata/lvdata

The manual page of lvextend also offers -l +100%FREEThis option consumes all the free space in the VG. I don’t use it by default: keeping some space makes future expansion or the creation of another volume easier.

Next, expand the ext4 or XFS file system.

At this stage, the LV is larger, but df -h It can still display the old capacity. The file system still needs to be enlarged. Check its type instead of deducing it from the volume name:

findmnt -no SOURCE,FSTYPE,TARGET /srv/data
lsblk -f /dev/vgdata/lvdata

For a file system ext4, use resize2fs on the logical volume:

The online extension works on modern ext4 systems when mounted. If the tool requests offline control or if the filesystem is old, schedule maintenance instead of forcing the extension. See the documentation for limitations. resize2fs.

For XFSTarget the mount point, not the device:

sudo xfs_growfs /srv/data

XFS must be mounted for this operation and cannot shrink a filesystem. The man page for xfs_growfs details this operation. Do not apply these commands to Btrfs, ZFS, LUKS or a RAID stack: each layer has its own procedure.

lvextend -r LVM can extend both the LVM and the filesystem in the same command when supported. For sensitive operations, I prefer the two steps to be separate. You can immediately see if LVM was successful and if filesystem extension is still required.

Case 2: The disc has grown, but the PV remains its original size.

This is a common occurrence after increasing the size of a disk in a hypervisor. lsblk can show a larger disk while pvs does not indicate any additional free space.

If the PV relies on a partition, that partition must first occupy the new disk space. Do not launch pvresize before confirming his new limit with lsblk and the partitioning tool. The tutorial on parted under Linux explains how to identify the disk, save the table and replay the changes without creating a new file system.

Once the LVM partition has actually been enlarged, test and then resize the PV:

sudo pvs -o pv_name,pv_size,pv_free,vg_name
sudo pvresize --test /dev/nvme0n1p3
sudo pvresize /dev/nvme0n1p3
sudo vgs -o vg_name,vg_size,vg_free

The new ability must appear in VFreeYou can then return to lvextend, then expand the file system. The documentation for pvresize confirms that the tool resizes an existing PV without automatically moving other layers.

If the PV uses the entire disk, if LUKS is located between the partition and LVM, or if you add a new disk to the VG, the order changes. Stop as soon as the string displayed by lsblk does not correspond to the example. A correct command executed on the wrong layer is still a bad intervention.

Check the size at each level after the extension

Finish by checking the PV up to the mounting point. Don’t just rely on the success message. lvextend :

sudo pvs -o pv_name,pv_size,pv_free,vg_name
sudo vgs -o vg_name,vg_size,vg_free
sudo lvs -o lv_name,vg_name,lv_size,lv_path,devices
findmnt -no SOURCE,FSTYPE,TARGET /srv/data
df -hT /srv/data
systemctl --failed
journalctl -k --since "-10 min" -p warning..alert --no-pager

If lvs displays the new size but not df, do not execute lvextend Try again. The file system probably hasn’t been expanded yet, or you’re controlling a different mount point. findmnt And lsblk -f before placing any new order.

sudo apt update && sudo apt upgrade