Last modified: August 04, 2024

This article is written in: 🇺🇸

Disk Partitions and Volumes

Think of data storage devices, such as DVDs, USB flash drives, and hard drives (HDDs or SSDs), as an entire cake. This cake can be cut into smaller slices or 'partitions'. These partitions are essentially divisions or sections within the storage device, helping to categorize or organize the storage for diverse uses. For instance, you can segregate different file types or even run multiple operating systems in separate partitions. The structure and arrangement of these partitions resemble a table, which you'd use to keep track of everything.

Just as a cake is divided into partitions, a bookshelf is divided into sections known as 'volumes'. A volume is analogous to a book genre section on a bookshelf, each having its own file system or a method of organizing files. While a partition is limited to a single disk (although that disk can have multiple partitions), a volume has the capability to span across several disks. This is similar to joining several bookshelves to create more space for your books. For instance, you might have a hard drive partitioned into three parts: one for your operating system, one for your personal documents, and a third for your multimedia files. However, if you need extra space for larger files, such as videos, you can create a volume that spans multiple devices to distribute the storage.

Logical Volume Manager (LVM)

The Logical Volume Manager (LVM) is a robust tool for managing disk storage on Linux-based systems. It allows the creation of 'logical volumes' that can be readily resized, relocated, and expanded. This introduces a level of flexibility as it facilitates the addition of storage space to your system without the need for additional physical disks. Furthermore, LVM allows for the creation of 'volume snapshots', which are extremely valuable for creating backups of your data.

Despite its advantages, using LVM can make the system setup process more complex, as it requires additional support from the kernel. In certain circumstances, repair images might not be compatible with LVM-based systems. This means it is essential to consider the potential complexity and support issues before deciding to use LVM in a Linux environment.

Understanding the Components of the Logical Volume Manager (LVM)

LVM comprises several fundamental components, which include:

Let's understand these components with an analogy. Think of a physical volume as a book, a volume group as a bookshelf containing several books, and a logical volume as a chapter in a book.

|-----------------|     |-----------------|     |-----------------|
|  Physical       |     |  Physical       |     |  Physical       |
|  Volume (PV)    |     |  Volume (PV)    |     |  Volume (PV)    |
|  /dev/sdb       |     |  /dev/sdc       |     |  /dev/sdd       |
|  Size: 50G      |     |  Size: 100G     |     |  Size: 150G     |
|-----------------|     |-----------------|     |-----------------|
        |                      |                       |
        |                      |                       |
        ------------------------                       |
                           |                           |
                           ----------------------------
                                       |
                                       |
|------------------------------------------------------------------|
|                         Volume Group (VG)                        |
|                              TEST                                |
|                           Total Size: 300G                       |
|                                                                  |
|  |------------------|     |------------------|                   |
|  | Logical Volume   |     | Logical Volume   |                   |
|  | (LV)             |     | (LV)             |     Unallocated   |
|  | vol_name_1       |     | vol_name_2       |     Space         |
|  | Size: 180G       |     | Size: 90G        |     Size: 30G     |
|  |------------------|     |------------------|                   |
|                                                                  |
|------------------------------------------------------------------|

This diagram illustrates that:

Creating and Managing LVM Volumes

Creating and managing LVM volumes involve a series of steps that utilize different commands. Here is a brief walkthrough:

I. Creating Physical Volumes

This is the initial step, where you use the pvcreate command to set up physical volumes on your disks. This command turns the entire disk into a single LVM physical volume. Here's an example using three devices (/dev/sdb, /dev/sdc, and /dev/sdd):

pvcreate /dev/sdb
pvcreate /dev/sdc
pvcreate /dev/sdd

II. Defining Volume Groups

Next, you create a volume group using the vgcreate command and add your physical volumes to it. The volume group allows you to manage multiple physical volumes as one. Here's an example of creating a volume group named TEST and extending it with additional physical volumes:

vgcreate TEST /dev/sdb
vgextend TEST /dev/sdc /dev/sdd

III. Creating Logical Volumes

After your volume group is set, you can create logical volumes within this group using the lvcreate command. These logical volumes will be your actual storage areas. In this example, two logical volumes (vol_name_1 and vol_name_2) of sizes 20GB and 12GB respectively, are created within the TEST volume group:

lvcreate -L 20G -n vol_name_1 TEST
lvcreate -L 12G -n vol_name_2 TEST

IV. Creating a File System on the Logical Volume

Before you can start storing data, you need to create a file system on the logical volume. You can do this using the mkfs command. In this example, an ext4 file system is created on vol_name_1:

mkfs -t ext4 /dev/TEST/vol_name_1

V. Mounting the Logical Volume

Finally, you mount the logical volume to a designated mounting point using the mount command. This allows you to start storing and accessing data. In this example, vol_name_1 is mounted at /mounting_point:

mount -t ext4 /dev/TEST/vol_name_1 /mounting_point

Extending a Logical Volume

