Help Center> Dedicated Enterprise Storage Service> User Guide> FAQs> Performing Post-Expansion Operations for a Linux Disk (fdisk)
Updated on 2022-02-22 GMT+08:00

Performing Post-Expansion Operations for a Linux Disk (fdisk)

After the capacity expansion has succeeded, you need to allocate the extended space to an existing partition, or allocate new partitions to the expanded disks.

Prerequisites

  • You have logged in to the BMS. For details, see section "Logging In to a Linux BMS" in Bare Metal Server User Guide.
  • You have attached the EVS disk to the BMS and the additional disk space remains to be allocated.

Context

The OS used in the following example is CentOS 7.0 (64-bit).

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 partition (services will not be interrupted)

    You can create a partition for the expanded disk without the need of detaching the original partitions. The impacts on services are smaller than expanding 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, the disk capacity cannot exceed 2 TB and the number of partitions cannot exceed the upper limit after the expansion completes.

  • Expand an existing partition (services will be interrupted)

    If the MBR partition style is used and the number of partitions has reached the upper limit, the existing partition needs to be expanded. Expanding an existing partition does not delete its data, but requires the partition to unmount. Therefore, the services will be interrupted.

    If the MBR partition style is used and the disk capacity after expansion has exceeded 2 TB, the excessive disk space cannot be partitioned. In this case, if you change the partition style from MBR to GPT, the original disk data will be cleared. Therefore, back up the disk data before you change the partition style.

Viewing the Partition Style

Before allocating the additional space, query the current disk partition style. If MBR is used, you can use either the fdisk or Parted partitioning tool. If GPT is used, only the Parted partitioning tool can be used.

  1. Run the following command to view the current disk partition style:

    fdisk -l

    Information similar to the following is displayed:
    [root@ecs-1120 linux]# fdisk -l
    
    Disk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c5712
    
        Device Boot      Start         End      Blocks   Id  System
    /dev/xvda1            2048    83886079    41942016   83  Linux
    WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
    
    Disk /dev/xvdb: 161.1 GB, 161061273600 bytes, 314572800 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: gpt
    
    
    #         Start          End    Size  Type            Name
     1           34    209715166    100G  Microsoft basic opt
     2    209715167    314572766     50G  Microsoft basic opt1
    WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
    
    Disk /dev/xvdc: 42.9 GB, 42949672960 bytes, 83886080 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: gpt
    
    
    #         Start          End    Size  Type            Name
     1           34     16777215      8G  Microsoft basic opt
     2     16777216     83884031     32G  Microsoft basic opt

    In the command output, parameter Disk label type indicates the disk partition style. Value dos indicates the MBR partition style, and value gpt indicates the GPT partition style.

Creating a Partition

The following example shows you how to create a partition for the expanded space of the system disk and mount the partition to /opt without interrupting services.

  1. Run the following command to view information about partitioning:

    fdisk -l

    Information similar to the following is displayed: (/dev/xvda is the system disk.)
    [root@ecs-bab9 test]# fdisk -l
    
    Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000cc4ad
    
        Device Boot      Start         End      Blocks   Id  System
    /dev/xvda1   *        2048     2050047     1024000   83  Linux
    /dev/xvda2         2050048    22530047    10240000   83  Linux
    /dev/xvda3        22530048    24578047     1024000   83  Linux
    /dev/xvda4        24578048    83886079    29654016    5  Extended
    /dev/xvda5        24580096    26628095     1024000   82  Linux swap / Solaris

  2. Run the following command to enter fdisk (/dev/xvda is used in this example):

    fdisk /dev/xvda

    Information similar to the following is displayed:
    [root@ecs-bab9 test]# fdisk /dev/xvda
    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 partition.

    Because the system disk has five existing partitions, the system automatically creates the sixth one.

    Information similar to the following is displayed:
    Command (m for help): n
    All primary partitions are in use
    Adding logical partition 6
    First sector (26630144-83886079, default 26630144): 

  4. Enter the new partition's first sector number, for example the default value, and press Enter.

    The serial number of first sector must be greater than that of the last sector of the existing partition.

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

  5. Enter the new partition's last sector number and press Enter.

    The default last sector is used in this example.

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

  6. Enter p and press Enter to view the created partition.

    Information similar to the following is displayed:
    Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000cc4ad
    
        Device Boot      Start         End      Blocks   Id  System
    /dev/xvda1   *        2048     2050047     1024000   83  Linux
    /dev/xvda2         2050048    22530047    10240000   83  Linux
    /dev/xvda3        22530048    24578047     1024000   83  Linux
    /dev/xvda4        24578048    83886079    29654016    5  Extended
    /dev/xvda5        24580096    26628095     1024000   82  Linux swap / Solaris
    /dev/xvda6        26630144    83886079    28627968   83  Linux
    
    Command (m for help): 

  7. Enter w and press Enter to write the changes into 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 successfully created.

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

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

    partprobe

  9. Run the following command to set the file system format of the new partition:

    In this example, the ext4 file system is used.

    mkfs -t ext4 /dev/xvda6

    Information similar to the following is displayed:
    [root@ecs-bab9 test]# mkfs -t ext4 /dev/xvda6
    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
    1790544 inodes, 7156992 blocks
    357849 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2155872256
    219 block groups
    32768 blocks per group, 32768 fragments per group
    8176 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
            4096000
    
    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. When done is displayed in the command output, the formatting is complete.

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

    mount /dev/xvda6 /opt

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

    If the new partition is mounted to a directory that is not empty, the subdirectories and files in the directory will be hidden. In this situation, you are advised to mount the new partition to an empty directory or a new directory. If the new partition must be mounted to 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.

  11. Run the following command to check the mounting result:

    df -TH

    Information similar to the following is displayed:
    [root@ecs-bab9 test]# df -TH
    Filesystem     Type      Size  Used Avail Use% Mounted on
    /dev/xvda2     xfs        11G  7.4G  3.2G  71% /
    devtmpfs       devtmpfs  4.1G     0  4.1G   0% /dev
    tmpfs          tmpfs     4.1G   82k  4.1G   1% /dev/shm
    tmpfs          tmpfs     4.1G  9.2M  4.1G   1% /run
    tmpfs          tmpfs     4.1G     0  4.1G   0% /sys/fs/cgroup
    /dev/xvda3     xfs       1.1G   39M  1.1G   4% /home
    /dev/xvda1     xfs       1.1G  131M  915M  13% /boot
    /dev/xvda6     ext4       29G   47M   28G   1% /opt

