1. 程式人生 > >關於硬盤分區管理mbr gpt

關於硬盤分區管理mbr gpt

mbr gpt


硬盤管理


一:硬盤基礎概念

硬盤最基本的組成部分是由堅硬金屬材料制成的塗以磁性介質的盤片,不同容量硬盤的盤片數不等。每個盤片有兩面,都可記錄信息。盤片被分成許多扇形的區 域,每個區域叫一個扇區,每個扇區可存儲128×2的N次方(N=0.1.2.3)字節信息

盤片表 面上以盤片中心為圓心,不同半徑的同心圓稱為磁道。硬盤中,不同盤片相同半徑的磁道所組成的圓柱稱為柱面。磁道與柱面都是表示不同半徑的圓,在許多場合, 磁道和柱面可以互換使用,我們知道,每個磁盤有兩個面,每個面都有一個磁頭,習慣用磁頭號來區分。扇區,磁道(或柱面)和磁頭數構成了硬盤結構的基本參 數,幫這些參數可以得到硬盤的容量,基計算公式為:

存儲容量=磁頭數×磁道(柱面)數×每道扇區數×每扇區字節數
要點:(1)硬盤有數個盤片,每盤片兩個面,每個面一個磁頭
(2)盤片被劃分為多個扇形區域即扇區
(3)同一盤片不同半徑的同心圓為磁道
(4)不同盤片相同半徑構成的圓柱面即柱面
(5)公式: 存儲容量=磁頭數×磁道(柱面)數×每道扇區數×每扇區字節數
(6)信息記錄可表示為:××磁道(柱面),××磁頭,××扇區

磁道:

當磁盤旋轉時,磁頭若保持在一個位置上,則每個磁頭都 會在磁盤表面劃出一個圓形軌跡,這些圓形軌跡就叫做磁道。這些磁道用肉眼是根本看不到的,因為它們僅是盤面上以特殊方式磁化了的一些磁化區,磁盤上的信息 便是沿著這樣的軌道存放的。相鄰磁道之間並不是緊挨著的,這是因為磁化單元相隔太近時磁性會相互產生影響,同時也為磁頭的讀寫帶來困難。

扇區:

磁盤上的每個磁道被等分為若幹個弧段,這些弧段便是磁盤的扇區,每個扇區可以存放512個字節的信息,磁盤驅動器在向磁盤讀取和寫入數據時,要以扇區為單位。1.44MB3.5英寸的軟盤,每個磁道分為18個扇區。
柱面:硬盤通常由重疊的一組盤片構成,每個盤面都被劃分為數目相等的磁道,並從外緣的“0”開始編號,具有相同編號的磁道形成一個圓柱,稱之為磁盤的柱面。磁盤的柱面數與一個盤面上的磁道數是相等的。由於每個盤面都有自己的磁頭,因此,盤面數等於總的磁頭數。所謂硬盤的CHS,即Cylinder(柱面)、Head(磁頭)、Sector(扇區),只要知道了硬盤的CHS的數目,即可確定硬盤的容量,硬盤的容量=柱面數*磁頭數*扇區數*512B


二:MBR

主引導記錄(MBR,Main Boot Record)是位於磁盤最前邊的一段引導(Loader)代碼。它負責磁盤操作系統(DOS)對磁盤進行讀寫時分區合法性的判別、分區引導信息的定位,它由磁盤操作系統(DOS)在對硬盤進行初始化時產生的

通常,我們將包含MBR引導代碼的扇區稱為主引導扇區。因這一扇區中,引導代碼占有絕大部分的空間,故而將習慣將該扇區稱為MBR扇區(簡稱MBR)。由於這一扇區承擔有不同於磁盤上其他普通存儲空間的特殊管理職能,作為管理整個磁盤空間的一個特殊空間,它不屬於磁盤上的任何分區,因而分區空間內的格式化命令不能清除主引導記錄的任何信息。

優點:MBR支持32bit和64bit系統;

缺點:1) MBR支持的分區數量有限制;2)MBR只支持不超過2TB的硬盤,超過2TB的硬盤只能使用2TB的空間(但是有第三方解決方法),應為MBR的尋址空間只有32bit長;


MBR結構:


主引導扇區由三個部分組成(共占用512個字節):

1.主引導程序即主引導記錄(MBR)(占446個字節)

可在FDISK程序中找到,它用於硬盤啟動時將系統控制轉給用戶指定的並在分區表中登記了的某個操作系統。

2.磁盤分區表項(DPT,Disk Partition Table)

由四個分區表項構成(每個16個字節)。

負責說明磁盤上的分區情況,其內容由磁盤介質及用戶在使用FDISK定義分區時決定。(具體內容略)

