A common task for Linux/VMware administrators is adding a new virtual disk to a Linux VM and expanding the filesystem to utilise the free space. Below is a quick guide on how to complete this task.
Adding a new disk to the VM and then extending the volume group:
First edit the settings of the VM and add a new disk. In the example below I’m adding a new 400GB thin provisioned disk to the VM (ensure you have sufficient free space on the datastore first).
Next you need to rescan in the OS for the new disk (this command will scan all SCSI hosts).
echo "- - -" | tee /sys/class/scsi_host/host?/scan
We’ll now use
fdisk -l to get the name of the new disk (it’ll have no partition table), in the example below it’s
We’ll now use the commands below to create a new primary partition and change the partition type to Linux LVM.
Open the disk with fdisk using
n for New Partition.
p for Primary Partition.
Accept the default of
1 for Partition Number.
Accept the default for the first sector.
Accept the default for the last sector (fdisk will automatically detect the start and end of the disk).
t to change the Partition Type.
8e to change the Partition Type to Linux LVM.
w to write the partition table and exit fdisk.
We’ll now use the following commands to add the new partitioned disk to the existing volume group. I’m using the default centos volume group created when allowing CentOS to auto create your disks during installation in this example.
pvcreate /dev/sdb1 to initialize the new partition for use by LVM.
vgdisplay to get the VG Name (centos in the below example)
vgextend centos /dev/sdb1 to add the new partition into the existing volume group.
vgs to verify the newly added partition is now available as free space in teh volume group.
Adding disk space to an existing file system:
Check the volume group to ensure there is available free space that can be added to the filesystem using the
If there is sufficient space then type in
df -h to obtain the filesystem name (/dev/mapper/centos-root in the example below).
lvextend -L+400GB /dev/mapper/centos-root to add 400GB to the logical volume.
df -T to get the filesystem type, we’ll need this to choose the right tool for extending the filesystem.
The above example shows the filesystem type as xfs so we’ll use xfs_growfs to increase the existing filesystem.
xfs_growfs /dev/mapper/centos-root to extend the existing filesystem onto the new free space (not specifying a size will use all available free space).
df -h to verify the filesystem now shows the additional free space.