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

15.Linux 硬盘分区管理

文章目录

  • Linux 硬盘分区管理
    • 硬盘为什么要分区?
    • MBR 分区方案
    • fdisk 工具
      • fdisk命令语法
      • 查看分区
      • 创建分区
      • 非交互方式管理
    • GPT 分区方案
    • gdisk 工具
      • gdisk命令语法
      • 查看分区表
      • 转换分区表方案
      • 创建分区
      • 更改分区名称
      • 查看分区详细信息
      • 删除分区
    • wipefs 工具
    • parted 工具
      • 查看分区表
      • 设置单位
      • 管理 MBR 磁盘
        • 设置磁盘分区管理方案
        • 创建分区
        • 扩展分区
        • 删除分区
        • 免交互操作
      • 管理 GPT 磁盘
        • 设置磁盘分区管理方案
        • 创建分区
        • 扩展分区
        • 删除分区
        • 免交互操作
    • 文件系统持久化挂载
      • 持久化挂载
      • 取消持久化挂载
    • 综合案例:文件系统空间不足
      • 情况1:大量的大文件占用空间
      • 情况2:删除文件后,空间没有释放
      • 情况3:大量的小文件占用空间

Linux 硬盘分区管理

硬盘为什么要分区?

  • 将操作系统文件与用户文件分隔开,避免应用数据太多撑满操作系统盘。
  • 限制应用或用户的可用空间。
  • 如果一个分区出现逻辑损坏,仅损坏该分区数据而不影响硬盘上其他分区。
  • 用于创建交换分区。
  • 限制磁盘空间使用,以提高诊断工具和备份镜像的性能。
  • 便于定制文件系统,例如有的文件系统存放大量小文件,有的文件系统存放大量大文件。

MBR 分区方案

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

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

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

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

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

类比为:笔记本只有4个usb接口,如果不够用,则将第四个usb接口外接扩展坞,由扩展坞提供多个usb接口。

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

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

在这里插入图片描述

  • 引导程序(占446个字节),硬盘启动时将系统控制权转给分区表中的某个操作系统。
  • 磁盘分区表项(DPT,Disk Partition Table),由四个分区表项构成(每个16个字节)。
  • 结束标志(占2个字节),其值为AA55(十六进制)。

fdisk 工具

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

fdisk命令语法

[root@server ~ 09:57:30]# fdisk -h
Usage:fdisk [options] <disk>    change partition tablefdisk [options] -l <disk> list partition table(s)fdisk -s <partition>      give partition size(s) in blocksOptions:-b <size>             sector size (512, 1024, 2048 or 4096)-c[=<mode>]           compatible mode: 'dos' or 'nondos' (default)-h                    print this help text-u[=<unit>]           display units: 'cylinders' or 'sectors' (default)-v                    print program version-C <number>           specify the number of cylinders-H <number>           specify the number of heads-S <number>           specify the number of sectors per track

除了使用-l选项查看分区表,其他选项暂时都不用

查看分区

fdisk工具大部分操作通过交互式完成,出了查看分区表。

DOS disklabel 指的硬盘管理方式是MBR。

我们使用上一章准备的一块硬盘/dev/sdb。

