目 录CONTENT

文章目录

磁盘和文件系统管理

简中仙
2020-01-13 / 0 评论 / 0 点赞 / 91 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
本文最后更新于2024-02-02,若内容或图片失效,请留言反馈。 本文如有错误或者侵权的地方,欢迎您批评指正!

一、管理磁盘及分区

在Linux服务器中,当现有硬盘的分区规划不能满足要求(例如,根分区的剩余空间过少无法继续安装新的系统程序)时,就需要对硬盘中的分区进行重新规划和调整,有时候还需要添加新的硬盘设备来扩展存储空间。

传统的硬盘分区都是MBR格式,MBR分区位于0扇区,一共512字节,前446字节是grub引导程序,中间64字节是分区表,每个分区需要16个字节表示,因此主分区和扩展分区一共只能有4个分区,超过4个的分区只能从扩展分区上再设置逻辑分区来表示。每个分区的大小无法超过2T。 MBR的最后2个字节是结束符号。

GPT格式,打破了MBR的限制,可以设置多达128个分区,分区的大小根据操作系统的不同有所变化,但是都突破了2T空间的限制。支持高达 18EB(1EB=1024PB,1PB=1024TB) 的卷大小,允许将主磁盘分区表和备份磁盘分区表用于冗余,还支持唯一的磁盘和分区 ID (GUID)。与 MBR 分区的磁盘不同,GPT的分区信息是在分区中,而不象MBR一样在主引导扇区。为保护GPT不受MBR类磁盘管理软件的危害,GPT在主引导扇区建立了一个保护分区 (Protective MBR)的MBR分区表,这种分区的类型标识为0xEE,这个保护分区的大小在Windows下为128MB,Mac OS X下为200MB,在Window磁盘管理器里名为GPT保护分区,可让MBR类磁盘管理软件把GPT看成一个未知格式的分区,而不是错误地当成一个未分区的磁盘

在MBR硬盘中,分区信息直接存储于主引导记录(MBR)中(主引导记录中还存储着系统的引导程序)。但在GPT硬盘中,分区表的位置信息储存在GPT头中。但出于兼容性考虑,硬盘的第一个扇区仍然用作MBR,之后才是GPT头。

二、常用文件系统

1、ext3

Ext3是一种日志文件系统,是Linux系统中的一种文件系统。它是基于Ext2文件系统发展而来,继承了Ext2的许多优点,并在其基础上增加了日志功能,从而提高了文件系统的可靠性和数据恢复能力。

Ext3文件系统的日志功能将整个磁盘的写入操作记录在磁盘的某个区域上,以便在需要时可以回溯和追踪。这种日志功能使得在系统突然崩溃或意外断电的情况下,文件系统能够快速恢复到一致状态,避免了数据丢失或损坏的问题。

Ext3文件系统兼容Ext2文件系统,可以无损地从Ext2升级到Ext3,而无需进行数据备份和恢复操作。此外,Ext3文件系统还支持更大的文件和分区大小,最高可达16TB,并且具有良好的可扩展性和性能表现。

2、ext4

Ext4是一种日志文件系统,是Linux系统下的第四代扩展文件系统。它是Ext3文件系统的后继版本,由Ext3的维护者Theodore Tso领导的开发团队实现,并从Linux 2.6.19内核开始被引入。

Ext4的产生原因是开发人员在Ext3中加入了新的高级功能,但在实现过程中出现了几个重要问题,包括新功能违背向后兼容性、新功能使Ext3代码变得更加复杂并难以维护以及新加入的更改使原来十分可靠的Ext3变得不可靠。因此,开发人员决定把Ext4从Ext3中分离出来进行独立开发。

与Ext3相比,Ext4进行了许多改进和扩展。首先,它支持更大的文件系统和文件,最大可支持1EB的文件系统和16TB的文件。其次,它支持无限数量的子目录,而Ext3目前只支持32,000个子目录。此外,Ext4引入了现代文件系统中流行的extents概念,提高了大文件的操作效率。此外,Ext4还支持多块分配和延迟分配等特性。

