1 理论基础
1.1 Quota的概念
Quota即限额的意思,用来限制用户、组、文件夹的空间使用量。
1.2 用途范例
- web服务器控制站点可用空间大小
- mail服务器控制用户可用空间大小
- file服务器控制用户可用空间大小
1.3 限制模式
- 根据用户(UID)控制每个用户的可用空间大小
- 根据组(GID)控制每个组的可用空间大小
- 根据目录(directory,project)控制每个目录的可用空间大小(xfs可用project模式)
1.4 使用条件
- EXT格式只能对文件系统进行限制,xfs可用对project进行限制
- 内核需要预开启对Quota支持
- Quota限制只对非管理员有效
- 默认只开启对/home使用Quota,其他需要配置SELinux
1.5 限制的可配置对象
- 根据用户(User)、组(Group)、特定目录(project)
- 容量限制或文件数量限制(block/inode)
- 限制值soft(超过空间用量给予警告和宽限时间)和hard(超过空间用量则剥夺用户使用权)
- 宽限时间(grace time),空间用量超出soft限定而未达到hard限定给予的处理时限(超出时限soft值变成hard值)
2 实际操作
2.1 配置前准备
2.1.1 建立用户组
1
|
groupadd gp1
|
2.1.2 添加组成员
1
2
3
4
5
|
useradd
-g gp1 user1
echo
"pwd1"
|
passwd
--stdin user1
useradd
-g gp1 user2
echo
"pwd1"
|
passwd
--stdin user2
|
2.1.2 创建用户目录并变更所有组
1
2
3
|
mkdir
/home/gp1
chgrp
gp1
/home/gp1
chmod
2770
/home/gp1
|
2.1.2 检查文件系统类型
1
|
df
-hT
/home
|
显示如下:
1
2
|
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-home
xfs 5.0G 67M 5.0G 2%
/home
|
2.2 启用文件系统的quota功能
2.2.1 编辑fstab
1
|
vim
/etc/fstab
|
修改内容如下:
1
|
/dev/mapper/centos-home
/home
xfs defaults,usrquota,grpquota 0 0
|
注,类型如下:
- 根据用户(uquota/usrquota/quota)
- 根据组(gquota/grpquota)
- 根据目录(pquota/prjquota)(不能与grpquota同时设定)
2.2.2 卸载并重新挂载
1
2
|
umount
/home
mount
-a
|
2.2.3 检查
1
|
mount
|
grep
home
|
显示如下:
1
|
/dev/mapper/centos-home
on
/home
type
xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)
|
2.3 查阅Quota信息
2.3.1 命令格式
1
2
3
4
5
6
7
8
9
|
xfs_quota -x -c
"子命令"
[挂载点]
选项:
-x :专家模式,使用-x才能使用-c
-c :子命令选项
子命令:
print :列出当前系统参数等
df
:类似于
df
,选项有-b (block) -i (inode) -h (加上單位)等
report:列出
quota
项目,包含-ugr (user
/group/project
)和-bi等
state :列出当前支持
quota
文件系统信息和相关的启动项
|
2.3.2 查询支持Quota的分区
1
|
xfs_quota -x -c
"print"
|
2.3.3 查询Quota目录的使用情况
1
|
xfs_quota -x -c
"df -h"
/home
|
2.3.4 显示用户的Quota的限制信息
1
|
xfs_quota -x -c
"report -ubih"
/home
|
注,显示项目加参数“-u”
2.4 配置限制
2.4.1 命令格式:
1
2
3
4
5
6
7
8
|
xfs_quota -x -c
"limit [-ug] b[soft|hard]=N i[soft|hard]=N name"
xfs_quota -x -c
"timer [-ug] [-bir] Ndays"
选项:
limit :限制的对象,包括user
/group/project
bsoft
/bhard
: block的soft
/hard
限制值
isoft
/ihard
: inode的soft
/hard
限制值
name : 用户和组的名称
timer :宽限时间(grace
time
)
|
2.4.2 根据用户和块大小限制
1
2
|
xfs_quota -x -c
"limit -u bsoft=250M bhard=300M user1"
/home
xfs_quota -x -c
"limit -u bsoft=250M bhard=300M user2"
/home
|
检查配置:
1
|
xfs_quota -x -c
"report -ubih"
/home
|
2.4.3 根据组和块大小限制
1
|
xfs_quota -x -c
"limit -g bsoft=950M bhard=1G gp1"
/home
|
检查配置:
1
|
xfs_quota -x -c
"report -gbih"
/home
|
2.4.5 配置宽限时间
1
|
xfs_quota -x -c
"timer -ug -b 14days"
/home
|
验证配置:
1
|
xfs_quota -x -c
"state"
/home
|
2.4.6 验证Quta
1
2
3
4
5
|
su
- user1
dd
if
=
/dev/zero
of=123.img bs=1M count=310
ll -h
exit
xfs_quota -x -c
"report -ubh"
/home
|
2.5 根据project限制
2.5.1 修改fstab
1
|
vim
/etc/fstab
|
2.5.2 卸载挂载并重新挂载
1
2
|
umount
/home
mount
-a
|
2.5.3 检查取消
1
|
xfs_quota -x -c
"state"
|
2.5.4 创建专案存储位置
1
|
mkdir
/home/proj01
|
2.5.5 指定项目识别号
1
|
echo
"01:/home/proj01"
>>
/etc/projects
|
2.5.6 指定项目名称并关联项目识别号
1
|
echo
"proj01:01"
>>
/etc/projid
|
2.5.7 初始化项目名称
1
|
xfs_quota -x -c
"project -s proj01"
|
检查配置:
1
2
|
xfs_quota -x -c
"print "
/home
xfs_quota -x -c
"report -pbih "
/home
|
2.5.8 根据块大小配置限制
1
|
xfs_quota -x -c
"limit -p bsoft=450M bhard=500M proj01"
/home
|
检查配置:
1
|
xfs_quota -x -c
"report -pbih "
/home
|
2.5.9 验证配置
1
|
dd
if
=
/dev/zero
of=
/home/myquota/123
.img bs=1M count=510
|
2.6 Quota的管理
2.6.1 临时禁用Quota限制
1
|
xfs_quota -x -c
"disable -up"
/home
|
检查禁用:
1
|
xfs_quota -x -c
"state"
/home
|
验证禁用:
1
|
dd
if
=
/dev/zero
of=
/home/user1/123
.img bs=1M count=520
|
查阅Quota状态:
1
|
xfs_quota -x -c
"report -pbh"
/home
|
清理测试文件:
1
|
rm
-rf
/home/user1/123
.img
|
2.6.2 临时启动Quota限制
1
|
xfs_quota -x -c
"enable -up"
/home
|
检验启动:
1
|
dd
if
=
/dev/zero
of=
/home/user1/123
.img bs=1M count=520
|
2.6.3 完全关闭Quota限制
1
|
xfs_quota -x -c
"off -up"
/home
|
测试关闭:
1
|
xfs_quota -x -c
"enable -up"
/home
|
恢复关闭:
1
|
umount
/home
;
mount
-a
|
2.6.4 删除Quota限制(无法恢复)
1
2
|
xfs_quota -x -c
"off -up"
/home
xfs_quota -x -c
"remove -p"
/home
|
验证删除:
1
|
xfs_quota -x -c
"report -phb"
/home
|
2.7 利用软连接实现Quota
1)启用/home分区的Quota功能
编辑fstab
1
|
vim
/etc/fstab
|
修改内容如下:
1
|
/dev/mapper/centos-home
/home
xfs defaults,usrquota,grpquota 0 0
|
2)创建软连接到Quota分区
1
|
ln
-s
/home/mail
/var/spool/mail
|
-------------------------------------------------------------
参阅文档
-------------------------------------------------------------
http://linux.vbird.org/linux_basic/0420quota.php