If you have available space in a volume group, you can extend a logical volume to use that space. This is done using the lvextend command:

lvextend -L +10G /dev/TEST/vol_name_1

This command extends vol_name_1 by an additional 10GB. The file system on this volume needs to be resized to recognize the additional space:

resize2fs /dev/TEST/vol_name_1

Reducing a Logical Volume

To reduce the size of a logical volume, ensure that the volume has enough free space to avoid losing data. First, unmount the logical volume and check it for errors:

umount /dev/TEST/vol_name_1
e2fsck -f /dev/TEST/vol_name_1

Then, reduce the file system size before reducing the logical volume:

resize2fs /dev/TEST/vol_name_1 15G
lvreduce -L 15G /dev/TEST/vol_name_1

Deleting a Logical Volume

You might need to delete a logical volume to free up space in your volume group. Unmount the logical volume and then use the lvremove command:

umount /dev/TEST/vol_name_1
lvremove /dev/TEST/vol_name_1

Backup and Recovery of LVM Configurations

Taking regular snapshots of logical volumes can be an effective backup strategy. A snapshot is a copy of the logical volume at a certain point in time. It's created using the lvcreate command:

lvcreate -L 1G -s -n snap_vol_name_1 /dev/TEST/vol_name_1

This command creates a 1GB snapshot named snap_vol_name_1 of vol_name_1. If a restore is needed, you can revert the logical volume to the snapshot state using the lvconvert command:

lvconvert --merge /dev/TEST/snap_vol_name_1

Partition Types Comparison

Feature Primary Partition Extended Partition Logical Partition Logical Volume (LVM)
Definition A partition that can contain a file system or be used for booting an OS. A container for logical partitions, not used for direct data storage. Partitions created within an extended partition. A virtual partition created using Logical Volume Manager (LVM) for flexible disk management.
Maximum Count Up to 4 primary partitions per disk. 1 extended partition per disk (occupies one primary slot). The number of logical partitions is limited by the space in the extended partition and system limitations (up to 128 logical partitions). Limited by system resources and configuration, practically very high (can easily handle hundreds of volumes).
Bootability Can be marked as active to boot an OS. Cannot be used for booting directly. Cannot be used for booting. Can be bootable with a dedicated boot partition or specific configurations.
Storage Capability Directly stores data or OS. Does not store data directly; only serves as a container for logical partitions. Directly stores data, can have file systems. Can aggregate multiple physical volumes into a single logical volume, allows complex configurations.
Resizing Limited; generally requires repartitioning. Cannot be resized independently; resizing affects contained logical partitions. Can be resized by adjusting the size of the extended partition. Easily resizable; logical volumes can be expanded or reduced dynamically.
File System Support Supports all file systems. Not applicable as it does not hold data. Supports all file systems within the extended partition. Supports multiple file systems, can have different file systems on different volumes.
Management Tools Managed by standard disk utilities (e.g., fdisk, Disk Management in Windows). Managed by standard disk utilities alongside primary partitions. Managed within the extended partition by standard disk utilities. Managed using LVM tools (e.g., lvcreate, lvextend, lvreduce).
Use Cases Typically used for OS installation, critical system data. Used when more than 4 partitions are needed on a disk. Used for separating data, organizing storage without using additional primary partitions. Used for flexible storage management, creating dynamic and scalable storage environments, advanced features like snapshots and mirroring.

Challenges

  1. How would you reduce the size of an LVM partition? Remember, you can only reduce a logical volume's size if there's enough free space within it. Also, ensure that you back up any crucial data on the partition before resizing. Describe the command(s) you'd use and the process you'd follow.
  2. How would you extend an LVM partition to utilize more free space within the volume group? What command(s) would you use for this purpose?
  3. Explain the process of taking a snapshot of an LVM logical volume. Why might this be useful? How can you create and restore from a snapshot?
  4. How would you remove a logical volume from an LVM setup? Detail the necessary steps to ensure data safety before, during, and after the process.
  5. How would you add a new physical disk to an existing volume group in an LVM setup? Describe the process and the command(s) you would need to use.
  6. Explain the concept of striping in LVM. What is its purpose, and how can you set up a striped logical volume?
  7. How can you monitor the status of your LVM setup, including checking for available free space, the status of logical volumes, and the status of volume groups? What command(s) would you use for this purpose?
  8. In a RAID (Redundant Array of Independent Disks) setup, what are the differences between using LVM and not using LVM? What benefits does LVM provide in a RAID environment?

Table of Contents

  1. Disk Partitions and Volumes
  2. Logical Volume Manager (LVM)
  3. Understanding the Components of the Logical Volume Manager (LVM)
  4. Creating and Managing LVM Volumes
  5. Extending a Logical Volume
  6. Reducing a Logical Volume
  7. Deleting a Logical Volume
  8. Backup and Recovery of LVM Configurations
    1. Partition Types Comparison
    2. Challenges