首页 > 代码库 > 磁盘配额
磁盘配额
Linux磁盘配额
限制用户在指定的磁盘分区的使用量(通过空间大小或文件个数来限制)
添加一个新的磁盘,创建新的分区来做磁盘配额的实验。分成ext4 和 xfs(RHEL7默认,可选)两种文件系统分别演示。
针对ext4文件系统,创建磁盘配额过程:
准备好一个ext4的分区,并挂载使用。对于ext4的文件系统,磁盘配额只能针对分区来做。对整个分区有效,而不能针对目个单一的子目录。
1.使文件系统支持磁盘配额;
2.生成磁盘配额记录文件;
3.设定用户的磁盘配额和警告期;
4.启动磁盘配额并测试。
1.使文件系统支持磁盘配额;
使用命令 :fdisk -cu /dev/vdb
查看/dev/vdb UUID : blkid /dev/vdb
[root@xueing ~]# vim /etc/fstab
UUID=ef10640d-45fa-4312-8a0e-3cdaa6a7a55e /dd ext4 defaults,usrquota,grpquota 1 1
[root@xueing ~]# mount -o remount /dd
[root@xueing ~]# mount |grep /dd
/dev/sda5 on /dd type ext4 (rw,usrquota,grpquota)
2.生成磁盘配额记录文件;
[root@xueing ~]# quotacheck -cug /dd
如果没有该命令,需要安装:
[root@server01 docs]# yum install quota -y
[root@xueing ~]# ls /dd/aquota.*
/dd/aquota.group /dd/aquota.user
3.设定用户的磁盘配额和警告期;
磁盘配额只对普通用户生效,对root无效。
[root@xueing ~]# edquota -u king
Disk quotas for user king (uid 501):
Filesystem blocks soft hard inodes soft hard
/dev/sda5 0 0 0 0 0 0
分区 已使用的块(1K/块) 块的软限额 块的硬限额 已使用的文件个数(等同于) inodes软限额 inodes的硬限额
设置后:
Disk quotas for user king (uid 501):
Filesystem blocks soft hard inodes soft hard
/dev/sda5 0 300 500 0 5 8
4.启动磁盘配额并测试。
[root@xueing ~]# quotaon /dd
测试
[root@xueing ~]# su - king
[king@xueing /dd]$ dd if=/dev/zero of=quota_test bs=1K count=200
记录了200+0 的读入
记录了200+0 的写出
204800字节(205 kB)已复制,0.00107211 秒,191 MB/秒
[king@xueing ~]$ ll
总用量 200
-rw-rw-r--. 1 king king 204800 3月 25 19:24 quota_test
[king@xueing /dd]$ dd if=/dev/zero of=quota_test2 bs=1K count=200
sda5: warning, user block quota exceeded.
记录了200+0 的读入
记录了200+0 的写出
204800字节(205 kB)已复制,0.000772908 秒,265 MB/秒
[king@xueing /dd]$ ll
总用量 400
-rw-rw-r--. 1 king king 204800 3月 25 19:24 quota_test
-rw-rw-r--. 1 king king 204800 3月 25 19:24 quota_test2
[king@xueing /dd]$ dd if=/dev/zero of=quota_test3 bs=1K count=200
sda5: write failed, user block limit reached.
dd: 正在写入"quota_test3": 超出磁盘限额
记录了101+0 的读入
记录了100+0 的写出
102400字节(102 kB)已复制,0.000720444 秒,142 MB/秒
[king@xueing /dd]$ ll
总用量 500
-rw-rw-r--. 1 king king 204800 3月 25 19:24 quota_test
-rw-rw-r--. 1 king king 204800 3月 25 19:24 quota_test2
-rw-rw-r--. 1 king king 102400 3月 25 19:25 quota_test3
[root@xueing /dd]# 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/sda5 7days 7days
[root@xueing /dd]# edquota -T king
Times to enforce softlimit for user king (uid 501):
Time units may be: days, hours, minutes, or seconds
Filesystem block grace inode grace
/dev/sda5 604638seconds unset
查看磁盘配额的使用报表:
[root@xueing ~]# quota -uvsl king
Disk quotas for user king (uid 501):
Filesystem blocks quota limit grace files quota limit grace
/dev/sda5 500* 300 500 6days 4 5 8
[root@xueing /dd]# repquota -aug
[root@xueing ~]# edquota -p king lisi
[root@xueing ~]# quota -uvsl lisi
Disk quotas for user lisi (uid 502):
Filesystem blocks quota limit grace files quota limit grace Accounting: ON
Enforcement: ON
/dev/sda5 32 300 500 9* 5 8 7days
练习: 完成上述实验过程。
思考:如何批量的创建N多用户的磁盘配额(假设配额值相同)
答案:
方式1 , 把所有用户加入到同一个组,然后针对组做磁盘配额。(测试组的磁盘配额是,所有成员共享一个值,还是每个成员有相同的值。)
方式2 , 使用edquota -p ,快速复制某个用户的配额到指定的用户;
方式3, 使用setquota 非交互的批量设置 ,可以配会shell脚本使用。
-------------------------------
针对RHEL7 xfs的磁盘配合的设置 (现阶段不要求)
参考:http://linux.vbird.org/linux_basic/0420quota.php#the_quota
过程演示:
[root@geust02 dd]# vim /etc/fstab
UUID="55a202bc-02c9-4712-84a4-af7f4de00425" /ee xfs defaults,usrquota,grpquota 0 0
如果已经挂载了/ee, 先卸载:umount /ee
[root@geust02 dd]# mount -a
[root@geust02 dd]# mount |grep vdb2
/dev/vdb2 on /ee type xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)
[root@geust02 dd]# xfs_quota -x /ee
xfs_quota> state
User quota state on /ee (/dev/vdb2)
Accounting: ON
Enforcement: ON
Inode: #131 (1 blocks, 1 extents)
Group quota state on /ee (/dev/vdb2)
Accounting: ON
Enforcement: ON
Inode: #132 (1 blocks, 1 extents)
Project quota state on /ee (/dev/vdb2)
Accounting: OFF
Enforcement: OFF
Inode: #132 (1 blocks, 1 extents)
Blocks grace time: [7 days 00:00:30]
Inodes grace time: [7 days 00:00:30]
Realtime Blocks grace time: [7 days 00:00:30]
xfs_quota> report -h
User quota on /ee (/dev/vdb2)
Blocks
User ID Used Soft Hard Warn/Grace
---------- ---------------------------------
root 0 0 0 00 [------]
Group quota on /ee (/dev/vdb2)
Blocks
Group ID Used Soft Hard Warn/Grace
---------- ---------------------------------
root 0 0 0 00 [------]
xfs_quota> limit bsoft=3m bhard=5m isoft=5 ihard=8 zhangsan
xfs_quota> report -h -u -i -b
User quota on /ee (/dev/vdb2)
Blocks Inodes
User ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
root 0 0 0 00 [------] 3 0 0 00 [------]
zhangsan 0 3M 5M 00 [------] 0 5 8 00 [------]
xfs_quota> quit
测试过程略
-------------------------
xfs 新增对project(某个指定的目录,与用户和组无关)的磁盘配额的支持。(了解)
[root@geust02 ee]# vim /etc/fstab
UUID="55a202bc-02c9-4712-84a4-af7f4de00425" /ee xfs defaults,prjquota 0 0
[root@geust02 ~]# umount /ee
[root@geust02 ~]# mount /ee
[root@geust02 ~]# mount |grep vdb2
/dev/vdb2 on /ee type xfs (rw,relatime,seclabel,attr2,inode64,prjquota)
[root@geust02 ~]# echo "uplooking:01" >> /etc/projid
[root@geust02 ~]# echo "01:/ee/test" >> /etc/projects
[root@geust02 ~]# xfs_quota -x /ee
xfs_quota> state
User quota state on /ee (/dev/vdb2)
Accounting: OFF
Enforcement: OFF
Inode: #131 (2 blocks, 2 extents)
Group quota state on /ee (/dev/vdb2)
Accounting: OFF
Enforcement: OFF
Inode: N/A
Project quota state on /ee (/dev/vdb2)
Accounting: ON
Enforcement: ON
Inode: N/A
Blocks grace time: [7 days 00:00:30]
Inodes grace time: [7 days 00:00:30]
Realtime Blocks grace time: [7 days 00:00:30]
xfs_quota> project -s uplooking
Setting up project uplooking (path /ee/test)...
Processed 1 (/etc/projects and cmdline) paths for project uplooking with recursion depth infinite (-1).
xfs_quota> limit -p bsoft=3m bhard=5m uplooking
xfs_quota> report -h -b -p
Project quota on /ee (/dev/vdb2)
Blocks
Project ID Used Soft Hard Warn/Grace
---------- ---------------------------------
uplooking 0 3M 5M 00 [------]
xfs_quota> quit
[root@geust02 ~]# cd /ee/test
[root@geust02 test]# dd if=/dev/zero of=1.db bs=1M count=6
dd: 写入"1.db" 出错: 设备上没有空间
记录了6+0 的读入
记录了5+0 的写出
5242880字节(5.2 MB)已复制,0.0367517 秒,143 MB/秒
[root@geust02 test]# ll -h
总用量 5.0M
-rw-r--r--. 1 root root 5.0M 12月 6 17:13 1.db
[root@geust02 test]# cd ..
[root@geust02 ee]# ll
总用量 0
drwxrwxrwx. 2 root root 28 12月 6 17:34 test
[root@geust02 ee]# dd if=/dev/zero of=2.db bs=1M count=4
记录了4+0 的读入
记录了4+0 的写出
4194304字节(4.2 MB)已复制,0.0201733 秒,208 MB/秒
[root@geust02 ee]# ll
总用量 4096
-rw-r--r--. 1 root root 4194304 12月 6 17:34 2.db
易忘之处:
quotacheck -cug(F强制) /a 生成磁盘配额 若是不成功 执行此命令:setenforce 0
/dev/vg01/lv02 /a ext4 defaults,usrquota,gpgquota 1 1 挂载
设置磁盘配额警告时间 edquota -t
磁盘配额