fdisk:Linux下管理磁盘分区的利器

系统 Linux
一块硬盘可以被划分成一个或多个逻辑磁盘,我们将其称作分区。我们对硬盘进行的划分信息被储存于建立在扇区 0 的分区表(MBR 或 GPT)中。

 [[256923]]

一块硬盘可以被划分成一个或多个逻辑磁盘,我们将其称作分区。我们对硬盘进行的划分信息被储存于建立在扇区 0 的分区表(MBR 或 GPT)中。

Linux 需要至少一个分区来当作根文件系统,所以我们不能在没有分区的情况下安装 Linux 系统。当我们创建一个分区时,我们必须将它格式化为一个适合的文件系统,否则我们就没办法往里面储存文件了。

要在 Linux 中完成分区的相关工作,我们需要一些工具。Linux 下有很多可用的相关工具,我们曾介绍过 Parted 命令。不过,今天我们的主角是 fdisk

人人都喜欢用 fdisk,它是 Linux 下管理磁盘分区的最佳利器之一。它可以操作最大 2TB 的分区。大量 Linux 管理员都喜欢使用这个工具,因为当下 LVM 和 SAN 的原因,并没有多少人会用到 2TB 以上的分区。并且这个工具被世界上许多的基础设施所使用。如果你还是想创建比 2TB 更大的分区,请使用 parted 命令 或 cfdisk 命令。

对磁盘进行分区和创建文件系统是 Linux 管理员的日常。如果你在许多不同的环境中工作,你一定每天都会重复几次这项操作。

Linux 内核是如何理解硬盘的?

作为人类,我们可以很轻松地理解一些事情;但是电脑就不是这样了,它们需要合适的命名才能理解这些。

在 Linux 中,外围设备都位于 /dev 挂载点,内核通过以下的方式理解硬盘:

  • /dev/hdX[a-z]: IDE 硬盘被命名为 hdX
  • /dev/sdX[a-z]: SCSI 硬盘被命名为 sdX
  • /dev/xdX[a-z]: XT 硬盘被命名为 xdX
  • /dev/vdX[a-z]: 虚拟硬盘被命名为 vdX
  • /dev/fdN: 软盘被命名为 fdN
  • /dev/scdN or /dev/srN: CD-ROM 被命名为 /dev/scdN/dev/srN

什么是 fdisk 命令?

fdisk 的意思是 固定磁盘Fixed Disk格式化磁盘Format Disk,它是命令行下允许用户对分区进行查看、创建、调整大小、删除、移动和复制的工具。它支持 MBR、Sun、SGI、BSD 分区表,但是它不支持 GUID 分区表(GPT)。它不是为操作大分区设计的。

fdisk 允许我们在每块硬盘上创建最多四个主分区。它们中的其中一个可以作为扩展分区,并下设多个逻辑分区。1-4 扇区作为主分区被保留,逻辑分区从扇区 5 开始。

如何在 Linux 下安装 fdisk?

fdisk 作为核心组件内置于 Linux 中,所以你不必手动安装它。

如何用 fdisk 列出可用磁盘?

在执行操作之前,我们必须知道的是哪些磁盘被加入了系统。要想列出所有可用的磁盘,请执行下文的命令。这个命令将会列出磁盘名称、分区数量、分区表类型、磁盘识别代号、分区 ID 和分区类型。

  1. $ sudo fdisk -l
  2. Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
  3. Units: sectors of 1 * 512 = 512 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disklabel type: dos
  7. Disk identifier: 0xeab59449
  8.  
  9. Device Boot Start End Sectors Size Id Type
  10. /dev/sda1 * 20973568 62914559 41940992 20G 83 Linux
  11.  
  12.  
  13. Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
  14. Units: sectors of 1 * 512 = 512 bytes
  15. Sector size (logical/physical): 512 bytes / 512 bytes
  16. I/O size (minimum/optimal): 512 bytes / 512 bytes
  17.  
  18.  
  19. Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
  20. Units: sectors of 1 * 512 = 512 bytes
  21. Sector size (logical/physical): 512 bytes / 512 bytes
  22. I/O size (minimum/optimal): 512 bytes / 512 bytes
  23.  
  24.  
  25. Disk /dev/sdd: 10 GiB, 10737418240 bytes, 20971520 sectors
  26. Units: sectors of 1 * 512 = 512 bytes
  27. Sector size (logical/physical): 512 bytes / 512 bytes
  28. I/O size (minimum/optimal): 512 bytes / 512 bytes
  29.  
  30.  
  31. Disk /dev/sde: 10 GiB, 10737418240 bytes, 20971520 sectors
  32. Units: sectors of 1 * 512 = 512 bytes
  33. Sector size (logical/physical): 512 bytes / 512 bytes
  34. I/O size (minimum/optimal): 512 bytes / 512 bytes

