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
- Install LVM tools: Ensure the
lvm2package is installed on your system. - Create Physical Volumes (PVs): Initialize the disks or partitions for LVM using the
pvcreatecommand. - Create a Volume Group (VG): Combine physical volumes into a volume group using the
vgcreatecommand. - Create Logical Volumes (LVs): Allocate space from the volume group to create logical volumes using the
lvcreatecommand. - 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:
- Performance: xfs is optimized for high-performance applications and large files, while ext4 is more versatile for general use.
- Scalability: xfs supports larger files and filesystems, making it suitable for enterprise environments.
- Features: xfs supports advanced features like dynamic inode allocation and delayed allocation, while ext4 has journaling and extents for better performance.
- Recovery: xfs has robust recovery tools, while ext4 is easier to repair with fsck.
- Compatibility: ext4 is widely supported across various Linux distributions, while xfs is often used in enterprise-grade systems.
Common LVM Commands
pvcreate- Initialize a physical volume.vgcreate- Create a volume group from physical volumes.lvcreate- Create a logical volume from a volume group.lvextend- Extend the size of a logical volume.lvreduce- Reduce the size of a logical volume (requires unmounting).lvdisplay- Display information about logical volumes.vgdisplay- Display information about volume groups.pvdisplay- Display information about physical volumes.lvremove- Remove a logical volume.vgremove- Remove a volume group.pvremove- Remove a physical volume.lvrename- Rename a logical volume.vgextend- Add a physical volume to an existing volume group.vgreduce- Remove a physical volume from a volume group.lvcreate -s- Create a snapshot of a logical volume.lvchange -ay- Activate a logical volume.lvchange -an- Deactivate a logical volume.lvs- Scan for all logical volumes.vgs- Scan for all volume groups.pvs- Scan for all physical volumes.lvresize- Resize a logical volume (can be used to both extend and reduce).
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:
- Check the current size of the logical volume: Use the
lvdisplaycommand to see the current size of the logical volume. - Check available space in the volume group: Use the
vgdisplaycommand to see how much free space is available in the volume group. - Resize the physical volume: If you have additional space on the physical disk, you can resize the partition using a tool like
fdiskorparted. After resizing the partition, runpartprobeto inform the OS of the changes. - Resize the volume group: If you have resized the physical volume, you may need to resize the volume group using the
vgextendcommand if you added a new physical volume. - Resize the logical volume: Use the
lvextendcommand to increase the size of the logical volume. For example, to extend it by 5GB: - Resize the filesystem: After extending the logical volume, you need to resize the filesystem to utilize the new space. For ext4 filesystems, use:
- Verify the changes: Use the
df -hcommand to check the new size of the filesystem.
sudo lvdisplay /dev/my_vg/my_lv
sudo vgdisplay my_vg
partprobe /dev/sdb
sudo vgextend my_vg /dev/sdb1
sudo lvextend -L +5G /dev/my_vg/my_lv
sudo resize2fs /dev/my_vg/my_lv
For xfs filesystems, use:
sudo xfs_growfs /dev/my_vg/my_lv
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
- Dynamic resizing of partitions.
- Snapshots for backup and recovery.
- Combining multiple physical disks into a single logical volume.
Migrate two VG in Single VG
To migrate two volume groups (VGs) into a single VG, you can follow these steps:
- Check the current VGs: Use the
vgscommand to list the existing volume groups. - unmounting: All Active LV unmount related to migrate vg part
- Deactivate the old VG: Before merging, you need to deactivate the old volume group.
- Migrate:Now we can migrate Vg2 to Vg1
- Activate the new VG: After merging, activate the new volume group.
- Verify the migration: Use the
vgscommand to check that the new volume group contains the logical volumes from both original VGs. - Mount the logical volumes: If needed, remount the logical volumes from the new volume group.
sudo vgs
sudo umount /dev/old_vg/old_lv
sudo vgchange -an old_vg
sudo vgmerge new_vg old_vg
sudo vgchange -ay new_vg
sudo vgs
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:
- Backup the current LVM configuration: Before restoring, it's a good practice to back up the current LVM configuration.
- Restore the LVM configuration list: Use the
vgcfgrestore -lcommand to list all avilable vg backup list - Restore the LVM configuration: Use the
vgcfgrestorecommand to restore the volume group configuration from a backup file. - Activate the volume group: After restoring, you may need to activate the volume group.
- Verify the restoration: Use the
vgscommand to check that the volume group and its logical volumes are restored correctly.
sudo vgcfgbackup
sudo vgcfgrestore -l my_vg
sudo vgcfgrestore -f /etc/lvm/backup/my_vgorsudo vgcfgrestore -f /etc/lvm/archive/my_vg123344.vg my_vg
sudo vgchange -ay my_vg
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.