Replacing the Existing Partition

In the following example, a disk has been attached to the BMS, and the partition /dev/xvdb1 has been mounted to /mnt/sdc. During the process of replacing the original partition /dev/xvdb1 and adding new capacity to it, services will be interrupted.

After the disk capacity has been expanded, the additional space is added to the end of the disk. When the disk has multiple partitions, only the partition at the end of the disk can be expanded.

  1. Run the following command to view information about partitioning:

    fdisk -l

    Information similar to the following is displayed:
    [root@ecs-b656 test]# fdisk -l
    
    Disk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000cc4ad
    
        Device Boot      Start         End      Blocks   Id  System
    /dev/xvda1   *        2048     2050047     1024000   83  Linux
    /dev/xvda2         2050048    22530047    10240000   83  Linux
    /dev/xvda3        22530048    24578047     1024000   83  Linux
    /dev/xvda4        24578048    83886079    29654016    5  Extended
    /dev/xvda5        24580096    26628095     1024000   82  Linux swap / Solaris
    
    Disk /dev/xvdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xb00005bd
    
        Device Boot      Start         End      Blocks   Id  System
    /dev/xvdb1            2048    20971519    10484736   83  Linux

    In the command output, the total capacity of the /dev/xvdb disk is 21.5 GB, and the disk has only one partition, dev/xvdb1. The partition's first sector is 2048, and its last sector is 20971519.

    View the /dev/xvdb capacity and check whether the additional space is included.
    • If the additional space is not included, refresh the capacity according to section Performing Post-Expansion Operations for a SLES Disk (fdisk).
    • If the additional space is included, take note of the first and last sectors of the dev/xvdb1 partition and then go to 2. These values will be used in the follow-up operations.

  2. Run the following command to unmount the disk partition:

    umount /mnt/sdc

  3. Run the following command to enter fdisk and enter d to delete the /dev/xvdb1 partition:

    fdisk /dev/xvdb

    The command output is as follows:
    [root@ecs-b656 test]# fdisk /dev/xvdb
    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): d
    Selected partition 1
    Partition 1 is deleted
    
    Command (m for help):

    If the partitions are recreated according to the following steps, deleting partitions will not cause data loss on the data disk.

  4. Enter n and press Enter to create a partition.

    To create a partition, enter n.

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

  5. Ensure that the entered partition type is the same as the original partition type, which is the primary partition in this example. Enter p and press Enter to create a primary partition.

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

    In the command output, Partition number specifies the serial number of the primary partition.

  6. Ensure that the serial number you entered is the same as the original partition's, which is 1 in this example. Enter 1 and press Enter.

    Information similar to the following is displayed:
    Partition number (1-4, default 1): 1
    First sector (2048-41943039, default 2048):

    In the command output, First sector specifies the first sector.

    Data will be lost if:
    • The first sector you selected is inconsistent with the original partition.
    • The last sector you selected is smaller than that of the original partition.

  7. Ensure that the entered first sector is the same as that of the original partition. In this example, first sector 2048 is recorded in 1. Enter 2048 and press Enter.

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

    In the command output, Last sector specifies the last sector.

  8. Ensure that the entered last sector is equal to or larger than the last sector recorded in 1. In this example, the recorded last sector is 20971519, and the default last sector 41943039 is used. Enter 41943039 and press Enter.

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

    The primary partition is successfully created.

  9. Enter p and press Enter to view the details about the created partition.

    Information similar to the following is displayed:
    Command (m for help): p
    
    Disk /dev/xvdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xb00005bd
    
        Device Boot      Start         End      Blocks   Id  System
    /dev/xvdb1            2048    41943039    20970496   83  Linux
    
    Command (m for help): 

    Details about the /dev/xvdb1 partition are displayed.

  10. Enter w and press Enter to write the changes into 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.
    Syncing disks.

    The partition is successfully created.

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

  11. Run the following command to check the correctness of the file system on /dev/xvdb1:

    e2fsck -f /dev/xvdb1

    Information similar to the following is displayed:
    [root@ecs-b656 test]# e2fsck -f /dev/xvdb1
    e2fsck 1.42.9 (28-Dec-2013)
    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
    /dev/xvdb1: 11/655360 files (0.0% non-contiguous), 83137/2621184 blocks

  12. Run the following command to expand the size of the file system on /dev/xvdb1:

    resize2fs /dev/xvdb1

    Information similar to the following is displayed:
    [root@ecs-b656 test]# resize2fs /dev/xvdb1
    resize2fs 1.42.9 (28-Dec-2013)
    Resizing the filesystem on /dev/xvdb1 to 5242624 (4k) blocks.
    The filesystem on /dev/xvdb1 is now 5242624 blocks long.

  13. Run the following command to mount the created partition to the /mnt/sdc directory:

    mount /dev/xvdb1 /mnt/sdc

  14. Run the following command to view the mounting result of /dev/xvdb2:

    df -TH