How to partition, manage and enable disk quotas on a Linux server
Preamble: The below steps help explain how to partition a disk specifically used for users and groups related to quota and then assigning quotas to users. Please also note that doing this on a used disk will erase that filesystem.
First it is good to list the disks that are currently available on the system, like the below example:
fdisk -l
Disk /dev/xvda: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x7d833f39
Disk /dev/xvdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/xvdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/xvdd: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
In the above example you have four available disks, in the following examples, I will be partitioning the xvdb disk as part of the steps for updating the quota (please note that the following steps up until step 12 could vary from end-user to end-user)
1) Below are the commands required to partition the drive, please note that the partitioning and drives involved may vary depending on your system:
fdisk /dev/xvdb
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2) Now to show the partition created above, please see the below example, I have created /dev/xvdb1:
fdisk -l /dev/xvdb
Disk /dev/xvdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa13e56fb
Device Boot Start End Blocks Id System
/dev/xvdb1 1 262 2104483+ 83 Linux
3) For this example I will change the partition type for /dev/xvdb1 to ext4, through the below command:
mkfs –t ext4 /dev/xvdb1
4) To edit the /etc/fstab file to be able to use user quota and group quota, I will be finding the UUID associated with /dev/xvdb1, below is the command to find that UUID:
blkid
/dev/xvdb1: LABEL="/" UUID="cb04b2c8-b35a-4507-b6c9-c0156f1a0b56" TYPE="ext4"
5) Below is the output of the file of /etc/fstab after editing the file to allow for user quota and group quota (please look at the bolded line for the changes that would be made to /etc/fstab:
LABEL=/ / ext4 defaults 0 0
LABEL=ebs-swap none swap sw 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
#mount the quota disk
UUID="86b66f90-d2fd-4789-ab6d-5ceee22f41ff" /home/ ext4 defaults,usrquota,grpquota 1 2
6) Need to mount system for quota to take effect:
Mount –a
7) Need to create user accounts, if they do not exist:
Adduser anyuser
Passwd anyuser
Enter in the password
8) Quotacheck –avugc to create the aquota.group and aquota.user for the quotas in the /home directory
9) ls –la /home
total 40
drwxrwxrwx 3 root root 4096 Nov 14 07:26 .
drwxr-xr-x 3 root root 4096 Nov 14 07:18 ..
-rw------- 1 root root 6144 Nov 14 07:26 aquota.group
rw------- 1 root root 6144 Nov 14 07:26 aquota.user
drwx------ 2 root root 16384 Nov 14 07:16 lost+found
10) Now you are able to edit the quota for a user, using the below command as root user:
edquota -u anyuser (now able to edit the users quota information).
Please note that blocks is quota size and inodes is related to file limits
Soft quota can be exceeded, but not hard quota
Disk quotas for user max (uid 503):
Filesystem blocks soft hard inodes soft hard
/dev/xvdb1 0 0 0 0 0 0
11) Below are the steps to make sure that the quota is turned quotaon -p /home – this shows that quota is not turned on.
group quota on /home (/dev/xvdb1) is off
user quota on /home (/dev/xvdb1) is off
[root]# quotaon –uagv (turns on quota)
/dev/xvdb1 [/home]: group quotas turned on
/dev/xvdb1 [/home]: user quotas turned on
12) Continue to repeat step 10 for each new user created. Please note that the below command will show all user quotas on the system:
repquota –a
*** Report for user quotas on device /dev/xvdf1
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 36 0 0 6 0 0
anyuser +- 14000 12000 14000 6days 774 0 0