Linux LVM

( Logical Volume Management)

LVM stands for Logical Volume Management. LVM is used to resize the partition in Linux. If partition will be formatted by ext/ext2/ext3/ext4 then user will not be able to extend/reduce the Partition size . if it will be formatted by LVM then We can make change in partition size as we need. One of the difficult decisions facing a new user installing Linux for the first time is that how to partition the disk drive. The need to estimate just how much space is likely to be needed for system files and user files makes the installation more complex than is necessary . In logical volume management, the whole disk would be allocated to a single volume group and logical volumes created to hold the /usr and /home file systems. If, for example the /home logical volume later filled up but there was still space available on /usr then it would be possible to shrink /usr by a few megabytes and reallocate that space to /home.
Another alternative would be to allocate minimal amounts of space for each logical volume and leave some of the disk unallocated. Then, when the partitions start to fill up, they can be expanded as necessary.
The benefits of logical volume management are more obvious on large systems with many disk drives.

LVM Architecture and Terminology

Before we dive into the actual LVM administrative commands, it is important to have a basic understanding of how LVM organizes storage devices and some of the terminology it employs.

LVM Storage Management Structures

LVM functions by layering abstractions on top of physical storage devices. The basic layers that LVM uses, starting with the most primitive, are.

Physical Volumes:

LVM utility prefix: pv...

Description: Physical block devices or other disk-like devices (for example, other devices created by device mapper, like RAID arrays) are used by LVM as the raw building material for higher levels of abstraction. Physical volumes are regular storage devices. LVM writes a header to the device to allocate it for management.

Volume Groups:

LVM utility prefix: vg...

Description: LVM combines physical volumes into storage pools known as volume groups. Volume groups abstract the characteristics of the underlying devices and function as a unified logical device with combined storage capacity of the component physical volumes.

Description: A volume group can be sliced up into any number of logical volumes. Logical volumes are functionally equivalent to partitions on a physical disk, but with much more flexibility. Logical volumes are the primary component that users and applications will interact with.

In summary, LVM can be used to combine physical volumes into volume groups to unify the storage space available on a system. Afterwards, administrators can segment the volume group into arbitrary logical volumes, which act as flexible partitions.

What are Extents?

Each volume within a volume group is segmented into small, fixed-size chunks called extents. The size of the extents is determined by the volume group (all volumes within the group conform to the same extent size).

The extents on a physical volume are called physical extents, while the extents of a logical volume are called logical extents. A logical volume is simply a mapping that LVM maintains between logical and physical extents. Because of this relationship, the extent size represents the smallest amount of space that can be allocated by LVM.

Extents are behind much of the flexibility and power of LVM. The logical extents that are presented as a unified device by LVM do not have to map to continuous physical extents. LVM can copy and reorganize the physical extents that compose a logical volume without any interruption to users. Logical volumes can also be easily expanded or shrunk by simply adding extents to or removing extents from the volume.

The Simple Use Case

Now that you are familiar with some of the terminology and structures LVM uses, we can explore some common ways to use LVM. We will start by walking through a basic procedure that will use two physical disks to form four logical volumes.

Our first step is scan the system for block devices that LVM can see and manage. You can do this by typing:

sudo lvmdiskscan

The output will display all available block devices that LVM can interact with:

From the above output, we can see that there are currently two disks and 17 partitions. The partitions are mostly /dev/ram* partitions that are used the system as a Ram disk for performance enhancements. The disks in this example are /dev/sda, which has 200G of space, and /dev/sdb, which has 100G.

Warning: Make sure that you double-check that the devices you intend to use with LVM do not have any important data already written to them. Using these devices within LVM will overwrite the current contents. If you already have important data on your server, make backups before proceeding.

Now that we know the physical devices we want to use, we can mark them as physical volumes within LVM using the pvcreate command:

As you can see, both of the devices are present under the PV column, which stands for physical volume.
Add the Physical Volumes to a Volume Group

Now that we have created physical volumes from our devices, we can create a volume group. We will have to select a name for the volume group, which we’ll keep generic. Most of the time, you will only have a single volume group per system for maximum flexibility in allocation. We will call our volume group LVMVolGroup for simplicity.

To create the volume group and add both of our physical volumes to it in a single command, type:

As you can see, our volume group currently has two physical volumes, zero logical volumes, and has the combined capacity of the underlying devices.
Creating Logical Volumes from the Volume Group Pool

Now that we have a volume group available, we can use it as a pool that we can allocate logical volumes from. Unlike conventional partitioning, when working with logical volumes, you do not need to know the layout of the volume since LVM maps and handles this for you. You only need to supply the size of the volume and a name.

To create logical volumes, we use the lvcreate command. We must pass in the volume group to pull from, and can name the logical volume with the -n option. To specify the size directly, you can use the -L option. If, instead, you wish to specify the size in terms of the number of extents, you can use the -l option.

We can create the first three logical volumes with the -L option like this:

We have added the last two columns of output so that we can see the space allocated to our logical volumes.

Now, we can allocate the rest of the space in the volume group to the “workspace” volume using the -l flag, which works in extents. We can also provide a percentage and a unit to better communicate our intentions. In our case, we wish to allocate the remaining free space, so we can pass in 100%FREE:

sudo lvcreate -l 100%FREE -n workspace LVMVolGroup

Output
Logical volume “workspace” created.

If we recheck the volume group information, we can see that we have used up all of the available space: