Are there times when your partition becomes full and you wonder if you should delete some data or move to a new VM? Well, its not always necessary to migrate to another VM. You can simply extend the disk space using LVM.
Logical Volume Management (LVM) allows us to group different block devices into a single larger device that can be handled as one. The kernel LVM module that allows us to use LVM is called lvm-mod.o
LVM allows you to grow your disk partitions to accommodate growth of data.
For LVM to be used to extend your partition, the filesystem type should be Linux LVM (type 8e).
NOTE: For standard partitions, you will end up loosing data in the partition you want to grow as you must reformat it into Linux LVM type.
Extend the / and /home partitions of our VM with the extra disk we have attached, without data loss.
We will be running our test in a guest OS in Oracle Virtual Box 6.2. The guest OS is a minimal installation of Centos 7.
We have attached 2 SATA hard disks to our VM. One disk is free and the other is mounted and having /, boot, swap and /home partitions already configured during OS installation. The free disk us unmounted.
1. Backup all your data in the partitions you want to extend. It is reckless to try and extend a partition without backup.
2. Confirm your disk layout by running lsblk
3. Create a new partition from the free disk. Make the partition of type 8e, Linux LVM then reboot the VM.
4. After reboot, when you run the lsblk command again, you will see a new partition created under sdb called sdb1. Scan your file system for available volume groups. You can then display the details of the found volume groups to see which one it is we want to extend. For our case, we only have one volume group called centos and this is the one we will extend as follows
[root@localhost ~]# vgextend centos /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
Volume group "centos" successfully extended
5. We can then extend our partitions as needed. Here is how we extend the / partition by 2GB and /home partition by 6GB
[root@localhost ~]# lvextend -L +2G /dev/centos/root
Size of logical volume centos/root changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
Logical volume centos/root successfully resized.
[root@localhost ~]# lvextend -L +6G /dev/centos/home
Size of logical volume centos/home changed from 4.49 GiB (1150 extents) to 10.49 GiB (2686 extents).
Logical volume centos/home successfully resized.
6. Finally, we can increase the partition size of / and /home using the xfs_growfs command and verify disk size has changed with df command
[root@localhost ~]# xfs_growfs /dev/centos/root
[root@localhost ~]# xfs_growfs /dev/centos/home
[root@localhost ~]# df -h
Now your disk size should have changed without loss of data.