LVM concepts, terminology, and procedures.

In this opening section, I will provide an overview of the topic at hand.

LVM, also known as Logical Volume Management, is a technology for managing storage devices that allows users to combine and organize the physical layout of storage components for easier administration. By using the device mapper framework in the Linux kernel, the latest version, LVM2, enables the grouping of existing storage devices and allocation of logical units from the combined space as required.

One option for paraphrasing the given text natively could be:

LVM provides several key benefits such as enhanced abstraction, versatility, and command. Naming logical volumes with meaningful titles, such as “databases” or “root-backup”, is made possible. Furthermore, volumes can be easily resized as needed, seamlessly moved between physical devices within the pool while the system is operational, or even exported. Additionally, LVM incorporates advanced functionalities like snapshotting, striping, and mirroring.

You will gain an understanding of LVM and get hands-on experience with essential commands to efficiently start using it on a physical server in this tutorial.

The structure and technical terms of LVM.

Before delving into the LVM administrative commands, it is crucial to grasp the fundamental concepts of how LVM organizes storage devices and the terminology it uses.

Structures for Managing Storage in LVM

LVM employs the technique of building layers on physical storage devices, wherein it utilizes various abstractions. These abstractions are arranged in a hierarchy, with the most basic ones at the foundation.

  • Physical Volumes: The LVM utility prefix for physical volumes is pv…. This physicallyl blocks devices or other disk-like devices (for example, other devices created by device mapper, like RAID arrays) and 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: The LVM utility prefix for volume groups is vg….
    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.
  • Logical Volumes: The LVM utility prefix for logical volumes is lv…, generic LVM utilities might begin with lvm…. 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.

LVM serves the purpose of merging physical volumes into volume groups, allowing the consolidation of storage space on a system. Administrators can then divide the volume group into any desired logical volumes, which function as adaptable partitions.

Comprehending the Scope

All partitions within a group of partitions are divided into small, consistent-sized portions referred to as extents, which are set by the volume group. Every partition within the group follows the same extent size.

Physical extents are referred to as extents on a physical volume, and logical extents are the extents on a logical volume. LVM establishes a mapping between logical and physical extents, which defines a logical volume. Consequently, the extent size denotes the minimum space that LVM can allocate.

LVM’s flexibility and power are largely attributed to extents. LVM combines logical extents to create a single device, and these logical extents are not required to correspond to continuous physical extents. LVM has the ability to copy and rearrange the physical extents of a logical volume without causing any disruption to users. Additionally, the size of logical volumes can be increased or decreased by adding or removing extents.

Typical scenarios where something is frequently used

Once you have become acquainted with the terminology and structures employed by LVM, you can delve into the various ways it is commonly utilized. Commencing with a step-by-step process, you will employ two physical disks in order to create four logical volumes.

Labeling the physical devices as physical volumes.

Start by conducting a scan of the system to identify block devices that LVM can access and control. This can be accomplished by executing the following command:

  1. sudo lvmdiskscan

 

The result will display a list of block devices that LVM can interact with.

Output

/dev/ram0 [ 64.00 MiB] /dev/sda [ 200.00 GiB] /dev/ram1 [ 64.00 MiB] . . . /dev/ram15 [ 64.00 MiB] /dev/sdb [ 100.00 GiB] 2 disks 17 partitions 0 LVM physical volume whole disks 0 LVM physical volumes

In this particular case, it is important to observe that there are presently two disks and a total of 17 partitions. These partitions primarily consist of /dev/ram* partitions, deployed in the system as RAM disks to optimize performance. The disks in this instance are /dev/sda, encompassing 200G of storage, and /dev/sdb, having a capacity of 100G.

Warning

Please ensure to verify that the devices you plan to use with LVM do not contain any crucial data. Utilizing these devices with LVM will replace the existing content. It is advisable to create backups of any significant data on your server before proceeding.

