Initialize a Disk

To use the attached disk, you need to initialize the disk first, including formatting, partitioning, and creating file systems.

Prerequisites

You have attached the disk to your elastic compute instance.

Background Information

Disks support the Master Boot Record (MBR) partition format.

MBR: recognize partitions up to 2 TiB in size and allows up to four partitions to be created on each disk.

Precautions

  • Disk partitioning and formatting are high-risk operations. Proceed with caution when you partition and format disks. This topic applies only to new disks.

  • Only disks can be partitioned. Please do not partition a Boot disk, otherwise risks such as system failures and data loss may occur.

Procedures

  1. Run the following command to view the information of the disk attached.

    fdisk -l
  2. Partition the disk. Assume /dev/vdb is the disk name for illustration.

    1. Run the following command to partition the disk.

      fdisk -u /dev/vdb
    2. Enter P to view the partition information of the disk.

    3. Enter N to create a partition.

    4. Enter P to set the partition as a primary partition.

    5. Enter the partition number and press the Enter key.

    6. Enter the number of the first available sector and press the Enter key.

    7. Enter the number of the last sector and press the Enter key.

    8. Enter P to view the partition information of the disk.

    9. Enter W to start partitioning. Then, exit after the disk is partitioned.

  3. Run the following command to view the new partition.

    fdisk -lu /dev/vdb
  4. Create a file system for the partition

    • Run the following command to create an ext4 file system.

      mkfs -t ext4 /dev/vdb1
    • ​​​​​​Run the following command to create an xfs file system.

      mkfs -t xfs /dev/vdb1
  5. Configure the /etc/fstab file and mount the partition Write the information of the new partition to /etc/fstab to enable this partition to be automatically mounted on instance startup.

    1. Run the following command to back up etc/fstab.

      cp /etc/fstab /etc/fstab.bak
    2. Write the information of the new partition to /etc/fstab.

      echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 defaults 0 0 >> /etc/fstab

      Description of the parameters in this command:

      • /dev/vdb1: the data disk partition that has a file system created. Replace it with your actual partition name.

      • /mnt: the directory to which the partition is mounted. Replace it with the actual directory of your partition.

      • ext4: the file system type of the partition. Replace it with the type of the created file system.

    3. Run the following command to check the information of the new partition in /etc/fstab.

      cat /etc/fstab
    4. Run the following command to mount the file system configured in the /etc/fstab file.

      mount -a
    5. Run the following command to check the mount result.

      df -h

Last updated