how to increase the size for /dev/mapper/centos-root?

My current settings are as below, where the /dev/mapper/centos-root partition is almost full.

Looks like this partition is on disk /dev/mapper/centos-root. but there is another disk /dev/vda , which still has enough free space

Are these two disks separate physical disks?

how to increase the /dev/mapper/centos-root partition?

[root@devbox ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G   17G  1.4G  93% /
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G   48M  3.8G   2% /dev/shm
tmpfs                    3.9G   74M  3.8G   2% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1                497M  297M  201M  60% /boot
tmpfs                    783M   48K  783M   1% /run/user/1001

[root@devbox ~]# fdisk -l

Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0001ec6a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM
/dev/vda3        41943040    52428799     5242880   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@devbox ~]# 

Solution 1:

The first thing is to check if you have free extents in your volume group, to do that, you will use:

vgdisplay

which will return details on the VG, the important line you must check is the one that states Free PE / Size. There you will see the size available to create or extend logical volumes. For instance in my case I have a server that says:

Free PE / Size           3834 / 14.98 GiB

Given that you have the required free space you should use:

lvextend /dev/mapper/centos-root -L +2G

In the latter case I am extending the logical volume adding 2GB. Note the +, if you give only the size, it will go to the specified size, I usually use this syntax because it is more transparent with the space you have available in the volume group.

After you successfully extended the volume (check with lvscan), you have to extend the file system, you can use:

resize2fs /dev/mapper/centos-root

Run df again to check that the available space has changed.

What if there's no space in the VG?

You have to first extend the volume group to be able to extend the logical volumes. For this matter you have to add a new disk. I am assuming that the CentOS box is a virtual machine because of the size of the disk, but of course this can be done on a physical server too, it is just that you have to physically add a disk.

Once you have the disk on the server, you have to create an LVM physical volume (PV), this can be created on a partition or even on the disk, I don't know the pros of doing it on the disk, but in my experience I have found it confusing as you won't be able to see a partition table, so I would recommend creating a partition first.

To create the PV over disk `/dev/vdb' partition 1 you do:

pvcreate /dev/vdb1

Once you have the PV, extend the VG (I don't know the name, I bet it is centos, check on your vgdisplay):

vgextend centos /dev/vdb1

TL;DR

For VG: vg0, LV:lv0 and new disk /dev/sdb. Extending 5GB

  1. Check available space on the VG: vgdisplay. If enough go to 4
  2. If you don't have space add a disk and create a PV: pvcreate /dev/sdb1
  3. Extend the VG: vgextend vg0 /dev/sdb1
  4. Extend the LV: lvextend /dev/vg0/lv0 -L +5G
  5. Check: lvscan
  6. Resize the file system: resize2fs /dev/vg0/lv0
  7. Check: df -h | grep lv0

Solution 2:

If you have free extents, which could be easiliy possible judging your outputs, then lvextend and xfs_growfs or resize2fs could be the commands you are looking for.