Format Disk Partition on Linux System
Overview
If the image of the BCC (Baidu Cloud Compute) you purchased runs on a Linux operating system, after buying a new CDS Cloud Disk Data Disk, you must partition and format the disk before it can be used properly.
Operation steps
You may use two partitioning methods: MBR or GPT. For differences between the two methods and precautions during disk partitioning, you may refer to [Disk Partition Overview](CDS/Disk Operation Guide/Basic Operations/Format cloud disks/Overview.md).
Using MBR partition
This guide demonstrates how to create a single disk partition on a new 5 GB data disk (device name: /dev/vdb) in a CentOS 7.6 x86_64 (64-bit) environment.
1. Remotely sign in to BCC instance
Please log in to the BCC Instance first. For specific login method, please refer to Sign in to Instance.
2. Check Current Disk Information
Run fdisk -l to view Current Disk Information.
1[root@instance-xxx ~]# fdisk -l
2 Disk /dev/vda: 42.9 GB, 42,949,672,960 bytes, 83,886,080 sectors
3 Units = sectors of 1 * 512 = 512 bytes
4 Sector size (logical/physical): 512 bytes / 512 bytes
5 I/O size (min/optimal): 512 bytes / 512 bytes
6 Disk Tag Type: dos
7 Disk identifier: 0x000b8163
8 Device Boot Start End Blocks Id System
9/dev/vda1 * 2048 83886046 41941999+ 83 Linux
10 Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
11 Units = sectors of 1 * 512 = 512 bytes
12 Sector size (logical/physical): 512 bytes / 512 bytes
13 I/O size (min/optimal): 512 bytes / 512 bytes
Prompt:
- Before accessing, ensure the data disk is mounted. If it is not mounted, please mount the disk to the instance first.
3. Partition the CDS Cloud Disk Server
To partition a Data Disk using fdisk, execute the following Commands in sequence:
- Execute
fdisk /dev/vdbto partition the Data Disk; - Enter p and press Enter: Print the Disk Partition list to view Data Disk partitioning; in this Instance, the disk has not been partitioned yet;
- Type 'n' and press Enter: Create a new partition;
-
Enter p and press Enter: Select the partition type as primary;
Prompt:
- This process establishes a single disk partition for the disk, requiring only a primary partition. If you need to create multiple partitions, please choose 'e'.
- Enter partition number and press Enter: In this example, only one partition is created, enter 1;
- Enter the first available sector number and press Enter: Use the default value 2048;
- After entering the last sector number, press Enter: To create a single partition, press Enter to use the default value; if you need to create multiple partitions, input the sector numbers according to actual requirements;
- Enter "p" and press Enter: Print the partition list to view the current Data Disk partition status;
- Enter w and press Enter: Begin Disk Partition operation;
1[root@instance-xxx ~]# fdisk /dev/vdb
2 Welcome to fdisk (util-linux 2.23.2).
3 Changes will remain in memory until you decide to write them to the disk.
4 Think carefully before executing the write Command.
5Device does not contain a recognized partition table
6 Create a new DOS Disk Tag using disk identifier 0x88504a48.
7 Command (enter m for help): p
8 Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
9 Units = sectors of 1 * 512 = 512 bytes
10 Sector size (logical/physical): 512 bytes / 512 bytes
11 I/O size (min/optimal): 512 bytes / 512 bytes
12 Disk Tag Type: dos
13 Disk identifier: 0xbc3ca63f
14 Device Boot Start End Blocks Id System
15 Command (enter m for help): n
16Partition type:
17 p primary (0 primary, 0 extended, 4 free)
18 e extended
19
20Select (default p): p
21 Partition number (1-4, default 1):1
22 Starting sector (2048-10485759, default 2048): 2048
23 Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
24 The default value 10485759 will be used
25 Partition 1 has been set to Linux type, with a size of 5 GiB
26 Command (enter m for help): p
27 Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
28 Units = sectors of 1 * 512 = 512 bytes
29 Sector size (logical/physical): 512 bytes / 512 bytes
30 I/O size (min/optimal): 512 bytes / 512 bytes
31 Disk Tag Type: dos
32 Disk identifier: 0xbc3ca63f
33 Device Boot Start End Blocks Id System
34/dev/vdb1 2048 10485759 5241856 83 Linux
35 Command (enter m for help): w
36The partition table has been altered!
37Calling ioctl() to re-read partition table.
38 Synchronizing disk.
Prompt:
- The fdisk partition command parameters are as follows: b: Edit BSD disklabel; c: Toggle DOS compatibility flag; d: Delete a partition; g: Create a new empty GPT partition table; G: Create an IRIX (SGI) partition table; l: List known partition types; m: Print help menu; n: Add a new Disk Partition; o: Create a new empty DOS partition table; p: Print partition table information; q: Quit without saving changes; s: Create a new empty Sun disk label; t: Change a partition's system ID; u: Change display/input unit; v: Verify partition table; w: Write the partition table to disk and exit; x: Additional features
4. View new partition
Execute the command fdisk -l to verify the new partition vdb1 has been created.
1[root@instance-xxx ~]# fdisk -l
2 Disk /dev/vda: 42.9 GB, 42,949,672,960 bytes, 83,886,080 sectors
3 Units = sectors of 1 * 512 = 512 bytes
4 Sector size (logical/physical): 512 bytes / 512 bytes
5 I/O size (min/optimal): 512 bytes / 512 bytes
6 Disk Tag Type: dos
7 Disk identifier: 0x000b8163
8 Device Boot Start End Blocks Id System
9/dev/vda1 * 2048 83886046 41941999+ 83 Linux
10 Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
11 Units = sectors of 1 * 512 = 512 bytes
12 Sector size (logical/physical): 512 bytes / 512 bytes
13 I/O size (min/optimal): 512 bytes / 512 bytes
14 Disk Tag Type: dos
15 Disk identifier: 0xbc3ca63f
16 Device Boot Start End Blocks Id System
17/dev/vdb1 2048 10485759 5241856 83 Linux
5. Create a File System
Execute the mkfs.ext4 /dev/vdb1 Command to create a File System on the new partition. Creation time varies based on CDS Disk Capacity.
1[root@instance-xxx ~]# mkfs.ext4 /dev/vdb1
2mke2fs 1.42.9 (28-Dec-2013)
3 File System label=
4OS type: Linux
5 Block size=4096 (log=2)
6 Part size=4096 (log=2)
7Stride=0 blocks, Stripe width=0 blocks
8327680 inodes, 1310464 blocks
965523 blocks (5.00%) reserved for the super user
10 First data block=0
11Maximum filesystem blocks=1342177280
1240 block groups
1332768 blocks per group, 32768 fragments per group
148192 inodes per group
15Superblock backups stored on blocks:
1632768, 98304, 163840, 229376, 294912, 819200, 884736
17 Allocating group tables: Done
18 Writing into inode table: Done
19 Creating journal (32768 blocks): Done
20 Writing superblocks and filesystem accounting information: Done
Prompt:
- You can also create other file systems based on your requirements.
6. Mount the file system
Execute the mount /dev/vdb1 /mnt Command to mount the File System to the /mnt node.
1[root@instance-xxx ~]# mount /dev/vdb1 /mnt
Prompt:
- To mount CDS disks to different directories, replace /mnt in the above command with your desired mount point.
7. Query Disk Partition UUID
UUID is the unique identifier for disk partitions in Linux operating systems.
You can execute the Command: blkid Disk Partition to query the UUID, such as blkid /dev/vdb1
1[root@instance-xxx ~]# blkid /dev/vdb1
2/dev/vdb1: UUID="2bc6f6d2-0c6b-4946-b038-91562520a942" TYPE="ext4"
8. Write Disk Partition Information
There are two formats for writing Disk Partition information:
- Drive letter format, e.g.: /dev/vdb1 /mnt ext4 defaults 0 0
- UUID format, e.g.: UUID=84953f78-xxxx-4dbd-ac60-aaabc2e4cb9c / ext4 defaults 1 1
During events like reboot or shutdown, Baidu Cloud Compute drive letters may change (e.g., /dev/vdb1 might become /dev/vdb2), which could cause startup or operational issues. It is therefore recommended to configure disk partition information using UUID format.
Open the '/etc/fstab' file using the vim editor with the Command: vim /etc/fstab
After entering edit mode by typing 'i', add the previously obtained UUID, drive letter, File System type, and other information to the fstab file, press ESC, and then enter ':wq' to save:
1UUID=2bc6f6d2-0c6b-4946-b038-91562520a942 /mnt ext4 defaults 0 0
The format in fstab is as follows:
1<device-spec> <mount-point> <fs-type> <options> <dump> <pass>
- device-spec: Device name or Tag, such as the UUID of the disk partition or drive letter. In this example, UUID=2bc6f6d2-0c6b-4946-b038-91562520a942, drive letter=/dev/vdb1;
- mount-point: Mount target information; in this example, it is /mnt
- fs-type: The File System format of the Disk Partition, ext4 in this example;
- options: mount options, set to "defaults";
- dump: backup option, selectable as 0 or 1. If 0, dump will skip backing up this File System; if 1, it will perform the backup. In this example, it is set to 0;
- pass: Determines whether fsck is used to check sectors during BCC startup with the options: 0, 1, 2. 0: No check; 1: Primary check (typically only the root directory is set to 1); 2: Checked after 1. In this example: 0.
9. View new partition information
Execute cat /etc/fstab command; the following information indicates successful new partition writing.
1[root@instance-xxx ~]# cat /etc/fstab
2# /etc/fstab
3# Created by anaconda on Thu Mar 21 07:01:01 2019
4#
5# Accessible filesystems, by reference, are maintained under '/dev/disk'
6# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7#
8UUID=84953f78-xxxx-4dbd-ac60-aaabc2e4cb9c / ext4 defaults 1 1
9UUID=2bc6f6d2-0c6b-4946-b038-91562520a942 /mnt ext4 defaults 0 0
10. Verify Auto-mount Functionality
- Unmount a mounted Disk Partition: umount partition, e.g., umount /dev/vdb1;
- Mounting/etc/fstab information: mount -a;
- Query the File System information of the mount target: mount | grep mount target, for example, mount | grep /mnt. If the output displays similar information as follows, it indicates that the automatic Attach Disk and Disk Partition feature upon startup has taken effect:
1[root@instance-xxx ~]# mount | grep /mnt
2/dev/vdb1 on /mnt type ext4 (rw,relatime,data=ordered)
11. View the current disk space
Run the df -h command to check the Mount status. If Disk Partition information appears, the new partition is successfully mounted.
1[root@instance-xxx ~]# df -h
2 File system Capacity Used Available Used% Mount target
3/dev/vda1 40G 1.7G 36G 5% /
4devtmpfs 2.0G 0 2.0G 0% /dev
5tmpfs 2.0G 0 2.0G 0% /dev/shm
6tmpfs 2.0G 17M 2.0G 1% /run
7tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
8tmpfs 396M 0 396M 0% /run/user/0
9/dev/vdb1 4.8G 20M 4.6G 1% /mnt
Using GPT partition
This guide explains how to create a single disk partition on a new 2300 GB data disk (device name: /dev/vdc) in a CentOS 7.6 x86_64 (64-bit) environment.
1. Remotely sign in to BCC instance
Please log in to the BCC Instance first. For specific login method, please refer to Sign in to Instance.
2. Check Current Disk Information
Execute the fdisk -l command to view current disk information; you will see that the /dev/vdc disk is in an unpartitioned state.
1[root@instance-xxxx ~]# fdisk -l
2 Disk /dev/vda: 42.9 GB, 42,949,672,960 bytes, 83,886,080 sectors
3 Units = sectors of 1 * 512 = 512 bytes
4 Sector size (logical/physical): 512 bytes / 512 bytes
5 I/O size (min/optimal): 512 bytes / 512 bytes
6 Disk Tag Type: dos
7 Disk identifier: 0x000b8163
8 Device Boot Start End Blocks Id System
9/dev/vda1 * 2048 83886046 41941999+ 83 Linux
10 Disk /dev/vdc: 2469.6 GB, 2,469,606,195,200 bytes, 4,823,449,600 sectors
11 Units = sectors of 1 * 512 = 512 bytes
12 Sector size (logical/physical): 512 bytes / 512 bytes
13 I/O size (min/optimal): 512 bytes / 512 bytes
Prompt:
- Before accessing, ensure the data disk is mounted. If it is not mounted, please mount the disk to the instance first.
3. Partition the CDS Cloud Disk
- Use Parted to partition the data disk by executing the command
parted /dev/vdcand pressing Enter. Follow the prompts to proceed step-by-step as below: - Enter
mklabel gptand press Enter to switch to GPT partition format; - Enter
mkpart primary 1 100%and press Enter to create a primary partition and set its start and end positions; - Enter
align-check optimal 1and press Enter to check partition alignment. If it returns 1, the partition is aligned; - Enter
printand press Enter to view the partition table of the current disk - Enter
quitand press Enter to exit.
1[root@instance-xxxx ~]# parted /dev/vdc
2GNU Parted 3.1
3 Use /dev/vdc
4Welcome to GNU Parted! Type 'help' to view a list of commands.
5(parted) mklabel gpt
6(parted) mkpart primary 1 100%
7(parted) align-check optimal 1
81 aligned
9(parted) print
10Model: Virtio Block Device (virtblk)
11Disk /dev/vdc: 2470GB
12Sector size (logical/physical): 512B/512B
13Partition Table: gpt
14Disk Flags:
15 Number Start End Size File system Name Flags
16 1 1049kB 2470GB 2470GB primary
17(parted) quit
18 Information: You may need to update /etc/fstab.
4. Reread Disk Partition Table
Run the command partprobe to make the system reread the partition table.
1[root@instance-xxxx ~]# partprobe
5. Create the file system
Execute the command mkfs -t <File System format> /dev/vdc1 to create a file system, using ext4 as an example in this case.
1[root@instance-xxxx ~]# mkfs -t ext4 /dev/vdc1
2mke2fs 1.42.9 (28-Dec-2013)
3 File System label=
4OS type: Linux
5 Block size=4096 (log=2)
6 Part size=4096 (log=2)
7Stride=0 blocks, Stripe width=0 blocks
8150732800 inodes, 602931200 blocks
930146560 blocks (5.00%) reserved for the super user
10 First data block=0
11Maximum filesystem blocks=2751463424
1218400 block groups
1332768 blocks per group, 32768 fragments per group
148192 inodes per group
15Superblock backups stored on blocks:
16 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
17 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
18 102400000, 214990848, 512000000, 550731776
19 Allocating group tables: Done
20 Writing into inode table: Done
21 Creating journal (32768 blocks): Done
22 Writing superblocks and filesystem accounting information: Done
6. Creating Mount Points and Mounting File Systems
Execute mkdir <mount target> to create a mount target. This instance is mounted to the /set mount target;
Execute mount /dev/vdc1 /set to mount partition /dev/vdc to /set;
Execute df -h to view current disk space and usage.
1[root@instance-xxxx ~]# mkdir /set
2[root@instance-xxxx ~]# mount /dev/vdc1 /set
3[root@instance-xxxx ~]# df -h
4 File system Capacity Used Available Used% Mount target
5/dev/vda1 40G 1.7G 36G 5% /
6devtmpfs 2.0G 0 2.0G 0% /dev
7tmpfs 2.0G 0 2.0G 0% /dev/shm
8tmpfs 2.0G 17M 2.0G 1% /run
9tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
10/dev/vdb1 4.8G 20M 4.6G 1% /mnt
11tmpfs 396M 0 396M 0% /run/user/0
12/dev/vdc1 2.3T 85M 2.1T 1% /set
Note: If you are using CentOS or Ubuntu systems, you can use the CDSCMD tool for automatic partitioning operations.
