Monday, August 24, 2009

Disk Quota in Linux

http://linuxhelp.blogspot.com/2005/10/disk-quotas-in-linux-explained.html

Disk Quotas in GNU/Linux explained

Have you ever encountered a situation where your children who are using your PC are hoarding music and video on the harddisk and filling up all the space ? In linux, there is a way for you to prohibit others from hogging all the disk space. This you do by using quotas. Here I will explain how to setup disk quotas in Linux.
Setting up disk quotas
In this example, let us assume that the /home is on its own seperate partition and it is running out of space. So we use quota system to manage and restrict disk space to all users (or a select few).
  1. Enter Single User mode - As we'll need to remount the /home filesystem it's best to ensure that no other users or processes are using it. This is best achieved by entering single user mode from the console. This may be unnecessary if you are certain that you're the only user on the system.
    # init 1
  2. Edit your /etc/fstab file - You'll need to add the usrquota option to the /etc/fstab file to let it know that you are enabling user quotas in your file system.
    ---------------------------------------------------------- Old  /etc/fstab file LABEL=/home     /home   ext3    defaults            1   2 ---------------------------------------------------------- New /etc/fstab file LABEL=/home     /home   ext3    defaults,usrquota   1   2 ---------------------------------------------------------- 
  3. Remount your file system - Once you finish editing your /etc/fstab file, you have to remount your filesystem as follows :
    # mount -o remount /home
  4. Create aquota.user and/or aquota.group files - These are created in the top most directory of the file system. In our case /home. In our case, we are enabling only per user quotas so only aquota.user file is required.
    # touch /home/aquota.user # chmod 600 /home/aquota.user
  5. Make linux read the aquota.user file - This is done using the quotacheck command.
    # quotacheck -vagum
  6. Modify the user's quota information - Use the edquota command for this purpose.
    # edquota -u
    The above command will invoke the vi editor which will allow you to edit a number of fields.
    Disk quota for user ravi (uid 503):  Filesystem   blocks  soft  hard   inodes  soft  hard /dev/hda3      24     0      0       7      0     0     
    Blocks : The amount of space in 1k blocks the user is currently using
    inodes : The number of files the user is currently using.
    Soft Limit : The maximum blocks/inodes a quota user may have on a partition. The role of a soft limit changes if grace periods are used. When this occurs, the user is only warned that their soft limit has been exceeded. When the grace period expires, the user is barred from using additional disk space or files. When set to zero, limits are disabled.
    Hard Limit : The maximum blocks/inodes a quota user may have on a partition when a grace period is set. Users may exceed a soft limit, but they can never exceed their hard limit.
    In our example, we limit the user ravi to a maximum of 5MB of data storage on /dev/hda3 (/home) as follows:
    Disk quota for user ravi (uid 503):  Filesystem  blocks   soft   hard  inodes    soft   hard /dev/hda3    24      5000    0       7       0      0     
    Note: 5MB is used just for test purposes. A more realistic size will be '5 GB' if you are having a hard disk of size 20 GB.

  7. Get out of single user mode : Return to your original run level by typing either init 3 or init 5 commands.
Other quota commands

Editing grace periods
 # edquota -t 
This command sets the grace period for each filesystem. The grace period is a time limit before the soft limit is enforced for a quota enabled file system. Time units of seconds, minutes, hours, days, weeks and months can be used. This is what you will see with the 'edquota -t' command:

Grace period before enforcing soft limits for users : Time units may be : days, hours, minutes or seconds File       System Block grace period         Inode grace period /dev/hda3         7days                           7days 
Editing Group quotas
# edquota -g
Checking quotas regularly - Linux doesn't check quota usage each time a file is opened, you have to force it to process the aquota.user and aquota.group files periodically with the quotacheck command.You can setup a cron job to run a script similar to the one below to achieve this.
#!/bin/bash quotacheck -vagu 
Getting quota reports - The repquota command lists quota usage limits of all users of the system. Here is an example.
# repquota /home
 *** Report for user quotas on device /dev/hda3 Block grace time: 7days; Inode grace time: 7days Block limits File limits User   used   soft  hard   grace   used   soft   ------------------------------------------------ root  52696    0     0     1015     0      0 ... ... ... ravi   24      0     0      7       0      0

No comments: