Increase/expand an XFS filesystem in RHEL 7 / CentOS 7
This guide will explain how to grow an XFS filesystem on VMWare Virtual Machine without a reboot.
Verify if your server has XFS file system (you should see "xfs" in the Type column):
1
# df -Th
2
3
Filesystem Type Size Used Avail Use% Mounted on
4
/dev/mapper/cl-root xfs 143G 27G 117G 19% /
5
devtmpfs devtmpfs 908M 0 908M 0% /dev
6
tmpfs tmpfs 918M 4.0K 918M 1% /dev/shm
7
tmpfs tmpfs 918M 90M 828M 10% /run
8
tmpfs tmpfs 918M 0 918M 0% /sys/fs/cgroup
9
/dev/sda1 xfs 1014M 184M 831M 19% /boot
10
/dev/mapper/cl-home xfs 8.0G 33M 8.0G 1% /home
11
tmpfs tmpfs 184M 0 184M 0% /run/user/0
Copied!
To increase the disk size of Virtual Machine, you need to do 2 major steps:
  1. 1.
    First, you need to increase the disk's size in your vSphere Client or through the CLI. This will increase the "hardware" disk that your Virtual Machine can see.
  2. 2.
    Then, you need to utilize that extra space by partitioning it.

Step 1. Increase a hardware disk size in VMWare ESXi host

Checking if you can extend the current disk or need to add a new one

This is rather important step, because a disk that has been partitioned in 4 primary partitions already can not be extended any more. To check this, log into your server and run fdisk -l at the command line.
1
# fdisk -l
2
3
Disk /dev/sda: 137.4 GB, 137438953472 bytes, 268435456 sectors
4
Units = sectors of 1 * 512 = 512 bytes
5
Sector size (logical/physical): 512 bytes / 512 bytes
6
I/O size (minimum/optimal): 512 bytes / 512 bytes
7
Disk label type: dos
8
Disk identifier: 0x000aa739
9
10
Device Boot Start End Blocks Id System
11
/dev/sda1 * 2048 2099199 1048576 83 Linux
12
/dev/sda2 2099200 268435455 133168128 8e Linux LVM
Copied!
If it looks like that, with only 2 partitions, you can safely extend the current hard disk in the Virtual Machine.
However, if it looks like this:
1
# fdisk -l
2
3
Disk /dev/sda: 187.9 GB, 187904819200 bytes
4
255 heads, 63 sectors/track, 22844 cylinders
5
Units = cylinders of 16065 * 512 = 8225280 bytes
6
7
Device Boot Start End Blocks Id System
8
/dev/sda1 * 1 25 200781 83 Linux
9
/dev/sda2 26 2636 20972857+ 8e Linux LVM
10
/dev/sda3 2637 19581 136110712+ 8e Linux LVM
11
/dev/sda4 19582 22844 26210047+ 8e Linux LVM
Copied!
It will show you that there are already 4 primary partitions on the system, and you need to add a new Virtual Disk to your Virtual Machine. You can still use that extra Virtual Disk to increase your LVM size, so don't worry.

Adding diskspace to Virtual Machine

Using VMWare vSphere Client, open the properties of the virtual machine and increase the Provisioned Size.
If the "Provisioned Size" area (top right corner) is greyed out, consider turning off the VM first (if it does not allow "hot adding" of disks/sizes), and check if you have any snapshots made of that VM. You can not increase the disk size, as long as there are available snapshots.
Alternatively, if you already have 4 primary paritions, you can also choose "Add..." to add new Hardware "Virtual Disk" to your VM, with the desired extra space.

Step 2. Extend partition within a Virtual Machine

Partitioning the unallocated space: if you've increased the disk size

