Linux Expand disk space on LVM
expand disk space on a logical volume via an extra disk.
Background information
Sometimes the required disk space for an application or service is greater than initially anticipated for. To prevent that you have to reinstall and reconfigure the server to suit your disk space needs, we can expand the filesystems with extra space that was added by adding an extra physical disk and use it to expand the available disk space to the system. For this procedure to work you need to already utilize LVM on your disks, if not, well it's still possible but a lot harder to expand your disk space. This procedure can be performed on a live system without experiencing any downtime.
To make this process as simple as possible, the process is divided into several steps. Each of these steps completes a certain task required in this process.
Check if the available disk is completely empty (no partition tables on the disk) and remove them when it's not empty
The command lsblk will show all available block devices
# lsblk -fa
NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
fd0
loop0 squashfs 0 100% /snap/core18/2344
sda
├─sda1
├─sda2 LVM2_member ycKirk-5Njf-jl0R-lGOE-jKbV-xmM5-dNEo0u
│ ├─ubuntu--vg-lv_boot ext4 09833142-b287-4b63-a06b-9ad220d3feb1 997M 16% /boot
│ ├─ubuntu--vg-lv_root ext4 8787c3f7-6651-4787-8fbb-907161694f75 8.1G 40% /
│ ├─ubuntu--vg-lv_var ext4 7d71eec1-eec5-4083-b882-aa5256bfa98e 2.2G 72% /var
│ ├─ubuntu--vg-lv_swap swap eb6d1ce5-46c3-4057-860b-ac9658b122df
│ └─ubuntu--vg-lv_home ext4 d8248abf-b466-480b-b962-3dfc00cc810e 5.9G 2% /home
└─sda3 LVM2_member Gzk1sQ-MTMY-tlr3-AH1g-2bAI-0O4V-SdkfXM
├─ubuntu--vg-lv_boot ext4 09833142-b287-4b63-a06b-9ad220d3feb1 997M 16% /boot
└─ubuntu--vg-lv_root ext4 8787c3f7-6651-4787-8fbb-907161694f75 8.1G 40% /
sdb
sdc
sr0
The output shows that both the sdb and the sdc disks have no partitions on them. If the disks have a partition on them you will get the following error when you try to add the disk as a physical Volume and you will need to remove the partition table manually with wipefs
# pvcreate /dev/sdc
Device /dev/sdc excluded by a filter.
Run wipefs with the following options to remove the partition table
wipefs --all --backup /dev/sdc
When you re-run the lsblk -fa command you will see that the disk is now empty.
lsblk -fa
Initialize the empty disk as a Physical Volume so we can add it to a volume group (PV)
The pvcreate command Initializes the empty disk so we can assign it to a volume group
pvcreate /dev/sdc
Check which Volume Group you want to assign the disk to
The vgdisplay command will show you which Volume Groups are available
vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 11
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 5
Open LV 4
Max PV 0
Cur PV 3
Act PV 3
VG Size 84.87 GiB
PE Size 4.00 MiB
Total PE 21727
Alloc PE / Size 16583 / <64.78 GiB
Free PE / Size 5144 / 20.09 GiB
VG UUID DD2SCh-4T1o-PhO9-sTNm-p72z-3zJg-2NNC0j
In this case, we only have one Volume Group available the "ubuntu-vg"
Extend the Volume Group with the disk we just initialized
vgextend ubuntu-vg /dev/sdc
The output looks something like below
Volume group "ubuntu-vg" successfully extended
You can now check with the pvscan command if the PV is assigned to the Ubuntu VG
PV /dev/sda2 VG ubuntu-vg lvm2 [<29.00 GiB / 0 free]
PV /dev/sda3 VG ubuntu-vg lvm2 [<5.88 GiB / 100.00 MiB free]
PV /dev/sdc VG ubuntu-vg lvm2 [<50.00 GiB / <50.00 GiB free]
Total: 3 [84.87 GiB] / in use: 3 [84.87 GiB] / in no VG: 0 [0 ]
Assign the extra space to a Logical Volume
To assign the extra space to a specific Logical Volume we need to run the lvextend command
lvextend -L+30G /dev/ubuntu-vg/lv_var
The output should be similar to this
Size of logical volume ubuntu-vg/lv_var changed from 10.00 GiB (2560 extents) to 40.00 GiB (10240 extents).
Logical volume ubuntu-vg/lv_var successfully resized.
Resizing the Filesystem
Now that the additional disk space is assigned to the Logical Volume, we can now proceed with resizing the filesystem to make the additional space available.
The resize2fs command handles this process and is very straightforward, it only needs a single option and that's the path of the Logical Volume that it needs to resize
resize2fs /dev/ubuntu-vg/lv_var
Output should be something similar to this
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/ubuntu-vg/lv_var is mounted on /var; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 5
The filesystem on /dev/ubuntu-vg/lv_var is now 10485760 (4k) blocks long.
If you now run the df command you can see that the additional space is available to the filesystem
df -h /var
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-lv_var 40G 7.1G 31G 19% /var
The above procedure shows how simple it is to expand the filesystem when configured with LVM. So my suggestion would be to never again install a server without LVM because it makes life so much easier.