Once upon a time, disk space was at a premium (in fact, most compute resources were). Many times, companies and educational institutions provided access to systems via a shell (telnet or SSH) to their employees or students. Since those resources were at a premium, they had to be divided amongst the users in various forms. The most common resource that required strict controls was disk space. Even today, you may need to occasionally limit the disk space (or number of files) that a user account can use. It is one of those system administration functions that is used less often, but it is an important topic to understand (and is one of the objectives on the launched Certified System Administrator (EX200) Exam Prep). Let’s take a look at some of what goes into restricting disk space for your user community.
As you might expect, we need to configure our drives (mounts) so that they support quotas that we can then assign on a per user basis. Assuming they already have a partition (which we will refer to here as /dev/sdc2) of 20 GB in size and that it is intended to be mounted on a directory called ‘/mnt/data’, as an ‘ext4’ filesystem, we would have to configure the mount so that it supports user and group quotas. Within the /etc/fstab file, we would add the parameters ‘usrquota’ and ‘grpquota’ to the line that mounts our partition on the intended directory. That line would look something like this:
/dev/sdc1 /mnt/data ext4 defaults,usrquota,grpquota 1 2
You can then either reboot your system, or manually unmount and remount the drive so that the quotas can be applied in the subsequent steps.
Applying a Quota
There are a few quota files that need to be created the first time we intend to apply quotas to our intended partition. Run the command (as root or with sudo privileges) ‘quotacheck -avugc’. This will create, for all configured partitions (see the options in the above section within your /etc/fstab file), user and group quota files, which will store the quotas we indicate next. (We will reuse this command to update the quota files after we are done, leaving off the ‘c’, so that we just update the existing configuration.)Now, we need to create a disk quota for a particular user (in our example). Let’s choose the user called ‘test’ and create two types of quotas – a ‘soft quota’ and a ‘hard quota’. A soft quota is meant to be a warning that you are getting close to the limits of your configured disk space. Once you reach the soft quota, you will be able to continue creating files and consuming space, but each time you do you will get a warning telling you that you have almost reached your limits. The ‘hard quota’ is the limit at which everything stops. If you reach the limit, the transaction creating or consuming space beyond that will fail, displaying an error message indicating that you have reached the limits of your quota. We configure these limits for the ‘test’ user with the command ‘edquota -u test’. This will open your default system editor with the following information (note that your information will differ slightly based on your system and partition):
Disk quotas for user test (uid 503): Filesystem blocks soft hard inodes soft hard /dev/sdc2 0 10000 11000 0 0 0
Once this file has been written, this quota is now enabled for the ‘test’ user so that they can create files taking up to 10 MB of space before they get a warning (soft limit) and up to 11 MB of space before they get errors (hard limit). You can create limits on ‘inodes’ (number of files) or you can do both. Note that the ‘blocks’ and ‘inodes’ entries are not intended to be edited as those columns simply contain the blocks and inodes that are currently in use on that filesystem.Although the quota itself is enabled, we do still have to apply it to the filesystem itself (i.e. turn it on). We can determine if a quota is turned on or off with the ‘quotaon -p /mnt/data’ command (in our example) and will see that the user and group quotas are currently off. We can turn on all configurations for all users, groups, and partitions with the command ‘quotaon -uagv’, and then you can repeat ‘quotaon -p /mnt/data’ to confirm that user and group quotas are on for the indicated partition.
At this point, we are ready to go. Quotas are configured, enabled and not turned on for our system on all partitions mounted with the appropriate options. However, if we run the command ‘quota test’ (in our example, the user ‘test’ is our example account), you will get back a message that says ‘Disk quotas for user test (uid 503): none’. That simply means that the ‘test’ user has yet to create any files on the partition where the limits are applied. We can see that quotas are reporting correctly by creating a simple text file on the partition (in our case, the ‘test’ user created a file called ‘/mnt/data/test.txt’ 4 bytes in size).Now, if we run the command ‘quota’ (or as root ‘quota test’), we will see output like the following:
Disk quotas for user test (uid 503): Filesystem blocks soft hard grace files soft hard grace /dev/sdc2 4 10000 11000 1 0 0
This tells us that we have a 10 MB soft limit and an 11 MB hard limit on the filesystem in question. We are using 4b of space (blocks) and 1 file (inode). The extra column ‘grace’ is the amount of time (in S/M/H/D/W/M) that you have between the soft and hard limits to resolve the space issue. By default, you have 7D (seven days) before the system will prevent any further files or space (or both depending on your configuration) from being created.
There are a number of other things you can do with your disk quota system, including running reports by user and partition, changing the grace period between soft and hard limits, and applying quotas for entire groups rather than just individuals. Take a look at that course for a lot more detail and learn more about the Lost Art of Disk Quota Management!
Get the skills you need for a better career.
Master modern tech skills, get certified, and level up your career. Whether you’re starting out or a seasoned pro, you can learn by doing and advance your career in cloud with ACG.