Resizing LVM partitions online in Ubuntu 16.04

Resizing paritions is not something I find myself doing very frequently so I tend to forget the procedure. Below is what I did to resize a parition in Ubuntu16.04. This server is a vm running inside esxi.

I’ve already resized the disk in the vmware console from 30 to 100GB. We are now ready to work on the server.

Reisizing the Parition

Here we can see that our Logical Volume is only 24Gb

1
2
3
4
5
6
7
8
9
root@lab:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 16G 0 16G 0% /dev
tmpfs 3.2G 15M 3.2G 1% /run
/dev/mapper/eve--ng--vg-root 24G 15G 6.9G 69% /
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/sda1 472M 205M 243M 46% /boot

We set the output to show in bytes and print the sizes of our partitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@lab:~# parted
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit b
(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 107374182400B
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
32256B 1048575B 1016320B Free Space
1 1048576B 511705087B 510656512B primary ext2 boot
511705088B 512752639B 1047552B Free Space
2 512752640B 32211206143B 31698453504B extended
5 512753664B 32211206143B 31698452480B logical lvm
32211206144B 107374182399B 75162976256B Free Space

Since our parition is on an extended partition, we need to resize both, the extended (2) and the logical (5) paritions.
We resize these paritions to use the rest of the free space on the disk. This is the value taken from the end column of the last line in the above output.

1
2
3
4
(parted) resizepart 2 107374182399B
(parted) resizepart 5 107374182399B
(parted) quit
Information: You may need to update /etc/fstab.

Next, we need to resize the adjust the size of the physical volume to account for the changes we made in the previous step.

1
root@lab:~# pvresize /dev/sda5

Now that we have the physical disk resized, we can resize the LVM. You can use lvscan to show the names of the LVM if you are unsure of the names.

1
2
3
root@lab:~# lvscan
ACTIVE '/dev/eve-ng-vg/root' [28.52 GiB] inherit
ACTIVE '/dev/eve-ng-vg/swap_1' [6.00 GiB] inherit

Here we extend the volume to use 100 percent of the free space on the physical volume.

1
2
3
root@lab:~# lvextend -l +100%FREE /dev/eve-ng-vg/root
Size of logical volume eve-ng-vg/root changed from 23.52 GiB (6021 extents) to 93.52 GiB (23941 extents).
Logical volume root successfully resized.

Lastly, we need to tell the partition about the new space so we can use it.

1
2
3
4
5
root@lab:~# resize2fs /dev/eve-ng-vg/root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/eve-ng-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 6
The filesystem on /dev/eve-ng-vg/root is now 24515584 (4k) blocks long.

Verify the new space

1
2
3
4
5
6
7
8
9
root@lab:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 16G 0 16G 0% /dev
tmpfs 3.2G 15M 3.2G 1% /run
/dev/mapper/eve--ng--vg-root 92G 16G 73G 18% /
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/sda1 472M 205M 243M 46% /boot