Linux(Centos 7.6)命令详解:fdisk

1.命令作用

控制磁盘分区表(manipulate disk partition table);

fdisk的第一种语法是一个菜单驱动的程序,用于创建和操作分区表;它支持GPT、MBR、Sun、SGI和BSD分区表;

fdisk默认情况下,不使用dos兼容模式和柱面为单位的显示方式;若有需要使用已弃用的dos行为,可以使用"-c=dos -u=cylinders"命令行选项;

2.命令语法

Usage: fdisk [options] <disk>        改变更新分区表
             fdisk [options] -l <disk>     列出分区表
             fdisk -s <partition>             给出以块为单位的分区大小

3.参数详解

OPTION:

  • -b <size>,扇区大小(size可为512(默认值), 1024, 2048 或 4096)
  • -c[=<mode>],兼容模式: 'dos' 或 'nondos' (默认)
  • -h,打印此帮助文本
  • -u[=<unit>],分区显示: 'cylinders'(单位为柱面) 或 'sectors'(单位为扇区,默认)
  • -v,打印程序版本
  • -C <number>,指定柱面数(cylinders)
  • -H <number>,指定磁头数(heads)
  • -S <number>,指定每个磁道(track)的扇区数(sectors)

程序内部交互指令:

DOS分区:是一种较早的分区方式,也称为MBR(Master Boot Record);它使用的是以前的BIOS引导方式,存在一些限制,只支持最多4个主分区或3个主分区和1个扩展分区;且单个主分区的容量不能超过2TB;

GPT分区:(GUID Partition Table)是一种全新的分区方式,它使用GUID(全局唯一标识符)来标识分区,GPT可以在同一硬盘上创建128个分区,而且每个分区的容量也可以超过2TB

  • a,设置引导扇区
  • b,编辑BSD磁盘标签(bsd)
  • c,切换DOS兼容标志(compatibility)
  • d,删除一个分区(delete)
  • g,创建一个新的空的GPT分区表
  • G,创建一个IRIX(SGI)分区表
  • l,列出已知的分区类型(list)
  • m,打印菜单(menu);即输出交互指令及其解释
  • n,添加一个新分区(new)
  • o,创建一个新的空的DOS分区表
  • p,打印分区表(print)
  • q,不保存更改就退出(quit)
  • s,创建一个新的空的Sun磁盘标签,执行后会按照Sun规则自动创建分区
  • t,更改一个分区的系统id
  • u,更改显示/输入单位(units),切换为扇区或者柱面为单位
  • v,验证分区表(verify)
  • w,将分区表写入磁盘并退出(write)
  • x,额外功能(仅限专家使用)

4.常用用例

4.1.简单DOS分区操作

4.1.1.虚拟机添加磁盘

VMware虚拟机软件里,选中一个虚拟机,鼠标右键,选择最下面的设置,点击弹出框左下角的添加,选择硬盘,然后下一步,选择磁盘类型(默认),然后下一步,选择创建新虚拟磁盘,然后下一步,设置磁盘大小,其他默认,然后下一步直到完成;

4.1.2.系统里查看虚拟机新磁盘

创建完成后重启虚拟机,可使用lsblk或者fdisk -l查看新添加的磁盘信息;

4.1.3.fdisk默认分区说明

fdisk默认是以扇区为单位计算的DOS分区;也可用通过-u=cylinders指定为柱面单位计算;也可用在交互界面使用菜单的g来创建一个GPT分区;

DOS分区:是一种较早的分区方式,也称为MBR(Master Boot Record);它使用的是以前的BIOS引导方式,存在一些限制,只支持最多4个主分区或3个主分区和1个扩展分区;且单个主分区的容量不能超过2TB;创建分区会显示如下内容:

Partition type:
    p   primary (0 primary, 0 extended, 4 free)    # 主分区
    e   extended                                   # 扩展分区

4.1.4.默认分区过程说明

[root@node4 ~]# fdisk /dev/sdb                  # 交互创建分区表
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n                         # 输入n,添加一个新分区,默认是dos分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)  # p代表主分区(dos最多可4个主分区,或者3个主分区多个扩展分区)
   e   extended                                 # e代表扩展分区
