Hack55.Prevent Disk Hogs with Quotas


Hack 55. Prevent Disk Hogs with Quotas

Wasting disk space can cost you resources and bloat backup times and storage requirements. Setting up disk quotas provides a quick solution.

Every network has one of those users who's the quintessential digital packrat, storing files and emails for years and years, regardless of their content or relative unimportance. With the growing popularity of digital media files that can range from 3 MB to 3 GB in size, these users can fill a disk to capacity in very little time. To prevent these types of users from crashing your server, consider implementing disk quotas to keep them in line.

5.11.1. Setting Up Disk Quotas

There are a few steps to setting up quotas, but it's a relatively simple process. After setting up quotas, you'll either have to reboot your system or manually unmount and remount any partitions to which you've added quotas. Adding and configuring disk quotas is best done while the system is in single-user mode or otherwise down for maintenance.

Let's first explore the basic concepts of disk quotas, which are soft and hard limits. The soft limit is the maximum number of disk blocks or inodes that the user can use. Once this number is exceeded, the user is warned and allowed to continue for a specified grace period. Once that grace period expires, the user may no longer allocate any additional blocks or inodes (depending on how you have the quota configured).

Hard limits are indeed hard. A hard limit may never be exceeded, and once it's reached the user will automatically be barred from using further disk space.

5.11.2. Installing the Quota Software

Your system may or may not already have the software for implementing and managing disk quotas installed. It is usually located in /sbin or /usr/sbin, depending on your Linux distribution. To check, su to root and use the which command to determine if the quotacheck package is installed, as in the following example:

 # which quotacheck /sbin/quotacheck 

If you get a response from the which command (as shown above), you have the quota software installed and can proceed to the next step. If you don't already have it, the latest version can be found at http://www.sourceforge.net/projects/linuxquota. The software installs with the typical commands:

 # ./configure, make, make install 

Alternatively, if you're using a package- or RPM-based distribution, you can install the quota software via your package manager. For instance, with Ubuntu or Debian you can simply issue the following command:

 $ sudo apt-get install quota 

This will install and configure the software for you. SUSE users can use YaST to accomplish the same thing. These days, many distributions come with quota enabled by default, though, so most likely you won't have to worry about it.

5.11.3. Entering Single-User Mode

