LVM: The Powerful Tool for Managing Storage on Linux Systems

LVM The Powerful Tool for Managing Storage on Linux Systems

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

image.png (1)

Physical Volumes

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.

Volume Groups

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

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.

Security Advice

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

Install 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:

sudo lsblk 

The output looks something like this:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 400G 0 disk
| -sda1 8:1 0 399.9G 0 part /
| -sda14 8:14 0 3M 0 part
`-sda15 8:15 0 124M 0 part /boot/efi

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:

pvcreate PARTITION

In our case, PARTITION is /dev/sda1:

pvcreate /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:

sudo pvs

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:

sudo pvs

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:

sudo lvs

Now, you can find your logical volume in the list of block devices:

sudo lsblk

You can find it here:

/dev/VOLUME_GROUP/VOLUME_NAME

In our case that would be:

/dev/group1/vol1

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:

mkfs.ext4 /dev/group1/vol1

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),  If you have a new storage medium attached to the machine, you first need to create a physical volume:

pvcreate PARTITION

For example:

pvcreate /dev/sdb1

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:

resize2fs /dev/VOLUME_GROUP/VOLUME_NAME

In our case:

resize2fs /dev/group1/vol1

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:

umount /dev/group1/vol1

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:

resize2fs /dev/group1/vol1

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”:

pvmove /dev/sdb1

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:

pvremove /dev/sdb1
See Also: Experience Our for Free VPS Hosting: Enjoy a 30-Day Trial with Risk-Free Servers

COMPLETE DIGITAL SERVER SOLUTIONS FOR ALL

Bare Metal Dedicated Servers

A single tenant, physical server allowing you full access to its resources

Read More

Cloud VPS

The cheapest way to get your own independent computing instance.
Read More

Cloud VDS

Virtualized server platform hosted on enterprise-grade physical servers

Read More

10 Gbps Unmetered Servers

Zumiv offers high bandwidth dedicated servers up to 20Gbps.
Read More

ZUMIV NEWSLETTER

Receive the latest news, updates and offers. You can unsubscribe at any time.

ZUMIV NEWSLETTER

Receive the latest news, updates and offers. You can unsubscribe at any time.

zumiv logo white final 1 1

Cloud VPS and Bare Metal Servers for Less

Registered Office:
124 City Road, London
EC1V 2NX, United Kingdom.
44-7-441-399-305
Support Hours: 24x7x365
Sale Office Hours: M-F, 7AM-5PM EST

We accept the following:

payment option

PRODUCTS

SERVICES

© 2023 Webcom Ltd. (DBA zumiv.com) Company,  CR: 14252964, ® All Rights Reserved

certifications

ZUMIV NEWSLETTER

Receive the latest news, and offers. You can unsubscribe at any time.

CALL US 44.7.441.399.305

  • PRODUCTS
  • LOCATIONS
  • SOLUTIONS
  • COMPANY
This is a staging enviroment

Please tell us more about yourself.

Complete the form below and one of our experts will contact you within 24 hours or less. For immediate assistance contact us.

In order to finalize your application, please read and accept our Terms and Conditions*.

CUSTOM QUOTE REQUEST

Complete the form below and one of our experts will contact you within 24 hours or less. For immediate assistance contact us.

We promise not to sell, trade or use your email for spam. View our Privacy Policy.