This topic describes how to use the growpart and resize2fs tools to resize partitions and file systems of Linux system disks.

Prerequisites

Before you resize partitions and file systems of system disks, make sure that the following requirements are met:

  1. You have created snapshots to back up data.

    To prevent data loss caused by misoperations, we recommend that you create a snapshot to back up your data before you resize file systems. If you have not created a snapshot, see Create a snapshot.

  2. You have resized the data disk.

    If not, see Extend a disk online or Resize cloud disks offline.

  3. Remotely connect to an ECS instance. For more information, see Overview.
  4. The growpart or xfsprogs tool is installed based on your operating system.
    • In CentOS 7 or Aliyun Linux, run the following commands to install the tools:
      yum install cloud-utils-growpart
      yum install xfsprogs
    • In Ubuntu 14, Ubuntu 16, Ubuntu 18, or Debian 9, run the following commands to install the tools:
      apt install cloud-guest-utils
      apt install xfsprogs
    • In Debian 8, OpenSUSE 42.3, OpenSUSE 13.1, or SUSE Linux Enterprise Server 12 SP2, use an upstream version of growpart or xfsprogs.
    Note If the partition fails to be resized due to a formatting tool error, you can reinstall the tool.
  5. The kernel version of your instance is checked, such as by running the uname -a command.

Background information

The procedures in this topic apply to system disks with the following partition and file system formats:
  • Partition formats: MBR and GPT
  • File system formats: ext*, XFS, and Btrfs

Procedure for instances with kernels 3.6.0 or later