3.結束標誌(占2個字節)

其值為AA55,存儲時低位在前,高位在後,即看上去是55AA(十六進制)。

分類

(1) 主分區:一塊硬盤最多只能創建4個主分區(因為在MBR分區表結構中中最多可以創建4個主分區表信息,也就是4個16字節的空間,如上圖)

(2)擴展分區:一個擴展分區會占用一個主分區的位置

(3) 邏輯分區:

1)是基於擴展分區創建出來的,先有擴展分區,然後在擴展分區的基礎上再創建邏輯分區;也就是說我們要使用邏輯分區,必須先要創建擴展分區;

2)擴展分區的空間是不能被直接使用的,我們必須在擴展分區的基礎上去建立邏輯分區,才能夠被使用;

3)Linux最多支持63個IDE分區和15個SCSI分區

三:GPT

GPT(GUID Partition Table):是一個較新的分區機制,解決了MBR的很多缺點

1)支持超過2TB的磁盤:GPT有64bit尋址空間;而MBR對硬盤空間地址尋址最多只支持32bit,2的32次方,硬盤空間是以地址方式來被識別的,所以MBR只支持容量為2TB以內的磁盤

2)向後兼容MBR

3)但是底層硬件必須支持UEFI(Intel提出的取代BIOS的新一代的引導系統)才能使用,也就是底層硬件必須使用UEFI

4)必須使用64位操作系統

5)Mac、LInux系統都能支持GPT分區格式

6)Windows 7 64bit、Windows Server2008 64bit支持GPT


四:磁盤分區

*註:分區並不是硬盤的物理功能,而是軟件功能


1:查看磁盤信息

fdisk -l ##系統中的所有磁盤設備
df -TH ##系統正在掛載的磁盤設備
blkid ##系統可以掛載的磁盤設備id

2:磁盤分區

(1)mbr硬盤分區

[[email protected] mnt]# 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.


Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (1435648-20971519, default 1435648):
Using default value 1435648
Last sector, +sectors or +size{K,M,G} (1435648-20971519, default 20971519): +200M
Partition 3 of type Linux and of size 200 MiB is set

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

(2)新建gpt分區

[[email protected] ~]# fdisk /dev/vdb
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):
Command (m for help): n
Partition number (1-128, default 1):
First sector (34-20971486, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +2M
Created partition 1


Command (m for help):

3:分區操作
partprobe ##同步分區表
mkfs.xfs /dev/vdbx ##格式化
mount /dev/vdb1 /mnt ##臨時掛載
vim /etc/fstab ##永久掛載
編寫格式:
device mountpoint ftype defaults(mountpoint) 0 0
/dev/vdb1 /mnt xfs defaults 0 0
mount -a ##使/etc/fastab中記錄的掛載生效

[[email protected] mnt]# partprobe 
[[email protected] mnt]# cat /proc/partitions
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 512000 vdb1
253 18 204800 vdb2
253 19 204800 vdb3
252 0 507904 dm-0

4:刪除分區

先使用umount命令解除掛載
註:當掛載點被使用時,無法解除掛載,需要用fuser -kvm結束使用的進程,然後進行解除掛載
解除掛載之後,使用fdisk /dev/vdb命令 刪除分區

[[email protected] mnt]# fdisk /dev/vdb   ##劃分/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.


Command (m for help): d
Partition number (1-3, default 3): 1
Partition 1 is deleted

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.


5:設置分區方式mbr轉換為gpt

在linux我們可以先把大容量的磁盤進行轉換,轉換為GPT格式。由於GPT格式的磁盤相當於原來MBR磁盤中原來保留4個 partition table的4*16個字節只留第一個16個字節,其它的類似於擴展分區,真正的partition table在512字節之後,所以對GPT磁盤表來講沒有四個主分區的限制。
對GPT磁盤分區表進行操作,我們要使用功能強大的parted命令。

mbr 轉為gpt

[[email protected] ~]# parted /dev/vdb                                      
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type ‘help‘ to view a list of commands.
(parted) mklabel
New disk label type?
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? gpt
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk
will be lost. Do you want to continue?
Yes/No? Yes
(parted) quit
Information: You may need to update /etc/fstab.



6:判斷當前硬盤是gpt還是mbr。

[[email protected] ~]# fdisk -l /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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


7:gpt轉mbr

[[email protected] ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type ‘help‘ to view a list of commands.
mklabel mkpart mktable
(parted) mklabel msdos
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk
will be lost. Do you want to continue?
Yes/No? Yes
(parted) quit


本文出自 “12462896” 博客,請務必保留此出處http://12472896.blog.51cto.com/12462896/1953812

關於硬盤分區管理mbr gpt