Expand CentOS /dev/mapper/centos-root size.

Note:

I am using fdisk for centos disk partitioning , this is a command line tool.

You could use cfdisk which provides a UI feature to do the same things like fdisk.

Both are available by default.

To use cfdisk, just write the command ‘cfdisk’ and press enter.

Prerequisite:

Delete all snapshots.

Shutdown the VM.

Expand Virtual disk:

Goto VM>Settings>Hardware>Hard Disk

Select Expand from Disk Utilities section.

Choose the size and click expand.

Power ON the VM and login as root.

Open terminal.

See the partition table:

fdisk is a tool to manipulate disk partition table meaning it allows to view and interact with disk partitions in a more easier way like the tools like windows disk manager.

-l will lists out the partition tables

fdisk -l

The output will show the actual physical disk and the partitions. Copy the name of physical disk.

Note: the full hard disk size reflects the expanded VM disk size, now we have to add it to partition.

Create new partition:

create new partition in the physical disk

 fdisk /dev/sda 

We will be in the fdisk prompt (Command (m for help):

  • Now press p to see the available partition ( Default will be sda1 and sda2)
  • Now press n to create a new primary partition.

Continue…. (All the values are default and so you can just press Enter)

  1. Now press p for choosing primary as we are not partitioning an existing partition.
  2. Note: By design only 4 Primary partitions are allowed , to over come this we use extended partition which are sub partitions of the actual primary ones Read more at : https://www.tldp.org/LDP/sag/html/partitions.html
  3. By default we only had 2 partitions , so press 3 to create partition 3. Note: for instance if you already had 3 partitions , then press 4 for the 4th one
  4. Press Enter two times. (To choose default memory block start and end)
  5. Press w to write the changes to the partition table.

Restart the system:

reboot the system and login as root again

reboot

Find out Volume Group name and Logical Volume:

Run below command and note the VG name :

vgdisplay 

Run below command and note the LV name:

lvdisplay

Read more about Physical Volume (PV),Volume Group(VG) , and Logical Volume (LV) at : Askubuntu

In short , we have the Logical Volumeroot‘ that sees the VG ‘centOS‘, and thinks that this is the maximum disk space available. So to increase the size of root we just have to increase the size of VG, and root feels like available disk size have increased. VG groups PVs together, so adding a new PV to VG, increases the total size of VG.

Create Physical Volume:

We have created a partition. Now we want to create physical volume to later combine with other PVs to expand the volume group ‘centos‘.

To create PV, use below command. (Here /dev/sda3 is the new partition we created)

pvcreate /dev/sda3

Extend VG:

syntax is vgextend <VG_name> <PV>

 vgextend centos /dev/sda3 

Check the free VG space:

centos- is our VG

we can see that we have free VG space

vgdisplay centos | grep "Free"

Extend the LV root:

Our logical volume is root, the syntax is

lvextend -L+#G /dev/<VG_name>/<LV_name>

were #- should be replaced with the size we got from above step ( we are utilizing full free space)

-L indicates size follows, +79G means we are adding 79GB to the existing size

lvextend -L+79G /dev/centos/root

Now extend the file system space:

We have extend ‘root‘, now will have extend the file system inside root . This can be done by below command.

 xfs_growfs /dev/centos/root

Reboot the system and check using the command df you can see the new root size:

df is an utility that shows disk space in the system ,

Reference:

@https://kb.vmware.com/