In Google Cloud I often use Debian 9 Stretch for my test instances. Today I was wondering if this OS automatically resizes the root file system if I resize the VM instance disk. I also want to see if this can be done “online” and by using scripts to automate resizing remotely. This answer is Yes to these questions.

The image used is “debian-9-stretch-v20181011”.

One of the items that I appreciate about Google Cloud (and all the major cloud vendors) is their serious approach to providing excellent command line tools.

Having a great GUI console is very important, but having great command line tools is critical for repeatable development processes. It is far easier to document a command line then a lot of GUI screen captures that will be out of date next month.

Important notes:

  • Before you modify the file system on your disk, create a snapshot.
  • Boot disks use MBR partitions, which are limited to 2 TB in size. Do not resize boot disks beyond 2 TB.
  • I recommend shutting down the system before modifying storage.
Step 1 – Create a default instance in the Google Console for testing.

The default disk size is 10 GB.

Step 2 – Connect to the instance.

Go to the Google Console -> Compute Engine. A list of VM instances is displayed. On the line with your instance, click “SSH”. You can also use the CLI: “gcloud compute ssh <instance_name>”.

Execute the “df” command and record the output.

Step 3 – Shutdown the instance.
Step 4 – Resize the disk to 16 GB.

Go to the Google Console -> Compute Engine -> Disks. Select the disk for the instance. Click the edit button and resize the disk to 16 GB.

Step 5 – Start the instance and connect with SSH.
Step 6 – Execute the “df” command again.

Comparing the line for /dev/sda1 confirms that Debian 9 Stretch on Google Compute does automatically resize the root file system. Now I wanted to know how.

Step 7 – Get the serial port console output.

Documentation for gcloud compute instances get-serial-port-output.

Step 8 – Analyze the Console Output.

This produced about 1,000 lines. To reduce this output I used grep to search for output that is probably related to resizing the file system. Notice how I spelled “resiz”. I want the search to include “resize” and “resizing”.

This produced the following:

Step 9 – Figure out what command is used to resize the root file system.

From the console output I can see the command “expand-root.sh”. Using the command “which expand-root.sh”, I found this command located at “/usr/bin/expand-root.sh”.

Debian 9 Stretch running on Google Cloud Compute Engine does resize the root volume automatically if you resize the root disk. Very nice.

Google Cloud offers the feature to resize the disk while the VM instance is running. Can you resize the root disk and the root filesystem while the system is running? If so, how?

Step 1 – Go to the Google Console -> Compute Engine -> Disks. Select the disk for the instance.

Step 2 – Click the “CREATE SNAPSHOT” button to create a recovery snapshot.

Step 3 – Click the edit button and resize the disk to 25 GB.

Step 4 – Connect to the instance.

Step 5 – Execute this command to see the disk size and partition layout:

This command produces the following output:

Notice that the disk size is reported as “25 Gib” but the partition size is still “15G”. This means that the disk drive was resized to 25 GB but the root partition (filesystem) stayed the same size. Let’s try to resize the root filesystem while the system is running.

Automating the Resize

Now let’s take this even further and completely automate resizing a VM disk and resizing the root filesystem from the command line or a script.

Step 1 – Snapshot Disk.

First, we will snapshot the disk just in case.

Documentation for gcloud compute disks snapshot.

Step 2 – Resize Disk.

This command will resize the VM instance disk. Modify for the size you require, disk name and zone. Notice the –quiet flag. This prevents the prompt to confirm the resize.

Documentation for gcloud compute disks resize.

Step 3 – Resize Root Filesystem.

This command will use SSH to connect to the instance and execute the expand-root.sh program remotely.

Documentation for gcloud compute ssh.

Step 4 – Verify Resize.

A final “df” command on the on VM instance shows that the new root filesystem is 30 GB (30,896,016 blocks).