Setting and Enforcing Disk Usage Quotas


Remember those users from earlier in the chapter who wouldn't stop uploading files and forced you to buy a new disk to accommodate them? Suppose you decided that you didn't want any single user to be able to use more than 100MB without special permission from you. You can set this limit with quotas.

Quotas are not built into the GENERIC kernel. To enable them, you have to add the following line to your custom kernel configuration file (/usr/src/sys/i386/conf/CUSTOM, copied from GENERIC, as discussed in Chapter 18):

options      QUOTA


See "Creating a Custom Kernel Configuration File," in Chapter 18 for information on building a custom kernel. You also can make use of a couple switches in /etc/rc.conf to enable support for quotas when the system is brought up. To use those switches, add these lines:

enable_quotas="YES" check_quotas="NO"


The first line turns on quota support globally. The second line tells the system to skip a long, time-consuming consistency check (quotacheck -a) at boot time, which ensures that the quota database is properly synchronized. If you want to enable this check, change the second line to "YES" or remove the line altogether ("YES" is the default).

The last step is to turn quotas on (or off) per filesystem. This is done in /etc/fstab by adding the userquota option and/or the groupquota option to the fourth field in each filesystem on which you want to enforce quotas. Here's an example:

/dev/ad0s1f     /var    ufs   rw,userquota                 2      2 /dev/ad0s1e     /usr    ufs   rw,gropquota                 2      2 /dev/ad1s1g     /home   ufs   rw,userquota,groupquota      2      2


After making all these changes, reboot the system. Now that you're armed for battle, you're ready to start assigning quotas to users. You can assign them user by user (which is hardly practical on a high-load server) or you can set one user's quota and then use it as a prototype to apply the same settings to a range of user IDs. To set the quota for that first user, you'll need to use the built-in edquota utility, which lets you edit the attributes as a text file (much like chfn, which you learn about in Chapter 13, "Users, Groups, and Permissions"). The text editor that edquota uses is whatever is specified in your EDITOR environment variable. The default installation specifies vi, but you can choose a more user-friendly text editor, such as pico or ee, by using a command such as setenv EDITOR pico. Here's an example of setting a user's quota ("hard" and "soft" limits will be discussed later):

# edquota -u bob Quotas for user bob: /usr: blocks in use: 65, limits (soft = 50, hard = 75)         inodes in use: 7, limits (soft = 50, hard = 60) /var: blocks in use: 0, limits (soft = 50, hard = 75)         inodes in use: 0, limits (soft = 50, hard = 60)


After this user's quota has been set up, you can then clone the settings throughout your system, as follows:

# edquota -p bob 1001-9999


This will apply the same quota settings to the entire range of UIDs specified, even those that haven't been created yet!

Note the difference between blocks and inodes. Both limits will be enforced. The term blocks refers to the total space used (in 1KB units), and inodes can be understood to mean "files."

Soft and Hard Limits

A word or two about hard and soft limitsand the grace periodare in order here:

  • A hard limit is strictly enforced. If a user's disk usage reaches the hard limit, the system will not permit any more space to be allocated to that user.

  • A soft limit does not prevent the user from creating more files or using more space; instead, it triggers a timer for the grace period, which is seven days by default (but can be changed using edquota -t). After this grace period expires, the soft limit is enforced the same way as a hard limit. This allows users to use more than their allocated space (up to the hard limit) for brief periods of time. If the user's disk usage drops below the soft limit, the grace period is reset.

After you have set these limits, you can view them as follows:

# quota bob Disk quotas for user bob (uid 1015): Filesystem  blocks   quota   limit   grace    files    quota   limit   grace /home       1812     20000   40000             37        0       0


The quota command shows the quota information for the user specified as the final argument, or for the current user if that argument is omitted. If the user is over either of these limits, an asterisk (*) will appear after the number of blocks or files by which the user is exceeding the limit, and the grace column will report the amount of time left before the soft limit is enforced. Here's an example:

# quota bob Disk quotas for user bob (uid 1015): Filesystem  blocks   quota   limit   grace   files    quota   limit   grace /home       28121*   20000   40000   6days    189       0       0


Note

To make sure that quotas are running properly, use the mount command with no arguments. The following is output from mount on a system in which the /home partition is using quotas:

/dev/ad0s1a on / (ufs, local) /dev/ad0s1e on /usr (ufs, local) procfs on /proc (procfs, local) /dev/ad1s1g on /home (ufs, local, with quotas)


If you don't see the with quotas flag, the filesystem was not properly mounted with quotas. Check /etc/fstab, /etc/rc.conf, and your kernel configuration and then try rebooting if everything looks correct.


Quotas can be turned off easily enough in one of three ways:

  • Globally, by setting enable_quotas="NO" in /etc/rc.conf and then rebooting.

  • Per filesystem, by editing /etc/fstab and then rebooting.

  • Per user, by using edquota and setting the hard and soft limits to zero. Then, you could use edquota -p to propagate these settings throughout a range of UIDs, if you want.

Filesystem management isn't an easy task, but once you have a firm grasp of the concepts, the versatility of UNIX filesystems becomes readily apparent. Multiuser operating systems like FreeBSD bring up all kinds of issues that don't exist on desktop systems, such as handling multiple filesystem types, monitoring usage, and enforcing quotas, but these features are what separates UNIX from its less-capable contemporaries. We will continue this discussion into the area of formatting and labeling new disks in Chapter 20.




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net