Extend VMware virtual machine LVM disk

In order to increase the disk space of an existing virtual machine on VMware ESXi we have to do 2 steps. First, we modify the virtual machine setting to have an addition disk. This is same as we plug a new physical hard disk into a real machine. Second, we will need to go into the OS and modify the disk layout. Let’s see 2 steps in detail.

Adding new disk to the existing VMware virtual machine

Login into VMware ESXi infrastructure using your favorite management tool. You can use the VMware vSphere Client, Web Client or CLI. In this tutorial, i use the VMware vSphere Web Client to add 100GB disk.

Right click on your virtual machine and select Edit settings. Click on Add hard disk and specify the size of new disk.

Add new disk to the virtual machine

Then click Save to apply the changes. If you have VMware Tool installed on the virtual machine, you won’t need to reboot the it to see the new disk in the OS.

Increase the LVM disk space

Create partition from new disk

Use fdisk tool to make changes to the new disk

1
$ sudo fdisk /dev/sdb

Press n to create new partition

1
Command (m for help): n

Choose your partition type. Because this is the new disk, you will create the primary partition by selecting p.

1
2
3
4
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p

Then specify the partition size. For example i want to use the maximum space that the disk has, so i hit ENTER key for 3 times to select the default values. Should be good.

1
2
3
4
5
Partition number (1-4, default 1):
First sector (2048-209715199, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199):

Created a new partition 1 of type 'Linux' and of size 100 GiB.

Now input t to change the partition system type. In the hex code input, use 8e as we are going to use for the LVM.

1
2
3
4
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Now it is time to write changes to the disk. Input w to do that.

1
2
3
4
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

If you receive a message saying that the kernel still using the old partition table, and to reboot to use the new table. You can use partprobe command to do that without reboot.

1
$ sudo partprobe -s

Extend the Logical Volume with the new partition

Before we begin, let’s check the current Volume Group. In this example, my Volume Group name is ubuntu-1604-vg which has 19.52 GiB space. We will need to extend the Volume Group before adding more space to the Logical Volume.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ vgdisplay
--- Volume group ---
VG Name ubuntu-1604-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.52 GiB
PE Size 4.00 MiB
Total PE 4997
Alloc PE / Size 4997 / 19.52 GiB
Free PE / Size 0 / 0
VG UUID nk2HOk-TuPh-GcK9-2x2w-NN74-9BrB-XLWuEf

Now, create the Physical Volume from the new partition.

1
2
$ sudo pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

Then extend the Volume Group with above Physical Volume

1
2
$ sudo vgextend ubuntu-1604-vg /dev/sdb1
Volume group "ubuntu-1604-vg" successfully extended

Double check to see whether the Volume Group was extended. We will notice that the total size was increased to 119.52 GiB.

1
2
3
4
$ pvscan
PV /dev/sda5 VG ubuntu-1604-vg lvm2 [19.52 GiB / 0 free]
PV /dev/sdb1 VG ubuntu-1604-vg lvm2 [100.00 GiB / 100.00 GiB free]
Total: 2 [119.52 GiB] / in use: 2 [119.52 GiB] / in no VG: 0 [0 ]

Now let’s extend the it’s Logical Volume. You might have one or more Logical Volume. In this example i will use /dev/ubuntu-1604-vg/root which is my OS root disk. Please adjust it in your system.

1
2
3
$ sudo lvextend /dev/ubuntu-1604-vg/root /dev/sdb1
Size of logical volume ubuntu-1604-vg/root changed from 18.52 GiB (4741 extents) to 118.52 GiB (30340 extents).
Logical volume root successfully resized.

All that remains now, it to resize the file system to the Volume Group, so we can use the space.

1
2
3
4
5
$ sudo resize2fs /dev/ubuntu-1604-vg/root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/ubuntu-1604-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 8
The filesystem on /dev/ubuntu-1604-vg/root is now 31068160 (4k) blocks long.

Now let’s see how the the current disk usage on the system with df -h again.

1
2
3
4
5
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.5G 0 1.5G 0% /dev
tmpfs 295M 4.7M 291M 2% /run
/dev/mapper/ubuntu--1604--vg-root 117G 9.9G 102G 9% /
Share Comments