分區的幾個概念
硬盤分區有三種,主分區、擴展分區、邏輯分區。
一個硬盤主分區至少有1個,最多4個,擴展分區可以沒有,最多1個。且主分區+擴展分區總共不能超過4個。邏輯分區可以有若干個。
在windows下激活的主分區是硬盤的啟動分區,他是獨立的,也是硬盤的第一個分區,正常分的話就是C區。 在linux下主分區和邏輯分區都可以用來放系統,引導os開機。
一塊硬盤分出主分區后,其余的部分可以分成擴展分區,一般是剩下的部分全部分成擴展分區,也可以不全分,那剩的部分就浪費了。
但擴展分區是不能直接用的,他是以邏輯分區的方式來使用的,所以說擴展分區可分成若干邏輯分區。他們的關系是包含的關系,所有的邏輯分區都是擴展分區的一部分。
在linux中第一塊硬盤分區為hda分區,主分區編號為hda1-4,邏輯分區從5開始。
硬盤的容量=主分區的容量+擴展分區的容量
擴展分區的容量=各個邏輯分區的容量之和
fdisk命令
fdisk是一個創建和維護分區的命令,常用用法:
- fdisk [選項] <disk> 改變分區
- fdisk [選項] -l <disk> 列出所有分區
菜單操作說明:
a 設置可引導標記(活動分區/引導分區之間切換)
b 編輯BSD磁盤標簽
c 設置DOS操作系統兼容標記(兼容/不兼容之間切換)
d 刪除一個分區
l 顯示已知的分區類型,其中82為Linux swap分區,83為Linux分區
m 顯示幫助信息
n 增加一個新的分區
o 創建一個新的空白的DOS分區表
p 顯示磁盤當前的分區表
q 退出fdisk程序,不保存任何修改
s 創建一個新的空白的Sun磁盤標簽
t 改變一個分區的系統ID,就是改變分區類型(比如把Linux Swap分區改為Linux分區)
u 改變顯示或輸入單位
v 驗證磁盤分區表
w 將分區表寫入磁盤並退出(保存並退出)
x 額外功能(專家級)
實例一
顯示當前分區情況:fdisk -l
[root@localhost ~]# fdisk -l Disk /dev/sda: 250.1 GB, 250059350016 bytes 255 heads, 63 sectors/track, 30401 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xd10cd10c
#從上面信息可以看出,這個硬盤是250G的,有255個磁面;63個扇區;30401個磁柱;每個 cylinder(磁柱)的容量是 8225280 bytes=8225.280 K(約為)=8.225280M(約為);
Device Boot Start End Blocks Id System /dev/sda1 1 3917 31463271 7 HPFS/NTFS /dev/sda2 3918 30401 212732730 f W95 Ext'd (LBA) /dev/sda5 3918 12841 71681998+ 7 HPFS/NTFS /dev/sda6 12842 21765 71681998+ 7 HPFS/NTFS /dev/sda7 21766 23070 10482381 b W95 FAT32 /dev/sda8 23071 25620 20482843+ 83 Linux /dev/sda9 25621 25747 1020096 82 Linux swap / Solaris /dev/sda10 25748 30401 37383223+ 83 Linux
#通過上面的信息,我們知道此機器中掛載一個硬盤sda ;其中 sda有兩個主分區(包括擴展分區),分別是主分區 sda1 和sda2(擴展分區) ;邏輯分區是 hda5到hda10
#根據起始磁柱的數值,上一個分區的End的值+1 就是下一個分區的Start 的值,所以sda1主分區與sda2擴展分區完全占用了30401個磁柱;
#同理,sda5至sda10也連續無間斷的占用了整個sda2磁柱(3918-30401),說明/dev/sda整個硬盤已完全划分。
實例二
使用操作菜單,創建一個新的分區
[root@jollypay-V4-1 ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xe155fe8e. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) Command (m for help): n //輸入n,表示新建 Command action e extended //擴展分區,創建擴展分區會占用一個主分區,並且只有創建擴展分區后才能創建邏輯分區 p primary partition (1-4) //主分區,最多創建4個 p //輸入p,表示新建主分區 Partition number (1-4): 1 //輸入1,即sdb1分區 First cylinder (1-13054, default 1): //開始磁柱,默認為1(回車) Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): //終止磁柱,默認為最后(回車),不僅僅可以用數值表示,也可以這么寫,+500M Using default value 13054 Command (m for help): w //寫入磁盤,保存並退出,否則之前的操作都無效。如果不想寫入磁盤就使用q The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
注意:fdisk用來對小於2T的磁盤進行分區,如果大於2T需用parted工具
參考:https://www.cnblogs.com/w-wfy/p/8870598.html
https://blog.csdn.net/qq_25408423/article/details/82867492