This topic describes how to extend partitions and file systems on a disk of a Linux Elastic Compute Service (ECS) instance. The disk can be the system disk or a data disk.

Scenarios

When you resize a disk on an instance in the ECS console, the partitions and file systems on the disk are not extended together with the disk. For the new disk capacity to take effect, you must connect to the instance, and then extend partitions and file systems on the disk. You can extend existing partitions or create partitions based on the increased disk space.

The operations that are described in this topic are suitable for the following scenarios:
  • You have resized a disk on a Linux instance and want to extend existing partitions on the disk into the added disk space.
    Note You can create partitions based on the increased disk space instead of extending existing partitions. For more information, see Resize partitions and file systems of Linux data disks.
  • A raw disk (unpartitioned disk) is attached to a Linux instance. In this scenario, proceed to Step 2: Extend file systems.

Prerequisites

Preparations: Check disk and partition information

Before you extend partitions and file systems on the disk, check disk and partition information to determine the operations that need to be performed.

  1. Connect to the instance.
  2. Run the following command to check disk and partition information:
    fdisk -lu
    Check partition informationThe command output shown in the preceding figure indicates that the instance has two disks: the system disk /dev/vda and the data disk /dev/vdb. In the following examples, /dev/vdb is used. Take note of the following information that is displayed in the command output:
    • ①: the device name of the disk.
    • ②: indicates whether the disk is partitioned and the names of the partitions.
      • Partitioned: If the disk is partitioned, partition information is displayed in the ② section. Device indicates the partition name. In this example, the partition name is /dev/vdb1. The value 1 in the partition name indicates the partition number. You must extend partitions and file systems on the disk.
      • Unpartitioned (raw device): If the disk is a raw device that is not partitioned, no partition information is displayed in the ② section. You do not need to extend partitions. Proceed to Step 2: Extend file systems on the disk.
    • ③: the partition style of the disk.

      If the value of Disk label type is dos, it indicates the Master Boot Record (MBR) partition style. If the value of this parameter is gpt, it indicates the GUID Partition Table (GPT) partition style.

      Note The values vary based on operating systems. If the Disk label type parameter is not included in the command output, you can check the System parameter to determine the partition style of the disk. If the value of System is Linux, it indicates the MBR partition style. If the value of this parameter is GPT, it indicates the GPT partition style.
    • ④: the total size of partitions on the disk. In the preceding command output, the total size of partitions on the disk is 125829120. If the End value of the last partition (/dev/vdb1 in the preceding command output) is significantly different from the total size of partitions on the disk, the disk space is not fully occupied by the partition. In the preceding command output, the End value of /dev/vdb1 is 83886079. You must extend the partition. You can also run the lsblk command to determine the partition that needs to be extended.

Step 1: Extend partitions

This section describes how to extend a disk partition from 40 GB to 60 GB on an instance after the disk is extended to 60 GB in the ECS console. In the following examples, the /dev/vdb1 partition and an instance that runs the Alibaba Cloud Linux 2.1903 LTS 64-bit public image are used. Operations vary based on actual configurations.

  1. Optional: If the information obtained in the Preparations: Check disk and partition information section indicates that your disk is an MBR disk and is extended to more than 2 TiB in the ECS console, convert the disk into a GPT disk before you perform the next step. If the preceding condition for the operation is not met, skip this step.
  2. Install tools.
    • If the instance runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 6 or later, run the following commands.
      1. Install the growpart utility.
        type growpart || yum install -y cloud-utils-growpart
      2. (Optional) Install the gdisk tool if your disk is a GPT disk based on the information that is obtained in the Preparations: Check disk and partition information section:
        type sgdisk || yum install -y gdisk
      Note CentOS 6 and CentOS 8 reached their end of life (EOL). If your instance runs CentOS 6 or CentOS 8, change the repository addresses of the operating system. For more information, see Change the CentOS 6 source address and Change CentOS 8 repository addresses.
    • If the instance runs Debian 8 or later, or Ubuntu 14 or later, run the following commands.
      1. Update the software repository.
        apt-get update
      2. Install cloud-guest-utils.
        type growpart || apt-get install -y cloud-guest-utils
      3. (Optional) Install the gdisk tool if your disk is a GPT disk based on the information that is obtained in the Preparations: Check disk and partition information section:
        type sgdisk || apt-get install -y gdisk
  3. Extend partitions.
    In this example, the /dev/vdb1 disk partition is used. Run the following command to extend the /dev/vdb1 partition. 1 specifies the partition number. Use a space to separate /dev/vdb from 1.
    LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
    LC_ALL=en_US.UTF-8 specifies an environment variable, which ensures compatibility with Chinese character sets.
    Important
    • If a single disk contains consecutive partitions, you only need to extend the last partition. For example, assume that the data disk /dev/vdb contains three partitions named /dev/vdb1, /dev/vdb2, and /dev/vdb3. You only need to run the growpart /dev/vdb 3 command to extend the /dev/vdb3 partition.
    • If an error is reported when you use the growpart utility, fix the error. For more information, see the FAQ section of this topic.
    If CHANGED is displayed as shown in the following figure, the partition is extended. Extend partitions
  4. Optional:Run the reboot command to restart the instance if the instance runs CentOS 6 or earlier.
    Typically, you do not need to restart instances after you extend the disk partitions on the instances. If you extend a partition on the system disk of a CentOS 6 instance or if the operating system is of an early version and does not recognize the new partition size, restart the instance for the new partition size to take effect. We recommend that you restart instances during off-peak hours to ensure service continuity.

