Initializing a Linux Data Disk (Less Than or Equal to 2 TiB)
Scenarios
After a newly purchased data disk is attached to a server, you must log in to the server and initialize the disk before you can use the disk. This section describes how to initialize a Linux data disk. The operations may vary depending on the server OS.
MBR supports disks up to 2 TiB, whereas GPT supports disks up to 18 EiB. If your disk capacity is less than 2 TiB, you can use either MBR or GPT. However, if your disk is greater than 2 TiB or you plan to expand it to over 2 TiB later, use GPT when initializing disks.
| Operation | OS | Partition Style | Common File Systems | Example Configuration |
|---|---|---|---|---|
| Any OS | MBR | ext* (such as ext2, ext3, and ext4), xfs, and btrfs |
| |
| Any OS |
| |||
| CentOS (OS language must be English) | MBR | ext4 |
|
Prerequisites
The disk has been attached to a server. For how to attach disks, see Attaching an EVS Disk.
Constraints
- A disk created from a data source, such as a backup or snapshot, does not require initialization. It already contains the source data when it is created. Initializing the disk may clear the initial data on it. If you need to re-initialize the disk, you are advised to back up the disk data first. For how to back up data using CBR, see Backing Up EVS Disks. For how to back up data using snapshots, see Creating an EVS Snapshot.
- Initializing a disk does not delete the snapshots created for the disk, so you can still use the snapshots to roll back data to the source disk after the disk is initialized.
The following example shows how to use fdisk to create two primary partitions (/dev/vdb1: 40 GiB; /dev/vdb2: 60 GiB) on the /dev/vdb data disk and set the partition style to MBR.
- Log in to the server as user root.
For how to log in to an ECS, see How Do I Log In to My ECS?
For how to log in to a BMS, see Linux BMS Login Methods.
- Create partitions. In this example, create two primary partitions, /dev/vdb1 and /dev/vdb2 for data disk /dev/vdb.
- Check that the capacity of the /dev/vdb data disk is 100 GiB. lsblk
[root@ecs-centos76 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 253:0 0 40G 0 disk └─vda1 253:1 0 40G 0 part / vdb 253:16 0 100G 0 disk
The command output shows that there are two EVS disks. /dev/vda is the system disk, and /dev/vdb is the new data disk, whose 100 GiB is not partitioned.
- Create the first primary partition /dev/vdb1 for /dev/vdb.
fdisk /dev/vdb
n
p
1[root@ecs-test-0001 ~]# fdisk /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 0x38717fc1. 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
- Entering p for Partition type creates a primary partition, and entering e creates an extended partition. The default value is p.
- Partition number indicates the partition serial number. Enter a value ranging from 1 to 4.
- Enter n and press Enter to create a new partition.
- Enter p and press Enter to create a primary partition.
- Enter 1 and press Enter to set a partition number. Partition number 1 is used in this example.
- Set First sector to 2048 and Last sector to 83886079 for partition /dev/vdb1 (40 GiB). For how to calculate the values, see Table 2.
First sector (2048-209715199, default 2048): 2048 Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):83886079 Partition 1 of type Linux and of size 40 GB is setTable 2 First and last sectors in this example are calculated as follows Sector
/dev/vdb1 (40 GiB)
/dev/vdb2 (60 GiB)
Formula for Calculating the Value of sectors
First sector
2048 (The first sector of the /dev/vdb data disk is used.)
Last sector of /dev/vdb1 + 1
= 83886079 + 1
= 83886080
Value of sectors
= Capacity × 1073741824/512
Last sector
Value of sectors – 1
= (40 × 1073741824/512) – 1
= 83886079
First sector + Value of sectors – 1
= 83886080 + (60 × 1073741824/512) – 1
= 209715199
- Create the second primary partition /dev/vdb2 for /dev/vdb.
n
p
2
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 2): 2
- Enter n and press Enter to create a new partition.
- Enter p and press Enter to create a primary partition.
- Enter 2 and press Enter to set a partition number. Partition number 2 is used in this example.
- Set the First sector to 83886080 and Last sector to 209715199 for partition /dev/vdb2.
First sector (83886080-209715199, default 83886080): 83886080 Last sector, +sectors or +size{K,M,G} (83886080-209715199, default 209715199):209715199 Partition 2 of type Linux and of size 60 GB is set - Check the sizes and partition styles of the new partitions. p
Command (m for help): p Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x994727e5 Device Boot Start End Blocks Id System /dev/vdb1 2048 83886079 41942016 83 Linux /dev/vdb2 83886080 209715199 62914560 83 Linux Command (m for help):
Disk label type: dos indicates the MBR partition style.
In case that you want to discard the changes made before, you can exit fdisk by entering q and press Enter. Then, re-create the partitions by referring to step 1 and step 2.
- Write the changes to the partition table.
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
The partitions are created.
If error message -bash: partprobe: command not found is returned, the system cannot identify the command. In this case, run yum install -y parted to install the command. Then, run the command again.
- Synchronize the new partition table to the OS.
- Check that the capacity of the /dev/vdb data disk is 100 GiB.
- Create file systems.
- Create ext4 file systems for partitions /dev/vdb1 (40 GiB) and /dev/vdb2 (60 GiB).
mkfs -t ext4 /dev/vdb1
mkfs -t ext4 /dev/vdb2
- mkfs -t <file-system-format> <disk-partition-name>: To create an xfs file system, run the mkfs -t xfs <disk-partition-name> command. To create a btrfs file system, run the mkfs -t btrfs <disk-partition-name> command.
- It takes some time to create file systems. Do not exit until the system returns the following information:
[root@ecs-test-0001 ~]# mkfs -t 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 2621440 inodes, 10485504 blocks 524275 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2157969408 320 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, 7962624 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done - Check whether the file system format is ext4.
[root@ecs-test-0001 ~]# blkid /dev/vdb /dev/vdb1: UUID="0b3040e2-1367-4abb-841d-ddb0b92693df" TYPE="ext4" /dev/vdb2: UUID="0d6769k2-1745-9dsf-453d-hgd0b34267dj" TYPE="ext4"
- Create ext4 file systems for partitions /dev/vdb1 (40 GiB) and /dev/vdb2 (60 GiB).
- Create directories to serve as mount points and mount the new partitions on the created mount points.
- Mount /dev/vdb1 on /mnt/sdc.
mkdir -p /mnt/sdc
mount /dev/vdb1 /mnt/sdc
- Mount /dev/vdb2 on /mnt/sdd.
mkdir -p /mnt/sdd
mount /dev/vdb2 /mnt/sdd
- View the mount results.
[root@ecs-test-0001 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 253:0 0 40G 0 disk ├vda1 253:1 0 40G 0 part / vdb 253:16 0 100G 0 disk ├vdb1 253:17 0 40G 0 part /mnt/sdc ├vdb2 253:18 0 60G 0 part /mnt/sdd
You should now see that partitions /dev/vdb1 and /dev/vdb2 are mounted on /mnt/sdc and /mnt/sdd.
- Mount /dev/vdb1 on /mnt/sdc.
- (Optional) Use the partition UUIDs to configure auto-mount at startup. In Linux, each partition has a UUID, which serves as its unique identifier. Mounts become invalid after a system reboot. You can configure auto-mount at startup by adding information of the new partitions into the /etc/fstab file.
- You are advised not to use device names to identify disks in the /etc/fstab file because device names are assigned dynamically and may change (for example, from /dev/vdb1 to /dev/vdb2) after a server stops or starts. This can even prevent your server from booting up.
- This operation will not affect the existing data on the ECS.
- Query the partition UUIDs.
blkid /dev/vdb2
[root@ecs-test-0001 ~]# blkid /dev/vdb /dev/vdb1: UUID="0b3040e2-1367-4abb-841d-ddb0b92693df" TYPE="ext4" /dev/vdb2: UUID="0d6769k2-1745-9dsf-453d-hgd0b34267dj" TYPE="ext4"
Take note of the partition UUIDs, which will be used in the next step. In this example, the partition UUIDs are 0b3040e2-1367-4abb-841d-ddb0b92693df and 0d6769k2-1745-9dsf-453d-hgd0b34267dj.
- Configure auto-mount at startup.
Press i to enter the editing mode, move the cursor to the end of the file, press Enter, and add the following content:
UUID=0b3040e2-1367-4abb-841d-ddb0b92693df /mnt/sdc ext4 defaults 0 2 UUID=0d6769k2-1745-9dsf-453d-hgd0b34267dj /mnt/sdd ext4 defaults 0 2
Press Esc, enter :wq, and press Enter to save the settings and exit the vi editor.
Table 3 Content description Example Value
Description
UUID=0b3040e2-1367-4abb-841d-ddb0b92693df
The UUID of the partition.
/mnt/sdc
The mount point of the partition.
ext4
The file system format of the partition.
defaults
The partition mount option. Normally, this parameter is set to defaults.
0
The Linux dump backup option.- 0: Linux dump backup is not used. Usually, dump backup is not used, so you can set this parameter to 0.
- 1: Linux dump backup is used.
2
The fsck option, which means whether to use fsck to check this file system during startup.- 2: Check this file system after the root file system (used for non-root file systems).
- 1: Check this file system first (typically used for the root / file system).
- 0: Do not check this file system.
- (Optional) Verify that auto-mount has taken effect.
You can restart the server to check if auto-mount takes effect. Alternatively, you can perform the following steps to simulate auto-mount.
- To verify that auto-mount has taken effect, unmount the partitions first.
umount /dev/vdb1
umount /dev/vdb2
- Reload all the content in the /etc/fstab file. /etc/fstab is a static file system table that contains the list of file systems that need to be automatically mounted during system startup.
mount -a
- Query the file system mount information.
mount | grep /mnt/sdc
mount | grep /mnt/sdd
If information similar to the following is displayed, auto-mount has taken effect:
root@ecs-test-0001 ~]# mount | grep /mnt/sdc /dev/vdb1 on /mnt/sdc type ext4 (rw,relatime,data=ordered) root@ecs-test-0001 ~]# mount | grep /mnt/sdd /dev/vdb2 on /mnt/sdd type ext4 (rw,relatime,data=ordered)
You can use parted to create either MBR or GPT partitions. The only difference is that the command used to set the partition style is different. All other operations are the same. For the initialization instructions with parted, see Initializing a Data Disk Using parted.
Commands for setting partition styles:
MBR:
mklabel msdos unit s p
GPT:
mklabel gpt unit s p
- Log in to the server as user root.
For how to log in to an ECS, see How Do I Log In to My ECS?
For how to log in to a BMS, see Linux BMS Login Methods.
- View the information of the disk to be initialized. lsblk
[root@ecs-centos76 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 253:0 0 40G 0 disk └─vda1 253:1 0 40G 0 part / vdb 253:16 0 100G 0 disk
The command output shows that there are two EVS disks. /dev/vda is the system disk, and /dev/vdb is the new data disk, whose 100 GiB is not partitioned.
- Obtain the auto initialization script.
wget https://ecs-instance-driver.obs.cn-north-1.myhuaweicloud.com/datadisk/LinuxVMDataDiskAutoInitialize.sh
Figure 1 Script obtained
- If the returned output is abnormal, check whether an EIP is bound to the ECS. The script can be obtained only after an EIP is bound.
- This script can only run on English-language OS only.
- Change the permission on the auto initialization script.
chmod +x LinuxVMDataDiskAutoInitialize.sh
- Execute the auto initialization script to automatically detect data disks to be initialized.
./LinuxVMDataDiskAutoInitialize.sh
The script automatically detects and displays the drive letters of all disks except the system disk on the server.
- Enter a drive letter, for example, /dev/vdb, and press Enter. Then, the script automatically creates a partition (/dev/vdb1) and formats the partition.
For a disk that has data on it, the script automatically detects that the disk is mounted and shows the mount location. You need to determine whether to unmount the disk. Entering y unmounts the disk, and entering n exits the script. After the disk is detached, determine whether to format the disk as prompted.

- Mount the disk. For example, if you enter /data-test as the mount point, the script automatically creates this directory for mounting.

The script automatically configures auto-mount at startup.

In the preceding command output, the /dev/vdb1 partition on the /dev/vdb disk is successfully created and mounted on the /data-test directory.
- (Optional) Check the mount results.
lsblk
[root@ecs-centos76 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 253:0 0 40G 0 disk └─vda1 253:1 0 40G 0 part / vdb 253:16 0 100G 0 disk └─vdb1 253:1 0 100G 0 part /data-test
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot