fdisk分区命令

fdisk是Linux系统中最常用的分区工具,通过这个命令也可以查看系统中所有可用的分区,但是这个命令只支持MBR的分区表(这句话应该只对某些系统,CentOS7-1810适用,Debian9.5和openSUSE15应该已经支持GPT分区表,下面对fdisk的操作都是在Debian9.5下操作),所以这个命令不能对大于2T的硬盘进行分区,大于2T的硬盘我们需要用GPT分区表来进行分区,GPT分区我们就要用gdisk或parted分区工具来进行分区。

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

一、fdisk参数使用方法:

fdisk [-l] 设备名

参数
-l :显示指定磁盘设备的分区表信息,如果没有指定磁盘设备,则显示/proc/partitions 文件中的信息,也就是系统中所有的磁盘设备。

举例:
-----------------------------------------------------------
root@debian:~# fdisk  -l          #没有指定任何磁盘设备

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors       #第一块磁盘/dev/sda容量20G
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                     #分区表的类型为MBR (dos表示MBR分区表,gpt表示GPT分区表)
Disk identifier: 0xefdc905c

Device          Boot    Start          End             Sectors   Size  Id       Type         #磁盘/dev/sda下的分区
/dev/sda1     *          2048        37748735   377466881     8G        83     Linux
/dev/sda2                37750782      41940991   4190210   2G         5       Extended
/dev/sda5      37750784     41940991          4190208          2G        82      Linux swap / Solaris

Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors      #第二块磁盘/dev/sdb容量5G
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

root@debian:~#
-----------------------------------------------------------
使用 "fdisk -l" 查看分区信息,不同的发行版可能显示的信息不同,通过上面的信息能够看到我们添加的两块硬盘(/dev/sda 和 /dev/sdb)的信息。我们解释一下这些信息,其上半部分是硬盘的整体状态,/dev/sda 硬盘的总大小是20GB等信息。

信息的下半部分是分区的信息,共 7 列,含义如下:

Device:分区的设备文件名。
Boot:是否为启动引导分区,在这里 /dev/sda1 为启动引导分区。
Start:起始柱面,代表分区从哪里开始。
End:终止柱面,代表分区到哪里结束。
Sectors:分区的大小,单位是扇区。
Size:分区的容量
id:分区内文件系统的 ID。
Type:分区的文件系统是什么。


二、fdisk交互模式磁盘分区

通过命令“fdisk 设备文件名” 就可以进入fdisk程序的交互模式,在交互模式中可以通过输入fdisk程序所提供的指令完成相应的操作,举例如下

-----------------------------------------------------------
root@debian:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.29.2).
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 0xe3779e93.

Command (m for help): m                                                                   #输入m可以获得帮助信息

Help:

DOS (MBR)
a   toggle a bootable flag                                                                        #设置可引导标记
b   edit nested BSD disklabel                                                                #编辑 bsd 磁盘标签
c    toggle the dos compatibility flag                                                  #设置 DOS 操作系统兼容标记

G  eneric
d  delete a partition                                                                                 #删除一个分区
F  list free unpartitioned space                                                           #列出空闲的未分区空间
l    list known partition types                                                                 #显示已知的分区类型。82 为 Linux swap 分区,83 为 Linux 分区
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           #从sfdisk脚本文件加载磁盘布局
O    dump disk layout to sfdisk script file           #将磁盘布局转储到sfdisk脚本文件

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         #新建空白GPT分区表,这个应该就表明fdisk已经支持GPT,还需要进一步的认证。
G    create a new empty SGI (IRIX) partition table         #新建空白IRIX分区表
o    create a new empty DOS partition table         #新建空白DOS分区表
s    create a new empty Sun partition table          #新建空白SUN磁盘标签


Command (m for help):
-----------------------------------------------------------

三、分区管理

1、新建主分区

root@debian:~# fdisk    /dev/sdb            #要新建分区的磁盘/dev/sdb不要加数字加数字就变成了分区

Welcome to fdisk (util-linux 2.29.2).
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 0xd9b1702f.

Command (m for help):  n              #输入n新建分区,默认直接创建MBR分区表
Partition type
p primary (0 primary, 0 extended, 4 free)       #主分区
e extended (container for logical partitions)       #扩展分区
Select (default p): p                  #输入p新建主分区
Partition number (1-4, default 1):           #输入分区号,直接回车就行,默认为1
First sector (2048-10485759, default 2048):      #输入起始扇区,直接回车就行,默认2048最前面开始
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759):  +1G                 #这里输入新增分区的大小可以通过扇区数来增加,也可以通过+size{K,M,G,T,P}方式来增加,这里要增加1G的容量就表示为+1G       

Created a new partition 1 of type 'Linux' and of size 1 GiB.      #提示创建了1G的分区

Command (m for help):  p                     #输入p查看创建的分区
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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                        #分区表类型为MBR
Disk identifier: 0xd9b1702f

Device    Boot   Start   End     Sectors   Size   Id   Type
/dev/sdb1      2048   2099199   2097152   1G   83   Linux

