Extending a Linux Logical Volume

After setting up my KVM system, I quickly realized that the 1Tb hard drive I was using for KVM-related files wasn't going to cut it, so I bought a 4Tb drive to go along with it.

Inserted the new drive, and booted up the system.

Prechecks

  • Before changing anything, dump the current config of the LVM VG, named kvm-vg

    sudo vgdisplay kvm-vg
      --- Volume group ---
      VG Name               kvm-vg
      System ID
      Format                lvm2
      Metadata Areas        1
      Metadata Sequence No  2
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                1
      Open LV               1
      Max PV                0
      Cur PV                1
      Act PV                1
      VG Size               931.51 GiB
      PE Size               4.00 MiB
      Total PE              238467
      Alloc PE / Size       238467 / 931.51 GiB
      Free  PE / Size       0 / 0
      VG UUID               4F1YAS-cprX-inKs-eMdp-5aYy-tfCx-e6oAmB
    
  • Check to make sure the newly inserted drive is recognized:

    sudo fdisk -l
    [sudo] password for tom:
    
    <snip>
    
    Disk /dev/sdc: 3.65 TiB, 4000787030016 bytes, 7814037168 sectors
    Disk model: WDC WD4003FRYZ-0
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    
    Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 200 GiB, 214748364800 bytes, 419430400 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 /dev/mapper/kvm--vg-kvm--lv: 931.52 GiB, 1000203091968 bytes, 1953521664 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    
  • Erase the current partition table on the drive, not necessary, but I still do it, just to be safe:

    sudo dd if=/dev/zero of=/dev/sdc bs=512 count=1
    1+0 records in
    1+0 records out
    512 bytes copied, 0.00103809 s, 493 kB/s
    

Use LVM commands to add the disk

  • Create a Physical Volume from the new drive:

    sudo pvcreate /dev/sdc
      Physical volume "/dev/sdc" successfully created.
    
  • Extend (add) the new drive to the Volume Group:

    sudo vgextend kvm-vg /dev/sdc
      Volume group "kvm-vg" successfully extended
    
  • Display the Volume Group, after adding the new drive:

    sudo vgdisplay kvm-vg
      --- Volume group ---
      VG Name               kvm-vg
      System ID
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  3
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                1
      Open LV               1
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size               <4.55 TiB
      PE Size               4.00 MiB
      Total PE              1192328
      Alloc PE / Size       238467 / 931.51 GiB
      Free  PE / Size       953861 / <3.64 TiB
      VG UUID               4F1YAS-cprX-inKs-eMdp-5aYy-tfCx-e6oAmB
    
  • Add the new drive to the Logical Volume, specifying to use all the space on the drive:

    sudo lvm lvextend -l +100%FREE /dev/kvm-vg/kvm-lv
      Size of logical volume kvm-vg/kvm-lv changed from 931.51 GiB (238467 extents) to <4.55 TiB (1192328 extents).
      Logical volume kvm-vg/kvm-lv successfully resized.
    
  • Resize the ext4 file system:

    sudo resize2fs -p /dev/mapper/kvm--vg-kvm--lv
    resize2fs 1.45.5 (07-Jan-2020)
    Filesystem at /dev/mapper/kvm--vg-kvm--lv is mounted on /kvm; on-line resizing required
    old_desc_blocks = 117, new_desc_blocks = 583
    The filesystem on /dev/mapper/kvm--vg-kvm--lv is now 1220943872 (4k) blocks long.
    
  • Verify the newly expanded file system shows up:

    df -h
    Filesystem                         Size  Used Avail Use% Mounted on
    udev                               126G     0  126G   0% /dev
    tmpfs                               26G  2.0M   26G   1% /run
    /dev/mapper/ubuntu--vg-ubuntu--lv  196G  101G   86G  54% /
    tmpfs                              126G     0  126G   0% /dev/shm
    tmpfs                              5.0M     0  5.0M   0% /run/lock
    tmpfs                              126G     0  126G   0% /sys/fs/cgroup
    /dev/sda2                          976M  107M  803M  12% /boot
    /dev/loop0                          56M   56M     0 100% /snap/core18/1944
    /dev/loop1                          56M   56M     0 100% /snap/core18/2066
    /dev/loop3                          33M   33M     0 100% /snap/snapd/12057
    /dev/loop2                          70M   70M     0 100% /snap/lxd/19188
    /dev/loop4                          68M   68M     0 100% /snap/lxd/20326
    /dev/loop5                          33M   33M     0 100% /snap/snapd/12159
    /dev/mapper/kvm--vg-kvm--lv        4.5T  133G  4.2T   4% /kvm
    tmpfs                               26G  4.0K   26G   1% /run/user/1000