It is a quite a common situation when you run out of space on a VM and you need to get an extra space.

It is quite well documented how to expand actual disk size as well as deal with simple disk layout.

However, it is a better practice for the storage subsystem to have LVM configured.

How to update your partition layout if you use LVM? It is also pretty straight forward 🙂

lsblk utility can be used to visualize your existing disk layout:

$ lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
fd0                         2:0    1     4K  0 disk
sda                         8:0    0   256G  0 disk
├─sda1                      8:1    0   243M  0 part /boot
├─sda2                      8:2    0     1K  0 part
└─sda5                      8:5    0 255.8G  0 part
  ├─debain--vg-lv_root    254:0    0 202.6G  0 lvm  /
  ├─debain--vg-swap_1     254:1    0     2G  0 lvm  [SWAP]
  ├─debain--vg-lv_home    254:2    0    14G  0 lvm  /home
  ├─debain--vg-lv_var     254:3    0  18.6G  0 lvm  /var
  ├─debain--vg-lv_tmp     254:4    0   9.3G  0 lvm  /var/tmp
  └─debain--vg-lv_var_log 254:5    0   9.3G  0 lvm  /var/log
sdb                         8:16   0    32G  0 disk
└─sdb1                      8:17   0    32G  0 part /mnt/resource

Ideally, there will be just the one, as in the above example you have the sda device.

From this hierarchy, we can see that our disk device is /dev/sda and the LVM volume group is on /dev/sda5. Next, print out the partition table on the sda device:

$ parted /dev/sda -s print
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 275GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size   Type      File system  Flags
 1      1049kB  256MB  255MB  primary   ext3         boot
 2      257MB   175GB  175GB  extended
 5      257MB   175GB  175GB  logical                lvm

Have a look at this partition layout, where we should have an extended partition which contains a logical (lvm) partition.

We will use parted to increase the size of the extended partition first, and then the logical partition second.

The number arguments given to parted are from the Number column shown above.

Giving -1s as a size argument tells parted to resize up to the last available sector.

NOTE: make sure you resize the extended partition first (number 2, in this example) and the logical partition second (number 5 here).

$ parted /dev/sda -s resizepart 2 100%

$ parted /dev/sda -s resizepart 5 100%

After running both resize commands, print the partition table again to check the new sizes.

$ parted /dev/sda -s print
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 275GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size   Type      File system  Flags
 1      1049kB  256MB  255MB  primary   ext3         boot
 2      257MB   275GB  275GB  extended
 5      257MB   275GB  275GB  logical                lvm

Here we can see the Size value for both partitions has increased from 175GB to 275GB.

Next we need to resize the LVM partition.

Check amount of free space on the currently mounted LVM partitions:

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/dm-0                          200G  1.9G  189G   1% /
udev                                10M     0   10M   0% /dev
tmpfs                              3.2G   73M  3.1G   3% /run
tmpfs                              7.9G     0  7.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/sda1                          232M   33M  187M  15% /boot
/dev/mapper/debain--vg-lv_var       19G  6.7G   11G  39% /var
/dev/mapper/debain--vg-lv_var_log  9.1G  238M  8.4G   3% /var/log
/dev/mapper/debain--vg-lv_tmp      9.1G   22M  8.6G   1% /tmp
/dev/mapper/debain--vg-lv_home      14G  883M   13G   7% /home
/dev/sdb1                           32G   48M   30G   1% /mnt/resource

Important: Identify what logical volumes may need size expansion

The pvdisplay command will show the physical volumes on the machine:

$ pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               debain-vg

Earlier, we saw that the volume group was located on /dev/sda5 which lines up with what we see here.

Now, we resize this physical volume:

$ pvresize /dev/sda5
Physical volume "/dev/sda5" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

Next, check on the physical volume again.

We’re looking for the PV Size value to have increased over the value we saw when checking earlier:

$ pvdisplay
--- Physical volume ---
PV Name               /dev/sda5
VG Name               debain-vg
PV Size               255.76 GiB / not usable 2.00 MiB
Allocatable           yes (but full)
PE Size               4.00 MiB
Total PE              65474
Free PE               0
Allocated PE          65474
PV UUID               lkdfpr-lfke-ffwf-DI-LKFor-LKF-KJFn9

Next, identify the logical volume to which we want to allocate the extra space to.

Earlier, we saw the LV names, but to get the whole path, run lvdisplay:

$ lvdisplay
--- Logical volume ---
LV Path /dev/debain-vg/swap_1
LV Name swap_1
VG Name debain-vg
...

--- Logical volume ---
LV Path /dev/debain-vg/lv_root
LV Name lv_root
VG Name debain-vg
...

--- Logical volume ---
LV Path /dev/debain-vg/lv_home
LV Name lv_home
VG Name debain-vg

Increase the size on the desired logical volume, by adding xx% of the new free space. Please distribute all the available space across logical volumes that require increase.

$ lvresize -l +xx%FREE /dev/debain-vg/lv_root

Example:

$ lvresize -l +25%FREE /dev/debain-vg/lv_root

Finally, resize the ext4 partition(s) within said volume:

$ resize2fs /dev/debain-vg/lv_root

For XFS filesystems the xfs_growfs utility should be used against your partitions in lieu of resize2fs ( to verify xfs or not, use ‘df -Th’ ):

$ xfs_growfs /dev/rhel/home

That will grow the FS to 100% of the available space on that ‘partition’, the -D flag can be used if 100% is not desired.

Check it out with df:

df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/dm-0                          200G  1.9G  189G   1% /
udev                                10M     0   10M   0% /dev
tmpfs                              3.2G   73M  3.1G   3% /run
tmpfs                              7.9G     0  7.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/sda1                          232M   33M  187M  15% /boot
/dev/mapper/debain--vg-lv_var       19G  6.7G   11G  39% /var
/dev/mapper/debain--vg-lv_var_log  9.1G  238M  8.4G   3% /var/log
/dev/mapper/debain--vg-lv_tmp      9.1G   22M  8.6G   1% /tmp
/dev/mapper/debain--vg-lv_home      14G  883M   13G   7% /home
/dev/sdb1                           32G   48M   30G   1% /mnt/resource