磁盘管理
写在开头:
磁盘有价,数据无价!
请读者在执行任何与磁盘相关操作之前,务必三思而后行。做好重要数据备份!
不论有无十足把握,请准备好回退方案。
一、认识文件系统和设备
(1)基础概念
- 存储介质:用于存储数据的物理设备。
- 磁盘分区:将存储设备逻辑上划分更小的区块,方便于管理。
- 创建文件系统:将分区格式化成不同的文件系统。
- 挂载:在Linux中要让文件系统内容可用,必须将它与一个目录进行关联,这个动作称为挂载,这个空目录称为挂载点。
在Linux中,无论有多少块硬盘,经过怎样的分区,最终呈现在我们面前都是这样一颗倒过来的目录树。
(2)磁盘命名
在Linux中一切皆文件,硬件设备也不例外,为了方便用户进行管理,系统内核中的udev设备管理器会自动对硬件设备进行命名。常见的硬件设备如下
设备类型 | 命名 |
---|---|
SATA/SAS/USB/SCSI盘 | /dev/sda,/dev/sdb…… |
virtio-blk超虚拟化存储 | /dev/vda,/dev/vdb…… |
NMVe盘 | /dev/nvme0,/dev/nvme1…… |
分区编号
- 主分区或扩展分区编号从1开始,到4结束
- 逻辑分区从编号5开始
eg:
/dev/sda1:表示第一块发现硬盘上的编号为1的主分区。
/dev/sdb5:表示第二块发现硬盘上的编号为5的逻辑分区。
/dev/nmve0n1p1:表示第一个控制器上第一个发现的设备上编号为1的主分区。
特别提醒:
- 设备命名的顺序,不是有插槽决定的,是根据内核识别的顺序来决定,sda设备之所以是a是因为这是内核发现的第一块设备。
- 分区编号表示仅仅是分区的编号,不表示分区的数量。
(3)查看文件系统
统计文件系统磁盘使用情况
df
命令可以显示主机上的文件系统和挂载点。
[root@rhce ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 4096 0 4096 0% /dev
tmpfs 1861116 0 1861116 0% /dev/shm
tmpfs 744448 9944 734504 2% /run
/dev/mapper/rhel-root 17811456 4837532 12973924 28% /
/dev/nvme0n1p1 1038336 298436 739900 29% /boot
tmpfs 372220 52 372168 1% /run/user/42
tmpfs 372220 92 372128 1% /run/user/0
根据df
命令的输出,可以发现在主机上有两个物理文件系统,挂载在/
和/boot
上。
tmpfs
和devtmpfs
设备时系统内存中的文件系统,在系统断电时,写入的数据都会删除。
为了方便用户阅读显示信息可以使用-h
选项。
[root@rhce ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs 727M 9.7M 718M 2% /run
/dev/mapper/rhel-root 17G 4.7G 13G 28% /
/dev/nvme0n1p1 1014M 292M 723M 29% /boot
tmpfs 364M 92K 364M 1% /run/user/0
du
命令可以查看指定目录下所有目录使用的空间状况,同样可以使用-h
选项。
[root@rhce cdrow]# du -h /cdrow/
6.9G /cdrow/AppStream/Packages
7.7M /cdrow/AppStream/repodata
7.0G /cdrow/AppStream
1.2G /cdrow/BaseOS/Packages
2.4M /cdrow/BaseOS/repodata
1.2G /cdrow/BaseOS
2.3M /cdrow/EFI/BOOT/fonts
6.5M /cdrow/EFI/BOOT
6.5M /cdrow/EFI
108M /cdrow/images/pxeboot
857M /cdrow/images
109M /cdrow/isolinux
9.1G /cdrow/
查看磁盘使用情况
lsblk
命令可以查看主机上当前已经识别的硬盘以及分区情况
[root@rhce ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom /run/media/root/RHEL-9-2-0-BaseOS-x86_64
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─rhel-root 253:0 0 17G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
NAME 表示设备名
MAJ:MIN 表示主要和次要设备号
RM 表示是否可移动设备,1表示可移动,0表示不可移动
SIAE 表示磁盘大小
RO 表示是否只读,0表示不是只读的
TYPE 表示设备类型,rom表示只读存储,disk表示是一个磁盘,part表示是一个分区
MOUNTPOINTS 表示挂载点
二、挂载和卸载文件系统
(1)手动挂载文件系统
mount
命令允许root用户手动挂载文件系统。第一个参数指定要挂载的文件系统。第二个参数指定用在挂载的目录。目录必须时以及存在的。
如果挂载点的目录不为空,挂载之后,目录之前的内容将暂时无法访问,知道将该文件系统卸载。大部分情况下我们会创建一个新的目录来挂载文件系统,或者在某些场景下,挂载到已存在的目录。下面介绍两种常用的挂载方法。
通过设备名挂载
将/dev/sr0
挂载到/mnt
目录。
[root@rhce ~]# mount /dev/sr0 /mnt/
mount: /mnt: WARNING: source write-protected, mounted read-only.
[root@rhce ~]# lsblk /dev/sr0
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom /mnt
/run/media/root/RHEL-9-2-0-BaseOS-x86_64
通过lsblk
的显示,我们可以发现一个磁盘可以挂载到多个目录。
通过设备名进行挂载在短时间内使用是可以正常工作的。在命名规则中说过,磁盘命名顺序是依据内核识别顺序来决定的,则设备名是可能发生变化的。更好的方式是通过一个唯一标识的名称进行挂载。
通过UUID挂载
UUID,用作通用唯一标识符。是一个非常长的十六进制数字。只有该文件系统没有被重新创建过,UUID就会保持不边。通过lsblk -fp
命令可以查看设备完整的路径、UUID和挂载点等信息。
[root@rhce /]# lsblk -fp
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
/dev/sr0 iso9660 Joliet Extension RHEL-9-2-0-BaseOS-x86_64 2023-04-13-16-58-02-00 0 100% /run/media/root/RHEL-9-2-0-BaseOS-x86_64
/dev/nvme0n1
├─/dev/nvme0n1p1 xfs 1b6c6e8b-b0ea-4edf-a24c-ebcf5f2d32f7 0 100% /mnt
│ /boot
└─/dev/nvme0n1p2 LVM2_member LVM2 001 HPRENt-Y8n0-cr0N-1u8h-3oGG-RXgY-9oBMZf
├─/dev/mapper/rhel-root xfs e7f53aae-57fb-4834-b9cf-a77602ee07c5 12.4G 27% /
└─/dev/mapper/rhel-swap swap 1 8da14274-8190-4188-9f2b-03b60d1be88c [SWAP]
[root@rhce /]# mount UUID="2023-04-13-16-58-02-00" /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
(2)卸载文件系统
关机或者重启都会自动卸载所有文件系统。在这一过程中,缓存在内存中的任何文件系统数据都会刷新到存储设备,保证文件系统数据不会遭受破坏。为了避免损坏磁盘上数据,务必先卸载可移动驱动器。保证数据完整性。
umount
命令用于卸载文件系统,需要使用挂载点作用参数。
[root@rhce /]# umount /mnt
如果挂载的文件正在使用中,则无法卸载文件系统。大部分都是如下情况。
[root@rhce mnt]# umount /mnt
umount: /mnt: target is busy.
[root@rhce mnt]# cd
[root@rhce ~]# umount /mnt
也可以通过lsof
命令去查看指定目录中访问的进程,等待进程完成或者手动发送信号结束进程。
[root@rhce mnt]# lsof /mnt/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 2738 root cwd DIR 11,0 2048 3328 /mnt
lsof 5913 root cwd DIR 11,0 2048 3328 /mnt
lsof 5914 root cwd DIR 11,0 2048 3328 /mnt
三、磁盘分区
(1)分区方案
为了方便管理,我们会将一块物理磁盘划分成几个逻辑区域。这样在物理磁盘不损坏的情况下,每个分区性能相互之间不受影响,同时某个分区损坏,仅仅是这个分区收到影响而非整个硬盘。我们通常由两种分区方案,下面来介绍这两种分区方案。
MBR分区方案
MBR(Master Boot Record)是一种比较旧的磁盘类型。在1983年首次在IBM PC DOS2.0中引入。由于MBR的年代久远。MBR分区方案只适用于最大容量2TB的硬盘,更大的容量使用MBR的话,超过2TB的部分无法识别。MBR分区将引导记录放在硬盘的第一个扇区,由于历史原因,一个扇区只有512字节,所以MBR最多只能创建4个主分区(3个主分区+扩展分区),在Linux中管理员可以通过扩展分区最多创建15个分区。由于现在磁盘大小越来越大,基于SAN卷可以更大。因此MBR分区方案已经逐渐不适用于当前环境,新的分区方案逐渐代替MBR。
GPT分区方案
GPT(Globally Unique Identifier Partition Table),全局唯一标识分区表,GPT最多可提供128个分区。GPT除了解决MBR分区方案的限制外。GPT使用全局唯一标识符来识别每一个磁盘和分区。同时GPT在磁盘头部存放一个分区表外,同时在磁盘尾部存在一个备份副本,为分区表信息提供冗余。
(2)分区操作
在Linux中,我们有两种磁盘管理工具,两种工具都可以对磁盘进行更改。如创建分区、删除分区和更改分区类型。
fdisk
fdisk
命令将整个磁盘的设备名称作为一个参数。可用使用-l
选项来查看指定磁盘的分区。
[root@rhce ~]# fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual NVMe Disk
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: 0x470fc48e
Device Boot Start End Sectors Size Id Type
/dev/nvme0n1p1 * 2048 2099199 2097152 1G 83 Linux
/dev/nvme0n1p2 2099200 41943039 39843840 19G 8e Linux LVM
如果不使用选项,fdisk
命令打开一个交互式会话来管理磁盘。
[root@rhce ~]# fdisk /dev/nvme0n2
Welcome to fdisk (util-linux 2.37.4).
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 0x3165a8fd.
Command (m for help): p
Disk /dev/nvme0n2: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual NVMe Disk
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: 0x3165a8fd
Command (m for help): q
[root@rhce ~]#
创建MBR分区
fdisk
命令来创建分区,默认是使用MBR分区方案。
[root@rhce ~]# fdisk /dev/nvme0n2
Welcome to fdisk (util-linux 2.37.4).
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 0x5eec43bf.
Command (m for help): n //创建一个新的分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p //创建一个主分区
Partition number (1-4, default 1): //分区编号,直接回车,默认从1开始
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +5G //指定分区大小
Created a new partition 1 of type 'Linux' and of size 5 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): e //创建扩展分区
Partition number (2-4, default 2):
First sector (10487808-41943039, default 10487808):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (10487808-41943039, default 41943039):
Created a new partition 2 of type 'Extended' and of size 15 GiB.
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (10489856-41943039, default 10489856):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (10489856-41943039, default 41943039): +10G
Created a new partition 5 of type 'Linux' and of size 10 GiB.
Command (m for help): p //查看分区情况
Disk /dev/nvme0n2: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual NVMe Disk
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: 0x5eec43bf
Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 10487807 10485760 5G 83 Linux
/dev/nvme0n2p2 10487808 41943039 31455232 15G 5 Extended
/dev/nvme0n2p5 10489856 31461375 20971520 10G 83 Linux
Command (m for help): w //保存分析表
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
创建GPT分区表
[root@rhce ~]# fdisk /dev/nvme0n2
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): g //将分区方案修改为GPT
Created a new GPT disklabel (GUID: FD40D838-E909-CB4D-8675-DD3A0900BE01).
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-41943006, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943006, default 41943006): +5G
Created a new partition 1 of type 'Linux filesystem' and of size 5 GiB.
Command (m for help): n
Partition number (2-128, default 2):
First sector (10487808-41943006, default 10487808):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (10487808-41943006, default 41943006): +5G
Created a new partition 2 of type 'Linux filesystem' and of size 5 GiB.
Command (m for help): n
Partition number (3-128, default 3):
First sector (20973568-41943006, default 20973568):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (20973568-41943006, default 41943006):
Created a new partition 3 of type 'Linux filesystem' and of size 10 GiB.
Command (m for help): p
Disk /dev/nvme0n2: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual NVMe Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: FD40D838-E909-CB4D-8675-DD3A0900BE01
Device Start End Sectors Size Type
/dev/nvme0n2p1 2048 10487807 10485760 5G Linux filesystem
/dev/nvme0n2p2 10487808 20973567 10485760 5G Linux filesystem
/dev/nvme0n2p3 20973568 41943006 20969439 10G Linux filesystem
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
其他常用选项
d 删除一个分区
m 显示帮助菜单
t 修改分区类型
g 退出交互式会话,不保存分区信息
i 查看指定分区的信息
parted
parted
命令同样是将整个设备名称作为第一个参数,并且有一个或多个子命令。使用print
子命令可用显示指定磁盘的分区信息。
[root@rhce ~]# parted /dev/nvme0n1 print
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n1: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1075MB 1074MB primary xfs boot
2 1075MB 21.5GB 20.4GB primary lvm
默认情况下,parted显示以10的幂次方表示所有空间大小(KB、MB、GB)可以使用unit子命令更改默认设置。参数如下
s 表示扇区
B 表示字节
MIB、GIB或TIB 2的幂次方
MB、GB或TB 10的幂次方
[root@rhce ~]# parted /dev/nvme0n1 unit GIB print
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n1: 20.0GiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 0.00GiB 1.00GiB 1.00GiB primary xfs boot
2 1.00GiB 20.0GiB 19.0GiB primary lvm
写入分区表
在分区之前需要先写入磁盘标签,磁盘标签决定了所用的分区方案
将MBR标签写入磁盘
[root@rhce ~]# parted /dev/nvme0n2 mklabel msdos
Warning: The existing disk label on /dev/nvme0n2 will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
将GPT标签写入磁盘
[root@rhce ~]# parted /dev/nvme0n2 mklabel gpt
Warning: The existing disk label on /dev/nvme0n2 will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
mklabel子命令会直接擦除现有的分区表。如果是重复利用磁盘,不需要考虑里面的数据,才使用mklabel命令。一旦分区表修改,之前的所用数据都将无法访问。
创建MBR分区
//写入MBR标签
[root@rhce ~]# parted /dev/nvme0n2 mklabel msdos
Warning: The existing disk label on /dev/nvme0n2 will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2
GNU Parted 3.5
Using /dev/nvme0n2
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart //创建分区
Partition type? primary/extended? primary //分区为主分区
File system type? [ext2]? xfs //标记文件系统类型
Start? 1M //开始位置
End? 5G //结束位置
(parted) p
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n2: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 5000MB 4999MB primary xfs
(parted) quit
Information: You may need to update /etc/fstab.
注:
- parted工具不能直接指定分区大小,分区大小size=end-start。
- 分区的单位可用指定s(扇区),MB,GB和TB,默认是MB。
创建GPT分区
[root@rhce ~]# parted /dev/nvme0n2 mklabel gpt
Warning: The existing disk label on /dev/nvme0n2 will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2
GNU Parted 3.5
Using /dev/nvme0n2
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart
Partition name? []? userdate //对于GPT分区方案,每一个分区会有一个名称。
File system type? [ext2]? xfs
Start? 1M
End? 4097M
(parted) p
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n2: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 4097MB 4096MB xfs userdate
(parted) quit
Information: You may need to update /etc/fstab.
删除分区
[root@rhce ~]# parted /dev/nvme0n2
GNU Parted 3.5
Using /dev/nvme0n2
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n2: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1075MB 1074MB userdate
(parted) rm 1
(parted) p
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n2: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted) quit
Information: You may need to update /etc/fstab.
非交互式创建分区
创建MBR分区
[root@rhce ~]# parted /dev/nvme0n2 mklabel msdos
Warning: The existing disk label on /dev/nvme0n2 will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2 mkpart primary ext4 1mib 1025mib
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2 print
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n2: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1075MB 1074MB primary
创建GPT分区
[root@rhce ~]# parted /dev/nvme0n2 mklabel gpt
Warning: The existing disk label on /dev/nvme0n2 will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2 mkpart userdate xfs 1mib 1025mib
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2 print
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n2: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1075MB 1074MB userdate
(3)格式化分区
磁盘分区后,下一步是向其中添加文件系统。在红帽Linux中,最常见的两种常见的类型是XFS和ext4。
使用mkfs.xfs
命令为块设备添加XFS文件系统,使用mkfs.ext4
命令为块设备添加ext4文件系统。
[root@rhce ~]# mkfs.ext4 /dev/nvme0n
nvme0n1 nvme0n1p1 nvme0n1p2 nvme0n2 nvme0n2p1
[root@rhce ~]# mkfs.ext4 /dev/nvme0n2p1
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 4eb011d0-9bc3-4abb-84af-5848708bf805
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
(4)永久挂载
前文中我们已经知道可以手动挂载文件系统来访问指定分区,但是手动挂载之后,每次重启挂载都会自动卸载,这样文件系统上的数据就无法访问,需要重新挂载。为了确保系统启动时会自动将指定的文件系统挂载。可以载/etc/fstab
文件中添加条目。该文件中指定了系统启动时需要自动挂载的文件系统。
[root@rhce ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Jul 24 06:13:18 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=1b6c6e8b-b0ea-4edf-a24c-ebcf5f2d32f7 /boot xfs defaults 0 0
/dev/mapper/rhel-swap none swap defaults 0 0
该文件中用空格分隔,每行具有6个字段。
第一个字段指定设备。可以使用UUID或者设备文件名称。这里更推荐使用UUID。
第二个字段是目录挂载点,该挂载点必须存在。
第三个字段是文件系统类型。也就是磁盘格式化使用的文件系统类型。
第四个字段是以逗号分隔的、应用于设备的选项列表。mount(8)中记录了所有选项,大部分情况下使用defaults
第五个字段是dump命令备份时会使用该字段,默认为0
第六个字段是fsck顺序字段,该字段决定在系统启动时是否启用fsck命令,来检查文件系统。对于xfs文件系统,该字段应写为0,因为xfs文件系
统不适用fsck检查。对于ext4文件系统,如果时根文件系统,该字段应设为1,其他情况设为2。
注意:/etc/fstab文件如果存在错误条目,可能导致计算机无法启动。系统重启前务必检查条目是否正确。可以使用mount -a或者mount /mountpoint重新挂载文件系统,来验证是否错误。
永久挂载配置
查看指定设备的UUID
[root@rhce ~]# lsblk -f /dev/nvme0n2p1
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
nvme0n2p1 xfs b09896e4-12ce-42b4-ab40-5d6351abddf9
往/etc/fstab文件中添加条目
[root@rhce ~]# vim /etc/fstab
UUID=b09896e4-12ce-42b4-ab40-5d6351abddf9 /mnt xfs defaults 0 0
验证添加的条目是否正确
[root@rhce ~]# mount -a
[root@rhce ~]# df /dev/nvme0n2p1
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/nvme0n2p1 1038336 40292 998044 4% /mnt
四、SWAP管理
(1)SWAP介绍
SWAP作用
swap分区又称交换分区,是受内核内存管理系统控制的磁盘区域。内核会使用swap分区来保存不活动的内存页。当系统上内存使用量达到上线后,还需要使用内存,内核会将已经分配给进程的内存页保存到磁盘中,将空出来的内存分配给新的进程。当需要使用已经在swap分区中内存页是,会重新找一个不活动的内存页写入磁盘,然后重新调用所需的页面。
但是swap分区唯于磁盘上面,跟内存相比,交换会比较慢,虽然可以增加系统内存,但是对于内存不满足负载需求时,不能够将swap分区作为可持续性解决方案。
SWAP分区大小
RAM | swap | 允许HIBERNATE(休眠)时的SWAP |
---|---|---|
2Gib或一下 | 两倍的RAM | 三倍的RAM |
2~8GiB | 同等的RAM | 两倍的RAM |
8~64GiB | 至少4GiB | 1.5倍的RAM |
64GiB以上 | 至少4GiB | 不建议开启HIBERNATE |
HIBETNATE功能会在关闭系统电源之前将RAM中的内容写入swap,重新开机后,内核会将swap中内容恢复到RAM中。
(2)SWAP分区管理
要创建swap分区,需要执行以下操作:
- 创建文件系统类型为linux-swap的分区
- 为设备放置交换签名
创建SWAP
用三种方式来常见一个大小为1Gib的交换分区。
parted交互式
[root@rhce ~]# parted /dev/nvme0n2 mklabel msdos
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2
GNU Parted 3.5
Using /dev/nvme0n2
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? linux-swap
Start? 1mib
End? 1025mib
(parted) p
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n2: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1075MB 1074MB primary linux-swap(v1) swap
(parted) quit
Information: You may need to update /etc/fstab.
[root@rhce ~]# mkswap /dev/nvme0n2p1
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=d8d7d5c7-3a3a-4b51-bb93-abac3be397f6
parted非交互式
[root@rhce ~]# parted /dev/nvme0n2 mklabel msdos
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2 mkpart primary linux-swap 1mib 1025mib
Information: You may need to update /etc/fstab.
[root@rhce ~]# parted /dev/nvme0n2 unit mib print
Model: VMware Virtual NVMe Disk (nvme)
Disk /dev/nvme0n2: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1.00MiB 1025MiB 1024MiB primary swap
[root@rhce ~]# mkswap /dev/nvme0n2p1
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=78f0ee0d-fca5-42ed-8a4a-14ea3d6eba59
fdisk
[root@rhce ~]# fdisk /dev/nvme0n2
Welcome to fdisk (util-linux 2.37.4).
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 0x42d52389.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +1G
Created a new partition 1 of type 'Linux' and of size 1 GiB.
Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): swap
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/nvme0n2: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual NVMe Disk
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: 0x42d52389
Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 2099199 2097152 1G 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@rhce ~]# mkswap /dev/nvme0n2p1
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=e8d6ef04-70e9-4664-b319-f15e54626735
激活SWAP
使用swapon
命令激活已经格式化的swap分区。
[root@rhce ~]# free -h
total used free shared buff/cache available
Mem: 3.5Gi 1.4Gi 1.8Gi 22Mi 610Mi 2.2Gi
Swap: 2.0Gi 0B 2.0Gi
[root@rhce ~]# swapon /dev/nvme0n2p1
[root@rhce ~]# free -h
total used free shared buff/cache available
Mem: 3.5Gi 1.4Gi 1.8Gi 24Mi 612Mi 2.2Gi
Swap: 3.0Gi 0B 3.0Gi
[root@rhce ~]#
卸载SWAP
[root@rhce ~]# free -h
total used free shared buff/cache available
Mem: 3.5Gi 1.4Gi 1.8Gi 24Mi 664Mi 2.2Gi
Swap: 3.0Gi 0B 3.0Gi
[root@rhce ~]# swapoff /dev/nvme0n2p1
[root@rhce ~]# free -h
total used free shared buff/cache available
Mem: 3.5Gi 1.4Gi 1.8Gi 24Mi 664Mi 2.2Gi
Swap: 2.0Gi 0B 2.0Gi
永久激活SWAP
[root@rhce ~]# vim /etc/fstab
UUID=d8d7d5c7-3a3a-4b51-bb93-abac3be397f6 swap swap defaults 0 0
重启验证
[root@rhce ~]# free -h
total used free shared buff/cache available
Mem: 3.5Gi 1.4Gi 1.8Gi 22Mi 638Mi 2.2Gi
Swap: 3.0Gi 0B 3.0Gi
(3)SWAP优先级
默认情况下,内核会根据顺序来决定使用swao的顺序。但是可以为每一个swap分区指定一个优先级,内核会首先使用优先级最高的swap分区。默认优先级为-2。可以通过修改/etc/fstab中第四个字段来定义优先级。当swap分区优先级相同,内核会以轮循方式向其中写入。
/dev/mapper/rhel-swap none swap defaults 0 0
UUID=d8d7d5c7-3a3a-4b51-bb93-abac3be397f6 swap swap pri=19 0 0
[root@rhce ~]# swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2
/dev/nvme0n2p1 partition 1024M 0B 19