avatarKarthick Dkk

Summary

The provided content is a comprehensive guide for DevOps engineers on mastering LVM (Logical Volume Manager) partitioning on Linux systems, detailing the benefits, setup, and management of LVM for flexible storage solutions.

Abstract

The article "Master LVM Storage: Step-by-Step Guide to LVM Partitioning" serves as an in-depth tutorial for DevOps engineers to master LVM partitioning on Linux. It highlights the advantages of LVM, such as flexible storage management, the ability to resize partitions on the fly, combine multiple drives, take snapshots, and easily expand storage. The guide emphasizes the importance of using GPT (GUID Partition Table) over MBR (Master Boot Record) for LVM partitions, especially for larger disk sizes, more partitions, redundancy, error checking, and UEFI compatibility. It provides step-by-step instructions on converting disks to GPT using gdisk, partitioning the disk for LVM, setting up LVM with physical volumes (PVs), volume groups (VGs), and logical volumes (LVs), and finally, updating the /etc/fstab file to ensure the logical volumes are automatically mounted at boot. The article also includes commands and their outputs for creating, displaying, and managing LVM components, with a reminder to back up data before making changes to storage configurations.

Opinions

  • The author expresses a strong preference for GPT over MBR when using LVM due to its support for larger disk sizes, more partitions, redundancy, error checking, and UEFI compatibility.
  • LVM is portrayed as a superior alternative to traditional partitioning, offering unparalleled flexibility and ease of storage management for Linux systems.
  • The guide is written with the assumption that the reader is looking to optimize their storage solutions for scalability, reliability, and compatibility with modern hardware and systems.
  • The author encourages readers to follow best practices, such as verifying partition tables and backing up data before performing LVM operations.
  • The article suggests that mastering LVM is an essential skill for DevOps engineers, implying that it is a valuable competency in the field.

Master LVM Storage: Step-by-Step Guide to LVM Partitioning

DevOps Engineer’s Guide to Mastering LVM Partitioning on Linux

Hello Mate! Welcome to another one. In this blog, we will Learn LVM Partitioning Like a Pro: Complete Guide for Linux Users.

LVM, or Logical Volume Manager, is a tool in Linux that helps manage your computer’s storage more easily and flexibly. Think of it as a way to handle your hard drives and partitions without the usual headaches of traditional partitioning.

Why LVM is Needed:

Flexible Storage Management:

Normally, once you set up a partition (a section of your hard drive), it’s hard to change its size. With LVM, you can change the size of these partitions whenever you need, without a big fuss. Imagine being able to add space to your computer’s “storage bins” whenever they start to fill up.

Resize Partitions on the Fly:

With LVM, you can make your storage bigger or smaller while still using your computer. No need to shut down or take a break. It’s like expanding a suitcase while you’re still packing!

Combining Multiple Drives:

LVM lets you take multiple hard drives or partitions and combine them into one big storage area. Instead of managing each disk separately, you can treat all your drives as a single, large storage space.

Snapshots:

LVM allows you to take a “snapshot” of your data at a specific point in time. This is handy for backups or testing things out without messing up your original data.

Easy Expansion:

When your storage gets full and you add a new hard drive, LVM lets you add it to your existing setup without needing to reinstall or reconfigure anything. You can just add the new space and keep working.

How It Works in Simple Terms:

  • Physical Volumes (PVs): These are your actual hard drives or parts of a hard drive.
  • Volume Groups (VGs): Think of these as a big pool that combines all your drives together into one larger space.
  • Logical Volumes (LVs): These are the partitions or sections of that pool where you store your files, photos, or programs.

In short, LVM makes managing storage on your Linux computer much simpler and more flexible. It’s like having a magic suitcase that can expand, change shape, and let you add more space whenever you need it!

Let’s Explore ‘From Zero to LVM Hero: Step-by-Step Linux Partitioning Guide’

Pre-Requisite:

For LVM (Logical Volume Manager) partitions, GPT (GUID Partition Table) is generally considered the better option compared to MBR (Master Boot Record), especially for the following reasons:

  1. Support for Larger Disk Sizes: GPT supports disk sizes larger than 2 TB, while MBR is limited to 2 TB. If you’re working with large storage devices or plan to scale your storage, GPT is essential.
  2. More Partitions: GPT supports up to 128 partitions by default, whereas MBR only supports up to 4 primary partitions (though you can create extended partitions to get more). LVM allows flexible partitioning, and GPT makes better use of this flexibility.
  3. Redundancy and Error Checking: GPT stores multiple copies of partition data across the disk for redundancy and includes a CRC32 checksum for error detection, providing better protection against partition table corruption compared to MBR.
  4. UEFI Compatibility: GPT is required if you are using UEFI (Unified Extensible Firmware Interface), which is becoming the modern standard for booting systems. MBR is typically used with older BIOS systems.

Since LVM is often used in enterprise environments or for managing complex storage setups, GPT is usually the best fit for its scalability, reliability, and compatibility with modern hardware and systems

Check the disk type MBR or GPT.

parted -l

Model: ATA Disk (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt         #  DISK Type Here

(OR)
sudo fdisk -l

Disk /dev/sda: 500 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Disk
Partition Table: gpt         # DISK Type Here

If the disk type is not in GPT, Then convert to GPT.

Step 1: Convert /dev/sda to GPT using gdisk

Install gdisk if it’s not installed yet:

sudo apt install gdisk  # On Ubuntu/Debian 
sudo yum install gdisk  # On CentOS/RHEL 
sudo dnf install gdisk  # On Fedora

Convert the disk to GPT: Run gdisk to convert /dev/sda to GPT:

sudo gdisk /dev/sda

Inside gdisk, you will see the options:

  • Press o to create a new GPT partition table.
  • Press w to write changes and confirm with Y.

Verify the GPT table: After conversion, check if the disk is now GPT:

sudo fdisk -l /dev/sda

The output should indicate a GPT partition table.

Note: Skip the above steps, if you are already using the GPT partition Table

Step 2: Partition the Disk for LVM

Now that /dev/sda has been converted to GPT, you can create a partition and set it up for LVM.

Create a partition using fdisk:

sudo fdisk /dev/sda

In fdisk:

  • Press n to create a new partition.
  • Choose primary and use the default values to make the partition take up the entire disk.
  • Press t and select partition type as LVM (8e — Linux LVM).
  • Press w to write the changes and exit.

Warning: The above action will format all the data and permently.

Check the new partition: Verify the new partition (/dev/sdb1):

sudo fdisk -l /dev/sda

Same we can do for /dev/sdb, /dev/sdc.

Step 3: Set up LVM on the Partition

Create a Physical Volume (PV): Initialize LVM:

sudo pvcreate /dev/sda1

sudo pvcreate /dev/sdb1

sudo pvcreate /dev/sdc1

pvdisplay

The pvdisplay command is used in Linux to display detailed information about physical volumes (PVs) in LVM (Logical Volume Manager). Below are some examples of how to use it, along with explanations of the output:

sudo pvdisplay

This will show detailed information about all the physical volumes on your system. Here’s an example of what the output might look like:

--- Physical volume ---
  PV Name               /dev/sda1
  VG Name               
  PV Size               <931.51 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              238466
  Free PE               0
  Allocated PE          238466
  PV UUID               y00q12-Qdqf-Ck2z-yX9m-j74G-JBME-BzRGWf

Output Breakdown:

  • PV Name: The name of the physical volume, typically a disk or partition (like /dev/sdb1).
  • VG Name: The volume group (VG) this PV is a part of (in this case, my_volume_group).
  • PV Size: The total size of the physical volume. Here, it’s 931.51 GiB.
  • Allocatable: Whether this PV can be used to allocate new extents. In this case, it says “yes (but full)” because it’s full.
  • PE Size: The size of each physical extent (PE). This shows that each PE is 4 MiB.
  • Total PE: The total number of physical extents in this PV (238466 in this example).
  • Free PE: How many physical extents are still available for allocation. In this case, it’s 0, meaning the PV is full.
  • Allocated PE: The number of physical extents that have already been allocated.
  • PV UUID: A unique identifier for the physical volume.

Create a Volume Group (VG): Create a new volume group :

# Create Initial Volume group from PV
sudo vgcreate vg_data /dev/sda1 /dev/sdb1 /dev/sdc1

# Extend Volume group after creating volume group ( Add more PV)
sudo vgextend vg_data /dev/sda2 /dev/sdc2

vgdisplay

The vgdisplay command provides critical information about a volume group in LVM, including its size, status, and configuration details. Understanding these parameters is vital for effective management of volume groups in Linux.

# Display volume group details
vgdisplay <vg_name>

vgdisplay vg_data
--- Volume group ---
  VG Name               vg-data        # Vg_name
  System ID
  Format                lvm2           # lvm format
  Metadata Areas        3
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1               # Current lvgroups
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <7.00 TiB       # Total VG Size
  PE Size               4.00 MiB  
  Total PE              1835005
  Alloc PE / Size       1835005 / <7.00 TiB
  Free PE / Size       0 / 0            # Free Size
  VG UUID               p9j6m7-x0xn-fdet5-SlGQ-42sq-cyXr-mAvIRT

Create a Logical Volume (LV): Create a logical volume in the vg_data volume group and use 100% space on it.

sudo lvcreate -l 100%FREE -n lv_storage vg_data

lvdisplay

Display the Logical volume details

lvdisplay lv_storage
--- Logical volume ---
  LV Path                lv_storage
  LV Name                newspace
  VG Name                vg_data
  LV UUID                rVqdFv-y0fY-Prs4-vtS1-KZE4-Lve0-B7paRh
  LV Write Access        read/write
  LV Creation host, time  Server.example.COM, 2020-10-13 18:46:57 +0530
  LV Status              available
  open                 0
  LV Size                <7.00 TiB
  Current LE             1835005
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

lvextend

Extend the Logical Volume: Use the following command to extend a Logical Volume. In this example, lv_storage is the name of the Logical Volume, and we are extending it to use all available free space in the VG:

lvextend -l +100%FREE /dev/vg_data/lv_storage

Format the Logical Volume: Format the logical volume with a file system (e.g., ext4):

sudo mkfs.ext4 /dev/vg_data/lv_storage

Warning: This mkfs command will format all the data

Mount the Logical Volume: Mount the new logical volume to make it accessible:

sudo mkdir /mnt/lvm-data

sudo mount /dev/vg_data/lv_storage /mnt/lvm-data

Step 4: Update /etc/fstab

To ensure the logical volume is automatically mounted at boot, update the /etc/fstab file:

Open /etc/fstab in a text editor:

sudo nano /etc/fstab

Add the following line to the end of the file:

/dev/vg_data/lv_storage /mnt/lvm-data ext4  defaults  0  2

Verify the Mount:

Check the mounted logical volume:

df -h

(OR)

lsblk

You should see the Logical Volume and filesystem reflect the new size.

can I create another partition and add it to the same volume group to extend the new PV?

Yes, you can add new partitions (Physical Volumes) to an existing Volume Group, and then extend your Logical Volumes to make use of the additional space. This flexibility is one of the key advantages of using LVM, allowing you to dynamically manage storage without downtime.

Reminder: Always ensure you have backups of important data before performing partitioning or resizing operations.

✅✅feel free to connect with us.

Follow for more: ✌️

Publication: DevSecOp-Community

Follow my Medium Account (To get valuable information)

For Email Notify: Subscribe on Email Newsletter

For more updates: subscribe to this medium account.

LinkedIn: LinkedIn/karthick-dkk

Lvm
Linux
DevOps
Storage
Ubuntu
Recommended from ReadMedium