Managing Disk Usage With Quota

In this article we are going to discuss how to Manage disk usage with quota. Disk Quota is a nice feature in Linux allows the administrator to control the disk usage from users. With the help of quota we can apply restriction on disk space usage and can allow the user to use fixed amount of disk space. Quota can only be created for Partitions.

For example Suppose We have common shared drive in our organisation where all users storing their data, in that case some users may missuse the disk space and keeps their personal data’s like Movies, Music..etc, to avoid such things we can allocate the user to use a fixed amount of disk space like 4 GB, If user cross the allocated limit then administrator will get a notification about that disk usage.

The Five very important terms we should know about while configuring quotas in linux :

  • Soft Limit : The amount of data allowed to the user to store data on File Server.
  • Hard Limit :  After exceeding the soft limit there is amount of extra disk space allowed by System Administrator to the user which is totally depend on Grace Period.

For Example we allowed soft limit to user is 10MB, hard limit is 12 MB and Grace Period is 7 Days, In that case if user crossed Soft Limit i.e. 10MB then he/she can store 2MB of extra data on the disk as we allow Hard Limit as 12MB that also for 7 Days. After 7 days user cannot store data on disk even the remaining 2MB is not utilized. in other case if the user utilize the hardlimit within 7 days then also he will denied to store data on disk.

Note: User cannot Exceed the Hard Limit.

  • Grace Period : After crossed the Soft Limit quota allows 7 days of Grace period to use the allowed Hard Limit, After 7 days user will denied to store data.
  • Blocks : Restrict the disk usage of user by Size.

For Example if Administrator allowed the user to utilize 10MB then user will be get “Access Denied” error after corss 10MB Limit.

  • Inodes : Restrict the disk usage of user by file/directory number.

For Example if Administrator allowed to create 10 inodes to an user then  user can create 10 files and after carossing the assigned file limit then user will get a error message i.e. “Access Denied”. even the created files/directories is 0 in Size.

Managing Disk Usage With Quota

Follow the below Steps to Implementing Disk Quota: Managing Disk Usage With Quota-

Step:1 Create a Partition

Let’s first create a Partition, Follow the below steps.

Use below command to list the already created Partitions and available free disk spaces.

   [[email protected] ~]# fdisk -l   # List Disk Partitions and Free Disks

   Disk /dev/sda: 21.4 GB, 21474836480 bytes
   255 heads, 63 sectors/track, 2610 cylinders
   Units = cylinders of 16065 * 512 = 8225280 bytes

      Device Boot      Start         End      Blocks   Id  System
   /dev/sda1   *           1          38      305203+  83  Linux
   /dev/sda2              39        2480    19615365   83  Linux
   /dev/sda3            2481        2610     1044225   82  Linux swap / Solaris

   Disk /dev/sdb: 10.7 GB, 10737418240 bytes
   255 heads, 63 sectors/track, 1305 cylinders
   Units = cylinders of 16065 * 512 = 8225280 bytes

      Device Boot      Start         End      Blocks   Id  System
   [[email protected] ~]# 

As we can see above we have one free Harddisk i.e. “/dev/sdb” so let’s create a partition.

So follow the below steps to create a New Partition.

   [[email protected] ~]# fdisk /dev/sdb     # To initiate the Partition Creation 

   The number of cylinders for this disk is set to 1305.
   There is nothing wrong with that, but this is larger than 1024,
   and could in certain setups cause problems with:
   1) software that runs at boot time (e.g., old versions of LILO)
   2) booting and partitioning software from other OSs
      (e.g., DOS FDISK, OS/2 FDISK)

   Command (m for help): n     # "n" for New Partition
   Command action
      e   extended
      p   primary partition (1-4)
   p                                    # "p" for Primary Partition
   Partition number (1-4): 1            # Select the available Partition Number
   First cylinder (1-1305, default 1):  # Just Press Enter to Select the Default Cylinder
   Using default value 1
   Last cylinder or +size or +sizeM or +sizeK (1-1305, default 1305): +5G     # Give the Partition Size

   Command (m for help): w     # "w" for Save the Partition Table
   The partition table has been altered!

   Calling ioctl() to re-read partition table.
   Syncing disks.
   [[email protected] ~]# partprobe /dev/sdb     # To avoid Reboot
   [[email protected] ~]# 

