Tuesday, May 11, 2010

How to Implement Disk Quota in Linux

Disk quotas can be configured for individual users as well as user groups. This kind of flexibility makes it possible to give each user a small quota to handle "personal" file (such as email and reports), while allowing the projects they work on to have more sizable quotas (assuming the projects are given their own groups).
In addition, quotas can be set not just to control the number of disk blocks consumed but to control the number of inodes. Because inodes are used to contain file-related information, this allows control over the number of files that can be created.
The quota RPM must be installed to implement disk quotas

Configuring Disk Quotas
To implement disk quotas, use the following steps:
1.       Enable quotas per file system by modifying /etc/fstab
2.       Remount the file system(s)
3.       Create the quota files and generate the disk usage table
4.       Assign quotas
 Enabling Quotas
As root, using the text editor of your choice, add the usrquota and/or grpquota options to the file systems that require quotas:





In this example, I have set /home file system has both user and group quotas enabled. After adding the userquota and grpquota options, remount each file system whose fstab entry has been modified. If the file system is not in use by any process, use the umount command followed by the mount to remount the file system. If the file system is currently in use, the easiest method for remounting the file system is to reboot the system.  For better practice please unmount  filesystem, or use single user mode.

Creating Quota Files

After each quota-enabled file system is remounted, the system is now capable of working with disk quotas. However, the file system itself is not yet ready to support quotas. The next step is to run the quotacheck command.
The quotacheck command examines quota-enabled file systems and builds a table of the current disk usage per file system. The table is then used to update the operating system's copy of disk usage. In addition, the file system's disk quota files are updated.

To create the quota files (aquota.user and aquota.group) on the file system, use the -c option of the quotacheck command. For example, if user and group quotas are enabled for the /home partition, create the files in the /home directory:

 
  The -a option means that all mounted non-NFS file systems in /etc/mtab are checked to see if quotas are enabled. The -c option specifies that the quota files should be created for each file system with quotas enabled, the -u specifies to check for user quotas, and the -g option specifies to check for group quotas.
If neither the -u or -g options are specified; only the user quota file is created. If only -g is specified, only the group quota file is created.
After the files are created, run the following command to generate the table of current disk usage per file system with quotas enabled:


The options used are as follows:
·         a — Check all quota-enabled, locally-mounted file systems
·         v — Display verbose status information as the quota check proceeds
·         u — Check user disk quota information
·         g — Check group disk quota information
After quotacheck has finished running, the quota files corresponding to the enabled quotas (user and/or group) are populated with data for each quota-enabled file system such as /home

Assigning Quotas per User

The last step is assigning the disk quotas with the edquota command.
To configure the quota for a user, as root in a shell prompt, execute the command:







 or you can use edquota –e
Perform this step for each user for which you want to implement a quota. For example, if a quota is enabled in /etc/fstab for the /home partition (/dev/hda3) and the command edquota testuser is executed, the following is shown in the editor configured as the default for the system





The first column is the name of the file system that has a quota enabled for it. The second column shows how many blocks the user is currently using. The next two columns are used to set soft and hard block limits for the user on the file system. The inodes column shows how many inodes the user is currently using. The last two columns are used to set the soft and hard inode limits for the user on the file system.
A hard limit is the absolute maximum amount of disk space that a user or group can use. Once this limit is reached, no further disk space can be used.
The soft limit defines the maximum amount of disk space that can be used. However, unlike the hard limit, the soft limit can be exceeded for a certain amount of time. That time is known as the grace period. The grace period can be expressed in seconds, minutes, hours, days, weeks, or months.


If any of the values are set to 0, that limit is not set. In the text editor, change the desired limits. For example:

To verify that the quota for the user has been set, use the command:






Assigning Quotas per Group

Quotas can also be assigned on a per-group basis. For example, to set a group quota for the devel group, use the command (the group must exist prior to setting the group quota): 










This command displays the existing quota for the group in the text editor:




