How to increase compute engine ubuntu VM disk capacity without restarting it?

How to increase compute engine ubuntu VM disk capacity without restarting it?

Have you ever run out of space on your computer and felt frustrated because you couldn't save any more files? The same thing can happen to virtual machines (VMs) that you create on cloud platforms like Google Cloud Platform (GCP). Fortunately, you can easily increase the disk capacity of your compute engine VM running Ubuntu without restarting it. In this blog post, we'll explain how to do it in a way that even a 6-year-old can understand.

First, let's start with some background. When you create a VM in GCP, you need to specify the amount of disk space you want it to have. This disk space is used to store your operating system, files, and applications. If you find that you're running out of disk space, you can increase the size of the disk from the disk section of the compute engine. Here's how you do it:

  1. Go to the GCP Console and select your project.

  2. Navigate to the Compute Engine section and select Disks.

  3. Find the disk that you want to resize and click on its name.

  4. Click Edit at the top of the page.

  5. Increase the size of the disk and click Save.

Once you've increased the size of your disk, you need to let your VM know about it. This involves logging into the root of the compute engine and running some commands. Don't worry if you don't understand what these commands do - just follow along and everything will work out fine.

  1. SSH into your compute engine VM from your terminal or command prompt.

  2. Run the following command to check the current disk space usage:

df -h

This will show you the current disk usage on your VM.

  1. Run the following command to show the current block devices attached to your VM:
lsblk

This will show you the current block devices attached to your VM.

  1. Run the following command to resize the root partition:
sudo growpart /dev/sda 1

This command will resize the root partition to fill up the extra space that you allocated in the disk section of the compute engine.

  1. Run the following command to resize the file system:
sudo resize2fs /dev/sda1

This command will resize the file system to use the newly allocated disk space.

  1. Finally, run the following command to check the new disk space usage:
df -h

This will show you the updated disk usage on your VM.

That's it! You've successfully increased the disk capacity of your compute engine VM without restarting it. If you need to restart your VM for any reason, you can use the following command:

sudo reboot

In conclusion, increasing the disk capacity of your compute engine Ubuntu VM is an easy process that you can do without any downtime. By following the steps outlined in this blog post, you can ensure that you never run out of disk space and can continue to use your VM without interruption. If you have any questions or run into any issues, don't hesitate to consult the GCP documentation or reach out to their support team for assistance.