返回首页DA系统C#IDE文件同步服务屏保 今天是: 2026-05-05    "立夏"  夏季的第一个节气,表示盛夏时节的正式开始

搜索
热搜: linux 技术
Hi~登录注册
查看: 1861|回复: 0

[转载] 【转载】Linux fdisk命令详解

[复制链接]
发表于 2022-4-21 16:13:27 | 显示全部楼层 |阅读模式

少侠不来段修仙之旅吗~

您需要 登录 才可以下载或查看,没有帐号?注册成为修仙之旅的少年~

x
Linux fdisk命令详解

注意:数据无价,谨慎操作!


我们在安装操作系统的过程中已经对系统硬盘进行了分区,但如果新添加了一块硬盘,想要正常使用,难道需要重新安装操作系统才可以分区吗?

当然不是,在 Linux 中有专门的分区命令 fdisk 和 parted。其中 fdisk 命令较为常用,但不支持大于 2TB 的分区;如果需要支持大于 2TB 的分区,则需要使用 parted 命令,当然 parted 命令也能分配较小的分区。我们先来看看如何使用 fdisk 命令进行分区。

fdisk 命令的格式如下:


[root@localhost ~]# fdisk ~l
#列出系统分区
[root@localhost ~]# fdisk 设备文件名
#给硬盘分区


注意,千万不要在当前的硬盘上尝试使用 fdisk,这会完整删除整个系统,一定要再找一块硬盘,或者使用虚拟机。这里给大家举个例子:

[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


使用 "fdisk -l" 查看分区信息,能够看到我们添加的两块硬盘(/dev/sda 和 /dev/sdb)的信息。我们解释一下这些信息,其上半部分态是硬盘的整体状态,/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:分区内安装的系统是什么。


如果这个分区并没有占满整块硬盘,就会提示 "Partition 1 does not end on cyl inder boundary",表示第一个分区没有到硬盘的结束柱面。大家发现了吗?/dev/sda 已经分配完了分区,没有空闲空间了。而第二块硬盘 /dev/sdb 已经可以被识别了,但是没有可分区。



我们以硬盘 /dev/sdb 为例来做练习,命令如下:

[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 /dev/sdb",这是因为硬盘并没有分区,使用 fdisk 命令的目的就是建立分区。

在 fdisk 交互界面中输入 m 可以得到帮助,帮助里列出了 fdisk 可以识别的交互命令,我们来解释一下这些命令,如表 1 所示。


表 1 fdisk 交互
命令说 明
a设置可引导标记
b编辑 bsd 磁盘标签
c设置 DOS 操作系统兼容标记
d删除一个分区
1显示已知的文件系统类型。82 为 Linux swap 分区,83 为 Linux 分区
m显示帮助菜单
n新建分区
0建立空白 DOS 分区表
P显示分区列表
q不保存退出
s新建空白 SUN 磁盘标签
t改变一个分区的系统 ID
u改变显示记录单位
V验证分区表
w保存退出
X附加功能(仅专家)



创建分区应该选择n,然后选择主分区,分区号默认,起止扇区默认即可得到一个完整的分区

输入w保存分区表退出fdisk!



游客
回复
*滑块验证:

DA论坛飞机票来了~
快速回复 返回顶部 返回列表