Help Center/ Dedicated Distributed Storage Service/ User Guide/ Disk Management/ Expanding the Capacity of a Disk/ Extending Partitions and File Systems of a System Disk on Linux (fdisk)
Updated on 2023-08-04 GMT+08:00

Extending Partitions and File Systems of a System Disk on Linux (fdisk)

Scenarios

In Linux, after the capacity expansion succeeded, the additional disk space needs to be allocated to an existing partition or a new partition.

This topic uses CentOS 7.4 64bit to describe how to allocate the additional system disk space to a partition using fdisk.

This document also describes how to create new partitions for system disks. See the following topics for more information:

Currently, a disk has been attached to a server, and the original disk capacity is 40 GB. In addition, a 40-GB space has been added to the disk on the management console, and the total disk capacity should be 80 GB. The following operations guide you to use the additional space and extend the partition.

The method for allocating the additional space varies depending on the server OS. This document is used for reference only. For the detailed operations and differences, see the corresponding OS documents.

Based on your service requirements and disk condition, you can choose either of the following ways to allocate the additional disk space:
  • Create a new partition (services will not be interrupted).

    Creating a new partition after expansion does not require the original partitions to be unmounted. Therefore, the impacts on services are minor than re-creating an existing partition. This method is recommended for system disks or disks carrying services that cannot be interrupted.

    If the MBR partition style is used, ensure that the disk capacity does not exceed 2 TB and the number of partitions does not reach the upper limit after the expansion.

  • Re-create an existing partition (services will be interrupted).

    If the MBR partition style is used and the number of partitions has reached the upper limit, you can only allocate the additional space to an existing partition. Re-creating an existing partition does not delete its data, but requires the partition to unmount. Therefore, services will be interrupted.

    If the MBR partition style is used and the disk capacity after expansion will exceed 2 TB, the space exceeding 2 TB cannot be used. To make use of that space, change the disk partition style from MBR to GPT. Data on the disk will be cleared during such a change. Therefore, back up the disk data before changing the partition style.

    Performing the expansion operations with caution. Misoperation may lead to data loss or exceptions. Therefore, you are advised to use CBR to back up the disk data before expansion. For details, see Managing Backups.

Prerequisites

  • You have logged in to the server.
    • For how to log in to an ECS, see the Elastic Cloud Server User Guide.
    • For how to log in to a BMS, see the Bare Metal Server User Guide.
  • You have attached the disk to the server, and the additional space has not been allocated.

Creating a New Partition

