All Products
Search
Document Center

Elastic Compute Service:Extend the partitions and file systems of disks on a Linux instance

Last Updated:Jan 12, 2024

When a disk of an Elastic Compute Service (ECS) instance is resized in the ECS console, the partitions and file systems on the disk are not automatically extended together with the disk. You must manually extend partitions and file systems on the disk into the added disk space to make the new disk size take effect. This topic describes how to extend partitions and file systems on a disk of a Linux ECS instance in two steps.

Prerequisites

  • An ECS instance that runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, CentOS 6 or later, Debian 8 or later, or Ubuntu 14 or later is created.

    Note

    For information about how to extend partitions and file systems on disks of an instance that uses a different operating system, see Offline extend partitions and file systems on a data disk of a Linux instance.

  • A disk of the instance is resized in the ECS console. For more information, see Step 1: Resize a disk to extend its capacity.

  • (Recommended) A snapshot of the disk is created to back up disk data. For more information, see Create a snapshot of a disk.

    Note

    In most cases, extending partitions and file systems does not cause data loss. To prevent data loss or exceptions caused by accidental operations, we recommend that you create snapshots to back up disk data.

  • This topic is not applicable to scenarios in which the multi-attach feature is enabled and cluster file systems are used. For more information, see Enable multi-attach.

Procedure

The operations that are described in this topic are suitable for the following scenarios:

This topic also provides sample commands that you can use to extend partitions and file systems. In the examples, Master Boot Record (MBR) partitions, GUID Partition Table (GPT) partitions, and a raw disk are used.

Preparation: Obtain disk information

Before you extend partitions and file systems on the disk, obtain disk information and partition information.

Method 1: Use the CLI

  1. Connect to the instance.

    For more information, see Connect to a Linux instance by using a password or key.

  2. Run the following command to check disk information and partition information:

    fdisk -lu

    确认分区情况As shown in the preceding figure, the instance has two disks: system disk /dev/vda and data disk /dev/vdb. In this example, the data disk /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 number 1 indicates the partition number. In this case, you must extend partitions and file systems on the disk.

      • Not partitioned (raw device): If the disk is a raw device, no partition information is displayed in the ② section. In this case, skip Step 1: Extend partitions and proceed to Step 2: Extend file systems.

    • ③: the partition style of the disk.

      If the value of Disk label type is dos, the MBR partition style is used. If the value is gpt, the GPT partition style is used.

      Note
      • The values vary based on the operating system. If the Disk label type field is not displayed, check the System field to determine the partition style. If the value of System is Linux, the MBR partition style is used. If the value is GPT, the GPT partition style is used.

      • If you use an earlier version of fdisk, the GPT partition style may not be displayed. You can run the gdisk -l /dev/vdb command to check the partition style.

    • ④: indicates whether the partition needs to be extended.

      The capacity of a disk or partition is calculated by using the following formula: Number of sectors in the disk or partition × 512 bytes.

      • In the preceding figure, the number of sectors in the /dev/vdb disk is 125,829,120. The disk capacity is 64.4 GB (or 60 GiB), which is calculated by using the following formula: 125,829,120 × 512 bytes.

      • The number of sectors in the /dev/vdb1 partition is 83,886,078, which is calculated by using the following formula: Value of End - Value of Start. The partition capacity is 42.9 GB (or 40 GiB), which is calculated by using the following formula: 83,886,078 × 512 bytes.

      The capacity of the /dev/vdb1 partition is smaller than that of the /dev/vdb disk. The /dev/vdb1 partition needs to be extended.

      Note

      You can also run the lsblk command to check which partition needs to be extended.

Method 2: Use the ECS console

Important

Take note of the following items:

  • This method is applicable only to specific users. You can go to the ECS console to check whether you can use this method.

  • You must install Cloud Assistant Agent on the instance to which the disk is attached. For more information, see Install Cloud Assistant Agent.

  1. On the Cloud Disk tab of the Instance Details page, find the disk that you want to resize.

  2. Move the pointer over the ① section and then click the image icons in the ② and ③ sections to view the device name, partition style, partition name, file system type, and mount point of the disk.

    image

Step 1: Extend partitions

In this example, the instance uses an Alibaba Cloud Linux 2.1903 LTS 64-bit public image, the disk of the instance is resized to 60 GiB, and the /dev/vdb1 partition of the disk is extended from 40 GiB to 60 GiB. The example is for reference only, and you must extend partitions based on actual scenarios.

  1. (Optional) If the partition style of the disk is MBR and the disk is resized in the ECS console to larger than 2 TiB, you must first change the partition style to GPT. For information about how to check the partition style and size of the disk, see the "Preparation" section of this topic.

    For information about how to change the partition style of a disk, see Convert MBR partitions into GPT partitions.

  2. Run the following command to extend a specified partition. For example, to extend the /dev/vdb1 partition, run the following command:

    Note

    Separate /dev/vdb and 1 with a space in the command. 1 indicates the number of the partition.

    Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 6 or later

    • MBR partition style

      type growpart || yum install -y cloud-utils-growpart
      LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
    • GPT partition style (gDisk must be installed)

      type growpart || yum install -y cloud-utils-growpart
      type sgdisk || yum install -y gdisk
      LC_ALL=en_US.UTF-8 growpart /dev/vdb 1 
    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.

    Debian 8 or later, or Ubuntu 14 or later

    • MBR partition style

      apt-get update
      type growpart || apt-get install -y cloud-guest-utils
      LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
    • GPT partition style (gDisk must be installed)

      apt-get update
      type growpart || apt-get install -y cloud-guest-utils
      type sgdisk || apt-get install -y gdisk
      LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
    Important

    Take note of the following items:

    • Set the LC_ALL environment variable to en_US.UTF-8 to ensure compatibility with Chinese character sets.

    • If you want to resize a system disk, the device name of the disk is /dev/vda in most cases.

    • If a single disk has successive partitions, you can only extend the last partition. For example, the /dev/vdb data disk has three partitions named /dev/vdb1, /dev/vdb2, and /dev/vdb3. When you resize the data disk, you can only extend the last partition (/dev/vdb3). To extend the partition, run the growpart /dev/vdb 3 command.

    • If the disk is a Nonvolatile Memory Express (NVMe) disk whose device name is /dev/nvme0n1p1 and you want to extend the /dev/nvme0n1p1 partition, run the growpart /dev/nvme0n1 1 command instead of growpart /dev/nvme0n1p1 1.

    • If you want to extend a logical partition (whose partition number is larger than or equal to 5 in most cases) of an extended MBR partition, you must first extend the extended partition. Suppose that the following partitions exist: /dev/vdb1 (primary), /dev/vdb2 (extended), and /dev/vdb5 (logical). You must run the growpart /dev/vdb 2 command and then run the growpart /dev/vdb 5 command to extend /dev/vdb5.

    • If you use the growpart utility and errors are reported, see the FAQ section of this topic for troubleshooting.

    If CHANGED is displayed as shown in the following figure, the partition is extended.

    扩容分区

  3. (Optional) Run the reboot command to restart the instance for the new partition size to take effect if the instance runs CentOS 6 or earlier.

    In most cases, you do not need to restart instances after you extend the disk partitions on the instances. If you extend a partition on a 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

  1. Run the following command to check the file system type and the directory to which the file system is mounted:

    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.

    image

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

  2. Extend the file system.

    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.

    Note

    If you use resize2fs 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 information about how to create snapshots, see Create a snapshot of a disk.

    ext file system including ext2, ext3, and ext4

    • For example, to extend the ext file system of the /dev/vdb1 partition, run the following command:

      resize2fs /dev/vdb1
    • For example, to extend the ext file system of the /dev/vdc raw device, run the following command:

      resize2fs /dev/vdc

    XFS file system

    For example, 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, 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 (59 GiB) of the /dev/vdb1 partition shown in the following figure is basically the same as the file system size (60 GiB) displayed in the ECS console. This indicates that the file system is extended.扩容效果

    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.

Examples

This section provides sample commands that you can use to extend partitions and file systems. In the examples, MBR and GPT partitions and a raw disk are used. The commands are for reference only. The actual commands may vary based on your environment.

Example 1: Extend an MBR partition and the ext4 file system of the partition

  • Device name: /dev/vda (system disk)

  • Partition: /dev/vda1

  • File system type: ext4

  • Operating system: Alibaba Cloud Linux 2

Run the following commands to extend the /dev/vda1 partition and the ext4 file system of the /dev/vda1 partition:

type growpart || yum install -y cloud-utils-growpart
LC_ALL=en_US.UTF-8 growpart /dev/vda 1
resize2fs /dev/vda1

Example 2: Extend a GPT partition and the XFS file system of the partition

  • Device name: /dev/vdb (data disk)

  • Two partitions: /dev/vdb1 and /dev/vdb2

  • File system type: XFS

  • /dev/vdb2 mount directory: /mnt

  • Operating system: Debian 8

Run the following commands to extend the /dev/vdb 2 partition and the XFS file system of the /dev/vdb 2 partition:

apt-get update
type growpart || apt-get install -y cloud-guest-utils
type sgdisk || yum install -y gdisk
LC_ALL=en_US.UTF-8 growpart /dev/vdb 2
type xfs_growfs || yum install -y xfsprogs
xfs_growfs /mnt

Example 3: Extend the file system of a raw data disk

  • Device name: /dev/vdc (raw data disk that is not partitioned)

  • File system type: ext4

  • Operating system: CentOS 7

Run the following command to extend the ext4 file system of the /dev/vdc disk:

resize2fs /dev/vdc

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. Question: 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, switch it 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 issue persists, run the following command to change the character encoding type:

        export LC_ALL=en_US.UTF-8
    2. If the error 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 back to the original character encoding type.

  2. Question: 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. If the issue persists, perform the steps described in the Offline extend partitions and file systems on a data disk of a Linux instance topic to extend the partition.

  3. Question: 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 that is not partitioned. You do not need to extend partitions but you need to extend file systems on the disk.

    Solution: Perform the operations described in the "Step 2: Extend file systems" section of this topic.

  4. Question: 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. Question: Why does the mkdir: cannot create directory ... No space left on device error message appear when I run a growpart command to extend a partition?

    Cause: The disk space is insufficient and the growpart command cannot create a temporary directory.

    Solution: Delete files that you no longer need and try again.

  6. Question: 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. CentOS 6 packages cannot be installed 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 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 issue persists, perform the steps that are described in Offline extend partitions and file systems on a data disk of a Linux instance to extend partitions.

  7. Question: Why does a partition of a device whose name starts with /dev/mapper fail to be extended?

    Cause: Typically, devices whose name starts with /dev/mapper are managed by Logical Volume Manager (LVM).

    Solution: You must extend the logical volume (LV) by using LVM and then extend the file system. For more information, see Extend an LV.

  8. Question: Why does the NOCHANGE: partition 1 could only be grown by 2015 [fudge=2048] error message appear when I run a growpart command to extend a partition?

    Cause: No space is available when you extend the partition. In most cases, this is because you did not restart the instance in the ECS console or by calling an API operation to make the new disk size take effect after you resize the disk offline in the ECS console.

    Solution: After you resize the disk offline, you must restart the instance in the ECS console or by calling an API operation to make the new disk size take effect. If you run the reboot command to restart the instance from within the instance, the new disk size does not take effect. For more information, see Restart an instance and RebootInstance.

FAQ about resize2fs

  1. Question: Why is the following information displayed when I run a resize2fs command to extend a file system?

    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 the file system. This may be caused by an ineffective partition extend operation.

    Solution:

    1. Check whether a growpart command is run to extend the partition. If not, run a growpart command to extend the partition.

    2. If a growpart command is run to extend the partition, CHANGED is displayed, and the preceding error message is still returned when you run a resize2fs command, the operating system of the instance may be of an earlier version that does not allow system partition tables to be updated online. Run the reboot command to restart the instance and then run a resize2fs command.

  3. Question: Why does the resize2fs: Permission denied to resize filesystem error message appear when I run a resize2fs command to extend a file system?

    Cause: File system inconsistency may occur.

    Solution:

    1. Run the dmesg -T | grep fs command. If an error message similar to There are errors in the filesystem is displayed in the command output, file system inconsistency occurs.

    2. Back up data for the disk. For more information, see Create a snapshot of a disk.

    3. On the premise that business is not affected, run the umount command to unmount the mounted partition or the entire disk based on the actual conditions.

    4. Run the e2fsck command to resolve the issue. For information about how to use the e2fsck command, see the relevant documentation.