在磁盘布局方面,Ext4文件系统将磁盘划分为分区,每个分区又划分为N个块组,在每个块组中根据块组的编号有不同的内部结构。Ext4文件系统主要使用块组0中的超级块和块组描述符表,在其他一些特定的块组中有超级块和块组描述符表的冗余备份。当格式化磁盘成为ext4文件系统时,mkfs将在块组描述符表后面分配预留GDT表数据块以用来将来扩展文件系统。紧接在预留GDT表数据块后的是数据块位图与inode表位图,这两个位图分别表示本块组内的数据块与inode表的使用。inode表数据块之后就是存储文件的数据块了。

3、xfs

XFS是一种高性能的日志文件系统,最早由Silicon Graphics为他们的IRIX操作系统而开发,是IRIX 5.3版的默认文件系统。2000年5月,Silicon Graphics以GNU通用公共许可证发布这套系统的源代码,之后被移植到Linux 内核上。

XFS具有以下特点:

  1. 数据完全性:XFS文件系统开启了日志功能,可以快速恢复磁盘文件内容。
  2. 传输特性:XFS文件系统采用优化算法,日志记录对整体文件操作影响非常小。
  3. 可扩展性:XFS是一个全64-bit的文件系统,可以支持上百万T字节的存储空间。对特大文件及小尺寸文件的支持都表现出众,支持特大数量的目录。
  4. 高效性:XFS使用高效的表结构(B+树),保证了文件系统可以快速搜索与快速空间分配。

XFS在很多方面确实做的比ext4好,例如可扩展性和scalability、各种锁的细化等。不过,ext4受限制于磁盘结构和兼容问题,可扩展性和scalability确实不如xfs。

4、zfs

ZFS是一种先进的文件系统,具有许多强大的特性。它是第一个128位文件系统,总容量是现有64位文件系统的1.84x10^19倍,单个存储卷容量达到16EiB(2^64byte),一个zpool存储池可以拥有2^64个卷,总容量最大256ZiB(2^78byte)。在相当长的未来时间内,ZFS几乎不太可能出现存储空间不足的问题。

ZFS最初由Sun Microsystems开发,现在在开源社区的驱动下持续发展,被广泛应用在各种环境中,从小型家用NAS到大型商用存储服务器。它提供了许多强大的特性,包括保护数据完整性、存储池、快照、克隆、数据压缩和去重、以及RAID-Z等。其中,保护数据完整性是ZFS的核心特性,通过校验和修复机制,ZFS可以防止数据在传输和存储过程中的任何损坏。而存储池和RAID-Z则为用户提供了强大而灵活的存储管理能力。

ZFS是一种分层存储系统,其行为类似于传统的Unix风格的文件系统。它支持所有标准文件和目录操作,如创建、删除和修改文件和目录。此外,ZFS还支持快照、克隆和回滚,使其适用于管理版本化文件存储和备份。

5、NFS

NFS(Network File System)是一种分布式文件系统,可以让不同计算机系统之间共享和访问文件。它允许客户端像访问本地文件一样访问远程文件,简化了多台计算机之间的文件共享和协作。

NFS的设计目标包括提供跨平台文件共享、简化应用程序对远程文件的访问、一次服务请求恢复系统等。其通过将文件系统挂载到远程服务器上,实现对文件和目录的访问和共享。当客户端请求访问某个文件或目录时,NFS将该请求传递给服务器,服务器返回所需要的数据或信息。

NFS的优点包括简单易用、高性能、灵活性、跨平台支持等。它适用于各种网络环境,如企业内部网络、云计算环境、大规模科学计算集群等。然而,NFS也存在一些缺点,如网络开销、安全问题、依赖性和单一故障点等。

6、SWAP

三、分区工具

1、fDisk

fdisk是一个基于命令行的传统分区工具,支持MBR(Master Boot Record)和GPT(GUID Partition Table)两种分区格式。使用fdisk可以创建、删除、修改磁盘分区等操作。fdisk的优点是简单易用,适合初学者使用。

fdisk只适合于用容量较小的硬盘,容量太大的硬盘就无法使用 fdisk 进行处理。 fdisk所能处理的的磁盘容量上限是2TB,若磁盘的容量大于 2TB就无法使用 fdisk。

2、gDisk

gdisk是一个专门针对GPT分区格式的命令行工具,功能比fdisk更强大。使用gdisk可以创建、删除、修改GPT分区表及其分区信息,并可以设置分区类型、属性等。gdisk的优点是功能强大,适合需要更高级功能的用户使用。

四、fDisk管理磁盘

1、安装相关软件包

yum install -y lvm2

