Linux常用命令-fdisk

命令

fdisk

描述

manipulate disk partition table
磁盘分区工具

用法

1
2
3
fdisk [options] <disk>    更改分区表
fdisk [options] -l <disk> 显示分区表
fdisk -s <partition> 以块为单位显示分区大小

选项

1
2
3
4
5
6
7
8
9
Options:
-b <size> 扇区大小(512, 1024, 2048 or 4096)
-c[=<mode>] 兼容模式,可选dos,nondos(default)
-h 显示帮助
-u[=<unit>] 显示单位,可选cylinders或sectors(default)
-v 显示版本
-C <number> specify the number of cylinders
-H <number> specify the number of heads
-S <number> specify the number of sectors per track

注意

使用fdisk划分2T以下的磁盘,大容量硬盘推荐使用parted命令

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 查看磁盘分区信息
$ fdisk -l /dev/sdb
Disk /dev/sdb: 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
# 对磁盘进行分区,交互式命令,如下划分一个5G的LVM分区
$ fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Command (m for help): m
Command action
a 切换为可启动标记
b 编辑bsd磁盘标签
c 切换为dos兼容标记
d 删除分区
g 创建一个新的GPT分区表
G 创建一个IRIX(SGI)分区表
l 显示可用的分区类型
m 显示此帮助菜单
n 创建一个新分区
o 创建一个新的DOS分区表
p 显示分区表
q 不保存退出
s 创建一个Sun磁盘标记
t 更改分区类型
u 更改显示单位
v 验证分区表
w 保存分区表到硬盘
x 额外的功能(专家模式)

Command (m for help): p
Disk /dev/sdb: 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: dos
Disk identifier: 0x0cf44cbe

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

Command (m for help): p
Disk /dev/sdb: 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: dos
Disk identifier: 0xcf4c6e26

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux

Command (m for help): v
Remaining 10485759 unallocated 512-byte sectors

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): l
...
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p
Disk /dev/sdb: 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: dos
Disk identifier: 0xcf4c6e26

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.