TExcelTAD
Home › Linux Server Administration › Disk Quotas

Disk Quotas

A shared filesystem is a shared risk: one user's runaway upload, one service's cache, one tenant's backups, and everyone else gets "No space left on device". Disk quotas let the kernel enforce a limit per user, per group or per directory tree ("project"), counted in blocks (space) and inodes (number of files). Setting them up needs root and a real filesystem, so the transcripts are sample sessions; the enforcement rules are modelled in Python.

Soft limit, hard limit, grace period

Limit Behaviour
hard can never be exceeded: the write fails with EDQUOT ("Disk quota exceeded")
soft may be exceeded for a grace period (default 7 days); after that the soft limit is enforced like a hard one until usage drops below it
grace period per filesystem, separately for blocks and inodes (setquota -t, edquota -t)

The soft limit is a warning with teeth: a user can finish today's job, but can't live above the limit.

python · try it yourself
DAY = 24 * 3600

def write(state, t, mib, soft=900, hard=1000, grace=7 * DAY):
    used = state["used"] + mib
    if used > hard:
        return f"day {t // DAY}: {mib:+} MiB -> EDQUOT (hard limit {hard} MiB)"
    if used > soft:
        state.setdefault("over_since", t)
        if t - state["over_since"] > grace:
            return f"day {t // DAY}: {mib:+} MiB -> EDQUOT (grace period over, still above soft {soft} MiB)"
    else:
        state.pop("over_since", None)
    state["used"] = used
    left = ""
    if "over_since" in state:
        left = f", grace ends day {(state['over_since'] + grace) // DAY}"
    return f"day {t // DAY}: {mib:+} MiB -> ok, used {used} MiB{left}"

s = {"used": 800}
for t, mib in [(0, 50), (1 * DAY, 80), (3 * DAY, 90), (5 * DAY, 10), (9 * DAY, 5), (9 * DAY, -200), (10 * DAY, 20)]:
    print(write(s, t, mib))
day 0: +50 MiB -> ok, used 850 MiB
day 1: +80 MiB -> ok, used 930 MiB, grace ends day 8
day 3: +90 MiB -> EDQUOT (hard limit 1000 MiB)
day 5: +10 MiB -> ok, used 940 MiB, grace ends day 8
day 9: +5 MiB -> EDQUOT (grace period over, still above soft 900 MiB)
day 9: -200 MiB -> ok, used 740 MiB
day 10: +20 MiB -> ok, used 760 MiB

The user crossed the soft limit on day 1, bounced off the hard limit on day 3, could still write small amounts on day 5, was refused everything after the grace period ended on day 8, and only recovered by deleting data (the -200 on day 9).

Setting up user and group quotas on ext4

Modern ext4 keeps quota data in hidden inodes (the quota feature); older setups use aquota.user files and quotacheck. Both start with mount options:

bash
$ sudo apt install quota                        # tools: setquota, repquota, quota, quotaon
$ grep srv/data /etc/fstab
UUID=9b1e7c44-2d3a-4c5e-8f60-1a2b3c4d5e6f /srv/data ext4 defaults,noatime,usrquota,grpquota 0 2
$ sudo umount /srv/data
$ sudo tune2fs -O quota /dev/sdb1               # enable the ext4 quota feature (unmounted)
$ sudo mount /srv/data
$ sudo quotaon -v /srv/data
/dev/sdb1 [/srv/data]: group quotas turned on
/dev/sdb1 [/srv/data]: user quotas turned on
$ sudo setquota -u ada 900M 1G 0 0 /srv/data     # soft blocks, hard blocks, soft inodes, hard inodes (0 = none)
$ sudo setquota -g uploads 20G 25G 500000 600000 /srv/data
$ sudo setquota -t 604800 604800 /srv/data       # grace periods in seconds: 7 days
$ sudo repquota -s /srv/data
*** Report for user quotas on device /dev/sdb1
Block grace time: 7days; Inode grace time: 7days
                        Space limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --     20K      0K      0K              2     0     0
ada       +-    930M    900M   1024M  6days     211     0     0
ben       --    120M    900M   1024M             58     0     0

The +- flag says Ada is over her block soft limit (the first character) but not over an inode limit (the second), with 6 days of grace left. Users check themselves with quota -s; the write that crosses a hard limit fails like this:

bash
$ cp big.iso /srv/data/ada/
cp: error writing '/srv/data/ada/big.iso': Disk quota exceeded

Project quotas: a limit per directory

User quotas don't fit services well: a whole app runs as one user. Project quotas limit a directory tree regardless of who writes into it, which is what you want for "the uploads directory may use 50 GB" or per-tenant directories. They are native on XFS and supported on ext4 (feature project, mount option prjquota):

bash
$ grep srv/www /etc/fstab
/dev/vg_data/lv_www /srv/www xfs defaults,prjquota 0 2
$ echo "42:/srv/www/uploads" | sudo tee -a /etc/projects
$ echo "uploads:42" | sudo tee -a /etc/projid
$ sudo xfs_quota -x -c 'project -s uploads' /srv/www
Setting up project uploads (path /srv/www/uploads)...
Processed 1 (/etc/projects and cmdline) paths for project uploads with recursion depth infinite (-1).
$ sudo xfs_quota -x -c 'limit -p bhard=50g uploads' /srv/www
$ sudo xfs_quota -x -c 'report -p -h' /srv/www
Project quota on /srv/www (/dev/mapper/vg_data-lv_www)
                        Blocks
Project ID   Used   Soft   Hard Warn/Grace
---------- ---------------------------------
#0            1.2G      0      0  00 [------]
uploads       31.4G     0    50G  00 [------]

Alternatives you will meet

Need Often solved with
a hard size cap for one service's data a dedicated LV or volume of that size (LVM): simple, and a full volume hurts only that service
limits per container Docker's --storage-opt size= (XFS with pquota), or Kubernetes ephemeral-storage limits
home directories on a shared server user quotas as above
cloud file storage the service's own quotas (EFS, Filestore, Azure Files)

Whatever enforces the limit, alert before it is reached: a quota or a dedicated volume turns "the server is down" into "one feature failed", but it still fails.

Test yourself

What happens when a user exceeds the soft limit but stays below the hard limit?

  1. Writes fail immediately
  2. Writes succeed during the grace period; after it expires the soft limit is enforced
  3. The user is logged out
  4. Nothing, soft limits are informational

Which quota type limits a directory tree regardless of which user writes into it?

  1. User quota
  2. Group quota
  3. Project quota
  4. Inode quota

In repquota, a user shows the flags -+. What does that mean?

  1. Over the block soft limit
  2. Over the inode (file count) soft limit, not the block limit
  3. Over both hard limits
  4. Quota disabled