今天给服务器添加磁盘格式化遇到个问题:4TB的磁盘无法分配所有磁盘空间到分区,最大只能2TB。为什么出现这个问题呢?原来是因为分区类型问题导致的。

关于GPT:

GUID分区表(简称GPT。使用GUID分区表的磁盘称为GPT磁盘)与普遍使用的主引导记录(MBR)分区方案相比,GPT提供了更加灵活的磁盘分区机制。

优点是支持2TB以上的大硬盘;每个磁盘的分区个数几乎没有限制,分区大小也几乎没有限制。

关于MBR:

MBR分区表(master boot record)的英文缩写,简称主引导记录,还可以叫做主引导扇区。它的运行机制存在于硬件的基础之上,它存储在硬盘的第一个扇区上。当我们在硬盘上安装Windows系统时,Windows操作系统的分区软件可以将MBR分区信息写入到硬盘中保存起来。

优点就是兼容性比较好,缺点就是不支持管理大硬盘结构。

GPT和MBR两者的区别:

1、MBR分区表最多只能识别2TB左右的空间,大于2TB的容量将无法识别从而导致硬盘空间浪费;GPT分区表则能够识别2TB以上的硬盘空间。

2、MBR分区表最多只能支持4个主分区或三个主分区+1个扩展分区(逻辑分区不限制);GPT分区表在Windows系统下可以支持128个主分区。

3、在MBR中,分区表的大小是固定的;在GPT分区表头中可自定义分区数量的最大值,也就是说GPT分区表的大小不是固定的。

Windows转换分区我们借助Diskgenius之类分区工具很容易操作,那么Linux上如何操作呢?且看下面操作。

我们先查看分区类型,主要看Partition Table参数,如果参数为msdos那么就是MBR分区,如果为GPT,那么毫无疑问分区类型为GPT。

# check partition type
[root@server ~]# parted /dev/sdb print
Model: ATA WDC WD40EZAZ-00S (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags

下面开始转换分区类型为GPT,进入parted管理界面使用mktable命令。

# convert partition type
[root@server ~]# parted /dev/sdb 
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mktable                                                          
New disk label type? 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? yes                                                               
(parted) q                                                                
Information: You may need to update /etc/fstab.

也可以进行免交互模式进行转换分区类型。

# mbr partition
parted /dev/sdb mklabel msdos -s

# gpt partition
parted /dev/sdb mklabel gpt -s

提示:

按照习惯MBR格式一般在linux下称作dos,如果在New disk label type?后输入dos或者mbr会提示无效命令,这时候要用help mktable查看帮助信息,可以看到parted命令中MBR分区被称作msdos,其它分区如下:aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop

最后再附上分区过程。

# format diskette
[root@server ~]# fdisk /dev/sdb 
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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.


Command (m for help): n
Partition number (1-128, default 1): 
First sector (34-7814037134, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-7814037134, default 7814037134): 
Created partition 1


Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# lsblk 
NAME                     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                        8:0    0 931.5G  0 disk 
├─sda1                     8:1    0   200M  0 part /boot/efi
├─sda2                     8:2    0     1G  0 part /boot
└─sda3                     8:3    0 930.3G  0 part 
  ├─centos_server-root   253:0    0   906G  0 lvm  /
  └─centos_server-swap   253:1    0  23.6G  0 lvm  [SWAP]
sdb                        8:16   0   3.7T  0 disk 
└─sdb1                     8:17   0   3.7T  0 part
[root@server ~]# mkfs.xfs -f /dev/sdb1 
Discarding blocks...Done.
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=244188597 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=976754385, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=476930, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0