Once you've changed the disk's size in VMware, boot up your VM again if you had to shut it down to increase the disk size in vSphere. If you've rebooted the server, you won't have to rescan your SCSI devices as that happens on boot. If you did not reboot your server, rescan your SCSI devices as such.
First, check the name(s) of your scsi devices.
1
# ls /sys/class/scsi_device/
2
1:0:0:0 2:0:0:0
Copied!
Then rescan the scsi bus. On this machine, we have two devices. Execute the following commands to re-scan them. Below you can replace the '1:0:0:0' with the actual scsi bus name found with the previous command. Each colon is prefixed with a slash, which is what makes it look weird.
1
# echo 1 > /sys/class/scsi_device/1\:0\:0\:0/device/rescan
2
# echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/rescan
Copied!
That will rescan the current scsi bus and the disk size that has changed will show up.
Execute fdisk -l to check if new size if visible to the Virtual Machine:
1
# fdisk -l
2
3
Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectors
Copied!

Partitioning the unalloced space: if you've added a new disk

If you've added a new disk on the server, the actions are similar to those described above. But instead of rescanning an already existing scsi bus like show earlier, you have to rescan the host to detect the new scsi bus as you've added a new disk.
1
# ls /sys/class/scsi_host/
2
total 0
3
drwxr-xr-x 3 root root 0 Feb 13 02:55 .
4
drwxr-xr-x 39 root root 0 Feb 13 02:57 ..
5
drwxr-xr-x 2 root root 0 Feb 13 02:57 host0
Copied!
Your host device is called host0, rescan it as such:
1
# echo "- - -" > /sys/class/scsi_host/host0/scan
Copied!
It won't show any output, but running fdisk -l will show the new disk.

Create the new partition

Once the rescan is done (should only take a few seconds), you can check if the extra space can be seen on the disk.
1
# fdisk -l
2
3
Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectors
4
Units = sectors of 1 * 512 = 512 bytes
5
Sector size (logical/physical): 512 bytes / 512 bytes
6
I/O size (minimum/optimal): 512 bytes / 512 bytes
7
Disk label type: dos
8
Disk identifier: 0x000aa739
9
10
Device Boot Start End Blocks Id System
11
/dev/sda1 * 2048 2099199 1048576 83 Linux
12
/dev/sda2 2099200 268435455 133168128 8e Linux LVM
Copied!
Using fdisk, create a new partition on the /dev/sda device. Enter n, to create a new partition:
1
# fdisk /dev/sda
2
Welcome to fdisk (util-linux 2.23.2).
3
4
Changes will remain in memory only, until you decide to write them.
5
Be careful before using the write command.
6
7
8
Command (m for help): n
Copied!
Now choose p to create a new primary partition. Please note, your system can only have 4 primary partitions on this disk! If you've already reached this limit, create an extended partition.
1
Partition type:
2
p primary (2 primary, 0 extended, 2 free)
3
e extended
4
Select (default p): p
Copied!
Choose your partition number. Since we already had /dev/sda1 and /dev/sda2, the logical number would be 3.
1
Partition number (3,4, default 3): 3
Copied!
Choose the first and last sectors for new partition, if you hit ENTER, then by default new partition will use all available disk space.
1
First sector (268435456-335544319, default 268435456): <ENTER>
2
Using default value 268435456
3
Last sector, +sectors or +size{K,M,G} (268435456-335544319, default 335544319): <ENTER>
4
Using default value 335544319
5
Partition 3 of type Linux and of size 32 GiB is set
Copied!
Now type t to change the partition type. When prompted, enter the number of the partition you've just created in the previous steps. When you're asked to enter the "Hex code", enter 8e, and confirm by hitting enter.
1
Command (m for help): t
2
Partition number (1-3, default 3): 3
3
Hex code (type L to list all codes): 8e
4
Changed type of partition 'Linux' to 'Linux LVM'
Copied!
Once you get back to the main command within fdisk, type w to write your partitions to the disk. You'll get a message about the kernel still using the old partition table, and to reboot to use the new table. The reboot is not needed as you can also rescan for those partitions using partprobe.
1
Command (m for help): w
2
The partition table has been altered!
3
4
Calling ioctl() to re-read partition table.
5
6
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
7
The kernel still uses the old table. The new table will be used at
8
the next reboot or after you run partprobe(8) or kpartx(8)
9
Syncing disks.
Copied!
Run the following to scan for the newly created partition:
1
# partprobe -s
2
/dev/sda: msdos partitions 1 2 3
Copied!
If that does not work for you, you can try to use "partx" to rescan the device and add the new partitions. In the command below, change /dev/sda to the disk on which you've just added a new partition.
1
# partx -v -a /dev/sda
Copied!
If that still does not show you the newly created partition for you to use, you have to reboot the server. Afterwards, you can see the newly created partition with fdisk.
1
# fdisk -l
2
3
Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectors
4
Units = sectors of 1 * 512 = 512 bytes
5
Sector size (logical/physical): 512 bytes / 512 bytes
6
I/O size (minimum/optimal): 512 bytes / 512 bytes
7
Disk label type: dos
8
Disk identifier: 0x000aa739
9
10
Device Boot Start End Blocks Id System
11
/dev/sda1 * 2048 2099199 1048576 83 Linux
12
/dev/sda2 2099200 268435455 133168128 8e Linux LVM
13
/dev/sda3 268435456 335544319 33554432 8e Linux LVM
Copied!

