Linux系统多人多任务环境,出现多人共用一块磁盘,那么如何分配就值得思考,搞平均注意还是根据用户的实际情况个性化分配呢?本文就来讲解一下,同时本文举例的磁盘配额只针对xfs文件系统。
【目录】
一 准备工作
二 批量创建用户
三 使用xfs_quota查看磁盘配额报告数据
一 准备工作
准备工作主要是针对/dev/sdb磁盘进行操作,这里只展示出最终结果。
[root@centos ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 2G 0 disk
└─sdb1 8:17 0 2G 0 part
└─quote_test-home_quota_test 253:2 0 2G 0 lvm /home
sr0 11:0 1 8.8G 0 rom /run/media/admin/CentOS 7 x86_64
[root@centos ~]# df -Th /home/
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/quote_test-home_quota_test xfs 2.0G 33M 2.0G 2% /home
复制
二 批量创建用户
前边中已经讲过shell脚本,那么批量创建用户,使用shell脚本肯定会更加方便一些。
编写creteuser.sh脚本
[root@centos achao]# vim creteuser.sh
[root@centos achao]# cat creteuser.sh
#!/bin/bash
groupadd test_quota_group
for username in test01 test02 test03 test04 test05
do
useradd -g test_quota_group ${username}
echo "Huawei@123" | passwd --stdin ${username}
done
mkdir /home/myquota
chgrp test_quota_group /home/myquota
chmod 2770 /home/myquota
[root@centos achao]# sh -n creteuser.sh #检验语言是否存在问题
[root@centos achao]# bash -n creteuser.sh #检验语法是否存在问题
复制
执行脚本
[root@centos achao]# sh creteuser.sh
Changing password for user test01.
passwd: all authentication tokens updated successfully.
Changing password for user test02.
passwd: all authentication tokens updated successfully.
Changing password for user test03.
passwd: all authentication tokens updated successfully.
Changing password for user test04.
passwd: all authentication tokens updated successfully.
Changing password for user test05.
passwd: all authentication tokens updated successfully.
#查看
[root@centos achao]# ls -l /home/
total 0
drwxrws---. 2 root test_quota_group 6 Aug 14 00:38 myquota
drwx------. 3 test01 test_quota_group 78 Aug 14 00:38 test01
drwx------. 3 test02 test_quota_group 78 Aug 14 00:38 test02
drwx------. 3 test03 test_quota_group 78 Aug 14 00:38 test03
drwx------. 3 test04 test_quota_group 78 Aug 14 00:38 test04
drwx------. 3 test05 test_quota_group 78 Aug 14 00:38 test05
复制
实现永久挂载需要写入/etc/fstab文件中,
[root@centos ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu May 16 08:53:55 2024
#
# 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/centos-root / xfs defaults 0 0
UUID=5a23d334-3fd0-4a46-b0ea-26801b98d412 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/mapper/quote_test-home_quota_test /home xfs defaults,usrquota,grpquota 0 0
复制
写入最后一行,注意观察填写参数defaults,usrquota,grpquota
接下来进行umount和mount -a,前边的文章中已经说过,检测修改/etc/fstab文件中是否有错误,如果没有则修改并挂载成功。
[root@centos ~]# umount -a
umount: /run/user/1000/gvfs: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /run/user/1000: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /sys/fs/cgroup/systemd: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /sys/fs/cgroup: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /run: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /dev: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@centos ~]# mount -a
[root@centos ~]#
[root@centos ~]# mount | grep home
/dev/mapper/quote_test-home_quota_test on /home type xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)
复制
对磁盘配额测参数做如下解释:
usrquota/uquota/quota:针对用户账号的设置。
gquota/grpquota:针对用户组的设置。
pquota/prjquota:针对单一目录的设置,但是不可于grpquota同时存在。
三 使用xfs_quota查看磁盘配额报告数据
3.1 语法格式及参数介绍
xfs_quota(8) System Manager's Manual xfs_quota(8)
NAME
xfs_quota - manage use of quota on XFS filesystems
SYNOPSIS
xfs_quota [ -x ] [ -p prog ] [ -c cmd ] ... [ -d project ] ... [ path ... ]
xfs_quota -V
DESCRIPTION
xfs_quota is a utility for reporting and editing various aspects of filesystem quota.
The options to xfs_quota are:
-c cmd xfs_quota commands may be run interactively (the default) or as arguments on the command line. Multiple -c arguments may
be given. The commands are run in the sequence given, then the program exits.
-p prog Set the program name for prompts and some error messages, the default value is xfs_quota.
-x Enable expert mode. All of the administrative commands (see the ADMINISTRATOR COMMANDS section below) which allow modi‐
fications to the quota system are available only in expert mode.
-d project
Project names or numeric identifiers may be specified with this option, which restricts the output of the individual
xfs_quota commands to the set of projects specified. Multiple -d arguments may be given.
-V Prints the version number and exits
复制
3.2 列出目前系统各个文件系统,以及文件系统的磁盘配额挂载参数支持
[root@centos ~]# xfs_quota -x -c "print"
Filesystem Pathname
/ /dev/mapper/centos-root
/boot /dev/sda1
/home /dev/mapper/quote_test-home_quota_test (uquota, gquota)
复制
3.3 列出目前/home这个支持磁盘配额的挂载点文件系统使用情况
[root@centos ~]# xfs_quota -x -c "df -h" /home
Filesystem Size Used Avail Use% Pathname
/dev/mapper/quote_test-home_quota_test
1.9G 32.3M 1.9G 2% /home
复制
3.4 列出目前/home的所有用户的磁盘配置限制
[root@centos ~]# xfs_quota -x -c "report -ubin" /home/
User quota on /home (/dev/mapper/quote_test-home_quota_test)
Blocks Inodes
User ID Used Soft Hard Warn/Grace Used Soft Hard Warn/ Grace
---------- -------------------------------------------------- --------------------------------------------------
#0 0 0 0 00 [--------] 4 0 0 00 [--------]
#1001 12 0 0 00 [--------] 7 0 0 00 [--------]
#1002 12 0 0 00 [--------] 7 0 0 00 [--------]
#1003 12 0 0 00 [--------] 7 0 0 00 [--------]
#1004 12 0 0 00 [--------] 7 0 0 00 [--------]
#1005 12 0 0 00 [--------] 7 0 0 00 [--------]
[root@centos ~]# xfs_quota -x -c "report -ugr" /home/
User quota on /home (/dev/mapper/quote_test-home_quota_test)
Realtime Blocks
User ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 0 0 0 00 [--------]
test01 0 0 0 00 [--------]
test02 0 0 0 00 [--------]
test03 0 0 0 00 [--------]
test04 0 0 0 00 [--------]
test05 0 0 0 00 [--------]
Group quota on /home (/dev/mapper/quote_test-home_quota_test)
Realtime Blocks
Group ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 0 0 0 00 [--------]
test_quota_group
复制
列出所有用户的目前文件使用情况,并且列出设置值。注意单位Blocks
3.5 列出目前支持的磁盘配额文件系统是否启动了磁盘配额功能
[root@centos ~]# xfs_quota -x -c "state"
User quota state on /home (/dev/mapper/quote_test-home_quota_test)
Accounting: ON #启用计算功能
Enforcement: ON #有实际磁盘配额管理功能
Inode: #88 (2 blocks, 2 extents) #以上几行说明有启动user的限制能力
Group quota state on /home (/dev/mapper/quote_test-home_quota_test)
Accounting: ON
Enforcement: ON
Inode: #89 (2 blocks, 2 extents) #同理,有启动group的限制能力
Project quota state on /home (/dev/mapper/quote_test-home_quota_test)
Accounting: OFF
Enforcement: OFF
Inode: #89 (2 blocks, 2 extents) #同理,project并未支持
Blocks grace time: [7 days] #grace time的选项
Inodes grace time: [7 days]
Realtime Blocks grace time: [7 days]
复制
说明:report:列出目前磁盘配额选项,有-ugr(user,group,project)及-bi等。
在默认情况下,xfs_quota的report命令会将支持的user,group,directory相关信息显示出来。如果只是想要某个特定的选项,如果想要列出用户的信息,则在report后边加上-u即可。
下篇我们主要讲解如何设置soft、hard及inode等。