操作場景
本文以物理機的操作系統為“CentOS 7.6 64位”為例,采用fdisk分區工具為數據盤設置分區。
說明物理機的不同操作系統的格式化操作可能不同,本文僅供參考,具體操作步驟和差異請參考對應操作系統的產品文檔。
前提條件
- 已登錄物理機。
- 掛載至物理機的云盤數據盤或者物理機的本地數據盤還沒有被初始化。
劃分分區并掛載磁盤
本操作以該場景為例,當物理機掛載了一塊新的數據盤時,使用fdisk分區工具將該數據盤設為主分區,分區形式默認設置為MBR,文件系統設為ext4格式,掛載在“/mnt/sdc”下,并設置開機啟動自動掛載。
操作步驟
- 執行以下命令,查看新增數據盤。
fdisk -l
回顯類似如下信息:
[root@test-sharedDisk ~]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/vda: 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: gpt
Disk identifier: 942EF8CC-BD4F-4346-9713-78E8EB355090
# Start End Size Type Name
1 2097152 4194303 1G BIOS boot primary
2 4194304 10485759 3G Linux filesyste primary
3 10485760 20971519 5G Linux filesyste primary
4 20971520 188743679 80G Linux filesyste primary
5 188743680 209713151 10G Linux filesyste primary
Disk /dev/vdb: 32.2 GB, 32212254720 bytes, 62914560 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


表示當前的服務器有兩塊磁盤,“/dev/vda”是系統盤,“/dev/vdb”是新增數據盤。
- 執行以下命令,進入fdisk分區工具,開始對新增數據盤執行分區操作。
fdisk 新增數據盤
以新掛載的數據盤“/dev/vdb”為例:
fdisk /dev/vdb
回顯類似如下信息:
[root@test-sharedDisk ~]# 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 0x59ac6ad9.
Command (m for help):

- 輸入“n”,按“Enter”,開始新建分區。
回顯類似如下信息:
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):


表示磁盤有兩種分區類型:
- “p”表示主要分區。
- “e”表示延伸分區。
- 以創建一個主要分區為例,輸入“p”,按“Enter”,開始創建一個主分區。
回顯類似如下信息:
Select (default p): p
Partition number (1-4, default 1): 1


“Partition number”表示主分區編號,可以選擇1-4。
- 以分區編號選擇“1”為例,輸入主分區編號“1”,按“Enter”。
回顯類似如下信息:
Partition number (1-4, default 1): 1
First sector (2048-62914559, default 2048):


“First sector”表示初始磁柱區域,可以選擇2048-62914559,默認為2048。
- 以選擇默認初始磁柱編號2048為例,按“Enter”。
回顯類似如下信息:
First sector (2048-62914559, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559):


“Last sector”表示截止磁柱區域,可以選擇2048-62914559,默認為62914559。
- 以選擇默認截止磁柱編號62914559為例,按“Enter”。
回顯類似如下信息:
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559):
Using default value 62914559
Partition 1 of type Linux and of size 30 GiB is set
Command (m for help): p


表示分區完成,即為10GB的數據盤新建了1個分區。
- 輸入“p”,按“Enter”,查看新建分區的詳細信息。
回顯類似如下信息:
Command (m for help): p
Disk /dev/vdb: 32.2 GB, 32212254720 bytes, 62914560 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: 0x2c6e939c
Device Boot Start End Blocks Id System
/dev/vdb1 2048 62914559 31456256 83 Linux
Command (m for help): w


表示新建分區“/dev/vdb1”的詳細信息。
- 輸入“w”,按“Enter”,將分區結果寫入分區表中。
回顯類似如下信息:。
Command (m for help): w
The partition table has been altered!
Calling ioctl() [ 2244.854718] vdb: vdb1
to re-read partition table.
Syncing disks.
[ 2244.860481] vdb: vdb1


表示分區創建完成。
說明如果之前分區操作有誤,請輸入“q”,則會退出fdisk分區工具,之前的分區結果將不會被保留。
- 執行以下命令,將新的分區表變更同步至操作系統。
Partprobe
- 執行以下命令,將新建分區文件系統設為系統所需格式。
mkfs -t 文件系統格式 /dev/vdb1
以設置文件系統為“ext4”為例:
mkfs -t ext4 /dev/vdb1
回顯類似如下信息:
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
1966080 inodes, 7864064 blocks
393203 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2155872256
240 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done


格式化需要等待一段時間,請觀察系統運行狀態,不要退出。
說明不同文件系統支持的分區大小不同,請根據您的業務需求選擇合適的文件系統。
- 執行以下命令,新建掛載點。
mkdir 掛載點
以新建掛載點“/mnt/sdc”為例:
mkdir /mnt/sdc
- 執行以下命令,將新建分區掛載到12中新建的掛載點下。
mount /dev/vdb1 掛載點
以掛載新建分區至“/mnt/sdc”為例:
mount /dev/vdb1 /mnt/sdc
[root@test-sharedDisk ~]# mount /dev/vdb1 /mnt/sdc
[ 3118.784219] EXT4-fs (vdb1): mounted filesystem with ordered data mode. Opts: (null)


- 執行以下命令,查看掛載結果。
df -TH
回顯類似如下信息:


表示新建分區“/dev/vdb1”已掛載至“/mnt/sdc”。
設置開機自動掛載磁盤
操作場景
如果您需要在服務器系統啟動時自動掛載磁盤,不能采用在/etc/fstab直接指定 /dev/vdb1的方法,因為云中設備的順序編碼在關閉或者開啟服務器過程中可能發生改變,例如/dev/vdb可能會變成/dev/vdc。推薦使用UUID來配置自動掛載數據盤。
說明磁盤的UUID(Universally Unique Identifier)是Linux系統為磁盤分區提供的唯一的標識字符串。
操作步驟
- 執行如下命令,查詢磁盤分區的UUID。
blkid 磁盤分區
以查詢磁盤分區“/dev/vdb1”的UUID為例:
blkid /dev/vdb1
回顯類似如下信息:
[root@test-sharedDisk ~]# blkid /dev/vdb1
/dev/vdb1: UUID="8f42fd24-1731-4016-8058-19795c3153c8" TYPE="ext4"


- 執行以下命令,使用vi編輯器打開“fstab”文件。
vi /etc/fstab
- 按“i”,進入編輯模式。
- 將光標移至文件末尾,按“Enter”,添加如下內容:UUID=8f42fd24-1731-4016-8058-19795c3153c8 /mnt/sdc ext4 defaults 0 2
UUID=3C27-7C31 /boot/efi vfat defaults 0 0
UUID=d257ff0f-c5e1-4d90-af3a-de94720d90fe /boot ext2 defaults 0 0
UUID=9136110f-0c77-4174-9870-e8243dbe2662 / xfs defaults 0 1
UUID=8f42fd24-1731-4016-8058-19795c3153c8 /mnt/sdc ext4 defaults 0 2


5.按“ESC”后,輸入:wq,按“Enter”保存設置并退出編輯器。

