用 fdisk 管理分區
fdisk 命令工具默認將磁盤劃分為 mbr 格式的分區
命令: fdisk 設備名 fdisk 命令以交互方式進行操作的,在菜單中選擇相應功能鍵即可

[root@localhost ~]# fdisk /dev/sda
# 對 sda 進行分區
Command (m for help):
# 進入 fdisk 交互界面進行分區
Command (m for help): m
# 輸入 m 可查看幫助信息
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
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
O dump disk layout to sfdisk script file 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
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help): n
# 輸入 n 創建新分區
Partition type
p primary (0 primary, 0 extended, 4 free)
# 輸入 p 創建主分區
e extended (container for logical partitions)
# 輸入 e 創建擴展分區
Select (default p): p
# 輸入 p 創建主分區
Partition number (1-4, default 1): 1
# 選擇主分區號,輸入 1 表示第一個主分區
First sector (2048-10485759, default 2048):
# 直接按回車從當前第 2048 個扇區開始分區
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +300M
# 設置第一個分區為 300M
Created a new partition 1 of type 'Linux' and of size 300 MiB.
# 一個主分區創建完成,大小為 300M
Command (m for help): p
# 輸入 p 查看分區信息
Disk /dev/sda: 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: 0x5bb0744f
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
/dev/sda2 616448 10485759 9869312 4.7G 0 Empty
# 有兩個分區,其中新添加的 sda2 有誤,需要刪除
Command (m for help): d
# 輸入 d 刪除分區
Partition number (1,2, default 2): 2
# 輸入 2 ,選擇需要刪除的是第二個分區
Partition 2 has been deleted.
Command (m for help): p
Disk /dev/sda: 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: 0x5bb0744f Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
# 查看現有分區,有誤的 sda2 已刪除
# 創建一個擴展分區
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): 2
# 選擇分區號,輸入 2 表示第二個分區
First sector (616448-10485759, default 616448):
# 直接按回車從當前第 616448 個扇區開始分區
Last sector, +sectors or +size{K,M,G,T,P} (616448-10485759, default 10485759): +500M
# 添加一個 500M 的分區
Created a new partition 2 of type 'Extended' and of size 500 MiB.
Command (m for help): p
Disk /dev/sda: 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: 0x5bb0744f
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 616447 614400 300M 83 Linux
/dev/sda2 616448 1640447 1024000 500M 5 Extended
# 輸入 p 查看,新增一個 500M 的擴展分區
Command (m for help): w
# 所有分區設置完成后,輸入 w 保存,即可退出 fdisk 交互界面
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
使用 gdisk 管理分區
gdisk 命令工具默認將磁盤劃分為 GPT 格式的分區
[root@localhost ~]# gdisk /dev/sda
# 進入 gdisk 交互界面
GPT fdisk (gdisk) version 1.0.3
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present Command (? for help): ?
# 輸入 ? 可查看幫助信息
b
back up GPT data to a file
c
change a partition's name
d
delete a partition
i
show detailed information on a partition
l
list known partition types
n
add a new partition
o
create a new empty GUID partition table (GPT)
p
print the partition table
q
quit without saving changes
r
recovery and transformation options (experts only)
s
sort partitions
t
change a partition's type code
v
verify disk
w
write table to disk and exit
x
extra functionality (experts only)
?
print this menu
#gdisk 創建新分區(默認 GPT 格式)
Command (? for help): n
# 輸入 n 創建新分區
Partition number (1-128, default 1):
# 輸入分區編號,默認為 1 ,因為是 /dev/sda 硬盤的第一塊分區
First sector (34-10485726, default = 2048) or {+-}size{KMGTP}: # 輸入扇區的開始位置,選擇默認即
可,也可手動指定
Last sector (2048-10485726, default = 10485726) or {+-}size{KMGTP}: +300M
# 輸
入扇區的結束位置,一般都是指定磁盤大小,這里輸入 +300M 表示新分區大小為 300M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
# 默認即可,分區完成后可以修改
Changed type of partition to 'Linux filesystem'
Command (? for help): p
# 輸入 p 查看分區情況
Disk /dev/sda: 10485760 sectors, 5.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 61FA16FB-FAD8-4873-A857-BD387FE0A592
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 10485726
Partitions will be aligned on 2048-sector boundaries
Total free space is 9871293 sectors (4.7 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 616447 300.0 MiB 8300 Linux filesystem Command (? for help): w
# 輸入 w 保存分區并退出 gdisk 交互界面
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.