Manage quota in Debian
Foreword
Quota are a method to handle disk usage on a per user/group basis: if you're running a multiuser system you'd very likely want to prevent a user from filling the entire /home partition (e.g.), maybe with videos or pictures or garbage, thus cutting off all the other user from the system forbidding them to write on the disk any more data.
How-to
- note: commands starting with a pound sign - # - are meant to be run as root user, while commands starting with a dollar sign - $ - have to be run as normal user.
First of all we need to remount our device (in this tutorial i'll talk about the /home partition) with the correct options. Let's
# nano /etc/fstab
and add the options "usrquota,grpquota" to the /home device, like this:
/dev/md2 /home ext3 defaults,usrquota,grpquota 0 1
Next we're going to remount that partition, in this way:
# mount -o remount /home
Try and issue the "mount" command by itself and check for your partition, it should now look as:
/dev/md2 on /home type ext3 (rw,usrquota,grpquota)
Good.
Now we can install the needed packages as in this example:
# apt-get install quota quotatool
and load the kernel module for the quota service:
# modprobe quota_v2
To make it load at boot time, issue an:
# echo 'quota_v2' >> /etc/modules
At this point we should issue two commmands to create the aquota.user and aquota.group files under your /home partition and to finally enable quota:
# quotacheck -F vfsv0 -avugm
# quotaon -avug
# /etc/init.d/quota start
You can use the "sysv-rc-conf" tool to enable the quota service at boot time.
Editing quota
Now that, hopefully, your quota system is up and running, let's see how to get the quota set:
# repquota /home
This commands shows an output with system users and quota applied to each of them (initially there should be none).
To actually set a particular quota for a user, you can try the "edquota user" command, given as root: you'll be presented with an editor and a line for each device where quota can be applied to tha user - modify the soft/hard record that you're interested in and save the changes. If you want to see the result, just issue the "quota user" command. Try again the repquota command and look to the changes... keeep editing your users quota one at a time untill you're done.
Testing
Login to a user with quota applied and run something similiar:
$ dd if=/dev/zero of=file_2GB bs=1024k count=2k
This command will create a 2 Gigabyte zero-filled file: if the quota soft/hard limits are reached, you'll be fisrt warned and then the process will fail.
Final advice
This kind of quota are not "journaled" and this means that if your system happens to crash you may loose some data about quota or get it corrupted - to solve this problem you'll have to run the following command:
# quotacheck -avugm
This kind of crashes often involve a system reboot, so you may consider the possibility to run the quotacheck command at every boot; this task can be done in several ways and i'm going to explain how i managed it: simply edit your root crontab and add the following line:
10 16 * * * /usr/bin/touch /forcequotacheck
The time is clearly irrilevant to us but it's important that this check will occur at least before your system may crash, so i'll keep it running once per day, no big deal. What cron will do is create a new empy file in your root directory, named exactly "forcequotacheck" and that file will trigger a "quotacheck" command at each and every reboot: the point is that the system , once completed the task will remove that file, so we need to recreate it (that is why we use cron).
The /forcequotacheck file is very similar to the /forcefsck one, witch instead triggers a filesystem check.