磁碟空間管理
Home Up

 

磁碟空間管理(Disk Quota)

什麼是Disk Quota
DISK Quota是系統管理者可用來管理磁碟使用空間的一種工具,利用quota管理者可以在兩方面控制user不至於濫用磁碟,一是磁碟空間,一是檔案數目。UNIX中quota的設定是以file system為對象,而非整個系統,也就是說你可只針對某一file system設定quota。
Soft Limit Hard Limit & Grace Period
Soft Limit、Hard Limit & Grace Period是管理者利用quota觀念,對user使用磁碟所設的三個限制。

Soft Limit
Soft Limit 是user使用所能使用磁碟空間的大小,但如果Grace Period不是設為0 的話,則Soft Limit代表是一個警戒線,使用者使用磁碟空間超過這個警戒線,就會開始收到系統所給的警告。

Hard Limit
只有當Grace Period不是設為0 時,Hard Limit的設定才會生效。Hard Limit代表的是user對於磁碟空間使用的絕對上限。使用者使用磁碟空間絕對無法超越此一上限。

Grace period
Grace period是user可被容許超過softlimt的時間,假設Grace period設定為3天,超過soft limit的user在三天內未處理超過limit的檔案的話,user將無法再使用任何磁碟空間(這會造成該user無法login)。
Disk Quota on Linux
Linux支援disk quota,但目前僅限於ext2 file system,為了練習disk quota,接下來我們將在現有的linux上建立一個ext2的檔案系統。

Build an Ext2 File System on Linux

Before we start
Before we start,我們必須確定兩點:一是現有磁碟中尚存未被使用的空間(Not formated),二是這些空間未在DOS下切割partition(Not Partitioned )。

Build an Ext2 File System on Linux
Step1.
將PC開啟成Linux系統,執行指令 fdisk [/dev/hda]劃分一個ext2的磁區。
(在教室中之硬碟尚有200M未被劃分磁區,因此ext2 file system將建立在跟系統同一科磁碟下,指令中/dev/hda可略之,因為它是fdisk的default drive。)
***Linux fdisk指令與DOS非常類似,使用時須相當小心,以免將現有系統毀於一旦。

