天天看点

centos7 xfs文件系统磁盘限额

linux磁盘限额是个非常有用的技术,可以将存储空间公平的分配给多个用户,防止某一个用户过分使用磁盘空间,导致各种问题。稍微研究了一下,记录一下研究结果。

不同的文件系统磁盘限额方法也不同,结果也不同。研究了一天xfs文件系统的限额方法,得出一点心得分享下,感兴趣的朋友可以看下。

参考:

鸟哥的linux私房菜第十四章、磁碟配額(Quota)與進階檔案系統管理14.1 磁碟配額 (Quota) 的應用與實作

linux就该这么学第6章 存储结构与磁盘划分6.7 磁盘容量配额

首先文件系统必须是 xfs 格式的,查看一下已挂载文件和文件系统格式

centos7 xfs文件系统磁盘限额

也可以通过开机挂载文件查看

[[email protected] /]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan 31 05:02:34 2019
#
# 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/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=ef2e0ad8-5864-4c48-880d-3afe279a2280 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/sdb1       /media/1        xfs     defaults        0 0
/dev/sdb2       /media/2        xfs     defaults        0 0 
/dev/sdb3       /media/3        xfs     defaults        0 0

           

然后就是开启 quota 功能,修改开机挂载文件

centos7 xfs文件系统磁盘限额

接着就是取消挂载,重新挂载 /dev/sdb1。也可以重新启动,效果都是相同的。

centos7 xfs文件系统磁盘限额

也可以用 xfs_quota -x -c “print” 或 xfs_quota -x -c “state” 命令

centos7 xfs文件系统磁盘限额

对目录进行实际的操作

[[email protected] /]# xfs_quota -x -c "limit -u bsoft=20m bhard=30m isoft=3 ihard=5 hezong" /media/1
##对用户组进行操作时,只需要将 -u 参数改为 -g

           

可以查看一下设置之后的情况

[[email protected] /]# xfs_quota -x -c "report -ubih" /dev/sdb1
User quota on /media/1 (/dev/sdb1)
                        Blocks                            Inodes              
User ID      Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace  
---------- --------------------------------- --------------------------------- 
root            0      0      0  00 [------]      3      0      0  00 [------]
hezong          0    20M    30M  00 [------]      0      3      5  00 [------]
##				    bsoft   bhard                      isoft   ihard

           

也可以用 edquota 查看和更改设定值,敲完edquota后直接调用vim编译器,可以对文档内容进行更改。

[[email protected] /]# edquota -u hezong 
Disk quotas for user hezong (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb1                         0      20480      30720          0        3        5
                                            bsoft      bhard               isoft    ihard
           

检验一下结果吧。

[[email protected] /]# chmod 777 /media/1  ##给予其它用户足够权限
[[email protected] /]# su hezong
[[email protected] 1]$ touch 1 2 3   ##新建三个文件没毛病
[[email protected] 1]$ touch 4 5 6
touch: cannot touch ‘6’: Disk quota exceeded   ##不能创建第六个文件
##控制文件个数检验正常
[[email protected] 1]$ ls
1  2  3  4  5    #最终只会有五个文件

           
[[email protected] 1]$ dd if=/dev/zero of=file bs=20M count=1
dd: failed to open ‘file’: Disk quota exceeded
[[email protected] 1]$ rm 1    ##忘记已经有5个文件了,再建就是第六个所以报警。删一个
[[email protected] 1]$ dd if=/dev/zero of=file bs=20M count=1
1+0 records in
1+0 records out
20971520 bytes (21 MB) copied, 0.105474 s, 199 MB/s  ##新建一个20M的文件写入正常
[[email protected] 1]$ dd if=/dev/zero of=file bs=20M count=2
dd: error writing ‘file’: Disk quota exceeded   #设置的限额是30M,这个命令将创建一个40M的文件。结果会报警并且创建一个30M的文件。
2+0 records in
1+0 records out
31457280 bytes (31 MB) copied, 0.101867 s, 309 MB/s
[[email protected] 1]$ ls
2  3  4  5  file
[[email protected] 1]$ du -sh file
30M	file   ##最终这个文件只会是30M

           

文件个数最大能达到设置的ihard,文件的大小最大能达到设置的bhard。

最后还有宽限时间,当硬盘配额达到指定值后会有一个宽限时间,如果日期到了磁盘空间还没有减少,就会锁定该用户的磁盘使用权限。据说还会报警,我用虚拟机重启了好几遍也没有看到报警信息,可能是哪里没搞对吧。先记下

将宽限时间改为10天

[ro[email protected] 1]# xfs_quota -x -c "timer -ugb 10days" /dev/sdb1
           

查看一下效果

[[email protected] Desktop]$ su hezong    ##不要在root下创建文件,否则会出错
[[email protected] Desktop]$ cd /media/1
[[email protected] 1]$ ll -h
total 0
-rw-rw-r--. 1 hezong hezong 0 Jul 13 18:00 2
-rw-rw-r--. 1 hezong hezong 0 Jul 13 18:00 3
-rw-rw-r--. 1 hezong hezong 0 Jul 13 18:00 4
-rw-rw-r--. 1 hezong hezong 0 Jul 13 18:00 5
[[email protected] 1]$ dd if=/dev/zero of=file bs=30M count=1
1+0 records in
1+0 records out
31457280 bytes (31 MB) copied, 0.0753442 s, 418 MB/s
[[email protected] Desktop]$ exit	##记得切换成 root 否则权限不足

[[email protected] Desktop]# xfs_quota -x -c "report -ubih" /media/1
User quota on /media/1 (/dev/sdb1)
                        Blocks                            Inodes              
User ID      Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace  
---------- --------------------------------- --------------------------------- 
root            0      0      0  00 [------]      3      0      0  00 [------]
hezong        30M    20M    30M  00 [9 days]      5      3      5  00 [6 days]


           

好像成功了