<1>Linux中存储的基本管理


一、设备识别

设备接入系统后都是以文件的形式存在

设备文件名称 使用形式
SATA/SAS/USB /dev/sda,/dev/sdb ##s= SATA, d=DISK a=第几块
IDE /dev/hd0,/dev/hd1 ##h= hard
VIRTIO-BLOCK /dev/vda,/dev/vdb ##v=virtio
M2(SSD) /dev/nvme0,/dev/nvme1 ##nvme=m2
SD/MMC/EMMC(卡) /dev/mmcblk0,/dev/mmcblk1 ##mmcblk=mmc 卡
光驱 /dev/cdrom,/dev/sr0,/dev/sr1

二、设备查看

指令 作用
fdisk -l 查看磁盘分区情况
lsblk 设备使用情况
blkid 设备管理方式及设备 id
df 查看正在被系统挂载的设备
cat /proc/partitions 查看系统识别设备

三、设备挂载

1,临时挂载

在系统中有设备 id 的设备是可以被系统使用的

挂载命令:
[root@localhost ~]# man mount #查看用法
[root@localhost ~]# fdisk -l
[root@localhost ~]# mount -o rw /dev//sdb1 /mnt/  #把U盘挂载到mnt下,mount -o 挂载参数 device 挂载点
[root@localhost ~]# mount -o rw /dev/vda1 /westos
[root@localhost ~]# mount -o remount,ro /westos ##转换挂载参数由读写变为只读
[root@localhost ~]# cd /mnt   #切到目录下
[root@localhost ~]# ls
[root@localhost ~]# umount /mnt  #umount 设备|挂载点。
若切到目录下后取消挂载时显示busy
[root@localhost ~]# fuser -vm /mnt
[root@localhost ~]# fuser -kvm /mnt    #fuser -kvm 设备|挂载点 #-k 结束进程, -v 现实详细信息 -m 显示进程
[root@localhost ~]# umount /mnt

2,永久挂载

[root@localhost ~]# vim /etc/fstab ##设备挂载策略文件
[root@localhost ~]# cat /etc/fstab 
/dev/sr0 /westos iso9660 defaults 0 0  #设备 挂载点 文件系统类型 挂载参数 是否备份 是否检测,可用mount查看。
[root@localhost ~]# mount -a ##因为此文件在编写完成后不会马上生效,需重新读取/etc/fstab 文件
注意:若此文件内容编写错误会导致系统启动失败,按照提示在操作界面输入超级用户密码,注释错误行,再重启系统即可。

四、设备中文件的查找

文件创建及设置:
[root@localhost mnt]# mkdir westosdir
[root@localhost mnt]# touch westosdir/westosfile{1..5}
[root@localhost mnt]# ls -lR
[root@localhost mnt]# useradd lee
[root@localhost mnt]# cd westosdir/
[root@localhost westosdir]# ls
westosfile1  westosfile2  westosfile3  westosfile4  westosfile5
[root@localhost westosdir]# chown lee westosfile1
[root@localhost westosdir]# chgrp lee westosfile2
[root@localhost westosdir]# chown westos.lee westosfile3
[root@localhost westosdir]# chown lee.lee westosfile4
[root@localhost westosdir]# ll
[root@localhost westosdir]# chmod 444 westosfile1
[root@localhost westosdir]# chmod 640 westosfile2
[root@localhost westosdir]# chmod 755 westosfile3
[root@localhost westosdir]# ll
total 0
-r--r--r--. 1 lee    root 0 Nov 14 10:40 westosfile1
-rw-r-----. 1 root   lee  0 Nov 14 10:40 westosfile2
-rwxr-xr-x. 1 westos lee  0 Nov 14 10:40 westosfile3
-rw-r--r--. 1 lee    lee  0 Nov 14 10:40 westosfile4
-rw-r--r--. 1 root   root 0 Nov 14 10:40 westosfile5