cc201:~# fdisk
^^^^^
Using /dev/hda as default device!
^^^^^^^^^^^^^fdisk未加參數,default 為/dev/hda
Command (m for help): p
^^顯示目前partition切割情形
Disk /dev/hda: 64 heads, 63 sectors, 620 cylinders
Units = cylinders of 4032 * 512 bytes
Device Boot Begin Start End Blocks Id System
/dev/hda1 * 1 1 356 717664+ 6 DOS 16-bit >=32M
Command (m for help):n
^^Create a new partition
Command action
e extended
p primary partition (1-4)
p
^^Create a new primary partiton
Partition number (1-4): 2
^^/dev/hda1had been set,select create the second partiotion
First cylinder (357-620): 357
^^^選擇新partition第一個cylynder所在,一般就選剩餘空間的第一個
Last cylinder or +size or +sizeM or +sizeK ([357]-620): +10M
^^^^選擇新partition size
Command (m for help):p
Disk /dev/hda: 64 heads, 63 sectors, 620 cylinders
Units = cylinders of 4032 * 512 bytes
Device Boot Begin Start End Blocks Id System
/dev/hda5 * 1 1 356 717664+ 6 DOS 16-bit >=32M
/dev/hda2 357 357 362 12096 83 Linux native
新的partition已經產生^^^^^^^^
Command (m for help):w
^^將partition資料寫入系統(生效

Step 2.
Reboot系統,讓partition生效。

Step 3. (這個動作類似DOS2的format,目的是將硬碟製作成UNIX格式)
產生ext2 file system
cc201:~# mke2fs -c /dev/hda2
mke2fs 1.04, 16-May-96 for EXT2 FS 0.5b, 95/08/09
3024 inodes, 12096 blocks
604 blocks (4.99%) reserved for the super user
First data block=1
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2 block groups
8192 blocks per group, 8192 fragments per group
1512 inodes per group
Superblock backups stored on blocks:
8193
Checking for bad blocks (read-only test): done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
cc201:~#

Step 4.
Mount New File System
cc201:~# df
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/hda1 717472 683504 33968 95% /
cc201:~# mkdir /home1
cc201:~# mount -t ext2 -o rw /dev/hda2 /home1
cc201:~# df
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/hda1 717472 683520 33952 95% /
/dev/hda2 11709 13 11092 0% /home1
New file system had been mounted as /home1 ^^^^^^^
cc201:~#

Setting up Quota on Linux Ext2 File System

Quota Setup

Step1.
Modify /etc/fstab as following:
***/etc/fstab定義的是系統開機後,會被自動mount的檔案系統。

/dev/hda1 / umsdos defaults 1 1
/dev/hda2 /home1 ext2 derfaults,usrquota 1 1
/dev/swapfile swap swap defaults 1 1
none /proc proc defaults 1 1

Step 2.
Create quota record "quota.user" on the base of new file system(/home1)
cc201:/etc# touch /home1/quota.user
cc201:/etc# chmod 600 /home1/quota.user

Step3.
Reboot for the change we have made.
系統重開之後,/home1 file system就會被自動mount,同時具有disk quota 功
能。

Enforce disk quota on users

Edit user disk quota
cc201:~# edquota woody
Quotas for user woody:
/dev/hda2: blocks in use: 0, limits (soft = 4000, hard = 4500)
inodes in use: 0, limits (soft = 400, hard = 420)
~
~
~
上面例子設定user woody可以使用file system 2 M or 4000 blocks空間(soft limit),絕對上限
為2.25M or 4500 blocks(hard limit)﹔可以儲存檔案數為400個,檔案個數絕對上限為420
個。
cc201:/home1# edquota -t
Time units may be: days, hours, minutes, or seconds
Grace period before enforcing soft limits for users:
/dev/hda2: block grace period:4 days, file grace period: 3 days
~
~
~
上面例子設定系統block(空間)Grace Period為4天,inode(file數) Grace Period為3天。

Enable Disk Quota
cc201:/home1# quotaon –a
quota on之後系統才會在每一次user使用磁碟時,立即check disk quota。

Run Quota Check Manually
cc201:/home1# quotacheck -a
通常於系統開機時進行之,以確定disk quota記錄up to date。

Check quota usage – by user
cc201:/home1# mkdir woody ####mkdir woody and change owner to woody,so that
cc201:/home1# chown woody woody #### woody can write file to /home1
cc201:/home1# su woody ####以woody身分使用系統
cc201:/home1# su woody
> cp /etc/* /home1/woody #### copy some files to /home/woody
> quota –v #### woody檢查自己quota使用情形
Disk quotas for user woody (uid 501):
Filesystem blocks quota limit grace files quota limit grace
/dev/hda2 270 4000 4200 68 400 450
user woody has used 270 blocks of space and 68 files (Still under soft limt, grace period is
empty!

Check quota usage – by root
cc201:/home1/woody# repquota -a
Block limits File limits
User used soft hard grace used soft hard grace
root -- 6 0 0 6 0 0
woody -- 270 4000 4200 68 400 450
edu -- 131 0 0 8 0 0
cc201:/home1/woody#
***請注意上例中user root及edu因未設quota所以soft & hard Limit均為0。
Limit設為零表示該user不受diqk quota限制。

Make disk quota on automatically on system boot up
在system initialization file /etc/rc.d/rc.S(in RedHAT this would be /etc/rc.d/rc.sysinit)中,系統mount /etc/fstab所有檔案系統之後(在 "/sbin/mount -avt nonfs" 之後),加入以下內容:
#Check quota and then run quota on
if [-x /usr/sbin/quotacheck]
then
echo "Checking quotas. This may take some time."
/usr/sbin/quotacheck –avug
echo "Done"
fi
if [-x /usr/sbin/quotaon]
then
echo "Turning on quota."
/usr/sbin/quotaon –avug
fi
 

建置及維護者:
國立中興大學 電算中心    吳賢明   woody@nchu.edu.tw
本網頁最近更新時間:2000/08/28 PM 03:45:26