How Do I Resize a Cloud Server File System?

Updated on November 20, 2023
How Do I Resize a Cloud Server File System? header image

Introduction

Upgrading a cloud server to a larger plan increases the virtual disk size, but existing partitions may not resize automatically. This article explains how to resize the file system on a Linux cloud server and expand the root partition to utilize the full disk space.

This article uses a Ubuntu 24.04 instance but the included instructions apply to any Linux distribution with the fdisk and resize2fs tools installed. The example server has a 55 GB virtual disk after upgrade with the following partitions:

  • vda: The virtual disk with 55 GB.

  • vda1: The first partition (EFI) with 512 MB.

  • vda2: The second partition (root file system) with 24.5 GB.

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
    sr0     11:0    1 1024M  0 rom  
    vda    253:0    0   55G  0 disk 
    ├─vda1 253:1    0  512M  0 part /boot/efi
    └─vda2 253:2    0 24.5G  0 part /

    The vda2 partition is 100% utilized with no free space based on the following df -h command output:

    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           197M   17M  180M   9% /run
    efivarfs        256K   25K  227K  10% /sys/firmware/efi/efivars
    /dev/vda2        23G   23G     0 100% /
    tmpfs           982M     0  982M   0% /dev/shm
    tmpfs           5.0M     0  5.0M   0% /run/lock
    /dev/vda1       511M  6.2M  505M   2% /boot/efi
    tmpfs           197M   12K  197M   1% /run/user/0

    You will expand the second partition (vda2) to fill the virtual disk while preserving data on the partition. Use this example as a general guide depending on your particular situation.

Prerequisites

Before you begin, ensure to:

Warning
Do not run the commands in this article directly in your active server session. Instead, boot the instance using a rescue ISO and apply the changes while the virtual disk is unmounted and not in use. You may lose data if you make a mistake while following the instructions in this article. Ensure to take a snapshot of your instance to create a backup you can use to recover the server in case of any failures. In addition, avoid making any changes to the vda1 EFI boot partition.

Inspect the Partition Table

  1. Run the following fdisk -l command to inspect all disks and partitions on your server.

    console
    # fdisk -l
    

    Note the start and end sectors of your target /dev/vda virtual disk partitions similar to the following output.

    Disk /dev/vda: 55 GiB, 59055800320 bytes, 115343360 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 6E38F556-1B5C-4438-B719-3782CAB8D459
    
    Device       Start      End  Sectors  Size Type
    /dev/vda1     2048  1050623  1048576  512M EFI System
    /dev/vda2  1050624 52428766 51378143 24.5G Linux filesystem
    ...........................

Remove the Second Partition

Follow the steps below to remove the vda2 partition without losing any existing data.

  1. Run the following fdisk command to manage the /dev/vda virtual disk partitions.

    console
    # fdisk /dev/vda
    

    Output:

    Welcome to fdisk (util-linux 2.34).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help):
  2. Enter i to view information about a specific partition on the /dev/vda disk.

    Command (m for help): i
  3. Enter 2 to view information about the second partition (vda2) you want to delete.

    Partition number (1,2, default 2): 2

    Note the start and end sectors in your output similar to the one below.

    
             Device: /dev/vda2
              Start: 1050624
                End: 52428766
            Sectors: 51378143
               Size: 24.5G
               Type: Linux filesystem
          Type-UUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
               UUID: 71C0247B-89D2-4590-8B81-A1089FB0154F
    
    Command (m for help): 

    1050624 is the start sector, and 52428766 is the end sector for the vda2 partition, based on the output above.

  4. Enter d to delete a partition.

    Command (m for help): d
  5. Enter 2 to delete the second partition (vda2).

    Partition number (1,2, default 2): 2

    Output:

    Partition 2 has been deleted.

Create a New Partition

Follow the steps below to create a new partition and preserve data using the same start sector while keeping the existing ext4 signature.

  1. Enter n to create a new partition.

    Command (m for help): n
  2. Enter 2 to create the second partition.

    Partition number (2-128, default 2): 2
  3. Verify that the default first sector value matches your original vda2 partition value and press Enter. If the default value is different, enter your original vda2 start sector value instead.

    First sector (1050624-115343326, default 1050624):
  4. Press Enter to use the suggested last sector value that fills the entire available disk space.

    Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-115343326, default 115341311): 

    Output:

    Created a new partition 2 of type 'Linux filesystem' and of size 54.5 GiB.
  5. Enter n when prompted to remove the existing ext4 signature and press Enter.

    Partition #2 contains a ext4 signature.
    
    Do you want to remove the signature? [Y]es/[N]o: n

Write the Changes to Disk

Follow the steps below to write and apply the partition changes you performed in the previous steps to your virtual disk.

  1. Enter w to write the partition changes to the disk.

    Command (m for help): w

    Output:

    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.
  2. Use the lsblk command to view the modified partition table of the disk.

    console
    # lsblk
    

    Output:

    .......
    vda    254:0    0    55G  0 disk 
    ├─vda1 254:1    0   512M  0 part 
    └─vda2 254:2    0  54.5G  0 part 

Resize the File System

Follow the steps below to test the file system of the second partition (vda2) and resize it to fill the new partition.

  1. Check the vda2 partition for errors and corrupted nodes.

    console
    # e2fsck -f /dev/vda2
    

    Enter y when prompted to fix any blocks on the partition similar to the output below.

    /dev/vda2: recovering journal
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    Free blocks count wrong (4096, counted=4601).
    Fix<y>? 
  2. Run the following resize2fs command to expand the file system and use the full size of the new vda2 partition.

    console
    # resize2fs /dev/vda2
    

    Output:

    resize2fs 1.47.1 (20-May-2024)
    Resizing the filesystem on /dev/vda2 to 14286336 (4k) blocks.
    The filesystem on /dev/vda2 is now 14286336 (4k) blocks long.

Remove the Rescue ISO and Reboot the Server

Access your instance's management page, remove the rescue ISO, and reboot the instance. Then, follow the steps below to test the changes made to your server's disk partitions.

  1. Run the following lsblk command in your server's terminal session to verify the new partition table.

    console
    $ lsblk
    

    Output:

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
    sr0     11:0    1 1024M  0 rom  
    vda    253:0    0   55G  0 disk 
    ├─vda1 253:1    0  512M  0 part /boot/efi
    └─vda2 253:2    0 54.5G  0 part /
  2. Use the df -h command to view the disk usage information and verify that the vda2 partition is resized.

    console
    $ df -h
    

    Your output should be similar to the below.

    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           197M  1.2M  196M   1% /run
    efivarfs        256K   34K  218K  14% /sys/firmware/efi/efivars
    /dev/vda2        52G   23G   26G  47% /
    tmpfs           982M     0  982M   0% /dev/shm
    tmpfs           5.0M     0  5.0M   0% /run/lock
    /dev/vda1       511M  6.2M  505M   2% /boot/efi
    tmpfs           197M   12K  197M   1% /run/user/0

    The vda2 partition usage is 47% as compared to 100% before resizing based on the above output.

Conclusion

You have resized your server's file system and preserved all existing data. If you added or removed partitions, adjust the mount points in the /etc/fstab file. For more information and disk resizing options, please visit the following documentation resources.