• Root/
  • Linux/
  • LVM
  • LVM offers extending logical partitions over multiple hard drives or creating mirrored storage. Whilst seemingly easy and user-friendly, caution has to be taken as boot repair is sometime not possible and issues can occur while trying to extend partitions or execute various LVM-specific operations. Below is example of reasonable LVM operation (extending logical volume using new physical device). Note that all LVM operations carry significant risk of total data loss due to human/software error.

    Helpful commands

    1. Displaying layout and names of disk devices (device names in first column) sudo lsblk -fp
    2. Displaying mounted partitions (including LVM logical volume names, when used) df -hT
    3. Listing LVM volume groups sudo vgs
    4. Listing LVM logical volumes sudo lvs
    5. Extending logical volume LV_NAME (see: listing LVM logical volumes), a member of volume group VG_NAME (see: listing LVM volume groups) by 10 GiB sudo lvextend -L +10G VG_NAME/LV_NAME or by 10% of free (unassigned) space sudo lvextend -l +10%FREE VG_NAME/LV_NAME
    6. Growing ext4 filesystem of FILESYSTEM_PATH (see: displaying mounted partitions) to fill out all unused space on logical volume sudo resize2fs FILESYSTEM_PATH for XFS sudo xfs_growfs FILESYSTEM_PATH

    Expanding LVM partition by adding disk device

    This is typical, least-risky method of adding space to a partition that is located on Logical Volume. It's still risky operation which might result in outage/data loss without any operator error, especially under heavy IO load and it "pollutes" list of devices attached to VM (in case of virtualized environment).

    1. Open new tmux session, to ensure operation will continue even if connection is lost tmux If you've lost connection to machine performing changes, attach to an existing tmux session tmux a Alternatively, if you prefer to use screen, open new screen session screen To detach screen session from other and attach to it screen -d -r
    2. Extend volume group VG_NAME (see: listing LVM volume groups) by device with device path DEVICE_PATH (see: displaying layout and names of disk devices) sudo vgextend VG_NAME DEVICE_PATH
    3. Follow instructions on how to extend logical volumes
    4. Follow instructions on how to resize filesystem

    Expanding Logical Volume that uses LVM-based pseudo-RAID1

    LVM redundancy capabilities cost performance and are fragile (as LVM is).

    ❗ This guide offer less details on how to determine correct arguments. Always learn in safe test environment, never do anything on production without backups that were recently verified to be sufficient and accessible.

    1. Open LVM config file - make sure scan_lvs is set to 1 sudo nano /etc/lvm/lvm.conf
    2. Create PVs on both new devices sudo pvcreate NEW_FIRST_DEVICE_PATH sudo pvcreate NEW_SECOND_DEVICE_PATH
    3. Create new VG NEW_VG_NAME with members PVs NEW_FIRST_DEVICE_PATH and NEW_SECOND_DEVICE_PATH sudo vgcreate NEW_VG_NAME NEW_FIRST_DEVICE_PATH NEW_SECOND_DEVICE_PATH
    4. Create new LV NEW_LV_NAME in new VG NEW_VG_NAME sudo lvcreate --mirrors 1 --type raid1 -l 100%FREE --nosync -n NEW_LV_NAME NEW_VG_NAME
    5. Create new PV (it will accessible as NEW_VG_NAME!) in LV NEW_LV_NAME (we do this to extend LV that is already in LVM RAID1) sudo pvcreate /dev/NEW_LV_NAME
    6. Follow Expanding LVM partition by adding disk device

      Use NEW_VG_NAME from here as LV_NAME there

    Expanding LVM partition by resizing disk device

    It's more risky and complex method of adding space to partition that is located on Logical Volume which does not add additional disk devices to gain more space without rebooting.

    1. Issue iSCSI rescan command; - characters are wildcards to scan for any change of channel, SCSI target ID and LUN
      echo "- - -" > /sys/class/scsi_host/host0/scan
      echo "- - -" > /sys/class/scsi_host/host1/scan
      echo "- - -" > /sys/class/scsi_host/host2/scan
    2. Refresh size of iSCSI devices echo 1 > /sys/block/DISK_DEVICE_PATH/device/rescan
    3. Check for capacity change in kernel logs sudo dmesg | grep 'detected capacity change'
    4. Check current partition table of DEVICE_PATH (see: displaying layout and names of disk devices) to determine number of partition (PARTITION_NUMER) you wish to extend. Typically, on boot disk, first partition will be backward-compatibility related, second will be boot partition and third will be root partition fdisk -l DEVICE_PATH | grep ^/dev
    5. Resize target partition on DEVICE_PATH
      parted DEVICE_PATH
      print
      F
      resizepart PARTITION_NUMER 100%
      quit
    6. Resize PV to use all of available space sudo pvresize PV_NAME
    7. Follow instructions on how to extend LV
    8. Follow instructions on how to resize filesystem

    Last update: 2024-08-10