磁盘分区管理
前言
对于想要学习 Linux 的同学来说,想真正的玩转 Linux 就必须要知道如何管理磁盘分区(比如创建和删除磁盘分区),因为文件系统的挂载就与磁盘分区有关。
在 Linux 中,一切皆文件,所有的外部存储磁盘设备都在 /dev 目录下。比如我们经常会看到类似于 /sda 、 /sda1 、 /sda2 这样的设备(如下),其实 sda 才是真正的设备,而带数字后缀(如 /sda1 、 /sda2 )的设备只是该设备的某个分区。我们是无法直接使用 /sda2 这样的设备的,因为它只是一个抽象文件,想要使用该存储设备就必须进行一次文件挂载。所以,想要学习 Linux 的文件系统,磁盘分区我们就必须要了解!
$ ls /dev/ | grep 'sd'
sda
sda1
sda2
sdb
sdb1
sdb2
sdb3
查看磁盘分区
在当前的 Linux 操作系统中,主要的磁盘分区格式有 GPT 和 MBR,其中 GPT 是目前主流的分区格式,而 MBR 则慢慢的被 GPT 取代(有关 GPT 和 MBR 分区的知识可以参考文章最后的 参考链接)。
管理磁盘分区主要使用 gdisk 和 fdisk 工具,这两个工具分别用于管理 GPT 分区和 MBR 分区,在使用时最好不要混用,以免造成不可挽回的后果。
那如何查看磁盘分区的信息及格式呢?这就要介绍下下面三个工具了:
lsblk 工具:列出磁盘列表
lsblk 命令的全称是 list block devices。也就是用于显示系统上的磁盘分区列表信息,常用的命令语法如下:
$ lsblk [-adptJ] [-o +columns] [device...]
下面是主要可选参数说明:
-a 列出所有磁盘设备及分区
-d 仅列出磁盘设备本身, 不显示分区
-p 显示设备的完整文件名(路径), 而不是仅列出具体的设备名
-t 列出磁盘设备的详细数据, 如磁盘阵列机制、预读写的数据大小等
-J 以 JSON 的数据格式数据
关于 -o 参数需要做特别的说明,该参数用于显示额外的列信息,比如显示设备的 LABEL 和 UUID :
$ lsblk -o +LABEL,UUID
注意 -o 的语法,使用 + 追加要显示的列,如果有多个使用 , 分隔。
另外,如果想要查看具体有哪些可用于展示的列,可以使用 help 或 man 命令查看,示例:
$ lsblk --help
...
Available output columns:
NAME device name
KNAME internal kernel device name
PATH path to the device node
MAJ:MIN major:minor device number
FSAVAIL filesystem size available
FSSIZE filesystem size
FSTYPE filesystem type
FSUSED filesystem size used
FSUSE% filesystem use percentage
FSVER filesystem version
MOUNTPOINT where the device is mounted
LABEL filesystem LABEL
UUID filesystem UUID
PTUUID partition table identifier (usually UUID)
...
下面来看下具体的示例:
列出所有磁盘设备及分区
$ lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
|-sda1 8:1 0 100G 0 part
`-sda2 8:2 0 100G 0 part
sdb 8:16 0 223.6G 0 disk
|-sdb1 8:17 0 512M 0 part /boot/efi
|-sdb2 8:18 0 222.1G 0 part /
`-sdb3 8:19 0 976M 0 part [SWAP]
...
loop0 7:0 0 43.6M 1 loop /snap/snapd/14978
loop1 7:1 0 164.8M 1 loop /snap/gnome-3-28-1804/161
...
loop6 7:6 0 0B 0 loop
loop7 7:7 0 0B 0 loop
其实, -a 参数在实际中用的比较少,一半都是直接 lsblk 就好了~
注意看 sda 和 sdb 这两个磁盘,在他们下面都挂着两个分区。比如 sda 磁盘,两个分区的名字分别是 sda1 和 sda2 :
...
sda 8:0 0 931.5G 0 disk
|-sda1 8:1 0 100G 0 part
`-sda2 8:2 0 100G 0 part
...
下面就以 sda 磁盘设备做具体的说明:
磁盘分区说明
在此之前,这里有几个关于分区的概念需要知道:分区就是成功从磁盘设备中 “申请指定大小的内存空间”,创建分区就是尝试从磁盘设备中 “申请指定大小的空间”,删除分区就是将指定分区的内存归还给磁盘设备。
sda 是我的一块希捷 1T 大小的机械硬盘,在当前 Linux 中显示的设备名是 sda ,容量总大小为 931.5G(居然不是 1T,奸商!)
当前这个硬盘下有两个分区 sda1 和 sda2 , sda1 分区从 sda 磁盘设备中申请了 100G 空间, sda2 分区也从 sda 磁盘设备中申请了 100G 空间。
需要强调的一点是:申请成功的空间才是我们能够使用的区域,也就是说我们能够真正使用的大小就只有 200G,剩下的 700G 就是这块硬盘的剩余 “可申请” 的空间,下面是磁盘分区抽象图。

另外, sda1 和 sda2 分区虽然都是从 sda 磁盘上划分出来的,但是 sda1 和 sda2 之间是物理隔离的,这两块分区之间的数据是不互通的。如果你想在 sda1 分区中存储 110G 的数据是不可能实现的,因为在划分区的时候只给 sda1 分配了 100G 大小空间,所以这里需要额外的注意一下。
仅列出磁盘设备本身, 不显示分区
使用 -d 参数能够很直观的看到磁盘设备的空间大小(如下),当然了,在实际上还是以 lsblk -a 和 lsblk 为主。
$ lsblk -d
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
sdb 8:16 0 223.6G 0 disk
...
loop0 7:0 0 43.6M 1 loop /snap/snapd/14978
loop1 7:1 0 164.8M 1 loop /snap/gnome-3-28-1804/161
...
显示设备的完整文件名(路径)
前面的显示命令都只显示具体设备名,没有显示设备的具体位置,比如 sda 设备其实是在 /dev 目录下的。如果想要显示设备的具体路径可以使用 -p 参数:
$ lsblk -p
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
/dev/sda 8:0 0 931.5G 0 disk
|-/dev/sda1 8:1 0 100G 0 part
`-/dev/sda2 8:2 0 100G 0 part
/dev/sdb 8:16 0 223.6G 0 disk
|-/dev/sdb1 8:17 0 512M 0 part /boot/efi
|-/dev/sdb2 8:18 0 222.1G 0 part /
`-/dev/sdb3 8:19 0 976M 0 part [SWAP]
...
/dev/loop0 7:0 0 43.6M 1 loop /snap/snapd/14978
/dev/loop1 7:1 0 164.8M 1 loop /snap/gnome-3-28-1804/161
...
列出磁盘设备的详细数据
-t 参数一般使用的比较少,不过可以更详细的显示磁盘设备信息,如磁盘阵列机制等:
$ lsblk -t
NAME ALIGNMENT MIN-IO OPT-IO PHY-SEC LOG-SEC ROTA SCHED RQ-SIZE RA WSAME
loop0 0 512 0 512 512 0 mq-deadline 256 128 0B
loop1 0 512 0 512 512 0 mq-deadline 256 128 0B
loop2 0 512 0 512 512 0 mq-deadline 256 128 0B
loop3 0 512 0 512 512 0 mq-deadline 256 128 0B
loop4 0 512 0 512 512 0 mq-deadline 256 128 0B
sda 0 4096 0 4096 512 1 mq-deadline 64 128 0B
|-sda1 0 4096 0 4096 512 1 mq-deadline 64 128 0B
`-sda2 0 4096 0 4096 512 1 mq-deadline 64 128 0B
sdb 0 512 0 512 512 0 mq-deadline 64 128 0B
|-sdb1 0 512 0 512 512 0 mq-deadline 64 128 0B
|-sdb2 0 512 0 512 512 0 mq-deadline 64 128 0B
`-sdb3 0 512 0 512 512 0 mq-deadline 64 128 0B
以 JSON 的数据格式数据
上面输出的信息其实已经很直观了,不过如果想要将这些数据导出来最好还是以某种数据结构进行展示,比如 JSON 数据格式:
$ lsblk -J
{
"blockdevices": [
{"name":"loop0", "maj:min":"7:0", "rm":false, "size":"43.6M", "ro":true, "type":"loop", "mountpoint":"/snap/snapd/14978"},
{"name":"loop1", "maj:min":"7:1", "rm":false, "size":"164.8M", "ro":true, "type":"loop", "mountpoint":"/snap/gnome-3-28-1804/161"},
{"name":"loop2", "maj:min":"7:2", "rm":false, "size":"4K", "ro":true, "type":"loop", "mountpoint":"/snap/bare/5"},
{"name":"loop3", "maj:min":"7:3", "rm":false, "size":"65.2M", "ro":true, "type":"loop", "mountpoint":"/snap/gtk-common-themes/1519"},
{"name":"loop4", "maj:min":"7:4", "rm":false, "size":"55.5M", "ro":true, "type":"loop", "mountpoint":"/snap/core18/2284"},
{"name":"sda", "maj:min":"8:0", "rm":false, "size":"931.5G", "ro":false, "type":"disk", "mountpoint":null,
"children": [
{"name":"sda1", "maj:min":"8:1", "rm":false, "size":"100G", "ro":false, "type":"part", "mountpoint":null},
{"name":"sda2", "maj:min":"8:2", "rm":false, "size":"100G", "ro":false, "type":"part", "mountpoint":null}
]
},
{"name":"sdb", "maj:min":"8:16", "rm":false, "size":"223.6G", "ro":false, "type":"disk", "mountpoint":null,
"children": [
{"name":"sdb1", "maj:min":"8:17", "rm":false, "size":"512M", "ro":false, "type":"part", "mountpoint":"/boot/efi"},
{"name":"sdb2", "maj:min":"8:18", "rm":false, "size":"222.1G", "ro":false, "type":"part", "mountpoint":"/"},
{"name":"sdb3", "maj:min":"8:19", "rm":false, "size":"976M", "ro":false, "type":"part", "mountpoint":"[SWAP]"}
]
}
]
}
显示额外的列信息
默认情况下, lsblk 命令仅仅展示某几列数据,如 Name、SIZE、RO、TYPE:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 43.6M 1 loop /snap/snapd/14978
loop1 7:1 0 164.8M 1 loop /snap/gnome-3-28-1804/161
loop2 7:2 0 4K 1 loop /snap/bare/5
loop3 7:3 0 65.2M 1 loop /snap/gtk-common-themes/1519
loop4 7:4 0 55.5M 1 loop /snap/core18/2284
sda 8:0 0 931.5G 0 disk
|-sda1 8:1 0 100G 0 part
`-sda2 8:2 0 100G 0 part
sdb 8:16 0 223.6G 0 disk
|-sdb1 8:17 0 512M 0 part /boot/efi
|-sdb2 8:18 0 222.1G 0 part /
`-sdb3 8:19 0 976M 0 part [SWAP]
其实,可显示的列远不止这些,还有如 UUID、LABEL 等列。如果想要查看具体有哪些列,可以使用 --help 和 man 命令查看,这里不做说明了。
想要显示额外的指定列,可以使用 -o 参数, + 表示增加显示列,否则表示仅显示指定列。他的语法如下:
$ lsblk -o [+][column,column...] [device...]
比如我想额外显示 UUID、LABEL,则命令如下:
$ lsblk -o +UUID,LABEL
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT UUID LABEL
loop0 7:0 0 43.6M 1 loop /snap/snapd/14978
loop1 7:1 0 164.8M 1 loop /snap/gnome-3-28-1804/161
loop2 7:2 0 4K 1 loop /snap/bare/5
loop3 7:3 0 65.2M 1 loop /snap/gtk-common-themes/1519
loop4 7:4 0 55.5M 1 loop /snap/core18/2284
sda 8:0 0 931.5G 0 disk
|-sda1 8:1 0 100G 0 part
`-sda2 8:2 0 100G 0 part
sdb 8:16 0 223.6G 0 disk
|-sdb1 8:17 0 512M 0 part /boot/efi D8C7-8235
|-sdb2 8:18 0 222.1G 0 part / 042055c6-546b-4889-8d3f-3b7aec794412
`-sdb3 8:19 0 976M 0 part [SWAP] 660f3d0e-0a77-4d60-8532-b867c3a9e0d8
blkid 工具:显示分区 UUID
blkid 我在使用时最主要就是用于显示分区表的 UUID,因为在做设备挂载时虽然能够直接使用设备名,但是我更建议使用分区表的 UUID。
blkid 工具就比较简单了,直接输入即可(在某些 Linux 发行版,普通用户是没有权限使用该命令的,需要借助 sudo ):
$ sudo blkid
/dev/sdb1: UUID="D8C7-8235" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="8591882f-1b8b-44ed-a33b-f539f356e802"
/dev/sdb2: UUID="042055c6-546b-4889-8d3f-3b7aec794412" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="016e491b-23c1-45f0-99a4-7a6d440e1aac"
/dev/sdb3: UUID="660f3d0e-0a77-4d60-8532-b867c3a9e0d8" TYPE="swap" PARTUUID="35cd0ea7-73db-463e-aad9-136e916b01c9"
/dev/loop0: TYPE="squashfs"
/dev/loop1: TYPE="squashfs"
/dev/loop2: TYPE="squashfs"
/dev/loop3: TYPE="squashfs"
/dev/loop4: TYPE="squashfs"
/dev/sda1: PARTLABEL="Linux filesystem" PARTUUID="a1ac0702-797d-4ebb-94b1-b0f7e0cb0ee2"
/dev/sda2: PARTLABEL="Linux filesystem" PARTUUID="a942b1c6-5058-4820-a9bf-b6231ead20cc"
可以看到,我的 /dev/sdb1 分区对应的分区 UUID 就是 a1ac0702-797d-4ebb-94b1-b0f7e0cb0ee2 ,这个信息在做系统挂载时很有用的。
当然了,其实使用 lsblk 命令也是可以能够实现显示分区 UUID 的(如下),只不过没有 blkid 用起来方便。
$ lsblk -o +PARTUUID
parted 工具:查看分区格式和数据
在开始的时候就说了分区格式有 GPT 和 MBR 两种,但是到现在为止展示的一直是磁盘设备的辅助信息,如何查看分区格式呢?当然还是需要使用 parted 工具了。
parted 常用语法如下:
$ parted -l
$ parted device_name print
其中, -l 命令用于列出所有设备分区信息(不包括 loop 设备),而后一个命令则用于仅显示指定设备的分区信息。现在来使用该工具看下 /dev/sda 设备分区信息:
$ sudo parted /dev/sda print
Model: ATA ST1000LM048-2E71 (scsi) # 磁盘型号厂商(希捷)
Disk /dev/sda: 1000GB # 磁盘大小
Sector size (logical/physical): 512B/4096B # 每个物理扇区大小
Partition Table: gpt # 分区格式 GPT
Disk Flags:
# 下面才是分区数据
Number Start End Size File system Name Flags
1 1049kB 107GB 107GB Linux filesystem
2 107GB 215GB 107GB Linux filesystem
所以,我当前这块机械硬盘的分区格式是 GPT。
gdisk 管理 GPT 格式分区
再次强调,管理磁盘分区的工具主要有 gdisk 和 fdisk ,分别适用于 GPT 分区格式和 MBR 分区格式。如果你的磁盘分区是 GPT 那么就使用 gdisk 工具,否则就使用 fdisk 工具,最好不要混用,以免造成数据丢失的后果。
gdisk 工具语法如下:
$ gdisk [-l] device
如果指定了 -l 参数则用于显示设备的信息,以 /dev/sda 为例:
$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.6
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present # 表示当前磁盘是 GPT 分区
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 3FD2DD42-E48F-42AA-B691-F84F18580A8F # 这里显示的是设备的 UUID, 不是分区表
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 1534094701 sectors (731.5 GiB)
# 分区数据(1表示的就是 sda1, 2表示的就是 sda2)
Number Start (sector) End (sector) Size Code Name
1 2048 209717247 100.0 GiB 8300 Linux filesystem
2 209717248 419432447 100.0 GiB 8300 Linux filesystem
如果是想要管理(修改或创建分区表)就使用下面的命令语法:
$ gdisk device
下面来看下具体操作:
$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.6
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present # GPT 分区
Found valid GPT with protective MBR; using GPT.
Command (? for help): # 输入命令执行才做, ? 来看下有哪些命令
gdisk 参数说明
当我们键入 ? 后显示如下:
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition <=============== # 删除分区
i show detailed information on a partition
l list known partition types
n add a new partition <=============== # 创建新分区
o create a new empty GUID partition table (GPT)
p print the partition table <=============== # 显示当前分区表信息
q quit without saving changes <=============== # 直接退出, 放弃修改的结果
r recovery and transformation options (experts only) # 进入恢复模式
s sort partitions
t change a partition's type code
v verify disk <=============== # 验证分区修改结果
w write table to disk and exit <=============== # 保存修改的操作并退出
x extra functionality (experts only)
? print this menu
Command (? for help):
其实上面显示的已经很清晰了,我们只需要按照说明傻瓜式的操作即可。另外,有两个参数需要特别说明:
q 表示不保存修改结果,直接退出 gdisk(如果是个人学习的话建议使用这个)。
w 表示保存修改结果并退出 gdisk。
显示分区表信息
Command (? for help): p
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 3FD2DD42-E48F-42AA-B691-F84F18580A8F
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 1534094701 sectors (731.5 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 209717247 100.0 GiB 8300 Linux filesystem
2 209717248 419432447 100.0 GiB 8300 Linux filesystem
Command (? for help):
删除分区表
Command (? for help): d # d 命令删除
Partition number (1-2): 2 # 输入指定分区编号
Command (? for help): p # 查看修改后的分区信息
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 3FD2DD42-E48F-42AA-B691-F84F18580A8F
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 1743809901 sectors (831.5 GiB)
# 此时还有一个分区
Number Start (sector) End (sector) Size Code Name
1 2048 209717247 100.0 GiB 8300 Linux filesystem
Command (? for help): d # 继续删除
Using 1 # 因为只有一个分区了, 所以就直接删除掉了
Command (? for help): p # 再看下修改后的分区信息
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 3FD2DD42-E48F-42AA-B691-F84F18580A8F
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 1953525101 sectors (931.5 GiB) # 此时待分配的空间又变成 1T 了
Number Start (sector) End (sector) Size Code Name
注意, d 虽然是分区命令,但是还没有执行保存操作。此时我们输入 q 退出的话是没有任何变化的,只有 w 保存才会生效。
创建分区表
仔细看下面的操作,连续创建3个分区:
Command (? for help): n # 创建第一个分区
Partition number (1-128, default 1): # 最大允许分区编号是 128, 当前还没有任何分区, 所以默认为 1. 对应的分区设备就是 sda1
First sector (34-1953525134, default = 2048) or {+-}size{KMGTP}: # 分区开始位置, 默认即可
Last sector (2048-1953525134, default = 1953525134) or {+-}size{KMGTP}: +100G # 分区结束位置, 可以输入具体值, 也可以是容量单位(前面必须有 + 符号)
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): # 8300 Linux默认系统格式
Changed type of partition to 'Linux filesystem'
Command (? for help): p # 看下分区信息
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 3FD2DD42-E48F-42AA-B691-F84F18580A8F
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 1743809901 sectors (831.5 GiB) # 剩余可分配空间
Number Start (sector) End (sector) Size Code Name
1 2048 209717247 100.0 GiB 8300 Linux filesystem # 第一个分区, 大小 100G
Command (? for help): n # 创建第二个分区
Partition number (2-128, default 2): # 默认
First sector (34-1953525134, default = 209717248) or {+-}size{KMGTP}: # 默认
Last sector (209717248-1953525134, default = 1953525134) or {+-}size{KMGTP}: +300G ## 第二个分区大小为 300G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): # 默认 Linux 系统格式
Changed type of partition to 'Linux filesystem'
Command (? for help): p # 再看下分区信息
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 3FD2DD42-E48F-42AA-B691-F84F18580A8F
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 1114664301 sectors (531.5 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 209717247 100.0 GiB 8300 Linux filesystem
2 209717248 838862847 300.0 GiB 8300 Linux filesystem # 第二个分区大小为 300G
Command (? for help): n ## 创建第三个分区
Partition number (3-128, default 3):
First sector (34-1953525134, default = 838862848) or {+-}size{KMGTP}:
Last sector (838862848-1953525134, default = 1953525134) or {+-}size{KMGTP}: # 注意这里, 没有指定分区大小, 默认会取最大值
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 3FD2DD42-E48F-42AA-B691-F84F18580A8F
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 209717247 100.0 GiB 8300 Linux filesystem
2 209717248 838862847 300.0 GiB 8300 Linux filesystem
3 838862848 1953525134 531.5 GiB 8300 Linux filesystem # 因为第三次没有指定分区大小, 所以将剩余的全部可分配空间全部分配了
保存分区表
分区创建完成之后就可以保存退出了,这里有两个选择:不保存修改结果直接退出 q 和 保存修改结果并退出 w 。
现在来使用 w :
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y # 确认修改
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.
当保存之后就可以使用 parted 工具或 gdisk 工具再来看下 /dev/sda 设备分区信息了:
parted 工具输出示例:
$ sudo parted /dev/sda print
Model: ATA ST1000LM048-2E71 (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 107GB 107GB Linux filesystem
2 107GB 429GB 322GB Linux filesystem
3 429GB 1000GB 571GB Linux filesystem
gdisk 工具输出示例:
$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.6
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 3FD2DD42-E48F-42AA-B691-F84F18580A8F
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 209717247 100.0 GiB 8300 Linux filesystem
2 209717248 838862847 300.0 GiB 8300 Linux filesystem
3 838862848 1953525134 531.5 GiB 8300 Linux filesystem
不管哪个工具,我们都能看到相同的结果。到此,分区就修改完成了~
GPT 转 MBR 格式
分区格式其实是可以转换的,还记得 ? 命令里面有个恢复模式 r 命令?现在来看下将 GPT 转 MBR 分区:
$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.6
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only) ## r 恢复模式
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
Command (? for help): r # r 命令
Recovery/transformation command (? for help): ? # 看下恢复模式有哪些命令
b use backup GPT header (rebuilding main)
c load backup partition table from disk (rebuilding main)
d use main GPT header (rebuilding backup)
e load main partition table from disk (rebuilding backup)
f load MBR and build fresh GPT from it
g convert GPT into MBR and exit # GPT 转 MBR 命令
h make hybrid MBR
i show detailed information on a partition
l load partition data from a backup file
m return to main menu
o print protective MBR data
p print the partition table
q quit without saving changes
t transform BSD disklabel partition
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
Recovery/transformation command (? for help): g # GPT 转 MBR
MBR command (? for help): ? # 注意看, 此时已经变成 MBR 了
a toggle the active/boot flag
c recompute all CHS values
l set partition as logical
o omit partition
p print the MBR partition table
q quit without saving changes
r set partition as primary
s sort MBR partitions
t change partition type code
w write the MBR partition table to disk and exit
MBR command (? for help): w # 保存并退出
Converted 3 partitions. Finalize and exit? (Y/N): y # 确认保存
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
保存之后最好再使用 partprobe 工具检查修复下:
$ sudo partprobe
现在再来看下分区格式:
$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.6
Warning: Partition table header claims that the size of partition table
entries is 0 bytes, but this program supports only 128-byte entries.
Adjusting accordingly, but partition table may be garbage.
Warning: Partition table header claims that the size of partition table
entries is 0 bytes, but this program supports only 128-byte entries.
Adjusting accordingly, but partition table may be garbage.
Partition table scan:
MBR: MBR only # 看这里, 变成 MBR 分区了
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format # MBR 分区
in memory.
***************************************************************
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: ST1000LM048-2E71
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 4E085E7E-5F9F-4E85-858E-3AE0871A5DBC
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 209717247 100.0 GiB 8300 Linux filesystem
2 209717248 838862847 300.0 GiB 8300 Linux filesystem
3 838862848 1953525134 531.5 GiB 8300 Linux filesystem
MBR 转 GPT 格式
既然 GPT 能转 MBR,那么 MBR 肯定也能转 GPT 了。MBR 转 GPT 格式简直不要太简单,直接保存即可:
$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.6
Warning: Partition table header claims that the size of partition table
entries is 0 bytes, but this program supports only 128-byte entries.
Adjusting accordingly, but partition table may be garbage.
Warning: Partition table header claims that the size of partition table
entries is 0 bytes, but this program supports only 128-byte entries.
Adjusting accordingly, but partition table may be garbage.
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************
Command (? for help): w ## 直接保存就会转换成 GPT 分区了
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y ## 确认转换
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.
fdisk 管理 MBR 格式分区
fdisk 工具与 gdisk 工具是一致的,都是按照提示操作即可。不过 fdisk 有几个可用于查看磁盘信息的参数,这里需要说明下:
$ fdisk [-lx] -o [+][column...] [device]
-l 显示磁盘设备信息
-x 显示磁盘设备详细信息(比 -l 多一些)
-o 显示指定列或显示额外列(与 lsblk 使用方式一样)
另外,如果不指定具体磁盘设备会显示所有磁盘设备数据~
以 /dev/sda 为例:
显示分区信息
-l 参数只会显示基本信息:
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
Device Start End Sectors Size Type
/dev/sda1 2048 209717247 209715200 100G Linux filesystem
/dev/sda2 209717248 838862847 629145600 300G Linux filesystem
/dev/sda3 838862848 1953525134 1114662287 531.5G Linux filesystem
而 -x 则会显示分区UUID、名称等数据:
$ sudo fdisk -x /dev/sda
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
First LBA: 34
Last LBA: 1953525134
Alternative LBA: 1953525167
Partition entries LBA: 2
Allocated partition entries: 128
Device Start End Sectors Type-UUID UUID Name Attrs
/dev/sda1 2048 209717247 209715200 0FC63DAF-8483-4772-8E79-3D69D8477DE4 8CEC377B-7907-4252-A34E-B96CF3F68009 Linux filesystem
/dev/sda2 209717248 838862847 629145600 0FC63DAF-8483-4772-8E79-3D69D8477DE4 2C541EF2-A66F-4321-A283-448AA74F6EAF Linux filesystem
/dev/sda3 838862848 1953525134 1114662287 0FC63DAF-8483-4772-8E79-3D69D8477DE4 A7855368-BD5F-467A-86C8-E22C48A08049 Linux filesystem
其他的就没啥好说的了~
fdisk 参数说明
使用 fdisk 管理磁盘分区命令与 gdisk 一致,当进入 fdisk 后就会提示你如何操作了:
$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
唯一的区别是 gdisk 的帮助命令是 ? ,而 fdisk 的帮助命令是 m !
当输入 m 之后就能看到所有命令了:
Command (m for help): m
Help:
GPT
M enter protective/hybrid MBR
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
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
查看分区表信息
这里与 gdisk 一致,就不做额外说明了:
Command (m for help): p
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
Device Start End Sectors Size Type
/dev/sda1 2048 209717247 209715200 100G Linux filesystem
/dev/sda2 209717248 838862847 629145600 300G Linux filesystem
/dev/sda3 838862848 1953525134 1114662287 531.5G Linux filesystem
删除分区表
Command (m for help): p ## 查看分区表信息
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
# 有三个分区表
Device Start End Sectors Size Type
/dev/sda1 2048 209717247 209715200 100G Linux filesystem
/dev/sda2 209717248 838862847 629145600 300G Linux filesystem
/dev/sda3 838862848 1953525134 1114662287 531.5G Linux filesystem
Command (m for help): d # 删除分区表
Partition number (1-3, default 3): # 默认最大的那个分区表
Partition 3 has been deleted.
Command (m for help): p
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
# 现在只有两个分区表了
Device Start End Sectors Size Type
/dev/sda1 2048 209717247 209715200 100G Linux filesystem
/dev/sda2 209717248 838862847 629145600 300G Linux filesystem
Command (m for help): d # 继续删除
Partition number (1,2, default 2):
Partition 2 has been deleted.
Command (m for help): p
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
# 还一个分区表了
Device Start End Sectors Size Type
/dev/sda1 2048 209717247 209715200 100G Linux filesystem
Command (m for help): d # 继续删除
Selected partition 1
Partition 1 has been deleted.
Command (m for help): p # 现在就没有分区表了
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
创建分区表
Command (m for help): n # 创建分区表
Partition number (1-128, default 1): # 默认第一个分区表编号是 1
First sector (34-1953525134, default 2048): # 与 GPT 一致
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1953525134, default 1953525134): +200G ## 分配 200G 空间
Created a new partition 1 of type 'Linux filesystem' and of size 200 GiB.
Command (m for help): p # 查看分区表
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
Device Start End Sectors Size Type
/dev/sda1 2048 419432447 419430400 200G Linux filesystem # 第一个分区
Command (m for help): n # 继续创建
Partition number (2-128, default 2): # 第二个分区表编号, 默认自动累加
First sector (419432448-1953525134, default 419432448):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (419432448-1953525134, default 1953525134): +500G # 分配 500G
Created a new partition 2 of type 'Linux filesystem' and of size 500 GiB.
Command (m for help): p
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
Device Start End Sectors Size Type
/dev/sda1 2048 419432447 419430400 200G Linux filesystem
/dev/sda2 419432448 1468008447 1048576000 500G Linux filesystem # 第二个分区表
Command (m for help): n # 继续创建
Partition number (3-128, default 3):
First sector (1468008448-1953525134, default 1468008448):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1468008448-1953525134, default 1953525134): # 默认最大值
Created a new partition 3 of type 'Linux filesystem' and of size 231.5 GiB.
Command (m for help): p
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM048-2E71
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: E4FB3887-8182-4CC1-82F5-484D4B204F1B
Device Start End Sectors Size Type
/dev/sda1 2048 419432447 419430400 200G Linux filesystem
/dev/sda2 419432448 1468008447 1048576000 500G Linux filesystem
/dev/sda3 1468008448 1953525134 485516687 231.5G Linux filesystem # 第三个分区表
fdisk 工具其实与 gdisk 工具操作是一致的,照着说明说操作就好了~
保存分区表
最后别忘了保存啊,如果想要直接丢弃就使用 q 命令即可,但是如果想要保存变更就使用 w :
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
如何使用分区
当分区创建完成后是没法直接使用的,因为我们还没有指定分区的具体文件类型。文件类型有很多种,比如 Linux 常用的 EXT2、3、4,Windows 常用的 vfat 和 ntfs。
为了演示我使用一块权限的磁盘做说明:
$ sudo parted /dev/sdb print
Model: ATA ST1000LM048-2E71 (scsi)
Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.7GB 10.7GB ext4 Linux filesystem
2 10.7GB 21.5GB 10.7GB Linux filesystem
这块硬盘我创建了两个分区,分区 1 使用的是 ext4 文件系统,而分区 2 还没有进行格式化。现在我如果将这两个分区分别挂载到 ~/Volume1 和 ~/Volume2 上会怎样:
$ sudo mount /dev/sdb1 ~/Volume1
$ sudo mount /dev/sdb2 ~/Volume2
mount: /home/ituknown/Volume2: wrong fs type...
结果 /dev/sdb1 正常挂载, /dev/sdb2 则提示文件类型有问题,原因就是因为 /dev/sdb2 没有进行格式化!
其实,所谓的格式化就是将一块磁盘空间格式化成指定的一种文件系统类型。
比如现在将 /dev/sdb2 格式化成 vfat :
$ sudo mkfs.vfat /dev/sdb2
$ sudo parted /dev/sdb print
Model: ATA ST1000LM048-2E71 (scsi)
Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.7GB 10.7GB ext4 Linux filesystem
2 10.7GB 21.5GB 10.7GB fat32 Linux filesystem # 分区2文件系统就成了 fat32 了
现在我们如果再执行挂载就一切正常了:
$ sudo mount /dev/sdb2 ~/Volume2
在看下磁盘挂载情况:
$ sudo lsblk -lp
/dev/sdb 8:16 0 931.5G 0 disk
/dev/sdb1 8:17 0 10G 0 part /home/ituknown/Volume1
/dev/sdb2 8:18 0 10G 0 part /home/ituknown/Volume2
...
怎么样,现在是不是就解决了很多疑惑~
参考链接
https://en.wikipedia.org/wiki/GUID_Partition_Table
https://en.wikipedia.org/wiki/Master_boot_record
https://zhuanlan.zhihu.com/p/26098509
https://www.howtogeek.com/193669/whats-the-difference-between-gpt-and-mbr-when-partitioning-a-drive/
https://linux.die.net/man/8/mkfs
https://www.thegeekdiary.com/mkfs-ext4-command-examples-in-linux/