概述

fdisk是一种用于管理磁盘分区的工具,常用于Linux和其他Unix-like操作系统中。它可以用于创建、删除和修改磁盘分区,并支持多种文件系统类型,例如FAT、ext2、ext3等。

fdisk还可以显示当前系统中所有磁盘的分区信息,包括磁盘标识符、分区类型、分区大小等。使用fdisk,用户可以轻松地管理磁盘空间,为不同的操作系统或应用程序分配不同的存储空间。

除此之外,fdisk还支持MBR(Master Boot Record)分区方案,它是一种常见的磁盘分区方案,能够在BIOS引导下启动操作系统。

MBR

MBR(Master Boot Record)分区是指使用MBR分区方案的磁盘分区方式。MBR分区方案是一种常见的分区方案,能够在BIOS引导下启动操作系统。MBR分区方案将磁盘的前512个字节(即MBR)用于存储分区表和引导程序。其中分区表记录了磁盘分区的信息,包括分区类型、分区起始位置、分区大小等。MBR分区方案最多支持4个主分区或3个主分区和1个扩展分区,扩展分区可以划分为多个逻辑分区。MBR分区方案已经存在了很长时间,但是它有一个缺点,即它只支持最大2TB的磁盘容量。如果需要使用更大的磁盘,就需要使用GPT(GUID Partition Table)分区方案。

环境

系统:Rocky Linux release 8.5

工具:fdisk from util-linux 2.32.1

硬盘:虚拟机添加了一块20G的硬盘

虚拟机属性

实践

主分区

[root@host ~]# lsblk -p                           // 通过lsblk来查看一下新增的硬盘位置(/dev/sdb)
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
/dev/sr0                 11:0    1 1024M  0 rom
[root@host ~]# fdisk /dev/sdb                    // 使用fdisk工具对/dev/sdb这块硬盘进行分区

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x178d8de5.

Command (m for help): n                         // 输入n进行新建分区操作
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p                            // p是创建主分区,e是创建逻辑分区
Partition number (1-4, default 1): 1             // 选择分区号,默认为1,这里可以改其他的(MBR分区最多有4个分区)
First sector (2048-41943039, default 2048):      // 起始扇区选择默认即可
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +5G    //设置分区大小我这里设置5G

Created a new partition 1 of type 'Linux' and of size 5 GiB.    //提示创建成功

Command (m for help): p                     // p查看分区表
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x178d8de5

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 10487807 10485760   5G 83 Linux                // 创建好的分区

Command (m for help): w                                        // 保存之前的分区操作
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@host ~]# mkfs.xfs /dev/sdb1                          // 格式化创建好的分区
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@host ~]# mkdir /sdb1                           // 创建挂载位置
[root@host ~]# mount /dev/sdb1 /sdb1                  // 将格式化好的分区进行挂载
[root@host ~]# lsblk -p                                // 查看分区情况
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
└─/dev/sdb1               8:17   0    5G  0 part /sdb1          // 已经挂载好可以使用了
/dev/sr0                 11:0    1 1024M  0 rom

逻辑分区

[root@host ~]# fdisk /dev/sdb                            // 对/dev/sdb进行分区

