RHEL6 学习:磁盘配额

今天学习了 RHEL6 磁盘配额的内容,在生产环境下可能会用到,实验步骤如下:

磁盘配额主要步骤

  1. 修改 /etc/fstab ,打开目录配额选项。
  2. 重新 mount 需要限制配额的目录
  3. 创建配额配置文件 ( quotacheck )
  4. 制定目录限额策略,例如目录使用大小,文件使用总数等。

磁盘配额

2.1 修改/etc/fstab 打开目录配额选项

修改 /etc/fstab 文件,增加 userquota, grpquota 选项,文件如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# /etc/fstab  
# Created by anaconda on Thu Mar 8 22:18:35 2012
#
# 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/vg_redhat6-lv_root / ext4 defaults 1 1
UUID=a0565a60-ddff-492e-aa25-ef8bca28e710 /boot ext4 defaults 1 2
/dev/mapper/vg_redhat6-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
/dev/mapper/vg_redhat6-lv_pgdata_01 /database/skytf/pgdata1 ext4 defaults,usrquota,grpquota 0 0

2.2 重新 mount 需要限制配额的目录

1
[root@redhat6 ~]# mount -o remount /database/skytf/pgdata1

2.3 创建配额配置文件(quotacheck)

1
[root@redhat6 ~]# quotacheck -cug /dev/mapper/vg_redhat6-lv_pgdata_01

2.4 给指定用户制定目录配额策略
有两种方法制定用户限额策略。

2.4.1 使用 edquota 编辑

1
2
3
4
[root@redhat6 ~]# edquota postgres  
Disk quotas for user postgres (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/mapper/vg_redhat6-lv_pgdata_01 20 40960 102400 5 6 0

2.4.2 使用 setquota 命令

1
[root@redhat6 pgdata1]# setquota -u postgres 40960 102400 5 6 /database/skytf/pgdata1

备注:上述命令是限制 postgresql 用户在 目录 /database/skytf/pgdata1 下最多使用 100 M空间,当使用空间达到 40 MB 时发出 warning 信息,同时最多创建 6 个文件,当创建第五个文件时发出 warning 信息。

2.5 查看当前限额情况

1
2
3
4
5
6
7
[root@redhat6 log]# repquota -s /database/skytf/pgdata1  
* Report for user quotas on device /dev/mapper/vg_redhat6-lv_pgdata_01
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
postgres -- 16 40960 100M 1 5 6

2.6 开启配额

1
2
3
[root@redhat6 log]# quotaon -vaug  
/dev/mapper/vg_redhat6-lv_pgdata_01 [/database/skytf/pgdata1]: group quotas turned on
/dev/mapper/vg_redhat6-lv_pgdata_01 [/database/skytf/pgdata1]: user quotas turned on

2.7 测试一 ( 文件 limit )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[postgres@redhat6 pgdata1]$ cd /database/skytf/pgdata1  
[postgres@redhat6 pgdata1]$ ll
total 32
-rw-------. 1 root root 6144 Mar 14 15:53 aquota.group
-rw-------. 1 root root 6144 Mar 14 16:36 aquota.user
drwx------. 2 postgres postgres 16384 Mar 14 15:04 lost+found
[postgres@redhat6 pgdata1]$
[postgres@redhat6 pgdata1]$ touch a.txt
[postgres@redhat6 pgdata1]$ touch b.txt
[postgres@redhat6 pgdata1]$ touch c.txt
[postgres@redhat6 pgdata1]$ touch d.txt
[postgres@redhat6 pgdata1]$ touch e.txt
dm-2: warning, user file quota exceeded.
[postgres@redhat6 pgdata1]$ touch f.txt
dm-2: write failed, user file limit reached.
touch: cannot touch "f.txt": Disk quota exceeded

备注:在目录 /database/skytf/pgdata1 下创建第五个文件时,发出 warning 信息,但第五个文件创建成功,当创建第六个文件时,提示 failed ,提示失败。

2.8 测试二: 配额测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[postgres@redhat6 pgdata1]$ dd if=/dev/zero of=1.txt bs=1M count=10  
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0504467 s, 208 MB/s
[postgres@redhat6 pgdata1]$ dd if=/dev/zero of=2.txt bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0469055 s, 224 MB/s
[postgres@redhat6 pgdata1]$ dd if=/dev/zero of=3.txt bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0476163 s, 220 MB/s
[postgres@redhat6 pgdata1]$ dd if=/dev/zero of=4.txt bs=1M count=10
dm-2: warning, user block quota exceeded.
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0420879 s, 249 MB/s
[postgres@redhat6 pgdata1]$ dd if=/dev/zero of=5.txt bs=1M count=10
dm-2: warning, user file quota exceeded.
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.114665 s, 91.4 MB/s
[postgres@redhat6 pgdata1]$ ll
total 51232
-rw-rw-r--. 1 postgres postgres 10485760 Mar 14 17:01 1.txt
-rw-rw-r--. 1 postgres postgres 10485760 Mar 14 17:01 2.txt
-rw-rw-r--. 1 postgres postgres 10485760 Mar 14 17:01 3.txt
-rw-rw-r--. 1 postgres postgres 10485760 Mar 14 17:01 4.txt
-rw-rw-r--. 1 postgres postgres 10485760 Mar 14 17:02 5.txt
-rw-------. 1 root root 6144 Mar 14 15:53 aquota.group
-rw-------. 1 root root 6144 Mar 14 16:59 aquota.user
drwx------. 2 postgres postgres 16384 Mar 14 15:04 lost+found

备注:创建五个文件,每个文件大小为 10M, 当使用空间达到 40 M 时,系统发出 warning 信息。

常见问题

3.1 quotacheck 时异常

1
2
3
4
5
[root@redhat6 ~]# quotacheck -cug /dev/mapper/vg_redhat6-lv_pgdata_01  
quotacheck: Cannot create new quotafile /database/skytf/pgdata1/aquota.user.new: Permission denied
quotacheck: Cannot initialize IO on new quotafile: Permission denied
quotacheck: Cannot create new quotafile /database/skytf/pgdata1/aquota.group.new: Permission denied
quotacheck: Cannot initialize IO on new quotafile: Permission denied

备注:解决方法,关闭 seliniux,关闭,启动 selinux 如下所示。

3.2 关闭 seliniux

1
2
/usr/sbin/setenforce 0 立刻关闭 SELINUX  
/usr/sbin/setenforce 1 立刻启用 SELINUX

最后推荐和张文升共同编写的《PostgreSQL实战》,本书基于PostgreSQL 10 编写,共18章,重点介绍SQL高级特性、并行查询、分区表、物理复制、逻辑复制、备份恢复、高可用、性能优化、PostGIS等,涵盖大量实战用例!

购买链接:https://item.jd.com/12405774.html

PostgreSQL实战
感谢支持!
0%