Linux disk quota

Arkadiusz Rychliński
3 min readMar 14, 2023

--

For some reason I wanted to set limits on disk usage in Linux. I’ll skip the description of the search for information on the Internet, but in a nutshell — for reasons unknown to me, for the most part, only old stuff is described. In the article I briefly describe how it looks like in current kernel versions.

How it used to be

It used to be like this: leaving out the XFS fielsystem, information about disk usage and inodes was stored in quota.user and quota.groupfiles. Then something was corrected and the new files are called aquota.user and aquota.group.

For the limits to work, the disk must be mounted with the usrquota and grpquota options. Another new feature introduced an improvement on how the current disk usage is recorded for the user. The mount options have changed to

usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv1

and so the disk usage files are updated along with file changes and go through the journal.

Then you have to run

sudo quotacheck -cugv /mnt/
sudo quotaon -ug /mnt

and disk limits are enabled.

But quotacheck command wrote me out a message:

quotacheck: Your kernel probably supports ext4 quota feature but you are 
using external quota files. Please switch your filesystem to use
ext4 quota feature as external quota files on ext4 are deprecated.

And so I’ve learned that ext4 supports limit files as hidden inodes, only you have to enable this option.

And this is how it looks now

So, what does it look like on the new kernel.

Assumptions:

  • I have a disk …. e.g. /dev/loop30,
  • I mount it on /mnt.

The kernel was built with the options:

  • CONFIGQUOTA=y
  • CONFIGQUOTA_TREE=m

We have the tools to handle quotas (such as apt install quota on Ubuntu)

We have the necessary kernel modules (sudo modprobe quota_v2 works).

The procedure is as follows:

Create a filesystem (ext4):

sudo mkfs.ext4 /dev/loop30

Enable internal quota handling (this is the latest news)

sudo tune2fs -O quota /dev/loop30

Feature quota means: “Enable internal file system quota inodes”.

And now one more thing: you can have not only per-user and per-group limits but also per-project limits. I have no idea how the latter works, but you can turn them all on and off independently (see man tune2fs):

sudo tune2fs -Q usrquota,grpquota,prjquota /dev/loop30

You can verify that the limits have been enabled by calling:

sudo tune2fs -l /dev/loop30 | grep “Filesystem features”
Filesystem features: has_journal ext_attr resize_inode dir_index filetype 
extent 64bit flex_bg sparse_super large_file huge_file dir_nlink
extra_isize quota metadata_csum project

The presence of the word “quota” means that the feature is enabled.

The next step: mount the filesystem.

sudo mount -o usrquota,grpquota,prjquota /dev/loop30 /mnt

It’s probably a good idea to put this mountpoint for yourself in /etc/fstab, but we’re simply going to enable the appropriate quota counting for this disk.

No limit files in /mnt can be seen, but we can run

sudo repquota /mnt

And we will see the consumption of this disk by the root user (because only his objects are in this directory for now).
By the way, repquotawith the -g parameter will show limits for groups, and -P limits for projects.

Limits can be set with the setquota and edquota commands (but this is already standard — every manual describes how to do it).

It seems that if we used usrquota, grpquota options when mounting, the limits are immediately enforced (without having to enable them with quotaon), which can be checked using the -p option:

sudo quotaon -p /mnt
Group limits on /mnt (/dev/loop30) are enabled
User limits on /mnt (/dev/loop30) are enabled
Project limits on /mnt (/dev/loop30) are enabled

And it works.

Links:

--

--