In this brief post we will take a look at how to expand a Logical Volume (LVM) that your Ubuntu Server operating system is installed in. This worked for me on Ubuntu Server 20.04. When you install Ubuntu Server it creates an LVM and is designed to use up the least amount of space required so as to minimize the amount of space taken up by the volume. This way you have more control over this in the future and can expand it if you would like per your own requirements (rather than assuming you want to use up all the available space from the start). Unfortunately this behaviour is not made very clear when you install Ubuntu. This post on askUbuntu.com demonstrates this confusion and is also a helpful guide in how to expand the LVM after Ubuntu has been installed.
The first thing that you will likely want to do is get a list of the drives available on your system to see what is mounted at root “/”. In order to do this you can execute the following on the command line:
This will display something that looks as follows, notice that /dev/mapper/ubuntu–vg-ubuntu-lv is mounted at root “/”. My server is setup with a total capacity of 6TB but we can see here that only 175GB has been allocated to the Ubuntu Server LVM so we will want to expand this to take advantage of the full capacity:
Filesystem Size Used Avail Use% Mounted on udev 126G 0 126G 0% /dev tmpfs 26G 2.6M 26G 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 196G 12G 175G 7% / 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 203M 707M 23% /boot /dev/loop0 56M 56M 0 100% /snap/core18/2128 /dev/loop1 62M 62M 0 100% /snap/core20/1081 /dev/loop3 68M 68M 0 100% /snap/lxd/21545 /dev/loop2 72M 72M 0 100% /snap/lxd/16099 /dev/loop4 33M 33M 0 100% /snap/snapd/13170 /dev/loop6 30M 30M 0 100% /snap/snapd/8542 /dev/loop5 55M 55M 0 100% /snap/core18/1880 //192.168.1.3/ris-data 14T 22G 14T 1% /mnt/ris-data tmpfs 26G 0 26G 0% /run/user/1000
Next you may want to check and see what additional space is actually available in the volume group for your current mapped drive. You can do this by executing the following on the command line:
sudo vgdisplay -A
You should see something with the following result, notice that the Alloc PE/Size is listed as 200TGiB and that the Free PE/Size is listed as 6.35TiB, this means there is roughly 6.35TB of available space that is not allocated to the LVM that your Ubuntu install is currently on and we want to expand it:
--- Volume group --- VG Name ubuntu-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 <6.55 TiB PE Size 4.00 MiB Total PE 1716223 Alloc PE / Size 51200 / 200.00 GiB Free PE / Size 1665023 / 6.35 TiB VG UUID XDRpII-cxO9-jqAV-83eC-Ukea-aV4y-OHXWW1
In order to change the size utilized by your Ubuntu Server install you’ll need to use a tool called Logical Volume Manager (LVM). In order to start this tool enter the following in the command line, this should open the lvm tool and you should see an lvm> prompt waiting for you to type:
When the LVM prompt enter the following command to extend your LVM to utilize the full amount of space available on your drive. The command as displayed in the following code block should work on most Ubuntu Server base installs as it is the default unless you have configured your installation differently the the defaults available. For further details on the lvextend command please refer to the man page.
lvm> lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
When this is complete you should have output similar to to following:
Size of logical volume ubuntu-vg/ubuntu-lv changed from 200.00 GiB (51200 extents) to <6.55 TiB (1716223 extents). Logical volume ubuntu-vg/ubuntu-lv successfully resized.
Type exit to quite the LVM tool:
You’re not quite finished yet, lvextend increases the size of the Logical Volume to 100% but the file system still needs to be updated to utilizes the newly available capacity. You do this using resize2fs with the following command, for further details on how the resize2fs command works please refer to the man page:
sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
The following is an example of what the output looks like when this completes successfully, note on my system this took a few minutes to complete:
resize2fs 1.45.5 (07-Jan-2020) Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required old_desc_blocks = 25, new_desc_blocks = 838 The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 1757412352 (4k) blocks long.
Once this has completed you can re-run the df -h command to confirm that the full capacity is now being used by your LVM. The output should look similar to the following with the LVM which is mounted on root indicating the newly available capacity as the full drive capacity, in my case 6.2 TB.
Filesystem Size Used Avail Use% Mounted on udev 126G 0 126G 0% /dev tmpfs 26G 2.6M 26G 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 6.5T 12G 6.2T 1% / 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 203M 707M 23% /boot /dev/loop0 56M 56M 0 100% /snap/core18/2128 /dev/loop1 62M 62M 0 100% /snap/core20/1081 /dev/loop3 68M 68M 0 100% /snap/lxd/21545 /dev/loop2 72M 72M 0 100% /snap/lxd/16099 /dev/loop4 33M 33M 0 100% /snap/snapd/13170 /dev/loop6 30M 30M 0 100% /snap/snapd/8542 /dev/loop5 55M 55M 0 100% /snap/core18/1880 //192.168.1.3/ris-data 14T 22G 14T 1% /mnt/ris-data tmpfs 26G 0 26G 0% /run/user/1000
This might seem daunting at first but it is actually really easy to do. What’s more, you can perform this whole task while the drive is in use which is amazing. I hope that this has been helpful and if you have any thoughts to add feel free to commend below.