各种查找:
[root@localhost westosdir]# find /mnt/ -name "westosfile*"   按名称查找
[root@localhost westosdir]# find /mnt/ -user root   #按用户查找
[root@localhost westosdir]# find /mnt/ -group lee  #按用户组查找
[root@localhost ~]# find /mnt/ -group lee -user root 
[root@localhost ~]# find /mnt/ -group lee -a -user root   ##用户是root,且用户组是lee
[root@localhost ~]# find /mnt/ -group lee -o  -user root ##用户是root,或用户组是lee
[root@localhost westosdir]# find /mnt -type f  #查找文件
[root@localhost westosdir]# find /mnt -type d  #查找目录
[root@localhost westosdir]# find /mnt/ -perm 444  #u只有r,g只有r,o只有r
[root@localhost westosdir]# find /mnt/ -perm /444  #u或g或o有r
[root@localhost westosdir]# find /mnt/ -perm -444 #u和g和o都有r;还可以有其他的
[root@localhost westosdir]# find / -perm -002  #other有r
[root@localhost westosdir]# ll -ld /lib  #是链接
[root@localhost westosdir]# find /  -perm -002 -type l  #查找类型是链接的
[root@localhost westosdir]# find /  -perm -002 -not  -type l  #查找类型不是链接的
[root@localhost westosdir]# find /mnt -perm -004  #o位有r的
[root@localhost westosdir]# find /mnt -maxdepth 1 -perm -004  #最大深度为1,/mnt  #深度为0,/mnt/westosdir为1
[root@localhost westosdir]# find /mnt -mindepth 1 -perm -004#最小深度为1

[root@localhost westosdir]# find /mnt/ -cmin 1  #查找小于1
[root@localhost westosdir]# find /mnt/ -cmin -1
[root@localhost westosdir]# find /mnt/ -cmin +1

[root@localhost westosdir]# find / -size 100M 
[root@localhost westosdir]# find / -size -100M   #查找大小小于100M的文件
[root@localhost westosdir]# find / -size +100M   #查找大小大于100M的文件
[root@localhost ~]# du -sh /usr/lib64/firefox/libxul.so

[root@localhost mnt]# chmod a+w /mnt/westosdir/file1  #把文件的ugo位都加上w
[root@localhost mnt]# find /mnt/ -perm -002
[root@localhost mnt]# find /mnt/ -perm -002 -exec chmod o-w {} \;  #对照到的所有文件进行操作o-w

在这里插入图片描述

五、磁盘分区

1、常规分区

mbr分区方式:
分区 作用
主分区 主分区表记录分区的信息并可以直接使用的分区
扩展分区 主分区表记录的分区,不可直接使用,只是逻辑分区容器
逻辑分区 扩展分区之上划分的分区叫做逻辑分区
mbr分区方式下的分区方法:
分区 作用
fdisk /dev/vdb(磁盘名称,不一定是vdb) 交互模式下分区
parted 非交互和交互模式下分区(采用parted分区的话一般采用非交互模式,交互模式下相对比较繁琐)
交互式:
[root@localhost mnt]# fdisk /dev/vdb  #步骤如下图
[root@localhost mnt]# udevadm settle   #同步分区表,或partprobe
[root@localhost mnt]# cat /proc/partitions 
[root@localhost mnt]# partx -d /dev/sdb ##清理分区表
[root@localhost mnt]# partx -a /dev/sdb ##重新加载分区表

在这里插入图片描述

非交互式:
[root@localhost ~]# fdisk -l
[root@localhost ~]# dd if=/dev/zero of=/dev/vdb bs=1M count=1
[root@localhost ~]#   parted -l
[root@localhost ~]#   parted /dev/vdb help
[root@localhost ~]#   parted /dev/vdb mklabel msdos  
[root@localhost ~]#   parted -l   #有了磁盘msdos
[root@localhost ~]#   parted /dev/vdb mkpart primary 1 100  #建立第一个分区从1到101
[root@localhost ~]#   fdisk -l  #第一个分区从1到101被建立
[root@localhost ~]#   parted /dev/vdb mkpart primary 101 1101#建立第二个分区从101到1101
[root@localhost ~]#   fdisk -l  #第二个分区从101到1101被建立
[root@localhost ~]#   parted /dev/vdb help   
[root@localhost ~]#   parted /dev/vdb rm 2
[root@localhost ~]#   fdisk -l
[root@localhost ~]#   parted /dev/vdb rm 1
[root@localhost ~]#   fdisk -l

