Ubuntu 22.04
Sponsored Link

ディスククォータを設定する2022/09/22

 
ディスククォータを設定してディスクの使用量を制限します。
例として、[/home] にマウントした [ext4] ファイルシステムに対してクォータを設定します。
RedHat 系でデフォルトとなっている XFS ファイルシステムのクォータ設定はこちらを参照ください
[1] クォータツールをインストールします。
root@dlp:~#
apt -y install quota quotatool
[2] マウントオプションを追加してクォータを有効にします。
root@dlp:~#
df -h /home

Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1        79G   52K   75G   1% /home

# クォータを設定して再マウント

root@dlp:~#
umount /home

root@dlp:~#
tune2fs -O quota /dev/sdb1

tune2fs 1.46.5 (30-Dec-2021)
root@dlp:~#
mount /dev/sdb1 /home
# 確認

root@dlp:~#
tune2fs -l /dev/sdb1 | grep -i quota

Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize quota metadata_csum
User quota inode:         3
Group quota inode:        4
[3] ユーザークォータを設定します。例として [ubuntu] ユーザーにクォータを適用します。
# ユーザークォータを有効にする

root@dlp:~#
quotaon -uv /home

/dev/sdb1 [/home]: user quotas turned on
# 設定確認

root@dlp:~#
quotaon -ap

group quota on /home (/dev/sdb1) is on
user quota on /home (/dev/sdb1) is on
project quota on /home (/dev/sdb1) is off

# [ubuntu] ユーザーにクォータを設定

root@dlp:~#
edquota -u ubuntu
# Softリミット 4G, Hardリミット 5G を設定 (キロバイト単位で指定)

Disk quotas for user ubuntu (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft   hard
  /dev/sdb1                        28    4096000    5120000         10        0      0

# 設定確認

root@dlp:~#
*** Report for user quotas on device /dev/sdb1
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
ubuntu    --      28 4096000 5120000             10     0     0
[4] あるユーザーへの既存設定を他ユーザーにも適用する場合は以下のように設定します。
# [ubuntu] ユーザーのクォータ設定を [debian] ユーザーにも適用

root@dlp:~#
edquota -p ubuntu debian

root@dlp:~#
*** Report for user quotas on device /dev/sdb1
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
ubuntu    --      28 4096000 5120000             10     0     0
debian    --      16 4096000 5120000              4     0     0
[5] グループクォータを設定します。例として [ubuntu] グループにクォータを適用します。
# グループクォータを有効にする

root@dlp:~#
quotaon -gv /home

/dev/sdb1 [/home]: group quotas turned on
# 設定確認

root@dlp:~#
quotaon -ap

group quota on /home (/dev/sdb1) is on
user quota on /home (/dev/sdb1) is on
project quota on /home (/dev/sdb1) is off

# [ubuntu] グループにクォータを設定

root@dlp:~#
edquota -g ubuntu
# Softリミット 4G, Hardリミット 5G を設定 (キロバイト単位で指定)

Disk quotas for group ubuntu (gid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft   hard
  /dev/sdb1                        28    4096000    5120000         10        0      0

# 設定確認

root@dlp:~#
*** Report for group quotas on device /dev/sdb1
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
Group           used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      20       0       0              2     0     0
ubuntu    --      28 4096000 5120000             10     0     0
debian    --      16       0       0              4     0     0
[6] Warnquota を設定すると、制限を超えて使用しているユーザーに警告メールを送信することができます。
メールを送信するため、SMTPサーバーが稼働していることが前提となります
root@dlp:~#
vi /etc/quotatab
# クォータを設定したデバイスと要約を追記

/dev/sdb1: Home Directory
root@dlp:~#
vi /etc/warnquota.conf
# 必要であればメール送信元等を変更

MAIL_CMD        = "/usr/sbin/sendmail -t"
FROM            = "root@localhost"
# but they don't have to be:
SUBJECT         = Over quota
CC_TO           = "root@localhost"
SUPPORT         = "root@localhost"
PHONE           = "<enter your phone number here>"
#
CHARSET         = UTF-8

# warnquota 実行

root@dlp:~#
warnquota -s
# warnquota 実行時に制限を超えているユーザーには以下のようなメールが送信される

Return-Path: <root@dlp.srv.world>
X-Original-To: root@localhost
Delivered-To: root@localhost
Received: by dlp.srv.world (Postfix, from userid 0)
        id 75279A1521; Thu, 22 Sep 2022 04:33:26 +0000 (UTC)
From: root@localhost
Reply-To: root@localhost
Subject: Over quota
To: ubuntu@dlp.srv.world
Cc: root@localhost
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
Message-Id: <20220922043326.75279A1521@dlp.srv.world>
Date: Thu, 22 Sep 2022 04:33:26 +0000 (UTC)

Hi,

We noticed that you are in violation with the quotasystem
used on this system. We have found the following violations:


Home Directory (/dev/sdb1)

                        Block limits               File limits
Filesystem           used    soft    hard  grace    used  soft  hard  grace
/dev/sdb1      +-   5000M   4000M   5000M  6days      13     0     0


We hope that you will cleanup before your grace period expires.

Basically, this means that the system thinks you are using more disk space
on the above partition(s) than you are allowed.  If you do not delete files
and get below your quota before the grace period expires, the system will
prevent you from creating new files.

For additional assistance, please contact us at root@localhost
or via phone at <enter your phone number here>.
関連コンテンツ