Sometime you want to create a directory with upper limit on size in linux, this could be to restrict usage of a applicaiton or a user or for any other reason. There is no direct way to set limit on quota/size limit on a direcotry in linux. But as the saying goes, you can do anything on linux.
One common way of having size limit directories is creating a virual mount point. Here are the steps to create Vitual Mount Point. Here are the steps for the same:
- Create a directory for filesystem image
- Create a filesystem on disk, my preferred way is to use
fallocate -l 100G /vdisk/my_disk.img
Alternatively you can use
dd if=/dev/zero of=/vdisk/my_disk.img
- Create fileystem on the disk image, I use
xfsbut you can use ex3 or ext4.
- Create the desired directory to mount the disk.
mkdir -p /opt/data/my_disk
- Mount the virtual disk.
mount -t auto -o loop /vdisk/my_disk.img /opt/data/my_disk
mount -t auto -o loop,rw,usrquota,grpquota /vdisk/my_disk.img /opt/data/my_disk
If you would like to set user or group quotas.
- Add the mount to
/etc/fstabto make it permanent.