2.3 挂载
挂载:将额外文件系统与根文件系统某现存的目录建立起关联关系,进而使得此目录做为其它文件访问入口的行为
卸载:为解除此关联关系的过程
把设备关联挂载点:mount Point
挂载点下原有文件在挂载完成后会被临时隐藏,因此,挂载点目录一般为空
进程正在使用中的设备无法被卸载
2.3.1 挂载文件系统 mount
格式:
mount [-fnrsvw] [-t vfstype] [-o options] device mountpoint
device:指明要挂载的设备
- 设备文件:例如:/dev/sda5
- 卷标:-L ‘LABEL’, 例如 -L ‘MYDATA’
- UUID: -U ‘UUID’:例如 -U ‘0c50523c-43f1-45e7-85c0-a126711d406e’
- 伪文件系统名称:proc, sysfs, devtmpfs, configfs
**mountpoint:**挂载点目录必须事先存在,建议使用空目录
mount常用命令选项
-t fstype 指定要挂载的设备上的文件系统类型,如:ext4,xfs
-r readonly,只读挂载
-w read and write, 读写挂载
-n 不更新/etc/mtab,mount不可见
-a 自动挂载所有支持自动挂载的设备(定义在了/etc/fstab文件中,且挂载选项中有
auto功能)
-L 'LABEL' 以卷标指定挂载设备
-U 'UUID' 以UUID指定要挂载的设备
-B, --bind 绑定目录到另一个目录上
-o options:(挂载文件系统的选项),多个选项使用逗号分隔
async 异步模式,内存更改时,写入缓存区buffer,过一段时间再写到磁盘中,效率高,但不安全
sync 同步模式,内存更改时,同时写磁盘,安全,但效率低下
atime/noatime 包含目录和文件
diratime/nodiratime 目录的访问时间戳
auto/noauto 是否支持开机自动挂载,是否支持-a选项
exec/noexec 是否支持将文件系统上运行应用程序
dev/nodev 是否支持在此文件系统上使用设备文件
suid/nosuid 是否支持suid和sgid权限
remount 重新挂载
ro/rw 只读、读写
user/nouser 是否允许普通用户挂载此设备,/etc/fstab使用
acl/noacl 启用此文件系统上的acl功能
loop 使用loop设备
_netdev 当网络可用时才对网络资源进行挂载,如:NFS文件系统
defaults 相当于rw, suid, dev, exec, auto, nouser, async
挂载规则:
- 一个挂载点同一时间只能挂载一个设备
- 一个挂载点同一时间挂载了多个设备,只能看到最后一个设备的数据,其它设备上的数据将被隐藏
- 一个设备可以同时挂载到多个挂载点
- 通常挂载点一般是已存在空的目录
2.3.2 卸载文件系统 umount
卸载时:可使用设备,也可以使用挂载点
umount 设备名|挂载点
2.3.3 查看挂载情况
查看挂载
#通过查看/etc/mtab文件显示当前已挂载的所有设备
mount
#查看内核追踪到的已挂载的所有设备
cat /proc/mounts
查看挂载点情况
findmnt MOUNT_POINT|device
查看正在访问指定文件系统的进程
lsof MOUNT_POINT
fuser -v MOUNT_POINT
终止所有在正访问指定的文件系统的进程
fuser -km MOUNT_POINT
2.3.4 持久挂载
将挂载保存到 /etc/fstab 中可以下次开机时,自动启用挂载
/etc/fstab格式帮助:
man 5 fstab
每行定义一个要挂载的文件系统,,其中包括共 6 项
- 要挂载的设备或伪文件系统
设备文件
LABEL:LABEL=""
UUID:UUID=""
伪文件系统名称:proc, sysfs - 挂载点:必须是事先存在的目录
- 文件系统类型:ext4,xfs,iso9660,nfs,none
- 挂载选项:defaults ,acl,bind
- 转储频率:0:不做备份 1:每天转储 2:每隔一天转储
- fsck检查的文件系统的顺序:允许的数字是0 1 2
0:不自检 ,1:首先自检;一般只有rootfs才用 2:非rootfs使用
添加新的挂载项,需要执行下面命令生效
mount -a
范例:
[root@rocky8 ~]# mkdir /mnt/sdb{1,2}
[root@rocky8 ~]# ls /mnt
sdb1 sdb2
[root@rocky8 ~]# ls /mnt/sdb1
[root@rocky8 ~]# touch /mnt/sdb1/sdb1.txt
[root@rocky8 ~]# touch /mnt/sdb2/sdb2.txt
/mnt
├── sdb1
│ └── sdb1.txt
└── sdb2
└── sdb2.txt
2 directories, 2 files
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47636 2039276 3% /mnt
[root@rocky8 ~]# touch /mnt/sdb1/sdb1-2.txt
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47636 2039276 3% /mnt #创建的第一个文件used没有变化,还是47636
[root@rocky8 ~]# touch /mnt/sdb1/sdb1-3.txt
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47636 2039276 3% /mnt #创建的第二个文件used没有变化,还是47636
[root@rocky8 ~]# ll /mnt/sdb1/*
-rw-r--r-- 1 root root 0 Oct 15 22:46 /mnt/sdb1/sdb1-2.txt
-rw-r--r-- 1 root root 0 Oct 15 22:48 /mnt/sdb1/sdb1-3.txt
-rw-r--r-- 1 root root 0 Oct 15 22:45 /mnt/sdb1/sdb1.txt
[root@rocky8 ~]# echo > /mnt/sdb1/sdb1-4.txt
[root@rocky8 ~]# ll /mnt/sdb1/sdb1-4.txt
-rw-r--r-- 1 root root 1 Oct 15 22:51 /mnt/sdb1/sdb1-4.txt
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47660 2039252 3% /mnt #给文件写数据占用4k大小,因为一个文件最小占用4k大小空间
[root@rocky8 ~]# du /mnt/sdb1
4 /mnt/sdb1
[root@rocky8 ~]# echo > /mnt/sdb1/sdb1-5.txt
[root@rocky8 ~]# du /mnt/sdb1
8 /mnt/sdb1
#创建一个新文件又占用了4k
[root@rocky8 ~]# xfs_info /dev/sda2
meta-data=/dev/sda2 isize=512 agcount=4, agsize=6553600 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=26214400, imaxpct=25 #块大小4096
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=12800, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@rocky8 ~]# mount /dev/sdb1 /mnt/sdb1 #mount 挂载
[root@rocky8 ~]# ls /mnt/sdb1
lost+found
#挂载之后原来的数据就看不见了,原来的数据被隐藏了
[root@rocky8 ~]# echo > /mnt/sdb1/a.txt
[root@rocky8 ~]# echo > /mnt/sdb1/b.txt
[root@rocky8 ~]# ls /mnt/sdb1
a.txt b.txt lost+found
[root@rocky8 ~]# du -sh /mnt/sdb1
4.0K /mnt/sdb1
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47644 2039268 3% /mnt
/dev/sdb1 1031836 2821 972491 1% /mnt/sdb1 #已经占用2821的磁盘空间
[root@rocky8 ~]# echo > /mnt/sdb1/c.txt
[root@rocky8 ~]# du -sh /mnt/sdb1
5.0K /mnt/sdb1
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47644 2039268 3% /mnt
/dev/sdb1 1031836 2822 972490 1% /mnt/sdb1 #新建文件增加了1k 空间
[root@rocky8 ~]# ls /mnt/sdb1/ -a
. .. a.txt b.txt c.txt lost+found
[root@rocky8 ~]# umount /mnt/sdb1/
[root@rocky8 ~]# ls /mnt/sdb1/ -a
. .. sdb1-2.txt sdb1-3.txt sdb1-4.txt sdb1-5.txt sdb1.txt
#卸载了分区挂载,就可以看到原来根分区上的数据
#作为挂载点文件夹,第一文件夹必须存在,第二这个文件夹应该是一个空文件夹
[root@rocky8 ~]# ls /mnt/sdb2
sdb2.txt
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47644 2039268 3% /mnt
[root@rocky8 ~]# mount /dev/sdb2 /mnt/sdb2
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47644 2039268 3% /mnt
#只要挂载了分区,用df才能看的见
[root@rocky8 ~]# ls /mnt/sdb2
sdb1 sdb2
#xfs文件系统,比ext4元数据占用的空间要大
[root@rocky8 ~]# xfs_info /dev/sdb2
meta-data=/dev/sdb2 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
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
#df 默认查看的是以1k 为单位的
[root@rocky8 ~]# ls /mnt/sdb2
sdb1 sdb2
[root@rocky8 ~]# echo > /mnt/sdb2/x.txt
[root@rocky8 ~]# ls /mnt/sdb2
sdb1 sdb2 x.txt
[root@rocky8 ~]# du -sh /mnt/sdb2
12K /mnt/sdb2
[root@rocky8 ~]# echo > /mnt/sdb2/y.txt
[root@rocky8 ~]# du -sh /mnt/sdb2
16K /mnt/sdb2
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47652 2039260 3% /mnt
[root@rocky8 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=382688k,nr_inodes=95672,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=37,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22459)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=80116k,mode=700)
/dev/sdb2 on /mnt type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2 type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
#mount 也可以查看挂载信息
[root@rocky8 ~]# mount /dev/sdb1 /mnt/sdb2
[root@rocky8 ~]# ls /mnt/sdb2
a.txt b.txt c.txt lost+found
#一个目录 只能挂载一个分区,如果挂载多个分区,原来挂载的分区,里边的文件就被隐藏了,除非卸载现在挂载的分区才能看到
[root@rocky8 ~]# umount /mnt/sdb2
[root@rocky8 ~]# ls /mnt/sdb2
sdb1 sdb2 x.txt y.txt
#卸载了,原来的又回来了
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633816 102172584 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb2 2086912 47652 2039260 3% /mnt
[root@rocky8 ~]# mkdir /mnt/sdb2-2
[root@rocky8 ~]# mount /dev/sdb2 /mnt/sdb2-2
[root@rocky8 ~]# ls /mnt/sdb2-2
sdb1 sdb2 sdb2-2 x.txt y.txt
#一个分区可以挂载多个挂载点,但是它里边的数据是一样的
[root@rocky8 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=382688k,nr_inodes=95672,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=37,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22459)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=80116k,mode=700)
/dev/sdb2 on /mnt type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2 type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2-2 type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
#reltime 延迟更新读时间,当读时间达到一天以上才更新,或者读时间,比写时间旧的时候才更新,只有追加时,只是修改了文件,没有读文件,才会出现读时间比修改时间早,只有这个时候才会更新读时间
#可以在挂载的时候加上noatime,nodiratime,它就不会去更新读时间,但是如果touch文件它还是会更新读时间
[root@rocky8 ~]# touch /mnt/sdb2/xx.txt
[root@rocky8 ~]# cd /mnt/sdb2
#如果有人正在访问目录
[root@rocky8 sdb2]# umount /mnt/sdb2
umount: /mnt/sdb2: target is busy.
#现在是不能卸载挂载的分区的
[root@rocky8 sdb2]# fuser -v /mnt/sdb2 #fuser -v 查看谁正在访问此目录
USER PID ACCESS COMMAND
/mnt/sdb2: root kernel mount /mnt
root 917 ..c.. bash
[root@rocky8 sdb2]# fuser -mk /mnt/sdb2 #fuser -mk 可以强行踢掉使用此目录的用户
/mnt/sdb2: 917c
Connection closed.
Disconnected from remote host(rocky8.4-172.31.1.8) at 23:07:17.
Type `help' to learn how to use Xshell prompt.
[C:\~]$
Reconnecting in 1 seconds. Press any key to exit local shell.
.
Connecting to 172.31.1.8:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Fri Oct 15 19:09:34 2021 from 172.31.0.1
[root@rocky8 ~]#
#踢了访问的用户就会退出登录
[root@rocky8 ~]# fuser -v /mnt/sdb2
USER PID ACCESS COMMAND
/mnt/sdb2: root kernel mount /mnt
#现在就没有人访问了
[root@rocky8 ~]# umount /mnt/sdb2
#现在取消挂载就可以了
[root@rocky8 ~]# mount /dev/sdb2 /mnt/sdb2
[root@rocky8 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=382688k,nr_inodes=95672,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=37,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22459)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2-2 type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=80116k,mode=700)
/dev/sdb2 on /mnt/sdb2 type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
[root@rocky8 ~]# mount -o remount,ro /mnt/sdb2
#用mount -o remount 重新挂载,不用踢正在访问的用户
[root@rocky8 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=382688k,nr_inodes=95672,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=37,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22459)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2-2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=80116k,mode=700)
/dev/sdb2 on /mnt/sdb2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota) #这里已经是ro 只读
[root@rocky8 ~]# touch /mnt/sdb2/yy.txt
touch: cannot touch '/mnt/sdb2/yy.txt': Read-only file system
#现在就不能往里写文件了
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633912 102172488 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
/dev/sdb2 2086912 336 2086576 1% /mnt
tmpfs 80116 0 80116 0% /run/user/0
[root@rocky8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part
├─sdb2 8:18 0 2G 0 part /mnt/sdb2
├─sdb3 8:19 0 512B 0 part
├─sdb5 8:21 0 2G 0 part
└─sdb6 8:22 0 3G 0 part
sdc 8:32 0 10G 0 disk
├─sdc1 8:33 0 1G 0 part
└─sdc2 8:34 0 2G 0 part
sr0 11:0 1 9.2G 0 rom
nvme0n1 259:0 0 10G 0 disk
[root@rocky8 ~]# umount /boot
[root@rocky8 ~]# umount /data
[root@rocky8 ~]# umount /
umount: /: target is busy.
#/ 根分区是不能取消挂载的
[root@rocky8 ~]# mount -a
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633932 102172468 3% /
/dev/sdb2 2086912 336 2086576 1% /mnt
tmpfs 80116 0 80116 0% /run/user/0
/dev/sda1 1038336 191796 846540 19% /boot
/dev/sda3 52403200 398404 52004796 1% /data
[root@rocky8 ~]# findmnt /data
TARGET SOURCE FSTYPE OPTIONS
/data /dev/sda3 xfs rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota
#findmnt 查看挂载情况
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633912 102172488 3% /
/dev/sdb2 2086912 336 2086576 1% /mnt
tmpfs 80116 0 80116 0% /run/user/0
/dev/sda1 1038336 191796 846540 19% /boot
/dev/sda3 52403200 398404 52004796 1% /data
[root@rocky8 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs c72110fc-5dae-470c-9940-97ba978c7dc7 /boot
├─sda2 xfs 57f3a077-bb67-42c3-9e6c-bc8a0fed3776 /
├─sda3 xfs 02ae6744-8c28-4c86-b85b-e113c06a834f /data
├─sda4
└─sda5 swap 4f387202-0a81-4202-9e12-90d04af4d308 [SWAP]
sdb
├─sdb1 ext4 f7d5345f-5f29-417d-a044-37508852e80f
├─sdb2 xfs 8a2c8fdf-9294-4030-b10c-edc8a7b3e78f /mnt/sdb2
├─sdb3
├─sdb5
└─sdb6
sdc
├─sdc1
└─sdc2
sr0 iso9660 Rocky-8-4-x86_64-dvd 2021-06-20-16-16-21-00
nvme0n1
[root@rocky8 ~]# blkid /dev/sdb1
/dev/sdb1: UUID="f7d5345f-5f29-417d-a044-37508852e80f" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="a62c9118-01"
[root@rocky8 ~]# vim /etc/fstab
#添加下面内容
UUID=f7d5345f-5f29-417d-a044-37508852e80f /mnt/sdb1 ext4 ro,noatime,nodiratime 0 0
[root@rocky8 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=382688k,nr_inodes=95672,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=37,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22459)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sdb2 on /mnt type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2-2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=80116k,mode=700)
/dev/sdb2 on /mnt/sdb2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
#现在/dev/sdb1是没有挂载的
[root@rocky8 ~]# mount -a #mount -a 立即挂载
[root@rocky8 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=382688k,nr_inodes=95672,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=37,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22459)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sdb2 on /mnt type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2-2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=80116k,mode=700)
/dev/sdb2 on /mnt/sdb2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb1 on /mnt/sdb1 type ext4 (ro,noatime,nodiratime)
#现在/dev/sdb1已经挂载了
[root@rocky8 ~]# cd /mnt/sdb1
[root@rocky8 sdb1]# ls
a.txt b.txt c.txt lost+found
[root@rocky8 sdb1]# rm -f a.txt
rm: cannot remove 'a.txt': Read-only file system
#由于是只读挂载,现在是不能删除里边的文件的
[root@rocky8 sdb1]# vim /etc/fstab
UUID=f7d5345f-5f29-417d-a044-37508852e80f /mnt/sdb1 ext4 noatime,nodiratime 0 0
#删了ro,默认就是rw
[root@rocky8 sdb1]# mount -a
[root@rocky8 sdb1]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=382688k,nr_inodes=95672,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=37,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22459)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sdb2 on /mnt type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2-2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=80116k,mode=700)
/dev/sdb2 on /mnt/sdb2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb1 on /mnt/sdb1 type ext4 (ro,noatime,nodiratime)
#mount -a 是不能立即生效的
[root@rocky8 sdb1]# mount -o remount /dev/sdb1 #必须要remount重新挂载
[root@rocky8 sdb1]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=382688k,nr_inodes=95672,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=37,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22459)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sdb2 on /mnt type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb2 on /mnt/sdb2-2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=80116k,mode=700)
/dev/sdb2 on /mnt/sdb2 type xfs (ro,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/sdb1 on /mnt/sdb1 type ext4 (rw,noatime,nodiratime)
#现在就生效了
#mount -a 是如果从来没有在/etc/fstab存在的,新加的是立即生效的,如果已经存在,改的是不能立即生效的
[root@rocky8 sdb1]# cd /mnt/sdb1
[root@rocky8 sdb1]# ls
a.txt b.txt c.txt lost+found
[root@rocky8 sdb1]# rm -rf lost+found/
[root@rocky8 sdb1]# ls
a.txt b.txt c.txt
#现在就可以删除里边的文件或文件夹了,lost+found文件夹,是ext2、 ext3、etx4的特性,相当于收容所
[root@rocky8 sdb1]# stat b.txt
File: b.txt
Size: 1 Blocks: 2 IO Block: 1024 regular file
Device: 811h/2065d Inode: 13 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-10-15 22:55:26.196775264 +0800
Modify: 2021-10-15 22:55:26.196775264 +0800
Change: 2021-10-15 22:55:26.196775264 +0800
Birth: -
[root@rocky8 sdb1]# cat b.txt
[root@rocky8 sdb1]# stat b.txt
File: b.txt
Size: 1 Blocks: 2 IO Block: 1024 regular file
Device: 811h/2065d Inode: 13 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-10-15 22:55:26.196775264 +0800
Modify: 2021-10-15 22:55:26.196775264 +0800
Change: 2021-10-15 22:55:26.196775264 +0800
Birth: -
#cat atime是不修改的
[root@rocky8 sdb1]# touch b.txt
[root@rocky8 sdb1]# stat b.txt
File: b.txt
Size: 1 Blocks: 2 IO Block: 1024 regular file
Device: 811h/2065d Inode: 13 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-10-15 23:21:03.783761878 +0800
Modify: 2021-10-15 23:21:03.783761878 +0800
Change: 2021-10-15 23:21:03.783761878 +0800
Birth: -
#强行touch 了,atime是修改的
[root@rocky8 sdb1]# reboot
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5748 394832 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2632648 102173752 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 192152 846184 19% /boot
/dev/sdb1 1031836 2821 972491 1% /mnt/sdb1
tmpfs 80116 0 80116 0% /run/user/0
#重启之后,/dev/sdb1是自动挂载的
[root@rocky8 ~]# vim /etc/fstab
UUID=f7d5345f-5f29-417d-a044-37508852e800 /mnt/sdb1 ext4 noatime,nodiratime 0 0
#假如在设置时候把UUID 写错了,系统重启后是不能正常启动的
[root@rocky8 ~]# reboot
#会进入故障状态,输入root密码
#查看/dev/sdb1 是没有挂载的
#进入配置文件
#把报错的磁盘挂载那行先注释掉
#重启
#现在系统就能正常启动了
[root@rocky8 ~]# vim /etc/fstab
/dev/sdb1 /mnt/sdb1 ext4 noatime,nodiratime 0 0
[root@rocky8 ~]# mount -a
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2632880 102173520 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 192152 846184 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb1 1031836 2821 972491 1% /mnt/sdb1
范例:centos7, 8 /etc/fstab 的分区UUID错误,无法启动
自动进入emergency mode,输入root 密码
#cat /proc/mounts 可以查看到/ 以rw方式挂载
#vim /etc/fstab
#reboot
范例:centos 6 /etc/fstab 的分区UUID错误,无法启动
#如果/etc/fstab 的挂载设备出错,比如文件系统故障,并且文件系统检测项(即第6项为非0),将导致无法启动
#自动进入emergency mode,输入root 密码
#cat /proc/mounts 可以查看到/ 以ro方式挂载,无法直接修改配置文件
#mount -o remount,rw /
#vim /etc/fstab
#将故障行的最后1项,即第6项修改为0,开机不检测此项挂载设备的健康性,从而忽略错误,能实现启动
范例:/etc/fstab格式
[root@centos8 ~]#cat /etc/fstab
UUID=cb7cae1e-d227-4f64-872b-cd6cce20c911 /data/mysql ext4 noatime
0 0
/disk.img data/disk xfs defaults
0 0
/etc /mnt/etc none bind
0 0
[root@centos6 ~]#cat /etc/fstab
/disk.img /mnt/disk ext4 loop 0 0
2.4 处理交换文件和分区
2.4.1 swap 介绍
swap交换分区是系统RAM的补充,swap 分区支持虚拟内存。当没有足够的 RAM 保存系统处理的数据时会将数据写入 swap 分区,当系统缺乏 swap 空间时,内核会因 RAM 内存耗尽而终止进程。配置过多 swap 空间会造成存储设备处于分配状态但闲置,造成浪费,过多 swap 空间还会掩盖内存泄露
注意:为优化性能,可以将swap 分布存放,或高性能磁盘存放
官方推荐推荐系统 swap 空间
https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/installation_guide/sect-disk-partitioning-setup-ppc#sect-recommended-partitioning-scheme-ppc
系统中的 RAM 量 | 推荐的 swap 空间 | 允许休眠的建议 swap 空间大小 |
---|---|---|
低于 2 GB | RAM 量的2倍数 | RAM 容量的三倍 |
2 GB - 8 GB | 等于 RAM 量 | RAM 量的倍数 |
8 GB - 64 GB | 4 GB 到 RAM 容量的 0.5 倍 | RAM 容量的 1.5 倍 |
超过 64 GB | 独立负载(至少 4GB) | 不建议使用休眠功能 |
范例:
[root@rocky8 ~]# free -h
total used free shared buff/cache available
Mem: 782Mi 178Mi 396Mi 5.0Mi 207Mi 470Mi
Swap: 2.0Gi 0B 2.0Gi
#如果内存被占满,没有虚拟内存的时候,会出现OOM报错,OOM 就是 out of memory,内存满了
[root@rocky8 ~]# dd if=/dev/zero of=/dev/null bs=8G count=1
dd: memory exhausted by input buffer of size 8589934592 bytes (8.0 GiB)
#立即使用8G内存,会报内存不足
[root@rocky8 ~]# dd if=/dev/zero of=/dev/null bs=4G count=1
dd: memory exhausted by input buffer of size 4294967296 bytes (4.0 GiB)
[root@rocky8 ~]# dd if=/dev/zero of=/dev/null bs=100M count=1
1+0 records in
1+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0722939 s, 1.5 GB/s
[root@rocky8 ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0xa62c9118
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux
/dev/sdb2 2099200 6293503 4194304 2G 83 Linux
/dev/sdb3 6293504 41943039 35649536 17G 5 Extended
/dev/sdb5 6295552 10489855 4194304 2G 83 Linux
/dev/sdb6 10491904 16783359 6291456 3G 8e Linux LVM
[root@rocky8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part /mnt/sdb1
├─sdb2 8:18 0 2G 0 part
├─sdb3 8:19 0 1K 0 part
├─sdb5 8:21 0 2G 0 part
└─sdb6 8:22 0 3G 0 part
sdc 8:32 0 10G 0 disk
├─sdc1 8:33 0 1G 0 part
└─sdc2 8:34 0 2G 0 part
sr0 11:0 1 9.2G 0 rom
nvme0n1 259:0 0 10G 0 disk
[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5744 394836 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2632880 102173520 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 192152 846184 19% /boot
tmpfs 80116 0 80116 0% /run/user/0
/dev/sdb1 1031836 2821 972491 1% /mnt/sdb1
[root@rocky8 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): t
Partition number (1-3,5,6, default 6): 5
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0xa62c9118
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux
/dev/sdb2 2099200 6293503 4194304 2G 83 Linux
/dev/sdb3 6293504 41943039 35649536 17G 5 Extended
/dev/sdb5 6295552 10489855 4194304 2G 82 Linux swap / Solaris #把/dev/sdb5改成swap
/dev/sdb6 10491904 16783359 6291456 3G 8e Linux LVM
Command (m for help): w
The partition table has been altered.
Syncing disks.
[root@rocky8 ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0xa62c9118
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux
/dev/sdb2 2099200 6293503 4194304 2G 83 Linux
/dev/sdb3 6293504 41943039 35649536 17G 5 Extended
/dev/sdb5 6295552 10489855 4194304 2G 82 Linux swap / Solaris
/dev/sdb6 10491904 16783359 6291456 3G 8e Linux LVM
[root@rocky8 ~]# mkswap /dev/sdb5 #mkswap 创建swap分区
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=9a3b3cde-e94d-4f62-9c7e-069d2e3e400b
[root@rocky8 ~]# blkid
/dev/sda2: UUID="57f3a077-bb67-42c3-9e6c-bc8a0fed3776" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-02"
/dev/sda1: UUID="c72110fc-5dae-470c-9940-97ba978c7dc7" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-01"
/dev/sda3: UUID="02ae6744-8c28-4c86-b85b-e113c06a834f" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-03"
/dev/sda5: UUID="4f387202-0a81-4202-9e12-90d04af4d308" TYPE="swap" PARTUUID="43d7507d-05"
/dev/sdb1: UUID="f7d5345f-5f29-417d-a044-37508852e80f" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="a62c9118-01"
/dev/sdb2: UUID="8a2c8fdf-9294-4030-b10c-edc8a7b3e78f" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="a62c9118-02"
/dev/sdb5: UUID="9a3b3cde-e94d-4f62-9c7e-069d2e3e400b" TYPE="swap" PARTUUID="a62c9118-05"
/dev/sdb6: PARTUUID="a62c9118-06"
/dev/sdc1: PARTLABEL="Linux filesystem" PARTUUID="7a7090a5-623a-45a0-b7e7-2c613355b7ea"
/dev/sdc2: PARTLABEL="Linux LVM" PARTUUID="cb838081-f185-4983-ad7e-6eea78c54565"
/dev/sr0: BLOCK_SIZE="2048" UUID="2021-06-20-16-16-21-00" LABEL="Rocky-8-4-x86_64-dvd" TYPE="iso9660" PTUUID="4a14a4ee" PTTYPE="dos"
[root@rocky8 ~]# vim /etc/fstab
UUID=9a3b3cde-e94d-4f62-9c7e-069d2e3e400b swap swap defaults 0 0
[root@rocky8 ~]# swapon -a #立即生效
[root@rocky8 ~]# swapon -s #查看swap情况
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -2
/dev/sdb5 partition 2097148 0 -3
#-2 ,-3 是优先级,数字越小优先级越高,优先使用
[root@rocky8 ~]# free -h
total used free shared buff/cache available
Mem: 782Mi 179Mi 391Mi 5.0Mi 210Mi 469Mi
Swap: 4.0Gi 0B 4.0Gi
#swap 增加2G
#删除swap 要保证被删除的分区,没有被使用
[root@rocky8 ~]# vim /etc/fstab
UUID=9a3b3cde-e94d-4f62-9c7e-069d2e3e400b swap swap pri=100 0 0
#pre=100 就是设置优先级,设置的正数优先级,高于系统默认的负数优先级
[root@rocky8 ~]# swapoff /dev/sdb5
[root@rocky8 ~]# swapon -a
[root@rocky8 ~]# swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -2
/dev/sdb5 partition 2097148 0 100
#删除swap
[root@rocky8 ~]# swapoff /dev/sdb5 #先关闭/dev/sdb5,swap
[root@rocky8 ~]# vim /etc/fstab
#删除下面内容
UUID=9a3b3cde-e94d-4f62-9c7e-069d2e3e400b swap swap pri=100 0 0
[root@rocky8 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
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-3,5,6, default 6): 5
Partition 5 has been deleted.
Command (m for help): w
The partition table has been altered.
Syncing disks.
#删除分区
[root@rocky8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part /mnt/sdb1
├─sdb2 8:18 0 2G 0 part
├─sdb3 8:19 0 1K 0 part
└─sdb5 8:21 0 3G 0 part
sdc 8:32 0 10G 0 disk
├─sdc1 8:33 0 1G 0 part
└─sdc2 8:34 0 2G 0 part
sr0 11:0 1 9.2G 0 rom
nvme0n1 259:0 0 10G 0 disk
[root@rocky8 ~]# swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -2
[root@rocky8 ~]# free -h
total used free shared buff/cache available
Mem: 782Mi 181Mi 333Mi 5.0Mi 267Mi 467Mi
Swap: 2.0Gi 0B 2.0Gi
[root@rocky8 ~]# swapoff /dev/sda5 #把/dev/sda5正在使用的挪走
[root@rocky8 ~]# free -h
total used free shared buff/cache available
Mem: 782Mi 179Mi 336Mi 5.0Mi 266Mi 468Mi
Swap: 0B 0B 0B
#现在内存使用变大,swap分区没有了
2.4.2 交换分区实现过程
- 创建交换分区或者文件
- 使用mkswap写入特殊签名
- 在/etc/fstab文件中添加适当的条目
- 使用swapon -a 激活交换空间
启用swap分区
swapon [OPTION]... [DEVICE]
选项:
-a:激活所有的交换分区
-p PRIORITY:指定优先级,也可在/etc/fstab 在第4列指定:pri=value
范例:创建swap分区
[root@rocky8 ~]# echo -e 'n\np\n\n\n+2G\nt\n82\nw\n' | fdisk /dev/sdc
[root@rocky8 ~]# mkswap /dev/sdc1
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=d3140a7a-65b7-4cb7-8a2b-12d38aa98c6f
[root@rocky8 ~]# blkid /dev/sdc1
/dev/sdc1: UUID="d3140a7a-65b7-4cb7-8a2b-12d38aa98c6f" TYPE="swap"
PARTUUID="b094d43d-01
[root@rocky8 ~]# vim /etc/fstab
UUID=d3140a7a-65b7-4cb7-8a2b-12d38aa98c6f swap swap defaults,pri=100 0
0
[root@rocky8 ~]# swapon -a
[root@rocky8 ~]# free -h
total used free shared buff/cache available
Mem: 3.7Gi 264Mi 3.2Gi 9.0Mi 261Mi 3.2Gi
Swap: 4.0Gi 0B 4.0Gi
[root@rocky8 ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -2
/dev/sdc1 partition 2097148 0 100
禁用swap分区:
swapoff [OPTION]... [DEVICE]
范例:禁用swap分区
[root@rocky8 ~]# sed -i.bak '/swap/d' /etc/fstab
[root@rocky8 ~]# swapoff -a
SWAP的优先级
可以指定swap分区0到32767的优先级,值越大优先级越高
如果用户没有指定,那么核心会自动给swap指定一个优先级,这个优先级从-1开始,每加入一个新的没有用户指定优先级的swap,会给这个优先级减一
先添加的swap的缺省优先级比较高,除非用户自己指定一个优先级,而用户指定的优先级(是正数)永远高于核心缺省指定的优先级(是负数)
2.4.3 swap的使用策略
/proc/sys/vm/swappiness 的值决定了当内存占用达到一定的百分比时,会启用swap分区的空间
范例:
[root@centos8 ~]# cat /proc/sys/vm/swappiness
30
[root@rhel5 ~]# cat /proc/sys/vm/swappiness
60
说明:内存在使用到100-30=70%的时候,就开始出现有交换分区的使用。简单地说这个参数定义了系统对swap的使用倾向,默认值为30,值越大表示越倾向于使用swap。可以设为0,这样做并不会禁止对swap的使用,只是最大限度地降低了使用swap的可能性
范例:
[root@rocky8 ~]# cat /proc/sys/vm/swappiness
30
[root@rocky8 ~]# echo "vm.swappiness=0" >> /etc/sysctl.conf
[root@rocky8 ~]# sysctl -p
vm.swappiness = 0
[root@rocky8 ~]# cat /proc/sys/vm/swappiness
0
2.5 磁盘常见工具
2.5.1 文件系统空间占用等信息的查看工具df
df [OPTION]... [FILE]...
常用选项
-H 以10为单位
-T 文件系统类型
-h human-readable
-i inodes instead of blocks
-P 以Posix兼容的格式输出
范例:
[[root@rocky8 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 382688 0 382688 0% /dev
tmpfs 400580 0 400580 0% /dev/shm
tmpfs 400580 5740 394840 2% /run
tmpfs 400580 0 400580 0% /sys/fs/cgroup
/dev/sda2 104806400 2633380 102173020 3% /
/dev/sda3 52403200 398404 52004796 1% /data
/dev/sda1 1038336 192152 846184 19% /boot
/dev/sdb1 1031836 2821 972491 1% /mnt/sdb1
tmpfs 80116 0 80116 0% /run/user/0
[root@rocky8 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 374M 0 374M 0% /dev
tmpfs 392M 0 392M 0% /dev/shm
tmpfs 392M 5.7M 386M 2% /run
tmpfs 392M 0 392M 0% /sys/fs/cgroup
/dev/sda2 100G 2.6G 98G 3% /
/dev/sda3 50G 390M 50G 1% /data
/dev/sda1 1014M 188M 827M 19% /boot
/dev/sdb1 1008M 2.8M 950M 1% /mnt/sdb1
tmpfs 79M 0 79M 0% /run/user/0
[root@rocky8 ~]# df -Th #-T 显示文件系统
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 374M 0 374M 0% /dev
tmpfs tmpfs 392M 0 392M 0% /dev/shm
tmpfs tmpfs 392M 5.7M 386M 2% /run
tmpfs tmpfs 392M 0 392M 0% /sys/fs/cgroup
/dev/sda2 xfs 100G 2.6G 98G 3% /
/dev/sda3 xfs 50G 390M 50G 1% /data
/dev/sda1 xfs 1014M 188M 827M 19% /boot
/dev/sdb1 ext4 1008M 2.8M 950M 1% /mnt/sdb1
tmpfs tmpfs 79M 0 79M 0% /run/user/0
范例:
[root@rhel5 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
99415464 2084776 92199220 3% /
/dev/sda1 101086 12056 83811 13% /boot
tmpfs 517536 0 517536 0% /dev/shm
[root@rhel5 ~]# df -P
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/mapper/VolGroup00-LogVol00 99415464 2084776 92199220 3% /
/dev/sda1 101086 12056 83811 13% /boot
tmpfs
2.5.2 查看某目录总体空间占用状态du
du [OPTION]... DIR
常用选项
-h human-readable
-s summary
--max-depth=# 指定最大目录层级
-x, --one-file-system #忽略不在同一个文件系统的目录
范例:
[root@rocky8 ~]# du /data
4 /data
[root@rocky8 ~]# echo a>/data/a.txt
[root@rocky8 ~]# du /data
8 /data
[root@rocky8 ~]# du -h /data
8.0K /data
[root@rocky8 ~]# mkdir /data/dir{1,2}
[root@rocky8 ~]# echo b >/data/dir1/b.txt
[root@rocky8 ~]# echo c >/data/dir2/c.txt
[root@rocky8 ~]# du /data
4 /data/dir1
4 /data/dir2
16 /data
[root@rocky8 ~]# du -sh /data
16K /data
#-s 只显示/data目录信息
2.5.3 工具dd
dd 命令:convert and copy a file
格式:
dd if=/PATH/FROM/SRC of=/PATH/TO/DEST bs=# count=#
常用选项
if=file 从所命名文件读取而不是从标准输入
of=file 写到所命名的文件而不是到标准输出
bs=size block size, 指定块大小(既是是ibs也是obs)
cbs=size 一次转化size个byte
skip=blocks 从开头忽略blocks个ibs大小的块
seek=blocks 从开头忽略blocks个obs大小的块
count=n 复制n个bs
conv=conversion[,conversion...] 用指定的参数转换文件
conversion 转换参数:
ascii 转换 EBCDIC 为 ASCII
ebcdic 转换 ASCII 为 EBCDIC
lcase 把大写字符转换为小写字符
ucase 把小写字符转换为大写字符
nocreat 不创建输出文件
noerror 出错时不停止
notrunc 不截短输出文件
sync 把每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐
fdatasync 写完成前,物理写入输出文件
范例:
#skip 是从if 目标输入时,跳过多少个
#seek 是从of 目标输出时,跳过多少个
[root@rocky8 ~]# dd of=/data/a.txt conv=ucase #转换大小写
abc
xyz
0+2 records in
0+1 records out
8 bytes copied, 24.516 s, 0.0 kB/s
#ctrl+d 结束
[root@rocky8 ~]# cat /data/a.txt
ABC
XYZ
[root@rocky8 ~]# cat >f1.txt <<EOF
abcdef
EOF
[root@rocky8 ~]# cat >f2.txt <<EOF
123456789
EOF
[root@rocky8 ~]# dd if=f1.txt of=f2.txt bs=1 count=2 skip=3 seek=4
2+0 records in
2+0 records out
2 bytes copied, 8.4238e-05 s, 23.7 kB/s
[root@rocky8 ~]# cat f2.txt
1234de
# bs=1每次读1个字节,count=2读2次,skip=3 从f1.txt跳过3个,seek=4从f2.txt跳过4个, 输出到f2.txt,f1.txt的最后f被截断了,f2.txt的789也被截断了,所以就没有了,最后结果就是1234de。
[root@rocky8 ~]# cat >f2.txt <<EOF
123456789
EOF
[root@rocky8 ~]# cat f1.txt
abcdef
[root@rocky8 ~]# cat f2.txt
123456789
[root@rocky8 ~]# dd if=f1.txt of=f2.txt bs=1 count=2 skip=3 seek=4 conv=notrunc
2+0 records in
2+0 records out
2 bytes copied, 9.5318e-05 s, 21.0 kB/s
[root@rocky8 ~]# cat f2.txt
1234de789
#conv=notrunc 这就是不截断
[root@rocky8 ~]# dd if=/dev/zero of=/data/disk.img bs=1M count=0 seek=1024
0+0 records in
0+0 records out
0 bytes copied, 3.1539e-05 s, 0.0 kB/s
[root@rocky8 ~]# du /data/disk.img
0 /data/disk.img
#创建的是一个稀疏文件,或者叫空洞文件
范例:
#备份MBR
dd if=/dev/sda of=/tmp/mbr.bak bs=512 count=1
#破坏MBR中的bootloader
dd if=/dev/zero of=/dev/sda bs=64 count=1 seek=446
2.6 练习
1、创建一个2G的文件系统,块大小为2048byte,预留1%可用空间,文件系统ext4,卷标为TEST,要求此分区开机后自动挂载至/test目录,且默认有acl挂载选项
2、写一个脚本,完成如下功能:
(1) 列出当前系统识别到的所有磁盘设备
(2) 如磁盘数量为1,则显示其空间使用信息
否则,则显示最后一个磁盘上的空间使用信息
3、将CentOS6的CentOS-6.10-x86_64-bin-DVD1.iso和CentOS-6.10-x86_64-bin-DVD2.iso两个文件,合并成一个CentOS-6.10-x86_64-Everything.iso文件,并将其配置为yum源