Linux添加硬盤並編輯配額
在vmware中實現給linux添加硬盤
實驗環境:redhat 5.5 ,vmware workstation 8.0
一、添加虛擬磁盤
給虛擬主機添加 一塊磁盤,大小20.0G,重啓以後登錄:
[root@xml-ora1 u01]# fdisk -l
Disk /dev/hda: 500.1 GB,500105249280 bytes
255 heads, 63 sectors/track, 60801cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Device Boot Start End Blocks Id System
/dev/hda1 1 60801 488384001 83 Linux
Disk /dev/sda: 19.3 GB,19327352832 bytes
255 heads, 63 sectors/track, 2349 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 637 5116671 83 Linux
/dev/sda2 638 892 2048287+ 83 Linux
/dev/sda3 893 1019 1020127+ 83 Linux
/dev/sda4 1020 2349 10683225 5 Extended
/dev/sda5 1020 1146 1020096 83 Linux
/dev/sda6 1147 1273 1020096 82 Linux swap / Solaris
/dev/sda7 1274 2349 8642938+ 83 Linux
Disk /dev/sdb: 21.4 GB,21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Device Boot Start End Blocks Id System
上面會出現一塊 /dev/sdb的磁盤,正是我們剛剛添加的磁盤。
二、創建分區
[root@xml-ora1 u01]# fdisk /dev/sdb
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes willremain in memory only,
until you decide to write them. After that,of course, the previous
content won't be recoverable.
The number of cylinders for this disk isset to 2610.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partitiontable 4 will be corrected by w(rite)
Command (m for help):_n_ ······輸入n 創建一個新的分區
Command action
e extended
p primarypartition (1-4)······ 一到四個主要分區
p ······創建一個主要分區
Partition number (1-4): 1 ······創建一個新主分區,區號爲1
First cylinder (1-2610, default 1): ___ ······柱面默認1
Using default value 1
Last cylinder or +size or +sizeM or +sizeK(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.
三、格式化和mount分區
查看創建完成的磁盤信息
[root@xml-ora1 u01]# fdisk -l /dev/sdb
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 244 1959898+ 83 Linux
[root@xml-ora1 u01]# mkfs.ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
245280 inodes, 489974 blocks
24498 blocks (5.00%) reserved for the superuser
First data block=0
Maximum filesystem blocks=503316480
15 block groups
32768 blocks per group, 32768 fragments pergroup
16352 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystemaccounting information: done
This filesystem will be automaticallychecked every 26 mounts or
180 days, whichever comes first. Usetune2fs -c or -i to override.
創建一個目錄,並且將分區掛載到該目錄
[root@xml-ora1 u01]# mkdir /u01
[root@xml-ora1 u01]# mount /dev/sdb1 /u01
[root@xml-ora1 u01]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 4.8G 1.3G 3.3G 28% /
/dev/sda7 1.9G 35M 1.8G 2% /u01
/dev/sda5 965M 18M 898M 2% /tmp
/dev/sda3 965M 43M 873M 5% /var
/dev/sda2 1.9G 36M 1.8G 2% /home
tmpfs 502M 0 502M 0% /dev/shm
/dev/sdb1 1.9G 35M 1.8G 2% /u01
/dev/hda1 459G 417G 19G 96% /u02
用戶同樣可以通過查看mtab來查看當前操作系統磁盤掛載情況
[root@xml-ora1 u01]# cat /etc/mtab
/dev/sda1 / ext3 rw 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 00
/dev/sda7 /u01 ext3 rw 0 0
/dev/sda5 /tmp ext3 rw 0 0
/dev/sda3 /var ext3 rw 0 0
/dev/sda2 /home ext3 rw 0 0
tmpfs /dev/shm tmpfs rw 0 0
/dev/sdb1 /u01 ext3 rw,usrquota,grpquota 00
/dev/hda1 /u02 ext3 rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_miscrw 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefsrw 0 0
四、fstab自動掛載
/etc/fstab文件每一行由空格分爲六項,包括:
• 設備文件
• 掛載點
• 文件系統類型
• mount參數:包含defaults和auto的行會在開機時自動被加載。
• dump參數:0表示永不備份。1表示每天備份,2表示每兩天備份一次,依次類推。
• fsck順序:0表示忽略。1、2、3表示檢查順序。一般1被根文件系統使用。網絡文件系統與光驅軟驅等設備應該被忽略。
[root@xml-ora1 u01]# vi /etc/fstab
LABEL=/ / ext3 defaults 1 1
LABEL=/u01 /u01 ext3 defaults 1 2
LABEL=/tmp /tmp ext3 defaults 1 2
LABEL=/var /var ext3 defaults 1 2
LABEL=/home /home ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda6 swap swap defaults 0 0
/dev/hda1 /u02 ext3 defaults 00
/dev/sdb1 /u01 ext3 defaults,usrquota,grpquota 0 0
添加以上最後一行信息,包括配額設置信息:
/dev/sdb1 /u01 ext3 defaults,usrquota,grpquota 0 0
使用remount指令將修改信息加載到/etc/mtab中
[root@xml-ora1 u01]#mount -o remount /u01
五、磁盤配額分配
1、quotacheck生成配額文件
[root@xml-ora1 u01]# quotacheck -auvg
quotacheck: Scanning /dev/sdb1 [/u01] done
quotacheck: Checked 4 directories and 4files
或者
[root@xml-ora1 u01]# quotacheck -uvg /u01
quotacheck: Scanning /dev/sdb1 [/u01] done
quotacheck: Checked 4 directories and 4files
這樣就在文件系統的根目錄下建立aquota.user及aquota.group兩個文件,查看如下:
[root@xml-ora1 u01]# ll /u01/
total 36
drwxr-xr-x 2 oracle oinstall 4096 Aug 19 16:58 1
-rw------- 1 root root 7168 Aug 20 15:53 aquota.group
-rw------- 1 root root 7168 Aug 20 15:53 aquota.user
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
quotacheck [-gucfinvdmMR] [-F <quota-format>] filesystem|-a
參數說明:
-a :掃瞄所有在 /etc/mtab 裏頭已經 mount 的具有 quota 支持的磁盤
-u :掃瞄用戶的檔案與目錄
-v :顯示掃瞄過程
-g :掃瞄羣組使用的檔案與目錄
說明:
這個指令主要的目的在掃瞄某一個磁盤的quota 空間,他會針對該磁盤進行掃瞄,並且,由於該磁盤若持續運作時,可能掃瞄的過程中,檔案可能會增減,造成 quota 掃瞄的錯誤發生,因此,當使用 quotacheck 時,該磁盤將『自動被設定成爲唯獨磁區(read-only);至於掃瞄完畢之後,掃瞄所得的磁盤空間結果會寫入該磁區最頂端(例如:在我的例子中,掃瞄 /home 這個 /dev/hda3 的磁區,如果是初次掃瞄,那麼掃瞄完畢之後會產生 aquota.user 與 aquota.group ,會放置在 /home/aquota.xxx 底下!而如果是建立 quota 後的掃瞄,那麼就會更新這兩個檔案!)另外, Linux 也特別強調 quota 在使用的時候,需要特別注意在 reboot 時,得先將 quota 關閉纔好!
2、edquota編輯配額文件
使用edquota 用戶名與edquota –g 組名編輯用戶與組的磁盤限額
[root@xml-ora1 u01]# edquota oracle
Disk quotas for user oracle (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/sdb1 4 40 0 1 0 0
參數說明:
filesystem:開啓配額的分區partition
blocks: 已經使用的塊大小
soft: 軟限制使用的大小,單位Kbytes (數值爲 0 的時候,表示沒有限制)
hard: 硬限制使用的大小(當軟限制的大小到達後系統允許擴長,直到達硬限制數)
inodes: 當前用戶在本文件系統中可創建的目錄數
edquota(edit quota)
功能說明:編輯用戶或羣組的quota。
edquota: Usage:
edquota [-u] [-x] [-F formatname] [-p username] [-f filesystem] username...
edquota -g [-x] [-F formatname] [-p groupname] [-f filesystem] groupname...
edquota [-u|g] [-x] [-F formatname] [-f filesystem] -t
edquota [-u|g] [-x] [-F formatname] [-f filesystem] -Tusername|groupname ...
語 法:edquota [-p <源用戶名稱>][-ug][用戶或羣組名稱...] 或 edquota [-ug] -t
補充說明:edquota預設會使用vi來編輯使用者或羣組的quota設置。
參 數:
-u 設置用戶的quota,這是預設的參數。
-g 設置羣組的quota。
-p <源用戶名稱> 將源用戶的quota設置套用至其他用戶或羣組。
-t 設置寬限期限。(就是超過quota 值後,還能使用硬碟的寬限期限)
- f filesystem:這個是那個 partition 的意思
說明:
這個指令就是在編輯每一個『個人』或者是『羣組』的 quota 數值!通常以 edquota -u username 或者是 edquota -g groupname 來編輯個人與羣組的 quota 設定值。他支持直接 copy 一個人的設定值給其他人。
3、quotaon開啓配額功能
開啓配額功能:
[root@xml-ora1 u01]# quotaon -guv /u01
/dev/sdb1 [/u01]: group quotas turned on
/dev/sdb1 [/u01]: user quotas turned on
[root@xml-ora1 u01]# quotaon -guvp /u01
group quota on /u01 (/dev/sdb1) is on
user quota on /u01 (/dev/sdb1) is on
關閉配額功能:
[root@xml-ora1 u01]# quotaoff -guv /u01
/dev/sdb1 [/u01]: group quotas turned off
/dev/sdb1 [/u01]: user quotas turned off
[root@xml-ora1 u01]# quotaoff -guvp /u01
group quota on /u01 (/dev/sdb1) is off
user quota on /u01 (/dev/sdb1) is off
quotaon /quotaoff: Usage:
quotaon /quotaoff [-guvp] [-F quotaformat] [-x state] -a
quotaon /quotaoff [-guvp] [-F quotaformat] [-x state] filesystem
參數說明:
-a :全部的 quota 設定都啓動(會自動去尋找/etc/mtab 的設定)
-u :使用者的 quota 啓動
-g :羣組的 quota 設定啓動
-s :顯示訊息
說明:這個指令是在啓動 quota 的。由於這個指令是啓動aquota.group 與 aquota.user 的,所以必須要先完成qutoacheck,然後才能進行quotaon -a 啓動。
4、quota顯示配額設置
[root@xml-ora1 u01]# quota -u oracle
Disk quotas for user oracle (uid 500):
Filesystem blocks quota limit grace files quota limit grace
/dev/sdb1 500* 40 500 3 3 0
quota: Usage: quota [-guqxvs] [-l | -Q][-i] [-F quotaformat]
quota [-qxvs] [-l | -Q] [-i] [-F quotaformat] -u username ...
quota [-qxvs] [-l | -Q] [-i] [-F quotaformat] -g groupname ...
參數說明:
-g :顯示 group 羣組配額設置
-u :顯示 user用戶配額設置
-v :顯示 quota 的值
-s :選擇 inod 或 硬碟空間來顯示
說明:
這個指令僅是使用來顯示(display)目前某個羣組或者某個使用者的 quota 限值。
5、soft配額測試
[root@xml-ora1 u01]# pwd
/u01
[root@xml-ora1 u01]# ll /home/oracle/catoutw.log
-rw-r--r-- 1 oracle oinstall 202487Jun 28 15:04 /home/oracle/catoutw.log
[root@xml-ora1 u01]# cp /home/oracle/catoutw.log /u01/cp1.log
root@xml-ora1 u01]# ll
total 236
-rw------- 1 root root 7168 Aug 20 17:08 aquota.group
-rw------- 1 root root 7168 Aug 20 17:08 aquota.user
-rw-r--r-- 1 root root 202487 Aug 20 17:06 cp1.log
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
切換到配額用戶oracle:
[root@xml-ora1 u01]# su - oracle
[oracle@xml-ora1 ~]$ cd /u01
[oracle@xml-ora1 u01]$ ll
total 236
-rw------- 1 root root 7168 Aug 20 17:08 aquota.group
-rw------- 1 root root 7168 Aug 20 17:08 aquota.user
-rw-r--r-- 1 root root 202487 Aug 20 17:06cp1.log
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
[oracle@xml-ora1 u01]$ cp /home/oracle/catoutw.log /u01/cp2.log
sdb1: warning, user block quota exceeded.
[oracle@xml-ora1 u01]$ ll
total 440
-rw------- 1 root root 7168 Aug 20 17:11 aquota.group
-rw------- 1 root root 7168 Aug 20 17:08 aquota.user
-rw-r--r-- 1 root root 202487 Aug 20 17:06 cp1.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:11 cp2.log
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
[oracle@xml-ora1 u01]$ cp/home/oracle/catoutw.log /u01/cp3.log
[oracle@xml-ora1 u01]$ ll
total 440
-rw------- 1 root root 7168 Aug 20 17:11 aquota.group
-rw------- 1 root root 7168 Aug 20 17:08 aquota.user
-rw-r--r-- 1 root root 202487 Aug 20 17:06 cp1.log
-rw-r--r-- 1 oracleoinstall 202487 Aug 20 17:11 cp2.log
-rw-r--r-- 1 oracleoinstall 202487 Aug 20 17:11 cp3.log
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
結論:軟限制當超過時,不會馬上阻止用戶空間使用,因爲硬限制設置爲0,可以持續增長。但是會提示warning,警告用戶超過了配額限制。
6、hard配額測試
[root@xml-ora1 u01]# edquota oracle
Disk quotas for user oracle (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/sdb1 408 40 500 2 3 0
[root@xml-ora1 u01]# su - oracle
[oracle@xml-ora1 ~]$ cp/home/oracle/catoutw.log /u01/cp4.log
sdb1: write failed, user block limit reached.
cp: writing `/u01/cp4.log': Disk quotaexceeded
[oracle@xml-ora1 u01]$ ll
total 736
-rw------- 1 root root 7168 Aug 20 17:11 aquota.group
-rw------- 1 root root 7168 Aug 20 17:08 aquota.user
-rw-r--r-- 1 root root 202487 Aug 20 17:06 cp1.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:11 cp2.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:13 cp3.log
-rw-r--r-- 1 oracle oinstall 90112 Aug 20 17:46 cp4.log
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
結論:這個時候會提示錯誤,說到達限制,無法繼續執行cp命令,而目錄下的文件的cp4.log仍然執行了部分拷貝,查看它的內容,文件被截斷了,是部分數據位90K,而完整文件爲2M。
繼續試驗,如果不是用戶創建的,是root將存在的文件屬主更改爲oracle的話,如何呢?
[root@xml-ora1 u01]# quota -s oracle
Disk quotas for user oracle (uid 500):
Filesystem blocks quota limit grace files quota limit grace
/dev/sdb1 500* 40 500 3 3 0
注意這裏的grace爲空
[root@xml-ora1 u01]#ll
total 736
-rw------- 1 root root 7168 Aug 20 17:11 aquota.group
-rw------- 1 root root 7168 Aug 20 17:08 aquota.user
-rw-r--r-- 1 root root 202487 Aug 20 17:06cp1.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:11 cp2.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:13 cp3.log
-rw-r--r-- 1 oracle oinstall 90112 Aug 20 17:46 cp4.log
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
[root@xml-ora1 u01]# chown oracle.oinstallcp1.log
[root@xml-ora1 u01]# ll
total 736
-rw------- 1 root root 7168 Aug 20 17:11 aquota.group
-rw------- 1 root root 7168 Aug 20 17:08 aquota.user
-rw-r--r-- 1 oracle oinstall 202487 Aug 20 17:06 cp1.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:11 cp2.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:13 cp3.log
-rw-r--r-- 1 oracle oinstall 90112 Aug 20 17:46 cp4.log
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
[root@xml-ora1 u01]# quota -s oracle
Disk quotas for user oracle (uid 500):
Filesystem blocks quota limit grace files quota limit grace
/dev/sdb1 704* 40 500 4* 3 0 7days
注意這裏的grace爲7days,說明寬限期爲7天,這個是可以修改的。如下:
7、修改軟限額期限
[root@xml-ora1 u01]# edquota -t
Grace period before enforcing soft limitsfor users:
Time units may be: days, hours, minutes, orseconds
Filesystem Block grace period Inode grace period
/dev/sdb1 7days 7days
默認爲7天,修改爲希望的值,比如3天
[root@xml-ora1 u01]# edquota -t
Grace period before enforcing soft limitsfor users:
Time units may be: days, hours, minutes, orseconds
Filesystem Block graceperiod Inode grace period
/dev/sdb1 3days 3days
8、複製配額到其他用戶
可以一個一個的複製:
[root@xml-ora1 u01]# quota nobody
Disk quotas for user nobody (uid 99): none
[root@xml-ora1 u01]# edquota -p oracle nobody
[root@xml-ora1 u01]# chown nobody.nobodycp1.log
[root@xml-ora1 u01]# ll
total 740
-rw------- 1 root root 7168 Aug 20 18:42 aquota.group
-rw------- 1 root root 10240 Aug 20 18:42 aquota.user
-rw-r--r-- 1 nobody nobody 202487 Aug 20 17:06 cp1.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:11 cp2.log
-rw-r--r-- 1 oracle oinstall 202487 Aug 2017:13 cp3.log
-rw-r--r-- 1 oracle oinstall 90112 Aug 20 17:46 cp4.log
drwx------ 2 root root 16384 Aug 19 16:17 lost+found
[root@xml-ora1 u01]# quota nobody
Disk quotas for user nobody (uid 99):
Filesystem blocks quota limit grace files quota limit grace
/dev/sdb1 204* 40 500 3days 1 3 0
使用awk命令來批處理:
edquota -p test `awk -F: '$3 > 499 {print $1}' /etc/passwd`
複製到uid大於499的所有用戶,我的系統上有一個用戶nfsnobody也符合這個條件,手動關閉它的限額。
9、後續擴展
http://linux.vbird.org/linux_basic/0420quota.php