Select (default p):                             # 直接回车,或输入p创建一个主分区
Using default response p
Partition number (1-4, default 1):              # 主分区编号,直接回车,或输入1进入下一步;这里显示1-4,所有只能由4个主分区,分区编号从1开始,大于5会报错
First sector (2048-20971519, default 2048):     # 开始的扇区值,默认是2048()
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):      # 结束扇区,默认值20971519,即/dev/sdb所有空间设置为一个分区;或者根据规划几个分区,计算每个分区多大,再计算每个分区占用多少扇区来填写;或者直接设置带有单位的空间大小,如5G;
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set                           # 提示创建分区的类型和大小

Command (m for help): w                         # 保存并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@node4 ~]# 

4.2.查看磁盘分区

可用使用lsblk命令查看,这里主要说明fdisk查看分区信息。

4.2.1.查看服务器所有磁盘分区

直接fdisk -l 查看所有磁盘分区

[root@node4 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000c34aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    41943039    18566144   83  Linux

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: 0xecaae6fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux
[root@node4 ~]# 

4.2.2.查看指定磁盘的分区

  • 命令行查看,直接使用fdisk -l /dev/sdb((完整设备磁盘名称))
[root@node4 ~]# 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
Disk label type: dos
Disk identifier: 0xecaae6fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux
[root@node4 ~]# 
  • 交互式查看,fdisk /dev/sdb(完整设备磁盘名称)进入交互界面,然后输入p 大于分区情况
[root@node4 ~]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


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: 0xecaae6fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux

Command (m for help): 

4.2.3.查看分区输出详细解释

  • Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
    • Disk /dev/sdb: 磁盘设备名称,这里是磁盘不是分区,所以后面一般没有数字
    • 10.7 GB: 磁盘总大小,单位GB
    • 10737418240 bytes: 磁盘总大小,单位bytes
    • 20971520 sectors: 扇区数量,默认以扇区为单位进行分区
  • Units = sectors of 1 * 512 = 512 bytes
    • Units: 扇区单位
    • sectors of 1 * 512: 一个扇区大小
    • 512 bytes: 一个扇区大小,分区的单位是扇区
  • Sector size (logical/physical): 512 bytes / 512 bytes: 磁盘的逻辑/物理扇区大小分别为: 512 bytes / 512 bytes
  • I/O size (minimum/optimal): 512 bytes / 512 bytes: 磁盘的最小/最优I/O分别为: 512 bytes / 512 bytes
  • Disk label type: dos: 磁盘标签类型为: dos,默认为dos,交互模式下执行g可改为GPT
  • Disk identifier: 0xecaae6fb: 磁盘标识码(0x72b55ba6)
Device BootBootStartEndBlocksIdSystem
/dev/sdb12048209715191048473683Linux
磁盘分区设备名称一般挂载的磁盘该列为空,如是系统启动的引导分区会显示星号(*)分区起始扇区编号分区结束扇区编号块分区大小,以1K块为单位分区类型ID,83代表Linux分区类型描述,如Linux、swap

4.2.4.分区内相关计算

  • 磁盘大小(10737418240 bytes) = 扇区数量(20971520) * 每个扇区(512 bytes)
  • 磁盘大小(10737418240 bytes) = 柱面数量(11529.1) * 每个柱面(931328 bytes)
  • 每个柱面(931328 bytes) = 每个柱面包括的扇区(1819 = 磁头数 * 每条磁道的扇区数) * 每个扇区(512 bytes)

以上计算可用参考下面查询的结果对应查看;以下是以扇区为单位进行分区,然后使用扇区和柱面进行分区查看的结果

[root@node4 ~]# fdisk -l /dev/sdb                                 # 以扇区为单位进行分区

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors       # 磁盘总大小=扇区数量*扇区大小(Units=512bytes)
Units = sectors of 1 * 512 = 512 bytes                            # 单位大小(以扇区为单位<单位=512bytes>)
Sector size (logical/physical): 512 bytes / 512 bytes             # 逻辑/物理扇区大小(512 bytes / 512 bytes)
I/O size (minimum/optimal): 512 bytes / 512 bytes                 # 最小/最优I/O大小(512 bytes / 512 bytes)
Disk label type: dos                                              # 磁盘标签类型(dos)
Disk identifier: 0x72b55ba6                                       # 磁盘标识码(0x72b55ba6)

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux
[root@node4 ~]#
[root@node4 ~]# fdisk -l -u=cylinders /dev/sdb                    # 以扇区为单位进行分区
                                                                  
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors       # 磁盘总大小=扇区数量*扇区大小(Units=512bytes)
Units = cylinders of 1819 * 512 = 931328 bytes                    # 单位大小(以柱面为单位<单位=931328bytes>)
Sector size (logical/physical): 512 bytes / 512 bytes             # 逻辑/物理扇区大小(512 bytes / 512 bytes)
I/O size (minimum/optimal): 512 bytes / 512 bytes                 # 最小/最优I/O大小(512 bytes / 512 bytes)
Disk label type: dos                                              # 磁盘标签类型(dos)
Disk identifier: 0x72b55ba6                                       # 磁盘标识码(0x72b55ba6)

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               2       11530    10484736   83  Linux
[root@node4 ~]# 

4.3.删除分区

注:删除分区后,退出交互模式前需要保存,否则分区不会被删除

[root@node4 ~]# fdisk /dev/sdb               # 进入交互模式
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


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: 0xecaae6fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux

Command (m for help): d                     # 上面看只有一个,直接输入d,就会删除,如果有多个分区,会提示输出删除哪一个分区
Selected partition 1
Partition 1 is deleted

Command (m for help): w                     # 删除后,需要保存,不保存并不会实际删除
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@node4 ~]# 

