Easy live partition resizing on Ubuntu 16.04
Overview
A great benefit of the cloud servers is the easy scaling of resources up and down. But after scaling to a larger cloud machine and acquiring more storage space, you should extend the Linux partitions on it as well as the file system, in order to use that new space.
The tricky part here comes when you want to resize the root partition and some old methods included taking your server off, mounting the storage drive on another Linux machine and resizing the partitions. That of course is very inconvenient in the case of a production server and there are better ways to resize your root partition without having to take it offline.
In this tutorial we will see how to easily resize an online root partition on Ubuntu 16.04 without unmounting and so on.
Step 1: Resizing the partition
We are resizing the root partition which in the beginning is 30G.
root@cloud:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 473M 0 473M 0% /dev
tmpfs 99M 3.1M 96M 4% /run
/dev/mapper/cloud–vg-root 30G 1.6G 27G 6% /
tmpfs 492M 0 492M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 492M 0 492M 0% /sys/fs/cgroup
/dev/sda2 473M 69M 381M 16% /boot
/dev/sda1 511M 3.6M 508M 1% /boot/efi
tmpfs 99M 0 99M 0% /run/user/0
The actual resize is done using the parted tool:
parted /dev/sda resizepart 3 100%
Step 2: Resizing physical volume
Now after we’ve resized the partition, we need to scale the physical volume:
pvresize /dev/sda3
Step 3: Resizing logical volume
Resize the logical volume goes like this:
root@cloud:~# lvextend -l +100%FREE /dev/cloud-vg/root
Size of logical volume cloud-vg/root changed from 30.02 GiB (7684 extents) to 62.02 GiB (15878 extents).
Logical volume root successfully resized.
Step 4: Resizing the file system
Last step is extend the file system:
resize2fs -p /dev/cloud-vg/root
The result of the df command now should be showing the new size:
root@cloud:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 473M 0 473M 0% /dev
tmpfs 99M 3.1M 96M 4% /run
/dev/mapper/cloud–vg-root 61G 1.7G 57G 3% /
tmpfs 492M 0 492M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 492M 0 492M 0% /sys/fs/cgroup
/dev/sda2 473M 132M 317M 30% /boot
/dev/sda1 511M 3.6M 508M 1% /boot/efi
tmpfs 99M 0 99M 0% /run/user/0
Final thoughts
This easy and quick online resizing procedure can be used to expand live partition on your cloud servers.