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 LimitsA word or two about hard and soft limitsand the grace periodare in order here:
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:
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. |