2、检测并确认新硬盘

# fdisk -l
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0009b7f3

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *       2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 83886079 81786880  39G 8e Linux LVM

Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 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 /dev/mapper/centos-root: 35.1 GiB, 37706792960 bytes, 73646080 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 /dev/mapper/centos-swap: 3.9 GiB, 4160749568 bytes, 8126464 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

如果是在虚拟机中添加磁盘,在不关机的情况下可以这样识别

# echo "- - -" >>/sys/class/scsi_host/host0/scan
# echo "- - -" >>/sys/class/scsi_host/host1/scan
# echo "- - -" >>/sys/class/scsi_host/host2/scan

2、规划硬盘中的分区

进入交互式的分区管理界面

# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.29).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x4847abc2.

1、列出硬盘中的分区情况

Command (m for help): p
Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x4847abc2

2、新建分区

Primary分区:主分区是直接创建在硬盘上的分区,也称为基本分区。每个硬盘最多可以有四个主分区,因此一个硬盘最多可以有四个主分区和一个扩展分区。主分区是直接包含文件系统和数据的分区,可以直接访问和使用

Extended分区:扩展分区是一种特殊的分区类型,它本身不包含文件系统和数据,而是作为其他逻辑分区的容器。扩展分区内部可以包含多个逻辑分区,这些逻辑分区通过扩展分区的边界来定义和创建。扩展分区本身并不直接参与文件系统的构建和数据的存储,而是提供了一种方式来创建和管理多个逻辑分区。

选择分区号时主分区和扩展分区的序号只能为1-4,为保证分区连续,分区起始位置一般由fdisk默认识别即可,结束位置或大小可以使用+sizeM+sizeG的形式如+20G 表示将该分区的容量设置为20GB。

Command (m for help): n									# 创建第1个分区
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):										# 选择创建的为主分区,默认为主分区

Using default response p.
Partition number (1-4, default 1):						# 设置第一个主分区的编号为1
First sector (2048-83886079, default 2048):				# 直接按En七er键接受默认值
Last sector, +sectors or +size{K,M,G,T,P} (2048-83886079, default 83886079): +20G

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

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (2-4, default 2):
First sector (41945088-83886079, default 41945088):
Last sector, +sectors or +size{K,M,G,T,P} (41945088-83886079, default 83886079): +2G

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

Command (m for help): p
Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7b3a87

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdb1           2048 41945087 41943040  20G 83 Linux
/dev/sdb2       41945088 46139391  4194304   2G 83 Linux

创建一个扩展分区和两个逻辑分区

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (3,4, default 3): 4
First sector (46139392-83886079, default 46139392):
Last sector, +sectors or +size{K,M,G,T,P} (46139392-83886079, default 83886079):

Created a new partition 4 of type 'Extended' and of size 18 GiB.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (46141440-83886079, default 46141440):
Last sector, +sectors or +size{K,M,G,T,P} (46141440-83886079, default 83886079): +2G

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

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (50337792-83886079, default 50337792):
Last sector, +sectors or +size{K,M,G,T,P} (50337792-83886079, default 83886079): +10G

Created a new partition 6 of type 'Linux' and of size 10 GiB.

查看分区

Command (m for help): p
Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7b3a87

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdb1           2048 41945087 41943040  20G 83 Linux
/dev/sdb2       41945088 46139391  4194304   2G 83 Linux
/dev/sdb4       46139392 83886079 37746688  18G  5 Extended
/dev/sdb5       46141440 50335743  4194304   2G 83 Linux
/dev/sdb6       50337792 71309311 20971520  10G 83 Linux

3、保存和退出

"w"指令将保存分区操作,而"q"指令不会保存对硬盘所做的分区操作

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

重新探测/dev/sdb磁盘中的分区变化

# partprobe /dev/sdb

3、删除分区

Command (m for help): d
Partition number (1,2,5, default 5): 2

Partition 2 has been deleted.

4、变更分区的类型

XFS、Swap文件系统对应的ID号分别为83、82,而用于Windows中的NTFS文件系统对应的ID号一般为86

将逻辑分区"dev/sdb5"的类型更改为Swap

Command (m for help): p
Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7b3a87

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdb1           2048 41945087 41943040  20G 83 Linux
/dev/sdb2       41945088 46139391  4194304   2G 83 Linux
/dev/sdb4       46139392 83886079 37746688  18G  5 Extended
/dev/sdb5       46141440 50335743  4194304   2G 83 Linux
/dev/sdb6       50337792 71309311 20971520  10G 83 Linux

