Linux系統中有兩個分區工具
- fdisk
- parted
二者都可以對linux的硬盤進行分區,但是二者從細節上來說,又有很大的區別,如果不加註意,有可能造成磁盤空間的浪費。
首先我們來介紹一下磁盤,Linux系統中,每個磁盤中有一部分叫做MBR主引導記錄。
**
MBR
**
- 大小512字節,分爲三部分
- 主引導程序:446字節
- 硬盤分區表:64字節
- 分區結束標記(硬盤有效位):2字節
其中硬盤分區表DPT需要特別注意
DPT的大小是64字節,每個主分區要佔用16字節,擴展分區也要佔用16個字節的主分區空間,所以每個磁盤上最多隻能建立四個主分區
那麼如果需要將磁盤分成5個或者更多個分區的話,可以採取的方法:
- 創建一到三個主分區,將剩下的空間作爲擴展分區,由擴展分區分出多個邏輯分區
- 不創建主分區,全做磁盤作爲擴展分區,由擴展分區分出來多個邏輯分區
接下來就是如何使用fdisk和parted進行分區
單純的使用這兩個工具去進行硬盤分區倒沒什麼難點,兩者都是交互式的界面,如果忘記如何分區,可以進入交互之後使用h或者help查看幫助。
但是在使用二者進行分區的時候,就需要注意一些區別:
1、fdisk
(1)fdisk命令只支持msdos,分區的時候只支持小容量硬盤(<=2T),但是如果不需要分區的話,那麼整塊sdb硬盤,類型爲msdos,那麼他的大小是可以大於2T的。
(2)fdisk命令不支持gpt,所以當使用fdisk命令給gpt類型硬盤分區是會出現告警,就像下面例子中的WARNING
[root@centos6 ~]# fdisk /dev/sdc
WARNING: GPT (GUID Partition Table) detected on '/dev/sdc'! The util fdisk doesn't support GPT. Use GNU Parted.
WARNING: The size of this disk is 5.4 TB (5368709120000 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID
partition table format (GPT).
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help):
(3)當使用parted命令給一個5T的硬盤分好三個分區之後,在使用fdisk命令查看,會不兼容(parted打印的分區有三個,而fdisk命令打印的分區只有一個)
[root@centos6 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 5369GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
[root@centos6 ~]# parted /dev/sdc
GNU Parted 2.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart a 1 100G
(parted) mkpart b 100G 500G
(parted) mkpart c 500G 2000G
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 5369GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 1049kB 100GB 100GB ext4 a
2 100GB 500GB 400GB b
3 500GB 2000GB 1500GB c
(parted) quit
Information: You may need to update /etc/fstab.
[root@centos6 ~]# fdisk -l /dev/sdc
WARNING: GPT (GUID Partition Table) detected on '/dev/sdc'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sdc: 5368.7 GB, 5368709120000 bytes
255 heads, 63 sectors/track, 652708 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdc1 1 267350 2147483647+ ee GPT
[root@centos6 ~]#
2、parted:
(1)支持msdos和gpt,可以支持大硬盤,也支持小硬盤。
(2)區分parted命令給msdos類型和gpt類型硬盤分區的不同:
①下面是parted給msdos類型硬盤分區:
[root@centos6 ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
(parted) mkpart aaa 1 10G 使用mkpart添加分區
parted: invalid token: aaa 顯示無效的輸入
Partition type? primary/extended? primary 這裏要求選擇分區類型(主分區/擴展分區?)先創建一個主分區
File system type? [ext2]? ext4 這裏提醒確定文件系統的類型,輸入ext4
Start? 1 分區的開始:1
End? 10G 分區結束:10G那麼就確定了分區是一個10G大小的分區
(parted) print print打印分區表
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 10.0GB 9999MB primary
這裏可以看到一個大小爲10G,類型爲主分區的文件系統已經創建完成
(parted)
②下面是parted給gpt類型硬盤分區:
[root@centos6 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 5369GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
[root@centos6 ~]# parted /dev/sdc
GNU Parted 2.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart a 1 100G
(parted) mkpart b 100G 500G
(parted) mkpart c 500G 2000G
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 5369GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 1049kB 100GB 100GB ext4 a
2 100GB 500GB 400GB b
3 500GB 2000GB 1500GB c
(parted)
3、msdos
- 支持小於2T的硬盤,不支持大於2T的硬盤
- 有主分區,擴展分區,邏輯分區
- 如果使用fdisk命令給一個大於2T的msdos類型硬盤分區,只能給2T的部分分區,超過2T的部分不能使用,也就浪費了。
4、gpt
- 支持大小容量的硬盤
- 不區分主分區,擴展分區,邏輯分區,也沒有4個分區數量的限制