Always Upgrading Blog

A blog (mostly) about Enterprise IT upgrades.

Add New Virtual Disk to Linux VM and Expand Filesystem

Rack Layout

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).

New Disk

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 /dev/sdb

New Disk

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 fdisk /dev/sdb
Type n for New Partition.
Choose 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).
Type t to change the Partition Type.
Type 8e to change the Partition Type to Linux LVM.
Type 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.

Type pvcreate /dev/sdb1 to initialize the new partition for use by LVM.
Type vgdisplay to get the VG Name (centos in the below example)
vgdisplay
Type vgextend centos /dev/sdb1 to add the new partition into the existing volume group.
Type vgs to verify the newly added partition is now available as free space in teh volume group.
vgextend

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 vgs command.
vgs
If there is sufficient space then type in df -h to obtain the filesystem name (/dev/mapper/centos-root in the example below).
Rack Layout
Type lvextend -L+400GB /dev/mapper/centos-root to add 400GB to the logical volume.
Type df -T to get the filesystem type, we’ll need this to choose the right tool for extending the filesystem.
dft
The above example shows the filesystem type as xfs so we’ll use xfs_growfs to increase the existing filesystem.
Type 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).
Finally type df -h to verify the filesystem now shows the additional free space.


Share