This topic describes how to create a single partition on a data disk attached to an Elastic Compute Service (ECS) instance that runs the Linux system and mount a file system onto the partition. You can create multiple partitions for a data disk based on your business needs.

Prerequisites

Before formatting a data disk for an ECS instance that runs the Linux system, you must make sure that you meet the following prerequisites:
  • Data disks that you have purchased separately are attached to ECS instances. If data disks are purchased with ECS instances, skip this operation. For more information about how to attach data disks to ECS instances, see Attach a cloud disk.
  • The device name of the data disk is obtained. You can view the device name of the data disk on the Disks page in the ECS console by choosing More > Modify Disk Property.
    Note By default, device names are assigned by the system. The device names for I/O optimized instances start from /dev/vdb to /dev/vdz. If the device name is dev/xvd* where * indicates a lowercase letter, the instance in use is not I/O optimized.

Background information

The following procedure only applies to data disks with a size of less than 2 TiB. For more information about instructions for data disks with a size of greater than 2 TiB, see Partition and format data disks larger than 2 TiB.
This example uses a new 20 GiB data disk with a device name of /dev/vdb attached to an I/O optimized instance that runs the CentOS 7.6 operating system. A single Master Boot Record (MBR) partition is created and the ext4 file system is mounted onto the partition.
Note You can also use the GUID Partition Table (GPT) format. For more information, see Partition and format data disks larger than 2 TiB.
The following section describes the risks of formatting:
  • Disk partitioning and formatting are high-risk operations. Proceed with caution. This topic describes how to format a new data disk. If you have stored data on the data disk, you must create a snapshot for the data disk to avoid data loss. For more information about how to create a snapshot, see Create a snapshot.
  • You can only partition data disks attached to ECS instances. You cannot perform this operation on the system disk. If you forcibly use a third-party tool to partition the system disk, unknown risks such as system failure and data loss may occur. You are only allowed to extend partitions or add new partitions for the system disk that has been expanded. For more information, see Resize partitions and file systems of Linux system disks.

Procedure

  1. Connect to the ECS instance. For more information, see Connect to an ECS instance.
  2. Run the fdisk -l command to view all data disks attached to the ECS instance.
    Note If /dev/vdb is not displayed in the command results, the ECS instance does not have a data disk. Check whether the data disk is attached to the ECS instance.
  3. Run the following commands to create a single partition for the data disk.
    1. Run the fdisk -u /dev/vdb command to partition the data disk.
    2. Enter p to view the partition information of the data disk. In this example, the data disk is not partitioned.
    3. Enter n to create a new partition.
    4. Enter p to set the partition as the primary partition.
      Note In this example, you only need to create a primary partition for the data disk. If you want to create more than four partitions, enter e(extended) to create at least one extended partition.
    5. Enter the partition number and press Enter. In this example, you can enter 1 to create one partition.
    6. Enter the number of the first available sector, or press Enter to use the default value 2048.
    7. Enter the number of the last sector. In this example, press Enter to use the default value.
    8. Enter p to view the intended partitions of the data disk.
    9. Enter w to start partitioning and exit after partitioning is complete.
    [root@ecshost~ ]# fdisk -u /dev/vdb
    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.
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0x3e60020e.
    
    Command (m for help): p
    Disk /dev/vdb: 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: 0x3e60020e
    Device Boot Start End Blocks Id System
    
    Command (m for help): n
    Partition type:
    p primary (0 primary, 0 extended, 4 free)
    e extended
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-41943039, default 2048):
    Using default value 2048
    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): p
    
    Disk /dev/vdb: 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: 0x3e60020e
    Device Boot Start End Blocks Id System
    /dev/vdb1 2048 41943039 20970496 83 Linux
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
  4. Run the fdisk -u /dev/vdb command to view the new partition.
    If the following information appears, the new partition has been created.
    [root@ecshost~ ]# fdisk -lu /dev/vdb
    
    Disk /dev/vdb: 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: 0x3e60020e
    
    Device Boot Start End Blocks Id System
    /dev/vdb1 2048 41943039 20970496 83 Linux
  5. Run the mkfs.ext4/dev/vdb1 command to create a file system in the new partition.

    In this example, an ext4 file system is created. You can also create other file systems based on your needs. For example, if you want to share files between the Linux, Windows, and Mac systems, you can run the mkfs.vfat command to create a Virtual File Allocation Table (VFAT) file system.

    Note The time required to create a file system depends on the size of the data disk.
    [root@ecshost~ ]# mkfs.ext4 /dev/vdb1
    
    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
    1310720 inodes, 5242624 blocks
    262131 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2153775104
    160 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
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
  6. Optional: Run the cp /etc/fstab /etc/fstab.bak command to back up the etc/fstab file.
  7. Run the echo /dev/vdb1 /mnt ext4 defaults 0 0 >> /etc/fstab command to write the information of the new partition into the /etc/fstab file.

    You can separately mount the data disk as a folder to store webpages. In this case, the /mnt portion in the command must be replaced with the intended mount point folder path.

    Note The Ubuntu 12.04 operating system does not support barriers. You need to run the echo '/dev/vdb1 /mnt ext4 barrier=0 0 0' >> /etc/fstab command to write the information of the new partition into the file.
  8. Run the cat/etc/fstab the command to view the information of the new partition in the /etc/fstab file.
    [root@ecshost~ ]# cat /etc/fstab
    #
    # /etc/fstab
    # Created by anaconda on Wed Dec 12 07:53:08 2018
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    UUID=d67c3b17-255b-4687-be04-f29190d37396 / ext4 defaults 1 1
    /dev/vdb1 /mnt ext4 defaults 0 0
  9. Run the mount/dev/vdb1/mnt command to mount the file system.
  10. Run the df -h command to view the current disk space and usage.
    If the new file system information appears, mounting is successful. You can use the new file system without restarting the instance.
    [root@ecshost~ ]# df -h
    
    Filesystem Size Used Avail Use% Mounted on
    /dev/vda1 40G 1.6G 36G 5% /
    devtmpfs 234M 0 234M 0% /dev
    tmpfs 244M 0 244M 0% /dev/shm
    tmpfs 244M 484K 244M 1% /run
    tmpfs 244M 0 244M 0% /sys/fs/cgroup
    tmpfs 49M 0 49M 0% /run/user/0
    /dev/vdb1 20G 45M 19G 1% /mnt