One of the wonderful things about cloud computing is the ability to quickly create, resize, and use virtual storage devices, called volumes. In this article, I will describe the process of resizing an attached volume on a Linux VPS (Virtual Private Server) running Ubuntu 22.04.
To resize a volume attached to a VPS (Virtual Private Server) is performed in the following steps:
- Backup of the volume you want to resize.
- Take a snapshot of the VPS.
- Power off the VPS.
- Detach the volume from the VPS.
- Resize the volume.
- Reattach the volume to the VPS.
- Power on the VPS.
- Stop services that make use of the volume.
- Unmount the volume.
- Check the filesystem for errors.
- Resize the filesystem on the volume.
- Remount the volume.
- Restart the stopped services.
The rest of this article will describe these steps in further detail.
IMPORTANT: Do not skip the steps where you take a backup of the volume and snapshot of the VPS. If the resizing for some unforeseen reason goes bad, you will need a backup.
Resize the volume
Cloud provider button clicking action
The first thing you need to do is power off the VPS. It's impossible to resize a volume attached to a running VPS. This can be done either through SSH, a web shell or through a button in the cloud provider's web GUI.
Once the VPS is turned off, you can detach it from the VPS. Some cloud providers, such as Linode allow you to resize the volume without detaching it from a VPS, but others like the ones using OpenStack force you to detach the volume before you are allowed to resize it.
To resize the volume you simply find the correct button on the volume and set the the new volume size. Typically you change a number from the current size to the wanted size and click apply. The change is applied instantly in most cases. If not you will get some sort of visual indicator when the resizing job is completed.
Finally, the resized volume can be reattached to the VPS, and the VPS can be booted.
Expand the filesystem
Once the VPS has successfully booted you can expand the filesystem to make use of all the new space available. Before you start you must stop all services that may use the volume. In many cases, this is services such as web servers (Nginx and Apache) and file-sharing services (samba).
Because you cannot resize a mounted filesystem, you will have to start by unmounting it. Please make sure you unmount the correct drive.
# Unmount the volume sudo umount /dev/sdb1
When the filesystem is unmounted you can start the resizing process. The first step is to check the filesystem for errors. Depending on how large the filesystem is, and how many files you have on it, this can take anything from a very short time to several minutes.
# Check filesystem sudo e2fsck -f /dev/sdb1
When the check is completed you can resize the filesystem to make use of the entire volume. This is usually a very quick process. But if you are expanding a huge filesystem this may take some time as well.
# Resize volume sudo resize2fs /dev/sdb1
Finally, the volume can be reattached to your filesystem. Please make sure to remount it at the same location as it was previously mounted.
# Remount volume to filesystem sudo mount /dev/sdb1 /mnt/test
Now you can restart all the services you stopped, and pat yourself on the back for successfully resizing the volume on your VPS 🎉