Wednesday, February 15, 2023

Ubuntu and CentOS VM's Disk extend

For ubuntu(tested on ubuntu 20.04) disk extened:

After you added the extra space to the disk run: echo 1 > /sys/block/sda/device/rescan (must run as root)
enter fdisk: sudo cfdisk
you should see the free space in green
go to the device you want to take the free space(usally the last one), and choose resize
now your device should be with the extra space, choose write and write yes not only y to save and than exit
run sudo pvresize /dev/sda3 (dev/sdaX number is from the previous step)
run sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv (run lvdisplay to get the lv path)
run sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv (run df -h to get the filesystem name)
check with df -h you have the new space added


CentOS(tested on CentOS 7) Disk extend:

check if there is unusable space: cfdsik /dev/sda
if you added disk space but you dont see it in the OS, run: echo 1 > /sys/block/sda/device/rescan
than check again in cfdsik /dev/sda
fdisk /dev/sda
view current partitions: p
delete the last partition: d and than the number
re-create the partition with a bigger size: n, choose p for primary and the number (same as in the last step)
configure the created partition in the last step, partition table same as it was: t and the number (same as in the last step), write 8e for the Linux LVM table
you can check with p all the changes, that it looks exaclty the same but the end sector of the last partition is bigger
press w to save the changes and than reboot the machine
now we need to extend the LVM physical and logical volumes
pvdisplay
pvresize /dev/sda4 (/dev/sdaX is the number from fdisk)
lvdisplay to show
lvresize /dev/mapper/centos-root /dev/sda4
*you can get the name from df-h
*/dev/sdaX is the number from fdisk8
next check the type of the logical volume with the df -Th command and check the type column of the /dev/mapper/centos-root
for xfs use:
xfs_growfs /dev/mapper/centos-root