To get to the guide, scroll down and skip the prologue.
PrologueOnce my 1 year free trial with Amazaon AWS was over, I found that I'm paying for 30 GB of storage space while I'm actually using only about 5 GB. Hence I decided to shrink it down to about 7 GB or so. My plan is to just monitor the usage and see if I can reduce the usage or expand the volume later as needed.
Searching for guides on Google, the top hit was this blog post. But I found that the instructions are actually terrible. Here are the problems with it:
- The guide tells you to FORMAT the volume with your existing data. YES! That's ridiculous, but it's true. It's this command: sudo mkfs -t ext4 /dev/xvdf. I did it and destroyed my data, but lucky for me, I had made a snapshot. No problem, just restore the snapshot to a volume. But what if you DON'T have a snapshot?
- The guide tells you to use rsync to copy files from your old volume to new one. If you want to know what's wrong with doing that, just check the disk usage (df command) after you're done with it. You'll find that "something is missing". When you want a true clone of a volume, this is NOT good enough. There will be stuff missing like hidden files, metadata, whatnot!
- If your volume is a root volume, forget about your system ever booting from the shrunken volume. Your instance will not even start.
After experimenting with that, I found another blog post in the comments. Well, this kind of pointed me in the right direction, but it is still lacking (hence the reason for my own post). Here is the good and bad about this post:
- Good: No need to mount your volumes. The guide tells you to actually clone the disk or partition to the new one, which is indeed the correct method - you will not miss any data in the process.
- Good: A couple of commands make sure everything is working correctly. This is really good, as you can be confident you're doing the right thing when you see the tests pass.
- Bad: Does not guide you on how to correctly clone a root volume. It just tells you "use fdisk to create one [partition]" (basically the equivalent of "go figure it out yourself") and leaves you hanging there. Without correct guidance to do this, you'll end up with an unbootable volume.
- Bad: Again with the root volume, no guidance whatsoever on cloning the boot loader. If you go look for guides, you're told to install GRUB manually in the new volume using a bunch of complicated commands. The good news is: I have found a much easier way. You can CLONE the existing boot loader, whatever it may be!
That being said, here's a complete guide to actually shrink a root volume. I will follow it with another guide to shrink a normal volume.