You need to enable JavaScript to run this app.
导航
xfs文件系统上对用户进行配额限制
最近更新时间:2024.01.02 11:34:48首次发布时间:2023.07.18 20:39:12

本文将介绍XFS文件系统上的用户配额限制。

前言

本文档介绍如在xfs文件系统上对用户进行quota限制。

关于实验
  • 预计部署时间:30分钟
  • 级别:初级
  • 相关产品:ECS
  • 受众: 通用
实验说明

如果还没有火山引擎账号,点击此链接注册账号
如果还没有云服务器,创建云服务器,参考链接

第一步、环境说明
  • ECS系统:Ubuntu 22.04.1
  • 内核版本:5.15.0-48-generic
第二步、安装xfsprogs
$ apt-get update
$ apt-get install xfsprogs
第三步、格式化磁盘并挂载
  1. 查看磁盘信息
fdisk -l /dev/vdb
Disk /dev/vdb: 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
  1. 格式化
$ mkfs.xfs -f /dev/vdb
  1. 挂载
$ mkdir /data
$ mount -o usrquota /dev/vdb /data
$ mount|grep data
/dev/vdb on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,usrquota)
  1. 查看设置的结果
$ xfs_quota -xc 'state -gpu'
User quota state on /data (/dev/vdb)
  Accounting: ON   # 已经开启
  Enforcement: ON   # 已经开启
  Inode: #131 (1 blocks, 1 extents)
Blocks grace time: [7 days]
Blocks max warnings: 5
Inodes grace time: [7 days]
Inodes max warnings: 5
Realtime Blocks grace time: [7 days]
Group quota state on /data (/dev/vdb)
  Accounting: OFF
  Enforcement: OFF
  Inode: N/A
Blocks grace time: [--------]
Blocks max warnings: 0
Inodes grace time: [--------]
Inodes max warnings: 0
Realtime Blocks grace time: [--------]
Project quota state on /data (/dev/vdb)
  Accounting: OFF
  Enforcement: OFF
  Inode: N/A
Blocks grace time: [--------]
Blocks max warnings: 0
Inodes grace time: [--------]
Inodes max warnings: 0
Realtime Blocks grace time: [--------]
第四步、设置quota
  1. 创建用户并设置密码
$ useradd quota-test
$ echo "quota-test:password" | chpasswd
  1. 对用户 quota-test 进行设置
$ xfs_quota  -xc 'limit -u bsoft=300M bhard=500M quota-test' /data/
  1. 查看设置的结果
$ xfs_quota -xc 'report -ugphbir' /data
User quota on /data (/dev/vdb)
                        Blocks                            Inodes                        Realtime Blocks          
User ID      Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace    Used   Soft   Hard Warn/Grace   
---------- --------------------------------- --------------------------------- --------------------------------- 
root            0      0      0  00 [------]      3      0      0  00 [------]      0      0      0  00 [------]
quota-test      0   300M   500M  00 [------]      0      0      0  00 [------]      0      0      0  00 [------]
第五步、验证
  1. 修改权限否则 quota-test 会因为权限不足写入失败
$ chmod 777 /data
  1. 验证写入 200M 数据,写入成功
$ su - quota1
dd if=/dev/zero of=/data/quota1.txt bs=1M count=200
200+0 records in
200+0 records out
209715200 bytes (210 MB, 200 MiB) copied, 0.0727318 s, 2.9 GB/s

$ ls -lh /data/
total 200M
-rw-rw-r-- 1 quota-test quota-test 200M Feb 26 16:05 quota1.txt
  1. 再写入 100M 数据,也写入成功
$ dd if=/dev/zero of=/data/quota2.txt bs=1M count=200 
200+0 records in
200+0 records out
209715200 bytes (210 MB, 200 MiB) copied, 0.0738258 s, 2.8 GB/s

$ ls -lh /data/
total 400M
-rw-rw-r-- 1 quota-test quota-test 200M Feb 26 16:05 quota1.txt
-rw-rw-r-- 1 quota-test quota-test 200M Feb 26 16:07 quota2.txt
  1. 再次写入 150M 数据,提示超过quota,只写入了 100M的数据
$ dd if=/dev/zero of=/data/quota3.txt bs=1M count=150
dd: error writing '/data/quota3.txt': Disk quota exceeded
101+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0370117 s, 2.8 GB/s

$ ls -lh /data/
total 500M
-rw-rw-r-- 1 quota-test quota-test 200M Feb 26 16:05 quota1.txt
-rw-rw-r-- 1 quota-test quota-test 200M Feb 26 16:07 quota2.txt
-rw-rw-r-- 1 quota-test quota-test 100M Feb 26 16:10 quota3.txt
  1. 查看磁盘quota使用
$ root@iv-yc9exq1dl338df7pjgs8:~# xfs_quota -xc 'report -ugphbir' /data
User quota on /data (/dev/vdb)
                        Blocks                            Inodes                        Realtime Blocks          
User ID      Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace    Used   Soft   Hard Warn/Grace   
---------- --------------------------------- --------------------------------- --------------------------------- 
root            0      0      0  00 [------]      3      0      0  00 [------]      0      0      0  00 [------]
quota-test   500M   300M   500M  00 [6 days]      3      0      0  00 [------]      0      0      0  00 [------]
参考链接

https://manpages.ubuntu.com/manpages/jammy/man8/xfs_quota.8.html