如何使用 fdisk 列出特定分区信息?

如果你希望查看指定分区的信息,请使用以下命令:

  1. $ sudo fdisk -l /dev/sda
  2. Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
  3. Units: sectors of 1 * 512 = 512 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disklabel type: dos
  7. Disk identifier: 0xeab59449
  8.  
  9. Device Boot Start End Sectors Size Id Type
  10. /dev/sda1 * 20973568 62914559 41940992 20G 83 Linux

如何列出 fdisk 所有的可用操作?

fdisk 中敲击 m,它便会列出所有可用操作:

  1. $ sudo fdisk /dev/sdc
  2.  
  3. Welcome to fdisk (util-linux 2.30.1).
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6.  
  7. Device does not contain a recognized partition table.
  8. Created a new DOS disklabel with disk identifier 0xe944b373.
  9.  
  10. Command (m for help): m
  11.  
  12. Help:
  13.  
  14. DOS (MBR)
  15. a toggle a bootable flag
  16. b edit nested BSD disklabel
  17. c toggle the dos compatibility flag
  18.  
  19. Generic
  20. d delete a partition
  21. F list free unpartitioned space
  22. l list known partition types
  23. n add a new partition
  24. p print the partition table
  25. t change a partition type
  26. v verify the partition table
  27. i print information about a partition
  28.  
  29. Misc
  30. m print this menu
  31. u change display/entry units
  32. x extra functionality (experts only)
  33.  
  34. Script
  35. I load disk layout from sfdisk script file
  36. O dump disk layout to sfdisk script file
  37.  
  38. Save & Exit
  39. w write table to disk and exit
  40. q quit without saving changes
  41.  
  42. Create a new label
  43. g create a new empty GPT partition table
  44. G create a new empty SGI (IRIX) partition table
  45. o create a new empty DOS partition table
  46. s create a new empty Sun partition table

如何使用 fdisk 列出分区类型?

fdisk 中敲击 l,它便会列出所有可用分区的类型:

  1. $ sudo fdisk /dev/sdc
  2.  
  3. Welcome to fdisk (util-linux 2.30.1).
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6.  
  7. Device does not contain a recognized partition table.
  8. Created a new DOS disklabel with disk identifier 0x9ffd00db.
  9.  
  10. Command (m for help): l
  11.  
  12. 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
  13. 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
  14. 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
  15. 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
  16. 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
  17. 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
  18. 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
  19. 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
  20. 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
  21. 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
  22. a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
  23. b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
  24. c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment
  25. e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
  26. f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
  27. 10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
  28. 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
  29. 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
  30. 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
  31. 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
  32. 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
  33. 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
  34. 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
  35. 1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
  36. 1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT

如何使用 fdisk 创建一个磁盘分区?

如果你希望新建磁盘分区,请参考下面的步骤。比如我希望在 /dev/sdc 中新建四个分区(三个主分区和一个扩展分区),只需要执行下文的命令。

首先,请在操作 “First sector” 的时候先按下回车,然后在 “Last sector” 中输入你希望创建分区的大小(可以在数字后面加 KB、MB、G 和 TB)。例如,你希望为这个分区扩容 1GB,就应该在 “Last sector” 中输入 +1G。当你创建三个分区之后,fdisk 默认会将分区类型设为扩展分区,如果你希望创建第四个主分区,请输入 p 来替代它的默认值 e

  1. $ sudo fdisk /dev/sdc
  2.  
  3. Welcome to fdisk (util-linux 2.30.1).
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6.  
  7.  
  8. Command (m for help): n
  9. Partition type
  10. p primary (0 primary, 0 extended, 4 free)
  11. e extended (container for logical partitions)
  12. Select (default p): Enter
  13.  
  14. Using default response p.
  15. Partition number (1-4, default 1): Enter
  16. First sector (2048-20971519, default 2048): Enter
  17. Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +1G
  18.  
  19. Created a new partition 1 of type 'Linux' and of size 1 GiB.
  20.  
  21. Command (m for help): p
  22. Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
  23. Units: sectors of 1 * 512 = 512 bytes
  24. Sector size (logical/physical): 512 bytes / 512 bytes
  25. I/O size (minimum/optimal): 512 bytes / 512 bytes
  26. Disklabel type: dos
  27. Disk identifier: 0x8cc8f9e5
  28.  
  29. Device Boot Start End Sectors Size Id Type
  30. /dev/sdc1 2048 2099199 2097152 1G 83 Linux
  31.  
  32. Command (m for help): w
  33. The partition table has been altered.
  34. Calling ioctl() to re-read partition table.
  35. Syncing disks.

如何使用 fdisk 创建扩展分区?

请注意,创建扩展分区时,你应该使用剩下的所有空间,以便之后在扩展分区下创建逻辑分区。

  1. $ sudo fdisk /dev/sdc
  2.  
  3. Welcome to fdisk (util-linux 2.30.1).
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6.  
  7.  
  8. Command (m for help): n
  9. Partition type
  10. p primary (3 primary, 0 extended, 1 free)
  11. e extended (container for logical partitions)
  12. Select (default e): Enter
  13.  
  14. Using default response e.
  15. Selected partition 4
  16. First sector (6293504-20971519, default 6293504): Enter
  17. Last sector, +sectors or +size{K,M,G,T,P} (6293504-20971519, default 20971519): Enter
  18.  
  19. Created a new partition 4 of type 'Extended' and of size 7 GiB.
  20.  
  21. Command (m for help): p
  22. Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
  23. Units: sectors of 1 * 512 = 512 bytes
  24. Sector size (logical/physical): 512 bytes / 512 bytes
  25. I/O size (minimum/optimal): 512 bytes / 512 bytes
  26. Disklabel type: dos
  27. Disk identifier: 0x8cc8f9e5
  28.  
  29. Device Boot Start End Sectors Size Id Type
  30. /dev/sdc1 2048 2099199 2097152 1G 83 Linux
  31. /dev/sdc2 2099200 4196351 2097152 1G 83 Linux
  32. /dev/sdc3 4196352 6293503 2097152 1G 83 Linux
  33. /dev/sdc4 6293504 20971519 14678016 7G 5 Extended
  34.  
  35. Command (m for help): w
  36. The partition table has been altered.
  37. Calling ioctl() to re-read partition table.
  38. Syncing disks.

如何用 fdisk 查看未分配空间?

上文中,我们总共创建了四个分区(三个主分区和一个扩展分区)。在创建逻辑分区之前,扩展分区的容量将会以未分配空间显示。

使用以下命令来显示磁盘上的未分配空间,下面的示例中显示的是 7GB:

  1. $ sudo fdisk /dev/sdc
  2.  
  3. Welcome to fdisk (util-linux 2.30.1).
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6.  
  7.  
  8. Command (m for help): F
  9. Unpartitioned space /dev/sdc: 7 GiB, 7515144192 bytes, 14678016 sectors
  10. Units: sectors of 1 * 512 = 512 bytes
  11. Sector size (logical/physical): 512 bytes / 512 bytes
  12.  
  13. Start End Sectors Size
  14. 6293504 20971519 14678016 7G
  15.  
  16. Command (m for help): q

如何使用 fdisk 创建逻辑分区?

创建扩展分区后,请按照之前的步骤创建逻辑分区。在这里,我创建了位于 /dev/sdc51GB 逻辑分区。你可以查看分区表值来确认这点。

  1. $ sudo fdisk /dev/sdc
  2.  
  3. Welcome to fdisk (util-linux 2.30.1).
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6.  
  7. Command (m for help): n
  8. All primary partitions are in use.
  9. Adding logical partition 5
  10. First sector (6295552-20971519, default 6295552): Enter
  11. Last sector, +sectors or +size{K,M,G,T,P} (6295552-20971519, default 20971519): +1G
  12.  
  13. Created a new partition 5 of type 'Linux' and of size 1 GiB.
  14.  
  15. Command (m for help): p
  16. Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
  17. Units: sectors of 1 * 512 = 512 bytes
  18. Sector size (logical/physical): 512 bytes / 512 bytes
  19. I/O size (minimum/optimal): 512 bytes / 512 bytes
  20. Disklabel type: dos
  21. Disk identifier: 0x8cc8f9e5
  22.  
  23. Device Boot Start End Sectors Size Id Type
  24. /dev/sdc1 2048 2099199 2097152 1G 83 Linux
  25. /dev/sdc2 2099200 4196351 2097152 1G 83 Linux
  26. /dev/sdc3 4196352 6293503 2097152 1G 83 Linux
  27. /dev/sdc4 6293504 20971519 14678016 7G 5 Extended
  28. /dev/sdc5 6295552 8392703 2097152 1G 83 Linux
  29.  
  30. Command (m for help): w
  31. The partition table has been altered.
  32. Calling ioctl() to re-read partition table.
  33. Syncing disks.

如何使用 fdisk 命令删除分区?

如果我们不再使用某个分区,请按照下面的步骤删除它。

请确保你输入了正确的分区号。在这里,我准备删除 /dev/sdc2 分区:

  1. $ sudo fdisk /dev/sdc
  2.  
  3. Welcome to fdisk (util-linux 2.30.1).
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6.  
  7.  
  8. Command (m for help): d
  9. Partition number (1-5, default 5): 2
  10.  
  11. Partition 2 has been deleted.
  12.  
  13. Command (m for help): p
  14. Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
  15. Units: sectors of 1 * 512 = 512 bytes
  16. Sector size (logical/physical): 512 bytes / 512 bytes
  17. I/O size (minimum/optimal): 512 bytes / 512 bytes
  18. Disklabel type: dos
  19. Disk identifier: 0x8cc8f9e5
  20.  
  21. Device Boot Start End Sectors Size Id Type
  22. /dev/sdc1 2048 2099199 2097152 1G 83 Linux
  23. /dev/sdc3 4196352 6293503 2097152 1G 83 Linux
  24. /dev/sdc4 6293504 20971519 14678016 7G 5 Extended
  25. /dev/sdc5 6295552 8392703 2097152 1G 83 Linux
  26.  
  27. Command (m for help): w
  28. The partition table has been altered.
  29. Calling ioctl() to re-read partition table.
  30. Syncing disks.

如何在 Linux 下格式化分区或建立文件系统?

在计算时,文件系统控制了数据的储存方式,并通过 索引节点Inode Tables 来检索数据。如果没有文件系统,操作系统是无法找到信息储存的位置的。

在此,我准备在 /dev/sdc1 上创建分区。有三种方式创建文件系统:

  1. $ sudo mkfs.ext4 /dev/sdc1
  2. $ sudo mkfs -t ext4 /dev/sdc1
  3. $ sudo mke2fs /dev/sdc1
  4.  
  5. mke2fs 1.43.5 (04-Aug-2017)
  6. Creating filesystem with 262144 4k blocks and 65536 inodes
  7. Filesystem UUID: c0a99b51-2b61-4f6a-b960-eb60915faab0
  8. Superblock backups stored on blocks:
  9. 32768, 98304, 163840, 229376
  10.  
  11. Allocating group tables: done
  12. Writing inode tables: done
  13. Creating journal (8192 blocks): done
  14. Writing superblocks and filesystem accounting information: done

当你在分区上建立文件系统时,以下重要信息会同时被创建:

  • Filesystem UUID: UUID 代表了通用且独一无二的识别符,UUID 在 Linux 中通常用来识别设备。它 128 位长的数字代表了 32 个十六进制数。
  • Superblock: 超级块储存了文件系统的元数据。如果某个文件系统的超级块被破坏,我们就无法挂载它了(也就是说无法访问其中的文件了)。
  • Inode: Inode 是类 Unix 系统中文件系统的数据结构,它储存了所有除名称以外的文件信息和数据。
  • Journal: 日志式文件系统包含了用来修复电脑意外关机产生下错误信息的日志。

如何在 Linux 中挂载分区?

在你创建完分区和文件系统之后,我们需要挂载它们以便使用。我们需要创建一个挂载点来挂载分区,使用 mkdir 来创建一个挂载点。

  1. $ sudo mkdir -p /mnt/2g-new

如果你希望进行临时挂载,请使用下面的命令。在计算机重启之后,你会丢失这个挂载点。

  1. $ sudo mount /dev/sdc1 /mnt/2g-new

如果你希望永久挂载某个分区,请将分区详情加入 fstab 文件。我们既可以输入设备名称,也可以输入 UUID。

使用设备名称来进行永久挂载:

  1. # vi /etc/fstab
  2.  
  3. /dev/sdc1 /mnt/2g-new ext4 defaults 0 0

使用 UUID 来进行永久挂载(请使用 blkid 来获取 UUID):

  1. $ sudo blkid
  2. /dev/sdc1: UUID="d17e3c31-e2c9-4f11-809c-94a549bc43b7" TYPE="ext2" PARTUUID="8cc8f9e5-01"
  3. /dev/sda1: UUID="d92fa769-e00f-4fd7-b6ed-ecf7224af7fa" TYPE="ext4" PARTUUID="eab59449-01"
  4. /dev/sdc3: UUID="ca307aa4-0866-49b1-8184-004025789e63" TYPE="ext4" PARTUUID="8cc8f9e5-03"
  5. /dev/sdc5: PARTUUID="8cc8f9e5-05"
  6.  
  7. # vi /etc/fstab
  8.  
  9. UUID=d17e3c31-e2c9-4f11-809c-94a549bc43b7 /mnt/2g-new ext4 defaults 0 0

使用 df 命令亦可:

  1. $ df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. udev 969M 0 969M 0% /dev
  4. tmpfs 200M 7.0M 193M 4% /run
  5. /dev/sda1 20G 16G 3.0G 85% /
  6. tmpfs 997M 0 997M 0% /dev/shm
  7. tmpfs 5.0M 4.0K 5.0M 1% /run/lock
  8. tmpfs 997M 0 997M 0% /sys/fs/cgroup
  9. tmpfs 200M 28K 200M 1% /run/user/121
  10. tmpfs 200M 25M 176M 13% /run/user/1000
  11. /dev/sdc1 1008M 1.3M 956M 1% /mnt/2g-new
责任编辑:庞桂玉 来源: Linux中国
相关推荐

2011-01-18 09:51:59

Linux磁盘分区

2009-10-19 18:01:35

Linux磁盘分区

2011-01-18 10:00:59

Linux磁盘分区

2011-01-18 10:25:19

Linux磁盘分区

2023-01-31 14:00:01

TIAPUUID

2014-06-09 10:09:31

Linux磁盘分区

2014-06-06 13:51:36

磁盘分区IO分析

2017-03-09 14:58:19

GPartedLinux磁盘分区

2010-06-24 15:53:10

Linux指令

2013-08-23 14:07:45

磁盘分区

2022-04-12 17:39:14

Linux磁盘分区

2010-03-02 15:09:26

Linux mount

2018-06-25 11:58:23

Linuxparted磁盘分区

2021-07-30 23:05:22

Windows 11Windows微软

2010-02-25 13:34:24

Linux磁盘分区

2017-05-10 07:00:20

磁盘分区dcfldd工具备份分区

2009-12-15 08:50:58

Windows 7系统分区

2017-09-13 09:23:04

磁盘分区bash

2020-01-11 19:00:01

GParted磁盘分区工具开源

2010-05-28 18:23:27

Linux分区工具
点赞
收藏

51CTO技术栈公众号