How to quickly increase the disk size on the server

Hi all! Recently, I encountered a seemingly simple task - to increase the “hot” disk size on a Linux server.

Task Description

There is a server in the cloud. In my case, this is Google Cloud - Compute Engine. The operating system is Ubuntu. A 30 GB disk is currently connected. The database grows, the files swell, so you need to increase the disk size, say, up to 50 GB. At the same time, we do not disable anything, we do not reboot anything.

Attention! Before you start, make a backup of all important information!

1. First, let's check how much free space we have. In the Linux console we write:

df -h

How to quickly increase the disk size on the server
In simple words, I have 30 GB in total and 7.9 GB is now free. Need to increase.

2. Then I go and connect a little more GB through the console of my hoster. In Google Cloud, this is done easily, without rebooting. I go to Compute Engine -> Disks -> I select the disk of my server - we will change the size for it:

How to quickly increase the disk size on the server
I go inside, click "Edit" and increase the size of the disk to the size I need (in my case, up to 50 GB).

3. So now we have 50 GB. Let's check it on the server with the command:

sudo fdisk -l

How to quickly increase the disk size on the server
We see our new 50 GB, but for now we can only use 30 GB.

4. Now let's delete the current 30 GB disk partition, create a new 50 GB partition. You may have multiple sections. You may also need to create a few new partitions. For this operation we will use the program Fdisk, which allows you to manage hard disk partitions. It is also important to understand what disk partitions are and what they are for - read here. To start the program Fdisk use the command:

sudo fdisk /dev/sda

5. Inside the interactive mode of the program Fdisk perform several operations.

First we drive in:

p

How to quickly increase the disk size on the server
The command prints out a list of our current partitions. In my case, one partition is 30 GB and another 20 GB is free floating, if I may say so.

6. Then we drive in:

d

How to quickly increase the disk size on the server
We delete the current partition in order to create a new one for all 50 GB. Before the operation, we check once again whether we have made a backup of important information!

7. Next, we indicate to the program:

n

How to quickly increase the disk size on the server
The command creates a new partition. All parameters should be set by default - you can just press Enter. If you have some special case, then specify your parameters. As you can see from the screenshot, I created a 50 GB partition - what I need.

8. As a result, I indicate to the program:

w

How to quickly increase the disk size on the server
This command writes the changes and exits Fdisk. We are not afraid that reading the partition table failed. The following command will fix this. Left just a little bit.

9. We left Fdisk and returned to the main Linux - a line. Next, we drive in, as we were advised earlier:

sudo partprobe /dev/sda

If everything went well, then you will not see any message. If you do not have the program installed partprobethen install it. Exactly partprobe will update the partition tables, which will allow us to expand the partition to 50 GB online. Go ahead.

Clue! Install partprobe you can like this:

 apt-get install partprobe


10. Now it remains to redefine the partition size using the program resize2fs. She will do this online - even at that moment the scripts were working for me and writing to disk.

Program resize2fs will overwrite the file system metadata. For this we use the following command:

sudo resize2fs /dev/sda1

How to quickly increase the disk size on the server
Here sda1 is the name of your partition. In most cases, this is sda1, but there may be exceptions. Be careful. As a result, the program changed the size of the partition for us. I think it's a success.

11. Now let's make sure that the size of the partition has changed and now we have 50 GB. To do this, repeat the very first command:

df -h

How to quickly increase the disk size on the server

Source: habr.com

Add a comment