当前位置: 首页 > ai >正文

[Linux] Linux硬盘分区管理

目录

硬盘为什么要分区?

MBR 分区方案

fdisk工具

查看分区

删除分区

创建分区

非交互方式管理

GPT 分区方案

gdisk 工具

gdisk命令语法

查看分区表

转换分区表方案

创建分区

更改分区名称

查看分区详细信息

删除分区

wipefs 工具

parted 工具

查看分区表

设置单位

管理 MBR 磁盘

设置磁盘分区管理方案

创建分区

扩展分区

删除分区

免交互操作

管理 GPT 磁盘

设置磁盘分区管理方案

创建分区

扩展分区

删除分区

免交互操作

文件系统持久化挂载

环境准备

持久化挂载

取消持久化挂载

综合案例:文件系统空间不足

情况1:大量的大文件占用空间

准备环境

解决办法:找到该文件并删除

情况2:删除文件后,空间没有被释放

准备环境

解决办法:终止该程序

情况3:大量小文件占用空间

准备环境

解决方法:删除大量的小文件

综合案例:恢复删除文件

情况1:被删除的文件正在被占用

准备环境

情况2:恢复 xfs 文件系统文件

情况3:恢复 ext4 文件系统文件

情况2:恢复 xfs 文件系统文件

情况3:恢复 ext4 文件系统文件


硬盘为什么要分区?

  • 将操作系统文件与用户文件分隔开,避免应用数据太多撑满操作系统盘。

  • 限制应用或用户的可用空间。

  • 如果一个分区出现逻辑损坏,仅损坏该分区数据而不影响硬盘上其他分区。

  • 用于创建交换分区。

  • 限制磁盘空间使用,以提高诊断工具和备份镜像的性能。

  • 便于定制文件系统,例如有的文件系统存放大量小文件,有的文件系统存放大量大文件。

MBR 分区方案

自 1982年以来,主启动记录(MBR)分区方案指定了在运行 BIOS 固件的系统上如何对磁盘进行分区:

  • 该方案支持最多4个主分区。

  • 在 Linux系统上,管理员可以使用扩展分区和逻辑分区来创建最多 15个分区

    逻辑分区是可以格式化(format),扩展分区是不可以格式化。

  • MBR 记录用4个字节(1byte=8bit)存储分区的总扇区数,最大能表示2的32次方的扇区个数,按每扇区512字节计算,每个分区最大不能超过 2 TiB

通常,我们将磁盘第一个扇区称为主引导扇区,位于硬盘的柱面0、磁头0、扇区1的位置,这一扇区包含MBR引导代码,承担系统启动职能。它不属于磁盘上任何分区,因而分区空间内的格式化命令不能清除主引导记录的任何信息。

主引导扇区由三个部分组成:

  • 引导程序(占446个字节),硬盘启动时将系统控制权转给分区表中的某个操作系统。

  • 磁盘分区表项(DPT,Disk Partition Table),由四个分区表项构成(每个16个字节)。

  • 结束标志(占2个字节),其值为AA55(十六进制)。

fdisk工具

fdisk工具可用于管理采用MBR分区方案的磁盘,用户可以根据实际情况进行划分分区。

进入 fdisk 交互界面后,输入 m 可查看帮助。以下是关键命令:

|命令|功能| |-|-| |n|新建分区| |d|删除分区| |p|打印当前分区表| |t|修改分区类型(如 Linux=83, swap=82, EFI=EF00)| |w|保存并退出| |q|不保存退出| |g|创建新的 GPT 分区表(会清空磁盘!)| |o|创建新的 MBR 分区表(会清空磁盘!)|

查看分区

