Linux XFS 磁盘配额限制

发布时间: 更新时间: 总字数:2050 阅读时间:5m 作者: IP上海 分享 网址

Linux 系统使用磁盘时,有需要对磁盘容量和 inode 限制的需求,下面介绍如何对 XFS 磁盘做限制

作用范围

  • 类型:XFSEXT4 等磁盘分区生效
  • 范围:指定用户、组生效,如果是用户组,组内所有用户的磁盘总和不能超过限制
  • 类型:磁盘容量、文件数(Inode)
  • 方式:软限制、硬限制。硬限大于等于软限制,否者软限制失效

命令

xfs_quotaxfsprogs 提供,安装命令:

yum install xfsprogs -y

help

$ man xfs_quota
NAME
       xfs_quota - manage use of quota on XFS filesystems

SYNOPSIS
       xfs_quota [ -x ] [ -p prog ] [ -c cmd ] ... [ -d project ] ... [ path ... ]
       xfs_quota -V
...
       limit [ -g | -p | -u ] bsoft=N | bhard=N | isoft=N | ihard=N | rtbsoft=N | rtbhard=N -d | id | name
              Set quota block limits (bhard/bsoft), inode count limits (ihard/isoft) and/or realtime  block  limits
              (rtbhard/rtbsoft).  The  -d option (defaults) can be used to set the default value that will be used,
              otherwise a specific user/group/project name or numeric identifier must be specified.
xfs_quota -x -c 'limit [-ug] bsoft=N bhard=N isoft=N ihard=N <name>' <mount-point>

说明:

  • -x 专家模式,允许对配额系统进行修改
  • -c 子命令选项,依赖专家模式
  • 子命令:
    • df -b (block) -i (inode) -h
    • timer 宽限时间
    • limit
      • -u:对用户限制
      • -g:对组限制
      • bsoft:磁盘容量软限制
      • bhard:磁盘容量硬限制
      • isoft:文件数量软限制
      • ihard:文件数量硬限制
    • report 列出quota项目
      • -u:对用户查看
      • -g:对组查看
      • -a:查看所有可用分区的配额使用报告
      • -b:查看磁盘容量
      • -i:查看文件数
    • print 当前系统参数
    • state 列出当前系统信息和相关的启动项

其他命令:

xfs_quota -x -c "timer [-ug] [-bir] Ndays"
xfs_quota -x -c 'df'
xfs_quota -x -c 'df -b'
xfs_quota -x -c 'df -b -i'
xfs_quota -x -c 'df -b -i -h'
xfs_quota -x -c 'print'
xfs_quota -x -c 'report'
xfs_quota -x -c 'report -ugr' <mount-point>  # user/group/project
xfs_quota -x -c 'report -abi' <mount-point>
xfs_quota -x -c 'state'

示例

初始化用户、组

  • 建立用户组
groupadd tg
  • 添加组成员
useradd -g tg user1
echo "pwd1" | passwd --stdin user1

useradd -g tg user2
echo "pwd1" | passwd --stdin user2
  • 创建用户目录并变更所有组
mkdir /home/tg
chgrp tg /home/tg
chmod 2770 /home/tg

磁盘挂载

  • 格式化
mkfs.xfs /dev/sdb
  • 修改 /etc/fstab
/dev/sdb /home    xfs    defaults,usrquota,grpquota    0 0

说明:

  • uquota/usrquota/quota 用户配额限制

  • gquota/grpquota 组配额限制

  • pquota/prjquota 目录配额限制,不能与 grpquota 同时设定

  • 重启或重新挂载,挂载后信息如下:

$ mount -a
$ mount | grep home
/dev/sdb on /home type xfs (rw,relatime,attr2,inode64,usrquota,grpquota)

Quota 限制

状态信息

# 查询支持Quota的分区
$ xfs_quota -x -c "print"
Filesystem          Pathname
...
/home               /dev/sdb (uquota, gquota)

# 查询Quota目录的使用情况
$ xfs_quota -x -c "df -h" /home
Filesystem     Size   Used  Avail Use% Pathname
/dev/sdb      20.0G  32.4M  20.0G   0% /home

# 显示用户的Quota的限制信息
$ xfs_quota -x -c "report -ubih" /home
User quota on /home (/dev/sdb)
                        Blocks                            Inodes
