I wanted to limit the size of specific directories where output is written in a Linux server running Apache. I didn’t want to use quotas because it doesn’t really solve my problem. If www-data fills its partition, other www-data programs will still suffer from lack of disk space. So I really wanted to limit the size of one single directory.
The solution I found is to use a file as a filesystem, which I got from this thread http://www.justskins.com/forums/limit-directory-size-123005.html
Simply create a file with dd, format as ext3 and mount with -o loop as a directory. The file will have a maximum size given by dd.
dd if=/dev/zero of=/home/user/my_file bs=1024 count=5000000
bs specifies block size and count specifies the number of blocks, in the example above you’ll get 5Gb. Use anything you want.
Now format the file
sudo mount my_file /mnt/my_file -o loop
Add an entry to /etc/fstab so it mounts on boot
/home/user/my_file /mnt/my_file ext3 loop