Now Format the Partition as shown below.

   [[email protected] ~]# mkfs.ext3 /dev/sdb1     # Format the Partition with ext3 Partition
   mke2fs 1.39 (29-May-2006)
   Filesystem label=
   OS type: Linux
   Block size=4096 (log=2)
   Fragment size=4096 (log=2)
   611648 inodes, 1222940 blocks
   61147 blocks (5.00%) reserved for the super user
   First data block=0
   Maximum filesystem blocks=1254096896
   38 block groups
   32768 blocks per group, 32768 fragments per group
   16096 inodes per group
   Superblock backups stored on blocks: 
           32768, 98304, 163840, 229376, 294912, 819200, 884736

   Writing inode tables: done                            
   Creating journal (32768 blocks): done
   Writing superblocks and filesystem accounting information: done

   This filesystem will be automatically checked every 21 mounts or
   180 days, whichever comes first.  Use tune2fs -c or -i to override.
   [[email protected] ~]# 

As shown on the output above we successfully created a new partition i.e. “/dev/sdb1“, now we need to mount the partition to use it.

For that create a directory and mount the partition in “/etc/fstab” as shown below (Highlighted in Blue Color).

[[email protected] ~]# mkdir /quota      # Create a Directory

Managing Disk Usage With Quota

Mount Partition in fstab

Step: 2 Enabling Quotas on Partition

Just edit the “/etc/fstab” file and add usrquota (to Enable Quota for Users) & grpquota (to Enable Quota for Groups ) next to defaults seperated by comma to that partition where you want to enable quota for users and groups as shown below on the snapshot.

Managing Disk Usage With Quota

Enabling Quota on Partition

Step: 3 Remount the File systems using mount Command

After enabled the quota in file-system by modifying the /etc/fstab file remount the file system to take effect.

Note :  Before using the below command ensure that the file system is not in use, If it is in use then simply reboot the system to take effect.

   [[email protected] ~]# mount -o remount /quota/     # Remount the FileSystem
   [[email protected] ~]#

Now run the mount command to check if User and Group quota got enabled in the file system or not.

   [[email protected] ~]# mount
   /dev/sda2 on / type ext3 (rw)
   proc on /proc type proc (rw)
   sysfs on /sys type sysfs (rw)
   devpts on /dev/pts type devpts (rw,gid=5,mode=620)
   /dev/sda1 on /boot type ext3 (rw)
   tmpfs on /dev/shm type tmpfs (rw)
   none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
   none on /proc/fs/vmblock/mountPoint type vmblock (rw)
   sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
   .host:/ on /mnt/hgfs type vmhgfs (rw,ttl=1)
   /dev/sdb1 on /quota type ext3 (rw,usrquota,grpquota)
   [[email protected] ~]# 

Or You can Try the below command to do so.

   [[email protected] ~]# mount | grep -i /quota
   /dev/sdb1 on /quota type ext3 (rw,usrquota,grpquota)
   [[email protected] ~]#

Step: 4 Create Quota Database Files for Users and Groups

After enable the quota on the file-system we need to create Quota database file by using quotacheck command.

   [[email protected] ~]# quotacheck -cugv /quota/ 

Where :

-c : Create Quota Database File-u : Enable Quota for Users-g : Enable Quota for Groups-v : For Verbose Output

   [[email protected] ~]# cd /quota/
   [[email protected] quota]# ls aquota.user  lost+found

As we can see on the above output after running the quotacheck command quota has created two databse files  ( For Groups ) and aquota.user  ( For Users ).

Step: 5 Enable the Quota by quotaon Command

After creating database files for users and groups using above step now we have to enable the quota by quotaon command as shown below.

  [[email protected] ~]# quotaon -v /quota/
   /dev/sdb1 [/quota]: group quotas turned on
   /dev/sdb1 [/quota]: user quotas turned on
   [[email protected] ~]#  

