How to extend a LVM-disk in Linux


Extending a disk in a non-graphical interface in Linux is a bit tricky if you’re not used to it. A lot can go wrong, so make sure you have backups of the content.

I’m choosing to focus on LVM-disks as that’s what I tend to use the most, and they are easy to work with in my opinion, both on physical and virtual machines.

To run the majority of the commands below, you need to be root.

Run fdisk -l to see the current size of the disk you want to extend and the name of it (sda, sdb…).

fdisk -l

If you haven’t already grown the disk in your hypervisor, do it now.
Now you have to scan the virtual SCSI-bus for changes to your disks.
* May only be necessary if you’ve added a new disk to grow your LVM.

for host in /sys/class/scsi_host/*; do
  echo "- - -" | sudo tee $host/scan
  ls /dev/sd*
done

Next, do a scan of the devices for each device under “/sys/class/scsi_device/“. You need to “echo” a 1 to the rescan-file under each device. Do it for all devices if you’re unsure which one you’ve resized.

ls /sys/class/scsi_device/
echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/rescan
echo 1 > /sys/class/scsi_device/32\:0\:0\:0/device/rescan
echo 1 > ...

Now you should see a difference in size on the disk you’ve grown.

fdisk -l

If you have partitions on the drive you want to extend, you need to recreate the partition table. If you use a “raw-disk” for you lvm, you can skip this part.

Run fdisk with the disk you’ve gown (sda, sdb…). Make not of the start-sector of the partition you want to extend.

fdisk /dev/sda
Device       Start       End   Sectors   Size Type
/dev/sda1     2048   2203647   2201600     1G EFI System
/dev/sda2  2203648   6397951   4194304     2G Linux filesystem
/dev/sda3  6397952 234438655 228040704 108.7G Linux filesystem

Delete (yes, delete) the partition table of the partition where the LVM is located. Usually on it might be number 3 (sda3). Do it in the following order:

d - for delete
  Select the numer of the partition to delete
n - for new partition. Make sure the start sector is the same as before. The rest of the questions should be left as default (unless you don't want you use all of the disk)
p - print the outcome
t - set partition type (not mandatory)
(Linux LVM)
p - print again
w - write (save) configuration

Now you need to extend the physical volume.

pvresize /dev/sda3

Verify that you have the expected free space on the volume-group using vgs. Example:

# vgs
VG  #PV #LV #SN Attr   VSize VFree
vg0 2   5   0   wz--n- 120g  30g

Find the lv (logical volume) you want to extend using df -h. Example:

/dev/mapper/vg0-lv--var 50G 45G 5G 90% /var

Extend the lv.
-r – Resizes the filesystem (ext4, xfs…)
-l +100%FREE – use all of the free disk

lvextend -r -l +100%FREE /dev/mapper/vg0-lv--var

If you’re using btrfs instead of ext4 or xfs, remove the “-r” and run this command afterwards.

btrfs filesystem resize max /var

Verify that that the filesystem is resized.

df -h