Linux LVM Partitioning - Full Guide

What is LVM?

Logical Volume Manager (LVM) is a storage management solution in Linux that allows for flexible disk management. It enables resizing, creating, and managing disk partitions without unmounting or rebooting the system.

Steps to Configure LVM

  1. Install LVM tools: Ensure the lvm2 package is installed on your system.
  2. Create Physical Volumes (PVs): Initialize the disks or partitions for LVM using the pvcreate command.
  3. Create a Volume Group (VG): Combine physical volumes into a volume group using the vgcreate command.
  4. Create Logical Volumes (LVs): Allocate space from the volume group to create logical volumes using the lvcreate command.
  5. Format and Mount: Format the logical volume with a filesystem and mount it for use.

Difference between xfs and ext4

xfs and ext4 are two popular filesystems in Linux. Here are their key differences:

Common LVM Commands

Extent or Chunks

In LVM, an extent is the basic unit of storage allocation. When you create a logical volume, it is made up of one or more extents. Each extent is typically 4MB in size, but this can be configured during the creation of the volume group.

Extents allow LVM to manage storage more efficiently by allocating space in fixed-size chunks. This helps in reducing fragmentation and improving performance when resizing logical volumes.

PE calcuation

Physical Extents (PE) are the smallest unit of space that LVM uses to manage storage. When you create a volume group, it divides the available space into physical extents. The size of each physical extent is determined when the volume group is created and is typically 4MB by default.

To calculate the number of physical extents in a volume group, you can use the following formula:

Number of PEs = Total Size of Volume Group / Size of Each Physical Extent

For example, if you have a volume group with a total size of 100GB and the size of each physical extent is 4MB, the calculation would be:

Number of 125 PE * 4 LE = 500 MB

This means that the volume group can be divided into 25,600 physical extents, each of which can be allocated to logical volumes as needed.

Example

Step-by-Step Commands

# Install LVM tools
sudo apt install lvm2

# Create physical volumes
sudo pvcreate /dev/sdb /dev/sdc

# Create a volume group
sudo vgcreate my_vg /dev/sdb /dev/sdc

# Create a logical volume
sudo lvcreate -L 10G -n my_lv my_vg

# Format the logical volume
sudo mkfs.ext4 /dev/my_vg/my_lv

# Create a mount point and mount the volume
sudo mkdir /mnt/my_data
sudo mount /dev/my_vg/my_lv /mnt/my_data

# Verify the setup
df -h /mnt/my_data
            

To provide a inode number range

When creating a filesystem, you can specify the inode number range using the -i option with the mkfs command. For example:

sudo mkfs.ext4 -i 4096 /dev/my_vg/my_lv
            

This command sets the inode size to 4096 bytes, which determines how many inodes are created for the filesystem. The number of inodes affects how many files can be stored on the filesystem.

Extend a partition size step

To extend a partition size in LVM, you can follow these steps:

  1. Check the current size of the logical volume: Use the lvdisplay command to see the current size of the logical volume.
  2. sudo lvdisplay /dev/my_vg/my_lv
  3. Check available space in the volume group: Use the vgdisplay command to see how much free space is available in the volume group.
  4. sudo vgdisplay my_vg
  5. Resize the physical volume: If you have additional space on the physical disk, you can resize the partition using a tool like fdisk or parted. After resizing the partition, run partprobe to inform the OS of the changes.
  6.  partprobe /dev/sdb
  7. Resize the volume group: If you have resized the physical volume, you may need to resize the volume group using the vgextend command if you added a new physical volume.
  8. sudo vgextend my_vg /dev/sdb1
  9. Resize the logical volume: Use the lvextend command to increase the size of the logical volume. For example, to extend it by 5GB:
  10. sudo lvextend -L +5G /dev/my_vg/my_lv
                    
  11. Resize the filesystem: After extending the logical volume, you need to resize the filesystem to utilize the new space. For ext4 filesystems, use:
  12. sudo resize2fs /dev/my_vg/my_lv
                    

    For xfs filesystems, use:

    sudo xfs_growfs /dev/my_vg/my_lv
                    
  13. Verify the changes: Use the df -h command to check the new size of the filesystem.

These steps will allow you to extend a logical volume and resize the filesystem accordingly.

Extend a LV partition(LV partition formate in ext4)

To extend a logical volume, you can use the lvextend command. For example, to extend the logical volume by 5GB:

sudo lvextend -L +5G /dev/my_vg/my_lv
# Resize the filesystem to use the new space
sudo resize2fs /dev/my_vg/my_lv
        

This command increases the size of the logical volume by 5GB and then resizes the filesystem to utilize the additional space.

Extend a LV partition(LV partition formate in xfs)

To extend a logical volume formatted with xfs, you can use the lvextend command followed by the xfs_growfs command. For example, to extend the logical volume by 5GB:

sudo lvextend -L +5G /dev/my_vg/my_lv
# Resize the filesystem to use the new space
sudo xfs_growfs /dev/my_vg/my_lv
            

This command increases the size of the logical volume by 5GB and then resizes the xfs filesystem to utilize the additional space.

Extend a LV partition with resizing in a single command

To extend a logical volume and resize the filesystem in a single command, you can use the following syntax:

sudo lvextend -L +5G -r /dev/my_vg/my_lv
            

The -r option automatically resizes the filesystem after extending the logical volume, making it a convenient way to manage space.

Advantages of LVM

Migrate two VG in Single VG

To migrate two volume groups (VGs) into a single VG, you can follow these steps:

  1. Check the current VGs: Use the vgs command to list the existing volume groups.
  2. sudo vgs
  3. unmounting: All Active LV unmount related to migrate vg part
  4. sudo umount /dev/old_vg/old_lv
  5. Deactivate the old VG: Before merging, you need to deactivate the old volume group.
  6. sudo vgchange -an old_vg
  7. Migrate:Now we can migrate Vg2 to Vg1
  8. sudo vgmerge new_vg old_vg
  9. Activate the new VG: After merging, activate the new volume group.
  10. sudo vgchange -ay new_vg
  11. Verify the migration: Use the vgs command to check that the new volume group contains the logical volumes from both original VGs.
  12. sudo vgs
  13. Mount the logical volumes: If needed, remount the logical volumes from the new volume group.
  14. sudo mount /dev/new_vg/new_lv /mnt/new_data

How to set PE size

To set a custom physical extent (PE) size when creating a volume group, you can use the -s option with the vgcreate command. For example, to set the PE size to 8MB:

sudo vgcreate -s 8M my_vg /dev/sdb /dev/sdc
        

This command creates a volume group named my_vg with a physical extent size of 8MB, using the specified physical volumes.

Restore LVM

To restore LVM configuration from a backup, you can follow these steps:

  1. Backup the current LVM configuration: Before restoring, it's a good practice to back up the current LVM configuration.
  2. sudo vgcfgbackup
  3. Restore the LVM configuration list: Use the vgcfgrestore -l command to list all avilable vg backup list
  4. sudo vgcfgrestore -l my_vg
  5. Restore the LVM configuration: Use the vgcfgrestore command to restore the volume group configuration from a backup file.
  6. sudo vgcfgrestore -f /etc/lvm/backup/my_vg
    or
    sudo vgcfgrestore -f /etc/lvm/archive/my_vg123344.vg my_vg
    
  7. Activate the volume group: After restoring, you may need to activate the volume group.
  8. sudo vgchange -ay my_vg
  9. Verify the restoration: Use the vgs command to check that the volume group and its logical volumes are restored correctly.
  10. sudo vgs

These steps will help you restore the LVM configuration from a backup, ensuring that your logical volumes and volume groups are back to their previous state.