To configure your partitions to work with quotas, you'll need to bring the system into single-user mode. If this is not possible for you, at the very least you'll want to make sure that no one is logged in when you start this process, and that the system will remain in a quiet state [Hack #3]. To bring the system down to single-user mode, physically log into the console and issue the following command:

 # init 1 

This will bring the system into single-user mode, thereby disabling all network services (such as ssh and ftp).

5.11.4. Editing /etc/fstab

Navigate to the /etc directory. Fire up vi (or your favorite text editor) and edit the file /etc/fstab. The contents of an /etc/fstab file from a typical one-disk system might look something like the following:

 LABEL=/ /  ext3 defaults    1 1 LABEL=/boot /boot  ext3 defaults    1 2 none /dev/pts devpts gid=5,mode=620 0 0 none /dev/shm tmpfs  defaults       0 0 LABEL=/data /data  ext3   defaults    1 3 none /proc  proc   defaults       0 0 none            /sys     sysfs  defaults       0 0 LABEL=SWAP-hda2 swap     swap   defaults       0 0 

The only change that you need to make to this file is to add the usrquota option next to the partitions on which you wish to enable disk quotas. Once you've done that, you're done editing the fstab filesimply save it and exit your text editor to commit your changes to the file.

Next, you'll need to remount your filesystems so that your changes to the filesystem mount options will take effect. If, for example, you want to remount the /data partition, you can do this by simply issuing the following command:

 # mount o remount /data 

Once the partition is remounted, you're ready to revert back into your original runlevel. To do so, issue the command init 5 or init 3, depending on what your original runlevel was.

5.11.5. Initializing the Quota Configuration Files

You'll then need to create two files in the root of each partition to which you've just added quotas. These two files are named aquota.user for userbased disk quotas and aquota.group for group-based quotas. You can create both of these files using the touch command. Make sure you change the access permissions with the command chmod 600. This will help prevent your disk quotas from being circumvented.

Once you've created these files, you'll need to import your user and group data into the quota files that you just created in each filesystem. This might take a long time if you had to do it by hand, but thankfully there's an automated utility to do it for you:

 # quotacheck vguam  

The options tell the quotacheck command to be verbose (v), to check group (g) and user (u) quotas on all (a) filesystems on which quotas have been enabled, and not to try to mount (m) the filesystem as read-only in order to do the check.

The first time you use the quotacheck command, it might return an error telling you that it can't save the quota settings. This is normal and can safely be ignored.

5.11.6. Configuring Your Quotas

Now that you've created and initialized the files, you may now edit the quota information. This can be accomplished with the edquota command. This command offers several options that are of interest to us. The three most prominent are u, g, and t. Using any of these options launches your default text editor to edit the relevant configuration files. The u flag allows you to edit the quotas on a per-user basis, while the g flag acts on a per-group basis (the t option will be explained in just a bit). Both of these configuration files are largely the same, so we'll just look at the user file here:

 $ sudo edquota -u jdouble Disk quotas for user jdouble (uid 1001):       Filesystem   blocks    soft     hard     inodes   soft   hard   /dev/hda1    100000    200000   250000   127       0        0 

As you can see, there are two main ways in which you may limit users: via the total number of blocks they can utilize, or via the total number of inodes (i.e., the total number of files the user may have on the partition). I tend to use blocks when allocating disk space, but you may of course do as you see fit. While assigning quotas, keep in mind that 1,000 blocks equals 1 MB. In the example above, you can see that the user jdouble is currently using 100 MB of space and has a soft limit of 200MB and a hard limit of 250MB. The listing under inodes tells us that jdouble has 127 files on the filesystem /dev/hda1. You might also note that as the hard and soft limits after the inodes listing are set to zero, there is no quota for the total number of files the user can have.

The edquota t command allows you to configure grace periods for your users. Grace periods are periods of time during which users are allowed to temporarily violate their disk quotas while they receive warnings regarding their disk utilization. Once the grace period has ended, the user may no longer violate her quota, and the soft-limit quota is enforced. You'll see something like this when you run this command:

 # 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/hda1                     3days                  99days 

Be sure to set something reasonable here so that your users have at least a few minutes to free up some space if they accidentally overrun their quotas.

Even with quotas enabled, you'll probably be interested to know which of your users are utilizing the most disk space. Thankfully, there's a built-in feature to handle that as well. The repquota command, which takes a directory or filesystem as an argument, will give you a brief report of your users' total disk utilization, as well as their configured hard and soft limits.

 # repquota / *** Report for user quotas on device /dev/hda1 Block grace time: 3days; Inode grace time: 99days Block limits                File limits User   used   soft   hard   grace  used  soft  hard  grace ----------------------------------------------------------------     root  -- 20932272      0       0     73865     0     0 daemon  --  44       0       0         4     0     0 man  --      396      0       0        21     0     0 news  --        4      0       0         1     0     0 postfix  --       88   0       0        45     0     0 jdouble  --   100000 200000  250000       127     0     0 klog  --        8      0       0         3     0     0 kida  --     2800      0       0       181     0     0 cupsys  --       72      0       0        11     0     0 fetchmail--        4      0       0         1     0     0 hal      --        8      0       0         2     0     0 

Through this report, you can see the disk utilization of every user on the system, including our test case jdouble. Any user with the default 0 under the hard or soft column is not subject to disk quotas.

By using the report feature within a cron job, you can be updated on disk utilization as often as you'd like. I have this information emailed to me every weekday morning so that I can keep track of my users and hunt down those pesky disk hogs. To do this, I added the following entry to root's crontab file using the crontab e command as root:

 0 5 * * * repquota -a 

This tells the cron processes to check all filesystems on which quotas are enabled at 5 A.M. every day and to mail the output of the repquota command to root. Configuring disk quotas can be a lifesaver if you run a heavily used, multi-user server and can be made even more powerful if you take advantage of the group features of disk quotas. The Linux quota system's group mechanism provides a way of creating different levels of users, from those that may only need 10MB of space all the way up to those who want (and actually need) gigabytes of space.

5.11.7. See Also

  • man edquota

  • man repquota

  • man quota

  • man quotacheck

  • man quotactl

Brian Warshawsky



Linux Server Hacks (Vol. 2)
BSD Sockets Programming from a Multi-Language Perspective (Programming Series)
ISBN: N/A
EAN: 2147483647
Year: 2003
Pages: 162
Authors: M. Tim Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net