Tuesday, October 29, 2013

Limiting your users use of disk space with quotas

When you run a multi-user system it's possible for a single user to unduly hog the system, by filling their home directory with a lot of files, and filling a disk so that other users have no space of their own. Quotas are a system of preventing this. It's possible to setup limits on the amount of space a single user, or a single group, can use.
Using quotas on Debian is very straight forward as the Debian kernel packages all have quota support compiled in.
There are two ways to use quotas:
  • Per user
  • Per group
When using per-user quotas you are effectively giving a limit on how much disk space the specific user may consume. In the case of per-group quotas you're giving a limit on the total disk usage of all members in that group combined.
Generally I find it much more useful to apply quotas on a per-user basis, as this way you dont have to work out which member of a group is consuming all the space.
To setup quota usage on your system you'll need to do three things:
  • Mount your filesystems with quota support
  • Install the quota software
  • Configure your limits
Quota support is enabled as the filesystems are mounted, and so it must be specified in the options section of your /etc/fstab file.
Specifying the quota options is as simple as adding usrquota for per-user quota, or grpquota for per-group quotes in the options column.
For example this first entry without quotas:
/dev/hda1       /home       ext2    defaults           1       1
Would become the following with per-user quotas enabled:
/dev/hda1       /home       ext2    defaults,usrquota  1       1
Once this has been added you can remount the partition by rebooting, and install the software. This will setup the partitions to be mounted with quota support enabled, however you haven't enabled them for the running system.
To change the options run:
mount -o remount,usrquota /home
(Repeat for any other filesystems you wish to modify).
You will also need to load the quota kernel module, and cause that to be loaded at boot time. You can do that by running:
modprobe quota_v2
echo 'quota_v2' >> /etc/modules
The quota system has a notion of a soft limit and a hard limit. A soft limit is a limit which the user can live with for a while (by default 7 days). A hard limit is a limit which cannot be crossed ever.
These limits are recorded inside special files which we need to create for each filesystem which is being used with quotas.
Continuing with our example of /home we would create one file for group quotas and one file for user quotas:
touch /home/aquota.user
touch /home/aquota.group
chmod 600 /home/aquota.user /home/aquota.group
Now we're ready to install the software.
apt-get install quota quotatool
As you installed the software you will have been prompted to see if you wish to email your users when their quotas are exceeded, this is their soft limit.
If you don't reboot you'll need to turn on the quota system by running:
quotacheck -vagum
To set the soft limits to 200Mb and the hard limit to 250Mb for a user called skx you would run:
quotatool -u skx -bq 200M -l '250 Mb' /home
A cronjob will run every day to check and warn them if they exceed their soft quota (if you said 'Yes' to the email notification question when the packages were installed), and the user will be informed immediately if they exceed their hard quota:
skx@undecided:~/Articles$ mkdir f
mkdir: cannot create directory `f': Disk quota exceeded
skx@undecided:~/Articles$ 
The system administrator can see what the disk and quota usage is like by invoking:
repquota /home
If ever you wish to remove a quota for a user simply set their hard and soft limits to '0'.

No comments:

Post a Comment