How to configure Quota on debian 7 wheezy

Posted by Md. Mahidul Hasan on 11:54 AM with No comments
How to configure Quota on debian 7 wheezy

Topics:  Quotas, Hard quota, Soft quota, Inode, Grace period, dump, tcpdump, Mount, Mount point


CONCEPT:

Generally we user two types of quota. One is based on folder/directory and the other one is based on the user/group.
. user & group quota
. folder quota
. quota with webmin
. warnquota

Configuration:
Please follow the below two configuration for the user/group and for folder quota. And follow the explanation at the end of the document.


Virtual Directory/Folder Quota
Now, If you do not with to quota on user or group basis you can use the Virtual Directory Quota. Linux does not permit quota on directory. That's why we will create a virtual drive first and then we will quota on it. So, to make a quota on a directory without having to resort to quota software or manual usage checking, it utilizes Linux virtual filesystem support to do the trick. The following assumes we want a ~100MB quota on a directory.

1. Make a mount point. Here my mount point folder is /mnt/quotadir and mount point image is /mnt/quota.img. But you can set the location anywhere you want.
root@mail:~# mkdir -p /mnt/quotadir

2. Create an image file for the virtual file system. create a file full of /dev/zero, large enough to the maximum size you want to reserve for the virtual file-system.
root@mail:~# dd if=/dev/zero of=/mnt/quota.img bs=1024 count=100000

3. Format the drive. format this file with an ext3 file-system (you can format a disk space even if it is not a block device, but double check the syntax of every - dangerous - formatting command)
root@mail:~# mkfs.ext3 /mnt/quota.img

4. Mount the drive. mount the newly formatted disk space in the directory you've created as mount point, e.g.
root@mail:~# mount -o loop /mnt/quota.img /mnt/quotadir

or we can also done the job like the below way,
root@mail:~# mount -o loop,rw,usrquota,grpquota /mnt/quota.img /mnt/quotadir

As a result you now have a directory in /path/of/mount/point with a size limitation.

5. But this partition will be temporary. If you restart the server this partition will be removed. So, to make it a permanent patition add the fstab entry like the below way,

root@mail:~# vim /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system>                 <mount point>   <type>          <options>       <dump>  <pass>
proc                            /proc           proc            defaults        0       0

# / was on /dev/sda6 during installation
UUID=2dcfd294-49ae-4fcd-ad0f-6542d6f1db0b     /               ext3            errors=remount-ro 0     1

# /boot was on /dev/sda1 during installation
UUID=2307603e-b7a0-4d2f-a652-6f88893a5c98     /boot           ext3            defaults        0       2

# swap was on /dev/sda5 during installation
UUID=bf5dacd2-cc26-419c-990b-74509168362c     none            swap            sw              0       0

/dev/scd0                       /media/cdrom0   udf,iso9660         user,noauto      0       0
/mnt/quota.img                   /mnt/quotadir      ext3            loop            1     2

6. To check the entry use the following command,
root@mail:~# more /etc/mtab
# <file system> <mount point>   <type>  <options>               <dump>  <pass>
/dev/sda6     /         ext3    rw,errors=remount-ro         0     0
tmpfs         /lib/init/rw     tmpfs    rw,nosuid,mode=0755         0     0
proc         /proc         proc    rw,noexec,nosuid,nodev         0     0
sysfs         /sys        sysfs    rw,noexec,nosuid,nodev         0     0
udev         /dev        tmpfs    rw,mode=0755             0     0
tmpfs         /dev/shm     tmpfs     rw,nosuid,nodev         0     0
devpts         /dev/pts     devpts    rw,noexec,nosuid,gid=5,mode=620 0     0
/dev/sda1     /boot         ext3     rw                 0     0
/dev/loop0     /mnt/quotadir     ext3     rw                 0     0

7. Now If you wish to add/reduce space from 100MB to 10MB on (trim the size of) the directory,
root@mail:~# umount /mnt/quotadir
root@mail:~# e2fsck -f /mnt/quota.img
root@mail:~# resize2fs -p /mnt/quota.img 10M
root@mail:~# mount -o loop /mnt/quota.img /mnt/quotadir

or we can also do the job like the below way,
root@mail:~# mount -o loop,rw,usrquota,grpquota /mnt/quota.img /mnt/quotadir

I also found a similar set of tools for reiserfs, couldn't find a resize tool for jfs. In this solution the space gets eaten away (about 2MB) from the host partition(s), use with caution, be sure to save some space for future expansion.


user & group Quota
root@mail:~# apt-get install quota quotatool

