Category Archives: Linux Basics

Click for Linux Basics

Linux File System Quotas

This feature of Linux allows the system administrator to allocate a maximum amount of disk space a user or group may use. It can be flexible in its adherence to the rules assigned and is applied per filesystem. The default Linux Kernel which comes with Redhat and Fedora Core comes with quota support compiled in.

Two versions of quotas have been released. Version 2 is used by the Linux 2.4 and 2.6 kernel. Quotas version 1 is used by the Linux 2.2 kernel. Both are discussed in this tutorial.

Configuration of disk usage quotas on Linux – Perform the following as root:

# Edit file /etc/fstab to add qualifier “usrquota” or “grpquota” to the partition. The following file system mounting options can be specified in /etc/fstab: grpquota, noquota, quota and usrquota. (These options are also accepted by the mount command but ignored.) The filesystem when mounted will show up in the file /etc/mtab, the list of all currently mounted filesystems.)

* To enable user quota support on a file system, add “usrquota” to the fourth field containing the word “defaults”.


/dev/hda2 /home ext3 defaults,usrquota 1 1

* Replace “usrquota” with “grpquota”, should you need group quota support on a file system.


/dev/hda2 /home ext3 defaults,grpquota 1 1

* Need both user quota and group quota support on a file system?


/dev/hda2 /home ext3 defaults,usrquota,grpquota 1 1

This enables user and group quotas support on the /home file system.

# touch /partition/aquota.user
where the partition might be /home or some partition defined in /etc/fstab.
then

chmod 600 /partition/aquota.user

The file should be owned by root. Quotas may also be set for groups by using the file aquota.group

Quota file names:

* Quota Version 2 (Linux 2.4/2.6 kernel: Red Hat 7.1+/8/9,FC 1-3): aquota.user, aquota.group
* Quota Version 1 (Linux 2.2 kernel: Red Hat 6, 7.0): quota.user, quota.group

The files can be converted/upgraded using the convertquota command.

# Re-boot or re-mount file partition with quotas.

* Re-boot: shutdown -r now
* Re-mount partition: mount -o remount /partition

After re-booting or re-mounting the file system, the partition will show up in the list of mounted filesystems as having quotas. Check /etc/mtab:


/dev/hda5 / ext3 rw,usrquota 0 0

# quotacheck -vgum /partition
or

quotacheck -vguma

* For example (Linux kernel 2.4+: Red Hat 7.1+, Fedora): quotacheck -vguma

quotacheck: WARNING – Quotafile //aquota.user was probably truncated. …
quotacheck: Scanning /dev/hda5 [/] done
quotacheck: Checked 9998 directories and 179487 files

* For example (Linux kernel 2.2: Red Hat 6/7.0): quotacheck -v /dev/hda6
System response:

Scanning /dev/hda6 [/home] done
Checked 444 directories and 3136 files
Using quotafile /home/quota.user

Quotacheck is used to scan a file system for disk usages, and updates the quota record file “quota.user/aquota.user” to the most recent state. It is recommended thet quotacheck be run at bootup (part of Redhat default installation)

Man page: quotacheck – scan a filesystem for disk usage, create, check and repair quota files

# quotaon -av
System Response: /dev/hda6: user quotas turned on

quotaon – enable disk quotas on a file system.
quotaoff – turn off disk quotas for a file system.

# edquota -u user_id
Edit directly using vi editor commands. (See below for more info.)
For example: edquota -u user1

* System Response (RH 7+):

Disk quotas for user user1 (uid 501):
Filesystem blocks soft hard inodes soft hard
/dev/hda5 1944 0 0 120 0 0

o blocks: 1k blocks
o inodes: Number of entries in directory file
o soft: Max number of blocks/inodes user may have on partition before warning is issued and grace persiod countdown begins.
If set to “0” (zero) then no limit is enforced.
o hard: Max number of blocks/inodes user may have on partition.
If set to “0” (zero) then no limit is enforced.

* System Response (RH 6):

Quotas for user user1:
/dev/sdb6: blocks in use: 56, limits (soft = 0, hard = 0)
inodes in use: 50, limits (soft = 0, hard = 0)

Something failed if you get the response:

/dev/sdb6: blocks in use: 0, limits (soft = 0, hard = 0)
inodes in use: 0, limits (soft = 0, hard = 0)

Edit limits:

