FreeBSD Quotas – bulk updates (using Perl)

Another day, another 63 pence (according to xe.com), and another potentially mind numbing repetitive FreeBSD admin operation. Todays request was to increase all of the web quotas to 1GB on a web server which has variable quotas set on a per user basis. Actually when we get to the nub of the task we need to increase the quotas of everybody set to less that 1GB to 1GB. Sounds like a job for Perl right? Well not entirely…

Free BSD does not have a single line command to edit quotas, nor is there a program like vipw for quotas to leverage. The standard method to modify a quota is to use the edquota command.

# edqota -u test
===== TEXT EDITOR OF CHOICE ======
Quotas for test dan:
/data: kbytes in use: 110234, limits (soft = 100000, hard = 110000)
        inodes in use: 25, limits (soft = 0, hard = 0)

You can then edit the quota settings as you see fit. This example is a 100MB quota on the data partition. All vey nice but not great for a bulk update, we need something that will run on a single command line. A quick search on Google and http://www.freebsd.org/ports soon reveals a program called ‘setquota’, so here we go:

# cd /usr/ports/sysutils/setquota/
# make install

This installation comes with the getquota and the setquota programs, here is the former in action:

# getquota -u -f /data testuser
 username  blocks   soft    hard    grace   files   soft    hard    grace
------------------------------------------------------------------------
testuser   72280 100000  110000 1308754423    6710      0       0 1188947345

Here is an example of using setquota:

# setquota  -u -f /data -bh1100000 -bs1000000 dan

So we should be able to work with this to write a perl script to change the quotas to our requirements. I didn’t like the output of the getquota so i’ll be using the output from:

# repquota -a

...
dan              --   110234  2000000  2100000      -       25       0       0      -
testuser         --    72280   100000   110000      -     6710       0       0      -
testuser2        --    59730   100000   110000      -     1863       0       0      -
...

So here is the simple script which checks the quota, and adjusts as needed:

#! /usr/bin/perl -w

use strict;

while (<>){
        my @temp_array = split;
        if ($temp_array[3] <= 1000000){
                print "$temp_array[0]\twas:$temp_array[3] moving to 1000000\n";
                system "setquota  -u -f /data -bh1100000 -bs1000000 $temp_array[0]";
        }else{
                print "$temp_array[0]\t$temp_array[3] is over 1GB so no change\n";
        }
}

I copied the result of repquota -a into a text file called quot.txt and removed all the non-relevant accounts manually. The output is as follows:

# ./resetquota.pl quot.txt 
...
dan	2000000 is over 1GB so no change
testuser	was:100000 moving to 1000000
testuser2	was:100000 moving to 1000000
...
This entry was posted in FreeBSD Administration, Perl and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *