LVM stands for Logical Volume Manager and is a tool to manage partitions. It adds a level of abstraction between disks, partitions and filesystems, and also allows partitions to span multiple drives.
Besides the traditional partitions, LVM introduces a new logical layer between the partition table and the file system. This new layer allows for easy changes to the partitions afterwards.
The LVM Architecture
Physical volumes in LVM represent a physical storage device connected to the machine. Before you can use hard drives in LVM, it’s required to first register them as physical volumes to LVM.
A volume group combines one or more physical volumes together. Volume groups are a central part of the LVM architecture as they connect the physical volumes with the logical volumes.
Logical Volumes are part of a volume group and can be compared to regular partitions. They contain the file system and are attached to the physical volumes through volume groups.
Because of that architecture, it’s possible for a logical volume to be larger than the physical hard drives. Combined with the possibility to add and remove physical volumes afterwards, it’s possible to extend a file system as needed by simply adding more hard drives and updating the LVM configuration.
If you have a volume group that spans multiple drives, it is highly recommended that you connect the drives in a RAID array (at least RAID level 1). Otherwise, if one drive fails, all the data in that volume group is at risk.
Getting started with LVM
If not done already, start by installing LVM:
sudo apt-get install lvm2
To setup LVM, we first need an overview of all drives connected to the machine. We can list them using lsblk:
The output looks something like this:
As you can see in the “type” column, we have one disk attached to the server a size of 400 GB and a few partitions. The partition we want to focus on is the sda1 partition with a size of 399.9 GB.
Initialize a Physical Volume
Please be advised to perform a backup of all important data before proceeding! Changes to the partition table always bear the risk of a complete data loss!
In order to use partitions in LVM, we first need to “register” them to LVM by creating physical volumes. We can do so using the pvcreate command:
In our case, PARTITION is /dev/sda1:
This command registers the partition sda1 as physical volume to LVM.
The following command can be used to get all currently registered physical volumes:
Creating a Volume Group
Next, we need a volume group. Volume groups can contain many physical volumes, but for now we only have one. So, let’s create a volume group called “group1” that contains the physical volume “/dev/sda1”:
lgcreate group1 /dev/sda1
If you have more physical volumes, you can just add them to the command like so:
lgcreate group1 /dev/sda1 /dev/sdb1 /dev/sdc1
Again, we can use the following command to view all volume groups:
Setting up Logical Volumes
With at least one volume group created, we can now create logical volumes using the lvcreate command:
lvcreate –L 40G –n vol1 group1
In the command above, “-L” specifies the size (40 GB), and “-n” the name (“vol1”). The new logical volume will be added to the volume group “group1”.
Hint: Instead of “-L 40G”, you can also use “-l 100%FREE” to use all the available space in the volume group.
Once created, we can take a look at all logical volumes with the following command:
Now, you can find your logical volume in the list of block devices:
You can find it here:
In our case that would be:
Adding a Filesystem
To store files on this new logical volume, we need to first add a file system. We can do so by creating a new one with the following command:
This sets up a new ext4 filesystem and we can now store data on this partition.
Extend an Existing LVM Partition
To extend an existing partition, you first need to make sure the disk space you want to add is available in your volume group. If you extended an already connected drive (for example because of a storage extension on a VPS), follow this guide. If you have a new storage medium attached to the machine, you first need to create a physical volume:
Next, add the new physical volume to your existing volume group:
vgextend GROUP_NAME PHYSICAL_VOLUME
In this case:
vgextend group1 /dev/sdb1
Once the new storage space is available in the volume group, we can increase the size of logical volumes. There are multiple ways to achieve this:
lvextend -L +10G group1/vol1 # Add 10 G to the volume “vol1” in the group “group1” lvextend -l 100%FREE group1/vol1 # Extend the volume “vol1” in the group “group1” to use all the available space in the volume group
In the last step, resize the filesystem to also use all the newly available space:
In our case:
Note: Depending on your filesystem, you may need to unmount your filesystem first before extending it. Ext4 can be extended while mounted, but make sure to check this for your filesystem.
Shrink an Existing LVM Partition
When shrinking your LVM partition, it’s important to do so in the correct order – the filesystem must be resized before the logical volume, otherwise you lose data or even break the whole filesystem. Ext4 requires to be unmounted while shrinking. We can unmount the filesystem using the umount command:
Next, the filesystem can be resized:
resize2fs /dev/group1/vol1 29G
Note: If you want to resize your partition to e.g. 30 G, it’s recommended to reduce the filesystem size before the shrinking process and then extend it again once the partitions are updated.
You may be asked to check your filesystem first before resizing it, in that case run the displayed command and then run the resize command again.
After the filesystem is shrunk down, we can continue by shrinking the logical volume:
lvreduce -L 30G /dev/group1/vol1
This will reduce the size of the logical volume “vol1” in the volume group “group1” to a total size of 30 G.
As a final step, let’s extend the filesystem again to make use of all the available space in the logical volume:
Again, you can use the lsblk command to verify the changes.
Remove a Physical Volume from a Volume Group
If you want to remove a physical volume from your LVM configuration, you first need to make sure no data is stored on the physical volume you want to remove. Let’s say we want to remove the physical volume “/dev/sdb1” and it is part of the volume group “group1”:
Note: This command moves all data from the /dev/sdb1 volume to other available volumes in the group. Make sure there is enough space available.
Next, the physical volume can be removed from the volume group:
vgreduce group1 /dev/sdb1
You can now add the physical volume to another volume group, or fully remove it from LVM: