Previous Section  < Day Day Up >  Next Section

Recipe 8.22. Using Disk Quotas

8.22.1 Problem

You want to limit the amount of disk storage any user can consume. Most sites have a few disk hogs around, who just love to fill the disk with their MP3 collections and downloaded sitcoms.

8.22.2 Solution

Use the Linux Disk Quota package. This contains several components, including quota, edquota, quotacheck, and repquota.

First, edit /etc/fstab and select the partitions to enable quotas. Your choices are usrquota, for individual users, or grpquota, for putting quotas on groups. It's okay to have both:

/dev/hda6  /      ext3 defaults                    0   1

/dev/hda7  /home  ext3 defaults,usrquota,grpquota  0   2

Now remount the filesystem:

# mount -o remount /home

Quota's init script will run quotacheck, which will examine the installation, create a database of disk usage, and create quota files.

Next, assign a quota to a user. This opens a configuration file in your default editor:

# edquota -u vhenson

Disk quotas for user vhenson (uid 1550):

 Filesystem  blocks   soft  hard   inodes   soft   hard

 /dev/hda7   550466    0     0      47466    0      0

Soft limits allow a grace period, with warnings to the user. Hard limits cut them off immediately. To set limits, simply edit the file:

# edquota -u vhenson

Disk quotas for user vhenson (uid 1550):

 Filesystem  blocks  soft    hard     inodes   soft   hard

 /dev/hda7   550466  650000  700000   47466    0      0

Blocks are always 1024 bytes, so 650,000 blocks is about 665 megabytes.

Save and close the file, and verify that the quota is in effect:

# quota vhenson

Disk quotas for user vhenson (uid 1550): 650000  700000

To assign a quota to a group use:

# edquota -g engineers

Note that if a single greedy user in a group uses up the group quota, it's too bad for the rest of the group—it's all gone.

This invokes the default editor, for setting the grace period for soft limits on the entire filesystem:

# edquota -t

Grace period before enforcing soft limits for users:

Time units may be: days, hours, minutes, or seconds

  Filesystem    Block grace period    Inode grace period

  /dev/hda3     7days                 7days

You can use one "prototype" user as the model for new users:

# edquota -p vhenson dnorth

or for a whole bale of users:

# edquota -p vhenson `awk -F: '$3 > 999 {print $1}' /etc/passwd`

Or you can list several at once:

# edquota -p vhenson dnorth jvesperman sanvin

You'll doubtless want to keep an eye on things. This repquota command gives a system-wide snapshot of current usage:

# repquota -a

For a specific filesystem, use:

# repquota /home

8.22.3 Discussion

Add this line to the mass_useradd script, after the chmod line, to automatically apply vhenson's quota to all the new users:

/usr/sbin/edquota -p vhenson $username

The adduser command can be configured to apply quotas. Edit adduser.conf:

QUOTAUSER="vhenson"

And now, the bad news. Quota is in a transition phase. The Quota code in the 2.2 and 2.4 kernel trees is obsolete and doesn't work with the current versions of Quota. If your distribution has kindly patched the kernel for you, you're good to go. You probably won't know for sure until you install and configure it and try to set a quota on a user (although it's possible that your distribution's documentation tells the story). If your distribution does not come with a ready-to-go version of Quota, you'll probably have to install a raft of kernel patches. See the "Quota mini-HOWTO" for complete instructions.

SuSE supplies the only reliable ready-to-go Quota-enabled ReiserFS, as of this writing. ReiserFS patches for other distributions are available from the ReiserFS home page at http://www.namesys.com.

The good news is that the 2.6 kernel tree fully supports Quota, so we won't have to go through all this fol-de-rol forever.

After passing the kernel hurdle, the next step is to install the latest Quota. It's best to find the RPM for your system (Debian users, use apt-get install quota). That way the init scripts will be set up and ready to go. If you find handcrafting init scripts to be an enjoyable activity, you can download the tarball and have at it. Instructions are in the "Quota mini-HOWTO."

8.22.4 See Also

    Previous Section  < Day Day Up >  Next Section