Linux 磁盘配额配置 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

本次试验以/dev/sdb为例配置磁盘配额

首先对/dev/sdb进行分区,格式化

fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memory only,

until you decide to write them. After that, of course, the previous

content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-261, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-261, default 261):

Using default value 261

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes

255 heads, 63 sectors/track, 261 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         261     2096451   83  Linux

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

 mkfs.ext3 /dev/sdb1

mke2fs 1.39 (29-May-2006)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

262144 inodes, 524112 blocks

26205 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=536870912

16 block groups

32768 blocks per group, 32768 fragments per group

16384 inodes per group

Superblock backups stored on blocks:

        32768, 98304, 163840, 229376, 294912

Writing inode tables: done

Creating journal (8192 blocks): done

Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

没开启磁盘配额前的状态如下:

mount /dev/sdb1 /mnt/sdb1

ls /mnt/sdb1

lost+found

[root@test2 ~]# useradd test1

[root@test2 ~]# passwd test1

Changing password for user test1.

New UNIX password:

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

[root@test2 ~]# chown -R test1 /mnt/sdb1

[root@test2 ~]# ll /mnt/sdb1

total 16

drwx------ 2 test1 root 16384 Sep 17 08:55 lost+found

[root@test2 ~]# umount /mnt/sdb1

[root@test2 ~]# mount -o usrquota,grpquota /dev/sdb1 /mnt/sdb1

查看是否启用quota

[root@test2 ~]# cat /etc/mtab

/dev/sda3 / ext3 rw 0 0

proc /proc proc rw 0 0

sysfs /sys sysfs rw 0 0

devpts /dev/pts devpts rw,gid=5,mode=620 0 0

/dev/sda1 /boot ext3 rw 0 0

tmpfs /dev/shm tmpfs rw 0 0

none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0

/dev/sdb1 /mnt/sdb1 ext3 rw,usrquota,grpquota 0 0

进入单用户模式

Init 1

Quotacheck需要安装quota.i386 1:3.13-1.2.5.el5

[root@test2 ~]# which quotacheck

/sbin/quotacheck

[root@test2 ~]# quotacheck -cvug /dev/sdb1

quotacheck: Scanning /dev/sdb1 [/mnt/sdb1] quotacheck: Cannot stat old user quota file: No such file or directory

quotacheck: Cannot stat old group quota file: No such file or directory

done

quotacheck: Checked 3 directories and 2 files

quotacheck: Old file not found.

[root@test2 ~]# ls /mnt/sdb1

aquota.group  aquota.user  lost+found

aquota.user文件说明磁盘配额检测成功;

启用磁盘配额

quotaon /dev/sdb1

Init 3

mkdir /mnt/sdb1/aaa

chown -R test1 /mnt/sdb1/aaa

对用户test1进行配额限制

edquota -u test1

Disk quotas for user test1 (uid 500):

  Filesystem                   blocks       soft       hard     inodes     soft     hard

  /dev/sdb1                        20        500        600          2        4        5

说明如下:blocks下面20代表目前实际大小 soft代表软限制为500k  硬限制为600k

          Inodes下表2代表目前有两个文件 soft代表软限制文件个数为硬限制个数为5

测试如下:

[root@test2 ~]# su test1

[test1@test2 root]$ quota

Disk quotas for user test1 (uid 500):

     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

      /dev/sdb1      24     500     600         3     5       6

测试文件个数限制:限制为6,可以创建3个,第四个会报错

[test1@test2 root]$ cd /mnt/sdb1/aaa

[test1@test2 aaa]$ ls

[test1@test2 aaa]$ touch 111

[test1@test2 aaa]$ quota

Disk quotas for user test1 (uid 500):

     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

      /dev/sdb1      24     500     600               4       5       6

[test1@test2 aaa]$ touch 222

[test1@test2 aaa]$ quota

Disk quotas for user test1 (uid 500):

     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

      /dev/sdb1      24     500     600               5       5       6

[test1@test2 aaa]$ touch 333

sdb1: warning, user file quota exceeded.

[test1@test2 aaa]$ ll

total 0

-rw-rw-r-- 1 test1 test1 0 Sep 17 11:05 111

-rw-rw-r-- 1 test1 test1 0 Sep 17 11:05 222

-rw-rw-r-- 1 test1 test1 0 Sep 17 11:06 333

[test1@test2 aaa]$ quota

Disk quotas for user test1 (uid 500):

     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

      /dev/sdb1      24     500     600               6*      5       6

[test1@test2 aaa]$ touch 444

sdb1: write failed, user file limit reached.

touch: cannot touch `444': Disk quota exceeded

[test1@test2 aaa]$ ll

total 0

-rw-rw-r-- 1 test1 test1 0 Sep 17 11:05 111

-rw-rw-r-- 1 test1 test1 0 Sep 17 11:05 222

-rw-rw-r-- 1 test1 test1 0 Sep 17 11:06 333

测试用户使用空间限制:500k警告 大于600不允许再创建

先创建一个大小400k的文件,名字为aaa1,创建成功;

[test1@test2 aaa]$ dd if=/dev/zero of=aaa1 bs=1k count=400

400+0 records in

400+0 records out

409600 bytes (410 kB) copied, 0.0142511 seconds, 28.7 MB/s

[test1@test2 aaa]$ quota

Disk quotas for user test1 (uid 500):

     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

      /dev/sdb1     428     500     600               4       5       6

再创建一个大小100k的文件,名字为aaa2,创建成功,但警告

[test1@test2 aaa]$ dd if=/dev/zero of=aaa2 bs=1k count=100

s db1: warning, user block quota exceeded.

100+0 records in

100+0 records out

102400 bytes (102 kB) copied, 0.00265624 seconds, 38.6 MB/s

[test1@test2 aaa]$ quota

Disk quotas for user test1 (uid 500):

     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

      /dev/sdb1     532*    500     600   7days       5       5       6

再创建一个大小100k的文件,名字为aaa3,可以创建,但只会使用剩余空间;

[test1@test2 aaa]$ dd if=/dev/zero of=aaa3 bs=1k count=100

sdb1: warning, user file quota exceeded.

sdb1: write failed, user block limit reached.

dd: writing `aaa3': Disk quota exceeded

65+0 records in

64+0 records out

65536 bytes (66 kB) copied, 0.00413993 seconds, 15.8 MB/s

[test1@test2 aaa]$ ls -lh

total 576K

-rw-rw-r-- 1 test1 test1 400K Sep 17 11:11 aaa1

-rw-rw-r-- 1 test1 test1 100K Sep 17 11:13 aaa2

-rw-rw-r-- 1 test1 test1   64K  Sep 17 11:15 aaa3

此时已经达到了硬限制,再创建会提示失败

[test1@test2 aaa]$ dd if=/dev/zero of=aaa4 bs=1k count=100

sdb1: write failed, user file limit reached.

dd: opening `aaa4': Disk quota exceeded

[test1@test2 aaa]$ ls -lh

total 576K

-rw-rw-r-- 1 test1 test1 400K Sep 17 11:11 aaa1

-rw-rw-r-- 1 test1 test1 100K Sep 17 11:13 aaa2

-rw-rw-r-- 1 test1 test1  64K Sep 17 11:15 aaa3

Edquota -t  更改默认过期天数,默认天数为7

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/sdb1                      7days                   7days

使磁盘配额开机启动,操作如下:

[root@test2 ~]# cat /etc/fstab

LABEL=/                 /                       ext3    defaults        1 1

LABEL=/boot             /boot                   ext3    defaults        1 2

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults        0 0

proc                    /proc                   proc    defaults        0 0

LABEL=SWAP-sda2         swap                    swap    defaults        0 0

/dev/sdb1               /mnt/sdb1               ext3    defaults,usrquota,grpquota       1 1

备注:

如果每个用户都要这么麻烦的设置的话,那这种重复的体力劳动实在有点令人不寒而栗,而且也太浪费时间了。幸好edquota还有个 -p 参数(prototype)可以对已有的用户设置进行拷贝。例如,我们想对JackTomChen三个用户使用和lanf一样的限额配置,可以使用如下的命令:
edquota -p lanf -u Jack Tom Chen
这样一来,这三个用户就被赋予了和lanf一样的磁盘配额。
对组的配额,除了edquota命令中对应-u参数的改为-g参数,例如下面对webterm1组的操作:
edquota -g webterm1

实际上,以上的限制只是对用户设定的硬限制在起作用。如果需要使软限制也起作用的话,还需要对用户的软限制设定宽限期——缺省的,软限制的宽限期是无穷大——这可以使用edquota命令的-t选项来实现。运行下面的命令:
edquota -t
edquota将打开缺省编辑器显示如下内容:
Time units may be:days,hours,minutes,or seconds
Grace period before enforcing soft limits for users:
/dev/sda5:block grace period:0 days,file grace period:0 days
可以使用天、小时、分、秒为单位来设定宽限期。例如,在下面这个例子中,磁盘空间限制的宽限期为两天,而文件数量限制的宽限期只有6个小时。
Time units may be:days,hours,minutes,or seconds
Grace period before enforcing soft limits for users:
/dev/sda5:block grace period:2 days,file grace period:6 hours

  b.通过setquota工具加入:

  比如加入用户bye2000的磁盘配额,执行以下命令:

  setquota –u / 2000 2500 100 110 bye2000

  以下是setquota命令用法的简单描述:

  setquota [ -u|-g ] 装载点 软块数 硬块数 软文件数 硬文件数 用户名/组名




查看用户磁盘使用情况
要查明某一个用户使用了多少磁盘空间,例如lanf,可以使用如下的命令:
quota -u lanf
显示:
Disk quotas for user lanf(uid 503):
Filesystem blocks quota limit grace file quota limit grace
/dev/sda5 3 102400 409800 1 12800 51200 
同样,可以使用quota -g groupname命令来参看某个组的磁盘使用情况。
注意: 1、如果该用户没有配置磁盘限额的话,输出显示如下:
Disk quotas for user hujm (uid 503): none
2、如果不带任何参数运行quota的话,查看的是你自己的配额使用情况。