Linux fdisk命令详解:给硬盘分区

1、命令及其意义

命令:fdisk
意义:在安装操作系统的过程中已经对系统硬盘进行了分区,但如果新添加了一块硬盘,为了避免麻烦,重新安装操作系统,就可以使用fdisk命令来完成分区。
注意:千万不要在当前的硬盘上尝试使用 fdisk,这会完整删除整个系统,一定要再找一块硬盘,或者使用虚拟机。

2、创建分区

2.1 列出系统中识别的硬盘的分区(fdisk ~l)

[root@localhost ~]# fdisk ~l
#列出系统分区

示例

[root@localhost ~]# fdisk -l
#查询本机可以识别的硬盘和分区
Disk /dev/sda:32.2 GB, 32212254720 bytes
#硬盘文件名和硬盘大小
255 heads, 63 sectors/track, 3916 cylinders
#共255个磁头、63个扇区和3916个柱面
Units = cylinders of 16065 *512 = 8225280 bytes
#每个柱面的大小
Sector size (logical/physical): 512 bytes/512 bytes
#每个扇区的大小
I/O size (minimum/optimal): 512 bytes/512 bytes
Disk identifier: 0x0009e098
Device Boot Start End Blocks ld System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
#分区1没有占满硬盘
/dev/sda2 26 281 2048000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary
#分区2没有占满硬盘
/dev/sda3 281 3917 29203456 83 Linux
#设备文件名启动分区 起始柱面 终止柱面容量 ID 系统
Disk /dev/sdb: 21.5 GB, 21474836480 bytes #第二个硬盘识别,这个硬盘的大小
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes Disk identifier: 0x00000000

示例解读:例子中显示的是添加的两块硬盘的信息(/dev/sda 和 /dev/sdb):
硬盘/dev/sda
上半部分态是硬盘的整体状态,/dev/sda 硬盘的总大小是 32.2 GB,共有 3916 个柱面,每个柱面由 255 个磁头读/写数据,每个磁头管理 63 个扇区。每个柱面的大小是 8225280 Bytes,每个扇区的大小是 512 Bytes。
下半部分是分区的信息,共 7 列,含义如下:

  • Device:分区的设备文件名。
  • Boot:是否为启动引导分区,在这里 /dev/sda1 为启动引导分区。
  • Start:起始柱面,代表分区从哪里开始。
  • End:终止柱面,代表分区到哪里结束。
  • Blocks:分区的大小,单位是 KB。
  • id:分区内文件系统的 ID。在 fdisk 命令中,可以 使用 “i” 查看。
  • System:分区内安装的系统是什么。

硬盘 /dev/sdb
已经可以被识别了,但是没有分区。

2.2 对硬盘 /dev/sdb进行分区 (fdisk 设备文件名)

2.2.1 查看帮助指令
[root@localhost ~]# fdisk /dev/sdb
#给/dev/sdb分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xed7e8bc7.
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
#交互界面的等待输入指令的位置,输入 m 得到帮助
Command action
#可用指令
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
I 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 verity the partition table
w write table to disk and exit
x extra functionality (experts only)

在 fdisk 交互界面中输入 m 可以得到帮助,帮助里列出了 fdisk 可以识别的交互命令,这些命令解释如下:

命令说明
a设置可引导标记
b编辑 bsd 磁盘标签
c设置 DOS 操作系统兼容标记
d删除一个分区
l显示已知的文件系统类型。82 为 Linux swap 分区,83 为 Linux 分区
m显示帮助菜单
n新建分区
o建立空白 DOS 分区表
p显示分区列表
q不保存退出
s新建空白 SUN 磁盘标签
t改变一个分区的系统 ID
u改变显示记录单位
v验证分区表
w保存退出
x附加功能(仅专家)
2.2.2 正式新建
2.2.2.1 主分区的建立
[root@localhost ~]# fdisk /dev/sdb
…省略部分输出…
Command (m for help): p
#显示当前硬盘的分区列表
Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 *512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb4b0720c
Device Boot Start End Blocks id System
#目前一个分区都没有
Command (m for help): n
#那么我们新建一个分区
Command action
#指定分区类型
e extended
#扩展分区
p primary partition (1-4)
#主分区
p
#这里选择p,建立一个主分区
Partition number (1-4): 1
#选择分区号,范围为1~4,这里选择1
First cylinder (1 -2610, default 1):
#分区的起始柱面,默认从1开始。因为要从硬盘头开始分区,所以直接回车
Using default value 1
#提示使用的是默认值1
Last cylinder, +cylinders or +size{K, M, G}(1-2610, default 2610): +5G
#指定硬盘大小。可以按照柱面指定(1-2610)。我们对柱面不熟悉,那么可以使用size{K, M, G}的方式指定硬盘大小。这里指定+5G,建立一个5GB大小的分区
Command (m for help):
#主分区就建立了,又回到了fdisk交互界面的提示符
Command (m for help): p
#查询一下新建立的分区
Disk /dev/sdb: 21.5GB, 21474836480 bytes
255 heads,63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes 1512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb4b0720c
Device Boot Start End Blocks id System
/dev/sdb1 1 654 5253223+ 83 linux
#dev/sdb1已经建立了吧

