您的位置:

Linux下使用fdisk进行磁盘分区步骤

一、fdisk命令概述

fdisk命令是Linux下的一个磁盘分区工具,它可以创建、删除、查看硬盘的分区。在使用fdisk分区之前,需要注意以下几点:

1、fdisk只能对未挂载的硬盘进行分区,如果需要对已挂载到系统中的硬盘进行分区,需要先将硬盘卸载。

2、在分区之前需要备份数据,因为分区操作会清除硬盘上的数据。

3、分区时需要根据实际需要来分配分区的大小。

二、使用fdisk命令进行磁盘分区

1、使用fdisk查看磁盘信息

首先,我们需要使用fdisk来查看磁盘的分区情况:

fdisk -l

这个命令会列出系统上所有的硬盘分区,并给出它们的分区表信息。例如:

Disk /dev/sda: 512 GB, 512110190592 bytes
255 heads, 63 sectors/track, 62260 cylinders, total 1000215216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x0009b5d8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   100021452   50009652+  83  Linux

Disk /dev/sdb: 512 GB, 512110190592 bytes
255 heads, 63 sectors/track, 62260 cylinders, total 1000215216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size             (logical/physical): 512 bytes / 4096 bytes
I/O size             (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x0009b5d6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   100021452   50009652+  83  Linux

2、创建新的分区

要创建一个新的分区,请使用以下命令:

fdisk /dev/sda

接下来进入fdisk界面,输入n创建新分区,分配大小,默认单位是MB。创建多个分区需要重复执行n命令。

Welcome to fdisk (util-linux 2.21.2).

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

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   100021452   50009652+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (100021452+1): 
Last sector, +sectors or +size{K,M,G} (100021452+1): +100000M

Created a new partition 2 of type 'Linux' and of size 95.4 GiB.

在这个例子中,我们创建了一个95.4GB(100000MB)的新分区。

3、选择分区类型

在分区之后,我们需要指定这个分区的类型,例如,我们可以将这个分区设为Linux交换分区类型,使用以下命令:

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list all codes): 82

在这个例子中,我们将这个分区的类型改为Linux交换分区类型。

4、保存分区表

分区操作完成后,需要使用w命令保存分区表:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

在保存之后,将会出现警告信息,需要使用partprobe命令或者重启系统,让系统重新读取分区表信息。

三、删除分区

如果需要删除一个分区,需要使用以下命令:

fdisk /dev/sda
Command (m for help): d
Partition number (1-4): 2
Partition 2 is deleted
Command (m for help): w

在这个例子中,我们使用d命令删除了/dev/sda的第二个分区。

四、总结

fdisk是Linux下一个常用的磁盘分区工具,可以方便地对硬盘进行分区操作。使用fdisk进行分区时需要先备份数据,并且注意分配分区的大小。使用fdisk时需要小心,避免误操作导致数据丢失。