磁盘配额的基础应用
第一节 基本概念
Red Hat系统提供两种磁盘配额的限制方式。
A. 区块配额(Block Quota)
B. 索引节点配额(Inode Quota)
如果同时定义区块配额和索引节点配额,将会以最先到达的限制为准。
不管是区块还是索引节点配额,都有两个级别的限制。
1. 软性限制(Soft Limit)
先给出警告,但允许在限期(默认为7天)内,继续使用文件系统。
2. 硬性限制(Hard Limit)
只要超过Hard Limit,就停止用户使用文件系统。
第二节 建立磁盘配额的步骤
【step 1】修改文件系统的挂载参数
例子:
mount -o usrquota,grpquota /dev/hda8 /mnt/tmp
参数说明:
* usrquota: 启动对某个用户的磁盘配额功能
* grpquota: 启动对群组的磁盘配额功能
如果文件系统已经挂载,则可以用这样的命令来重新挂载:
mount -o remount,usrquota,grpquota /dev/hda8 /mnt/tmp
【step 2】 初始化磁盘配额数据库
初始化磁盘配额数据库就是在目标文件系统的挂载目录里建立数据库文件aquota.user和aquota.group。
[root@airhouse mnt]# mount -o remount,usrquota,grpquota /dev/sdb3 /mnt/tmp
[root@airhouse tmp]# mount | grep sdb3
/dev/sdb3 on /mnt/tmp type ext3 (rw,usrquota,grpquota)
[root@airhouse mnt]# quotacheck -ucg /dev/sdb3
[root@airhouse tmp]# ls -l
total 26
-rw------- 1 root root 6144 2009-07-10 11:40 aquota.group
-rw------- 1 root root 6144 2009-07-10 11:40 aquota.user
drwx------ 2 root root 12288 2009-07-10 11:34 lost+found
执行了命令quotacheck后,在挂载点目录/mnt/tmp里出现了两个二进制文件aquota.group和aquota.user.
【step 3】 启用与停用磁盘配额
磁盘配额默认是停用的状态。
[root@airhouse lvm]# quotaon -p /dev/sdb3
group quota on /mnt/tmp (/dev/sdb3) is off
user quota on /mnt/tmp (/dev/sdb3) is off
参数-p表示查看磁盘配额功能是否开启。
[root@airhouse lvm]# quotaon /dev/sdb3
[root@airhouse lvm]# quotaon -p /dev/sdb3
group quota on /mnt/tmp (/dev/sdb3) is on
user quota on /mnt/tmp (/dev/sdb3) is on
通过下面的命令关闭它。
[root@airhouse lvm]# quotaoff /dev/sdb3
[root@airhouse lvm]# quotaon -p /dev/sdb3
group quota on /mnt/tmp (/dev/sdb3) is off
user quota on /mnt/tmp (/dev/sdb3) is off
【step 4】 设置磁盘配额的参数
有两个命令可以用来设置磁盘配额的参数。它们是:
1)edquota
2)setquota
这两个命令的区别仅在于,edquota利用$EDITOR所设置的文字编辑器来帮助用户设置参数,而setquota则将参数带在命令行中。
我们下面举的例子是使用setquota来设置磁盘配额中的软性限制和硬性限制。
[root@airhouse lvm]# tune2fs -l /dev/sdb3 | grep 'Block'
Block count: 498012
Block size: 1024
Blocks per group: 8192
可见,块的大小是1KB。
使用下面的命令设置区块配额如下:
软性限制:2048*1KB=2MB
硬性限制:3072*1KB=3MB
索引节点配额如下:
软性限制:5个
硬性限制:10个
[root@airhouse lvm]# setquota -u flagonxia 2048 3072 5 10 /dev/sdb3
a)试验区块配额的有效性
[flagonxia@airhouse tmp]$ dd if=/dev/zero of=/mnt/tmp/file bs=1M count=4
dd: writing `/mnt/tmp/file': Disk quota exceeded
3+0 records in
2+0 records out
3129344 bytes (3.1 MB) copied, 0.0411066 s, 76.1 MB/s
[flagonxia@airhouse tmp]$ ls -l
total 3100
-rw------- 1 root root 7168 2009-07-10 13:08 aquota.group
-rw------- 1 root root 7168 2009-07-10 13:07 aquota.user
-rw-rw-r-- 1 flagonxia flagonxia 3129344 2009-07-10 13:08 file
drwx------ 2 root root 12288 2009-07-10 11:34 lost+found
拷贝4M的文件,得到错误信息:dd: writing `/mnt/tmp/file': Disk quota exceeded。结果,只拷贝了3M的文件。
b)试验索引节点配额的有效性
[flagonxia@airhouse tmp]$ rm file
删除file,否则我们服务再新建文件。
[flagonxia@airhouse tmp]$ for i in $(seq 1 11); do touch /mnt/tmp/file$i && echo "Create file$i"; done
Create file1
Create file2
Create file3
Create file4
Create file5
Create file6
Create file7
Create file8
Create file9
Create file10
touch: cannot touch `/mnt/tmp/file11': Disk quota exceeded
系统不允许我们拷贝第11个文件,因为超出了索引的硬性限制10.
使用setquota还能修改区块配额和索引配额的限期。如:
[root@airhouse mail]# setquota -t 432000 432000 /dev/sdb3
[root@airhouse mail]# edquota -t
Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
Filesystem Block grace period Inode grace period
/dev/mapper/xboxVG-xboxLV 7days 7days
/dev/sdb3 5days 5days
注意,setquota是以"秒"为单位。
第三节 生成磁盘配额报表
[root@airhouse tmp]# repquota -a
*** Report for user quotas on device /dev/mapper/xboxVG-xboxLV
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 26730 0 0 5 0 0
*** Report for user quotas on device /dev/sdb3
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 10544 0 0 4 0 0
flagonxia -+ 10 2048 3072 10 5 10 6days
用户flagonxia边上的+表示该用户已经达到磁盘配额的硬性限制了。
第四节 寄出警告信
还有一个warnquota的工具用来发送邮件来警告已经超出磁盘配额的用户。信件的内容放在/etc/warnquota.conf中定义。