I use Google Cloud Platform on a daily basis. It’s great in a bunch of ways, but the hard disk space is pretty expensive.
Google gives you recommendations on if you are using too much CPU or RAM resources, but if you have too much hard disk space.
It’s super easy to expand the hard disk, but shrinking a disk, especially a boot disk is another story.
Many sites incorrectly state you can use the same method that you use to expand, to shrink, but I don’t reckon they’ve ever done it, as the documentation clearly states that you can’t.
For me, I had a 1TB drive and was only consistently using 300GB, so a 400GB drive would suffice, this a saving of approximately $200 AUD a month. Definitely worth the resize for me.
So how can it be done?
Basically, you need to add another, smaller drive then copy everything from the source drive to this new drive.
You can use DD or Rsync, but these are tricky if you are trying to shrink the boot disk.
In comes Weresync. Weresync is a tool which takes care of the copy for you and even copies the partition tables and boot loader.
The only catch I found is that it uses Python3, which isn’t default on a bunch of operating systems, but once you have Python3 handy, then it’s easy to install.
- Make a Snapshot of your server instance. Just in case you screw something up.
- Install Python3 for your platform. There are a bunch of sites on this info. You might even be lucky and have it.
- Create your new drive in GCP and attach it to the instance. Basically edit the instance and select the new drive option. This is done in the GCP console.
- Format the new drive
#mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/[DEVICE_ID]
The new [DEVICE_ID] is most likely /dev/sdb if you only had one drive, you can use lsblk to see what drives you have. - Install weresync
#pip install weresync
- Run the weresync daemon (this has to run in the background for weresync to work).
weresync-daemon & >/dev/null 2>&1
- Start up weresync to copy your data
#weresync -C -L grub2 -g 1 /dev/sda /dev/sdb
- Disconnect your new drive from the instance.
- Either switch boot disks to the new drive on your existing instance, or create a new instance with the new drive as the boot disk, then switch the IP addresses, if all is good 🙂
Weresync documentation: https://weresync.readthedocs.io/en/master/