jueves, 28 de abril de 2016

LINUX-lab understanding quotas

En el siguiente lab vamos a entender para qué es y cómo se utiliza las quotas en el S.O.
La quota es un concepto que limita la cantidad de espacio en el filesystem que puede ser utilizado por un user o un grupo de users. Así, es posible asignar destinar y acotar un espacio en el filesystem para cada user que lo requiera deacuerdo a los aplicativos que ejecute.



Instalación del paquete quota

[root@localhost ~]# yum --disablerepo=\* --enablerepo='base' provides quota
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.brisanet.com.br
1:quota-4.01-11.el7.x86_64 : System administration tools for monitoring users' disk usage
Repo        : base

[root@localhost ~]# yum --disablerepo=\* --enablerepo='base' install quota
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.brisanet.com.br
Resolving Dependencies
--> Running transaction check
---> Package quota.x86_64 1:4.01-11.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
 Package              Arch                  Version                       Repository           Size
====================================================================================================
Installing:
 quota                x86_64                1:4.01-11.el7                 base                176 k

Transaction Summary
====================================================================================================
Install  1 Package

Total download size: 176 k
Installed size: 866 k
Is this ok [y/d/N]: y
Downloading packages:
quota-4.01-11.el7.x86_64.rpm                                                 | 176 kB  00:00:01    
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:quota-4.01-11.el7.x86_64                                                       1/1
  Verifying  : 1:quota-4.01-11.el7.x86_64                                                       1/1

Installed:
  quota.x86_64 1:4.01-11.el7                                                                      

Complete!
[root@localhost ~]#

Una vez instalado el package quota, disponemos de los siguientes comandos para administrar.

[root@localhost ~]# ls -ltr /usr/sbin/*quota*
-rwxr-xr-x  1 root root  76744 jun 10  2014 /usr/sbin/rpc.rquotad
-rwxr-xr-x  1 root root  72864 jun 10  2014 /usr/sbin/repquota
-rwxr-xr-x  1 root root  76832 jun 10  2014 /usr/sbin/quotaon
-rwxr-xr-x  1 root root 105768 jun 10  2014 /usr/sbin/quotacheck
-rwxr-xr-x  1 root root  80992 jun 10  2014 /usr/sbin/edquota
-rwxr-xr-x  1 root root  85152 jun 10  2014 /usr/sbin/setquota
-rwxr-xr-x  1 root root  14368 jun 10  2014 /usr/sbin/quotastats
-rwxr-xr-x  1 root root  72584 jun 10  2014 /usr/sbin/convertquota
-rwxr-xr-x. 1 root root  82768 mar  6  2015 /usr/sbin/xfs_quota
lrwxrwxrwx  1 root root      7 abr 26 11:34 /usr/sbin/quotaoff -> quotaon
[root@localhost ~]#

Habilitar quota en el punto de montaje del filsystem

Se habilita como parámetro en el arranque del filesystem. [root@localhost ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Nov 17 15:59:28 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       ext4    defaults        1 1
UUID=6452b296-0321-4f23-824a-6f86585c8f9f /boot                   ext3    defaults        1 2
/dev/mapper/centos-home /home                   ext4    usrquota,acl,defaults        1 2
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost ~]#

[root@localhost ~]# quotacheck -um /home
quotacheck: Quota for users is enabled on mountpoint /home so quotacheck might damage the file.
Please turn quotas off or use -f to force checking.
[root@localhost ~]#

Donde los parámetros:
u: indica que debe crear un archivo índice para los new users.
m: indica no montar el filesystem read-only.

Verificar la habilitación de quota en el filesystem
[root@localhost ~]# quotaoff /home/
[root@localhost ~]# quotacheck /home
[root@localhost ~]# quotaon /home
[root@localhost ~]# quotacheck /home
quotacheck: Quota for users is enabled on mountpoint /home so quotacheck might damage the file.
Please turn quotas off or use -f to force checking.
[root@localhost ~]#

Configurar quote para users 

En el siguiente ejemplo, el user davis es utilizado para configurar una quota de uso.
[root@localhost ~]# cat /etc/passwd | grep davis
davis:x:3000:10502::/home/davis:/bin/bash
[root@localhost ~]# 