总结:“fdisk 硬盘名 -> n(新建)->p(建立主分区) -> 1(指定分区号) -> 回车(默认从 1 柱面开始建立分区)-> +5G(指定分区大小)”。新建的分区由于还没有格式化和挂载,所以不能使用。

2.2.2.2 扩展分区的建立

注意:主分区和扩展分区一共最多只能有四个,并且扩展分区最多只能建立一个。

[root@localhost ~]# fdisk /dev/sdb
…省略部分输出…
Command (m for help): n
#新建立分区
Command action
e extended
p primary partition (1-4)
e
#这次建立扩展分区
Partition number (1-4): 2
#给扩展分区指定分区号2
First cylinder (655-2610, default 655):
#扩展分区的起始柱面。上节建立的主分区1已经占用了1~654个柱面,所以我们从655开始建立,注意:如果没有特殊要求,则不要跳开柱面建立分区,应该紧挨着建立分区
Using default value 655
提示使用的是默认值655
Last cylinder, +cylinders or +size{K, M, G} (655-2610, default 2610):
#这里把整块硬盘的剩余空间都建立为扩展分区
Using default value 2610
#提示使用的是默认值2610

这里把 /dev/sdb 硬盘的所有剩余空间都建立为扩展分区,也就是建立一个主分区,剩余空间都建立成扩展分区,再由扩展分区中建立逻辑分区。
注意:扩展分区是不能被格式化和直接使用的,所以还要在扩展分区内部再建立逻辑分区。

2.2.2.3 逻辑分区的建立
[root@localhost ~]# fdisk /dev/sdb
…省略部分输出…
Command (m for help): n
#建立新分区
Command action
l logical (5 or over)
#由于在前面章节中,扩展分区已经建立,所以这里变成了l(logic)
p primary partition (1-4)
l
#建立逻辑分区
First cylinder (655-2610, default 655):
#不用指定分区号,默认会从5开始分配,所以直接选择起始柱面
#注意:逻辑分区是在扩展分区内部再划分的,所以柱面是和扩展分区重叠的
Using default value 655
Last cylinder, +cylinders or +size{K, M, G} (655-2610, default 2610):+2G
#分配2GB大小
Command (m for help): n
#再建立一个逻辑分区
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (917-2610, default 917):
Using default value 917
Last cylinder, +cylinders or +size{K, M, G} (917-2610, default 2610):+2G
Command (m for help): p
#查看一下已经建立的分区
Disk /dev/sdb: 21.5GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 *512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb4b0720c
Device Boot Start End Blocks id System
/dev/sdb1 1 654
5253223+ 83 Linux
#主分区
/dev/sdb2 655 2610 15711570
5 Extended
#扩展分区
/dev/sdb5 655 916
2104483+ 83 Linux
#逻辑分区 1
/dev/sdb6 917 1178
2104483+ 83 Linux
#逻辑分区2
Command (m for help): w
#保存并退出
The partition table has been altered!
Calling ioctl。to re-read partition table.
Syncing disks.
[root@localhost -]#
#退回到提示符界面

所有的分区立过程中如果不保存并退出是不会生效的,所以建立错了也没有关系,使用 q 命令不保存退出即可。如果使用了 w 命令,就会保存退出。有时因为系统的分区表正忙,所以需要重新启动系统才能使新的分区表生效。命令如下:

Command (m for help): w
#保存并退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
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.
#要求重新启动,才能格式化
Syncing disks.

如果不想重新启动,则可以使用 partprobe 命令。这个命令的作用是让系统内核重新读取分区表信息,这样就可以不用重新启动了。命令如下:

[root@localhost ~]# partprobe

如果这个命令不存在,则请安装 parted-2.1-18.el6.i686 这个软件包。partprobe 命令不是必需的,如果没有提示重启系统,则直接格式化即可。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值