# 方法1:
[root@rocky8 ~]# fdisk -l /dev/sdbDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffeDevice Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux# 方法2:
[root@rocky8 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.# 输入m,查看帮助信息
Command (m for help): `m`
Command actiona   toggle a bootable flagb   edit bsd disklabelc   toggle the dos compatibility flagd   delete a partitiong   create a new empty GPT partition tableG   create an IRIX (SGI) partition tablel   list known partition typesm   print this menun   add a new partitiono   create a new empty DOS partition tablep   print the partition tableq   quit without saving changess   create a new empty Sun disklabelt   change a partition's system idu   change display/entry unitsv   verify the partition tablew   write table to disk and exitx   extra functionality (experts only)# 输入p,打印分区表
Command (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffeDevice Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux# 输入q,退出管理
Command (m for help): q

删除分区

[root@rocky8 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.# 输入p,打印分区表
Command (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffeDevice Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux# 输入d,删除分区,因为只有1个分区,所以自动删除了第一个分区
Command (m for help): `d`
Selected partition 1
Partition 1 is deletedCommand (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffeDevice Boot      Start         End      Blocks   Id  System

创建分区

# 输入n,创建一个新分区
Command (m for help): `n`# 选择分区类型
Partition type:p   primary (0 primary, 0 extended, 4 free)e   extended
# 直接回车,选择默认分区类型:primary
Select (default p): `回车`
Using default response p# 直接回车,分区号,使用默认值1
Partition number (1-4, default 1): # 直接回车,设置分区起始位置为:默认值2048扇区,也就是1M位置。
First sector (2048-41943039, default 2048): 
Using default value 2048# 设置分区结束位置,输入+2G,也就是起始位置之后2
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G
Partition 1 of type Linux and of size 2 GiB is setCommand (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffeDevice Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux# 输入w,保存更改并退出
# 输入q,不保存更改并退出
Command (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.# 再次验证分区表变化
[root@rocky8 ~]# lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  20G  0 disk 
`-sdb1   8:17   0   2G  0 part

注意:如果此时分区表未生成,执行以下命令,通知kernel重新生成分区表。有时候重启系统才会生成最新分区表。

[root@rocky8 ~]# partprobe

如果硬盘空间比较大,需要的文件系统数量超过4个,那么就需要借助扩展分区创建逻辑分区了。

如下示例,创建第4个分区的时候,类型选择扩展分区;创建第5个分区的时候,类型选择逻辑分区。

[root@rocky8 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.# 再次创建一个容量为3G的分区
Command (m for help): n
Partition type:p   primary (1 primary, 0 extended, 3 free)e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 
First sector (4196352-41943039, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +3G
Partition 2 of type Linux and of size 3 GiB is set# 再次创建一个容量为4G的分区
Command (m for help): n
Partition type:p   primary (2 primary, 0 extended, 2 free)e   extended
Select (default p): 
Using default response p
Partition number (3,4, default 3): 
First sector (10487808-41943039, default 10487808): 
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +4G
Partition 3 of type Linux and of size 4 GiB is set# 创建第4个分区的时候,选择扩展分区,并且使用剩余所有容量
Command (m for help): n
Partition type:p   primary (3 primary, 0 extended, 1 free)e   extended
Select (default e): 
Using default response e
Selected partition 4
First sector (18876416-41943039, default 18876416): 
Using default value 18876416
Last sector, +sectors or +size{K,M,G} (18876416-41943039, default 41943039): 
Using default value 41943039
Partition 4 of type Extended and of size 11 GiB is set# 创建第5个分区:在扩展分区中,创建逻辑分区,分配5G容量
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (18878464-41943039, default 18878464): 
Using default value 18878464
Last sector, +sectors or +size{K,M,G} (18878464-41943039, default 41943039): +5G
Partition 5 of type Linux and of size 5 GiB is setCommand (m for help): p         Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffeDevice Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    10487807     3145728   83  Linux
/dev/sdb3        10487808    18876415     4194304   83  Linux
/dev/sdb4        18876416    41943039    11533312    5  Extended
/dev/sdb5        18878464    29364223     5242880   83  LinuxCommand (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.# 查看分区表
[root@rocky8 ~]# lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  20G  0 disk 
|-sdb1   8:17   0   2G  0 part 
|-sdb2   8:18   0   3G  0 part 
|-sdb3   8:19   0   4G  0 part 
|-sdb4   8:20   0   1K  0 part 
|-sdb5   8:21   0   5G  0 part 

创建分区演示完成,只保留1个分区,多余的删除。

[root@rocky8 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Command (m for help): d
Partition number (1-5, default 5): 
Partition 5 is deletedCommand (m for help): d
Partition number (1-4, default 4): 
Partition 4 is deletedCommand (m for help): d
Partition number (1-3, default 3): 
Partition 3 is deletedCommand (m for help): d
Partition number (1,2, default 2): 
Partition 2 is deletedCommand (m for help): d
Selected partition 1
Partition 1 is deletedCommand (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
非交互方式管理

示例1:创建一个分区

# 将要执行的fdisk命令行写入到一个文本文件
[root@rocky8 ~]# vim fdisk-create.txt
n
p
1
2048
+2G
p
w# 执行
[root@rocky8 ~]# fdisk /dev/sdb < fdisk-create.txt
[root@rocky8 ~]# lsblk /dev/sdb
NAME &nbsp; MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb &nbsp; &nbsp; &nbsp;8:16 &nbsp; 0  20G &nbsp;0 disk 
|-sdb1 &nbsp; 8:17 &nbsp; 0 &nbsp; 2G &nbsp;0 part

示例2:删除一个分区

# 将要执行的fdisk命令行写入到一个文本文件
[root@rocky8 ~]# vim fdisk-delete.txt
d
p
w# 执行
[root@rocky8 ~]# fdisk /dev/sdb < fdisk-delete.txt
[root@rocky8 ~]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb &nbsp; &nbsp;8:16 &nbsp; 0  20G &nbsp;0 disk 

GPT 分区方案

GPT是运行统一可扩展固件接口(UEFI)固件系统上硬盘分区表的标准。

  • MBR分区方案只能管理最大2TiB分区和磁盘。全局唯一标识分区表(GPT,GUID Partition Table)使用8个字节(1byte=8bit)存储分区的总扇区数,可支持最多8 ZiB(=264*512 Byte),即80亿太字节的分区和磁盘。

  • MBR分区方案支持最多15个分区。GPT分区方案最多可提供128个分区

  • GPT提供分区表信息的冗余

GPT分区表结构如下:

  • LBA0(保护性MBR),分为两个部分:

  • 第一部分是与MBR中446字节相似的区块,用于存放第一阶段的启动引导程序

  • 第二部分是与MBR分区表记录区对应,该区域存储一个特殊标识符0xEE,用于表示该磁盘为GPT格式。若磁盘管理程序(比较老的软件)无法识别该磁盘,不能修改这个分区信息,进一步保护磁盘。

  • LBA1(GPT表头记录),记录了分区表自身的位置和大小,同时也记录了前面提到备份用的GPT分区所在位置(最后34个LBA),还放置了分区表的校验码(CRC32),校验码的作用是让操作系统判断GPT的正确与否,倘若发现错误则可以从备份的GPT中恢复正常运行。

  • LBA2-33,记录分区信息,每个LBA可以提供4组的分区记录,默认情况下可以有4×32=128组分区记录。因为每个LBA都有512字节,所以每组分区记录所占128字节,除去每组记录需要的标识符和相关记录信息外,GPT在每组记录中提供了64位记载分区的扇区总数。

  • 虽然GPT最大支持128个分区,但是实际使用过程中分区超过第120个会出现无法格式化使用的情况。

gdisk 工具

gdisk工具用于管理采用GPT分区方案的磁盘分区,主要用于管理磁盘容量超过2T的磁盘。

gdisk命令语法
gdisk [ -l ] device
查看分区表
[root@rocky8 ~]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.10# 这里显示识别到了MBR分区方案
Partition table scan:MBR: MBR onlyBSD: not presentAPM: not presentGPT: not present***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. 
***************************************************************Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1038836E-C6BD-48D3-A490-8FFF2133146B
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)Number  Start (sector) &nbsp;  End (sector)  Size &nbsp; &nbsp; &nbsp; Code  Name
转换分区表方案

gdisk工具用于管理gpt分区,所以我们需要将磁盘的分区管理方案由MBR转换成GPT。

[root@rocky8 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10Partition table scan:MBR: MBR onlyBSD: not presentAPM: not presentGPT: 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!
***************************************************************# 输入?,查看帮助信息。gdisk管理命令跟fdisk很相似。
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# 输入o,将磁盘的分区管理方案由MBR转换成GPT
Command (? for help): o &nbsp;
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 99FE6E5F-8316-428D-BA81-D824C786CC43
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)Number  Start (sector) &nbsp;  End (sector)  Size &nbsp; &nbsp; &nbsp; Code  Name# 输入w,保存更改并退出
# 输入q,不保存更改并退出
Command (? for help): wFinal 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/sdb.
The operation has completed successfully.
创建分区
[root@rocky8 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10# 这里识别到了GPT分区方案
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: presentFound valid GPT with protective MBR; using GPT.# 输入n,创建新分区
Command (? for help): `n`# 设置分区ID
Partition number (1-128, default 1): `回车`# 设置分区起始位置
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: `回车`# 设置分区结束位置
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: `+2G`
Current type is 'Linux filesystem'# 设置分区类型,输入L查看所有分区类型
Hex code or GUID (L to show codes, Enter = 8300): `L`
0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
3000 ONIE boot             3001 ONIE config           4100 PowerPC PReP boot   
4200 Windows LDM data      4201 Windows LDM metadata  7501 IBM GPFS            
7f00 ChromeOS kernel       7f01 ChromeOS root         7f02 ChromeOS reserved   
8200 Linux swap            8300 Linux filesystem      8301 Linux reserved      
8302 Linux /home           8400 Intel Rapid Start     8e00 Linux LVM           
a500 FreeBSD disklabel     a501 FreeBSD boot          a502 FreeBSD swap        
a503 FreeBSD UFS           a504 FreeBSD ZFS           a505 FreeBSD Vinum/RAID  
a580 Midnight BSD data     a581 Midnight BSD boot     a582 Midnight BSD swap   
a583 Midnight BSD UFS      a584 Midnight BSD ZFS      a585 Midnight BSD Vinum  
a800 Apple UFS             a901 NetBSD swap           a902 NetBSD FFS          
a903 NetBSD LFS            a904 NetBSD concatenated   a905 NetBSD encrypted    
a906 NetBSD RAID           ab00 Apple boot            af00 Apple HFS/HFS+      
af01 Apple RAID            af02 Apple RAID offline    af03 Apple label         
af04 AppleTV recovery      af05 Apple Core Storage    be00 Solaris boot        
bf00 Solaris root          bf01 Solaris /usr & Mac Z  bf02 Solaris swap        
bf03 Solaris backup        bf04 Solaris /var          bf05 Solaris /home       
bf06 Solaris alternate se  bf07 Solaris Reserved 1    bf08 Solaris Reserved 2  
bf09 Solaris Reserved 3    bf0a Solaris Reserved 4    bf0b Solaris Reserved 5  
c001 HP-UX data            c002 HP-UX service         ea00 Freedesktop $BOOT   
eb00 Haiku BFS             ed00 Sony system partitio  ed01 Lenovo system partit# 输入回车继续查看
Press the <Enter> key to see more codes:`回车` 
ef00 EFI System            ef01 MBR partition scheme  ef02 BIOS boot partition 
fb00 VMWare VMFS           fb01 VMWare reserved       fc00 VMWare kcore crash p
fd00 Linux RAID            # 输入回车,选择默认分区类型
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1A3D170A-56FC-4655-8B9C-01E1B89294FE
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)Number  Start (sector)    End (sector)  Size       Code  Name
1            2048         4196351   2.0 GiB     8300  Linux filesystem
更改分区名称
# 输入c,修改分区ID为1的分区名
Command (? for help): c
Using 1
# 输入新名称
Enter name: data01# 查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1A3D170A-56FC-4655-8B9C-01E1B89294FE
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)Number  Start (sector)    End (sector)  Size       Code  Name1            2048         4196351   2.0 GiB     8300  data01
查看分区详细信息
Command (? for help): i
Using 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: 19AE5CDC-1AB2-4EBA-B9E2-AE9E7BF42653
First sector: 2048 (at 1024.0 KiB)
Last sector: 4196351 (at 2.0 GiB)
Partition size: 4194304 sectors (2.0 GiB)
Attribute flags: 0000000000000000
Partition name: 'data01'
删除分区
# 输入d,删除分区ID为1的分区
Command (? for help): d
Using 1Command (? for help): p &nbsp; 
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1A3D170A-56FC-4655-8B9C-01E1B89294FE
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)Number  Start (sector) &nbsp;  End (sector)  Size &nbsp; &nbsp; &nbsp; Code  NameCommand (? for help): wFinal 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/sdb.
The operation has completed successfully.

wipefs 工具

作用:清除磁盘分区表信息。

注意:数据无价,操作需谨慎,最好提前备份。

# 清除未挂载磁盘的分区表
[root@rocky8 ~]# wipefs -a /dev/sdb# 禁止使用 -f 强制清除分区表
[root@rocky8 ~]# wipefs -fa /dev/sdb

parted 工具

parted 工具既可以管理采用MBR分区方案的磁盘,又可以管理采用GPT分区方案的磁盘。

parted 命令同时支持交互式操作和非交互式操作(编写脚本)。

我们先来看看交互式操作。

操作流程:

  1. 查看分区表。如果是未初始化硬盘,创建分区。

  2. 设置单位(MiB)

  3. 创建分区

  4. 调整分区大小

  5. 调整分区类型

  6. 删除分区

查看分区表
[root@rocky8 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb# 查看帮助,输入help
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) `help`                                                             align-check TYPE N                        check partition N for TYPE(min|opt) alignmenthelp [COMMAND]                           print general help, or help on COMMANDmklabel,mktable LABEL-TYPE               create a new disklabel (partition table)mkpart PART-TYPE [FS-TYPE] START END     make a partitionname NUMBER NAME                         name partition NUMBER as NAMEprint [devices|free|list,all|NUMBER]     display the partition table, available devices,free space, all found partitions, or a particular partitionquit                                     exit programrescue START END                         rescue a lost partition near START and ENDresizepart NUMBER END                    resize partition NUMBERrm NUMBER                                delete partition NUMBERselect DEVICE                            choose the device to editdisk_set FLAG STATE                      change the FLAG on selected devicedisk_toggle [FLAG]                       toggle the state of FLAG on selected deviceset NUMBER FLAG STATE                    change the FLAG on partition NUMBERtoggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBERunit UNIT                                set the default unit to UNITversion                                  display the version number and copyrightinformation of GNU Parted# 查看分区表,输入 print
(parted) `print`                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start  End  Size  File system  Name  Flags
设置单位

parted工具默认单位是MB(103K),设置为MiB(210KiB)

(parted) unit MiB
管理 MBR 磁盘
设置磁盘分区管理方案
# 输入mklabel或mktable设置磁盘分区管理方案
# 设置分区方案为msdos,也就是MBR,输入mklabel msdos
(parted) `mklabel msdos`                                                    
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
# 确认更改,输入y
Yes/No? `y`                                                                 
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
# 分区表已改成msdos
Partition Table: `msdos`
Disk Flags: Number  Start  End  Size  Type  File system  Flags

注意:parted命令所做更改立刻生效。

创建分区
# 创建分区,输入 mkpart
(parted) `mkpart`# 设置分区类型,输入 primary
Partition type?  primary/extended? `primary`                     # 设置分区文件系统类型,输入 xfs,实际不生效,格式化文件系统仍需手动操作
File system type?  [ext2]? `xfs`                                 # 设置分区起始位置,输入 1
Start? `1M` # 设置分区结束位置,输入 2049
End? `2049`                                                              
(parted) `print`                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start    End      Size     Type     File system  Flags
1      1.00MiB  2049MiB  2048MiB  primary  xfs
扩展分区
# 1 代表分区号,4097代表分区结束位置
(parted) resizepart 1 4097                                                
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start    End      Size     Type     File system  Flags
1      1.00MiB  4097MiB  4096MiB  primary  xfs
删除分区
# 1 代表分区号
(parted) rm 1                                                             
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start  End  Size  Type  File system  Flags
免交互操作
# 设置磁盘分区管理方案
[root@rocky8 ~]# parted /dev/sdb mklabel msdos
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this
disk will be lost. Do you want to continue?
Yes/No? y                                                                 
Information: You may need to update /etc/fstab.# 查看分区
[root@rocky8 ~]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start  End  Size  Type  File system  Flags# 创建分区
[root@rocky8 ~]# parted /dev/sdb unit MiB mkpart primary 1 2049
Information: You may need to update /etc/fstab.[root@rocky8 ~]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start    End      Size     Type     File system  Flags
1      1.00MiB  2049MiB  2048MiB  primary  xfs# 扩展分区
[root@rocky8 ~]# parted /dev/sdb unit MiB resizepart 1 5121
Information: You may need to update /etc/fstab.[root@rocky8 ~]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start    End      Size     Type     File system  Flags
1      1.00MiB  5121MiB  5120MiB  primary  xfs# 删除分区
[root@rocky8 ~]# parted /dev/sdb rm 1
Information: You may need to update /etc/fstab.[root@rocky8 ~]# parted /dev/sdb unit MiB print                              
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start  End  Size  Type  File system  Flags
管理 GPT 磁盘

管理GPT磁盘基本与管理MBR磁盘一致,除了创建分区。

设置磁盘分区管理方案
# 设置分区方案为gpt,输入mklabel gpt
(parted) `mklabel gpt`                                                      
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
Yes/No? y                                                                 
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
# 分区表已改成gpt
Partition Table: `gpt`
Disk Flags: Number  Start  End  Size  File system  Name  Flags
创建分区
(parted) unit MiB                                                         
(parted) mkpart                                                   # 设置分区名
Partition name?  []? data01                                               
File system type?  [ext2]? xfs                                            
Start? 1                                                                  
End? 2049                                                                 
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start    End      Size     File system  Name    Flags
1      1.00MiB  2049MiB  2048MiB  xfs          data01
扩展分区
(parted) resizepart 1 5121                                                
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start    End      Size     File system  Name    Flags
1      1.00MiB  5121MiB  5120MiB  xfs          data01
删除分区
(parted) rm 1                                                             
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start  End  Size  File system  Name  Flags
免交互操作
# 设置磁盘分区管理方案
[root@rocky8 ~]# parted /dev/sdb mklabel gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this
disk will be lost. Do you want to continue?
Yes/No? y                                                                 
Information: You may need to update /etc/fstab.# 查看分区
[root@rocky8 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start  End  Size  File system  Name  Flags# 创建分区
[root@rocky8 ~]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
Information: You may need to update /etc/fstab.[root@rocky8 ~]# parted /dev/sdb unit MiB print                         
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start    End      Size     File system  Name    Flags
1      1.00MiB  2049MiB  2048MiB  xfs          data01# 扩展分区
[root@rocky8 ~]# parted /dev/sdb unit MiB resizepart 1 5121
Information: You may need to update /etc/fstab.[root@rocky8 ~]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start    End      Size     File system  Name    Flags
1      1.00MiB  5121MiB  5120MiB  xfs          data01# 删除分区
[root@rocky8 ~]# parted /dev/sdb rm 1
Information: You may need to update /etc/fstab.[root@rocky8 ~]# parted /dev/sdb unit MiB print                         
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start  End  Size  File system  Name  Flags

文件系统持久化挂载

环境准备

利用 parted 创建一个分区,并格式化为xfs文件系统。

[root@rocky8 ~]# parted /dev/sdb mklabel gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this
disk will be lost. Do you want to continue?
Yes/No? y                                                                 
Information: You may need to update /etc/fstab.[root@rocky8 ~]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049      
Information: You may need to update /etc/fstab.[root@rocky8 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1    isize=512    agcount=4, agsize=131072 blks=             sectsz=512   attr=2, projid32bit=1=             crc=1        finobt=0, sparse=0
data     =             bsize=4096   blocks=524288, imaxpct=25=             sunit=0      swidth=0 blks
naming   =version 2    bsize=4096   ascii-ci=0 ftype=1
log      =internal log bsize=4096   blocks=2560, version=2=             sectsz=512   sunit=0 blks, lazy-count=1
realtime =none         extsz=4096   blocks=0, rtextents=0# 如果格式化时候,提示存在文件系统,则需要-f选项强制格式化
[root@rocky8 ~]# mkfs.xfs /dev/sdb1                             
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@rocky8 ~]# mkfs.xfs -f /dev/sdb1
持久化挂载

当服务器重启时,系统不会再次将文件系统自动挂载到目录树上,用户无法访问。为了确保系统在启动时自动挂载文件系统, 需要在 /etc/fstab文件中添加一个条目。

/etc/fstab 是以空格分隔的文件,每行具有六个字段。

  • 第一个字段指定设备。可以使用UUID或device来指定设备。

  • 第二个字段是目录挂载点。通过它可以访问目录结构中的块设备。挂载点必须存在;如果不存在,请使用mkdir命令进行创建。

  • 第三个字段包含文件系统类型,如xfs或ext4 。

  • 第四个字段是挂载选项,以逗号分隔的。 defaults是一组常用选项。详细信息参考mount(8) 。

  • 第五个字段指定dump命令是否备份设备。很少使用。

  • 第六个字段指定fsck顺序字段,决定了在系统启动吋是否应运行fsck命令,以验证文件系统是否干净。 该字段中的值指示了 fsck的运行顺序。 对于XFS文件系统, 请将该字段设为0 ,因为XFS并不使用fsck来检查自己的文件系统状态。 对于ext4 文件系统,如果是根文件系统, 请将该字段设 为 1 ; 如果是其他ext4 文件系统, 则将该字段设为2。 这样, fsck就会先处理根文件系统,然后同步检查不同磁盘上的文件系统,并按顺序检查同一磁盘上的文件系统。

示例:

[root@rocky8 ~]# blkid /dev/sdb1
/dev/sdb1: UUID="f5c35f10-0274-45af-b044-73694989fe01" TYPE="xfs" PARTLABEL="data01" PARTUUID="432d7d79-1a08-4429-bf38-7ab1444a0ab9"
[root@rocky8 ~]# mkdir /data01[root@rocky8 ~]# vim /etc/fstab
# 最后一行增加一个条目
UUID="f5c35f10-0274-45af-b044-73694989fe01" /data01 xfs defaults 0 0# 使用如下命令立刻挂载
[root@rocky8 ~]# mount /data01
# 或者
[root@rocky8 ~]# mount /dev/sdb1# 验证
[root@rocky8 ~]# df -h /data01
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G   33M  2.0G   2% /data01# 重启系统验证
[root@rocky8 ~]# reboot
[root@rocky8 ~]# df -h /data01
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G   33M  2.0G   2% /data01
取消持久化挂载

删除/etc/fstab中对应条目即可。

综合案例:文件系统空间不足

情况1:大量的大文件占用空间
准备环境
parted /dev/sda mklabel gpt
parted /dev/sda unit MiB mkpart data01 xfs 1 2049
mkfs.xfs /dev/sda1
mkdir /myapp
#挂载
mount /dev/sda1 /myapp
cp -r /etc/ /myapp/#创建大文件
dd if=/dev/zero of=/myapp/etc/authselect/custom/bigfile bs=1M count=2000
解决办法:找到该文件并删除
find /myapp/ -size +100M
#删除即可

情况2:删除文件后,空间没有被释放

准备环境
#创建大文件
dd if=/dev/zero of=/myapp/etc/authselect/custom/bigfile bs=1M count=2000
#让程序始终在后台运行
tail -f /myapp/etc/authselect/custom/bigfile &#删除文件后,空间没有被释放
rm -f /myapp/etc/authselect/custom/bigfile
df -h /myapp/

原因:被删除的文件仍有程序在使用

解决办法:终止该程序
lsof | grep delete |grep /myapp
kill 227852
df -h /myapp/

清理大文件时,建议先使用重定向清空文件内容,再删除文件

情况3:大量小文件占用空间

准备环境
parted /dev/sda unit MiB mkpart data02 ext4 2049 3073
mkfs.ext4 /dev/sda2
mkdir /myapp-2
mount /dev/sda2 /myapp-2
#创建大量小文件
touch /myapp-2/file-{00001..70000}

此时显示设备上没有空间,但实际上是有空间的

原因:文件系统中的inode使用完了

解决方法:删除大量的小文件

查看inode发现已经被使用完了

#思路1:直接删除
rm -f /myapp-2/file-0*#思路2:合并大量i奥文件为单个文件
cat /myapp-2/dile-{00001..10000} > file-00001

综合案例:恢复删除文件

情况1:被删除的文件正在被占用
准备环境
#后台运行
tail -f /myapp/etc/fstab &
#误删文件
rm -f /myapp/etc/fstab

解决办法

lsof /myapp | grep /myapp/etc/fstab
cd /proc/393447/fd
#复制一份
cp 3 /root/fstab-restore

情况2:恢复 xfs 文件系统文件

前提:准备1个xfs文件系统。

[root@rocky8 ~]# dnf install -y tcl tcllib
情况3:恢复 ext4 文件系统文件

前提:准备1个 ext4 文件系统。

[root@rocky8 ~]# dnf install extundelete
情况2:恢复 xfs 文件系统文件

前提:准备1个xfs文件系统。

[root@rocky8 ~]# dnf install -y tcl tcllib
情况3:恢复 ext4 文件系统文件

前提:准备1个 ext4 文件系统。

[root@rocky8 ~]# dnf install extundelete
http://www.xdnf.cn/news/17939.html

相关文章:

  • 配置 Docker 镜像加速,解决 docker pull 拉取镜像失败、docker search 查询镜像失败等问题
  • 数据库Microsoft Access、SQL Server和SQLite三者对比及数据库的选型建议
  • Win11和Win10共享打印机提示709用添加Windows凭据来解决的小方法
  • 【UHD】vivado 2021.1 编译
  • 接口自动化测试框架搭建
  • maven与maven-archetype-plugin版本匹配问题
  • 一周学会Matplotlib3 Python 数据可视化-绘制绘制甘特图
  • 跑实验记录
  • Python Day30 CSS 定位与弹性盒子详解
  • python---内置函数
  • 微服务之注册中心与ShardingSphere关于分库分表的那些事
  • 【手撕JAVA多线程】1.从设计初衷去看JAVA的线程操作
  • Camera相机人脸识别系列专题分析之十九:MTK ISP6S平台FDNode原生代码
  • 【自动化运维神器Ansible】Ansible比较操作符详解:从基础到实战应用
  • 笔试——Day40
  • AI生成视频开源模型技术解析
  • 算法题打卡力扣第42题接雨水(hard)
  • OpenJDK 17的C1和C2编译器实现中,方法返回前插入安全点(Safepoint Poll)的机制
  • 拒绝造轮子(C#篇)ZLG CAN卡驱动封装应用
  • 贺雨禾《梨花往事》北京首映,“野草型演员”深耕走出新赛道
  • 第4问 常见的指标有哪些?
  • 【CVPR2025】计算机视觉|GIFNet:一个模型实现所有图像融合任务!还能增强画质?!
  • [1Prompt1Story] 滑动窗口机制 | 图像生成管线 | VAE变分自编码器 | UNet去噪神经网络
  • 【Qt开发】常用控件(四)
  • 《深度解构:构建浏览器端Redis控制台的WebSocket协议核心技术》
  • 开源 Arkts 鸿蒙应用 开发(十八)通讯--Ble低功耗蓝牙服务器
  • Flink Stream API 源码走读 - window 和 sum
  • 前端开发入门书籍推荐:Vue.js 3与前端基础的完美组合
  • 九尾狐未来机械锂晶核
  • 数据结构:满二叉树 (Full Binary Tree) 和 完全二叉树 (Complete Binary Tree)