Before you can store, read, and write data on a new data disk that is attached to an Elastic Compute Service (ECS) instance, you must initialize the data disk. The initialization operations include partitioning the disk, formatting file systems, and mounting file systems. This topic describes how to initialize a data disk whose size does not exceed 2 TiB on a Linux instance.
Prerequisites
A data disk is attached to a Linux ECS instance. For more information, see Attach a data disk.Background information
Operating system | Resource | Description |
---|---|---|
Linux | Image used by the instance | Alibaba Cloud Linux 3.2104 64-bit public image |
Data disk |
|
Procedures
- (Recommended) Perform the following steps if you want to create a GUID partition table (GPT) partition:
- Perform the following steps if you want to create an master boot record (MBR) partition:
Step 1: Connect to the ECS instance and view the data disk
- Connect to the ECS instance. For more information, see Connect to a Linux instance by using a password or key.
- Run the following command to obtain the device name of the data disk:
fdisk -lu
A command output similar to the following one is returned, which indicates that the instance has two disks: the/dev/vda
system disk and the/dev/vdb
data disk. - Create a partition for the data disk.
- For information about how to create a GPT partition, see the Step 2: Create a GPT partition for the data disk section in this topic.
- For information about how to create an MBR partition, see the Step 2: Create an MBR partition for the data disk section in this topic.
Step 2: Create a GPT partition for the data disk
- Run the following command to install Parted:
yum install -y parted
Note The yum command in this step is suitable for Linux distributions such as CentOS. For other Linux distributions, modify the command based on your package management software. For example, run theapt-get install <Package name>
command for Debian or Ubuntu. - Use Parted to partition the data disk. The following figure shows the partition process by using Parted.
- Run the following command to re-read the partition table:
partprobe
- Run the following command to view the new partition. In this example, the
/dev/vdb
data disk is used.fdisk -lu /dev/vdb
/dev/vdb
specifies the device name of the data disk. Replace it with the actual device name.A command output similar to the following one is returned. If the new partition is created, the information about gpt is displayed.
Step 2: Create an MBR partition for the data disk
- Create an MBR partition. The following figure shows the partition process by using fdisk.
- Run the following command to view the new partition. In this example, the
/dev/vdb
data disk is used.fdisk -lu /dev/vdb
/dev/vdb
specifies the device name of the data disk. Replace it with the actual device name.A command output similar to the following one is returned. If the new partition is created, the information of/dev/vdb1
is displayed.
Step 3: Create a file system for the partition
Create a file system for the new partition. Partition sizes vary based on file system types. Create a file system that suits your needs.
- Run the following command to install e2fsprogs:
yum install -y e2fsprogs
Note The yum command in this step is suitable for Linux distributions such as CentOS. For other Linux distributions, modify the command based on your package management software. For example, run theapt-get install <Package name>
command for Debian or Ubuntu. - Run the following command to create a file system. The following commands are used to create an ext4 file system and an xfs file system.
- Create an ext4 file system
mkfs -t ext4 /dev/vdb1
/dev/vdb1
specifies the partition name of the data disk. Replace it with the actual partition name.Important- If an error is reported when you create an ext4 file system for a 16 TiB data disk, the version of the e2fsprogs package may be earlier than 1.42. Upgrade the version of e2fsprogs to 1.42 or later. For more information, see the How do I upgrade e2fsprogs on a Linux instance? section in this topic.
- The lazy init feature of ext4 file systems affects the I/O performance of data disks. You can disable the lazy init feature of ext4 file systems. For more information, see the How do I disable the lazy init feature on a Linux instance? section in this topic.
- Create an xfs file system
mkfs -t xfs /dev/vdb1
/dev/vdb1
specifies the partition name of the data disk. Replace it with the actual partition name.
A command output similar to the following one is returned. In this example, an ext4 file system is created for the/dev/vdb1
partition of the data disk. - Create an ext4 file system
(Optional) Step 4: Configure the /etc/fstab file and mount the partition
- Run the following command to back up etc/fstab:
cp /etc/fstab /etc/fstab.bak
- Write the information of the new partition to /etc/fstab.
- If you are a root user, you can run the following command to modify /etc/fstab:
Description of the parameters in this command:echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 defaults 0 0 >> /etc/fstab
/dev/vdb1
: the partition name of the data disk. Replace it with the actual partition name./mnt
: the mount point of the partition. Replace it with the actual mount point.ext4
: the file system type of the partition. Replace it with the file system type of the created file system.
Note Ubuntu 12.04 operating systems do not support barriers. You must run theecho `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 barrier=0 0 0 >> /etc/fstab
command to write the information of the new partition to /etc/fstab. If your instance does not run an Ubuntu 12.04 operating system, ignore the note. - If you are a common user, you can modify /etc/fstab. For more information, see Configure UUIDs in the fstab file to automatically attach data disks.
- If you are a root user, you can run the following command to modify /etc/fstab:
- Run the following command to check the information of the new partition in /etc/fstab:
cat /etc/fstab
A command output similar to the following one is returned. - Run the following command to mount the file system that is configured in /etc/fstab. If the file system is correctly configured in /etc/fstab, no error is reported.
mount -a
- Run the following command to check whether the file system is mounted as expected:
df -h
A command output similar to the following one is returned. If the file system is mounted, the information of the new file system is displayed.
FAQ about initializing disks
What do I do if a GPT partition is created but not aligned?
- Run the following commands to query the parameters of the disk:
cat /sys/block/vdb/queue/optimal_io_size cat /sys/block/vdb/queue/minimum_io_size cat /sys/block/vdb/alignment_offset cat /sys/block/vdb/queue/physical_block_size
- Run the following command to re-create a primary partition:
mkpart primary <Recommended sector number>s 100%
<Recommended sector number>=(<optimal_io_size> + <alignment_offset>)/<physical_block_size>
. For example, if the sector number is 1024, you can run themkpart primary 1024s 100%
command to re-create a primary partition.
How do I upgrade e2fsprogs on a Linux instance?
mkfs.ext4: Size of device /dev/vdb too big to be expressed in 32 bits using a blocksize of 4096.
Perform the following steps to install a later version of e2fsprogs. In this example, e2fsprogs 1.42.8 is used.
- Run the following command to check the current e2fsprogs version:
rpm -qa | grep e2fsprogs
A command output similar to the following one is returned.
If the version is earlier than 1.42, perform the following operations to upgrade e2fsprogs:
- Run the following command to download e2fsprogs 1.42.8. You can visit the e2fsprogs website to obtain the latest software package.
wget https://www.kernel.org/pub/linux/kernel/people/tytso/e2fsprogs/v1.42.8/e2fsprogs-1.42.8.tar.gz --no-check-certificate
- Compile the later version of e2fsprogs.
- Run the following command to check whether e2fsprogs is updated:
rpm -qa | grep e2fsprogs
How do I disable the lazy init feature on a Linux instance?
By default, the lazy init feature of ext4 file systems is enabled. When the feature is enabled, the instance initiates a thread to continuously initialize the metadata of ext4 file systems. If you format a data disk when the feature is enabled, the IOPS of the disk may be temporarily affected.
mke2fs -O 64bit,has_journal,extents,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize -E lazy_itable_init=0,lazy_journal_init=0 /dev/vdb1