安装新的SSD或硬盘后,您需要做的第一件事就是对其进行分区。 一个驱动器至少需要一个分区,然后才能对其进行格式化并在其中存储文件。
在Linux中,可以使用几种工具来创建分区,其中fdisk
是最常用的工具。
在本文中,我们将讨论fdisk
命令。
fdisk
是菜单驱动的命令行程序,可让您在硬盘上创建和操作分区表。
请注意,fdisk
是一种危险的工具,应格外小心。 只有root用户或具有 sudo
特权的用户才能操纵分区表。
列出分区
要列出设备的分区表,请使用-l
选项调用fdisk
命令,后跟设备名称。 例如,列出/dev/sda
分区表和分区:
fdisk -l /dev/sda
如果未指定任何设备作为参数,则fdisk
将打印/proc/partitions
文件中列出的所有设备的分区表:
fdisk -l
Disk /dev/nvme0n1: 232.91 GiB, 250059350016 bytes, 488397168 sectors
Disk model: Samsung SSD 960 EVO 250GB
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: gpt
Disk identifier: 6907D1B3-B3AB-7E43-AD20-0707A656A1B5
Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 1050623 1048576 512M EFI System
/dev/nvme0n1p2 1050624 34605055 33554432 16G Linux swap
/dev/nvme0n1p3 34605056 488397134 453792079 216.4G Linux filesystem
Disk /dev/sda: 465.78 GiB, 500107862016 bytes, 976773168 sectors
Disk model: WDC WD5000AAKS-0
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: 0x0001cca3
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 976771071 976769024 465.8G 83 Linux
上面的输出显示系统上已连接的所有设备的当前分区表。 通常,SATA设备名称遵循模式/dev/sd[a-z]
,而NVMe设备名称具有以下模式/dev/nvme[1-9]n[1-9]
。
创建分区表
要开始对驱动器进行分区,请使用设备名称运行fdisk
。 在此示例中,我们将研究/dev/sdb
:
fdisk /dev/sdb
命令提示符将更改,fdisk
对话框将打开并且您可以在其中键入命令:
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
您对分区表所做的更改只有在您使用w
命令将其写入后才会生效。 您可以退出fdisk
对话框,而无需使用q
命令保存更改。
要获取所有可用命令的列表,请输入m
:
m
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
The old ext4 signature will be removed by a write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x51550e9f.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help):
如果您要对新驱动器进行分区,则在开始创建分区之前,创建一个分区表。 如果设备已经有分区表且要保留它,则跳过此步骤。
fdisk
支持几种分区方案。 MBR和GPT是两个最受欢迎的分区标准方案,它们以不同的方式将分区信息存储在驱动器上。 GPT是允许使用的较新标准,并且比MBR具有许多优势。 选择要使用的分区标准时要考虑的要点:
- 使用MBR在传统BIOS模式下引导磁盘。
- 使用GPT在UEFI模式下引导磁盘。
- MBR标准最多支持创建2 TiB磁盘分区,如果磁盘的容量为2 TiB或更大,请使用GPT。
- MBR的限制为4个主分区。 如果需要更多分区,则可以将一个主分区设置为扩展分区,并保留其他逻辑分区。 使用GPT,最多可以有128个分区。 GPT不支持扩展分区或逻辑分区。
在此示例中,我们将使用GPT分区表。
输入g
创建一个新的空GPT分区表:
g
输出看起来像这样:
Created a new GPT disklabel (GUID: 4649EE36-3013-214E-961C-51A9187A7503).
下一步是创建新分区。
我们将创建两个分区。 第一个大小为100 GiB,第二个大小将占用其余磁盘空间。
运行n
命令以创建新分区:
n
系统会提示您输入分区号。 点击“ Enter”以使用默认值(1
):
Partition number (1-128, default 1):
接下来,命令将要求您指定第一个扇区。 通常,始终建议对第一个值使用默认值。 点击“ Enter”以使用默认值(2048
):
First sector (2048-500118158, default 2048):
在下一个提示下,您需要输入最后一个扇区。 您可以使用最后一个扇区的绝对值或起始扇区的相对值,并使用+符号加上分区大小。 大小可以指定为千字节(K),兆字节(M),千兆字节(G),兆字节(T)或PB(P)。
输入+100G
将分区大小设置为100 GiB:
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-500118158, default 500118158): +100G
Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.
默认情况下,新分区的类型设置为“ Linux文件系统”,这在大多数情况下应该没问题。 如果要更改类型,请按l
获取分区类型列表,然后按t
更改类型。
让我们创建第二个分区,该分区将占用其余磁盘空间:
n
为分区号,第一个和最后一个扇区使用默认值。 这将创建一个分区,该分区将使用磁盘上的所有可用空间。
Partition number (2-128, default 2):
First sector (209717248-625142414, default 209717248):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (209717248-625142414, default 625142414):
创建完分区后,使用p
命令显示新的分区表:
p
Disk /dev/sdb: 298.9 GiB, 320072933376 bytes, 625142448 sectors
Disk model: nal USB 3.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F8365250-AF58-F74E-B592-D56E3A5DEED1
Device Start End Sectors Size Type
/dev/sdb1 2048 209717247 209715200 100G Linux filesystem
/dev/sdb2 209717248 625142414 415425167 198.1G Linux filesystem
如果要删除分区,请使用d
命令。
通过运行w
命令来保存更改:
p
该命令会将表写入磁盘并退出fdisk
菜单。
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
内核将读取设备分区表,而无需重新启动系统。
激活分区
现在已经创建了分区,下一步是格式化分区并将其挂载到系统的目录树中。
我们将两个分区都格式化为ext4:
sudo mkfs.ext4 -F /dev/sdb1
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 51928145 4k blocks and 12984320 inodes
Filesystem UUID: 63a3457e-c3a1-43f4-a0e6-01a7dbe7dfed
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
在此示例中,将挂载到/mnt/audio
和/mnt/video
目录。
使用 mkdir
创建装载点:
sudo mkdir -p /mnt/audio /mnt/video
挂载新分区:
sudo mount /dev/sdb1 /mnt/audio
分区将保持挂载状态,直到您卸载它或关闭机器。 要在Linux系统启动时自动挂载分区,请在/etc/fstab
文件中定义挂载。
您现在可以使用新分区来存储文件。
结论
fdisk
是用于创建分区方案的命令行工具。 有关fdisk
命令的更多信息,请在终端中键入man fdisk
。
如果您有任何问题或反馈,请随时发表评论。