1. 程式人生 > >磁碟和檔案系統管理(二)

磁碟和檔案系統管理(二)

對於linux系統的使用者來說如何正確的來進行系統的分割槽,如何當初對系統評估不準確的話,那麼一旦系統分割槽不夠用了,那麼不得不備份,刪除相關資料,重要的時候被迫還得重新規劃分割槽並裝系統來滿足需求。今天我們來寫一下如何在linux下進行LVM邏輯卷的使用,讓我們自由調整分割槽容量。

一. LVM邏輯卷管理

  • 概述
LVM:(邏輯卷管理Logical Volume Manager)就是可以自由調整分割槽大小  作用:動態調整磁碟容量,從而提高磁碟管理的靈活性
需要注意:/boot分割槽用於存放引導檔案,不能基於LVM建立
圖形介面:system-config-lvm
  • LVM的管理命令

PV(Physical Volume,物理卷)

整個硬碟,或使用fdisk等工具建立的普通分割槽
包括許多預設4MB大小的PE(Physical Extent,基本單元)

VG(Volume Group,卷組)

 一個或多個物理卷組合而成的整體

LV(Logical Volume,邏輯卷)

 從卷組中分割出的一塊空間,用於建立檔案系統
  • LVM應用例項
功能 物理卷管理 卷組管理 邏輯卷管理
Scan 掃描 pvscan vgscan lvscan
Create 建立 pvcreate vgcreate lvcreate
Display 顯示 pvdisplay vgdisplay lvdisplay
Remove 刪除 pvremove vgremove lvremove
Extend 擴充套件 vgextend lvextend
Reduce 減少 vgreducelvreduce

首先我在系統上添加了兩塊硬碟 /dev/sdb /dev/dbc來為下面的LVM做準備,你也可以先進行fdisk工具分完區然後再做PV,此處我們直接拿新新增的硬碟來做PV

1.物理卷【PV】的建立、刪除.

  • 建立物理卷【pvcreate 裝置名/裝置名2….】
[root@localhost ~]# pvcreate /dev/sdb /dev/sdc 
  Physical volume "/dev/sdb" successfully created
  Physical volume "/dev/sdc" successfully created
[root@localhost ~]# pvscan   
  PV         VG       Fmt  Attr PSize   PFree  
  /dev/sda2  VolGroup lvm2 a--   49.51g      0 
  /dev/sdb            lvm2 a--  100.00g 100.00g
  /dev/sdc            lvm2 a--   50.00g  50.00g
[root@localhost ~]# pvdisplay 
  --- Physical volume --- 
  "/dev/sdb" is a new physical volume of "100.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               100.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               su9PDS-jtXa-SKWH-n4Pf-TM0j-w8SW-swgd6d

  "/dev/sdc" is a new physical volume of "50.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc
  VG Name               
  PV Size               50.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               AASOe8-1yje-2rjn-O33d-0y27-PmIw-YoYCpF
  • 刪除物理卷【pvremove 裝置名/裝置名2….】前提是下面沒有分配VG
[root@localhost ~]# pvremove /dev/sdc
  Labels on physical volume "/dev/sdc" successfully wiped