# 方法1:
[root@server ~ 09:58:30]# fdisk /dev/sdb -l磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节# 方法2:
[root@server ~ 09:58:57]# 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@server ~ 10:11:17]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。Device does not contain a recognized partition table
使用磁盘标识符 0x73930f31 创建新的 DOS 磁盘标签。
#查看帮助
命令(输入 m 获取帮助):m
命令操作a   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)命令(输入 m 获取帮助):p
#显示分区表
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x73930f31设备 Boot      Start         End      Blocks   Id  System命令(输入 m 获取帮助):n  #新建一个分区
Partition type:p   primary (0 primary, 0 extended, 4 free)e   extended
Select (default p): 
Using default response p
分区号 (1-4,默认 1)#回车
起始 扇区 (2048-41943039,默认为 2048)#回车
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+5G    #设置分区大小
分区 1 已设置为 Linux 类型,大小设为 5 GiB命令(输入 m 获取帮助):p
#查看是否建成
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x73930f31设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux命令(输入 m 获取帮助):w   #写入
The partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
#显示错误 告知可以使用partprobe命令
[root@server ~ 10:19:57]# partprobe 
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
#依然显示错误
[root@server ~ 10:20:22]# mkfs.xfs /dev/sdb1
mkfs.xfs: cannot open /dev/sdb1: 设备或资源忙
[root@server ~ 10:21:23]# mkdir /data1 
[root@server ~ 10:22:27]# mount /dev/sdb1 /data1
mount: /dev/sdb1 已经挂载或 /data1 忙
[root@server ~ 10:22:55]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                  485776       0   485776    0% /dev
tmpfs                     497816       0   497816    0% /dev/shm
tmpfs                     497816   14072   483744    3% /run
tmpfs                     497816       0   497816    0% /sys/fs/cgroup
/dev/mapper/centos-root 52403200 6356428 46046772   13% /
/dev/sda1                1038336  142212   896124   14% /boot
/dev/mapper/centos-home 49250820   33016 49217804    1% /home
/dev/sdb                20961280   33012 20928268    1% /data
tmpfs                      99564       0    99564    0% /run/user/0
/dev/sr0                 4635056 4635056        0  100% /usr/share/nginx/html/dvd#显示很多错误,查看发现/dev/sdb正在被/data挂载
[root@server ~ 10:23:25]# umount /data
#取消挂载,重新设置文件类型
[root@server ~ 10:23:36]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327680 blks=                       sectsz=512   attr=2, projid32bit=1=                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, 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
[root@server ~ 10:23:53]# mount /dev/sdb1 /data1
[root@server ~ 10:24:24]# lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  20G  0 disk 
└─sdb1   8:17   0   5G  0 part /data1

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

[root@server ~ 10:57:55]# partprobe
#若输入后任然无法打开,需要重启
[root@server ~ 10:57:55]# partprobe
Error: Partition(s) 1, 2 on /dev/sdb have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@server ~ 11:01:46]# reboot

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

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

[root@server ~ 10:58:55]# 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@server ~ 10:59:45]# 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@server ~ 11:02:55]# 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@server ~ 11:03:55]# vim fdisk-create.txt
n
p
1
2048
+2G
p
w# 执行
[root@server ~ 11:04:42]# fdisk /dev/sdb < fdisk-create.txt
[root@server ~ 11:05:42]# 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

示例2:删除一个分区

# 将要执行的fdisk命令行写入到一个文本文件
[root@server ~ 11:05:34]# vim fdisk-delete.txt
d
p
w# 执行
[root@server ~ 11:06:42]# fdisk /dev/sdb < fdisk-delete.txt
[root@server ~ 11:06:52]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb    8:16   0  20G  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@server ~ 10:34:11]# gdisk -l /dev/sdc
-bash: gdisk: 未找到命令
[root@server ~ 10:40:07]# yum provides gdisk
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
gdisk-0.8.10-3.el7.x86_64 : An fdisk-like partitioning tool for GPT disks
源    :base[root@server ~ 10:40:40]# yum install -y gdisk# 这里显示识别到了MBR分区方案
[root@server ~ 10:41:08]# gdisk -l /dev/sdc
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. 
***************************************************************Disk /dev/sdb: 41943040 sectors, 200.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 (200.0 GiB)Number  Start (sector)    End (sector)  Size       Code  Name

转换分区表方案

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