User ID      Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace
---------- --------------------------------- ---------------------------------
root            0      0      0  00 [------]      4      0      0  00 [------]
user1         12K      0      0  00 [------]      4      0      0  00 [------]
user2         12K      0      0  00 [------]      4      0      0  00 [------]

配置限制

# 根据用户和块大小限制
$ xfs_quota -x -c "limit -u bsoft=50M bhard=100M user1" /home
$ xfs_quota -x -c "limit -u bsoft=50M bhard=100M user2" /home

# 检查配置
$ xfs_quota -x -c "report -ubih" /home
User quota on /home (/dev/sdb)
                        Blocks                            Inodes
User ID      Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace
---------- --------------------------------- ---------------------------------
root            0      0      0  00 [------]      4      0      0  00 [------]
user1         12K    50M   100M  00 [------]      4      0      0  00 [------]
user2         12K    50M   100M  00 [------]      4      0      0  00 [------]

# 根据组和块大小限制
$ xfs_quota -x -c "limit -g bsoft=120M bhard=1G tg" /home

# 检查配置
$ xfs_quota -x -c "report -gbih" /home
Group quota on /home (/dev/sdb)
                        Blocks                            Inodes
Group ID     Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace
---------- --------------------------------- ---------------------------------
root            0      0      0  00 [------]      3      0      0  00 [------]
tg            24K   120M     1G  00 [------]      9      0      0  00 [------]

$ xfs_quota -x -c "state" /home
User quota state on /home (/dev/sdb)
  Accounting: ON
  Enforcement: ON
  Inode: #67 (2 blocks, 2 extents)
Group quota state on /home (/dev/sdb)
  Accounting: ON
  Enforcement: ON
  Inode: #68 (2 blocks, 2 extents)
Project quota state on /home (/dev/sdb)
  Accounting: OFF
  Enforcement: OFF
  Inode: #68 (2 blocks, 2 extents)
Blocks grace time: [7 days]
Inodes grace time: [7 days]
Realtime Blocks grace time: [7 days]

# 配置宽限时间
$ xfs_quota -x -c "timer -bir -g 14days" /home
$ xfs_quota -x -c "timer -bir -u 14days" /home

# 验证配置
$ xfs_quota -x -c "state" /home
User quota state on /home (/dev/sdb)
  Accounting: ON
  Enforcement: ON
  Inode: #67 (2 blocks, 2 extents)
Group quota state on /home (/dev/sdb)
  Accounting: ON
  Enforcement: ON
  Inode: #68 (2 blocks, 2 extents)
Project quota state on /home (/dev/sdb)
  Accounting: OFF
  Enforcement: OFF
  Inode: #68 (2 blocks, 2 extents)
Blocks grace time: [14 days]
Inodes grace time: [14 days]
Realtime Blocks grace time: [14 days]

验证配额

# 写 80M,功能正常
$ su - user1
$ dd if=/dev/zero of=data bs=1M count=80
80+0 records in
80+0 records out
83886080 bytes (84 MB) copied, 0.113732 s, 738 MB/s
$ ls -lhrt
total 80M
-rw-r--r-- 1 user1 tg 80M Oct  4 08:55 data
$ exit

# 查看配额
$ xfs_quota -x -c "report -ubh" /home
User quota on /home (/dev/sdb)
                        Blocks
User ID      Used   Soft   Hard Warn/Grace
---------- ---------------------------------
root            0      0      0  00 [0 days]
user1       80.0M    50M   100M  00 [13 days]
user2         12K    50M   100M  00 [------]

# 在写 80M,写入失败
$ dd if=/dev/zero of=data2 bs=1M count=80
dd: error writing ‘data2’: Disk quota exceeded
20+0 records in
19+0 records out
19922944 bytes (20 MB) copied, 0.0488839 s, 408 MB/s
$ ls -lhrt
total 99M
-rw-r--r-- 1 user1 tg 80M Oct  4 08:56 data
-rw-r--r-- 1 user1 tg 19M Oct  4 08:56 data2

Project 限制

配置 fstab

  • 修改 /etc/fstab
/dev/sdb /home    xfs    defaults,usrquota,pquota    0 0
  • 卸载挂载并重新挂载
$ umount /home
$ mount -a
$ mount -l | grep home
/dev/sdb on /home type xfs (rw,relatime,attr2,inode64,usrquota,prjquota)
  • 检查取消
$ xfs_quota -x -c "state"
User quota state on /home (/dev/sdb)
  Accounting: ON
  Enforcement: ON
  Inode: #67 (2 blocks, 2 extents)
Group quota state on /home (/dev/sdb)
  Accounting: OFF
  Enforcement: OFF
  Inode: #68 (2 blocks, 2 extents)
Project quota state on /home (/dev/sdb)
  Accounting: ON
  Enforcement: ON
  Inode: #68 (2 blocks, 2 extents)
Blocks grace time: [14 days]
Inodes grace time: [14 days]
Realtime Blocks grace time: [14 days]

限制和验证

# 创建目录
$ mkdir /home/proj

# 设置项目识别号,不重复原则
$ echo "01:/home/proj" >> /etc/projects

# 指定项目名称并关联项目识别号
$ echo "proj:01" >> /etc/projid

# 初始化项目名称
$ xfs_quota -x -c "project -s proj"
Setting up project proj (path /home/proj)...
Processed 1 (/etc/projects and cmdline) paths for project proj with recursion depth infinite (-1).
Setting up project proj (path /home/proj)...
Processed 1 (/etc/projects and cmdline) paths for project proj with recursion depth infinite (-1).
Setting up project proj (path /home/proj)...
Processed 1 (/etc/projects and cmdline) paths for project proj with recursion depth infinite (-1).

# 检查配置
$ xfs_quota -x -c "print " /home
Filesystem          Pathname
/home               /dev/sdb (uquota, pquota)
/home/proj          /dev/sdb (project 1, proj)
$ xfs_quota -x -c "report -pbih " /home
xfs_quota -x -c "report -pbih " /home
Project quota on /home (/dev/sdb)
                        Blocks                            Inodes
Project ID   Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace
---------- --------------------------------- ---------------------------------
#0          99.0M      0      0  00 [------]     15      0      0  00 [------]
proj            0      0      0  00 [------]      1      0      0  00 [------]

# 根据块大小配置限制
$ xfs_quota -x -c "limit -p bsoft=80M bhard=100M proj" /home

# 检查配置
$ xfs_quota -x -c "report -pbih" /home
Project quota on /home (/dev/sdb)
                        Blocks                            Inodes
Project ID   Used   Soft   Hard Warn/Grace     Used   Soft   Hard Warn/Grace
---------- --------------------------------- ---------------------------------
#0          99.0M      0      0  00 [------]     15      0      0  00 [------]
proj            0    80M   100M  00 [------]      1      0      0  00 [------]


# 验证配置
$ dd if=/dev/zero of=/home/proj/data bs=1M count=80
80+0 records in
80+0 records out
83886080 bytes (84 MB) copied, 0.062301 s, 1.3 GB/s
$ dd if=/dev/zero of=/home/proj/data2 bs=1M count=80
dd: error writing ‘/home/proj/data2’: No space left on device
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0247199 s, 848 MB/s

Quota 管理

临时禁用Quota

$ xfs_quota -x -c "disable -up" /home

# 检查禁用
$ xfs_quota -x -c "state" /home
User quota state on /home (/dev/sdb)
  Accounting: ON
  Enforcement: OFF
  Inode: #67 (2 blocks, 2 extents)
Group quota state on /home (/dev/sdb)
  Accounting: OFF
  Enforcement: OFF
  Inode: #68 (2 blocks, 2 extents)
Project quota state on /home (/dev/sdb)
  Accounting: ON
  Enforcement: OFF
  Inode: #68 (2 blocks, 2 extents)
Blocks grace time: [14 days]
Inodes grace time: [14 days]
Realtime Blocks grace time: [14 days]

# 验证禁用
$ dd if=/dev/zero of=/home/user1/data1 bs=1M count=120
120+0 records in
120+0 records out
125829120 bytes (126 MB) copied, 0.284074 s, 443 MB/s

# 查阅Quota状态
$ xfs_quota -x -c "report -pbh" /home
Project quota on /home (/dev/sdb)
                        Blocks
Project ID   Used   Soft   Hard Warn/Grace
---------- ---------------------------------
#0         355.0M      0      0  00 [------]
proj         100M    80M   100M  00 [------]

临时启动 Quota

$ xfs_quota -x -c "enable -up" /home

# 检验启动
$ su - user1
$ dd if=/dev/zero of=/home/user1/data1 bs=1M count=120
dd: error writing ‘/home/user1/data1’: Disk quota exceeded
1+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000681961 s, 0.0 kB/s

删除Quota限制

xfs_quota -x -c "off -up" /home
xfs_quota -x -c "remove -p" /home
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数