Quotas for user user1:
/dev/hda6: blocks in use: 992, limits (soft = 50000, hard = 55000)
inodes in use: 71, limits (soft = 10000, hard = 11000)

If editing group quotas: edquota -g group_name

# List quotas:

quota -u user_id

For example: quota -u user1
System response:

Disk quotas for user user1 (uid 501):
Filesystem blocks quota limit grace files quota limit grace
/dev/hda6 992 50000 55000 71 10000 11000

If this does not respond similar to the above, then restart the computer: shutdown -r now

Quota Reports:

# Report on all users over quota limits: quota -q

Quota summary report: repquota -a

*** Report for user quotas on device /dev/hda5
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
———————————————————————-
root — 4335200 0 0 181502 0 0
bin — 15644 0 0 101 0 0

user1 — 1944 0 0 120 0 0

No limits shown with this user as limits are set to 0.

Cron:

Quotacheck should scan the file system via cronjob periodically (say, every week?). Add a script to the /etc/cron.weekly/ directory.
File: /etc/cron.weekly/runQuotacheck

* Linux Kernel 2.4: Red Hat 7.1 – Fedora Core 3:

#!/bin/bash
/sbin/quotacheck -vguma

* Linux Kernel 2.2: Red Hat 6/7.0:

#!/bin/bash
/sbin/quotacheck -v -a

(Remember to chmod +x /etc/cron.weekly/runQuotacheck)

Edquota Note:

The “edquota” command puts you into a “vi” editing mode so knowledge of the “vi” editor is necessary. Another editor may be specified with the EDITOR environment variable. You are NOT editing the quota.user file directly. The /partition/quota.user or quota.group file is a binary file which you do not edit directly. The command edquota gives you an ascii interface with the text prepared for you. When you “:wq” to save the file from the vi session, it is converted to binary by the edquota command and stored in the quota.user file.

Assigning quota for a bunch of users with the same value. To rapidly set quotas for all users, on my system to the same value as user user1, I would first edit user user1’s quota information by hand, then execute:

edquota -p user1 `awk -F: ‘$3 > 499 {print $1}’ /etc/passwd`

This assumes that the user uid’s start from 500 and increment upwards. “blocks in use” is the total number of blocks (in kilobytes) a user has comsumed on a partition. “inodes in use” is the total number of files a user has on a partition.

edquota options:
Option Description
-r
-m Edit quotas on remote server using RPC. Remote server must be configured with the daemon rpc.rquotad
-u Edit user quota
-g Edit group quota
-p user-id Duplicate the quotas based on existing prototype user
-F format
-F vfsold
-F vfsv0
-F rpc
-F xfs Format:
vfsold – version 1
vfsv0 – version 2
rpc – quotas over NFS
xfs – quotas for XFS filesystem
-f /file-system Perform on specified filesystem. Default is to apply on all filesystems with quotas
-t Edit the soft time limits for each filesystem.
-T Edit time for user/group when softlimit is enforced. Specify number and unit or “unset”

Soft Limit and Hard Limits:

Soft limit indicates the maximum amount of disk usage a quota user has on a partition. When combined with “grace period”, it acts as the border line, which a quota user is issued warnings about his impending quota violation when passed. Hard limit works only when “grace period” is set. It specifies the absolute limit on the disk usage, which a quota user can’t go beyond his “hard limit”.

Grace Period:

“Grace Period” is configured with the command “edquota -t”, “grace period” is a time limit before the “soft limit” is enforced for a file system with quota enabled. Time units of sec(onds), min(utes), hour(s), day(s), week(s), and month(s) can be used. This is what you’ll see with the command “edquota -t”:

System response:

* Linux Kernel 2.4+: Red Hat 7.1+/Fedora:

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/hda5 7days 7days

* Linux Kernel 2.2: Red Hat 6/7.0:

Time units may be: days, hours, minutes, or seconds
Grace period before enforcing soft limits for users:
/dev/hda2: block grace period: 0 days, file grace period: 0 days

Change the 0 days part to any length of time you feel reasonable. A good choice might be 7 days (or 1 week).

Quota files: (non-XFS file systems)

The edquota command will create/edit the quota file at the root of the file system. (See /etc/mtab for the list of the currently mounted filesystems.)

* Version 2: aquota.user, aquota.group
* Version 1: quota.user, quota.group

