Tuesday 5 April 2016

Increasing the size of a virtual disk with CentOS / LVM

After expanding the virtual disk / vmdk within VMWare vSphere, Player or Workstation.

If you have the ability to dynamically increase the disk size (i.e. without rebooting the system) we will need to rescan the SCSI bus - so we should find out what the device is with:

ls /sys/class/scsi_device/

and then tell the kernel to rescan it:

echo 1 > /sys/class/scsi_device/0\:0\:0\:0/device/rescan

Now leave it for a minute and the changes should then be reflected. (You can verify this with 'fdisk -l'.)

We should now proceed by creating a new partition (you can alternatively delete the existing partition and expand it - although this will require dismounting the storage device and hence if it was the OS disk would require you to do it via a live CD.)

fdisk /dev/sdX

** Note ensure that there is not currently more than three primary partitions on the disk or this will fail **

'n' (for new partition.)

'p' (for primary partition type.)

You can usually accept the defaults for the sector size as fdisk will typically use all available sectors.

't' (to change disk type.)

and ensure '8e' (Linux LVM) is defined.

Finally hit 'w' to write the changes.

We now need to get the kernel to pickup the new partition tables / block device (/dev/sda3 in my case) - we can either reboot the system or simply use the partprobe command:

partprobe -s

We then want to create a new physical volume for LVM:

pvcreate /dev/sda3

and run pvdisplay to review the addition:

pvdisplay

We now want to add the new physical volume to our volume group - we should firstly identify what it's called with:

vgdisplay

In my case it's 'centos.'

So to add the physical volume in we issue:

vgextend centos /dev/sda3

and then issue lvdisplay to get the relevent logical volume we wish to extend:

lvdisplay

In my case it's called '/dev/centos/root' - so to extend it we issue:

lvextend /dev/centos/root /dev/sda3

and finally resize the file system:

resize2fs /dev/mapper/centos-root

OR if you are using CentOS 7 - which uses XFS by default you will need to use:

xfs_growfs /dev/mapper/centos-root

0 comments:

Post a Comment