Linux Exercise: Disk Quotas

Note: This exercise will use the ext4 filesystem and the ext4 quota tools. Those tools are the ones that will be tested on the LPI exam. However, if you use the xfs filesystem you will need to use the xfs quota tool, xfs_quota, instead. xfs_quota has a similar purpose but a different syntax from the tools described here.

Preparation

On a CentOS 7 or similar system, /home is a directory in your root filesystem, and your root filesystem is xfs. This doesn't allow us to work with ext2/3/4-style quotas. Plus, quotas on a root filesystem are not supported in Linux. We therefore need to create a separate ext2/3/4 filesystem for /home, and make sure that all content of the current /home is copied over before we can permanently mount that filesystem on /home.

  1. Make sure that no regular user accounts are logged in. Only one root login should be active.
  2. Create a 20 MB Logical Volume "home" in your "centos" Volume Group. Format this with the ext4 filesystem. Temporarily mount this filesystem on /mnt/home. Move all home directories from /home to /mnt/home, and then unmount and mount the LV on /home. Also change your /etc/fstab file so that this filesystem will be mounted on /home at the next reboot. To test, reboot your system.
    • # lvcreate -L 20M -n home centos
    • # mkfs -t ext4 /dev/centos/home
    • # mkdir /mnt/home
    • # mount /dev/centos/home /mnt/home
    • # cd /home
    • # mv * /mnt/home
    • # cd
    • # umount /mnt/home
    • # mount /dev/centos/home /home
    • # ls -l /home
    • # vi /etc/fstab
      Add the following line:
      /dev/centos/home /home ext4 defaults 0 2
    • # reboot
    • # mount

Implementing Quota

  1. Change your /etc/fstab file so that your /home filesystem is quota-enabled. Remount your filesystem with user quota enabled, or reboot your system.
    • # vi /etc/fstab
      Add the usrquota keyword to the "defaults" tag. The line describing your home fs will now look like this:
      /dev/centos/home /home ext4 defaults,usrquota 0 2
      Note that there should not be any spaces between "defaults" and "usrquota", just a comma.
    • # mount -o remount,usrquota /home
  2. Update the quota information, then enable quota monitoring.
    • # quotacheck /home
    • # ls -l /home
      There should be a file aquota.user now. This contains the current quota and usage of this filesystem.
    • # quotaon /home
  3. Check the current disk usage and quota.
    • # repquota -a
  4. In a separate SSH/terminal window, login as the regular user you created during the installation. Create a 10 MB file. Check your own quota.
    • $ dd if=/dev/zero of=bigfile bs=1024 count=10240
    • $ quota
  5. As root, assign a 15 MB soft, 16 MB hard quota to your own user account, in /home. Check the quota again.
    • # edquota username
      Go to the line describing your centos-home filesystem. In the first "soft" column, change the "0" to 15000, and change the "hard" number to 16000.
    • # repquota -a
  6. As your regular user, try to create another 5 MB file. Check your quota.
    • $ dd if=/dev/zero of=bigfile2 bs=1024 count=5120
      You should get a warning at this point because you exceeded the soft limits. But the action succeeds because you have not yet exceeded the hard limit.
    • $ quota
    • $ ls -l
  7. As your regular user, try to create yet another 5 MB file. Check your quota.
    • $ dd if=/dev/zero of=bigfile3 bs=1024 count=5120
      You should get an error at this point because you exceeded the hard limits.
    • $ quota
    • $ ls -l
  8. As root, check the quota again.
    • # repquota -a

Cleaning up

  1. In anticipation of the next exercises, disable quota an dmake sure they are not re-enabled when the system boots.
    • # quotaoff /home
    • # mount -o remount,noquota /home
    • # vi /etc/fstab
      Remove the "usrquota" optioin from the line describing /home.
End of exercise