一块磁盘有多个分区删除分区时会提示输出对应编号,这里的编号与分区名称sda1、sda2、sda3后面的数值是对应的,输入对应的值就删除指定的分区 

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    41943039    18566144   83  Linux

Command (m for help): d
Partition number (1-3, default 3): 

4.4.创建GPT分区

GPT分区:(GUID Partition Table)是一种全新的分区方式,它使用GUID(全局唯一标识符)来标识分区,GPT可以在同一硬盘上创建128个分区,而且每个分区的容量也可以超过2TB

[root@node4 ~]# fdisk /dev/sdb                    # 进入交互界面
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): g                           # 指定以GPT方式来分区,如果需要恢复DOS方式,这里执行o
Building a new GPT disklabel (GUID: E24F7688-EE95-44CA-BFD3-C4914EA57783)


Command (m for help): n                           # 新建分区
Partition number (1-128, default 1):              # 设置分区编号,默认1,GPT可用创建128个分区,比DOS分区限制功能好用,且单分区大小比DOS大
First sector (2048-20971486, default 2048):       # GPT分区起始扇区编号
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486):     # GPT结束扇区,默认值20971486,即/dev/sdb所有空间设置为一个分区;或者根据规划几个分区,计算每个分区多大,再计算每个分区占用多少扇区来填写;或者直接设置带有单位的空间大小,如5G;
Created partition 1


Command (m for help): w                           # 保存分区并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@node4 ~]# 

4.5.以柱面为单位创建/查看分区(不推荐使用)

4.51.以柱面为单位创建分区

[root@node4 ~]# fdisk -u=cylinders /dev/sdb   # 指定-u=cylinders参数进入交互模式,交互里面就以柱面作为计量单位设置磁盘分区大小

WARNING: cylinders as display units are deprecated. Use command 'u' to change units to sectors.   # 提示不建议使用,交互内可执行 u 切换单位为扇区
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n                        # 创建新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):                            # 默认指定创建主分区
Using default response p
Partition number (1-4, default 1):             # 主分区的分区编号
First cylinder (1-1305, default 1):            # 主分区柱面编号范围,及指定起始柱面编号
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305):         # 指定柱面结束编号,默认值1305,是将所有空间分给一个分区;也可用更具柱面大小计算来设置一个分区需要多少柱面;也可用直接指定带有代为的空间值,如5G
Using default value 1305
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): w                        # 保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@node4 ~]#

4.51.以柱面为单位查看分区

注:

  • 下图的Start和End,不再是扇区的起始结束编号,而是柱面的起始结束编号
  • 柱面作为单位分区,Blocks是一个大概的值(后面有一个加号),不建议使用该种分区方式
[root@node4 ~]# fdisk -u=cylinders /dev/sdb -l

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
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 label type: dos
Disk identifier: 0xf7fc7a18

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1305    10481388+  83  Linux
[root@node4 ~]# 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆是浪个

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值