Implementing quotas to restrict disk space usage

8143

Author: Shashank Sharma

If you manage a system that’s accessed by multiple users, you might have a user who hogs the disk space. Using disk quotas you can limit the amount of space available to each user. It’s fairly easy to set up quotas, and once you are done you will be able to control the number of inodes and blocks owned by any user or group.

Control over the disk blocks means that you can specify exactly how many bytes of disk space are available to a user or group. Since inodes store information about files, by limiting the number of inodes, you can limit the number of files users can create.

When working with quotas, you can set a soft limit or a hard limit, or both, on the number of blocks or inodes users can use. The hard limit defines the absolute maximum disk space available to a user. When this value is reached, the user can’t use any more space. The soft limit is more lenient, in that the user is only warned when he exceeds the limit. He can still create new files and use more space, as long as you set a grace period, which is a length of time for which the user is allowed to exceed the soft limit.

Configuration

Fedora supports quotas right out of the box. Ubuntu users must install the quota package from the distribution’s repositories. All of the commands below should be run as the root user.

Quotas are set up on a per-filesystem basis. To implement them, edit the /etc/fstab file and add the usrquota or grpquota option to the partition for which you want to enable quotas. For instance, for the / filesystem, your /etc/fstab file might look like this:

/dev/VolGroup00/LogVol00     /     ext3     defaults,usrquota,grpquota     1 1

After saving your changes you need to remount your filesystem with a command like mount -o remount /. If you’ve enabled quotas on multiple filesystems, you must run the mount -o remount command for each.

The next step is to run the quotacheck command to test the filesystem for any errors:

# quotacheck -augmv
quotacheck: Scanning /dev/mapper/VolGroup00-LogVol00 [/] done
quotacheck: Cannot stat old user quota file: No such file or directory
quotacheck: Cannot stat old group quota file: No such file or directory
quotacheck: Cannot stat old user quota file: No such file or directory
quotacheck: Cannot stat old group quota file: No such file or directory
quotacheck: Checked 8865 directories and 109083 files
quotacheck: Old file not found.
quotacheck: Old file not found.

The -a switch tells quotacheck to perform the check on all filesystems. The -u and -g switches tell it to check for user and group quotas. Using the -m switch means the filesystem will not be remounted as read-only.

The first time you run the quotacheck command, it will throw up several warnings because the filesystem had not been checked before. When it finishes, you should find two new files, aquota.user and aquota.group, in the top-level directory of the filesystem for which you have enabled quotas. The files hold the quota information for the users and the groups.

Your filesystems are now set up to use disk quotas. To turn them on, use the quotaon command:

# quotaon -augv
/dev/mapper/VolGroup00-LogVol00 [/]: group quotas turned on
/dev/mapper/VolGroup00-LogVol00 [/]: user quotas turned on

Once quotas are enabled, you can use the edquota command to limit the disk space available to users. The edquota -u username command will open up a text editor wherein you can specify the soft and hard inodes and blocks limits for a particular user. The edquota -g command can similarly be used to edit group quotas.

# edquota -u linuxlala
Disk quotas for user linuxlala (uid 500):
  Filesystem                         blocks       soft       hard     inodes     soft     hard
  /dev/mapper/VolGroup00-LogVol00    679876          0          0       5194        0        0

In the above example, user linuxlala is using about 679MB of space and almost 5,200 inodes.

Quotas kick in immediately after you change the soft and hard limits for either blocks or inodes or both and save the file. Once the quotas are set, individual users can check their limits using the quota command:

$ quota
Disk quotas for user linuxlala (uid 500): 
     Filesystem                      blocks   quota   limit   grace   files   quota   limit   grace
/dev/mapper/VolGroup00-LogVol00      679876  690000  700000            5194    5500    6000  

If the grace period is set to 0, despite being warned when the soft limit is reached, a user can continue to use more disk space until the hard limit is reached. By default, the grace period is 7 days. You can use the edquota -t command to change the filesystem grace period. The time period can be specified in days, hours, minutes, or even seconds, but make sure there is no space between the value and the unit of time. That is, to set a grace period of 15 days, write 15days. Once the grace period expires, the user won’t be able to create more files or use more disk space until he deletes some files.

To enable user-specific grace periods, use the -T switch. For instance, edquota -u linuxlala -T lets you specify a grace period for user linuxlala. A warning flashes on the screen if the user tries to create a new file after exceeding the quota limit:

$ touch testfile
touch: cannot touch `testfile': Disk quota exceeded

Unlike the clean spaces between each column entry as shown in the examples above, the editor might show the fields separated by single spaces. Don’t try to adjust the spacing between the columns if that happens. Quota only looks for space separated values, so it doesn’t matters if the columns are neatly arranged.

Since the purpose of implementing quotas is to keep disk usage under check, you can configure your system to send an email to the system administrator as well as the space-hogging user using the /etc/warnquota.conf file. The file is well-commented, and you can refer to its man page for more information: man warnquota.

Conclusion

Quotas are a smart way to ensure users don’t misuse the limited resources on a system. The soft limit warning and the grace period are features that give a quota-restricted user enough time to make room for new files.

Category:

  • System Administration