Shrinking a Linode Ext4 Block Storage Volume

Linode is an amazing platform. This platform recently just got better with the ability to boot your Linodes from Block Storage.

One shortcoming of Block Storage is that you can only resize it in one direction: up. This can prove to be particularly problematic when over-ambitious users first discover this feature and select too much storage than what they need or want. When they go to resize the Block Storage volume to decrease its size they quickly discover that, by default, Linode does not allow this.

Fortunately there is a workaround.

Step Zero: Size Up The Volume's Data

In order to successfully resize a volume, you need to make sure you know how much data you actually have so that you don't overdo it and loose data in the process.

This can be achieved by running df -h from within your Linode when it is running. Take the sizeof the /dev/root directory and add a Gigabyte or two to that. You now have your new volume's target size.

Step One: Create a new Volume of Equal Size

This allows us to effectively create a working copy of the Volume we wish to shrink while leaving the original volume alone. We will get rid of the original when we are convinced that we have successfully resized our volume (and everything is working properly). You can do this by creating a new Volume of equal size to the one you want to shrink and then clone the volume with the with the command line while in Rescue mode. You can also simply use the Volumes page in the Linode Manager while the Volume is not attached (or the Linode it is attached to is shut down) using the Clone button.

Step Two: Test the Cloned Volume

When the Clone has been created, you can test if you were successful by mounting it to your Linode and then simply trying to use it. Check to see if your important stuff is where it should be and if it is all there, proceed to the next step.

Step Three: Resize the Volume

From Rescue mode, you now resize the cloned volume.

e2fsck -f /dev/disk/by-id/scsi-0Linode_Volume_BlockStorageCLONE
resize2fs /dev/disk/by-id/scsi-0Linode_Volume_BlockStorageCLONE 40G

The first command prepare the volume "BlockStorageCLONE" for resizing and the second command resizes the volume "BlockStorageCLONE" to be 40 GB.

Step Four: Test the Cloned Volume (Again)

Now that the volume has been resized. Test it one more time just to make sure everything is in order.

Step Five: Create a Destination Volume of the Desired Size

Using the Linode Manager Volumes page, create one more volume. This time it should be of equal size (or a little bit bigger for best results... Remember we are converting between GiB and GB so there will be some discrepancy between actual sizes. It is always best to add a little bit more than what you actually want to ensure successful data transfer.)

Step Six: Clone the Cloned Volume to the Destination Volume

Now you can copy the resized volume from the cloned volume to the destination volume. This needs to be done with the command line while in Rescue mode.

Step Seven: Expand the Destination Volume

If you did this correctly, chances are there should be a discrepancy between the partition size of the disk and the size of the volume itself. Make sure you're using what you are paying for by expanding the file system to fill the whole volume. From within Rescue mode run:

e2fsck -f /dev/disk/by-id/scsi-0Linode_Volume_BlockStorageDESTINATION
resize2fs /dev/disk/by-id/scsi-0Linode_Volume_BlockStorageDESTINATION

These commands operate the same way as they do in step three, however without an additional attribute on the resize2fs command, the command will evaluate the volume and resize the partition to fit the remaining space.

Step Eight: Test the Destination Volume

Yet again, test your new volume, check to make sure everything is there.

Step Nine: Clean Up

Provided your tests were thorough and successful you can now delete the original volume and the cloned volume. And, while your Linode is off, you can even rename your destination volume to be the same name as the original volume once it is deleted.