Resize root partition of a virtual machine on VMware

When it comes to a situation that the disk space is not enough on a virtual machine, it’s easy to extend the disk space if it’s on LVM, even the /root partition.

This post is very helpful.

For my own memo:
Case:
VM on VMware, installed with CentOS 6.x
#fdisk -l shows
Disk /dev/sda: 12GB ….
/dev/sda1 2 501 512000 83 Linux
/dev/sda2 502 12288 12069888 8e Linux LVM

On /dev/sda2
Disk /dev/mapper/vg_node1-lv_root: 7.5 GB
Disk /dev/mapper/vg_node1-lv_swap: 4 GB

Goal: Add 8G extra space to /dev/mapper/vg_node1-lv_root

Steps:
1. Using VMware GUI to add 8G to the virtual machine. Make sure it recognizes the new size.

2. Memo the current cylinder start number of /dev/sda2
/dev/sda2 502 12288 12069888 8e Linux LVM

3. Delete partition /dev/sda2 then recreate it with the new size
fdisk /dev/sda
Use command “d”, then “2” to delete /dev/sda2
Use “n” to create a new partition, partition number is “2”
Use “t” to change partition type to 8e as “Linux LVM”
Then “w” to save the change

Reboot the VM

Now the partition size is changed. I need to change the pv size and vg size.

5. Change pv size
pvsize /dev/sda2

6. Change vg size
#vgdisplay (found the free PE = 2048 )
#lvextend /dev/mapper/vg_node1-lv_root –extents +2048

7. Still need to increase file system size
#resize2fs /dev/mapper/vg_node1-lv_root

————————————————————————-
Another way to do it

1. Add disk space from vmware GUI, however the following command didn’t work, so reboot
echo “- – -” > /sys/class/scsi_host/host0/scan

2. frisk -l shows new disk space on /dev/sda
create a new partition on /dev/sda
frisk /dev/sda -> n -> p -> 3 -> it will show the right start/end block number automatically, so just hit enter,
-> t -> 8e -> w

3. fdisk shows /dev/sda3, the new partition created.
However it warns the kernel won’t use the new table until reboot, so reboot for the 2nd time

4. Create a new physical volume on lvm using sda3
pvcreate /dev/sda3

5. find vg name from vgdisplay, find vg name is using “centos”, add sda3 to virtual group
vgextend centos /dev/sda3

6. pvscan shows sda3 as a member of vg “centos”
[root@mononoke ~]# pvscan
PV /dev/sda2 VG centos lvm2 [15.51 GiB / 0 free]
PV /dev/sda3 VG centos lvm2 [9.00 GiB / 0 free]
Total: 2 [24.50 GiB] / in use: 2 [24.50 GiB] / in no VG: 0 [0 ]

7. lvdisplay shows 2 logical volumes, /dev/centos/root and /dev/centos/swap,
lvextend /dev/centos/root /dev/sda3

8. resize file system, since it’s using XFS on centos7,
xfs_growfs /dev/centos/root

Here‘s the reference

——————————————————-
To check how much free space left in a physical volume, do pvdisplay, then find value of “Free PE”

— Physical volume —
PV Name /dev/sdb2
VG Name test_vg
PV Size 20.00 GiB / not usable 1.31 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5120
Free PE 2620
Allocated PE 2500