Command (m for help): t
Partition number (1,2,4-6, default 6): 5
Partition type (type L to list all types): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p
Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7b3a87

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdb1           2048 41945087 41943040  20G 83 Linux
/dev/sdb2       41945088 46139391  4194304   2G 83 Linux
/dev/sdb4       46139392 83886079 37746688  18G  5 Extended
/dev/sdb5       46141440 50335743  4194304   2G 82 Linux swap / Solaris
/dev/sdb6       50337792 71309311 20971520  10G 83 Linux

四、gDisk管理磁盘

gdisk命令的使用方法和fdisk相似,区别为gdisk命令可以对GPT格式磁盘进行操作,fdisk不可以。

# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

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): n									# 创建新分区
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'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p									# 查看分区信息
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): C4061B44-7C79-4569-BEB7-216C9F1C2C36
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

Command (? for help): t       							# 改变磁盘格式为LVM
Using 1
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to 'Linux LVM'

Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): C4061B44-7C79-4569-BEB7-216C9F1C2C36
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     8E00  Linux LVM
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/sdb.
The operation has completed successfully.

五、管理文件系统

1、创建文件系统

1、mkfs命令的使用

创建XFS文件系统

# mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242880, 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

或者

# mkfs.xfs /dev/sdb2
meta-data=/dev/sdb2              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

执行格式化时也可以指定额外的一些参数,可以高文件系统的相关性能

# mkfs.xfs -d agcount=256 -l size=128m,lazy-count=1,version=2 /dev/sdb3
参数说明
-d agcount=256设置XFS的块组(allocation group)数量为256,这样可以并行地执行I/O操作,提高性能
-l设置逻辑卷的属性
size=128m设置逻辑卷的大小为128MB
lazy-count=1启用延迟计数。这可以减少I/O操作,提高性能。
version=2设置逻辑卷的版本为2

2、mkswap命令的使用

将分区/dev/sdb5创建为交换分区(目标分区应先通过fdisk工具将ID号设为82)

# fdisk -l /dev/sdb
Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7b3a87

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdb1           2048 41945087 41943040  20G 83 Linux
/dev/sdb2       41945088 46139391  4194304   2G 83 Linux
/dev/sdb4       46139392 83886079 37746688  18G  5 Extended
/dev/sdb5       46141440 50335743  4194304   2G 82 Linux swap / Solaris
/dev/sdb6       50337792 71309311 20971520  10G 83 Linux
# mkswap /dev/sdb5
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=558bf67c-182f-46ce-ab12-0a35c04cccb2

启用、停用交换分区/dev/sdb5

# cat /proc/meminfo | grep "SwapTotal:"
SwapTotal:             0 kB
# swapon /dev/sdb5
# cat /proc/meminfo | grep "SwapTotal:"
SwapTotal:       2097148 kB

2、挂载、卸载文件系统

1、挂载文件系统

将光盘设备挂载到/media/cdrom目录

# mount /dev/cdrom /media/
mount: /dev/sr0 is write-protected, mounting read-only

将建立的/dev/sdb1分区挂载到新建的/mailbox目录下

# mkdir /mailbox
# mount /dev/sdb1 /mailbox/

2、卸载文件系统

通过挂载点目录卸载对应的分区

# umount /mailbox/

通过设备文件卸载光盘

# umount /dev/cdrom

3、设置文件系统的自动挂载

查看当前系统内自动挂载的设备或分区

# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Jul  2 13:10:48 2023
#
# 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=a7e89d62-22cd-417a-ab72-9e2cbbdee0d7 /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0
  • 第1字段:设备名或设备卷标名。
  • 第2字段:文件系统的挂载点目录的位置。
  • 第3字段:文件系统类型,如XFS、Swap等。
  • 第4字段:挂载参数,即mount命令"-o"选项后可使用的参数。例如,defaults, rw、ro、noexec 分别表示默认参数、可写、只读、禁用执行程序。
  • 第5字段:表示文件系统是否需要dump备份。一般设为1时表示 需要,设为0时将被dump忽略。
  • 第6字段:该数字决定在系统启动时进行磁盘检查的顺序。0表示不进行检查,1表示优先检查,2表示其次检查。根分区应设为1 ,其他分区设为2。

