Limit directory size in linux

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

mkfs.ext3 my_file

And mount

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

4 thoughts on “Limit directory size in linux

    1. Yes, I have, otherwise I wouldn’t have written the post. It works.
      I also use a file as a filesystem to make a bigger /tmp without repartitioning the disk. I also used it to make a smaller swap.

  1. Do you know how efficient this is compared to normal file access? What would happen if you make several dozen virtual directories like this?

    1. Unfortunately, I can’t say anything about efficiency, especially about having many virtual directories. But I don’t think it would be any less efficient than having the same number of “real” partitions. As I understand it, file access is done the same way, since it’s formatted like a regular partition. There might be a tiny overhead for the kernel to know that the file is in a different partition, but it would be same for “real” partitions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s