關於MBR和GPT這兩種分區表,wiki上講的很清楚。不過有些同學可能看不了,你需要先了解科學上網。這裏我就只把它們的結構圖貼上,簡要說明了。
https://zh.wikipedia.org/wiki/%E4%B8%BB%E5%BC%95%E5%AF%BC%E8%AE%B0%E5%BD%95
https://zh.wikipedia.org/wiki/GUID%E7%A3%81%E7%A2%9F%E5%88%86%E5%89%B2%E8%A1%A8
MBR
MBR使用磁盤的最開始的512Byte,結構圖如下。可以看到只有中間的64Byte是用來描述分區表的,每16Byte描述一個分區,所以最多隻能同時劃分4個主分區。
而描述硬盤分區結構信息的這16Byte是如下這樣的結構。可以發現只有最後4Byte是用來描述分區總的扇區數的,4Byte是32位,也就是分區最大隻能是2^32*512Byte,大概是2.2TB。
# 備份MBR分區表 [root@localhost ~]# dd if=/dev/sda of=/backup/mbr.bak bs=512 count=1
GPT
GPT的結構是下面這樣的,一個邏輯塊(LBA)大小是512Byte。出於兼容性考慮,LBA 0(即硬盤的第一個扇區)仍然用作MBR,之後LBA 1是分區表頭。LBA 2-33都用來描述分區表項,每個LBA可描述4個分區,即每個分區使用128Byte來描述。
另外,GPT分區將在磁盤最後的33個扇區作爲主分區表的備份,稱爲備份分區表。
而每個分區表項是如下這樣的結構,可以看到它使用8Byte描述分區起始位置和結束位置,那麼就是
(2^64-1)*512Byte=9.4ZB(9.4×10^21字節)。
# 備份GPT分區表 [root@localhost ~]# dd if=/dev/sda of=/backup/gpt.bak bs=512 count=34
parted
在執行parted命令過程中,我得到了這樣一個警告信息,說是分區在劃分時沒有對齊,可能會影響性能。
(parted) mkpart boot 0 200M Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel?
參照這兩篇文章
CentOS parted分割大於2TB硬盤的performance問題處理 | 月飛博客
How to align partitions for best performance using parted
parted的交互模式
[root@localhost /]# parted /dev/sdb GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel gpt Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? y (parted) mkpart boot 2048s 200M (parted) mkpart swap 200M 2248M (parted) mkpart data 2248M 100% (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 200MB 199MB ext4 boot 2 200MB 2248MB 2048MB swap 3 2248MB 21.5GB 19.2GB data (parted) rm 3 (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 200MB 199MB ext4 boot 2 200MB 2248MB 2048MB swap (parted) mkpart data 2248M -1 (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 200MB 199MB ext4 boot 2 200MB 2248MB 2048MB swap 3 2248MB 21.5GB 19.2GB data (parted) name 3 backup (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 200MB 199MB ext4 boot 2 200MB 2248MB 2048MB swap 3 2248MB 21.5GB 19.2GB backup (parted) (parted) quit Information: You may need to update /etc/fstab.
危險操作:清除GPT分區表
[root@localhost ~]# dd if=/dev/zero of=/dev/sdb bs=512 count=34
parted的非交互模式
[root@localhost ~]# cat parted-script.sh #!/usr/bin/bash parted -s /dev/sdb mklabel gpt parted -s /dev/sdb mkpart boot 2048s 200MiB parted -s /dev/sdb mkpart swap 200MiB 2248MiB parted -s /dev/sdb mkpart backup 2248Mib 100% parted -s /dev/sdb print [root@localhost homework]# bash parted-script.sh Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 210MB 209MB ext4 boot 2 210MB 2357MB 2147MB swap 3 2357MB 21.5GB 19.1GB backup