其中:

指令 作用
d 删除
l 列出所有分区类型
n 新建
p 显示分区表
t 更改分区类型
w 保存更改
q 退出
g 设定分区方式为 GPT
o 设定分区方式为 mbr

分区之后,分出来的磁盘区还不能直接挂载到指定目录下,因为缺少文件系统,此时需要格式化指定文件系统:

[root@localhost mnt]# mkfs.xfs -K /dev/vdb1 ##格式化设备为 xfs 文件系统(相当与在/dev/sda1 上安装设备管理软件),-K 不丢弃空数据块
[root@localhost mnt]# mount /dev/vdb1 /mnt/westos ##mount 命令挂在为临时挂在,如果需要永久挂在需要编写/etc/fstab
mbr分区方式转换成GPT步骤%
[root@localhost ~]#   parted /dev/磁盘名	
(parted) mklabel
New disk label type? gpt
Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost.
Do you want to continue?
Yes/No? yes
(parted) quit
[root@localhost ~]#  fdisk /dev/  vdb1  #在RHEL8中可以直接使用fdisk /dev/磁盘名	
 g		%g表示把/dev/磁盘名 设备的分区方式调整为GPT

2、swap分区

作用:程序在运行时所有数据是在RAM,当RAM使用量超过了限额,为了使系统更加稳定,我们在硬盘上划分一部分空间来作内存缓冲区swap。当内存使用超过限额,内核会把内存中闲置的数据存放到swap中。当程序需要swap分区中的数据时内核将swap分区中的数据在交还给内存进程处理。

[root@localhost ~]#  swapon -s	             %查看swap分区信息
创建swap分区:
[root@localhost ~]#  fdisk /dev/vdb/   #创建分区并设定分区的类型为Linuxswap,用t。l查看
[root@localhost ~]#  mkswap	 /dev/磁盘名  #格式化设备为swap格式
[root@localhost ~]#  swapon	 /dev/磁盘名	 -p 0-32767		#-p表示指定swap的优先级
以上操作都位临时操作,如果永久添加swap分区则需要编辑下面的文件:
[root@localhost ~]#  vim /etc/fstab
[root@localhost ~]#  cat /etc/fstab
/dev/磁盘名		swap	swap	    default,pri=1	0 0

[root@localhost ~]#  swapon -a

删除swap
[root@localhost ~]#  vim /etc/fstab
 /dev/sda1  swap    swap        default,pri=1   0 0	 #删除此行
[root@localhost ~]#  swapoff /dev/磁盘名	

3、设备删除

方式 特点
dd if=/dev/zero of=/dev/磁盘名 bs=1M count=1 一次性删除所有分区
fdisk /dev/磁盘名 在交互模式下删除,选d,之后选择删除的分区号
parted 非交互模式下删除,使用rm加指定分区号

六、磁盘配额

作用:设定用户能写入指定设备的最大额度

具体步骤:
1)分出来一个区,并格式化文件系统
2)做配额设定:
[root@localhost ~]#  mount /dev/vdb1	 /pub/ -o usrquota	#挂载设备并激活配额参数
[root@localhost ~]#  quotaon  -uv /dev/vdb1	 			#激活配额
[root@localhost ~]#  edquota -u lee				#设定用户lee配额
编辑内容:
Disk quotas for user lee (uid 1001):   #设备     用户已经创建数据 软限   硬限  户已经创文件个数 软限    硬限
Filesystem   blocks    soft   hard   inodes       soft    hard
/dev/vdb1	   0     0     20480       0            0       0
3)永久开启配额
[root@localhost ~]#  vim /etc/fstab
/dev/vdb1	/pub	xfs	defaults,usrquota    0 0
4)关闭配额
[root@localhost ~]#  quotaoff  -uv /dev/vdb1
[root@localhost ~]#  vim   /etc/fstab						#去掉配额参数usrquota

猜你喜欢

转载自blog.csdn.net/qiao_qing/article/details/109786028
今日推荐