Updated on 2022-02-22 GMT+08:00

Initializing a Linux Data Disk (fdisk)

Scenarios

This section uses CentOS 7.0 64-bit as an example to describe how to initialize a data disk attached to a Linux BMS and use fdisk to partition the data disk.

The method for initializing a disk varies depending on the OSs running on the server. This document is for reference only. For detailed operations and differences, see the product documents of the OSs running on the corresponding BMSs.

Prerequisites

  • You have logged in to the BMS as user root. For details, see section "Logging In to a Linux BMS" in Bare Metal Server User Guide.
  • A data disk has been attached to the BMS and has not been initialized.

Creating Partitions and Mounting a Disk

The following example shows you how to use fdisk to create a primary partition on a data disk that has been attached to the BMS. The default partitioning style is MBR and the default file system format is ext4. Mount the file system to /mnt/sdc, and configure automatic mounting upon system start.

  1. Run the following command to view information about the added disk:

    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: 10.7 GB, 10737418240 bytes, 20971520 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

    This command output indicates that the BMS contains two disks. /dev/xvda is used as the system disk, and /dev/xvdb is the newly added data disk.

  2. Run the following command to use fdisk to partition the added data disk:

    fdisk Added data disk

    In this example, /dev/xvdb is the added data disk.

    fdisk /dev/xvdb

    Information similar to the following is displayed:

    [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.
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0xb00005bd.
    Command (m for help): 

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

    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 the primary partition.
    • e indicates an extended partition.

  4. A primary partition is used 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):

    Partition number indicates the serial number of the primary partition. The value can be 1 to 4.

  5. Enter the serial number of the primary partition and press Enter. Primary partition number 1 is used in this example.

    Information similar to the following is displayed:

    Partition number (1-4, default 1): 1
    First sector (2048-20971519, default 2048):

    First sector indicates the first sector. The value can be 2048 to 20971519, and the default value is 2048.

  6. Select the default first sector 2048 and press Enter.

    Information similar to the following is displayed:

    First sector (2048-20971519, default 2048):
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):

    Last sector indicates the last sector. The value can be 2048 to 20971519, and the default value is 20971519.

  7. Select the default last sector 20971519 and press Enter.

    Information similar to the following is displayed:

    Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
    Using default value 20971519
    Partition 1 of type Linux and of size 10 GiB is set
    Command (m for help):

    A primary partition has been created for a 10 GB data disk.

  8. 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: 10.7 GB, 10737418240 bytes, 20971520 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
    
    Command (m for help): 

    Details about the /dev/xvdb1 partition are displayed.

  9. 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.

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

    partprobe

  11. Run the following command to set the file system format of the created partition:

    mkfs -t File system format /dev/xvdb1

    For example, run the following command to set the file system format to ext4:

    mkfs -t ext4 /dev/xvdb1

    Information similar to the following is displayed:

    [root@ecs-b656 test]# mkfs -t ext4 /dev/xvdb1
    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
    655360 inodes, 2621184 blocks
    131059 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2151677952
    80 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
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    The formatting takes a period of time. Wait for the system running, and do not exit.

    The partition sizes supported by file systems vary. Therefore, you are advised to choose an appropriate file system based on your service requirements.

  12. Run the following command to create a mounting point:

    mkdir Mounting point

    For example, run the following command to create a mounting point /mnt/sdc:

    mkdir /mnt/sdc

  13. Run the following command to mount the new partition to the mounting point created in 12.

    mount /dev/xvdb1 Mounting point

    For example, run the following command to mount the created partition to /mnt/sdc:

    mount /dev/xvdb1 /mnt/sdc

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

    df -TH

    Information similar to the following is displayed:

    [root@ecs-b656 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/xvdb1     ext4       11G   38M  9.9G   1% /mnt/sdc

    The new partition /dev/xvdb1 is mounted to /mnt/sdc.

Setting Automatic Disk Mounting at System Start

To automatically mount a disk when a BMS starts, you should not specify its partition, for example /dev/xvdb1, in /etc/fstab. This is because the sequence of cloud devices may change during the BMS stop and start. For example, /dev/xvdb1 may change to /dev/xvdb2. You are advised to use the universally unique identifier (UUID) to automatically mount data disks.

The universally unique identifier (UUID) is the unique character string for disk partitions in a Linux system.

  1. Run the following command to query the partition UUID:

    blkid Disk partition

    For example, run the following command to query the UUID of /dev/xvdb1:

    blkid /dev/xvdb1

    Information similar to the following is displayed:

    [root@ecs-b656 test]# blkid /dev/xvdb1
    /dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4"

    The UUID of /dev/xvdb1 is displayed.

  2. Run the following command to open the fstab file using the vi editor:

    vi /etc/fstab

  3. Press i to enter the editing mode.
  4. Move the cursor to the end of the file and press Enter. Then add the following information:

    UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc      ext4 defaults     0   2

  5. Press Esc, enter :wq, and press Enter.

    The system saves the configurations and exits the vi editor.