Revisión actual de la quota asignada al user davis
[root@localhost ~]# edquota davis
Disk quotas for user davis (uid 3000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/mapper/centos-home          20          0          0          5        0        0
[root@localhost ~]#

Donde la cantidad de bloques está expresados en KB, es decir, para el user davis, tiene 20 MB de uso.

[root@localhost ~]# vi /etc/fstab
[root@localhost ~]# cat /etc/fstab | grep root
/dev/mapper/centos-root /                       ext4    usrquota,defaults        1 1
[root@localhost ~]# mount -o remount /
[root@localhost ~]# quotacheck -cum /
[root@localhost ~]# quotacheck /
quotacheck: Cannot remount filesystem mounted on / read-only so counted values might not be right.
Please stop all programs writing to filesystem or use -m flag to force checking.
[root@localhost ~]#

Edición de la quota para un user

Se ejecuta el comando edquota, que comprende los siguientes parámetros de edición.
blocks informa la cantidad de bloques utilizados actualmente.    
soft: indica el límite de bloques a utilizar. Si se sobrepasa existe una tolerancia hasta llegar al limite hard.      
hard: indica el máximo de bloques a utilizar. Una vez que sobrepasó el límite soft, si alcanza el límite hard, no se puede utilizar niun sólo bloque más.    
inodes: informa la cantidad de nodos (cantidad de archivos) que utiliza el user.    
soft : indica el límite de cantidad de archivos a utilizar.     Si se sobrepasa existe una tolerancia hasta llegar al limite hard.       hard: indica la máxima cantidad de archivos a utilizar.  Una vez que sobrepasó el límite soft, si alcanza el límite hard, no se puede crear niun archivo más.  

[root@localhost ~]# edquota davis
Disk quotas for user davis (uid 3000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/mapper/centos-root           0          0          0          2        0        0
  /dev/mapper/centos-home          20          0          0          5        0        0
[root@localhost ~]#

[root@localhost ~]# edquota -f /home davis
Disk quotas for user davis (uid 3000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/mapper/centos-home          20          0          0          5        0        0
[root@localhost ~]#

[root@localhost ~]# quota davis
Disk quotas for user davis (uid 3000): none
[root@localhost ~]#

Es posible asignar la quota por filesystem. Asi un user puede tener restricciones sobre un filesystem en particular. 
[root@localhost ~]# edquota -f /home davis
Disk quotas for user davis (uid 3000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/mapper/centos-home          20          3          1          5        0        0
[root@localhost ~]# 

[root@localhost ~]# quota davis
Disk quotas for user davis (uid 3000): 
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
/dev/mapper/centos-home
                     20*      3       1   7days       5       0       0        
[root@localhost ~]# 

Configurar período de gracia para los users. 

El período de gracia se entiende como el tiempo de tolerancia en que el usuario que tiene sobrepasado el soft limit y que se encuentre aun por debajo del hard limit, pueda eliminar archivos y desocupar espacio en el filesystem. El período de gracia puede expresarse en 

[root@localhost ~]# edquota -t
Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
  Filesystem             Block grace period     Inode grace period
  /dev/mapper/centos-root                  7days                  7days
  /dev/mapper/centos-home                  7days                  7days
[root@localhost ~]# 

LAB: aumento de tamaño de un archivo hasta sobrepasar soft y hard limit
En el siguiente ejercicio ya se ha configurado un límite de 20MB en el filesystem /home y posteriormente intentamos crear un archivo de igual o mayor tamaño para observar el comportamiento de la quota.

[davis@localhost ~]$ dd if=/dev/zero of=/home/davis/test.img bs=20M count=1
dm-2: write failed, user block limit reached.
dd: error writing ‘/home/davis/test.img’: Disk quota exceeded
1+0 records in
0+0 records out
0 bytes (0 B) copied, 0.00890764 s, 0.0 kB/s
[davis@localhost ~]$ 



Fuentes

https://www.digitalocean.com/community/tutorials/how-to-enable-user-and-group-quotas
http://www.thegeekstuff.com/2010/07/disk-quota/
http://www.thegeekstuff.com/2011/08/yum-command-examples/
http://www.cyberciti.biz/faq/howto-create-lage-files-with-dd-command/

No hay comentarios:

Publicar un comentario