The Linux Kernel:

he default Red Hat/Fedora Core Linux kernel is shipped quota ready. If you have streamlined your kernel by rebuilding it with fewer options, make sure it has been configured with quotas support. When using the tools xconfig or menuconfig be sure to reply y to:
Quota support (CONFIG_QUOTA) [n] y

Fedora Core 3: grep CONFIG_QUOTA /usr/src/redhat/SOURCES/kernel-2.6.9-x86_64.config
Response:

CONFIG_QUOTA=y
CONFIG_QUOTACTL=y

The Redhat default init script /etc/rc.d/rc.sysinit will also contain a point in the script to run quotacheck:

* Red Hat 6, 7.0:

if [ -x /sbin/quotacheck ]; then
echo “Checking root filesystem quotas”
/sbin/quotacheck -v -a
fi

And turn quota checking on:

if [ -x /usr/sbin/quotaon ] then
echo “Turning on quota.”
/usr/sbin/quotaon -v -a
fi

REFERLINK : http://www.yolinux.com/TUTORIALS/LinuxTutorialQuotas.html

Linux Start up and Run Levels

The Init Program

As seen in the previous section, the kernel will start a program called init, if it finds it. The init process reads the file “/etc/inittab” and uses this file to determine how to create processes. Read the init man page for more information. Also note that init is always running and can dynamically do things and run processes based upon various signals. The administrator can also cause it to dynamically change system processes and runlevels by using the telinit program or editing the “/etc/inittab” file.

Runlevels

Linux utilizes what is called “runlevels”. A runlevel is a software configuration of the system that allows only a selected group of processes to exist. Init can run the system in one of eight runlevels. These runlevels are 0-6 and S or s. The system runs in only one of these runlevels at a time. Typically these runlevels are used for different purposes. Runlevels 0, 1, and 6 are reserved. For Redhat Linux version 6, the runlevels are:

0     halt
1     Single user mode
2     Multiuser, without NFS (The same as 3, if you don’t have networking)
3     Full multiuser mode
4     unused
5     X11
6     Reboot

The inittab file

The “/etc/inittab” file tells init which runlevel to start the system at and describes the processes to be run at each runlevel. An entry in the inittab file has the following format:

id:runlevels:action:process

  • id – A unique sequence of 1-4 characters which identifies an entry in inittab.
  • runlevels – Lists the runlevels for which the specified action should be taken. This field may contain multiple characters for different runlevels allowing a particular process to run at multiple runlevels. For example, 123 specifies that the process should be started in runlevels 1, 2, and 3.
  • action – Describes which action should be taken. Valid actions are listed below
    • respawn – The process will be restarted whenever it terminates.
    • wait – The process will be started once when the specified runlevel is entered and init will wait for its termination.
    • once – The process will be executed once when the specified runlevel is entered
    • boot – The process will be executed during system boot. The runlevels field is ignored.
    • bootwait – Same as “boot” above, but init waits for its termination.
    • off – This does nothing.
    • ondemand – This process will be executed whenever the specified ondemand runlevel is called.
    • initdefault – Specifies the runlevel which should be entered after system boot. If none exists, init will ask for a runlevel on the console. The process field is ignored.
    • sysinit – The process will be executed during system boot. It will be executed before any boot or bootwait entries. The runlevels field is ignored.
    • powerwait – The process will be executed when init receives the SIGPWR signal. Init will wait for the process to finish before continuing.
    • powerfail – Same as powerwait but init does not wait for the process to complete.
    • powerokwait – The process will be executed when init receives the SIGPWR signal provided there is a file called “/etc/powerstatus” containing the word “OK”. This means that the power has come back again.
    • ctrlaltdel – This process is executed when init receives the SIGINT signal. This means someone on the system console has pressed the “CTRL-ALT-DEL” key combination.
    • kbrequest – The process will be executed when init receives a signal from the keyboard handler that a special key combination was pressed on the console keyboard.
    • process – Specifies the process to be executed. If the process starts with the ‘+’ character, init will not do utmp and wtmp accounting for that process. This is needed for gettys that insist on doing their own utmp/wtmp housekeeping (a historic bug).

    Below is an example file:

    	# inittab       This file describes how the INIT process should set up
    	#               the system in a certain run-level.
    	#
    	# Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
    	#               Modified for RHS Linux by Marc Ewing and Donnie Barnes
    	#
    
    	# Default runlevel. The runlevels used by RHS are:
    	#   0 - halt (Do NOT set initdefault to this)
    	#   1 - Single user mode
    	#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
    	#   3 - Full multiuser mode
    	#   4 - unused
    	#   5 - X11
    	#   6 - reboot (Do NOT set initdefault to this)
    	#
    1)	id:3:initdefault:
    
    	# System initialization.
    2)	si::sysinit:/etc/rc.d/rc.sysinit
    
    3)	l0:0:wait:/etc/rc.d/rc 0
    4)	l1:1:wait:/etc/rc.d/rc 1
    5)	l2:2:wait:/etc/rc.d/rc 2
    6)	l3:3:wait:/etc/rc.d/rc 3
    7)	l4:4:wait:/etc/rc.d/rc 4
    8)	l5:5:wait:/etc/rc.d/rc 5
    9)	l6:6:wait:/etc/rc.d/rc 6
    
    	# Things to run in every runlevel.
    10)	ud::once:/sbin/update
    
    	# Trap CTRL-ALT-DELETE
    11)	ca::ctrlaltdel:/sbin/shutdown -t3 -r now
    
    	# When our UPS tells us power has failed, assume we have a few minutes
    	# of power left.  Schedule a shutdown for 2 minutes from now.
    	# This does, of course, assume you have powerd installed and your
    	# UPS connected and working correctly.
    12)	pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
    
    	# If power was restored before the shutdown kicked in, cancel it.
    13)	pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
    
    	# Run gettys in standard runlevels
    14)	1:2345:respawn:/sbin/mingetty tty1
    15)	2:2345:respawn:/sbin/mingetty tty2
    16)	3:2345:respawn:/sbin/mingetty tty3
    17)	4:2345:respawn:/sbin/mingetty tty4
    18)	5:2345:respawn:/sbin/mingetty tty5
    19)	6:2345:respawn:/sbin/mingetty tty6
    
    	# Run xdm in runlevel 5
    	# xdm is now a separate service
    20)	x:5:respawn:/etc/X11/prefdm -nodaemon

    On the left side of the file listing, above, are added numbers to help describe lines. Those lines without line numbers are either blank or begin with a “#” which means the line is a comment. Those line numbers are not part of the original file and are added here for reference purposes.

  • On line 1 above you see “id:3:initdefault:”. The id is “id” which stands for initdefault. Note that it is unique on all the numbered lines. The runlevel is 3 which sets the default starting runlevel to runlevel 3. The action is initdefault which tells init to make this runlevel the default runlevel. Note that the process field is blank since it is ignored by the initdefault action.
  • Line 2 tells init to run the program “/etc/rc.d/rc.sysinit” during system boot, before any other processes.
  • Lines 3 through 9 tell init to run the program “/etc/rc.d/rc” for runlevels 0 through 6. Note that for each line the appropriate runlevel is passed to the “/etc/rc.d/rc” script program on the command line. For example note on line 5 above the second field is the runlevel specifying 2. At the end of the line there is a space and a 2 which allows the variable 2 to be passed on the command line to the program.
  • Line 10 specifies that the program “/sbin/update” will run once for every runlevel.
  • Line 11 sets up the program “/sbin/shutdown” to run when someone on the system console has pressed the “CTRL-ALT-DEL” key combination.
  • Line 12 specifies “/sbin/shutdown” to run if the power fails. Note that there are different options passed on the command line for lines 11 and 12 although they run the same program.
  • Line 13 specified “/sbin/shutdown” will run if power is restored for any of runlevels 1 through 5.
  • Lines 14 through 19 specifies the “/sbin/mingetty” program to run on 6 different terminals for runlevels 2 through 5. This means that you can run 6 virtual terminals from your keyboard simultaneously by pressing “ALT-F1” through “ALT-F6”. Note pressing “ALT-F7” or above will do nothing, but the screen will not change from your current terminal.

Note the order of programs to run as specified above are:

  1. /etc/rc.d/rc.sysinit
  2. /etc/sbin/update
  3. /etc/rc.d/rc 3 – Note: we are running runlevel 3 here.

Therefore, the next thing that the system does is to run the rc.sysinit file, save buffers to the hard drive, then run system script files for the requested runlevel which will start up many system and network services as explained in the next section.