After identifying the specific physical devices you intend to utilize, designate them as physical volumes within LVM by employing the pvcreate command.

  1. sudo pvcreate /dev/sda /dev/sdb

 

Output

Physical volume “/dev/sda” successfully created Physical volume “/dev/sdb” successfully created

This action involves creating an LVM header on the devices, indicating their readiness for addition to a volume group.

To confirm the registration of physical volumes in LVM, execute the command “pvs” and ensure their presence.

  1. sudo pvs

 

Output

PV VG Fmt Attr PSize PFree /dev/sda lvm2 — 200.00g 200.00g /dev/sdb lvm2 — 100.00g 100.00g

Please observe that both of the devices can be found in the PV column, which represents physical volume.

Including the Physical Disks in a Volume Group

After you have successfully generated physical volumes from your devices, you are able to form a volume group. It is recommended to have only one volume group per system to achieve optimal flexibility in allocation. The given instance of a volume group is denoted as LVMVolGroup, but feel free to choose any name for your own volume group.

To establish the volume group and incorporate both of your physical volumes, execute the following command:

  1. sudo vgcreate LVMVolGroup /dev/sda /dev/sdb

 

Output

Volume group “LVMVolGroup” successfully created

Upon reviewing the pvs output once more, you will observe that your physical volumes are now linked to the new volume group.

  1. sudo pvs

 

Output

PV VG Fmt Attr PSize PFree /dev/sda LVMVolGroup lvm2 a– 200.00g 200.00g /dev/sdb LVMVolGroup lvm2 a– 100.00g 100.00g

Provide a brief overview of the volume group by using the command “vgs.”

  1. sudo vgs

 

Output

VG #PV #LV #SN Attr VSize VFree LVMVolGroup 2 0 0 wz–n- 299.99g 299.99g

At present, you have two physical volumes in your volume group, with no logical volumes, and the total capacity is the sum of the underlying devices.

Generating Logical Volumes from the Pool of Volume Groups.

Once a volume group is accessible, you can utilize it as a pool to assign logical volumes. In contrast to traditional partitioning, LVM takes care of mapping and managing the volume layout, so you don’t have to worry about it. All you have to provide is the volume size and a name.

You will divide your volume group into four distinct logical volumes in the upcoming example.

  • 10G “projects” volume
  • 5G “www” volume for web content
  • 20G “db” volume for a database
  • “workspace” volume that will fill the remaining space

In order to generate logical volumes, employ the lvcreate command. It is mandatory to provide the volume group to extract from and the logical volume can be designated using the -n option. To specify the size explicitly, utilize the -L option. Conversely, if you prefer to specify the size based on the number of extents, employ the -l option.

Use the -L option to establish the initial three logical volumes.

  1. sudo lvcreate -L 10G -n projects LVMVolGroup
  2. sudo lvcreate -L 5G -n www LVMVolGroup
  3. sudo lvcreate -L 20G -n db LVMVolGroup

 

Output

Logical volume “projects” created. Logical volume “www” created. Logical volume “db” created.

By opting for a custom output from the vgs command, you have the ability to observe the logical volumes and their correlation with the volume group.

  1. sudo vgs -o +lv_size,lv_name

 

Output

VG #PV #LV #SN Attr VSize VFree LSize LV LVMVolGroup 2 3 0 wz–n- 299.99g 264.99g 10.00g projects LVMVolGroup 2 3 0 wz–n- 299.99g 264.99g 5.00g www LVMVolGroup 2 3 0 wz–n- 299.99g 264.99g 20.00g db

In this specific case, you summed up the final two columns of the output, revealing the amount of allocated space for your logical volumes.

You now have the option to allocate the remaining space in the volume group to the “workspace” volume by using the -l flag, which functions in extents. Another way to specify your intentions more clearly is by providing a percentage and a unit. In this particular scenario, you can allocate the available free space by passing in 100%FREE.

  1. sudo lvcreate -l 100%FREE -n workspace LVMVolGroup

 

