Introduction
You may eventually need to restrict the amount of disk space used on each partition by each user or group of users as your disk drives become filled with data. The disk quota feature of RedHat/Fedora Linux enables you to do this, and the setup is fairly simple.
Setting Up Quotas
For example, your family Linux server is running out of space in the /home filesystem because of an abundance of MP3 downloads.
Enter Single-User Mode
As you’ll need to remount the /home filesystem, you make sure that no other users or processes are using it by first entering single-user mode from the console. If you are certain that you’re the only user on the system, you might be able to skip this step. Entering single-user mode automatically logs off all users and stops cron jobs, so wait until after hours to do in a business environment. The procedure is quick:
1) Use the who command to see which users are logged in. If there are any, besides yourself, send a message stating that the system is about to shutdown with the wall command:
[root@bigboy tmp]# who root pts/0 Nov 6 14:46 (192-168-1-242.my-site.com) bob pts/0 Nov 6 12:01 (192-168-1-248.my-site.com) bunny pts/0 Nov 6 16:25 (192-168-1-250.my-site.com) [root@bigboy tmp]# wall The system is shutting down now! Broadcast message from root (pts/0) (Sun Nov 7 15:04:27 2004): The system is shutting down now! [root@bigboy tmp]#
2) Log into the VGA console and enter single user mode.
[root@bigboy tmp]# init 1
Simple eh?
Edit Your /etc/fstab File
The /etc/fstab file lists all the partitions that need to be auto-mounted when the system boots. You have to alert Linux that quotas are enabled on the filesystem by editing the /etc/fstab file and modifying the options for the /home directory. You’ll need to add the usrquota option. In case you forget the name, the usrquota option is mentioned in the fstab man pages.
The old /etc/fstab looked like
LABEL=/home /home ext3 defaults 1 2
but your new /etc/fstab should be
LABEL=/home /home ext3 defaults,usrquota 1 2
but in case of Journal file system we need to use as below
LABEL=/home /home ext4 defaults,usrjquota=aquota.user 0 0
Remount The Filesystem
Editing the /etc/fstab file isn’t enough, Linux needs to reread the file to get its instructions for /home. You can do this using the mount command with the -o remount qualifier.
bash-2.05b# mount -o remount /home
Get Out of Single-user Mode
Return to your original run state by using either the exit, init 3 or init 5 commands. Continue to the next step once the system is back to its normal state. You can also use the exit command to return to your default runlevel.
bash-2.05b# exit
Create The Partition Quota Configuration Files
The uppermost directory of the filesystem needs to have an aquota.user file (defines quotas by user) and an aquota.group file (defines quotas by group), or both. The man page for quota lists them at the bottom. In this case just enable per-user quotas for the /home filesystem.
[root@bigboy tmp]# touch /home/aquota.user
[root@bigboy tmp]# chmod 600 /home/aquota.user [root@bigboy tmp]#
Initialize The Quota Table
Editing the /etc/fstab file and remounting the file system only alerted Linux to the fact that the filesystem has quota capabilities. You have to generate a quota table, separate from the aquota files, that lists all the current allocations for each user on the file system. This table will be automatically and transparently updated each time a file is modified. Linux compares the values in this table with the quota limitations that the systems administrator has placed in the aquota files and uses this information to determine whether the user has rights to increased disk usage. You initialize the table with the quotacheck command. Be prepared: You’ll get an error the first time you enter the command, because Linux will realize that the aquota file wasn’t created using one of the quota commands:
[root@bigboy tmp]# quotacheck -vagum quotacheck: WARNING - Quotafile /home/aquota.user was probably truncated. Can't save quota settings... quotacheck: Scanning /dev/hda3 [/home] done quotacheck: Checked 185 directories and 926 files [root@bigboy tmp]#
Edit The User’s Quota Information
Now you need to edit the user’s quota information. The edquota command enables you to selectively edit a portion of the aquota.user file on a per-user basis:
[root@bigboy tmp]# edquota -u mp3user
The command will invoke the vi editor.
Disk quotas for user mp3user (uid 503): Filesystem blocks soft hard inodes soft hard /dev/hda3 24 0 0 7 0 0
From here, you can edit a number of fields:
- Blocks: The amount of space in 1K blocks the user is currently using.
- Inodes: The number of files the user is currently using.
- Soft Limit: The maximum blocks/inodes a quota user may have on a partition. The role of a soft limit changes if grace periods are used. When this occurs, the user is only warned that their soft limit has been exceeded. When the grace period expires, the user is barred from using additional disk space or files. When set to zero, limits are disabled.
- Hard Limit: The maximum blocks/inodes a quota user may have on a partition when a grace period is set. Users may exceed a soft limit, but they can never exceed their hard limit.
Here user mp3user is limited to a maximum of 5MB of data storage on /dev/hda3 (/home):
Disk quotas for user mp3user (uid 503):
Filesystem blocks soft hard inodes soft hard /dev/hda3 24 5000 0 7 0 0
Testing
Linux checks the total amount of disk space a user uses each time a file is accessed and compares it against the values in the quota file. If the values are exceeded, depending on the configuration, then Linux prevents the creation of new files or the expansion of existing files to use more disk space.