Most people still use standard partitioning schemes on their Linux desktops and servers. But as with most things in Linux, you have choices.
An alternative to traditional partitioning is to use Logical Volume Manager, or LVM. This blog post explains what LVM does, and how to use it.
Key Advantages
- You can create single volumes from multiple physical hard disks/volumes.
- In the case of virtualisation, you can easily add additional space to a virtual machine without a reboot, and online resize your LVM/partition.
- Consistent backups can be taken whilst the volume is online by using LVM snapshots.
- LVMs can be created to include raid functionality, including Raid 1, 5 and 6.
- Entire LVs can be striped across multiple physical volumes, much like Raid 0.
Objectives
In order to learn what can be achieved with LVM, I’m going to assume you’re running a Debian- based distribution (e.g. Ubuntu, Debian); and we’re going to carry out the following:
- Create physical volumes with LVM
- Create volume groups with LVM
- Create logical volumes with LVM
- Perform an online resize of a logical volume
Create a physical volume with LVM
You can see if there are any existing volumes in place by running pvdisplay at the command line. If you get a command not found error, then you’ll need to install lvm with apt-get install lvm2.
Now we can move on to create a physical volume. You can find if any are spare by running fdisk -l and taking note of any physical devices without any partitions present. In my case, I have /dev/sdb and /dev/sdc free to use. As such, I will go ahead and run:
[email protected]:~# pvcreate /dev/sdb /dev/sdc Physical volume "/dev/sdb" successfully created Physical volume "/dev/sdc" successfully created [email protected]:~#
Now, I can see the existing physical volumes along with my newly created ones.
---Physical volume--- PV Name /dev/sda5 VG Name debian-test-vg PV Size 15.76 GiB / not usable 2.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 4034 Free PE 0 Allocated PE 4034 PV UUID DruG7X-n0H3-he8J-4KOb-sWyL-KBUz-1iT2ZD “/dev/sdb” is a new physical volume of “16.00 GiB” ---NEW Physical volume--- PV Name /dev/sdb VG Name PV Size 16.00 GiB Allocatable No PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID e3ZXGv-fa3c-1CBs-61Ue-FrLM-cbcM-PRucwn “/dev/sdc” is a new physical volume of “16.00 GiB” ---NEW Physical Volume--- PV Name /dev/sdb VG Name PV Size 16.00 GiB Allocatable No PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID YLPy6Q-CQJL-zPut-rTUt-1ro7-n2n1-Ksk0z2
Create volume groups with LVM
Now we’re ready to create a volume group with our new physical volumes. I’ll choose to create a volume group called ‘test’ on the new physical volumes we’ve added.
[email protected]:~# vgcreate test /dev/sdb /dev/sdc Volume group "test" successfully created [email protected]:~# We can list these by running vgdisplay
:
[email protected]:~# vgdisplay --- Volume group --- VG Name test System ID Formate 1vm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 31.99 GiB PE Size 4.00 MiB Total PE 8190 Alloc PE/Size 0 / 0 Free PE/Size 8190 / 31.99 GiB VG UUID zcmtQ3-L7yB-qksc-s3d7-8cLM-am0F-5JbJ1d
Create logical volumes with LVM
For simplicity’s sake, I’m going to create a logical volume utilising 100% of the free space on the volume group by running the following:
[email protected]:~# lvcreate -l 100%FREE -n test-lv test Logical volume "test-lv" created [email protected]:~#
The above command creates a logical volume named ‘test-lv’ on the volume group ‘test’.
We can see the result of our work by running lvdisplay :
[email protected]:~# 1vdisplay --- Logical volume --- LV Path /dev/test/test-lv LV Name test-lv VG Name test LV UUID tLOOIR-V0jb-de7s-Z4s9-LDpe-c105-ru8oQS LV Write Access read/write LV Creation host, time debian-test, 2017-05-15 22:28:15 +0100 LV Status available # open 0 LV Size 31.99 GiB Current LE 8190 Segments 2 Allocation inherit Read ahead sectors auto -Current set to 256 Block device 254:2
At this point, we’ll want to create a filesystem on our logical volume to be able to utilise it within Linux. Simply run: mkfs.ext4 /dev/test/test-lv—From here we can simply mount the filesystem in a directory as one normally would by running mount /dev/test/test-lv /mnt/
We can see that the partition is mounted successfully by running df -h /mnt/test/ :
[email protected]:~# df -h /mnt/test/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/test-test--1v 32G 48M 30G 1% /mnt/test [email protected]:~#
Perform an online resize of a logical volume
So, you’re all up and running with LVM and loving it…But Suzie from management says that a new project is coming your way that requires a whopping 60GB of space on the same server, and you can’t have any downtime. What do you do?!?! :O
This scenario is simple. If you’re running a physical server with hot swappable storage, you can simply add another disk whilst the server is running. Alternatively, as is often the case these days, you may be running a virtual machine where you can add a hard drive to the virtual machine without too much hassle. Once you’ve done this, search for it using fdisk -l the same way as before. Once you’ve found the new disk, simply run pvcreate as we did previously. In my case, it’s pvcreate /dev/sdd .
[email protected]:~# pvcreate /dev/sdd Physical volume "/dev/sdd" successfully created [email protected]:~#
Now let’s extend our volume group by running vgextend test /dev/sdd where the name of the volume group we created earlier and /dev/sdd is the new physical volume we added:
[email protected]:~# vgextend test /dev/sdd Volume group "test" successfully extended [email protected]:~#
We can see that we have grown our logical volume and its new size by running vgdisplay test :
[email protected]:~# vgdisplay test --- Volume group --- VG Name test System ID Formate 1vm2 Metadata Areas 3 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 3 Act PV 3 VG Size 91.99 GiB PE Size 4.00 MiB Total PE 23549 Alloc PE/Size 8190 / 31.99 GiB Free PE/Size 15359 / 60.00 GiB VG UUID zcmtQ3-L7yB-qksc-s3d7-8cLM-am0F-5JbJ1d
At this point we can extend our logical volume by running lvextend -l 100%FREE -n /dev/test/test-lv :
[email protected]:~# lvextend -l 100%FREE -n /dev/test/test-lv Size of logical volume test/test-lv changed from 31.99 GiB (8190 extents) to 60.00 GiB (15359 extents). Logical volume test-lv successfully resized [email protected]:~#
To resize the filesystem resident on the logical volume, simply run resize2fs /dev/test/test-lv and it will perform an online resize!
[email protected]:~# df -h /mnt/test/ Filesystem size Used Avail Use% Mounted on /dev/mapper/test-test--lv 59G 52M 57G 1% /mnt/test [email protected]:~#
Conclusion
There is a lot more you can do with LVM, but so far in my career, the functionality shown above has proven to be most useful. Further links below are included to assist you in your LVM journey!
Resources
- Further reading on LVM: https://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)
- Old (but still relevant) examples of LVM usage: http://tldp.org/HOWTO/LVM-HOWTO/
- An overview of LVM on Debian’s website: https://wiki.debian.org/LVM