添加自动挂载分区/dev/sdb1

# cat /etc/fstab
(略)
/dev/sdb1               /mailbox                xfs     defaults        0  0           

4、查看磁盘使用情况

查看当前系统中挂载的各文件系统的磁盘使用情况

# df -Th
文件系统                类型      容量  已用  可用 已用% 挂载点
devtmpfs                devtmpfs  894M     0  894M    0% /dev
tmpfs                   tmpfs     910M     0  910M    0% /dev/shm
tmpfs                   tmpfs     910M   11M  900M    2% /run
tmpfs                   tmpfs     910M     0  910M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  5.1G   12G   30% /
/dev/sda1               xfs      1014M  232M  783M   23% /boot
tmpfs                   tmpfs     182M  4.0K  182M    1% /run/user/42
tmpfs                   tmpfs     182M   40K  182M    1% /run/user/0
/dev/sr0                iso9660    11G   11G     0  100% /media
/dev/sdb1               xfs       2.0G   33M  2.0G    2% /mailbox

5、mount 挂载参数及用法

mount挂载时常用的参数有

参数说明
rw以读写模式挂载文件系统
atime更新文件的访问时间
noatime在读取文件时不会更新文件的access time属性,可能会影响需要访问时间的应用程序(如mutt)的正常运行
nodiratime在读取文件时不会更新目录的access time属性,的是减少对文件系统的访问次数,提高性能
suid当在文件系统内执行程序时,使用文件系统所属用户ID(SUID)权限
dev将字符设备和块设备当作目录来处理
exec允许在文件系统内执行程序
async涉及文件系统I/O的操作都是异步处理,即不会同步写入磁盘,此参数会提高性能,但会降低数据安全性
sync所有写入操作都会在写入数据到磁盘之前立即返回,好处是数据的一致性得到了保证,但缺点是它可能会降低性能
auto系统自动挂载,fstab默认就是这个选项
nouser开机不自动挂载
delalloc删除未分配的文件系统空间,将多个写操作请求集中到一起来进行
nodelalloc每个写操作后都会立即写入数据到磁盘
data=ordered强制在执行写操作时先执行读操作,再执行写操作,以确保数据的顺序一致性
data=journal允许文件系统使用日志(journaling)来提高性能和数据完整性
data=writeback数据会立即写入缓存中,但不会立即写入磁盘。这样可以提高性能,但可能会降低数据的安全性。
barrier文件系统会强制日志先于数据提交到磁盘
nobarrier强制所有写操作按顺序执行,但不会强制更新数据到磁盘
nodiscard禁用TRIM命令,以防止固态硬盘过度磨损

defaults默认包括rwatimesuiddevexecasyncautonouserdelallocdata=orderedbarriernodiscard,不再需要指定其他挂载参数。

如果您非常重视文件系统的数据安全,建议您使用rwatimesyncbarrierdata=journal参数挂载文件系统

mount -o rw,atime,sync,barrier,data=journal /dev/vdb /media/test
# cat /etc/fstab
/dev/vdb 	/media/test 	ext4 	rw,atime,sync,barrier,data=journal 	0 	0

如果您非常重视性能,建议您使用defaultsnoatimenodiratimenobarriernodelallocdata=writeback参数挂载文件系统,一般用于数据库服务器,数据库建议使用xfs文件系统,千万别用ext3;ext4勉强可用,但业务量很大的话,则一定要用xfs;

mount -t xfs -o defaults,noatime,nodiratime,nobarrier,nodelalloc,data=writeback,allocsize=256m,logbufs=8,attr2,logbsize=256k /dev/sdX /mnt/xfs
# cat /etc/fstab
/dev/sdX 	/mnt/xfs 	xfs 	defaults,noatime,nodiratime,nobarrier,nodelalloc,data=writeback,allocsize=256M,logbufs=8,attr2,logbsize=256k 	0 	0

其中"/dev/sdX"是要挂载的设备名,"mnt/xfs"是挂载点。

参数说明
allocsize=256M指定文件系统分配的初始大小
logbufs=8设置日志缓冲区的数量。日志缓冲区用于存储即将写入日志文件的日志条目
attr2启用xfs的第二版所有扩展属性(xfs文件系统独有参数)
logbsize=256k设置日志文件的块大小

6、裸磁盘分区扩容

