Logical Volume Manager (LVM) in Linux
Sources:
- Logical Volume Manager (LVM) versus standard partitioning in Linux
->Image source
Logical Volume Manager (LVM)
Logical volume management (LVM) is a form of storage virtualization that offers system administrators a more flexible approach to managing disk storage space than traditional partitioning.
LVM structure
The following are the components of LVM:
- Physical volumn: A physical volume (PV) is a raw storage device.
- A PV can be an entire disk (e.g.,
/dev/sda
) or a partition on a disk (e.g.,/dev/sda1
).
- A PV can be an entire disk (e.g.,
- Volume group: A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes can be allocated.
- A VG can consist of one or more PVs, potentially spanning multiple physical disks.
- VGs aggregate the storage capacity of all included PVs, making it easier to manage.
- You can create, resize, and delete Logical Volumes within a VG.
- Logical volume: A logical volume is created from the VG's storage pool and can be treated as traditional partitions.
- LVs are created from the space available in a VG.
- They can be resized dynamically, meaning you can extend or shrink them as needed.
- LVs are the volumes that you format with a filesystem and mount for use, just like traditional disk partitions.
Basic usage
Install a new hard disk drive
Purchase a one terabyte (1 TB) hard disk drive, and then physically install it into the server
Designate Physical Volumes
Physical Volumes (PV) are disks or partitions that are available to LVM as potential storage capacity. They have identifiers and metadata that describes each PV. It is interesting to note that, as opposed to RAID, PVs do not have to be the same size or or on disks that are the same speed. You can mix and match drive types to create PVs.
To implement LVM, first designate a drive (either a whole disk or a partition) as a Physical Volume.
Command to create a PV:
1
2pvcreate /dev/sdb1
pvcreate /dev/sdcThese two command examples are slightly different. The first command designates partition 1 on storage disk b as a PV. The second command sets the total capacity of storage disk c as a PV.
Display PV capacity and additional information:
1
pvdisplay
This command displays all of the Physical Volumes configured on the server.
Manage Volume Groups
Once one or more of the disks are available to LVM as Physical Volumes, the storage capacity is combined into Volume Groups (VGs).
There may be more than one VG on a server. The VG must have at least one member. The members you add to the VG must be PVs, not just raw devices. However, a device itself can be converted to different PVs, which can them be added to various VGs.
1
sudo vgcreate <volume-group-name> <PV-members>
Use the following command to create a Volume Group named
vg00
with PVs/dev/sdb1
and/dev/sdc
as members:1
sudo vgcreate vg00 /dev/sdb1 /dev/sdc
Display information for a VG named
vg00
:1
sudo vgdisplay vg00
Manage Logical Volumes
The VG can be subdivided into one or more Logical Volumes (LVs). These LVs are then used as if they were traditional partitions. The VG has a total capacity, and then some part of that capacity is allocated to a Logical Volume.
The
lvcreate
command carves storage capacity from a VG. There are a few options to be aware of.Option Description -n Name of LV - ex. sales-lv -L Size in G or T - ex. 10G -q Quiet, suppresses command output -v Verbose mode providing additional details The syntax for the
lvcreate
command is as follows:1
sudo lvcreate -L <size> -n <logical-volume-name> <volume-group-name>
Here is the command to create a 10 GB Logical Volume named sales-lv carved from the vg00 Volume Group:
1
lvcreate -L 10G -n sales-lv vg00
As you recall, we created the
vg00
Volume Group from two Physical Volumes,/dev/sdb1
and/dev/sdc
. So, in summary, we combined the capacity of/dev/sdb1
and/dev/sdc
intovg00
, then carved a Logical Volume namedsales-lv
from that aggregated storage space.You can use the
lvdisplay
command to see the Logical Volume's configuration.1
lvdisplay /dev/vg00/sales-lv
Apply a filesystem and set a mount point
Once the LV is created, it is managed as any other partition. It needs a filesystem and a mount point, just like we configured in the standard partition management section above.
- Run the
mkfs.ex4
command on the LV. - Create a mount point by using
mkdir.
- Manually mount the volume using the
mount
command, or edit the/etc/fstab
file to mount the volume automatically when the system boots. - Use the
df -h
command to verify the storage capacity is available.
- Run the
Scaling capacity
At this stage, we've seen the configuration of LVM, but we really have not yet been able to see the many benefits. One of the benefits of LVM configurations is the ability to scale storage capacity easily and quickly. Usually, of course, sysadmins need to scale up (increase capacity). It's worthwhile to note that you can also scale storage capacity down with LVM. That means that if storage capacity is over-allocated (you configured far more storage than you needed to), you can shrink it. I will cover both scenarios in this section.
Let's start with increasing capacity.
Increase capacity
You can add storage capacity to the Logical Volume. This is useful if the users consume more space than you anticipated. The process is pretty logical:
Add a disk and configure it as a PV.
Add it to a VG.
Add the capacity to the LV and then extend the filesystem.
Install a storage disk and then configure it as a PV
To increase capacity, install a new disk and configure it as a PV, as per the steps above. If there is already a disk with free space available, you can certainly use that, as well.
1
pvcreate /dev/sdb2
In this case, I am designating partition 2 on disk
/dev/sdb
as the new PV.Add space to the VG
Once the new capacity is designated for LVM, you can add it to the VG, increasing the pool's size.
Add a new PV to an existing VG:
1
vgextend vg00 /dev/sdb2
Now the storage pool is larger. The next step is to add the increased capacity to the specific Logical Volume. You can allocate any or all of the PV storage space you just added to the pool to the existing LV.
Add space to the LV
Next, add some or all of the new VG storage space to the LV that needs to be expanded.
Extend the LV to a given size:
1
lvextend -L3T /dev/vg00/sales-lv
Add 1 GB of space to the existing size:
1
lvextend -L+1G /dev/vg00/sales-lv
Extend the file system to make the storage capacity available
Finally, extend the file system. Both ext4 and XFS support this ability, so either filesystem is fine.
For ext4:
1
resize2fs /dev/vg00/sales-lv 3T
Commands
Physical Volumes (PV)
Create a PV:
1
sudo pvcreate <device>
Display VGs and their belonging VGs:
1
sudo pvdisplay
Show PVs in a Summary:
1
sudo pvs
Volume Groups (VG)
Create a VG
1
sudo vgcreate <volume-group-name> <PV-members>
Add new PVs to an existing VG:
1
sudo vgextend <volume-group-name> <PV-members>
Display VGs:
1
sudo vgdisplay
This won't show the LVs belonging to each VG.
Show VGs in a Summary:
1
sudo vgs
Logical Volumes (LV)
Create a Logical Volume:
1
sudo lvcreate -L <size> -n <logical-volume-name> <volume-group-name>
Extend a LV, note that each LV is indexed by
/dev/<VG-name>/<LV-name>
1
sudo lvextend -L +<size> /dev/<volume-group-name>/<logical-volume-name>
Or to extend to use all available space of its VG:
1
sudo lvextend -l +100\%FREE /dev/<volume-group-name>/<logical-volume-name>
Reduce a Logical Volume (Be Careful!):
1
sudo lvreduce -L 50G /dev/<volume-group-name>/<logical-volume-name>
Display LVs and their belonging VGs:
1
sudo lvdisplay
Filesystem commands
Resize Filesystem (ext4):
1
sudo resize2fs /dev/<volume-group-name>/<logical-volume-name>
Resize Filesystem (XFS):
1
sudo xfs_growfs /mount-point