[root@server ~ 10:35:41]# 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  
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, 200.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 (200.0 GiB)Number  Start (sector)    End (sector)  Size       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@server ~ 10:41:14]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10Partition table scan:MBR: not presentBSD: not presentAPM: not presentGPT: not present
# 这里识别到了GPT分区方案
Creating new GPT entries.Command (? for help): n
Partition number (1-128, default 1): #回车
First sector (34-419430366, default = 2048) or {+-}size{KMGTP}: #回车
Last sector (2048-419430366, default = 419430366) or {+-}size{KMGTP}: +15G
Current type is '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/sdc: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 52BED818-2AEF-4638-975E-A262F13F1E3B
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 387973053 sectors (185.0 GiB)Number  Start (sector)    End (sector)  Size       Code  Name1            2048        31459327   15.0 GiB    8300  Linux filesystemCommand (? 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/sdc.
The operation has completed successfully.[root@server ~ 10:44:05]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part ├─centos-root 253:0    0   50G  0 lvm  /├─centos-swap 253:1    0    2G  0 lvm  [SWAP]└─centos-home 253:2    0   47G  0 lvm  /home
sdb               8:16   0   20G  0 disk 
└─sdb1            8:17   0    5G  0 part /data1
sdc               8:32   0  200G  0 disk 
└─sdc1            8:33   0   15G  0 part 
sr0              11:0    1  4.4G  0 rom  /usr/share/nginx/html/dvd

更改分区名称

# 输入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'

删除分区

[root@server ~ 10:47:01]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10Partition table scan:MBR: protectiveBSD: not presentAPM: not presentGPT: presentFound valid GPT with protective MBR; using GPT.Command (? for help): p
Disk /dev/sdc: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 52BED818-2AEF-4638-975E-A262F13F1E3B
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 178257853 sectors (85.0 GiB)Number  Start (sector)    End (sector)  Size       Code  Name1            2048        31459327   15.0 GiB    8300  Linux filesystem2        31459328       241174527   100.0 GiB   8300  Linux filesystemCommand (? for help): d
Partition number (1-2): 2
#默认从最后开始删除
Command (? for help): p
Disk /dev/sdc: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 52BED818-2AEF-4638-975E-A262F13F1E3B
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 387973053 sectors (185.0 GiB)Number  Start (sector)    End (sector)  Size       Code  Name1            2048        31459327   15.0 GiB    8300  Linux filesystemCommand (? 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/sdc.
The operation has completed successfully.

wipefs 工具

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

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

# 清除未挂载磁盘的分区表
[root@server ~ 11:16:10]# wipefs -a /dev/sdc
/dev/sdc:8 个字节已擦除,位置偏移为 0x00000200 (gpt):45 46 49 20 50 41 52 54
/dev/sdc:8 个字节已擦除,位置偏移为 0x31fffffe00 (gpt):45 46 49 20 50 41 52 54
/dev/sdc:2 个字节已擦除,位置偏移为 0x000001fe (PMBR):55 aa
/dev/sdc: calling ioclt to re-read partition table: 成功
[root@server ~ 11:17:30]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part ├─centos-root 253:0    0   50G  0 lvm  /├─centos-swap 253:1    0    2G  0 lvm  [SWAP]└─centos-home 253:2    0   47G  0 lvm  /home
sdb               8:16   0   20G  0 disk 
└─sdb1            8:17   0    5G  0 part 
sdc               8:32   0  200G  0 disk 
sr0              11:0    1  4.4G  0 rom  
[root@server ~ 11:19:16]# wipefs -a /dev/sdb
/dev/sdb:2 个字节已擦除,位置偏移为 0x000001fe (dos):55 aa
/dev/sdb: calling ioclt to re-read partition table: 成功
[root@server ~ 11:19:32]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part ├─centos-root 253:0    0   50G  0 lvm  /├─centos-swap 253:1    0    2G  0 lvm  [SWAP]└─centos-home 253:2    0   47G  0 lvm  /home
sdb               8:16   0   20G  0 disk 
sdc               8:32   0  200G  0 disk 
sr0              11:0    1  4.4G  0 rom  

parted 工具

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

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

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

操作流程:

  1. 查看分区表。如果是未初始化硬盘,创建分区。
  2. 设置单位(MiB)
  3. 创建分区
  4. 调整分区大小
  5. 调整分区类型
  6. 删除分区

查看分区表

[root@server ~ 11:19:43]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help         #查看帮助列表                                                align-check TYPE N                        check partition N forTYPE(min|opt) alignmenthelp [COMMAND]                           print general help, or help onCOMMANDmklabel,mktable LABEL-TYPE               create a new disklabel (partitiontable)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 particularpartitionquit                                     exit programrescue START END                         rescue a lost partition nearSTART and ENDresizepart NUMBER END                    resize partition NUMBERrm NUMBER                                delete partition NUMBERselect DEVICE                            choose the device to editdisk_set FLAG STATE                      change the FLAG on selecteddevicedisk_toggle [FLAG]                       toggle the state of FLAG onselected deviceset NUMBER FLAG STATE                    change the FLAG on partitionNUMBERtoggle [NUMBER [FLAG]]                   toggle the state of FLAG onpartition NUMBERunit UNIT                                set the default unit to UNITversion                                  display the version number andcopyright information 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
#或者 unit GiB

管理 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命令所做更改立刻生效。(实时生效,不用等待)

创建分区
(parted) mklabel msdos  #mklabel MBR模式                                                   
(parted) print   #查看列表                                                         
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start  End  Size  Type  File system  标志(parted) mkpart                                                           
分区类型?  primary/主分区/extended/扩展分区? primary    #选择分区你类型                 
文件系统类型?  [ext2]? xfs #选择分区类型
起始点? 1MiB                                                             #自定义起始结尾点
结束点? 10241MiB
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start   End     Size    Type     File system  标志1      1049kB  10.7GB  10.7GB  primary  xfs(parted) unit MiB                                                         
(parted) print                                                            
Model: ATA 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  标志1      1.00MiB  10241MiB  10240MiB  primary  xfs(parted) unit GiB       #可以设置单位,后来将不用修改单位                                                  
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20.0GiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start    End      Size     Type     File system  标志1      0.00GiB  10.0GiB  10.0GiB  primary  xfs
#一次性生成
(parted) mkpart primary 10242MiB 20470MiB
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20.0GiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start    End      Size     Type     File system  标志1      0.00GiB  10.0GiB  10.0GiB  primary  xfs2      10.0GiB  20.0GiB  9.99GiB  primary(parted) rm 2
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20.0GiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start    End      Size     Type     File system  标志1      0.00GiB  10.0GiB  10.0GiB  primary  xfs
#实时查看
[root@server ~ 11:23:50]# lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  20G  0 disk 
└─sdb1   8:17   0  10G  0 part 
扩展分区
# 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  Flags1      1.00MiB  4097MiB  4096MiB  primary  xfs
删除分区
(parted) rm 2
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 20.0GiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number  Start    End      Size     Type     File system  标志1      0.00GiB  10.0GiB  10.0GiB  primary  xfs
#实时查看
[root@server ~ 11:23:50]# lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  20G  0 disk 
└─sdb1   8:17   0  10G  0 part 
免交互操作
# 设置磁盘分区管理方案
[root@centos7 ~]# 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@centos7 ~]# 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@centos7 ~]# parted /dev/sdb unit MiB mkpart primary 1 2049
Information: You may need to update /etc/fstab.[root@centos7 ~]# 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  Flags1      1.00MiB  2049MiB  2048MiB  primary  xfs# 扩展分区
[root@centos7 ~]# parted /dev/sdb unit MiB resizepart 1 5121
Information: You may need to update /etc/fstab.[root@centos7 ~]# 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  Flags1      1.00MiB  5121MiB  5120MiB  primary  xfs# 删除分区
[root@centos7 ~]# parted /dev/sdb rm 1
Information: You may need to update /etc/fstab.[root@centos7 ~]# 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磁盘一致,除了创建分区。

设置磁盘分区管理方案
[root@server ~ 11:36:33]# parted /dev/sdc
GNU Parted 3.1
使用 /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt   #使用gpt形式                                                   
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start  End  Size  File system  Name  标志
创建分区
(parted) mkpart                                                           
分区名称?  []? webapp01                                                  
文件系统类型?  [ext2]? ext4                                              
起始点? 1MiB
结束点? 20481MiB
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start   End     Size    File system  Name      标志1      1049kB  21.5GB  21.5GB  ext4         webapp01(parted) unti MiBalign-check TYPE N                        check partition N forTYPE(min|opt) alignmenthelp [COMMAND]                           print general help, or help onCOMMANDmklabel,mktable LABEL-TYPE               create a new disklabel (partitiontable)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 particularpartitionquit                                     exit programrescue START END                         rescue a lost partition nearSTART and ENDresizepart NUMBER END                    resize partition NUMBERrm NUMBER                                delete partition NUMBERselect DEVICE                            choose the device to editdisk_set FLAG STATE                      change the FLAG on selecteddevicedisk_toggle [FLAG]                       toggle the state of FLAG onselected deviceset NUMBER FLAG STATE                    change the FLAG on partitionNUMBERtoggle [NUMBER [FLAG]]                   toggle the state of FLAG onpartition NUMBERunit UNIT                                set the default unit to UNITversion                                  display the version number andcopyright information of GNU Partedalign-check TYPE N                        check partition N forTYPE(min|opt) alignmenthelp [COMMAND]                           print general help, or help onCOMMANDmklabel,mktable LABEL-TYPE               create a new disklabel (partitiontable)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 particularpartitionquit                                     exit programrescue START END                         rescue a lost partition nearSTART and ENDresizepart NUMBER END                    resize partition NUMBERrm NUMBER                                delete partition NUMBERselect DEVICE                            choose the device to editdisk_set FLAG STATE                      change the FLAG on selecteddevicedisk_toggle [FLAG]                       toggle the state of FLAG onselected deviceset NUMBER FLAG STATE                    change the FLAG on partitionNUMBERtoggle [NUMBER [FLAG]]                   toggle the state of FLAG onpartition NUMBERunit UNIT                                set the default unit to UNITversion                                  display the version number andcopyright information of GNU Parted
(parted) unit MiB                                                         
(parted) mkpart webapp02 xfs 20482 40962                             #一次性创建     
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start     End       Size      File system  Name      标志1      1.00MiB   20481MiB  20480MiB  ext4         webapp012      20482MiB  40962MiB  20480MiB               webapp02(parted) rm 2
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start    End       Size      File system  Name      标志1      1.00MiB  20481MiB  20480MiB  ext4         webapp01(parted)                      
[root@server ~ 11:37:47]# lsblk /dev/sdc
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdc      8:32   0  200G  0 disk 
└─sdc1   8:33   0   20G  0 part 
扩展分区
(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    Flags1      1.00MiB  5121MiB  5120MiB  xfs          data01
删除分区
(parted) rm 2
(parted) print                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number  Start    End       Size      File system  Name      标志1      1.00MiB  20481MiB  20480MiB  ext4         webapp01
免交互操作
# 设置磁盘分区管理方案
[root@centos7 ~]# 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@centos7 ~]# 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@centos7 ~]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
Information: You may need to update /etc/fstab.[root@centos7 ~]# 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    Flags1      1.00MiB  2049MiB  2048MiB  xfs          data01# 扩展分区
[root@centos7 ~]# parted /dev/sdb unit MiB resizepart 1 5121
Information: You may need to update /etc/fstab.[root@centos7 ~]# 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    Flags1      1.00MiB  5121MiB  5120MiB  xfs          data01# 删除分区
[root@centos7 ~]# parted /dev/sdb rm 1
Information: You may need to update /etc/fstab.[root@centos7 ~]# 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

文件系统持久化挂载

持久化挂载

当服务器重启时,系统不会再次将文件系统自动挂载到目录树上,用户无法访问。为了确保系统在启动时自动挂载文件系统, 需要在 /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@server ~ 13:40:48]# vim /etc/fstab
[root@server ~ 13:42:22]# cat /etc/fstab#
# /etc/fstab
# Created by anaconda on Tue Sep  2 10:18:30 2025
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=310a2880-db73-4906-89de-808ff9b7bac2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sr0      /usr/share/nginx/html/dvd       iso9660  defaults        0 0#全部取消挂载
[root@server ~ 13:42:27]# umount -a  
umount: /:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /sys/fs/cgroup/systemd:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /sys/fs/cgroup:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /run:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /dev:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)[root@server ~ 13:42:45]# df 
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                  485776       0   485776    0% /dev
tmpfs                     497816    7944   489872    2% /run
tmpfs                     497816       0   497816    0% /sys/fs/cgroup
/dev/mapper/centos-root 52403200 6351992 46051208   13% /
#自动连接挂载
[root@server ~ 13:42:50]# mount -a
mount: /dev/sr0 写保护,将以只读方式挂载
[root@server ~ 13:42:56]# df 
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                  485776       0   485776    0% /dev
tmpfs                     497816    7944   489872    2% /run
tmpfs                     497816       0   497816    0% /sys/fs/cgroup
/dev/mapper/centos-root 52403200 6352012 46051188   13% /
/dev/sda1                1038336  142212   896124   14% /boot
/dev/mapper/centos-home 49250820   33016 49217804    1% /home
/dev/sr0                 4635056 4635056        0  100% /usr/share/nginx/html/dvd

实列:(设置持久化挂载是只读)

[root@server ~ 13:43:00]# blkid |grep sdb
/dev/sdb1: UUID="fffb0b6f-4956-4b45-ab9a-2f55e99c7871" TYPE="xfs" 
[root@server ~ 13:47:08]# blkid |grep sdc
/dev/sdc1: UUID="77163192-34be-4616-bc6b-3c64f995da8d" TYPE="ext4" PARTLABEL="webapp01" PARTUUID="da569c2b-c976-4bfe-8634-0a17d6ed9412" 
[root@server ~ 13:47:12]# mount UUID="77163192-34be-4616-bc6b-3c64f995da8d" /webapp1#直接命令挂载默认是defaults模式
[root@server ~ 13:51:05]# vim /etc/fstab
[root@server ~ 13:55:07]# cat /etc/fstab#
# /etc/fstab
# Created by anaconda on Tue Sep  2 10:18:30 2025
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=310a2880-db73-4906-89de-808ff9b7bac2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sr0      /usr/share/nginx/html/dvd       iso9660  defaults        0 0
/dev/sdc1     /webapp1                         ext4    ro  0 0[root@server ~ 13:52:23]# cp /etc/hostname /webapp1
[root@server ~ 13:52:46]# ls /webapp1
hostname  lost+found
#是可以写入的
[root@server ~ 13:53:04]# umount /webapp1
#取消了defaults类型的挂载#这里的重新连接挂载点,因为被载入了配置文件,所以直接挂载当指定位置(ro)
[root@server ~ 13:53:35]# mount /webapp1
[root@server ~ 13:53:44]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part ├─centos-root 253:0    0   50G  0 lvm  /├─centos-swap 253:1    0    2G  0 lvm  [SWAP]└─centos-home 253:2    0   47G  0 lvm  /home
sdb               8:16   0   20G  0 disk 
└─sdb1            8:17   0   10G  0 part 
sdc               8:32   0  200G  0 disk 
└─sdc1            8:33   0   20G  0 part /webapp1
sr0              11:0    1  4.4G  0 rom  /usr/share/nginx/html/dvd
[root@server ~ 13:53:48]# cp /etc/fstab /webapp1
cp: 无法创建普通文件"/webapp1/fstab": 只读文件系统#用命令短暂的修改挂载类型 -o   因为已经挂载所以直接remount 重新挂载
[root@server ~ 13:54:10]# mount -o rw,remount /webapp1[root@server ~ 13:54:57]# cp /etc/fstab /webapp1
[root@server ~ 13:55:00]# ls /webapp1
fstab  hostname  lost+found

取消持久化挂载

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

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

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

准备环境

[root@server ~ 14:15:11]# vim /etc/fstab 
#将自己添加的注释
[root@server ~ 14:15:23]# umount -a
umount: /:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /sys/fs/cgroup/systemd:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /sys/fs/cgroup:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /run:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
umount: /dev:目标忙。(有些情况下通过 lsof(8) 或 fuser(1) 可以找到有关使用该设备的进程的有用信息)
#清除磁盘分区         
[root@server ~ 14:15:28]# wipefs -a /dev/sdb
/dev/sdb:2 个字节已擦除,位置偏移为 0x000001fe (dos):55 aa
/dev/sdb: calling ioclt to re-read partition table: 成功
#使用交互式设置
[root@server ~ 14:15:47]# parted /dev/sdb mklabel gpt
信息: You may need to update /etc/fstab.[root@server ~ 14:17:12]# parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
信息: You may need to update /etc/fstab.[root@server ~ 14:36:37]# 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 
#查看文件系统类型
[root@server ~ 14:36:47]# blkid /dev/sdb1
/dev/sdb1: UUID="fffb0b6f-4956-4b45-ab9a-2f55e99c7871" TYPE="xfs" PARTLABEL="data01" PARTUUID="53039bc1-2755-43cb-9e72-ac44cb67f487" 
[root@server ~ 14:37:14]# mkdir /myapp-1
[root@server ~ 14:38:23]# mount /dev/sdb1 /myapp-1
mount: /dev/sdb1:不能读超级块
#设置时格式化没有成功
[root@server ~ 14:38:42]# 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@server ~ 14:39:14]# mkfs.xfs -f /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[root@server ~ 14:39:39]# mount /dev/sdb1 /myapp-1
[root@server ~ 14:39:45]# df -h /myapp-1
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb1       2.0G   33M  2.0G    2% /myapp-1
[root@server ~ 14:40:00]# mkdir /myapp-1/etc#创建文件[root@server ~ 14:40:51]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
记录了2000+0 的读入
记录了2000+0 的写出
2097152000字节(2.1 GB)已复制,3.52224 秒,595 MB/秒
[root@server ~ 14:41:54]# df -h /dev/sdb1
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb1       2.0G  2.0G  5.8M  100% /myapp-1[root@server ~ 14:42:51]# dd if=/dev/zero of=/myapp-1/etc/bigfile1 bs=1M count=6
dd: 写入"/myapp-1/etc/bigfile1" 出错: 设备上没有空间
记录了6+0 的读入
记录了5+0 的写出  #内存不够不能完全写入
5308416字节(5.3 MB)已复制,0.00308036 秒,1.7 GB/秒
[root@server ~ 14:43:02]# df -h /dev/sdb1
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb1       2.0G  2.0G  736K  100% /myapp-1

原因:大文件占用大量空间。

解决方法:找到文件后删除。

#寻找大文件
#方法1:
[root@server ~ 14:43:09]# du -s /myapp-1/*
2053184	/myapp-1/etc
[root@server ~ 14:43:47]# du -s /myapp-1/etc/*
2048000	/myapp-1/etc/bigfile
5184	/myapp-1/etc/bigfile1
[root@server ~ 14:43:56]# du -s /myapp-1/etc/bigfile/*
du: 无法访问"/myapp-1/etc/bigfile/*": 不是目录#方法二:
[root@server ~ 14:44:07]# find /myapp-1/ -size +10M
/myapp-1/etc/bigfile
[root@server ~ 14:44:26]# rm -f /myapp-1/etc/bigfile

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

准备环境

[root@server ~ 14:44:50]# df -h /dev/sdb1
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb1       2.0G   38M  2.0G    2% /myapp-1
[root@server ~ 14:45:07]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
记录了2000+0 的读入
记录了2000+0 的写出
2097152000字节(2.1 GB)已复制,0.842486 秒,2.5 GB/秒
[root@server ~ 14:45:15]# df -h /dev/sdb1
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb1       2.0G  2.0G  736K  100% /myapp-1
[root@server ~ 14:45:18]# tail -f /myapp-1/etc/bigfile &
[1] 2318
#放到后台运行
[root@server ~ 14:45:46]# df -h /dev/sdb
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        475M     0  475M    0% /dev
[root@server ~ 14:46:58]# df -h /dev/sdb1
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb1       2.0G  2.0G  736K  100% /myapp-1
[root@server ~ 14:47:03]# rm -rf /myapp-1/etc/bigfile
#删除正在运行的文件
[root@server ~ 14:47:27]# ls /myapp-1/etc/
bigfile1
#已经无法找到
#但是删除显示内存依然没有减少
[root@server ~ 14:47:38]# df -h /dev/sdb1
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb1       2.0G  2.0G  736K  100% /myapp-1

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

解决方法:找到像一个的程序并,终止程序。

#查看已经删除的但是还在运行的文件
[root@server ~ 14:48:43]# lsof | grep delete | grep /myapp-1
tail      2318         root    3r      REG               8,17 2097152000         68 /myapp-1/etc/bigfile (deleted)
[root@server ~ 14:48:52]# kill 2318
[root@server ~ 14:49:07]# df -h /dev/sdb1
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb1       2.0G   38M  2.0G    2% /myapp-1
[1]+  已终止               tail -f /myapp-1/etc/bigfile

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

> 文件 或者 if= /dev/zero of=文件 清空

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

准备环境

[root@server ~ 14:49:23]# parted /dev/sdb unit MiB mkpart data02 ext4 2049 3073
信息: You may need to update /etc/fstab.[root@server ~ 14:50:55]# 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 /myapp-1
└─sdb2   8:18   0   1G  0 part 
[root@server ~ 14:51:20]# mkfs.ext4 /dev/sdb2
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 32768, 98304, 163840, 229376Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成[root@server ~ 14:53:14]# blkid /dev/sdb2
/dev/sdb2: UUID="acb0f2a7-bd8e-495f-9c38-670fcbfdd1da" TYPE="ext4" PARTLABEL="data02" PARTUUID="e2737337-fe0e-4287-9c95-dc345960a54e" 
[root@server ~ 14:53:47]# mkdir /myapp-2
[root@server ~ 14:54:07]# mount /dev/sdb2 /myapp-2
[root@server ~ 14:54:20]# df -h /myapp-2
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb2       976M  2.6M  907M    1% /myapp-2
[root@server ~ 14:54:33]# touch /myapp-2/file-{00001..70000}
....
touch: 无法创建"/myapp-2/file-69998": 设备上没有空间
touch: 无法创建"/myapp-2/file-69999": 设备上没有空间
touch: 无法创建"/myapp-2/file-70000": 设备上没有空间# df -i 可以查看文件系统能有多少个文件可以建
[root@server ~ 14:55:37]# df -i /dev/sdb2
文件系统       Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/sdb2      65536   65536       0     100% /myapp-2

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

解决方法:删除大量的小文件或者将这些小文件备份到其他地方。

#方法一:
[root@server ~ 15:04:31]# rm -rf /myapp-2/file*
[root@server ~ 15:07:12]# df -i /dev/sdb2
文件系统       Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/sdb2      65536      11   65525       1% /myapp-2#方法二:(将所有的小文件合并)
[root@server ~ 15:07:42]# cat /myapp-2/file-{00001..10000} > file-00001
http://www.dtcms.com/a/395787.html

相关文章:

  • 2.UE-准备环境(二)-下载虚幻引擎源码和搞成vs项目并使用vs打开
  • G-Star公益行获评CCF优秀技术公益案例,用开源技术传递善意
  • 文化赋能・创意西宁 西宁传媒行业创业发展沙龙成功举办 探索本土企业升级新路径
  • TDengine 与 MYSQL 的差异总结
  • Mysql杂志(二十一)——Hash索引和二叉搜索树、AVL树
  • 什么是类的实例化
  • 西门子 S7-200 SMART PLC 实操案例:中断程序的灵活应用定时中断实现模拟量滤波(下)
  • STM32FreeRtos入门(二)——创建第一个多任务程序
  • Qt QML and Qt Quick 简述及例程
  • Linux 系统移植
  • 小杰机器学习(six)——概率论——1.均匀分布2.正态分布3.数学期望4.方差5.标准差6.多维随机变量及其分布
  • 【Linux】Ext系列文件系统(上)
  • 【ROS2】Beginner: Client libraries - 发布者、订阅者例子 C++ Python
  • AI设计功能性病毒:从DNA语言模型到精准杀菌实战
  • Qt 共享指针QSharedPointer与std::shared_ptr
  • Java课程 第02周 预习、实验与作业:Java基础语法2:面向对象入门
  • 词性标注技术漫谈:为词语赋予语法灵魂的旅程
  • K230基础-MicroPython
  • 网站访问问题:无法访问此网站、404
  • Redis 与Memcached 的对比
  • PyTorch 神经网络工具箱:核心原理与实践指南
  • 广义矩估计错误指定时的一个推导【续5】
  • 【STM32】ADC数模转换器
  • Tensorboard学习记录
  • Redis中常见数据结构底层实现结构是什么
  • 高频交易技术演进:从毫秒到纳秒的极限延迟优化之路
  • 从零开始搭建并部署一个基于Django和YOLO的智能模型项目
  • MySQL零基础学习Day2——数据库基础操作
  • 数学笔试选择题:题组1
  • Linux常用命令51——tail查看文件尾部内容