How to Increase the Size of Root Volume in Google Cloud Platform - GCP
Google cloud platform is one of the popular public cloud providers out there. It has many features like Load Balancing, Container Engine (to run docker containers in google cloud), Virtual Private Networks, Interconnecting services (to connect your in house network to cloud), SQL Services, Object storage services and many more. In this article, we will be discussing one basic and very essential activity that we all need to perform when required.
When you create a VM instance in google cloud, you generally size it according to your current need. Upfront sizing of storage for future needs that may arise, is not required in a cloud environment like google, as it can save cost. However, you still have the capability to increase the storage size as and when needed, without downtime.
In this article, lets go through the steps for increasing the root volume size of a Linux machine running in google cloud without downtime. We will be using gcloud command line utility to achieve our goal.
Please note the fact that you need to have gcloud command line utility configured properly on your system for this to work. On most of the vm machines available in the google cloud, you have gcloud utility installed by default. So i would recommend to carry out this operation from any of your instance in the cloud.
Once you have gcloud utility available, its just the matter of configuring it with a service account that has required permissions to carry out the operation. Am assuming you have a service account, and gcloud configured correctly. If you do not have, you can create a service account and ask gcloud to use the service account json credential file.
gcloud auth activate-service-account --key-file=test-project.jsonIn the above command, test-project.json is the credentials file. You will get to download this, while creating a service account with required permission. Once this is done, you need to tell gcloud to use the required project and region in your account.
gcloud config set project my-project-1234"my-project-1234" is an example in the above command, you need to replace it with your actual project ID. You can get the project ID from your google cloud web console.
So let's get started. I have an instance running in my account, which has a root volume, and has 10G of storage space available. Let's go ahead and increase it to 20G without any downtime.
My instance name is "instance-10". Lets go ahead and login to that instance and see the current root volume size using the standard df -h command. See below.
root@instance-10:~# df -h Filesystem Size Used Avail Use% Mounted on udev 1.8G 4.0K 1.8G 1% /dev tmpfs 370M 268K 370M 1% /run /dev/sda1 9.9G 2.4G 7.0G 26% / none 4.0K 0 4.0K 0% /sys/fs/cgroup none 5.0M 0 5.0M 0% /run/lock none 1.9G 0 1.9G 0% /run/shm none 100M 0 100M 0% /run/user
See the size of /dev/sda1. Its 9.9 GB. We need to increas it to 20G without downtime. The first thing that we need to do is to identify the name of the disk using gcloud utility. As this disk is attached to our instance (instance-10), we can list the properties of this instance and grab the name of the disk from the output.
So let's go ahead and list the instance properties.
$ gcloud compute instances describe instance-10 |grep disk disks: source: https://www.googleapis.com/compute/v1/projects/my-project-1234/zones/us-east1-c/disks/instance-10
I have greped for only disk from the output, as the output is quite a long yaml data. So we got our disk name. It is same as the instance name in most cases(our disk name is "instance-10").
Let's now resize it to 20G. Run the command below.
$ gcloud compute disks resize instance-10 --size 20GB This command increases disk size. This change is not reversible. For more information, see: https://cloud.google.com/sdk/gcloud/reference/compute/disks/resize Do you want to continue (Y/n)? y Updated [https://www.googleapis.com/compute/v1/projects/my-project-1234/zones/us-east1-c/disks/instance-10]. ---
Please note the fact that i have trimmed the output of the above command. It will ask for confirmation and once you press "y" and proceed, the disk size will be changed to whatever you mentioned (ie: 20G in our case).
We need to now let the operating system know that the size is changed and the root partition also needs to grow to the new size. Apart from the partition, we need to also tell the file system to use the new space available in the partition. So we now have two things to do.
- Increase the size of the partition
- Build filesystem on the newly added space
In the earlier output of df -h that we saw, the name of our root partition is /dev/sda1. Here is how naming convention works by default. /dev/sda is the disk name. /dev/sda1 is the first partition in that disk.
Our disk is resized, and operating system is well aware of this fact. How to confirm? You can simply execute fdisk -l command and see that the operating system has already detected the new disk size (see a trimmed output below.)
root@instance-10:~# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes
df -h will still show the old size for /dev/sda1 (which is our root volume), because we have yet not changed our partition size. The tool that will help us do that is called as "growpart". Growpart will extend the size of a partition to whatever you like. If you do not specify any size, it will use all the available space in our disk (/dev/sda) to increase the size of our partition (/dev/sda1).
So we are going to simply fire the below command.
root@instance-10:~# growpart /dev/sda 1 CHANGED: partition=1 start=2048 old: size=20969472 end=20971520 new: size=41940992,end=41943040
Please note the fact that there is a space between /dev/sda and 1 in the above command. /dev/sda is the disk name, and 1 is the partition number.
From the output it is quite clear that we have resized the partition.
But still...df -h will show the old size. This is because df -h is looking at the file system size, which we are yet to build on the newly added space in that partition.
The command/tool that will help us here is called as resize2fs. resize2fs can be used to resize ext2, ext3, and ext4 file systems. See below.
root@instance-10:~# resize2fs /dev/sda1 resize2fs 1.42.9 (4-Feb-2014) Filesystem at /dev/sda1 is mounted on /; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/sda1 is now 5242624 blocks long.
Now df -h should show the new size of your partition. Also you applications should be able to use the new size (all this without any sort of down time).
Please keep the fact in mind that we used resize2fs because /dev/sda1 was using ext4 file system format. If your root volume is something else other than ext2 or ext3 or ext4, you will have to use appropriate tool for that.
For example, if you are using XFS file system, then all the steps in this article is the same, except the last step. In the case of XFS, we will use xfs_growfs command instead of resize2fs. See an example below.
#xfs_growfs /dev/sda1
Comments
Thanks !
Works great ...increased my disk from 10G after no space left to 50G with no down time. Thank you for writing the article.
Add new comment