After you attach a new data disk to an ECS instance, you must create at least one file system on the data disk and mount the file system. In this example, an I/O optimized instance that runs the Aliyun Linux 2.1903 LTS 64-bit operating system is used. A new 20 GiB data disk is attached to the instance. The device name of the data disk is /dev/vdb. A single Master Boot Record (MBR) partition is created on the data disk and an ext4 file system is mounted on the partition.

Prerequisites

Data disks that you have purchased separately are attached to ECS instances. If you purchased data disks with ECS instances, skip the attach operation. For more information, see Attach a data disk.
Note The attach operation refers to attaching disks to ECS instances in the console, instead of mounting file systems to the operating systems of ECS instances by running the mount command.

Background information

The following procedure applies only to data disks up to 2 TiB in size. Data disks larger than 2 TiB must be partitioned into the GPT format. For more information, see Partition and format a data disk larger than 2 TiB.

By default, device names of data disks are assigned by the system. Device names of data disks for I/O optimized instances ascend from /dev/vdb to /dev/vdz. Device names of data disks for non-I/O optimized instances ascend from /dev/xvdb to /dev/xvdz.

The following section describes the risks of formatting a data disk:
  • Disk partitioning and formatting are high-risk operations. Exercise caution. The procedure in this topic applies only to new data disks. If your data disk contains data, create a snapshot for the data disk to avoid data loss. For more information, see Create a normal snapshot.
  • Only data disks can be partitioned. You cannot partition system disks. If you use a third-party tool to forcibly partition a system disk, unknown risks such as system failure and data loss may occur. You can extend partitions or add new partitions only for the system disk that has been extended. For more information, see Resize partitions and file systems of Linux system disks.
Note The commands in the example also apply to CentOS 7.

Procedure

  1. Connect to an ECS instance. For more information, see Connect to an ECS instance.
  2. Run the fdisk -l command to view all the data disks attached to the ECS instance.
    Note If /dev/vdb is not displayed in the command output, the ECS instance does not have data disks. Check whether data disks are attached to the instance.
  3. Run the following commands in sequence 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 When you create a single partition for a data disk, the partition must be a primary partition. If you want to create four or more partitions, enter e (extended) to create at least one extended partition.
    5. Enter the partition number and press the Enter key.
      In this example, enter 1 to create one partition.
    6. Enter the number of the first available sector, or press the Enter key to use the default value 2048.
    7. Enter the number of the last sector.
      In this example, press the Enter key 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 -lu /dev/vdb command to view the new partition.
    If information about /dev/vdb1 is displayed, the new partition is 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 on the new partition.

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

    [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. Write the information of the new partition to /etc/fstab to enable automatic partition mounting when the instance is started.
    1. Run the cp /etc/fstab /etc/fstab.bak command to back up etc/fstab.
    2. Run the echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 defaults 0 0 >> /etc/fstab command to write the information of the new partition to the /etc/fstab file.

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

      Note
      • We recommend that you use a universally unique identifier (UUID) in /etc/fstab to reference the new partition. You can run the blkid command to obtain the UUID of the new partition.
      • The Ubuntu 12.04 operating system does not support barriers. You must run the echo '`blkid /dev/vdb1 | awk '{print $3}' | sed 's/\"//g'` /mnt ext4 barrier=0 0 0' >> /etc/fstab command to write the information of the new partition to the /etc/fstab file.
  7. Run the cat /etc/fstab command to view the information of the new partition in /etc/fstab.
    [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-f29190d4**** / ext4 defaults 1 1
    UUID=a4c73111-3a33-4569-a90c-b6d2e953**** /mnt ext4 defaults 0 0
  8. Run the mount /dev/vdb1 /mnt command to mount the file system.
    If the information of the new file system is displayed after you run the df -h command, the file system is mounted.
    [root@ecshost~ ]# mount /dev/vdb1 /mnt
    [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