The following example shows you how to make use of the additional capacity of a system disk attached to a server by creating a new partition. The system disk already has the /dev/vda1 partition, which is mounted on /. In the following operations, new partition /dev/vda2 will be created and mounted on /opt. During this process, services are not interrupted.

  1. Run the following command to view the disk partition information:

    fdisk -l

    Information similar to the following is displayed:
    [root@ecs-2220 ~]# fdisk -l
    
    Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x0008d18f
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048    83886079    41942016   83  Linux

    In the command output, the capacity of the dev/vda system disk is 80 GB, in which the in-use dev/vda1 partition takes 40 GB and the additional 40 GB has not been allocated.

  2. Run the following command to enter fdisk:

    fdisk /dev/vda

    Information similar to the following is displayed:
    [root@ecs-2220 ~]# fdisk /dev/vda
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help): 

  3. Enter n and press Enter to create a new partition.

    Information similar to the following is displayed:
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    There are two types of disk partitions:
    • Choosing p creates a primary partition.
    • Choosing e creates an extended partition.

  4. In this example, a primary partition is created. Therefore, enter p and press Enter to create a primary partition.

    Information similar to the following is displayed:
    Select (default p): p
    Partition number (2-4, default 2): 

  5. Enter the serial number of the primary partition and press Enter. Partition number 2 is used in this example. Therefore, enter 2 and press Enter.

    Information similar to the following is displayed:
    Partition number (2-4, default 2): 2
    First sector (83886080-167772159, default 83886080):

  6. Enter the new partition's start sector, for example the default value, and press Enter.

    The default start sector is used in this example.

    Information similar to the following is displayed:
    First sector (83886080-167772159, default 83886080):
    Using default value 83886080
    Last sector, +sectors or +size{K,M,G} (83886080-167772159,default 167772159):

  7. Enter the new partition's end sector and press Enter.

    The default end sector is used in this example.

    Information similar to the following is displayed:
    Last sector, +sectors or +size{K,M,G} (83886080-167772159,
    default 167772159):
    Using default value 167772159
    Partition 2 of type Linux and of size 40 GiB is set
    
    Command (m for help): 

  8. Enter p and press Enter to view the new partition.

    Information similar to the following is displayed:
    Command (m for help): p
    
    Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x0008d18f
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048    83886079    41942016   83  Linux
    /dev/vda2        83886080   167772159    41943040   83  Linux
    Command (m for help): 

  9. Enter w and press Enter to write the changes to the partition table.

    Information similar to the following is displayed:
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    
    WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    The kernel still uses the old table. The new table will be used at
    the next reboot or after you run partprobe(8) or kpartx(8)
    Syncing disks.

    The partition is created.

    In case that you want to discard the changes made before, you can exit fdisk by entering q.

  10. Run the following command to synchronize the new partition table to the OS:

    partprobe

  11. Run the following command to set the file system format for the new partition:

    (The ext4 file system is used in this example.)

    mkfs -t ext4 /dev/vda2

    The procedure for setting the xfs file system is the same as that for the ext3 or ext4 file system. The command for creating the xfs file system is mkfs -t xfs /dev/vda2.

    Information similar to the following is displayed:
    [root@ecs-2220 ~]# mkfs -t ext4 /dev/vda2
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    2621440 inodes, 10485760 blocks
    524288 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2157969408
    320 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
            4096000, 7962624
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    The formatting takes a while, and you need to observe the system running status. Once done is displayed in the command output, the formatting is complete.

  12. Run the following command to mount the new partition on a space-demanding directory, for example /opt:

    mount /dev/vda2 /opt

    Information similar to the following is displayed:
    [root@ecs-bab9 test]# mount /dev/vda2 /opt
    [root@ecs-bab9 test]#

    If the new partition is mounted on a directory that is not empty, the subdirectories and files in the directory will be hidden. Therefore, you are advised to mount the new partition on an empty directory or a new directory. If the new partition must be mounted on a directory that is not empty, move the subdirectories and files in this directory to another directory temporarily. After the partition is successfully mounted, move the subdirectories and files back.

  13. Run the following command to view the mount result:

    df -TH

    Information similar to the following is displayed:
    [root@ecs-2220 ~]# df -TH
    Filesystem     Type      Size  Used Avail Use% Mounted on
    /dev/vda1      ext4       43G  2.0G   39G   5% /
    devtmpfs       devtmpfs  509M     0  509M   0% /dev
    tmpfs          tmpfs     520M     0  520M   0% /dev/shm
    tmpfs          tmpfs     520M  7.2M  513M   2% /run
    tmpfs          tmpfs     520M     0  520M   0% /sys/fs/cgroup
    tmpfs          tmpfs     104M     0  104M   0% /run/user/0
    /dev/vda2      ext4       43G   51M   40G   1% /opt

Setting Automatic Mounting at System Start

To automatically mount partitions at system starts, do not specify partitions, for example, /dev/xvdb1, in /etc/fstab because the sequence of cloud devices, and therefore their names may change during the server stop or start. You are advised to use the UUID in /etc/fstab to set automatic mounting at system start.

UUID is the unique character string for disk partitions in a Linux system.

  1. Run the following command to query the partition UUID:

    blkid Disk partition

    For example, run the following command to query the UUID of the /dev/xvdb1 partition:

    blkid /dev/xvdb1

    Information similar to the following is displayed:

    [root@ecs-b656 test]# blkid /dev/xvdb1
    /dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4"

    The UUID of the /dev/xvdb1 partition is displayed.

  2. Run the following command to open the fstab file using the vi editor:

    vi /etc/fstab

  3. Press i to enter the editing mode.
  4. Move the cursor to the end of the file and press Enter. Then, add the following information:

    UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc      ext3 defaults     0   2
    UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc      ext4 defaults     0   2
    The preceding content is used for reference only. Add the information that is used in the environment. The parameters are described as follows:
    • The first column indicates the partition UUID obtained in 1.
    • The second column indicates the directory on which the partition is mounted. You can query the mount point using the df -TH command.
    • The third column indicates the file system format of the partition. You can query the file system format using the df -TH command.
    • The fourth column indicates the partition mount option. Normally, this parameter is set to defaults.
    • The fifth column indicates the Linux dump backup option.
      • 0: not use Linux dump backup. Normally, dump backup is not used, and you can set this parameter to 0.
      • 1: use Linux dump backup.
    • The sixth column indicates the fsck option, that is, whether to use fsck to check the attached disk during startup.
      • 0: not use fsck.
      • If the mount point is the root partition (/), this parameter must be set to 1.

        When this parameter is set to 1 for the root partition, this parameter for other partitions must start with 2 so that the system checks the partitions in the ascending order of the values.

  5. Press Esc, enter :wq, and press Enter.

    The system saves the configurations and exits the vi editor.