LVM (Logical Volume Manager): Dynamic Disk Management
Logical Volume Manager (LVM) provides a flexible way to manage disk space in Linux. Instead of working directly with physical disks and partitions, LVM creates a layer of abstraction that allows for dynamic resizing, creation, and deletion of logical volumes (think of them as flexible partitions). This is particularly useful for servers where storage requirements might change over time.
LVM’s core components are:
- Physical Volumes (PVs): Raw disk partitions or whole disks.
- Volume Groups (VGs): Collections of one or more PVs, acting as a pool of storage.
- Logical Volumes (LVs): “Virtual partitions” carved out of a VG, which can be formatted with a filesystem and mounted.
1. Identify Disk Layout (lsblk)
The lsblk command shows information about all block devices. In an LVM setup, it helps visualize the relationship between physical disks, partitions, and logical volumes.
lsblkExample Output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 63.7M 1 loop /snap/core20/2496
# ... (other loop devices) ...
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 2G 0 part /boot
└─sda3 8:3 0 48G 0 part # This partition is used as a Physical Volume for LVM
└─ubuntu--vg-ubuntu--lv 253:0 0 48G 0 lvm / # This is a Logical Volume mounted at /
sr0 11:0 1 2G 0 rom
In this example, sda3 is part of an LVM setup, and ubuntu--vg-ubuntu--lv is a logical volume.
2. Identify Volume Groups (vgs)
The vgs command displays information about Volume Groups. It shows the total size of the VG and how much free space is available within it.
vgsExample Output:
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <48.00g 0
VG: Name of the Volume Group (e.g.,ubuntu-vg).VSize: Total size of the Volume Group.VFree: Free space available within the Volume Group. In this case,0means the VG is fully utilized by its LVs.
3. Identify Logical Volumes (lvs)
The lvs command displays information about Logical Volumes.
lvsExample Output:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- <48.00g
LV: Name of the Logical Volume (e.g.,ubuntu-lv).VG: The Volume Group to which the LV belongs.LSize: The size of the Logical Volume.
4. Extend the Logical Volume
If you have free space in your Volume Group (or if you’ve added new Physical Volumes to it), you can extend an existing Logical Volume.
sudo lvextend -l +100%FREE /dev/<vg_name>/<lv_name>
# Example:
sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lvsudo lvextend: Command to extend a logical volume.-l +100%FREE: Extends the logical volume to use all available free space in its Volume Group. You can also specify an exact size (e.g.,-L +10Gto add 10GB)./dev/<vg_name>/<lv_name>: The full path to your logical volume.
WARNING: Always ensure you have a backup of your data before performing any resizing operations on logical volumes or filesystems.
5. Resize the Filesystem
After extending the Logical Volume, you must also extend the filesystem residing on that LV so that it can utilize the newly allocated space. The command depends on the filesystem type (e.g., resize2fs for ext2/3/4, xfs_growfs for XFS).
sudo resize2fs /dev/<vg_name>/<lv_name>
# Example for an ext4 filesystem:
sudo resize2fs /dev/ubuntu-vg/ubuntu-lvsudo resize2fs: Resizes an ext2/ext3/ext4 filesystem./dev/<vg_name>/<lv_name>: The full path to the logical volume containing the filesystem.
After these steps, your filesystem will reflect the increased size of the logical volume.