Step 2: Extend file systems

Note If you use resizefs to extend file systems when the file systems and their associated disk are being used, data issues may occur on the file systems. These issues are related to the image kernel version and resize2fs version but not to the disk resize operation. We recommend that you create snapshots for the disk to back up data beforehand. For more information, see Create a snapshot of a disk.
  1. Run the following command to check the file system type:
    df -Th

    Type indicates the file system type. Mounted on indicates the mount directory of the partition. In the command output shown in the following figure, the file system on the /dev/vdb1 partition is an ext4 file system, and the partition is mounted to /mnt.

    File system type

    If the Filesystem value does not contain a digit as a suffix, the disk is a raw device. Example: /dev/vdc.

  2. Extend file systems.

    Commands that are used to extend file systems vary based on file system types. Run one of the following commands to extend a file system based on the file system type that you obtained in the preceding step:

    • ext file system including ext2, ext3, and ext4
      • For example, if you want to extend the ext file system of the /dev/vdb1 partition, run the following command:
        resize2fs /dev/vdb1
      • For example, if you want to extend the ext file system of the /dev/vdc raw device, run the following command:
        resize2fs /dev/vdc
    • xfs file system
      For example, if you want to extend the xfs file system that is mounted to /mnt, run the following command:
      type xfs_growfs || yum install -y xfsprogs
      xfs_growfs /mnt
    • btrfs file system
      For example, if you want to extend the btrfs file system that is mounted to /mnt, run the following command:
      btrfs filesystem resize max /mnt
  3. Run the following command to check whether the file system is extended:
    df -Th

    If the file system size displayed in the command output is the same as the file system size displayed in the ECS console, the file system is extended.

    The file system size of the /dev/vdb1 partition shown in the following figure is basically the same as the file system size (60 GB) displayed in the ECS console. This indicates that the file system is extended. Extended file system
    After you extend file systems, check whether the file system sizes are the same as the specified values.
    • If the file system sizes are increased to the specified values and business applications on the instance can run as expected, the file systems are extended.
    • If the file system sizes are not increased to the specified values, use the snapshots that you created to roll back the disk. For more information, see Roll back a disk by using a snapshot.

FAQ

Note If issues occur and you are unable to resolve the issues, you can join the DingTalk group 25920014655 to contact Alibaba Cloud engineers.

FAQ about growpart

  1. Why does the unexpected output in sfdisk --version [sfdisk, from util-linux 2.23.2] error message appear when I run a growpart command to extend a partition?

    Cause: The Chinese operating system of the instance uses a character encoding type that is not en_US.UTF-8.

    Solution:
    1. Run the locale command to check the character encoding type of the instance. If the character encoding type is not en_US.UTF-8, change the type to en_US.UTF-8.
      1. Run the following command to change the character encoding type:
        export LANG=en_US.UTF-8
      2. If the problem persists, run the following command to change the character encoding type:
        export LC_ALL=en_US.UTF-8
    2. If the problem persists, run the reboot command to restart the instance.
    Important If the partition is extended after you change the character encoding type, we recommend that you switch to the original character encoding type.
  2. Why does the unexpected output in sfdisk --version [sfdisk (util-linux-ng 2.17.2)] error message appear when I run a growpart command to extend a partition?

    Cause: The fdisk version is incompatible with the growpart version.

    Solution: Downgrade growpart to version 0.27.

  3. Why does the FAILED: unable to determine partition type error message appear when I run a growpart command to extend a partition?

    Cause: The disk is a raw device and has no partitions. You do not need to extend partitions but you need to extend file systems on the disk.

    Solution: Extend file systems on the disk.

  4. Why does the WARNING: MBR/dos partitioned disk is larger than 2TB. Additional space will go unused error message appear when I run a growpart command to extend a partition?

    Cause: The MBR partition style does not support disks that are larger than 2 TiB in size.

    Solution: Convert the disk from MBR to GPT and then extend partitions on the disk. For more information, see Convert MBR partitions into GPT partitions.

  5. Why does the Error: Cannot retrieve repository metadata error message appear when I install the growpart utility on CentOS 6?

    Cause: CentOS 6 reached EOL. Software packages cannot be installed on CentOS 6 by using the default repositories.

    Solution:
    1. Change the YUM repository address for CentOS 6. For more information, see Change the CentOS 6 source address.
      Note CentOS 6 has reached EOL. To use YUM to install CentOS software packages, you must change the YUM repository address for CentOS 6.
    2. Run the following command to install the growpart utility:
      type growpart || yum install -y cloud-utils-growpart
    3. If the problem persists, perform the steps that are described in Resize partitions and file systems of Linux data disks to extend partitions.

FAQ about resize2fs

  1. Question: Why is the following information displayed when I run a resize2fs command to extend file systems?
    resize2fs: Device or resource busy while trying to open /dev/vda
    Couldn't find valid filesystem superblock.
    or
    open: Is a directory while opening /mnt

    Cause: When you run a resize2fs command to extend the file system on a partition, you must specify the partition name, instead of the device name of the disk or the mount directory of the partition.

    Solution: Specify correct partition names when you extend file systems.

  2. Question: Why does the The filesystem is already ... blocks long. Nothing to do! error message appear when I run a resize2fs command to extend a file system?

    Cause: No disk space is available for you to extend file systems. This may be caused by an ineffective partition extend operation.

    Solution: If the new partition size is not recognized by the operating system after you extend a partition, the reason may be that the operating system is of an early version. Run the reboot command to restart the instance for the new partition size to take effect.