1、停掉向挂载路径写文件的服务或进程

2、卸载挂载

umount /data

如果提示umount:/data:target is bus,使用fuser找出正在往挂载路径写文件的进程并kill掉,再次卸载挂载

# yum install psmisc -y
# fuser -mv /data
                     USER        PID ACCESS COMMAND
/data:               root     kernel mount /
                     root          1 .rce. systemd
                     root          2 .rc.. kthreadd

3、修复分区表

磁盘扩大容量后,分区表中记录的柱头等信息需要更新,否则创建新分区时会报GPT PMBR size mismatch

parted -l				# 在弹出Fix/Ignore?的提示时输入Fix后回车即可。

4、删掉旧分区再重建新分区

fdisk /dev/sdb
	d			# 删除原来的分区/dev/sdb1
	n			#	创建新的分区		
	1			# 分区号与旧的保持一致
	w			# 写入分区表并生效

5、调整分区

e2fsck -f /dev/sdb1 检查分区信息
resize2fs /dev/sdb1 调整分区大小

6、重新挂载并验证数据是否丢失,容量是否扩容

六、设置磁盘配额

1、查看系统是否支持磁盘配额功能

# grep -i quota /boot/config-3.10.0-1160.105.1.el7.x86_64
CONFIG_NETFILTER_XT_MATCH_QUOTA=m
CONFIG_XFS_QUOTA=y
CONFIG_QUOTA=y												# 这里是内核支持quota功能
CONFIG_QUOTA_NETLINK_INTERFACE=y
CONFIG_PRINT_QUOTA_WARNING=y
# CONFIG_QUOTA_DEBUG is not set
CONFIG_QUOTA_TREE=y
CONFIG_QUOTACTL=y
CONFIG_QUOTACTL_COMPAT=y

2、磁盘配额管理

1、以支持配额功能的方式挂载文件系统

创建两个分区

# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.29).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-83886079, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-83886079, default 83886079): +10G

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

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (2-4, default 2):
First sector (20973568-83886079, default 20973568):
Last sector, +sectors or +size{K,M,G,T,P} (20973568-83886079, default 83886079): +10G

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

Command (m for help): p
Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7b3a87

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdb1           2048 20973567 20971520  10G 83 Linux
/dev/sdb2       20973568 41945087 20971520  10G 83 Linux

Command (m for help): w

分别进行格式化

# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
# mkfs.xfs /dev/sdb2
meta-data=/dev/sdb2              isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621440, 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

挂载目录

usrquota和grpquota是用于限制用户和用户组磁盘使用空间的参数。它们通常用于文件系统挂载时,以启用用户和组配额功能。usrquota代表用户配额,用于限制特定用户的磁盘使用空间。grpquota代表组配额,用于限制特定用户组的磁盘使用空间。通过使用这些参数,可以有效地管理磁盘空间,确保用户和用户组不会超出分配给他们的磁盘容量限制。

# mkdir /quota{1,2}
# mount -o usrquota,grpquota /dev/sdb1 /quota1/        # 临时挂载
# mount -o usrquota,grpquota /dev/sdb2 /quota2/        # 临时挂载
# vim /etc/fstab        							   # 开机自动挂载
/dev/sdb1              /quota1             ext4    defaults,usrquota,grpquota        0 0 
/dev/sdb2              /quota2             xfs     defaults,usrquota,grpquota        0 0 
# df -hT  /dev/sdb1
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sdb1      ext4  9.8G   37M  9.2G   1% /quota1
# df -hT  /dev/sdb2
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sdb2      xfs    10G   33M   10G   1% /quota2

需要注意的是,XFS 文件系统只有在首次挂载时才启动磁盘限额功能,所以不能使用“-o remount”(重新挂载文件系统)挂载选项。

2、创建配额记录文件

注意:quota命令适用于ext2/ext3/ext4文件系统类型的分区,对于xfs文件系统类型的分区,该操作命令并不适用

1、ext4文件系统

# yum -y install quota
# 检查和修复文件系统的配额状态
# quotacheck -cugv /quota1/
quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.
quotacheck: Scanning /dev/sdb1 [/quota1] done
quotacheck: Cannot stat old user quota file /quota1/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /quota1/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old user quota file /quota1/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /quota1/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Checked 3 directories and 0 files
quotacheck: Old file not found.
quotacheck: Old file not found.
# ls /quota1/
aquota.group  aquota.user  lost+found
参数说明
-c--create 创建磁盘配额的配置文件
-u--user 针对用户配额的配置文件
-g--group 针对组配额的配置文件
-v--verbose 详细显示扫描过程中的信息
-a--all 检测系统中所有已经挂载的支持quota功能的分区

