Linux管理員 - 配額管理



CentOS磁碟配額可以同時啟用;在磁碟容量超過限制之前,提醒系統管理員並拒絕使用者進一步訪問磁碟儲存。當磁碟已滿時,根據磁碟上駐留的內容,整個系統可能會完全停止執行,直到恢復為止。

在CentOS Linux中啟用配額管理基本上是一個四步過程:

  • 步驟1 - 在`/etc/fstab`中為組和使用者啟用配額管理。

  • 步驟2 - 重新掛載檔案系統。

  • 步驟3 - 建立配額資料庫並生成磁碟使用情況表。

  • 步驟4 - 分配配額策略。

在`/etc/fstab`中啟用配額管理

首先,我們要備份我們的`/etc/fstab`檔案:

[root@centosLocal centos]# cp -r /etc/fstab ./

現在,我們在當前工作目錄中擁有已知正常工作的`/etc/fstab`的副本。

# 
# /etc/fstab 
# Created by anaconda on Sat Dec 17 02:44:51 2016 
# 
# 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/cl-root        /         xfs     defaults                      0 0
UUID = 4b9a40bc-9480-4     /boot     xfs     defaults                      0 0

/dev/mapper/cl-home        /home     xfs     defaults,usrquota,grpquota    0 0

/dev/mapper/cl-swap        swap      swap    defaults                      0 0

我們在要為使用者和組應用配額的卷或標籤的`/etc/fstab`選項部分進行了以下更改。

  • usrquota
  • grpquota

如您所見,我們使用的是xfs檔案系統。使用xfs時,會涉及額外的步驟。/home與`/`位於同一磁碟。進一步調查顯示`/`設定為`noquota`,這是一個核心級別的掛載選項。我們必須重新配置核心啟動選項。

root@localhost rdc]# mount | grep ' / ' 
/dev/mapper/cl-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

[root@localhost rdc]#

重新配置XFS檔案系統的核心啟動選項

此步驟僅在以下兩種情況下才需要:

  • 當我們正在啟用配額的磁碟/分割槽使用xfs檔案系統時
  • 當核心在啟動時將noquota引數傳遞給`/etc/fstab`時

步驟1 - 備份`/etc/default/grub`。

cp /etc/default/grub ~/

步驟2 - 修改`/etc/default/grub`。

這是預設檔案。

GRUB_TIMEOUT=5 
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" 
GRUB_DEFAULT=saved 
GRUB_DISABLE_SUBMENU=true 
GRUB_TERMINAL_OUTPUT="console" 
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet" 
GRUB_DISABLE_RECOVERY="true"

我們要修改以下行:

GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet" 

GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv 
=cl/swap rhgb quiet rootflags=usrquota,grpquota"

注意 - 正確複製這些更改非常重要。重新配置grub.cfg後,如果配置中出現任何錯誤,系統將無法啟動。請在非生產系統上嘗試本教程的這一部分。

步驟3 - 備份您的工作grub.cfg

cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg.bak

建立一個新的`grub.cfg`

[root@localhost rdc]# grub2-mkconfig -o /boot/grub2/grub.cfg 
Generating grub configuration file ... 
Found linux image: /boot/vmlinuz-3.10.0-514.el7.x86_64 
Found initrd image: /boot/initramfs-3.10.0-514.el7.x86_64.img 
Found linux image: /boot/vmlinuz-0-rescue-dbba7fa47f73457b96628ba8f3959bfd 
Found initrd image: /boot/initramfs-0-rescuedbba7fa47f73457b96628ba8f3959bfd.img 
done

[root@localhost rdc]#

重啟

[root@localhost rdc]#reboot

如果所有修改都精確無誤,我們應該無法向`xfs`檔案系統新增配額。

[rdc@localhost ~]$ mount | grep ' / ' 
/dev/mapper/cl-root on / type xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)
 
[rdc@localhost ~]$

我們已經透過grub傳遞了`usrquota`和`grpquota`引數。

現在,再次編輯`/etc/fstab`以包含`/`,因為`/home`位於同一物理磁碟上。

/dev/mapper/cl-root/xfs
defaults,usrquota,grpquota        0 0

現在讓我們啟用配額資料庫。

[root@localhost rdc]# quotacheck -acfvugM

確保配額已啟用。

[root@localhost rdc]# quotaon -ap 
group quota on / (/dev/mapper/cl-root) is on 
user quota on / (/dev/mapper/cl-root) is on 
group quota on /home (/dev/mapper/cl-home) is on 
user quota on /home (/dev/mapper/cl-home) is on 
[root@localhost rdc]#

重新掛載檔案系統