[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize   PFree 
  /dev/sdb   vg_new   lvm2 a--  100.00g 50.00g
[root@localhost ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg_new
  PV Size               100.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              25599
  Free PE               12799
  Allocated PE          12800
  PV UUID               su9PDS-jtXa-SKWH-n4Pf-TM0j-w8SW-swgd6d

2.卷組【VG】建立、擴充套件、減少、刪除、

  • 建立卷組【vgcreate 卷組名 物理卷名1 物理卷名2 】
[root@localhost ~]# vgcreate vg_new /dev/sdb /dev/sdc
  Volume group "vg_new" successfully created
[root@localhost ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg_new" using metadata type lvm2
  Found volume group "VolGroup" using metadata type lvm2
[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_new
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               149.99 GiB
  PE Size               4.00 MiB
  Total PE              38398
  Alloc PE / Size       0 / 0   
  Free  PE / Size       38398 / 149.99 GiB
  VG UUID               qZmTmm-g2fV-La4E-vzcN-yN9S-giyQ-eWra2G
  • 擴展卷組【vgextend 卷組名 物理卷名】
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  vg_new     1   0   0 wz--n- 50.00g 50.00g
[root@localhost ~]# vgextend vg_new /dev/sdb
  Volume group "vg_new" successfully extended
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree  
  vg_new     2   0   0 wz--n- 149.99g 149.99g
[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_new
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               149.99 GiB
  PE Size               4.00 MiB
  Total PE              38398
  Alloc PE / Size       0 / 0   
  Free  PE / Size       38398 / 149.99 GiB
  VG UUID               31tIMa-ldzd-caOd-0lsl-tZnY-QN47-H6mYO9
  • 縮減卷組【vgreduce 卷組名 物理卷】
[root@localhost ~]# vgreduce vg_new /dev/sdb
  Removed "/dev/sdb" from volume group "vg_new"
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  vg_new     1   0   0 wz--n- 50.00g 50.00g
[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_new
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               50.00 GiB
  PE Size               4.00 MiB
  Total PE              12799
  Alloc PE / Size       0 / 0   
  Free  PE / Size       12799 / 50.00 GiB
  VG UUID               31tIMa-ldzd-caOd-0lsl-tZnY-QN47-H6mYO9
  • 刪除卷組【vgremove 卷組名 】
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  VolGroup   1   2   0 wz--n- 49.51g     0 
  vg_new     1   0   0 wz--n- 50.00g 50.00g
[root@localhost ~]# vgremove vg_new
  Volume group "vg_new" successfully removed

3.邏輯卷【LV】建立、擴充套件、縮減、刪除

  • 建立邏輯卷【lvcreate -L 大小 -n 邏輯卷名 卷組名 】
[root@localhost ~]# lvcreate -L 50G -n lv_new vg_new
  Logical volume "lv_new" created
[root@localhost ~]# lvscan
  ACTIVE            '/dev/vg_new/lv_new' [50.00 GiB] inherit
[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg_new/lv_new
  LV Name                lv_new
  VG Name                vg_new
  LV UUID                wxH3Nn-GKXH-UrRX-Xotl-1bVi-GWUC-VQejzZ
  LV Write Access        read/write
  LV Creation host, time localhost, 2016-04-17 13:17:13 -0400
  LV Status              available
  # open                 0
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
 - currently set to     256
  Block device           253:2  
  • 擴充套件邏輯卷【lvextend -L +大小 /dev/卷組名/邏輯卷名】
[root@localhost ~]# lvs
  LV      VG       Attr      LSize  Pool Origin Data%  Move Log Cpy%Sync Convert                                  
  lv_new  vg_new    -wi-a---- 50.00g 
[root@localhost ~]# lvextend -L +20G /dev/vg_new/lv_new 
  Extending logical volume lv_new to 70.00 GiB
  Logical volume lv_new successfully resized
[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg_nw/lv_new
  LV Name                lv_new
  VG Name                vg_new
  LV UUID                1JAHRk-JShy-jsqE-IpOL-Bzbq-VGOS-0HhVtz
  LV Write Access        read/write
  LV Creation host, time localhost, 2016-04-17 13:56:42 -0400
  LV Status              available
  # open                 0
  LV Size                70.00 GiB
  Current LE             17920
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
 - currently set to     256
  Block device           253:2
  • 縮減邏輯卷空間【注意:必須按以下幾步操作】

1、 先解除安裝邏輯卷lv_new 【邏輯卷只在掛載目錄使用】

[root@localhost ~]# df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
              ext4     45G  3.2G   40G   8% /
tmpfs        tmpfs    491M     0  491M   0% /dev/shm
/dev/sda1     ext4    485M   38M  423M   9% /boot
/dev/mapper/vg_new-lv_new
              ext4     69G  180M   66G   1% /date
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# umount /date

2、 然後通過e2fsck命令檢測邏輯捲上空餘的空間。

[root@localhost ~]# e2fsck -f /dev/vg_new/lv_new 
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg_new/lv_new: 139/4587520 files (0.0% non-contiguous), 334336/18350080 blocks

3、 使用resize2fs將檔案系統減少到50G。

[root@localhost ~]# resize2fs /dev/mapper/vg_new-lv_new 50G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg_new-lv_new to 13107200 (4k) blocks.
The filesystem on /dev/mapper/vg_new-lv_new is now 13107200 blocks long.

4、 再使用lvreduce命令將邏輯卷減少到50G。

[root@localhost ~]# lvreduce -L 50G /dev/vg_new/lv_new 
  WARNING: Reducing active logical volume to 50.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_new? [y/n]: Y
  Reducing logical volume lv_new to 50.00 GiB
  Logical volume lv_new successfully resized
[root@localhost ~]# mount /dev/vg_new/lv_new /date
[root@localhost ~]# df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
              ext4     45G  3.2G   40G   8% /
tmpfs        tmpfs    491M     0  491M   0% /dev/shm
/dev/sda1     ext4    485M   38M  423M   9% /boot
/dev/mapper/vg_new-lv_new
              ext4     50G  182M   47G   1% /date

注意:檔案系統大小和邏輯卷大小一定要保持一致才行。如果邏輯卷大於檔案系統,由於部分割槽域未格式化成檔案系統會造成空間的浪費。如果邏輯卷小於檔案系統,哪資料就出問題了

  • 刪除邏輯卷【lvremove /dev/vg_new/lv_new】
[root@localhost ~]# lvs
  LV      VG       Attr      LSize  Pool Origin Data%  Move Log Cpy%Sync Convert                               
  lv_new  vg_new    -wi-a---- 50.00g                                             
[root@localhost ~]# lvremove /dev/vg_new/lv_new 
Do you really want to remove active logical volume lv_new? [y/n]: Y
  Logical volume "lv_new" successfully removed

如果某一個塊物理磁碟或者分割槽故障了,如何將資料轉移到其他卷組空間中。
1、通過pvmove命令轉移空間資料

[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize   PFree  
  /dev/sda2  VolGroup lvm2 a--   49.51g      0 
  /dev/sdb   vg0      lvm2 a--  100.00g 100.00g
  /dev/sdc   vg0      lvm2 a--   50.00g  50.00g
[root@localhost ~]# pvmove /dev/sdb /dev/sdc

2、通過vgreduce命令將即將壞的磁碟或者分割槽從卷組vgdata裡面移除除去。

[root@localhost ~]# vgreduce lv_new /dev/sdb

3、通過pvremove命令將即將壞的磁碟或者分割槽從系統中刪除掉。

[root@localhost ~]# pvremove /dev/sdb

4、手工拆除硬碟或者通過一些工具修復分割槽。

二.設定磁碟配額

  • 實現磁碟限額的條件
 1.需要Linux核心支援
 2.安裝quota軟體包
  • Linux磁碟限額的特點
作用範圍:針對指定的檔案系統(分割槽)
限制物件:使用者帳號、組帳號 
限制類型:
     磁碟容量(預設單位為KB)
     檔案數量
限制方法:
     軟限制:一個使用者在一定時間範圍內(預設為一週,可以使用命令“edquota -t”重新設定,時間單位可以為天、小時、分 鍾、秒)超過其限制的額度,在不超出硬限制的範圍內可以繼續使用空間,系統會發出警告(警告資訊設定檔案為“/etc/warnquota.conf”),但如果使用者達到時間期限仍未釋放空間到限制的額度下,系統將不再允許該使用者使用更多的空間。
     硬限制:一個使用者可擁有的磁碟空間或檔案的絕對數量,絕對不允許超過這個限制。
  • 啟用檔案系統的配額支援
 新增usrquota、grpquota掛載引數 
  • 檢測磁碟配額並建立配額檔案

    使用quotacheck命令建立配額檔案
    quotacheck -ugcv 檔案系統
    quotacheck -augcv

-u、-g:檢測使用者、組配額
 -c:建立配額資料檔案
 -v:顯示執行過程資訊
 -a:檢測所有可用的分割槽
  • 編輯使用者和組帳號的配額設定
 使用edquota命令編輯配額設定
  edquota -u 使用者名稱
  edquota -g 組名
  • 啟用、關閉檔案系統的配額功能
 使用quotaon、quotaoff命令
  • 驗證磁碟配額功能
必須切換到設定配額的分割槽(掛載目錄)
 建立指定數量的檔案:使用touch命令,或cp命令
 建立指定容量的檔案:使用dd命令,或cp命令
  • 檢視配額使用情況
 側重使用者、組帳號角度:使用quota命令
    quota -u 使用者名稱
    quota -g 組名
 側重檔案系統角度:使用repquota

1. 練習步驟:

  • 建立使用者hunter,並設定為此使用者進行磁碟配額
[root@localhost ~]# useradd hunter
[root@localhost ~]# passwd hunter
Changing password for user hunter.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
  • 建立一個邏輯分割槽,對此分割槽設定配額。
[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 4
First cylinder (1-13054, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): 
Using default value 13054

Command (m for help): 
Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb8d88355

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb4               1       13054   104856223+   5  Extended

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (1-13054, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): 
Using default value 13054

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb8d88355

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb4               1       13054   104856223+   5  Extended
/dev/sdb5               1       13054   104856192   83  Linux

Command (m for help): w
[root@localhost ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb4  /dev/sdb5
[root@localhost ~]# partx -a /dev/sdb
sdb   sdb4  sdb5  
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@localhost ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb4  /dev/sdb5
  • 建立掛載點,並格式化分割槽和檔案系統
[root@localhost ~]# mkdir /quota
[root@localhost ~]# mkfs.ext4 /dev/sdb5
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214048 blocks
1310702 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

修改配置檔案,設定掛載位置永久生效。

[root@localhost date]# df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
              ext4     45G  3.2G   40G   8% /
tmpfs        tmpfs    491M     0  491M   0% /dev/shm
/dev/sda1     ext4    485M   38M  423M   9% /boot
/dev/sdb5     ext4     99G  188M   94G   1% /quota
[root@localhost date]# vim /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Mon Jun 15 08:16:31 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/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=1773339b-7194-409d-872d-6a850058e748 /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
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
[root@localhost date]# mount -a 
[root@localhost date]# df -TH
Filesystem    Type     Size   Used  Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
              ext4      49G   3.5G    43G   8% /
tmpfs        tmpfs     515M      0   515M   0% /dev/shm
/dev/sda1     ext4     508M    39M   443M   9% /boot
/dev/sdb5     ext4     106G   197M   101G   1% /quota
  • 使用quotacheck命令生成配置磁碟配置的資料庫檔案,如果出現以下許可權不夠的情況,通過setenforce 0臨時關掉selinux就可以了,quotacheck執行成功可以看到/quota/下面多了兩個檔案。然後通過quotaon /quota/啟動對應檔案系統上的磁碟配額功能。
[root@localhost date]# quotacheck -cug /quota
quotacheck: Cannot create new quotafile /quota/aquota.user.new: Permission denied
quotacheck: Cannot initialize IO on new quotafile: Permission denied
quotacheck: Cannot create new quotafile /quota/aquota.group.new: Permission denied
quotacheck: Cannot initialize IO on new quotafile: Permission denied
[root@localhost date]# setenforce 0  ---> 將selinux進行關閉
[root@localhost date]# quotacheck -cug /quota
[root@localhost date]# ls -ltr 
total 0
[root@localhost date]# ls -ltr /quota
total 32
drwx------. 2 root root 16384 Apr 17 16:39 lost+found
-rw-------. 1 root root  6144 Apr 17 18:46 aquota.user
-rw-------. 1 root root  6144 Apr 17 18:46 aquota.group
  • 通過edquota -u hunter配置使用者hunter對這個磁碟分割槽的使用配額。還可以通過edquota -g groupname 對groupname這個組設定配額
[root@localhost date]# edquota -u hunter 

Disk quotas for user hunter (uid 501):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb5                         2      10240       20480          2        5       10

Filesystem 檔案系統

blocks 已經使用的塊數(塊的單位為1K)

    soft 塊數軟限制,0表示禁用

    hard 塊數硬限制,0表示禁用

    inodes 已經建立的檔案個數,如果後面有*表示已經超出軟限制

    soft 建立的檔案個數的軟限制,0表示禁用

    hard 建立的檔案個數的硬限制,0表示禁用

    edquota -p user1 user2 把user1使用者的設定複製給user2使用者

這裡對hunter這個用對該分割槽磁碟的容量軟限制為10M,硬限制為20M(即使該分割槽有50M的空間),對檔案個數的軟限制為5個,硬限制為10個。

下面進行測試,測試之前要給hunter這個使用者對/quota目錄寫許可權。

進入實戰測試,看剛才我給hunter使用者做的磁碟限額是否生效

[root@localhost date]# su - hunter        #切換到hunter使用者
[hunter@localhost ~]$ cd /quota/
[hunter@localhost quota]$ touch user{1..5}  #建立5個空檔案
[hunter@localhost quota]$ ls -ltr 
total 32
drwx------. 2 root   root   16384 Apr 17 16:39 lost+found
-rw-------. 1 root   root    7168 Apr 17 19:59 aquota.user
-rw-rw-r--. 1 hunter hunter     0 Apr 17 20:06 user5
-rw-rw-r--. 1 hunter hunter     0 Apr 17 20:06 user4
-rw-rw-r--. 1 hunter hunter     0 Apr 17 20:06 user3
-rw-rw-r--. 1 hunter hunter     0 Apr 17 20:06 user2
-rw-rw-r--. 1 hunter hunter     0 Apr 17 20:06 user1
-rw-------. 1 root   root    7168 Apr 17 20:06 aquota.group
[hunter@localhost quota]$ touch user6   #此處觸發了軟限制
sdb5: warning, user file quota exceeded.
[hunter@localhost quota]$ ls 
aquota.group  aquota.user  lost+found  use6  user1  user2  user3  user4  user5
[hunter@localhost quota]$ touch user{7..15} #此處真正到了硬限制
sdb5: write failed, user file limit reached.
touch: cannot touch `user11': Disk quota exceeded
touch: cannot touch `user12': Disk quota exceeded
touch: cannot touch `user13': Disk quota exceeded
touch: cannot touch `user14': Disk quota exceeded
touch: cannot touch `user15': Disk quota exceeded
[hunter@localhost quota]$ rm -rf user*  #刪除之後我們再來試驗一下容量的限制
[hunter@localhost quota]$ ls -ltr 
total 32
drwx------. 2 root   root   16384 Apr 17 16:39 lost+found
-rw-------. 1 root   root    7168 Apr 17 19:59 aquota.user
-rw-------. 1 root   root    7168 Apr 17 20:06 aquota.group
[hunter@localhost quota]$ dd if=/dev/zero of=text.txt bs=1M count=11
sdb5: warning, user block quota exceeded.  #此處已經觸發了容量10M的軟限制,不過還能進行寫入
11+0 records in
11+0 records out
11534336 bytes (12 MB) copied, 0.373587 s, 30.9 MB/s
hunter@localhost quota]$ dd if=/dev/zero of=text.txt bs=1M count=21  
sdb5: warning, user block quota exceeded.#此處已經觸發了20M的硬限制,現已經寫不進去了,看來做的對使用者的磁碟限額沒有問題。
sdb5: write failed, user block limit reached.
dd: writing `text.txt': Disk quota exceeded
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.351462 s, 59.7 MB/s
  • 切換root使用者使用repquota -a來檢視當前各磁碟配額的使用情況。從下圖可以看出hunter使用者已經達到了磁碟使用的最大容量限制
[root@localhost date]# repquota -a 
*** Report for user quotas on device /dev/sdb5
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      20       0       0              2     0     0       
hunter    +-   11264   10240   20480  6days       1     5    10       

需要注意的是,當用戶觸發軟限制時,grace time就會倒計時,在這個時間(預設是7天)沒有耗盡之前,若使用者還不清理磁碟使之符合軟限制的要求,則軟限制就會變成硬限制,這個時間叫寬限期。可以通過edquota -t設定這個時間,分別設定容量和檔案數量的寬限期。

[root@localhost date]# 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/sdb5                     7days                  7days

對於LVM邏輯卷和磁碟配額還是比較重要的,請各位大神多多指教!!!