# Quota
[TOC]
## What is qouta (wikipedia)
- A **limit** set by system admin that **restricts certain aspects of file system usage** on OS.
### Types of quotas
- **hard quota**:
- **usage/block quota**: limit the amount of disk space that can be used.
- **file/inode quota**: limit the number of files and directories that can be created.
- **soft quota**: defined a **warning level** at which users' resource usages reach the effective limit, or hard quota.
> There may also be a small **grace interval**, which allows users to temporarily violate their quotas by certain amounts if necessary.
- **threshold quota**: Typically, administrators set the threshold for disk limit to a value that is only slightly smaller than the disk limit, so that the threshold provides a "final warning" before writes start to fail.
## Configuration
- Disk quotas are typically implemented on a **per-user** or **per-group** basis.
- In most cases, quotas are also specific to individual file systems.
- In some file systems, it is also posible to define block and inode quota limits for a particular project or directory.
### `fstab` - static information about the filesystems
- The file *`/etc/fstab`* is only read by programs, and **not written**; it is the duty of the system administrator to properly create and maintain this file.
- The order of records in fstab is important because `fsck`, `mount`, and `umount` sequentially iterate through fstab doing their thing.
- Example on ms15
```
# Device Mountpoint FStype Options Dump Pass#
/dev/ada0p2 / ufs rw 1 1
/dev/ada0p3 none swap sw 0 0
```
> There are six fields in the file:
> 1. The first describes the block special device or remote filesystem to be mounted.
> 2. The second describes the mount point for the filesystem.
> 3. The third describes the type of the filesystem.
> 4. The fourth describes the mount options associated with the filesystem.
> 5. The fifth is used for these filesystems by the [`dump`](https://linux.die.net/man/8/dump) command to determine which filesystems need to be dumped.
> 6. The sixth is used by the [`fsck`](https://linux.die.net/man/8/fsck) program to determine the order in which filesystem checks are done at reboot time.
- Red Hat's *`fstab`* format is different from common Unix-like OS. (See [details](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/storage_administration_guide/ch-disk-quotas))
### `quota` - display disk usage and limits
- Reports the quotas of all the filesystems listed in *`/etc/mtab`*(default filesystems).
- Options
- `-g`: group
- `-u`: user
- `-v`: display quotas on filesystems where no storage is allocated
- `-s`: human-readable
- `-l`: report quotas only on local filesystems
## SOP
### Set File System Quotas on Ubuntu
- Install `quota` package
```bash=
sudo apt install quota -y
```
#### Enable file system quota
- Edit *`/etc/fstab`*, add `usrquota` or `grpqouta` at field 4

- Reboot server
- Enable quota on the root filesystem
```bash=
sudo mount -o remount,usrquota /
```
#### Initializing Quota on Filesystems
- Use only user quota on the root (/) filesystem
```bash=
sudo quotacheck -cum /
```
> `-cum` can be changed to:
> - `-cgm`: use group quota only
> - `-cugm`: use both
- Turn on quota on the root (/) filesystem
```bash=
sudo quotaon -v /
```

#### Working with User/Group Quota
- Set disk quota for the user
```bash=
sudo edquota -u phoebe
```
- Set a group quota for the group
```bash=
sudo edquota -g mygroup
```

- **Format**
- **Filesystem**
- It is the disk partition where this quota is to be applied. It’s *`/dev/sda2`*, the root (*`/`*) filesystem partition. **Don’t try to change it.**
- **Blocks**
- This is the amount of disk space (in blocks) the user can use.
> You have to convert MB or GB unit to the equivalent block size and use the block size here. **1 block is equal to 1 KB or 1024 bytes.**
- **Inode**
- It is the number of files or directories you can create in a filesystem.
- A good measure is keeping it about 60-70% of the total block size.
- **Soft**
- A user or group can exceed the soft limit for a certain number of days, called a grace period.
- Put 0 to disable soft limit.
- **Hard**
- By no means are they allowed to exceed the hard limit.
:::danger
You should only set the soft and hard limits. **Don’t modify the values of the blocks and inode columns.** They represent the blocks and inodes that the user is currently using.
:::
#### Changing Grace Period
```bash=
sudo edquota -t
```
- Change the number of days for block and inode grace period

#### Generate Quota Reports
```bash=
sudo repquota -aug
```
> `-au`: only shows user quota
> `-ag`: only shows group qouta

### Set File System Quotas on FreeBSD
- Add two lines in *`/etc/rc.conf`*, enable disk quotas
```
enable_quotas="YES"
check_quotas="YES"
```
- Add `,userquota` at field 4 in *`/etc/fstab`*
## Reference
### Official
- [Wikipedia - Disk quota](https://en.wikipedia.org/wiki/Disk_quota)
- [`fstab` - Linux man page](https://linux.die.net/man/5/fstab)
- [`quota` - Linux man page](https://linux.die.net/man/1/quota)
- [FreeBSD Handbook - Disk Quotas](https://docs.freebsd.org/doc/3.5-RELEASE/usr/share/doc/handbook/quotas.html)
- [Red Hat Training - DISK QUOTAS](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/storage_administration_guide/ch-disk-quotas)
### Article
1. [Differences among hard, soft, and threshold quotas](https://docs.netapp.com/ontap-9/index.jsp?topic=%2Fcom.netapp.doc.dot-cm-vsmg%2FGUID-42668748-5031-4250-99F1-1924D5752201.html)
2. [How to Check Disk Space Usage in Linux](https://www.hostinger.com/tutorials/vps/how-to-check-and-manage-disk-space-via-terminal)
3. [Disk Quota & How to Manage Disk Space](https://etc.engineering.uiowa.edu/help-deskcomputer-services-ecs/faq-about-computing-engineering/disk-quota-how-manage-disk-space)
4. [How to Set Up Disk Quota on Ubuntu 18.04 server](https://www.alibabacloud.com/blog/how-to-set-up-disk-quota-on-ubuntu-18-04-server_595877)
5. [How to use Quota on Ubuntu](https://linuxhint.com/disk_quota_ubuntu/)
6. [鳥哥 - 磁碟配額(Quota)與進階檔案系統管理](http://linux.vbird.org/linux_basic/0420quota.php)
7. [FreeBSD Quota Tutorial](https://www.howtoforge.com/howto_freebsd_quota)
8. [Setting Up disk quota on FreeBsd](https://www.unixcities.com/disk-and-files/index.html)