Extend the Logical Volume with the new partition

Now, create the physical volume as a basis for your LVM. Please replace /dev/sda3 with the newly created partition.
1
# pvcreate /dev/sda3
2
3
Physical volume "/dev/sda3" successfully created
Copied!
Now find out how your Volume Group is called. In our example, it has name cl
1
# vgdisplay
2
--- Volume group ---
3
VG Name cl
4
...
Copied!
Let's extend that Volume Group by adding the newly created physical volume to it.
1
# vgextend cl /dev/sda3
2
3
Volume group "cl" successfully extended
Copied!
With pvscan, we can see our newly added physical volume, and the usable space (32GB in this case).
1
# pvscan
2
PV /dev/sda2 VG cl lvm2 [127.00 GiB / 4.00 MiB free]
3
PV /dev/sda3 VG cl lvm2 [32.00 GiB / 32.00 GiB free]
4
Total: 2 [158.99 GiB] / in use: 2 [158.99 GiB] / in no VG: 0 [0 ]
Copied!
Now we can extend Logical Volume (as opposed to the Physical Volume we added to the group earlier).
First, check the logical volumes available on system using command ls /dev/VolGroupName (in our example volume group name is cl):
1
# ls /dev/cl
2
home root swap
Copied!
We have home, root and swap logical volumes. To extend the logical volume root, execute command:
1
# lvextend /dev/cl/root /dev/sda3
2
Size of logical volume cl/root changed from 111.00 GiB (28415 extents) to 142.99 GiB (36606 extents).
3
Logical volume cl/root successfully resized.
Copied!
All that remains now, is to resize the file system to the volume group, so we can use the space. Execute xfs_growfs command as shown below (replace cl-root with the name of volume group on your system).
1
# xfs_growfs /dev/mapper/cl-root
2
meta-data=/dev/mapper/cl-root isize=512 agcount=4, agsize=7274240 blks
3
= sectsz=512 attr=2, projid32bit=1
4
= crc=1 finobt=0 spinodes=0
5
data = bsize=4096 blocks=29096960, imaxpct=25
6
= sunit=0 swidth=0 blks
7
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
8
log =internal bsize=4096 blocks=14207, version=2
9
= sectsz=512 sunit=0 blks, lazy-count=1
10
realtime =none extsz=4096 blocks=0, rtextents=0
11
data blocks changed from 29096960 to 37484544
Copied!
Execute df -h to confirm that new disk size is available to the Virtual Machine.
1
# df -h
2
Filesystem Size Used Avail Use% Mounted on
3
/dev/mapper/cl-root 143G 27G 117G 19% /
4
devtmpfs 908M 0 908M 0% /dev
5
tmpfs 918M 4.0K 918M 1% /dev/shm
6
tmpfs 918M 89M 830M 10% /run
7
tmpfs 918M 0 918M 0% /sys/fs/cgroup
8
/dev/sda1 1014M 184M 831M 19% /boot
9
/dev/mapper/cl-home 8.0G 33M 8.0G 1% /home
10
tmpfs 184M 0 184M 0% /run/user/0
Copied!