如果分割槽或磁碟與正在積極引導的分割槽分開,則無需重新引導即可重新掛載。如果在根目錄`/`中引導的磁碟/分割槽上配置了配額,則可能需要重新引導作業系統。強制重新掛載並應用更改,重新掛載檔案系統的需要可能會有所不同。

[rdc@localhost ~]$ df 
Filesystem              1K-blocks     Used      Available      Use%     Mounted on
/dev/mapper/cl-root     22447404      4081860   18365544       19%         /
devtmpfs                903448        0         903448         0%          /dev
tmpfs                   919308        100       919208         1%          /dev/shm
tmpfs                   919308        9180      910128         1%          /run
tmpfs                   919308        0         919308         0%          /sys/fs/cgroup
/dev/sda2               1268736       176612    1092124        14%         /boot
/dev/mapper/cl-var      4872192       158024    4714168        4%          /var
/dev/mapper/cl-home     18475008      37284     18437724       1%          /home
tmpfs                   183864        8         183856         1%          /run/user/1000

[rdc@localhost ~]$

如我們所見,正在使用LVM卷。因此,只需重新引導即可。這將重新掛載`/home`並將`/etc/fstab`配置更改載入到活動配置中。

建立配額資料庫檔案

CentOS現在能夠在`/home`上使用磁碟配額。要啟用完全配額支援,我們必須執行`quotacheck`命令。

`quotacheck`將建立兩個檔案:

  • aquota.user
  • aquota.group

這些用於儲存已啟用配額的磁碟/分割槽的配額資訊。

以下是常見的quotacheck開關。

開關 動作
-u 檢查使用者配額
-g 檢查組配額
-c 應為每個啟用了配額的檔案系統啟用配額
-v 顯示詳細輸出

新增每個使用者的配額限制

為此,我們將使用`edquota`命令,後跟使用者名稱:

[root@localhost rdc]# edquota centos

Disk quotas for user centos (uid 1000):  
Filesystem                   blocks       soft       hard     inodes     soft     hard 
/dev/mapper/cl-root              12          0          0         13        0        0  
/dev/mapper/cl-home            4084          0          0        140        0        0

讓我們看看每一列。

  • 檔案系統 - 這是應用於使用者的檔案系統配額

  • - 使用者當前在每個檔案系統上使用的塊數

  • 軟限制 - 設定軟限制的塊數。軟限制允許使用者在給定時間段內使用配額

  • 硬限制 - 設定硬限制的塊數。硬限制是允許的總配額

  • 索引節點 - 使用者當前使用的索引節點數

  • 軟限制 - 軟索引節點限制

  • 硬限制 - 硬索引節點限制

要檢查我們當前作為使用者的配額:

[centos@localhost ~]$ quota 
Disk quotas for user centos (uid 1000):  
Filesystem             blocks     quota      limit grace    files   quota   limit   grace 
/dev/mapper/cl-home    6052604    56123456   61234568       475     0       0       [centos@localhost ~]$

以下是當用戶超過硬配額限制時給出的錯誤。

[centos@localhost Downloads]$ cp CentOS-7-x86_64-LiveKDE-1611.iso.part ../Desktop/

cp: cannot create regular file ‘../Desktop/CentOS-7-x86_64-LiveKDE-
1611.iso.part’: Disk quota exceeded

[centos@localhost Downloads]$

如我們所見,我們非常接近該使用者的磁碟配額。讓我們設定一個軟限制警告。這樣,使用者將在配額限制過期之前收到提前通知。根據經驗,當用戶上班時需要花費45分鐘清理檔案才能實際開始工作時,您會收到終端使用者的投訴。

作為管理員,我們可以使用`repquota`命令檢查配額使用情況。

[root@localhost Downloads]# repquota  /home  
                    Block limits                            File limits  
User            used     soft     hard     grace       used     soft     hard     grace 
----------------------------------------------------------------------------------------
root      --       0         0        0                   3        0        0        
centos    -+ 6189824  56123456 61234568                 541      520      540      6days 

[root@localhost Downloads]#

如我們所見,使用者centos已超過其硬塊配額,並且無法再在`/home`上使用任何磁碟空間。

-+ 表示在檔案系統上已超過硬配額。

在規劃配額時,需要進行一些計算。管理員需要知道:系統上有多少使用者?在使用者/組之間分配多少可用空間?檔案系統上的一個塊包含多少位元組?

根據與可用磁碟空間相關的塊來定義配額。建議在檔案系統上保留一個“安全”的可用空間緩衝區,這將在最壞情況下保留:所有配額同時超過。這尤其適用於系統用於寫入日誌的分割槽。

廣告
© . All rights reserved.