q42-disk-quotas
Énoncé§
Solve this question on: data-001
- Enable user and group quotas on the
/homefilesystem. - Set user
alicea soft limit of500Mand a hard limit of1Gfor blocks; soft1000and hard2000files (inodes). - Set the same defaults for any new user via the prototype technique.
- Write the current quota report for
/homeinto/opt/course/42/quota-report.
Solution§
Install the tools§
sudo apt install quota # Debian
sudo dnf install quota # RHEL
Step 1 — Enable quotas on the filesystem§
Edit /etc/fstab for the target filesystem:
UUID=... /home ext4 defaults,usrquota,grpquota 0 2
Re-mount or apply:
sudo mount -o remount /home
mount | grep /home # confirm options
XFS uses mount options at mount time instead of quotacheck:
uquota,gquota,pquota— enforceuqnoenforce, etc. — accounting only
For ext4 (or older filesystems) you must initialise the quota DB:
sudo quotacheck -cugm /home
# generates /home/aquota.user and /home/aquota.group
sudo quotaon -v /home
sudo quotaoff -v /home # disable
Step 2 — Set limits for a user§
Interactive editor:
sudo edquota -u alice
Block sizes are in KB:
Filesystem blocks soft hard inodes soft hard
/dev/vda2 3200 512000 1048576 145 1000 2000
Or non-interactively with setquota:
sudo setquota -u alice 512000 1048576 1000 2000 /home
# soft-blk hard-blk soft-inode hard-inode
For a group:
sudo setquota -g devs 5G 10G 0 0 /home
Soft vs hard limits + grace period§
- Soft — can be exceeded temporarily during the grace period.
- Hard — cannot be exceeded; writes get
EDQUOT. - Grace — time the soft limit can stay violated before it becomes hard.
sudo edquota -t # edit grace periods
Step 3 — Apply the same defaults to new users§
edquota -p copies one user’s settings to others:
sudo edquota -p alice -u bob carol dave
# or every user above UID 1000:
sudo edquota -p alice $(awk -F: '$3 >= 1000 {print $1}' /etc/passwd)
A common pattern: set up a template user, then in scripts that create accounts call edquota -p template <newuser>.
Step 4 — Reports§
sudo repquota -a > /opt/course/42/quota-report
sudo repquota /home # one filesystem
sudo repquota -ug /home # users + groups
quota -u alice # what alice is using
quota -g devs
XFS-specific tools§
sudo xfs_quota -x -c 'limit bsoft=500m bhard=1g alice' /home
sudo xfs_quota -x -c 'report -h' /home
sudo xfs_quota -x -c 'state' /home
XFS adds project quotas (per-directory) — enabled via /etc/projects and /etc/projid:
echo "10:/data/site1" | sudo tee -a /etc/projects
echo "site1:10" | sudo tee -a /etc/projid
sudo xfs_quota -x -c 'project -s site1' /data
sudo xfs_quota -x -c 'limit -p bsoft=5g bhard=6g site1' /data
Mailing users automatically when over quota§
sudo warnquota # send mail to over-quota users
sudo warnquota -g # also groups
# crontab once a day:
echo '0 6 * * * root /usr/sbin/warnquota' | sudo tee /etc/cron.d/warnquota
Mail templates: /etc/warnquota.conf.
—The Gardener