2、xfs文件系统

查看quota文件系统状态

# xfs_quota -x -c "print"
Filesystem          Pathname
/                   /dev/mapper/centos-root
/boot               /dev/sda1
/quota2             /dev/sdb2 (uquota, gquota)

3、编辑用户和组账号的配额设置

参数说明
bsoft设置磁盘容量的软限制数值
bhard设置磁盘容量的硬限制数值
isoft设置磁盘文件数的硬限制数值
ihard设置磁盘文件数的软限制数值
-x表示启动专家模式,在当前模式下允许对配额系统进行修改的所有管理命令可用
-c表示直接调用管理命令

设置用户zhangsan的磁盘配额:磁盘容量软限制80MB、磁盘容量硬限制100MB.文件数软限制40、文件数硬限制50

# xfs_quota -x -c 'limit -u bsoft=80M bhard=100M isoft=40 ihard=50 zhangsan' /quota2/

查看用户zhangsan的磁盘容量限制

# xfs_quota -c 'quota -uv zhangsan' /quota2/
Disk quotas for User zhangsan (1001)
Filesystem              Blocks      Quota      Limit  Warn/Time      Mounted on
/dev/sdb2                    0      81920     102400   00 [--------] /quota2

查看zhangsan用户的磁盘文件数限制

# xfs_quota -c 'quota -i -uv zhangsan' /quota2/
Disk quotas for User zhangsan (1001)
Filesystem               Files      Quota      Limit  Warn/Time      Mounted on
/dev/sdb2                    0         40         50   00 [--------] /quota2

设置组账号caiwubu的磁盘配额:磁盘容量软限制1GB、磁盘容量硬限制2GB、文件数软限制2000、文件数硬限制2500

# xfs_quota -x -c 'limit -g bsoft=1G bhard=2G isoft=2000 ihard=2500 caiwubu' /quota2/
# xfs_quota -c 'quota -gv caiwubu' /quota2/
Disk quotas for Group caiwubu (1001)
Filesystem              Blocks      Quota      Limit  Warn/Time      Mounted on
/dev/sdb2                    0    1048576    2097152   00 [--------] /quota2
# xfs_quota -c 'quota -i -gv caiwubu' /quota2/
Disk quotas for Group caiwubu (1001)
Filesystem               Files      Quota      Limit  Warn/Time      Mounted on
/dev/sdb2                    0       2000       2500   00 [--------] /quota2

4、查看配额使用情况

查看所有可用分区的磁盘容量配额使用情况

# xfs_quota -x -c 'report -a'
User quota on /quota2 (/dev/sdb2)
                               Blocks
User ID          Used       Soft       Hard    Warn/Grace
---------- --------------------------------------------------
root                0          0          0     00 [--------]
zhangsan            0      81920     102400     00 [--------]

Group quota on /quota2 (/dev/sdb2)
                               Blocks
Group ID         Used       Soft       Hard    Warn/Grace
---------- --------------------------------------------------
root                0          0          0     00 [--------]
caiwubu             0    1048576    2097152     00 [--------]

查看磁盘容量和文件输入的报告

# xfs_quota -x -c 'report -abi'
User quota on /quota2 (/dev/sdb2)
                               Blocks                                          Inodes
User ID          Used       Soft       Hard    Warn/Grace           Used       Soft       Hard    Warn/ Grace
---------- -------------------------------------------------- --------------------------------------------------
root                0          0          0     00 [--------]          3          0          0     00 [--------]
zhangsan            0      81920     102400     00 [--------]          0         40         50     00 [--------]

Group quota on /quota2 (/dev/sdb2)
                               Blocks                                          Inodes
Group ID         Used       Soft       Hard    Warn/Grace           Used       Soft       Hard    Warn/ Grace
---------- -------------------------------------------------- --------------------------------------------------
root                0          0          0     00 [--------]          3          0          0     00 [--------]
caiwubu             0    1048576    2097152     00 [--------]          0       2000       2500     00 [--------]
0

评论区