Other Commands : If you want to disable the Quota  for particular Partition or for all Partitions then use the below command.

   [[email protected] ~]# quotaoff /quota/    # Disable Quota for /quota Partition
   [[email protected] ~]# quotaoff -a     # Disable the Quota for all Partitions
  • a – Assign Quota for all Partitions
  • u – Assign Quota to Users
  • g – Assign Quota to Groups
  • v – For Verbose output.

For Example :

quotaon -a     # To Enable quota for all

quota -augv    # To Enable Quota for All Users and Groups

Step: 6 Apply Disk Quota to Users & Groups

We have successfully configured and Enabled the Quota on Partition, now it’s time to assign the Disk Quota restriction to users.

I have user called “itsmarttricks” and want to apply disk quota restriction to that user,So let’s follow the below steps.

To Assign the quota to any user we have to follow the below Syntax :

edquota -u <Username>     # For Users

edquota -g <GroupName>   # For Groups

So as a  example Let’s assign quota to user itsmarttricks:

   [[email protected] ~]# edquota -u itsmarttricks # To Assign Quota to User itsmarttricks

Note : We can assign quota restriction by KB and 1 Block is equal to 1 KB

There are two options by which we can assign quota to Users and Groups i.e. By Block & Inodes.

After run the above command use will get a screen like shown below :

Managing Disk Usage With Quota

Edited Quota Summery of User

Implementing Block Quota

Let’s first concentrate on Block Quota, So I am going to allow  Soft Limit as 100MB and Hard Limit as 110MB, See the Snapshot below :

Managing Disk Usage With Quota

Applying Block Quota to User

As we can see on snapshot above We allowed 102400KB (100 MB) as a Soft Limit and 112640KB (120 MB) as a Hard Limit.

The calcucation would be like this : 1024KB = 1MB So 102400 is equal to 100MB and 102400KB is equal to 110MB.

Now Save the configuration by press Escape button on the Keyboard and enter :wq  then press ENTER and Remount the partition to take effect.

Now we can check if quota is assigned to user properly or not by below steps :

Step:1 Switch to user itsmarttricks by su command :

Step:2 copy data till 100MB after crossing soft limit you will get a warning message “Quota Exceeded“and after Hardlimit you will get message “Limit Reached

Note : For testing purpose you can create blank file using dd command as shown below :

dd if=/dev/zero of=/quota/doc.txt bs=1024KB count=100

Where:doc.txt – is a File name i create in /quota mount point

Above command will create a zero file with size of 100MB.

Output :

# Create a 100MB File using dd Command

   [[email protected] ~]$ dd if=/dev/zero of=/quota/doc.txt bs=1024KB count=100
   100+0 records in
   100+0 records out
   102400000 bytes (102 MB) copied, 0.558218 seconds, 183 MB/s

   # Check the size of the created File
   [[email protected] ~]$ ls /quota/  aquota.user  doc.txt  lost+found
   [[email protected] ~]$ du -h /quota/doc.txt 
   98M     /quota/doc.txt    # Now file size is 98MB
   # Now create one more file of size 14MB
   [[email protected] ~]$ dd if=/dev/zero of=/quota/doc1.txt bs=1024KB count=14
   sdb1: warning, user block quota exceeded.   # Warning message as we crossed the Soft Limit
   sdb1: write failed, user block limit reached.
   dd: writing `/quota/doc1.txt': Disk quota exceeded
   13+0 records in
   12+0 records out
   12816384 bytes (13 MB) copied, 0.382714 seconds, 33.5 MB/s
   # As we can see below total file size we have created is 13+98=111MB (Soft Limit - 110MB)
   [[email protected] ~]$ du -h /quota/doc*
   13M     /quota/doc1.txt
   98M     /quota/doc.txt
   # Now create one more file of size 10MB

   [[email protected] ~]$ dd if=/dev/zero of=/quota/doc2.txt bs=1024KB count=10
   dd: writing `/quota/doc2.txt': Disk quota exceeded   # Quota Exceeded Message as we crossed the Hard Limit
   1+0 records in
   0+0 records out
   0 bytes (0 B) copied, 0.000262057 seconds, 0.0 kB/s
   [[email protected] ~]$ du -h /quota/doc*
   13M     /quota/doc1.txt
   0       /quota/doc2.txt   # The doc2.txt is 0 as we crossed the Hard Limit and Quota don't allow us to create the file.
   98M     /quota/doc.txt

