使用软件:quota

操作系统:Ubuntu 12.04_x86_64

针对分区:/home下的用户


一、安装quota


# apt-get install -y quota


二、配置磁盘配额分区


配置设备目录支持相应quota

# vi /etc/fstab    重启后生效 

/dev/sda7   /home  ext4   defaults,usrquota,grpquota  1 2


重现挂载/home分区

#mount -o remount,usrquota,grpquota /home 


测试quota

#quotacheck -avug

quotacheck -auvg

quotacheck: Quota for users is enabled on mountpoint /home so quotacheck might damage the file.

Please turn quotas off or use -f to force checking.


#quotacheck -avug -f

quotacheck: Cannot remount filesystem mounted on /home read-only so counted values might not be right.

Please stop all programs writing to filesystem or use -m flag to force checking.


# quotacheck -auvg -f -m

quotacheck: Scanning /dev/disk/by-uuid/52bce451-c00a-4655-b7c4-4ed911361be6 [/] quotacheck: lstat Cannot stat `//var/lib/lightdm/.gvfs': Permission denied

Guess you'd better run fsck first !

exiting...


# fsck

fsck from util-linux 2.20.1

e2fsck 1.42 (29-Nov-2011)

/dev/sda1 is mounted.  


WARNING!!!  The filesystem is mounted.   If you continue you ***WILL***

cause ***SEVERE*** filesystem damage.


Do you really want to continue<n>? yes


/dev/sda1: recovering journal

Clearing orphaned inode 291185 (uid=0, gid=0, mode=0100640, size=1074)

Clearing orphaned inode 291988 (uid=0, gid=0, mode=0100640, size=62)

Clearing orphaned inode 535083 (uid=104, gid=111, mode=0100600, size=1)

Clearing orphaned inode 793605 (uid=104, gid=111, mode=0100664, size=2028)

Setting free inodes count to 1011177 (was 1011199)

Setting free blocks count to 3061839 (was 3061776)

/dev/sda5: clean, 234007/1245184 files, 1918641/4980480 blocks


此时有两种方法:

法1:快速解决问题,不影响系统服务使用

#reboot

法2:结束掉正在使用的进程

#fuser -m /home

 10334rce 10665rce 10843rce

#ps -aux | grep 10334

000      4753  0.0  0.0 557828 23976 ?        Sl   15:35   0:00 /usr/bin/python /usr/lib/unity-scope-video-remote/unity-scope-video-remote


#quotacheck -avug

quotacheck: Quota for users is enabled on mountpoint /home so quotacheck might damage the file.

Please turn quotas off or use -f to force checking.

此时执行quotacheck提示,加上-f强制


#quotacheck -avug -f

quotacheck: Scanning /dev/sda7 [/home] done

quotacheck: Checked 236 directories and 1225 files


#ll /home/

total 52

drwxr-xr-x  6 root      root       4096 May 11 15:39 ./

drwxr-xr-x 28 root      root       4096 May 10 14:07 ../

-rw-------  1 root      root       7168 May 11 15:39 aquota.group

-rw-------  1 root      root       7168 May 11 15:39 aquota.user


三、开启磁盘配额


使用#quotaon -auvg命令后发现如下报错

#quotaon -auvg

quotaon: using /home/aquota.group on /dev/sda7 [/home]: Device or resource busy

quotaon: using /home/aquota.user on /dev/sda7 [/home]: Device or resource busy


此时关闭quota

#quotaoff -a


重现开启,显示已经完成对home分区的磁盘配额

#quotaon -auvg

/dev/sda7 [/home]: group quotas turned on

/dev/sda7 [/home]: user quotas turned on


四、对用户进行配额


对已经创建的用户quota_user_001进行配置

#edquota -u quota_user_001#配置用户磁盘配额

isk quotas for user qibo (uid 1001):

  Filesystem  blocks    soft    hard     inodes     soft$

  /dev/sda7   12     300000000  524288000   4        0$

单位为Kb

其中blocks为已经使用的容量,soft和hard分别是,限额287G和500G

ctrl+o 写入    

ctrl+x 退出


复制用户的配置信息

#edquota -p quota_user_001 -u quota_user_002 


修改宽限时间  当使用者文件超过大小时

#edquota -t   

36500

因为没有强制性需求,因此设置为36500天


编辑myquotagrp组的总共使用大小 

edquota -g quota_group_001 


查看全部磁盘配额用户的限制

#repquota -auv


#检查用户磁盘配额剩余情况

$quota -vsug quota_user_001


查看是否有警告信息

$warnquota


# quotaoff /home (取消磁盘空间限制。) 

# quotaon /home (现在启动磁盘空间限制。)


五、测试

可以设定50M的限额,创建容量超过80M的文件

测试命令如下

# su - quota_user_001

$ dd if=/dev/zero of=ceshi bs=1M count=40

记录了40+0 的读入

记录了40+0 的写出

41943040字节(42 MB)已复制,0.0535329 秒,783 MB/秒

tankai@dttzd07:~$ ls

ceshi

tankai@dttzd07:~$ du -sh *

40M     ceshi

$ dd if=/dev/zero of=ceshi2 bs=1M count=40

dd: 正在写入"ceshi2": 超出磁盘限额

记录了9+0 的读入

记录了8+0 的写出

9248768字节(9.2 MB)已复制,0.0151042 秒,612 MB/秒

$ du -sh *

40M     ceshi

8.9M    ceshi2


dd if=/dev/zero of=bigfile2 bs=1M count=40

会发现真是数据只有48.9M