Библиотека сайта rus-linux.net
Linux System Administrator's Survival Guide lsg21.htm
Chapter 21
Managing Resources
Even with today's high-capacity hard disks at reasonable prices, disk space shortages are chronic (especially on multiuser systems). Even the largest hard drives can get reduced to small available space when multiple operating systems, the full Linux system, swap space, and several users are brought into play. To combat this space shortage, there are several ways to manage the disk space you have available more effectively.
For multiuser systems, the ideal solution is to restrict the amount of space each user can use. This concept was first implemented in BSD UNIX with the quota command; the command then carried over to most versions of the software. Linux, because it is based primarily on BSD UNIX, also includes the quota system. This chapter looks at the quota command and how you can use it.
Understanding Quotas
One of the best tools for managing resources is quota and its attendant utilities. The quota tool is used to display users' disk usage and their limits. When invoked, quota scans the /etc/fstab file and checks disk usage in the order of filesystems in the /etc/fstab file.
Quotas are preassigned amounts of disk space that a user or group can occupy. Normally, the limits do not prevent users or groups from exceeding their allotment, but exceeding the limits can result in warning messages appearing on-screen and usage reports being sent to root.
Hard and Soft Limits
Even if a user is considerably over quota, restricting the user from saving information can be difficult and may be the wrong thing to do in many cases. To help enforce restrictions and minimize complications, though, limits come in two types: soft and hard. A hard limit cannot be exceeded regardless of circumstances. If the user is trying to save valuable information and is over the hard limit, something has to go first. Because a user receives no warning when they are approaching a hard limit, this step is rather drastic, but it can be necessary with some users. A soft limit allows users to exceed their quotas for a while, but they get warning messages. You can set the system to allow only so many warnings before imposing a hard limit. Ideally, you should set your system to have a soft limit somewhat smaller than a hard limit, so users get warnings before they are unable to save anything else. Just setting a hard limit with no warning mechanism can result in annoyed users!
When to Use Quotas
Not all filesystems need quotas. If you have several hard drives broken into filesystems, you may use some for unlimited storage, and others, which are near capacity, may need quotas. The decision as to which filesystems require quotas is up to you. Most versions of Linux that adhere closely to the BSD standards require a modification in the /etc/fstab file to indicate that the filesystem uses quotas. The word quota must go in the fourth column of the file, as in the following entry:
/dev/sda3 /usr rw,quota 1 3
This entry indicates that quotas are in place on the /usr filesystem. If the entry has the keyword noquota, you can change it to quota.
Setting User Quotas
The system administrator sets user quotas in a file called quota.user in the root directory of the filesystem to which the quotas apply. Similarly, group quotas (if used) are set in the file quota.group also in the root directory of the filesystem. You need to manually create the quota files, either by using cat to save a blank file or by using touch. The following commands show how to set quotas on the /usr filesystem:
cd /usr
touch quota.user
chmod 600 quota.user
touch quota.group
chmod 600 quota.group
The chmod command makes the file writable by root only. You set the quota limits with the edquota command, which is usable only by root. Follow edquota with the name of the user (or multiple users) or group that you want to set quotas for, as in the following example:
/usr/etc/edquota tparker ychow bsmallwood
You may not need to specify the path if the edquota command is in the default system search path. This command starts an editor (the default is vi). If you provide a group name, a temporary file stores information about the users in the group, and then writes the information to the quota.group file afterwards.
You can control edquota's behavior with a number of options. These options are usually supported by Linux' version of edquota:
-g | Edit a group's quota |
-p | Duplicate quotas of one user for others |
-t | Edit soft time limits (before a hard limit is imposed) |
-u | Edit a user's quota (default action) |
If you want to use the same quota entries for several users, you need edit only one and then use the -p option to duplicate the entries. For example, the command
edquota -p tparker ychow bsmallwood
uses the tparker user entries for ychow and bsmallwood.
After you set the quotas you want to use on the filesystems, you must turn on the quota system with the quotaon command. You can turn on quotas for a single filesystem with the command
quotaon /dev/sda3
which turns on quota checking for the partition /dev/sda3. To turn on quota checking for all partitions, use the following command:
quotaon -a
The quotaoff command, not surprisingly, performs the reverse action with the same arguments.
Using the quota Command
The syntax for the quota command is as follows:
quota [options] [user] [group]
These options for the quota command are valid:
-g | Displays group quotas for the group of which the user is a member |
-q | Displays only filesystems where usage is over quota |
-u | Displays quotas for users (the default action) |
-v | Displays quotas on a filesystem where no storage is allocated |
You can combine these options. For example, if you use both -g and -u, both the user and group quotas are displayed. The system administrator can display quotas for all users with the -u option followed by a username, as in the following:
quota -u tparker
You can only use the command with this option when logged in as root. If a non-root users issue the quota command, they can display only their user quotas and their group's quota.
Using the quotacheck Command
When you have quotas in place and running, you can use the quotacheck command at any time to scan your filesystems for current disk usage. The results of the scan are written into the quota.user and quota.group files in the filesystem root directories.
Set the quotacheck command to run every time the system is booted so that the quota files are updated automatically. The best way to accomplish this task is to place the quotacheck command in the rc startup files. Because checking filesystems can take some time, use this command primarily with multiuser systems that see a lot of use. Otherwise, get in the habit of using quotacheck at intervals or when an error has occurred on a mounted filesystem.
The quotacheck command accepts a number of options to display different information:
Using quotacheck regularly helps make sure the filesystem quotas are being followed.
Summary
The quota system lets you set limits on the amount of disk space your users occupy in order to prevent potential problems when capacities are exceeded. Using quotas also makes sense when you are sharing your system with friends, but don't want them to take your system for granted as a storage depot!