LVM
LVM, or Logical Volume Management, offers flexible disk management in Linux, allowing easy resizing and expansion of storage without reformatting. This guide covers essential LVM tasks: adding drives, extending volume groups, and resizing logical volumes and filesystems, with practical commands for each step.
How to add drive to LVM
Create a physical volume, volume group, and logical volume on a new drive:
1
2
3
4
# pvcreate /dev/sdb # Initializes /dev/sdb as a physical volume
# vgcreate VGname /dev/sdb # Creates a volume group "VGname" using /dev/sdb
# lvcreate -n LVname -L 5G VGname # Creates a 5GB logical volume "LVname" in "VGname"
# lvcreate -l 100%FREE -n LVname VGname # Uses all available space for "LVname"
How to extend a volume group
Expanding volume group on extended drive:
1
# pvextend /dev/sdx # Extends an existing physical volume on /dev/sdx
Adding new drive to existing volume group
1
# vgextend ubuntu-vg /dev/sdx # Adds /dev/sdx to the existing volume group "ubuntu-vg"
How to extend logical volume
The example assumes that the volume group has free space to expand the logical group. If there is no free space, see the previous example on how to add a new disk to an existing volume group, or expand an existing disk.
1
# lvextend -L +10G /dev/mapper/ubuntu--vg-ubuntu--lv
or use all available space:
1
2
# lvextend -l +100%FREE /dev/vg_ubuntu/lv_ubuntu
Extend the filesystem
The next step is to enlarge the file system to match the expanded logical volume size, examples for different types of file systems:
1
2
# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv # For ext3/ext4 filesystems
# xfs_growfs /mount/point # For xfs filesystems