Expand LVM Volume
Expanding the Logical Volume
-
Check Available Space: Before expanding, check how much free space is available in the volume group.
vgdisplay
- Resize the Logical Volume: To extend the logical volume, use the following command. Replace
<size>
with the amount you want to add (e.g.,10G
for 10 GB):sudo lvextend -L +<size> /dev/mapper/ubuntu--vg-ubuntu--lv
- Resize the Filesystem: After resizing the logical volume, you need to resize the filesystem to use the new space:
- For ext4 filesystem:
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
- For XFS filesystem:
sudo xfs_growfs /dev/mapper/ubuntu--vg-ubuntu--lv
- For ext4 filesystem:
Shrinking the Logical Volume
- Check Filesystem Size: Before shrinking, ensure the filesystem is smaller than the logical volume you want to resize it to. You might need to reduce the filesystem size first.
- For ext4 filesystem:
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv <new_size>
- For example, to shrink to 20G:
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv 20G
- For ext4 filesystem:
- Reduce the Logical Volume: Now you can safely reduce the logical volume:
sudo lvreduce -L <new_size> /dev/mapper/ubuntu--vg-ubuntu--lv
- Resize the Filesystem Again (if necessary): If you haven’t already resized the filesystem in the first step, you can do it now.