root@mail:~# vim /etc/fstab
# <file system>                 <mount point>   <type>      <options>       <dump>  <pass>
proc                            /proc           proc        defaults        0       0
# / was on /dev/sda6 during installation
UUID=2dcfd294-49ae-4fcd-ad0f-6542d6f1db0b     /        ext3         errors=remount-ro,usrquota,grpquota 0   1
# /boot was on /dev/sda1 during installation
UUID=2307603e-b7a0-4d2f-a652-6f88893a5c98     /boot           ext3        defaults      0       2
# swap was on /dev/sda5 during installation
UUID=bf5dacd2-cc26-419c-990b-74509168362c     none            swap        sw            0       0
/dev/scd0                       /media/cdrom0   udf,iso9660     user,noauto     0       0

Create The Partition Quota Configuration Files. The uppermost directory of the filesystem needs to have an aquota.user file (defines quotas by user) and an aquota.group file (defines quotas by group), or both. The man page for quota lists them at the bottom. In this case just enable per-user quotas for the /home filesystem. 

create files quota.user and quota.group under root.
root@mail:~# touch quota.user quota.group

root@mail:~# chmod 600 quota*

root@mail:~# ls -l
    total 4
    drwx------ 2 root root 4096 May  4 10:58 mail
    -rw------- 1 root root    0 May  5 19:44 quota.group
    -rw------- 1 root root    0 May  5 19:44 quota.user

Editing the /etc/fstab file isn't enough, Linux needs to reread the file to get its instructions for /home. You can do this using the mount command with the -o remount qualifier. 

root@mail:~# mount -o remount /

root@mail:~# cat /etc/mtab
    /dev/sda6 / ext3 rw,errors=remount-ro,usrquota,grpquota 0 0
    tmpfs /lib/init/rw tmpfs rw,nosuid,mode=0755 0 0
    proc /proc proc rw,noexec,nosuid,nodev 0 0
    sysfs /sys sysfs rw,noexec,nosuid,nodev 0 0
    udev /dev tmpfs rw,mode=0755 0 0
    tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
    devpts /dev/pts devpts rw,noexec,nosuid,gid=5,mode=620 0 0
    /dev/sda1 /boot ext3 rw 0 0

root@mail:~# quotacheck -mavug
    Here,
    m = don't try to remount the file syatem
    a = all file system in /etc/mtab
    v = verbose, display extra information
    u = only user quotas listed in /etc/mtab
    g = only group quotas listed in /etc/mtab

root@mail:~# quotaon -avug
    Here,
    a = all file systems in /etc/fstab with quotas
    v = verbose, display extra information
    u = users, manipulate user quotas
    g = groups, manipulate group quotas

root@mail:~# cd /home/mahidul

To set the soft limits to 4Mb and the hard limit to 5Mb for a user called mahidul you would run:
root@mail:~# quotatool -u mahidul -bq 4M -l '5 Mb' /home
or
root@mail:/home/mahidul# edquota -u mahidul
GNU nano 2.2.4                          File: /tmp//EdP.ayMEu4F

Disk quotas for user mahidul (uid 1000):
Filesystem                                   blocks     soft    hard    inodes    soft    hard
/dev/disk/by-uuid/2dcfd294-49ae-4fcd-ad0f-6542d6f1db0b   5096      4096       5096    6        0        0

Here,
    blocks: 1k blocks
    inodes: Number of entries in directory file
    soft: Max number of blocks/inodes user may have on partition before warning is issued and grace persiod countdown begins.
    If set to "0" (zero) then no limit is enforced.
    hard: Max number of blocks/inodes user may have on partition.
    If set to "0" (zero) then no limit is enforced.

If needed group quotas use below option, 
root@mail:~# edquota -g group_name

Set grace period before enforcing soft limits for users,
root@mail:~# 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/disk/by-uuid/2dcfd294-49ae-4fcd-ad0f-6542d6f1db0b         7days       7days

The system administrator can see what the disk and quota usage is like,
root@mail:~# repquota /
*** Report for user quotas on device /dev/disk/by-uuid/2dcfd294-49ae-4fcd-ad0f-6542d6f1db0b
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --  683356       0       0          24790     0     0
man       --     844       0       0            102     0     0
proxy     --   35372       0       0           5437     0     0
www-data  --      92       0       0             25     0     0
libuuid   --       4       0       0              1     0     0
mahidul   +-    5096    4096    5096  6days       6     0     0
test      --     100       0       0             21     0     0
info      --      80       0       0             18     0     0
soykot    --      16       0       0              4     0     0
smmta     --      52       0       0             10     0     0
smmsp     --       8       0       0              2     0     0
messagebus --      4       0       0              1     0     0


root@mail:/home/mahidul# su - mahidul

Now, copy some files to test your quota,
mahidul@mail:~$ dd if=/dev/zero of=bigfile bs=1M count=3
    3+0 records in
    3+0 records out
    3145728 bytes (3.1 MB) copied, 0.122912 s, 25.6 MB/s

