# 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](https://docs.console.zenlayer.com/welcome/elastic-compute/get-started/manage-storage/attach-a-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

{% tabs %}
{% tab title="Initialize disks (Linux)" %}

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
      ```

{% endtab %}

{% tab title="Initialize disks (Windows)" %}

1. Open the **Disk Management** window and find the disk that is not partitioned or formatted.
   1. On the Windows Server desktop, click the **Server Manager** icon in the lower-left corner.
   2. In the **Server Manager** window, choose **Tools** > **Computer Management** in the upper-right corner.
   3. In the left-side navigation pane, choose **Computer Management (Local)** > **Storage** > **Disk Management**.
   4. Find a disk that is not partitioned or formatted, which is in the **Offline** state.
2. Right-click the blank area around the disk and select **Online**.\
   When the disk goes online, it enters the **Not Initialized** state.
3. Right-click the blank area around the disk and select **Initialize Disk**.
4. In the **Initialize Disk** dialog box, select the disk, select a partition format, and then click **OK**.
5. Right-click the Unallocated section and select **New Simple Volume**.
6. In the **New Simple Volume Wizard** dialog box, perform the following operations.
   1. Click **Next**.
   2. In the **Specify Volume Size** step, set **Simple volume size in MB** and click **Next**.
   3. In the **Assign Drive Letter or Path** step, select **Assign the following drive letter**, select a drive letter, and then click **Next**.
   4. In the **Format Partition** step, select **Format this volume with the following settings**, configure formatting settings, and then click **Next**.
   5. Check the settings and click **Finish**.
      {% endtab %}
      {% endtabs %}
