磁碟配额 - Quota

需要套件
  • quota

系统需求

  • Kernel Support

线上资源

  • man quota
  • man quotacheck
  • man edquota

建置 Quota 时,最好把要做配额的目录使用一个分割区 mount 起来,原因是因为 quota 是以一个 file system 为单位,不是以目录为单位的去做配置。

Quota 能做到的范围

  • soft limit - 软性限制
  • hard limit - 硬性限制
  • inode - 档案数限制

实做对 /home 做 quota 限制

一、设定 Quota 的前置工作

设定 /etc/fstab,加入 usrquota 和 grpquota,能对使用者和群组做限制

# vi /etc/fstab
_____________________________________________________
LABEL=/home /home ext3 defaults,usrquota,grpquota 1 2

:____________________________________________________

修改完 /etc/fstab 之后,可以重新 mount 或是重新开机来让新的设定值生效

# mount -o remount /home

重新 mount 之后,再看看 /home 有没有使用 usrquota 和 grpquota 等选项

# mount
/dev/hda2 on / type ext3 (rw)
none on /proc type proc (rw)
usbdevfs on /proc/bus/usb type usbdevfs (rw)
/dev/hda1 on /boot type ext3 (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda5 on /home type ext3 (rw,usrquota,grpquota)
none on /dev/shm type tmpfs (rw)
/dev/md0 on /raiddata type ext3 (rw)

现在已经设定好 /etc/fstab 了,就可以来初始化 quota。

# quotacheck -cug /home

接著就到 /home 下看看有没有多了两个档案 aquota.group 和 aquota.user

# ls -l /home
total 27
-rw------- 1 root root 7168  Nov 21 20:14 aquota.group
-rw------- 1 root root 7168  Nov 21 20:14 aquota.user
drwx------ 2 root root 12288 Nov 18 02:16 lost+found

看到以上有 aquota.group 和 aquota.user 之后,就可以把 quota 开起来了。

# quotaon -ug /home

二、设定 Quota

Quota 设定时,可时用下列语法

针对 user

## 针对 steven 做限制
# edquota -u steven

针对 group

# edquota -g mis

知道如何进入设定画面时,先了解每个栏位的意思,先让我们在看看设定画面长什么样子:

# edquota -u steven
________________________________________________________________
Disk quotas for user steven (uid 500):
  Filesystem    blocks    soft    hard    inodes    soft    hard
  /dev/hda5         12       0       0        12       0       0
:_______________________________________________________________
 

Filesystem:steven 在 /dev/hda5 的限制

blocks:steven 在 /dev/hda5 已使用的容量 (KB),此项目无法更改

soft:软性限制,若超过的大小可充许暂时使用,使用单位为 KB

hard:硬性限制,若超过软性限制,无论如何并无法超过硬性大小限制,使用单位为 KB

inodes:steven 在 /dev/hda5 己使用的档案(目录及档案)数,此项目无法更改

soft:软性限制,若超过的单位可充许暂时使用

hard:硬性限制,若超过软性限制,无论如何并无法超过硬性单位限制

了解了以上栏位的资讯之后,现在来实做,让 steven 可以有 8 MB 的大小空间,但无法超过 10 MB,共且只能产生 50 个档案,最多不可超过 60 个。

# edquota -u steven
________________________________________________________________
Disk quotas for user steven (uid 500):
  Filesystem    blocks    soft    hard    inodes    soft    hard
  /dev/hda5         12    8192   10240        12      50      60
:_______________________________________________________________
 

以上再存起来,就可以立既生效了。如果要设定 group 的话,也是一样的方法。

三、测试

现在切到 steven 的身份,再建立一个档案大小为 11MB 的物件,看是否允许建立。

目前的使用状况

[steven@rhel steven]$ quota
Disk quotas for user steven (uid 500):
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/hda5      12    8192   10240              12      50      60

建立一个 7 MB 的空档案,再试看看

[steven@rhel steven]$ dd if=/dev/zero of=test bs=1M count=7
7+0 records in
7+0 records out
[steven@rhel steven]$ quota
Disk quotas for user steven (uid 500):
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/hda5    7209    8192   10240              13      50      60

上表可以清楚发现 steven 使用的 block 数变大了,现在在来新增一个 4 MB 的档案。

[steven@rhel steven]$ dd if=/dev/zero of=test2 bs=1M count=4
ide0(3,5): warning, user block quota exceeded.
ide0(3,5): write failed, user block limit reached.
ide0(3,5): write failed, user block limit reached.
dd: writing `test2': Disk quota exceeded
3+0 records in
2+0 records out

看到了吗,quota 在叫了呢告诉你已超过了容量。那我们再来看看到底是用了多少。

[steven@rhel steven]$ quota
Disk quotas for user steven (uid 500):
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/hda5   10238*   8192   10240   6days      14      50      60

上面可以看到, steven 已使用了 10238 KB 的容量了,再来,可以很清楚的看到在 grace 栏多了个 6 days 的值,这表示必需在 6 天之内把容量整埋到 8 MB 以下,否则超过的部份就会砍掉,那么致於 quota 会怎么砍呢?小弟也不了解。

Quota 重新开机后,会自动判别,因此不需要开启或关闭什么特定的 service。

 
Index