Implementing Inode Quota

As I explained earlier the Inode quota is a space restriction based on file number, for Example if i allow to create 5 Files as a Soft Limit and 7 files as Hard Limit then After crossing the Soft limit user will get a Warning message that Quota Exceeded and after crossing the Hard Limit quota will deny the user to create any of files.

Note: In case of Inode Quota Restriction Size doesn’t matters, If the user create 7 Blank files/Directories of Size 0KB then also he won’t be allowed to store any further data on that directory, So Let’s have look at the Steps for Implementation of Inode Quota.

To Implement Inode Quota just run the command edquota -u itsmarttricks to edit the quota restriction screen for user itsmarttricks and then enter the Inode Limit you want to give to user.

Example :

Here I am going to allow Soft Limit as 5 Files and Hard Limit as 7 Files for Inode Quota, Follow the Snapshot below.

Managing Disk Usage With Quota

Applying Inode Quota to User

To check if the quota is applied properly just login as itsmarttricks using command su – itsmarttricks, then create 5 files, you can successfully able to do so as we allowed soft-limit as 5 Inodes, When you create 6th file you will get a Warning Message “Quota Exceeded” as we crossed the Soft Limit, Now Let’s create more 3 files then you will get a message “Limit Reached” So out of 3 files one file will be created and other two will be won’t as we allowed 7 Inodes as a Hard Limit, See the Output below.Output :

   [[email protected] ~]# su - itsmarttricks   # Login as a itsmarttricks
   [[email protected] ~]$ cd /quota/
   [[email protected] quota]$ touch doc{1,2,3,4,5}.txt   # Create 5 Files
   [[email protected] quota]$ ls  aquota.user  doc1.txt  doc2.txt  doc3.txt  doc4.txt  doc5.txt  lost+found
   [[email protected] quota]$ touch doc6.txt   # Create 6th File
   sdb1: warning, user file quota exceeded.   # Warning Message after creating 6th File and crossed the Soft Limit
   [[email protected] quota]$ touch doc{7,8,9}.txt   # Create more 3 Files
   sdb1: write failed, user file limit reached.         # Limit Reached message after crossed the Hard Limit
   touch: cannot touch `doc8.txt': Disk quota exceeded
   touch: cannot touch `doc9.txt': Disk quota exceeded
   # As we can see below we able create doc7.txt file and other two files are not created as we allowed 7 Inodes as HardLimit

   [[email protected] quota]$ ls  aquota.user  doc1.txt  doc2.txt  doc3.txt  doc4.txt  doc5.txt  doc6.txt  doc7.txt  lost+found
   [[email protected] quota]$

We can check quota report utilized by user by using below command :

[[email protected] ~]# repquota -a
*** Report for user quotas on device /dev/sdb1
Block grace time: 7days; Inode grace time: 7days
                     Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
root      --  140768       0       0              4     0     0       
itsmarttricks +-  112640  102400  112640  6days       3     0     0

Grace Period

After crossed the Soft Limit quota allows 7 days of Grace period to use the allowed Hard Limit, After 7 days user will denied to store data.

We change the grace period by using edquota with option -T as shown below

edquota -T
Times to enforce softlimit for user itsmarttricks (uid 500):
Time units may be: days, hours, minutes, or seconds
  Filesystem                         block grace               inode grace
  /dev/sdb1                         602556seconds                  unset

Note: We have to mention the Grace Period in Seconds.

Also Read – How to Setup NFS Server (Network File System) On Redhat/Centos/Fedora

That’s all, In this article, we have explained Managing Disk Usage With Quota. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.