mahidul@mail:~$ dd if=/dev/zero of=bigfile bs=1M count=4
    sda6: warning, user block quota exceeded.
    4+0 records in
    4+0 records out
    4194304 bytes (4.2 MB) copied, 0.153786 s, 27.3 MB/s

mahidul@mail:~$ dd if=/dev/zero of=bigfile bs=1M count=5
    sda6: warning, user block quota exceeded.
    sda6: write failed, user block limit reached.
    dd: writing `bigfile': Disk quota exceeded
    5+0 records in
    4+0 records out
    5185536 bytes (5.2 MB) copied, 0.206898 s, 25.1 MB/s

Remember if we need, we can off the quota like this way,
root@mail:~# quotaoff -avug



Quota with webmin:
==================
mahidul@mahidul:~$ cp /etc/fstab /etc/fstab.orig
mahidul@mahidul:~$ sudo vim /etc/fstab
UUID=17ef55fd-bfb9-4052-a2a3-b6f960a2c6dd /home               ext3    defaults,usrquota,grpquota,errors=remount-ro 0       1

mahidul@mahidul:~$ mount -o remount,usrquota /home 
mahidul@mahidul:~$ mount | grep quota
mahidul@mahidul:~$ modprobe quota_v2 echo 'quota_v2' >> /etc/modules ###this line may not work but it will not cause any problem

#apt-get install quota quotatool
#touch /home/aquota.user 
#touch /home/aquota.group 
#chmod 600 /home/aquota.user /home/aquota.group 

turn on quatacheck without rebooting:
# quotacheck -vagum

If your kernel supports journaled quota but you are not using it you’ll probably get an error. Use this command in that case:
# quotacheck -fvagum

Set limits for user:
#quotatool -u someusername -bq 100M -l '200 Mb' /home 

Check quotas:
#repquota /home 



Webmin installation
===================
root@ns1:~# vim /etc/apt/sources.list
deb http://download.webmin.com/download/repository sarge contrib     //add this line

root@ns1:~# cd /root
root@ns1:~# wget http://www.webmin.com/jcameron-key.asc
root@ns1:~# apt-key add jcameron-key.asc
root@ns1:~# apt-get update
root@ns1:~# apt-get install webmin
root@ns1:~# apt-get install quota quota-tools

You should now be able to login to Webmin at the URL http://localhost:10000/. 
Or if accessing it remotely, https://your_ip_address:10000/. 

If you get any dependency error install these (easy) dependencies:
Run this from a terminal. Expect some trouble from ‘libmd5-perl’.
root@ns1:~# sudo aptitude -y install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl libmd5-perl apt-show-versions libapt-pkg-perl

Install the deprecated dependencies: 
Download the libmd5-perl deb file and install it manually: Open a browser and get the newest libmd5-perl package (from 2004 – lol)
http://ftp.debian.org/pool/main/libm/libmd5-perl/. The likely package is named: libmd5-perl_2.03-1_all.deb

so we download it and install it:

root@ns1:~# wget http://ftp.debian.org/pool/main/libm/libmd5-perl/libmd5-perl_2.03-1_all.deb
root@ns1:~# sudo dpkg -i libmd5-perl_2.03-1_all.deb

 
Alternative way, you can also install webmin from deb file.
root@ns1:~# wget http://downloads.sourceforge.net/project/webadmin/webmin/1.510/webmin_1.510-2_all.deb
root@ns1:~# sudo dpkg -i webmin_1.510-2_all.deb

Open your web browser and enter the following address:
https://server_ip_address:10000/

Login user will be root and password will be root's password.



Apply quota from wibmin:
Go to your browser https://mahidul:10000/
. Disk and Network file system > Use quotas : user and group > save
. System > Disk Quota > Edit user quota : mahidul > Edit quota on > Give the soft and hard limit here > Update


Apply warnquota: To Install webmin module for quotas
root@ns1:~# apt-get install webmin-quota

email warnings are triggered by warnquota
warnquota takes its configuration in /etc/warnquota.conf
warnquota is ran daily by default : /etc/cron.daily/quota

Here is my /etc/warnquota.conf
MAIL_CMD       = "/usr/sbin/sendmail -t"
FROM            = "your.address@your.domain.com"
SUBJECT         = You have exceeded your quota
CC_TO           = "your.address@your.domain.com"
SUPPORT         = "support.address@your.domain.com"
PHONE           = "01 02 03 04 05"
MESSAGE         = "quota exceed for this user"
SIGNATURE       = "Your signature"

Try dpkg-reconfigure -plow quota, it should ask you questions about warnquota configuration.