Objectives
- Understand the concept of swap and quotas
- Use utilities that help manage quotas
- quotacheck
- quotaon
- quotaoff
- edquota
- quota
- Use the monitoring utilities like
- df
- du
Swap
Its a virtual memory used to help the operating system. This swap memory is often allocated on the hard disk or a different location than the actual ram memory.
The recommended size of the swap memory is equal to the size of the actual memory.
To check the used swap memory
$ cat /proc/swaps
To check the memory usage meassured in gigabites "g"
$ free -g
mkswap
Format a swap partition or file
swapon
Enable a swap partition or file
swapoff
Disable a swap partition or file
Note
Kernel memory is never swapped out
Quotas
Its a mechanism that linux use to manage the usage of the storage resources, it can be used to limit
- user
- groups
the main commands to manage quotas are
- quotacheck : generates or updates quota accounting files
- edquota : used to edit user or group quota
- quotaon : enable quota accounting
- quotaoff : disables quota accounting
- quota : reports on usage and limits
In order to manage quotas, the next files must exists in the root directory of the filesystem using quotas
- aquota.user
- aquota.group
Hierarchy of quota administration
- Filesystem
- user
- group
Setting up Quotas
In order to setup quotas in a filesystem, such filesystems must have been mounted with user and/or group quota option.
- usrquota
- grpquota
Sample
1. Edit file /etc/fstab
/home/abernal/filesystem1 /mnt/tempdir ext4 defaults,usrquota 1 2 /home/abernal/filesystem2 /mnt/tempdir ext4 defaults,grpquota 1 2 /home/abernal/filesys3 /mnt/dir1 ext4 defaults,usrquota,grpquota 1 2
2. Test quotas with these commands
$ sudo mount -o remount /mnt/tempdir $ sudo quotacheck -vu /mnt/tempdir $ sudo quotaon -vu /mnt/tempdir $ sudo edquota abernal
quotacheck
The quotacheck command creates or updates the quota accounting file (quota.user or quota.group) for the filesystem
Update user files for all filesystems in /etc/fstab with user quota options
$ sudo quotacheck -ua
Update group files for all filesystems in /etc/fstab with group quota options
$ sudo quotacheck -ga
Update the user file for a particular filesystem
$ sudo quotacheck -u [givenfilesystem]
Update the group file for a particular filesystem
$ sudo quotacheck -g [givenfilesystem]
quotaon and quotaoff
This commands are used to
- quotaon : Turn Filesystem quotas on
- quotaoff : Turn Filesystem quotas off
Usage
$ sudo quotaon [flags] [filesystem]
$ sudo quotaoff [flags] [filesystem]
Flags
-a, --all turn quotas off for all filesystems
-f, --off turn quotas off
-u, --user operate on user quotas
-g, --group operate on group quotas
-p, --print-state print whether quotas are on or off
-x, --xfs-command=cmd perform XFS quota command
-F, --format=formatname operate on specific quota format
-v, --verbose print more messages
-h, --help display this help text and exit
-V, --version display version information and exit
Samples
$ sudo quotaon -av /dev/sda6 [/]: group quotas turned on /dev/sda5 [/home]: user quotas turned on
$ sudo quotaoff -av /dev/sda6 [/]: group quotas turned off /dev/sda5 [/home]: user quotas turned off
$ sudo quotaon -avu /dev/sda5 [/home]: user quotas turned on
$ sudo quotaoff -avu /dev/sda5 [/home]: user quotas turned off
$ sudo quotaon -avg /dev/sda6 [/]: group quotas turned on
$ sudo quotaoff -avg /dev/sda6 [/]: group quotas turned off
Note also that quota operations will fail if the files aquota.user or aquota.group do not exist.
quota
The quota utility is used to generate reports on quotas
- quota -u : returns your current user quota
- quota -g : returns your current group quota
- sudo quota abernal : returns quota configuration for abernal
edquota
Command used to modify quotas, soft and hard limits are the only parameters that can be modified
- edquota -u [username] : edit quota for username
- edquota -g [groupname] : edit quota for groupname
- edquota -u -p [userproto] [username] : sets quota configuration from userproto to username
- edquota -g -p [groupproto] [groupname] : sets quota configuration from groupproto to groupname
- edquota -t : to set grace periods
Soft limits can be exceded during grace periods. this is why grace periods are useful for
Filesystem Usage
df
Disk Free command are useful to see filesystem capacity and usage.
$ df -hT
Where
- -h : Human readable
- -T : Filesystem type
- -i : Shows inode information
du
Disk Usage command is useful to check the disk
To check disk usage for the current directory type
$ du
To list all files, not just directories
$ du -a
To list in human readable format
$ du -h
To display disk usage for a specific directory
$ du -h somedirectory
To display only totals, suppressing subdirectory output
$ du -s
Lab 13.1: Managing Swap Space
Examine your current swap space by doing:
$ cat /proc/swaps Filename Type Size Used /dev/sda11 partition 4193776 0 Priority -1
We will now add more swap space by adding either a new partition or a file. To use a file we can do:
$ dd if=/dev/zero of=swpfile bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 1.30576 s, 822 MB/s
$ mkswap swpfile Setting up swapspace version 1, size = 1048572 KiB no label, UUID=85bb62e5-84b0-4fdd-848b-4f8a289f0c4c
(For a real partition just feed mkswap the partition name, but be aware all data on it will be erased!) Activate the new swap space:
$ sudo swapon swpfile swapon: /tmp/swpfile: insecure permissions 0664, 0600 suggested. swapon: /tmp/swpfile: insecure file owner 500, 0 (root) suggested.
Notice RHEL 7 warns us we are being insecure, we really should fix with: $ sudo chown root:root swpfile
$ sudo chmod 600 swpfile
and ensure it is being used:
$ cat /proc/swaps Filename /dev/sda11 /tmp/swpfile Type Size Used Priority partition 4193776 0 -1 file 1048572 0 -2
Note the Priority field; swap partitions or files of lower priority will not be used until higher priority ones are filled. Remove the swap file from use and delete it to save space:
$ sudo swapoff swpfile $ sudo rm swpfile
Lab 13.2: Filesystem Quotas
1. Change the entry in /etc/fstab for your new filesystem to use user quotas (change noexec to usrquota in the entry for /mnt/tempdir). Then remount the filesystem.
2. Initialize quotas on the new filesystem, and then turn the quota checking system on.
3. Now set some quota limits for the normal user account: a soft limit of 500 blocks and a hard limit of 1000 blocks.
4. As the normal user, attempt to use dd to create some files to exceed the quota limits. Create bigfile1 (200 blocks) and bigfile2 (400 blocks).
You should get a warning. Why?
5. Create bigfile3 (600 blocks).
You should get an error message. Why? Look closely at the file sizes.
6. Eliminate the persistent mount line you inserted in /etc/fstab.
1. Change /etc/fstab to have one of the following two lines according to whether you are using a real partition or a loopback file:
/dev/sda11 /mnt/tempdir ext4 usrquota 1 2 /imagefile /mnt/tempdir ext4 loop,usrquota 1 2
Then remount:
$ sudo mount -o remount /mnt/tempdir
2. Set quota on tempdir
$ sudo quotacheck -u /mnt/tempdir $ sudo quotaon -u /mnt/tempdir $ sudo chown student.student /mnt/tempdir
(You won’t normally do the line above, but we are doing it to make the next part easier).
3. Substitute your user name for the student user account.
4. $ sudo edquota -u student
5. Create bigfile1 and bigfile2
$ cd /mnt/tempdir $ dd if=/dev/zero of=bigfile1 bs=1024 count=200 200+0 records in 200+0 records out 204800 bytes (205 kB) copied, 0.000349604 s, 586 MB/s $ quota Disk quotas for user student (uid 500): Filesystem blocks quota lim grace files qu lim gr /dev/sda11 200 500 1000 1 00 $ dd if=/dev/zero of=bigfile2 bs=1024 count=400 sda11: warning, user block quota exceeded. 400+0 records in 400+0 records out 4096600 bytes (410 kB) copied, 0.000654847 s, 625 MB/s
6. Create bigfile3 (600 blocks).
$ quota Disk quotas for user student (uid 500): Filesystem blocks quota limit grace files qu lim gr /dev/sda11 600* 500 1000 6days 2 0 0 $ dd if=/dev/zero of=bigfile3 bs=1024 count=600 sda11: write failed, user block limit reached. dd: writing ‘bigfile3’: Disk quota exceeded 401+0 records in 400+0 records out 409600 bytes (410 kB) copied, 0.00177744 s, 230 MB/s $ quota Disk quotas for user student (uid 500): Filesystem blocks quota limit grace files quota limit grace /dev/sda11 1000* 500 1000 6days 3 0 0 $ ls -l total 1068 -rw------- 1 root root 7168 Dec 10 18:56 aquota.user -rw-rw-r-- 1 student student 204800 Dec 10 18:58 bigfile1 -rw-rw-r-- 1 student student 409600 Dec 10 18:58 bigfile2 -rw-rw-r-- 1 student student 409600 Dec 10 19:01 bigfile3
Look closely at the file sizes.
7. Get rid of the line in /etc/fstab.