fdisk是Linux系統中最常用的分區工具,通過這個命令也可以查看系統中所有可用的分區,但是這個命令只支持MBR的分區表(這句話應該只對某些系統,CentOS7-1810適用,Debian9.5和openSUSE15應該已經支持GPT分區表,下面對fdisk的操作都是在Debian9.5下操作),所以這個命令不能對大於2T的硬盤進行分區,大於2T的硬盤我們需要用GPT分區表來進行分區,GPT分區我們就要用gdisk或parted分區工具來進行分區。
Linux 內核是如何理解硬盤的?
作為人類,我們可以很輕松地理解一些事情;但是電腦就不是這樣了,它們需要合適的命名才能理解這些。
在 Linux 中,外圍設備都位於 /dev 掛載點,內核通過以下的方式理解硬盤:
/dev/hdX[a-z]: IDE 硬盤被命名為 hdX
/dev/sdX[a-z]: SCSI 硬盤被命名為 sdX
/dev/xdX[a-z]: XT 硬盤被命名為 xdX
/dev/vdX[a-z]: 虛擬硬盤被命名為 vdX
/dev/fdN: 軟盤被命名為 fdN
/dev/scdN or /dev/srN: CD-ROM 被命名為 /dev/scdN 或 /dev/srN
fdisk 允許我們在每塊硬盤上創建最多四個主分區。它們中的其中一個可以作為擴展分區,並下設多個邏輯分區。1-4 扇區作為主分區被保留,邏輯分區從扇區 5 開始。
一、fdisk參數使用方法:
fdisk [-l] 設備名
參數
-l :顯示指定磁盤設備的分區表信息,如果沒有指定磁盤設備,則顯示/proc/partitions 文件中的信息,也就是系統中所有的磁盤設備。
舉例:
-----------------------------------------------------------
root@debian:~# fdisk -l #沒有指定任何磁盤設備
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors #第一塊磁盤/dev/sda容量20G
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos #分區表的類型為MBR (dos表示MBR分區表,gpt表示GPT分區表)
Disk identifier: 0xefdc905c
Device Boot Start End Sectors Size Id Type #磁盤/dev/sda下的分區
/dev/sda1 * 2048 37748735 377466881 8G 83 Linux
/dev/sda2 37750782 41940991 4190210 2G 5 Extended
/dev/sda5 37750784 41940991 4190208 2G 82 Linux swap / Solaris
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors #第二塊磁盤/dev/sdb容量5G
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
root@debian:~#
-----------------------------------------------------------
使用 "fdisk -l" 查看分區信息,不同的發行版可能顯示的信息不同,通過上面的信息能夠看到我們添加的兩塊硬盤(/dev/sda 和 /dev/sdb)的信息。我們解釋一下這些信息,其上半部分是硬盤的整體狀態,/dev/sda 硬盤的總大小是20GB等信息。
信息的下半部分是分區的信息,共 7 列,含義如下:
Device:分區的設備文件名。
Boot:是否為啟動引導分區,在這里 /dev/sda1 為啟動引導分區。
Start:起始柱面,代表分區從哪里開始。
End:終止柱面,代表分區到哪里結束。
Sectors:分區的大小,單位是扇區。
Size:分區的容量
id:分區內文件系統的 ID。
Type:分區的文件系統是什么。
二、fdisk交互模式磁盤分區
通過命令“fdisk 設備文件名” 就可以進入fdisk程序的交互模式,在交互模式中可以通過輸入fdisk程序所提供的指令完成相應的操作,舉例如下
-----------------------------------------------------------
root@debian:~# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xe3779e93.
Command (m for help): m #輸入m可以獲得幫助信息
Help:
DOS (MBR)
a toggle a bootable flag #設置可引導標記
b edit nested BSD disklabel #編輯 bsd 磁盤標簽
c toggle the dos compatibility flag #設置 DOS 操作系統兼容標記
G eneric
d delete a partition #刪除一個分區
F list free unpartitioned space #列出空閑的未分區空間
l list known partition types #顯示已知的分區類型。82 為 Linux swap 分區,83 為 Linux 分區
n add a new partition # 新建分區
p print the partition table #顯示當前磁盤的分區列表
t change a partition type #改變分區類型
v verify the partition table #驗證分區表
i print information about a partition #打印有關分區的信息
Misc
m print this menu #顯示幫助菜單
u change display/entry units #改變顯示記錄單位
x extra functionality (experts only) #附加功能(僅專家)
Script
I load disk layout from sfdisk script file #從sfdisk腳本文件加載磁盤布局
O dump disk layout to sfdisk script file #將磁盤布局轉儲到sfdisk腳本文件
Save & Exit
w write table to disk and exit #保存退出
q quit without saving changes #不保存退出
Create a new label
g create a new empty GPT partition table #新建空白GPT分區表,這個應該就表明fdisk已經支持GPT,還需要進一步的認證。
G create a new empty SGI (IRIX) partition table #新建空白IRIX分區表
o create a new empty DOS partition table #新建空白DOS分區表
s create a new empty Sun partition table #新建空白SUN磁盤標簽
Command (m for help):
-----------------------------------------------------------
三、分區管理
1、新建主分區
root@debian:~# fdisk /dev/sdb #要新建分區的磁盤/dev/sdb不要加數字加數字就變成了分區
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xd9b1702f.
Command (m for help): n #輸入n新建分區,默認直接創建MBR分區表
Partition type
p primary (0 primary, 0 extended, 4 free) #主分區
e extended (container for logical partitions) #擴展分區
Select (default p): p #輸入p新建主分區
Partition number (1-4, default 1): #輸入分區號,直接回車就行,默認為1
First sector (2048-10485759, default 2048): #輸入起始扇區,直接回車就行,默認2048最前面開始
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +1G #這里輸入新增分區的大小可以通過扇區數來增加,也可以通過+size{K,M,G,T,P}方式來增加,這里要增加1G的容量就表示為+1G
Created a new partition 1 of type 'Linux' and of size 1 GiB. #提示創建了1G的分區
Command (m for help): p #輸入p查看創建的分區
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos #分區表類型為MBR
Disk identifier: 0xd9b1702f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux
2、剩余可用的空間
Command (m for help): F #輸入F查看剩余可用的空間
Unpartitioned space /dev/sdb: 4 GiB, 4293918720 bytes, 8386560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Start End Sectors Size
2099200 10485759 8386560 4G #可以看到可用空間為4G
3、創建擴展分區及邏輯分區
Command (m for help): n #輸入n繼續創建分區
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e #輸入e創建擴展分區
Partition number (2-4, default 2): #輸入擴展分區號,直接回車就可以
First sector (2099200-10485759, default 2099200): #輸入起始扇區數,直接回車就可以
Last sector, +sectors or +size{K,M,G,T,P} (2099200-10485759, default 10485759): +2G #增加2G的容量
Created a new partition 2 of type 'Extended' and of size 2 GiB. #創建擴展分區成功
Command (m for help): n #輸入n繼續創建分區
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5) #只有創建了擴展分區才會有這個選項
Select (default p): l #輸入l創建邏輯分區
Adding logical partition 5 #擴展分區號從5開始
First sector (2101248-6293503, default 2101248):
Last sector, +sectors or +size{K,M,G,T,P} (2101248-6293503, default 6293503): +1G
Created a new partition 5 of type 'Linux' and of size 1 GiB.
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0xd9b1702f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux #主分區
/dev/sdb2 2099200 6293503 4194304 2G 5 Extended #擴展分區
/dev/sdb5 2101248 4198399 2097152 1G 83 Linux #邏輯分區
4、刪除分區
Command (m for help): d #輸入d刪除分區
Partition number (1,2,5, default 5): 2 #刪除擴增分區2,上面的邏輯分區也會一同刪除
Partition 2 has been deleted.
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0xd9b1702f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux
Command (m for help):
5、修改分區類型
Command (m for help): l #查看可用分區類型
#其中82為Linux swap分區、83為Linux分區、8e為LinuxLVM分區、b為Windows FAT32分區、e為Windows FAT16分區。
Command (m for help): t #輸入t更改分區類型
Partition number (1,2,5, default 5): 5 #更改分區5的類型
Partition type (type L to list all types): 8e #輸入更改分區類型為8e,這里輸入l也可以查看分區的類型
Changed type of partition 'Linux' to 'Linux LVM'. #提示更改成功
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0xd9b1702f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux
/dev/sdb2 2099200 6293503 4194304 2G 5 Extended
/dev/sdb5 2101248 4198399 2097152 1G 8e Linux LVM #更改后的分區類型
Command (m for help):
6、保存修改的結果
Command (m for help): w #輸入w保存配置,如果不想保存可以輸入q退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
使用w指令保存后,則在fdisk中所做的所有操作都會生效,且不可回退。如果分區表正忙,則需要重啟機器后才能使新的分區表生效。
四、新建GPT分區
不是所有的發行版帶的fdisk都支持GPT分區這里請注意
1、新建分區
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos #現在的分區類型為MBR
Disk identifier: 0xd9b1702f
Command (m for help): g #輸入g創建GPT分區
Created a new GPT disklabel (GUID: 8C0F4613-2830-40E1-8D23-596D1B7F5DDB).
Command (m for help): n #新建分區
Partition number (1-128, default 1): #可以看到這里沒有擴展分區的概念了
First sector (2048-10485726, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485726, default 10485726): +1G
Created a new partition 1 of type 'Linux filesystem' and of size 1 GiB.
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: gpt
Disk identifier: 8C0F4613-2830-40E1-8D23-596D1B7F5DDB
Device Start End Sectors Size Type #GPT下輸入p顯示的分區信息和MBR下略有不同,沒有分區類型id的選項
/dev/sdb1 2048 2099199 2097152 1G Linux filesystem #成功創建了一個主分區
Command (m for help):
2、 修改分區的類型
Command (m for help): l #查看分區的類型,GPT下同樣的分區類型對應的編號不同了,這里要注意
上面只是顯示了部分按空格顯示更多的信息
Command (m for help): t #輸入t更改分區類型,這里輸入l也可以查看分區的類型
Partition number (1,2, default 2): 2 #輸入要更改的分區
Hex code (type L to list all codes): 31 #輸入分區類型的編號
Changed type of partition 'Linux filesystem' to 'Linux LVM'. #更改分區類型成功
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: gpt
Disk identifier: 836D334F-D0EE-4AD3-8581-EC9ABFF31F50
Device Start End Sectors Size Type
/dev/sdb1 2048 2099199 2097152 1G Linux filesystem
/dev/sdb2 2099200 4196351 2097152 1G Linux LVM #分區類型為已經更改
Command (m for help):