Mozatr的不定期更新,處于學習階段的Mozart,愿不斷分享,不斷進步。
此次分享一個重要的管理分區命令:fdisk ,我們一一闡述。
(一)分區
首先我們了解下什么是分區,分區是將一個硬盤驅動器分成若干個邏輯驅動器,分區是把硬盤連續的區塊當做一個獨立的磁盤使用。分區表是一個硬盤分區的索引,分區的信息都會寫進分區表。
分區的優點:
(1)優化I/O性能
(2)實現磁盤空間配額限制增加磁盤空間使用效率
(3)提高修復速度
(4)隔離系統和程序
(5)安裝多個OS
(6)采用不同文件系統
(7)防止數據丟失
等等
(二)分區機制
其次在Linux系統中有MBR和GPT兩中分區機制,其中MBR的使用比較多,但是有限制。GPT是比MBR更新的一個分區機制。
兩者的區別:
MBR(Master Boot Record)主引導記錄,是傳統的分區機制,應用于絕大多數使用BIOS的PC設備。
MBR支持32位和64位系統。
MBR支持分區數量有限。
MBR只支持不超過2T的硬盤,超過2T的硬盤將只能用2T空間(有第三方解決方法)。
GPT(GUID Partition Table)全局唯一標識分區表,是一個較新的分區機制,解決了MBR很多缺點。
支持超過2T的磁盤(64位尋址空間)。fdisk最大只能建立2TB大小的分區,創建一個大于2TB的分區使用parted。
向后兼容MBR。
必須在支持uEFI的硬件上才能使用(Intel提出,用于取代BIOS)。
必須使用64位系統。
Mac、Linux系統都能支持GPT分區格式。
Windows 7/8 64bit、Windows Server 2008 64bit支持GPT。
總得來說GPT比MBR更先進,但是MBR的兼容性比GPT要好。
(三)fdisk 分區管理命令
重點接踵而至:接下來我們對fdisk命令管理磁盤進行詳解:
fdisk是對基于MBR機制分區的管理磁盤命令
(1) fdisk -l [-u -c …] [disk] 查看磁盤分區情況
CentOS6版本下:
[root@centos6 ~]# fdisk -l /dev/sda #默認以nondos模式列出磁盤情況 Disk /dev/sda: 214.7 GB, 214748364800 bytes #磁盤總大小 255 heads, 63 sectors/track, 26108 cylinders #磁盤“三圍”,heads 磁頭數 sectors/track 扇區/每磁道 cylinders 柱面數 Units = cylinders of 16065 * 512 = 8225280 bytes #單位:柱面以及柱面的大?。ㄗ止潱?Sector size (logical/physical): 512 bytes / 512 bytes #扇區大小512字節(固定) I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0004b712 Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux Partition 1 does not end on cylinder boundary. #提示分區1沒有在柱面邊界結束 (nondos模式與dos模式的區別處) /dev/sda2 131 6658 52428800 83 Linux /dev/sda3 6658 13185 52428800 83 Linux /dev/sda4 13185 26109 103808000 5 Extended /dev/sda5 13186 13447 2097152 82 Linux swap / Solaris #具體的分區情況,以柱面為分割 [root@centos6 ~]# fdisk -lc /dev/sda #以dos模式列出 ......... Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux /dev/sda2 131 6658 52428800 83 Linux /dev/sda3 6658 13185 52428800 83 Linux /dev/sda4 13185 26109 103808000 5 Extended /dev/sda5 13186 13447 2097152 82 Linux swap / Solaris [root@centos6 ~]# fdisk -lu /dev/sda #具體分區情況以字節列出 Disk /dev/sda: 214.7 GB, 214748364800 bytes 255 heads, 63 sectors/track, 26108 cylinders, total 419430400 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 identifier: 0x0004b712 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 2099200 106956799 52428800 83 Linux /dev/sda3 106956800 211814399 52428800 83 Linux /dev/sda4 211814400 419430399 103808000 5 Extended /dev/sda5 211818496 216012799 2097152 82 Linux swap / Solaris
CentOS7版本下:
[root@localhost ~]#fdisk -l /dev/sda #默認nondos模式列出磁盤情況,具體內容與CentOS6相似,具體分區情況以字節為分割 ....... Device Boot Start End Blocks Id System /dev/sda1 * 2048 1953791 975872 83 Linux /dev/sda2 1953792 99608575 48827392 83 Linux [root@localhost ~]#fdisk -l -c=dos /dev/sda #以dos模式列出磁盤情況 ...... Device Boot Start End Blocks Id System /dev/sda1 * 2048 1953791 975872 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 1953792 99608575 48827392 83 Linux ........ [root@localhost ~]#fdisk -l -c=dos -u=cylinder /dev/sda #具體分區情況以柱面為分割列出 Device Boot Start End Blocks Id System /dev/sda1 * 1 122 975872 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 122 6201 48827392 83 Linux
補充知識:對于IDE硬盤,每塊盤有一個設備名:對應于主板的四個IDE接口,設備名依次為:/dev/hda,/dev/hdb,/dev/hdc,/dev/hdd等,
如果還有IDE Raid卡,則依次為:/dev/hde,/dev/hdf,/dev/hdg,/dev/hdh。
對于SCSI硬盤,則設備名依次為/dev/sda,/dev/sdb…等等
(2) fdisk device 管理具體分區
(Centos7下)
fdisk device
進入交互模式
常用子命令:
m 幫助 | n 建立新分區 | d 刪除分區 | p 預覽分區信息 |
t 轉換分區類型 | a 將指定分區設置/取消為活動分區 | L/l 查看分區類型 | o 重建分區表 |
v 驗證分區表,顯示剩余沒有被分區劃分的扇區數量 | g 創建GPT格式的分區(不建議使用fdisk創建GPT分區) | q 退出不保存 | w 退出并保存,不保存,所有的修改都不生效 |
示例:
[root@localhost ~]#fdisk /dev/sdb 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): p #預覽分區信息 Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0xd0fc70a4 Device Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 83 Linux Command (m for help): n #創建新分區 Partition type: p primary (1 primary, 0 extended, 3 free) #創建主分區 e extended #創建擴展分區 Select (default p): p Partition number (2-4, default 2): #默認依次創建 First sector (2099200-104857599, default 2099200): #默認從上一分區結束空間開始 Using default value 2099200 Last sector, +sectors or +size{K,M,G} (2099200-104857599, default 104857599): +1G #默認分取剩余全部空間,可以以扇區或指定大小分取空間 Partition 2 of type Linux and of size 1 GiB is set Command (m for help): p Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0xd0fc70a4 Device Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 83 Linux /dev/sdb2 2099200 4196351 1048576 83 Linux Command (m for help): w #保存并退出 The partition table has been altered! #顯示分區表已經修改 Calling ioctl() to re-read partition table. #顯示分區情況已經同步到磁盤,分區完成。 Syncing disks.
fdisk命令使用注意事項:
(1)fdisk命令只有root用戶權限才能夠運行;
(2)使用fdisk -l 可以實時列出所有安裝的磁盤及其分區信息;
(3)使用fdisk /dev/sdb 可以對目標磁盤進行交互式分區操作;
(4)查看分區實時更新信息:cat /proc/partitions
(5)管理分區之后需要使用partprobe(CentOS7)命令讓內核更新分區信息,否則需要重啟才能識別新的分區或者參看(6);(CentOS6中partprobe命令存在bug)
(6)通知內核重新讀取硬盤分區表(不用重啟) (CentOS6/7通用)
新增分區用
partx -a DEVICE
刪除分區用
partx -d –nr M-N DEVICE
示例:當對系統所在磁盤進行添加刪除分區時,會提示:
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. #無法同步分區表
此時,可以選擇重啟電腦或者進行如下操作(CentOS6/7通用):
對于添加分區:
[root@localhost ~]#partx -a /dev/sda #告訴內核當前磁盤分區情況 #同步完成
對于刪除分區:
[root@localhost ~]#partx -d --nr n /dev/sda #n:刪除的分區號 支持范圍更新n-m
fdisk命令到此已全部介紹完畢,以上示例均親測有效,不足之處請大家多多諒解,同時歡迎大佬指教。
PS:博文寫到最后,只想說一句話:第一次感覺英語這么有用?。?!
原創文章,作者:Mozart,如若轉載,請注明出處:http://www.www58058.com/84971