This section uses an instance running CentOS 7 as an example to describe how to resize a system disk partition and file system.

  1. Run the fdisk -l command to check the current disk size.
    The following example shows that the /dev/vda disk size is 100 GiB:
    [root@ecshost ~]# fdisk -l
    Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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
    Disk label type: dos
    Disk identifier: 0x000bad2b
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048    83886046    41941999+  83  Linux
  2. Run the df -h command to check the partition size and the file system type.
    The following example shows that the /dev/vda1 partition size is 40 GiB, and the file system type is ext4:
    [root@ecshost ~]# df -Th
    Filesystem     Type      Size  Used Avail Use% Mounted on
    devtmpfs       devtmpfs  869M     0  869M   0% /dev
    tmpfs          tmpfs     879M     0  879M   0% /dev/shm
    tmpfs          tmpfs     879M  460K  878M   1% /run
    tmpfs          tmpfs     879M     0  879M   0% /sys/fs/cgroup
    /dev/vda1      ext4       40G  1.8G   36G   5% /
    tmpfs          tmpfs     176M     0  176M   0% /run/user/0
  3. Run the growpart <DeviceName> <PartionNumber> command to resize the partition.
    The following example shows that the first partition /dev/vda1 of the system disk is resized:
    [root@ecshost ~]# growpart /dev/vda 1
    CHANGED: partition=1 start=2048 old: size=83883999 end=83886047 new: size=209713119 end=209715167
  4. Resize the file system.
    You can use one of the following methods based on your file system type:
    • If the file system is ext*, such as ext3 and ext4, run the resize2fs <PartitionName> command.
      The following example shows that the file system of the /dev/vda1 partition is resized:
      [root@ecshost ~]# resize2fs /dev/vda1
      resize2fs 1.42.9 (28-Dec-2013)
      Filesystem at /dev/vda1 is mounted on /; on-line resizing required
      old_desc_blocks = 3, new_desc_blocks = 7
      The filesystem on /dev/vda1 is now 26214139 blocks long.
    • If the file system is XFS, run the xfs_growfs <mountpoint> command.

      The following example shows that the file system of the /dev/vda1 partition is resized. The mount point of the /dev/vda1 partition is the root directory /.

      [root@ecshost ~]# xfs_growfs /
      meta-data=/dev/vda1              isize=512    agcount=13, agsize=1310656 blks
               =                       sectsz=512   attr=2, projid32bit=1
               =                       crc=1        finobt=1, sparse=1, rmapbt=0
               =                       reflink=1
      data     =                       bsize=4096   blocks=15728379, imaxpct=25
               =                       sunit=0      swidth=0 blks
      naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
      log      =internal log           bsize=4096   blocks=2560, version=2
               =                       sectsz=512   sunit=0 blks, lazy-count=1
      realtime =none                   extsz=4096   blocks=0, rtextents=0
      data blocks changed from 15728379 to 20971259
      Note The xfs_growfs commands may vary by version. Run xfs_growfs --help to check the corresponding commands.
  5. Run the df -h command to check the size of the disk partition.
    The following example shows that the /dev/vda1 partition size is 100 GiB, indicating that the partition is resized:
    [root@ecshost ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    devtmpfs        869M     0  869M   0% /dev
    tmpfs           879M     0  879M   0% /dev/shm
    tmpfs           879M  492K  878M   1% /run
    tmpfs           879M     0  879M   0% /sys/fs/cgroup
    /dev/vda1        99G  1.8G   93G   2% /
    tmpfs           176M     0  176M   0% /run/user/0

Procedure for instances with kernels earlier than 3.6.0

This section uses an instance running CentOS 6 as an example to describe how to resize a system disk partition and file system.

  1. Install the dracut-modules-growroot tool.
    [root@ecshost ~]# yum install -y dracut-modules-growroot

    If a package manager other than yum is used, change yum in the preceding command based on the package manager.

  2. Run the following command to overwrite the existing initramfs file:
    [root@ecshost ~]# dracut -f
  3. Run the fdisk -l command to check the current disk size.
    The following example shows that the /dev/vda1 disk size is 100 GiB:
    [root@ecshost ~]# fdisk -l
    Disk /dev/vda: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x0003a7b4
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *           1        2611    20970496   83  Linux
  4. Run the df -h command to check the size of the disk partition.
    The following example shows that the /dev/vda1 partition size is 20 GiB:
    [root@ecshost ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           7.8G     0  7.8G   0% /dev/shm
    /dev/vda1        20G  1.1G   18G   6% /
  5. Run the growpart <DeviceName> <PartionNumber> command to resize the partition.
    The following example shows that the first partition /dev/vda1 of the system disk is resized:
    [root@ecshost ~]# growpart /dev/vda 1
    CHANGED: partition=1 start=2048 old: size=41940992 end=41943040 new: size=209710462,end=209712510
  6. Restart the instance from the console or by calling the RebootInstance operation. For more information about the procedure, see Restart an instance and RebootInstance.
  7. Connect to the instance.
  8. Resize the file system.
    You can use one of the following methods based on your file system type:
    • If the file system is ext*, such as ext3 and ext4, run the resize2fs <PartitionName> command.
      The following example shows that the file system of the /dev/vda1 partition is resized:
      [root@ecshost ~]# resize2fs /dev/vda1
      resize2fs 1.41.12 (17-May-2010)
      Filesystem at /dev/vda1 is mounted on /; on-line resizing required
      old desc_blocks = 2, new_desc_blocks = 7
      Performing an on-line resize of /dev/vda1 to 26213807 (4k) blocks.
      The filesystem on /dev/vda1 is now 26213807 blocks long.
    • If the file system is XFS, run the xfs_growfs <mountpoint> command.

      The following example shows that the file system of the /dev/vda1 partition is resized. The mount point of the /dev/vda1 partition is the root directory /.

      [root@ecshost ~]# xfs_growfs /
      Note The xfs_growfs commands may vary by version. Run xfs_growfs --help to check the corresponding commands.
  9. Run the df -h command to check the size of the disk partition.
    The following example shows that the /dev/vda1 partition size is 100 GiB, indicating that the partition is resized:
    [root@ecshost ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        99G  1.1G   93G   2% /
    tmpfs           7.8G     0  7.8G   0% /dev/shm