📜 ⬆️ ⬇️

How to quickly increase the size of the disk partition on the server

Hello! Recently, I faced a simple at first glance 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, the ext4 file system (suitable for all ext). A 30 GB disk is now connected. The base grows, files swell, so you need to increase the size of the disk, say, up to 50 GB. At the same time, we do not disconnect anything, do not reload anything.

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

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


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 host. In Google Cloud, this is done easily, without rebooting. I go to Compute Engine -> Disks -> I select my server disk - and I will change its size:


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. Check it on the server with the command:

 sudo fdisk -l 

image
We see our new 50 GB, but for now we can only use 30 GB.

4. Now we will delete the current 30 GB disk partition, create a new one with 50 GB. You may have several sections. You may need to create several new sections. For this operation, we will use the fdisk program, which allows you to manage hard disk partitions. It is also important to understand what disk partitions are and why they are needed - read here . To run the fdisk program use the command:

 sudo fdisk /dev/sda 

5. Inside the interactive mode of the fdisk program, we perform several operations.

First we drive in:

 p 

image
The team lists our current sections. In my case, one section of 30 GB and another 20 GB in free floating, if I may say so.

6. Then we drive in:

 d 

image
Delete the current partition in order to create a new one for all 50 GB. Before the operation we check once again whether we backed up important information!

7. Next, indicate the program:

 n 

image
The team creates a new section. All parameters should be set by default - you can simply press Enter. If you have a special case, then specify your options. As you can see from the screenshot, I created a 50 GB partition - what I need.

8. In the end, I specify the program:

 w 

image
This command writes changes and exits fdisk . We are not afraid that the reading of the partition table failed. The following command will help fix this. Left just a little bit.

9. We quit fdisk and returned to the main Linux 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 partprobe program installed , then install it. It is partprobe that will update the partition tables, which will allow us to expand the partition to 50 GB online. Go ahead.

Hint! You can install partprobe like this:

  apt-get install partprobe 

10. Now it remains to redefine the size of the partition using the resize2fs program. She will do it online - even at that moment I had scripts working and writing to disk.

The resize2fs program will overwrite the file system metadata. To do this, use the following command:

 sudo resize2fs /dev/sda1 

image
Here sda1 is the name of your partition. In most cases, this is sda1, but exceptions are possible. Be careful. As a result, the program has changed the size of the section. I think this is 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 

image

Source: https://habr.com/ru/post/450444/


All Articles