Output

Logical volume “workspace” created.

When you use the custom vgs command to inspect the volume group information, you will observe that all of the available space has been utilized.

  1. sudo vgs -o +lv_size,lv_name

 

Output

VG #PV #LV #SN Attr VSize VFree LSize LV LVMVolGroup 2 4 0 wz–n- 299.99g 0 10.00g projects LVMVolGroup 2 4 0 wz–n- 299.99g 0 5.00g www LVMVolGroup 2 4 0 wz–n- 299.99g 0 20.00g db LVMVolGroup 2 4 0 wz–n- 299.99g 0 264.99g workspace

The workspace volume has been successfully established and the entire LVMVolGroup volume group has been assigned.

The process of configuring and preparing the logical volumes for use.

Once you have logical volumes, you can utilize them as regular block devices.

You can find the logical devices in the /dev directory, just like other storage devices. These devices can be accessed in two different locations.

  • /dev/volume_group_name/logical_volume_name
  • /dev/mapper/volume_group_name-logical_volume_name

To format your four logical volumes using the Ext4 filesystem, execute these commands:

  1. sudo mkfs.ext4 /dev/LVMVolGroup/projects
  2. sudo mkfs.ext4 /dev/LVMVolGroup/www
  3. sudo mkfs.ext4 /dev/LVMVolGroup/db
  4. sudo mkfs.ext4 /dev/LVMVolGroup/workspace

 

You also have the option to execute the following:

  1. sudo mkfs.ext4 /dev/mapper/LVMVolGroupprojects
  2. sudo mkfs.ext4 /dev/mapper/LVMVolGroupwww
  3. sudo mkfs.ext4 /dev/mapper/LVMVolGroupdb
  4. sudo mkfs.ext4 /dev/mapper/LVMVolGroupworkspace

 

Once you have formatted the device, proceed to establish the designated mount points.

  1. sudo mkdir -p /mnt/{projects,www,db,workspace}

 

Next, proceed to attach the logical volumes to their respective destinations.

  1. sudo mount /dev/LVMVolGroup/projects /mnt/projects
  2. sudo mount /dev/LVMVolGroup/www /mnt/www
  3. sudo mount /dev/LVMVolGroup/db /mnt/db
  4. sudo mount /dev/LVMVolGroup/workspace /mnt/workspace

 

To ensure the mounts stay permanent, utilize your favored text editor to include them in the /etc/fstab file. The given illustration employs nano as an instance.

  1. sudo nano /etc/fstab

 

The “/etc/fstab” file needs to be paraphrased natively. Here’s one option:

“The file located at “/etc/fstab” requires paraphrasing.”

. . .

/dev/LVMVolGroup/projects /mnt/projects ext4 defaults,nofail 0 0
/dev/LVMVolGroup/www /mnt/www ext4 defaults,nofail 0 0
/dev/LVMVolGroup/db /mnt/db ext4 defaults,nofail 0 0
/dev/LVMVolGroup/workspace /mnt/workspace ext4 defaults,nofail 0 0

Once you have finished editing your document, save and close it. In case you are using nano, press CTRL+c, followed by y, and then ENTER.

The LVM logical volumes should be automatically mounted by the operating system during startup.

In summary,

Now you possess knowledge about the different parts that LVM oversees to establish a dynamic storage system, as well as the procedure to initiate storage devices in an LVM configuration.

If you want to gain a deeper understanding of working with LVM, please refer to our manual on utilizing LVM alongside Ubuntu 18.04.

 

More Tutorials

permissions in PostgreSQL(Opens in a new browser tab)

Installation of Arch Linux(Opens in a new browser tab)

Partition in Linux Step-by-Step Guide(Opens in a new browser tab)

Common errors that occur when using Nginx for connections.(Opens in a new browser tab)

Spring Boot CLI(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *