磁盘配额(Quota)就是 Linux 系统中用来限制特定的普通用户或用户组在指定的分区上占用的磁盘空间或文件个数的。
在此概念中,有以下几个重点需要注意:
磁盘配额在实际生活中其实是很常见的,比如,我们的邮箱不管多大,都是有限制的,而不可能无限制地存储邮件;我们可以上传文件的服务器也是有容量限制的,网页中的个人空间也不可能让我们无限制地使用。
磁盘配额就好像我们出租写字楼,虽然整栋楼的空间非常大,但是酬整栋楼的成本太高。我们可以分开出租,用户如果觉得不够用,则还可以租用更大的空间。不过租用是不能随便进行的,其中有几个规矩必须遵守:
用户配额和组配额
用户配额是指针对用户个人的配额,而组配额是指针对整个用户组的配额。如果我们需要限制的用户数量并不多,则可以给每个用户单独指定配额。如果用户比较多,那么单独限制太过麻烦,这时我们可以把用户加入某个用户组,然后给组指定配额,就会简单得多。
需要注意的是,组中的用户是共享空间或文件数的。也就是说,如果用户 lamp1、lamp2 和 lamp3 都属于 brother 用户组,我给 brother 用户组分配 100MB 的磁盘空间,那么,这三个用户不是平均分配这 100MB 空间的,而是先到先得,谁先占用,谁就有可能占满这 100MB 空间,后来的就没有空间可用了。
磁盘容量限制和文件个数限制
我们除了可以通过限制用户可用的 block 数量来限制用户可用的磁盘容量,也可以通过限制用户可用的 inode 数量来限制用户可以上传或新建的文件个数。
软限制和硬限制
软限制可理解为警告限制,硬限制就是真正的限制了。比如,规定软限制为 100MB,硬限制为 200MB,那么,当用户使用的磁盘空间为 100~200MB 时,用户还可以继续上传和新建文件,但是每次登录时都会收到一条警告消息,告诉用户磁盘将满。
宽限时间
如果用户的空间占用数处于软限制和硬限制之间,那么系统会在用户登录时警告用户磁盘将满,但是这个警告不会一直进行,而是有时间限制的,这个时间就是宽限时间,默认是 7 天。
如果到达宽限时间,用户的磁盘占用量还超过软限制,那么软限制就会升级为硬限制。也就是说,如果软限制是 100MB,硬限制是 200MB,宽限时间是 7天,此时用户占用了 120MB,那么今后 7 天用户每次登录时都会出现磁盘将满的警告,如果用户置之不理,7 天后这个用户的硬限制就会变成 100MB,而不是 200MB 了。
XFS supports disk quotas by user, by group, and by project. Project disk quotas allow you to limit the amount of disk space on individual directory hierarchies. You can configure both hard and soft limits on the number of disk blocks (or disk space), and the number of inodes, which limit the number of files a user can create. Quotas do not apply to the root user.
You must first enable quotas for users, groups, and/or projects by using a mount option when mounting for the XFS file system. After enabling quotas, use the xfs_quota command to set limits, to view quota information.
To enable quotas for users on an XFS file system, include the quota option in the /etc/fstab entry for the file system, or mount the file system with the quota option:
# mount –o quota /dev/xvdb1 /xfs
To enable quotas for groups, include the gquota option in the /etc/fstab entry for the file system, or mount the file system with the gquota option:
# mount –o gquota /dev/xvdb1 /xfs
To enable quotas for projects, include the prjquota option in the /etc/fstab entry for the file system, or mount the file system with the prjquota option:
# mount –o prjquota /dev/xvdb1 /xfs
Alternatively, you can include the quota mount options in the /etc/fstab file. The following example shows entries in the /etc/fstab file to enable quotas for users, groups, and projects, respectively, on an XFS file system. These examples also mount the file system with read/write permissions:
# vim /etc/fstab
/dev/xvdb1 /xfs xfs rw,quota 0 0
/dev/xvdb1 /xfs xfs rw,gquota 0 0
/dev/xvdb1 /xfs xfs rw,prjquota 0 0
Other “quota” mount options for XFS file systems are available. The following is a complete list of mount options to enable user quotas on XFS file systems:
Group quota mount options include the following:
Project quota mount options include the following:
You can use the following xfs_quota command to report the overall quota state information:
# xfs_quota -x -c state
User quota state on /data (/dev/mapper/vg_test-lv_test)
Accounting: ON
Enforcement: ON
Inode: #67 (1 blocks, 1 extents)
Group quota state on /data (/dev/mapper/vg_test-lv_test)
Accounting: OFF
Enforcement: OFF
Inode: #0 (0 blocks, 0 extents)
Project quota state on /data (/dev/mapper/vg_test-lv_test)
Accounting: OFF
Enforcement: OFF
Inode: #0 (0 blocks, 0 extents)
Blocks grace time: [7 days]
Inodes grace time: [7 days]
Realtime Blocks grace time: [7 days]
This command reports whether user, group, and project disk quota accounting is enabled and whether limits are being enforced. The grace period for blocks and inodes is also reported. The timer for the grace period is enabled whenever the soft limit is exceeded. If soft limits continue to be exceeded after the grace period expires, no more disk space or inodes are allocated.
After enabling quotas by using XFS file system mount options, use the xfs_quota command to report file system quota information, set block and inode limits, enable or disable quota enforcement, modify the quota enforcement timeout information (grace period), and perform other quota management operations on XFS file systems.
The xfs_quota utility provides a number of user and administrator subcommands. These subcommands can be issued in interactive mode or included as arguments to the xfs_quota command. Enter xfs_quota without any options or arguments to enter interactive mode. An xfs_quota> prompt appears in interactive mode. Enter help or ? to view the available subcommands. You can also enter help commandname to display additional information on a specific subcommand.
# xfs_quota
xfs_quota> help
df [-bir] [-hn] [-f file] -- show free and used counts for blocks and inodes
help [command] -- help for one or all commands
print -- list known mount points and projects
quit -- exit the program
quota [-bir] [-g|-p|-u] [-hnNv] [-f file] [id|name]... -- show usage and limits
Use 'help commandname' for extended help.
When including xfs_quota subcommands from the command line, use the –c [command] option. Any modifications to the quota system from the command line also require the -x option (enable expert mode). The following example uses the limit subcommand to set a soft limit of 5 MB, and a hard limit of 6 MB on the XFS file system for user john:
# xfs_quota –x –c 'limit –u bsoft=5m bhard=6m john' /xfs
The following example sets a soft limit of 100 inodes and a hard limit of 150 inodes for the students group:
# xfs_quota –x –c 'limit –g isoft=100 ihard=150 students' /xfs
Use the xfs_quota command to display information about disk quotas. To list all paths with devices and identifiers:
# xfs_quota -x -c print
Filesystem Pathname
/data /dev/mapper/vg_test-lv_test (uquota)
To report file system usage for blocks (-b) and inodes (-i):
# xfs_quota -x -c 'free -hb'
Filesystem Size Used Avail Use% Pathname
/dev/mapper/vg_test-lv_test 35.5G 41.1M 35.4G 0% /data
# xfs_quota -x -c 'free -hi'
Filesystem Inodes Used Free Use% Pathname
/dev/mapper/vg_test-lv_test 18.6m 5 18.6m 0% /data
To report file system quota information:
# xfs_quota -x -c report /data
User quota on /data (/dev/mapper/vg_test-lv_test)
Blocks
User ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 4 0 0 00 [--------]
To report quota information in human-readable form on /data:
# xfs_quota -x -c 'report -h' /data
User quota on /data (/dev/mapper/vg_test-lv_test)
Blocks
User ID Used Soft Hard Warn/Grace
---------- ---------------------------------
root 4K 0 0 00 [------]
XFS allows you to set quotas on individual directory hierarchies. You can create an entry in the /etc/projects file that associates the XFS file system directory hierarchy with a unique project ID. For example, the following entry in /etc/projects associates a project ID of 50 with the /data directory:
50:/data
You can optionally use the /etc/projid file to associate a project name to a project ID. For example, the following entry in the /etc/projid file associates the project name test with the project ID 50:
test:50
After defining a project in /etc/projects, use the xfs_quota command to initialize its project directory:
# xfs_quota –x –c 'project –s test' /data
Use the xfs_quota command to set limits for projects with initialized directories. The following example sets a soft limit of 5 MB and a hard limit of 6 MB for the test project:
# xfs_quota –x –c ‘limit –p bsoft=5m bhard=6m test’ /data