Please observe here limits are set interns of disk blocks & inodes
Modify the limits, save the file, and then configure the quota.
To verify that the group quota has been set, use the command:




Assigning Quotas per File System

To assign quotas based on each file system enabled for quotas, use the command:




Like the other edquota commands, this one opens the current quotas for the file system in the text editor:




Change the block grace period or inode grace period, save the changes to the file, and exit the text editor.
  Managing Disk Quotas
If quotas are implemented, they need some maintenance — mostly in the form of watching to see if the quotas are exceeded and making sure the quotas are accurate. Of course, if users repeatedly exceeds their quotas or consistently reaches their soft limits, a system administrator has a few choices to make depending on what type of users they are and how much disk space impacts their work. The administrator can either help the user determine how to use less disk space or increase the user's disk quota if needed.

Reporting on Disk Quotas

Creating a disk usage report entails running the repquota utility. For example, the command repquota /home produce this output: 






To view the disk usage report for all quota-enabled file systems, use the command:
#repquota -a



While the report is easy to read, a few points should be explained. The -- displayed after each user is a quick way to determine whether the block or inode limits have been exceeded. If either soft limit is exceeded, a + appears in place of the corresponding -; the first - represents the block limit, and the second represents the inode limit.
The grace columns are normally blank. If a soft limit has been exceeded, the column contains a time specification equal to the amount of time remaining on the grace period. If the grace period has expired, none appears in its place.

Keeping Quotas Accurate

Whenever a file system is not unmounted cleanly (due to a system crash, for example), it is necessary to run quotacheck. However, quotacheck can be run on a regular basis, even if the system has not crashed. Running the following command periodically keeps the quotas more accurate 
#quotacheck -avug

You can set the above command in CRON jobs also for periodic output.

Enabling and Disabling

It is possible to disable quotas without setting them to be 0. To turn all user and group quotas off, use the following command: 
# quotaoff -vaug


If neither the -u or -g options are specified, only the user quotas are disabled. If only -g is specified, only group quotas are disabled.
To enable quotas again, use the quotaon command with the same options.
For example, to enable user and group quotas for all file systems: 
#quotaon -vaug

To enable quotas for a specific file system, such as /home
#quotaon -vug /home

If neither the -u or -g options are specified, only the user quotas are enabled. If only -g is specified, only group quotas are enabled.

Edquota Note:
The "edquota" command puts you into a "vi" editing mode so knowledge of the "vi" editor is necessary. Another editor may be specified with the EDITOR environment variable. You are NOT editing the quota.user file directly. The /partition/quota.user or quota.group file is a binary file which you do not edit directly. The command edquota gives you an ascii  interface with the text prepared for you. When you ":wq" to save the file from the vi session, it is converted to binary by the edquota command and stored in the quota.user file.
Assigning quota for a bunch of users with the same value. To rapidly set quotas for all users, on my system to the same value as user user1, I would first edit user user1's quota information by hand, then execute:
  edquota -p user1 `awk -F: '$3 > 499 {print $1}' /etc/passwd`  or

We can use  edquota –p -u
This assumes that the user uid's start from 500 and increment upwards. "blocks in use" is the total number of blocks (in kilobytes) a user has consumed on a partition. "Inodes in use" is the total number of files a user has on a partition. 






Soft Limit and Hard Limits:
Soft limit indicates the maximum amount of disk usage a quota user has on a partition. When combined with "grace period", it acts as the border line, which a quota user is issued warnings about his impending quota violation when passed. Hard limit works only when "grace period" is set. It specifies the absolute limit on the disk usage, which a quota user can't go beyond his "hard limit".
Grace Period:
"Grace Period" is configured with the command "edquota -t", "grace period" is a time limit before the "soft limit" is enforced for a file system with quota enabled. Time units of sec(onds), min(utes), hour(s), day(s), week(s), and month(s) can be used. This is what you'll see with the command "edquota -t":


No comments:

Post a Comment