Welcome to fdisk (util-linux 2.32.1).
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 (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e                           // 创建逻辑分区
Partition number (2-4, default 2):              // 分区号
First sector (10487808-41943039, default 10487808):       // 设置起始扇区选择默认即可
Last sector, +sectors or +size{K,M,G,T,P} (10487808-41943039, default 41943039):       // 设置大小,逻辑分区的默认大小是剩余全部存储空间,这里我选择默认。 

Created a new partition 2 of type 'Extended' and of size 15 GiB.  

Command (m for help): p                                 // 查看分区表
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x178d8de5

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdb1           2048 10487807 10485760   5G 83 Linux
/dev/sdb2       10487808 41943039 31455232  15G  5 Extended               // 创建好了一个逻辑分区大小为15G

Command (m for help): w                      // 保存分区操作
The partition table has been altered.
Syncing disks.
[root@host ~]# lsblk -p
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
├─/dev/sdb1               8:17   0    5G  0 part /sdb1
└─/dev/sdb2               8:18   0   15G  0 part 
/dev/sr0                 11:0    1 1024M  0 rom

逻辑子分区

创建好逻辑分区之后就可以在逻辑分区里面创建无数个子分区,这样就逃离了MBR分区方式的分区限制。

[root@host ~]# fdisk /dev/sdb    // 对/dev/sdb进行分区

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n         // 新建分区
All space for primary partitions is in use.
Adding logical partition 5      // 当创建好逻辑分区之后就会发现分区号会从5开始增加
First sector (10489856-41943039, default 10489856):             // 磁盘起始位置,默认即可
Last sector, +sectors or +size{K,M,G,T,P} (10489856-41943039, default 41943039): +1G     //设置大小

Created a new partition 5 of type 'Linux' and of size 1 GiB.

Command (m for help): n                 // 继续创建
All space for primary partitions is in use.
Adding logical partition 6  
First sector (12589056-41943039, default 12589056): 
Last sector, +sectors or +size{K,M,G,T,P} (12589056-41943039, default 41943039): +1G

Created a new partition 6 of type 'Linux' and of size 1 GiB.

Command (m for help): n            // 继续创建
All space for primary partitions is in use.
Adding logical partition 7
First sector (14688256-41943039, default 14688256): 
Last sector, +sectors or +size{K,M,G,T,P} (14688256-41943039, default 41943039): +1G

Created a new partition 7 of type 'Linux' and of size 1 GiB.

Command (m for help): w              // 保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@host ~]# lsblk -p                  // 查看分区表
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
├─/dev/sdb1               8:17   0    5G  0 part /sdb1
├─/dev/sdb2               8:18   0    1K  0 part 
├─/dev/sdb5               8:21   0    1G  0 part 
├─/dev/sdb6               8:22   0    1G  0 part 
└─/dev/sdb7               8:23   0    1G  0 part 
/dev/sr0                 11:0    1 1024M  0 rom

这里会发现sdb磁盘一共创建了7个分区。

删除分区

以sdb1为例,sdb1这个分区已经挂载到了/sdb1目录已经在使用了,在删除分区前需要取消挂载。具体操作方法如下

[root@host ~]# lsblk -p                                      // 查看分区状态   
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
├─/dev/sdb1               8:17   0    5G  0 part /sdb1         // 发现/dev/sdb1已经挂载到/sdb1目录下
├─/dev/sdb2               8:18   0    1K  0 part 
├─/dev/sdb5               8:21   0    1G  0 part 
├─/dev/sdb6               8:22   0    1G  0 part 
└─/dev/sdb7               8:23   0    1G  0 part 
/dev/sr0                 11:0    1 1024M  0 rom  
[root@host ~]# umount /sdb1                                 // 取消挂载
[root@host ~]# lsblk -p                                              
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
├─/dev/sdb1               8:17   0    5G  0 part            // 已经取消挂载了
├─/dev/sdb2               8:18   0    1K  0 part 
├─/dev/sdb5               8:21   0    1G  0 part 
├─/dev/sdb6               8:22   0    1G  0 part 
└─/dev/sdb7               8:23   0    1G  0 part 
/dev/sr0                 11:0    1 1024M  0 rom  
[root@host ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d                                // d(delete)删除分区
Partition number (1,2,5-7, default 7): 1               // 删除分区号,这里选择1

Partition 1 has been deleted.

Command (m for help): w                                // 保存位置
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@host ~]# lsblk -p                          // 查看磁盘状态
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk      // 发现sdb1已经消失         
├─/dev/sdb2               8:18   0    1K  0 part 
├─/dev/sdb5               8:21   0    1G  0 part 
├─/dev/sdb6               8:22   0    1G  0 part 
└─/dev/sdb7               8:23   0    1G  0 part 
/dev/sr0                 11:0    1 1024M  0 rom

使用分区

格式化分区

创建好分区之后需要格式化之后才可以挂载使用,格式化需要使用mkfs工具,这里不多讲。关于格式化的格式可以使用mkfs.来查看

[root@host ~]# mkfs.
mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.minix   mkfs.xfs

这里使用xfs来格式化创建的分区(逻辑分区只能格式化子分区不能直接格式化逻辑分区)。

[root@host ~]# lsblk -p                          // 查看分区状态
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
├─/dev/sdb2               8:18   0    1K  0 part 
├─/dev/sdb5               8:21   0    1G  0 part 
├─/dev/sdb6               8:22   0    1G  0 part 
└─/dev/sdb7               8:23   0    1G  0 part 
/dev/sr0                 11:0    1 1024M  0 rom  
[root@host ~]# mkfs.xfs /dev/sdb5              // 使用xfs类型格式化sdb5
meta-data=/dev/sdb5              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@host ~]# mkfs.xfs /dev/sdb6               // 使用xfs类型格式化sdb6
meta-data=/dev/sdb6              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@host ~]# mkfs.xfs /dev/sdb7              // 使用xfs类型格式化sdb7                 
meta-data=/dev/sdb7              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@host ~]# lsblk -p                        // 查看分区状态
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
├─/dev/sdb2               8:18   0    1K  0 part 
├─/dev/sdb5               8:21   0    1G  0 part 
├─/dev/sdb6               8:22   0    1G  0 part 
└─/dev/sdb7               8:23   0    1G  0 part 
/dev/sr0                 11:0    1 1024M  0 rom

挂载分区

当格式化分区之后可以通过mount来进行挂载,挂载好的分区就可以使用了。

[root@host ~]# mkdir -p /disk/sdb{5..7}     // 创建挂载目录
[root@host ~]# mount /dev/sdb5 /disk/sdb5   // 挂载sdb5
[root@host ~]# mount /dev/sdb6 /disk/sdb6   // 挂载sdb6
[root@host ~]# mount /dev/sdb7 /disk/sdb7   // 挂载sdb7
[root@host ~]# lsblk -p          // 查看分区状态
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                  8:0    0   20G  0 disk 
├─/dev/sda1               8:1    0    1G  0 part /boot
└─/dev/sda2               8:2    0   19G  0 part 
  ├─/dev/mapper/rl-root 253:0    0   17G  0 lvm  /
  └─/dev/mapper/rl-swap 253:1    0    2G  0 lvm  [SWAP]
/dev/sdb                  8:16   0   20G  0 disk 
├─/dev/sdb2               8:18   0    1K  0 part 
├─/dev/sdb5               8:21   0    1G  0 part /disk/sdb5
├─/dev/sdb6               8:22   0    1G  0 part /disk/sdb6
└─/dev/sdb7               8:23   0    1G  0 part /disk/sdb7
/dev/sr0                 11:0    1 1024M  0 rom
最后修改:2023 年 04 月 06 日
如果觉得我的文章对你有用,请随意赞赏