2、剩余可用的空间

Command (m for help):  F                     #输入F查看剩余可用的空间
Unpartitioned space /dev/sdb: 4 GiB, 4293918720 bytes, 8386560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

Start     End     Sectors   Size
2099200   10485759   8386560    4G                   #可以看到可用空间为4G

3、创建扩展分区及逻辑分区

Command (m for help):  n                     #输入n继续创建分区
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):  e                          #输入e创建扩展分区
Partition number (2-4, default 2):                  #输入扩展分区号,直接回车就可以
First sector (2099200-10485759, default 2099200):          #输入起始扇区数,直接回车就可以
Last sector, +sectors or +size{K,M,G,T,P} (2099200-10485759, default 10485759): +2G                                    #增加2G的容量

Created a new partition 2 of type 'Extended' and of size 2 GiB.       #创建扩展分区成功

Command (m for help):  n                       #输入n继续创建分区
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)                      #只有创建了扩展分区才会有这个选项
Select (default p):  l                         #输入l创建逻辑分区

Adding logical partition 5                          #扩展分区号从5开始
First sector (2101248-6293503, default 2101248):
Last sector, +sectors or +size{K,M,G,T,P} (2101248-6293503, default 6293503): +1G

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

Command (m for help):  p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0xd9b1702f

Device    Boot  Start   End     Sectors   Size   Id   Type
/dev/sdb1     2048   2099199   2097152   1G   83   Linux           #主分区
/dev/sdb2      2099200     6293503   4194304    2G    5   Extended               #扩展分区
/dev/sdb5       2101248    4198399   2097152    1G   83   Linux               #逻辑分区

4、删除分区

Command (m for help): d                    #输入d删除分区
Partition number (1,2,5, default 5): 2               #删除扩增分区2,上面的逻辑分区也会一同删除

Partition 2 has been deleted.

Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0xd9b1702f

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux

Command (m for help):

5、修改分区类型

Command (m for help):  l                      #查看可用分区类型


#其中82为Linux swap分区、83为Linux分区、8e为LinuxLVM分区、b为Windows FAT32分区、e为Windows FAT16分区。

Command (m for help): t                  #输入t更改分区类型
Partition number (1,2,5, default 5): 5        #更改分区5的类型

Partition type (type L to list all types): 8e            #输入更改分区类型为8e,这里输入l也可以查看分区的类型

Changed type of partition 'Linux' to 'Linux LVM'.          #提示更改成功

Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0xd9b1702f

Device   Boot    Start    End     Sectors   Size   Id   Type
/dev/sdb1       2048   2099199   2097152   1G   83   Linux
/dev/sdb2       2099200   6293503   4194304    2G   5   Extended
/dev/sdb5       2101248   4198399   2097152    1G   8e   Linux LVM         #更改后的分区类型

Command (m for help):

6、保存修改的结果

Command (m for help):  w                        #输入w保存配置,如果不想保存可以输入q退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.


使用w指令保存后,则在fdisk中所做的所有操作都会生效,且不可回退。如果分区表正忙,则需要重启机器后才能使新的分区表生效。

四、新建GPT分区

不是所有的发行版带的fdisk都支持GPT分区这里请注意

1、新建分区

Command (m for help): p

Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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                  #现在的分区类型为MBR
Disk identifier: 0xd9b1702f

Command (m for help): g            #输入g创建GPT分区
Created a new GPT disklabel (GUID: 8C0F4613-2830-40E1-8D23-596D1B7F5DDB).

Command (m for help): n            #新建分区
Partition number (1-128, default 1):         #可以看到这里没有扩展分区的概念了
First sector (2048-10485726, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485726, default 10485726): +1G

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

Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: gpt
Disk identifier: 8C0F4613-2830-40E1-8D23-596D1B7F5DDB

Device    Start   End   Sectors   Size   Type                                               #GPT下输入p显示的分区信息和MBR下略有不同,没有分区类型id的选项
/dev/sdb1   2048       2099199     2097152         1G        Linux filesystem       #成功创建了一个主分区

Command (m for help):

2、 修改分区的类型

Command (m for help): l                                #查看分区的类型,GPT下同样的分区类型对应的编号不同了,这里要注意

 上面只是显示了部分按空格显示更多的信息

Command (m for help):  t                                          #输入t更改分区类型,这里输入l也可以查看分区的类型
Partition number (1,2, default 2):  2                      #输入要更改的分区
Hex code (type L to list all codes):  31                  #输入分区类型的编号 

Changed type of partition 'Linux filesystem' to 'Linux LVM'.               #更改分区类型成功

Command (m for help): p                                      
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: gpt
Disk identifier: 836D334F-D0EE-4AD3-8581-EC9ABFF31F50

Device    Start    End       Sectors   Size   Type
/dev/sdb1      2048         2099199    2097152        1G          Linux filesystem
/dev/sdb2    2099200    4196351     2097152      1G          Linux LVM                                #分区类型为已经更改

Command (m for help):

发布了136 篇原创文章 · 获赞 38 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/Pipcie/article/details/104964743