Linux磁盘配额

mac2026-06-15  14

Linux磁盘配额配置步骤

我们先把硬盘做成逻辑卷,再在此基础上进行磁盘配额。

四块硬盘为/dev/sdb,/dev/sdc,/dev/sdd,大小都为20G。

[root@localhost ~]# fdisk -l| grep /dev/sd Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 83886079 40893440 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors Disk /dev/sde: 21.5 GB, 21474836480 bytes, 41943040 sectors

用fdisk命令分别为四块新硬盘分区,都分为一个主分区,并准备做LVM,改system id为8e。

[root@localhost ~]# [root@localhost ~]# fdisk -l | grep /dev/sd Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 83886079 40893440 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors /dev/sdb1 2048 41943039 20970496 8e Linux LVM Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors /dev/sdc1 2048 41943039 20970496 8e Linux LVM Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors /dev/sdd1 2048 41943039 20970496 8e Linux LVM Disk /dev/sde: 21.5 GB, 21474836480 bytes, 41943040 sectors /dev/sde1 2048 41943039 20970496 8e Linux LVM

创建逻辑卷,更详细讲解可到笔者的博客:

https://blog.csdn.net/weixin_43515220/article/details/102864201

或者也可以参考其他博主的博客。

[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 //用四块磁盘创建物理卷 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdc1" successfully created. Physical volume "/dev/sdd1" successfully created. Physical volume "/dev/sde1" successfully created. [root@localhost ~]# vgcreate My_vg1 /dev/sd[b-e]1 //创建卷组,名为My_vg1 Volume group "My_vg1" successfully created [root@localhost ~]# lvcreate -L 40G -n My_lv1 My_vg1 //在卷组My_vg1上创建名为My_lv1的逻辑卷 Logical volume "My_lv1" created.

格式化逻辑卷

[root@localhost ~]# mkfs.xfs /dev/My_vg1/My_lv1 -f meta-data=/dev/My_vg1/My_lv1 isize=512 agcount=4, agsize=262144 = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=10485760, imaxpct = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=5120, version=2 = sectsz=512 sunit=0 blks, lazy-count realtime =none extsz=4096 blocks=0, rtextents=0

创建挂载点

[root@localhost ~]# mkdir /My_lv1

下面来配置磁盘配额

先介绍一下配置磁盘配额的步骤:

以支持配额功能的方式挂载文件系统编辑用户和组账户的配额设置验证磁盘配额功能查看磁盘配额使用情况 [root@localhost ~]# yum -y install xfsprogs &> /dev/null //需要安装xfsprogs和quota软件包,CentOS7集成了quota软件包

用vim编辑/etc/fstab文件,添加下面的配置来完成自动挂载和修改挂载权限

/dev/My_vg1/My_lv1 /My_lv1 xfs defaults,usrquota,grpquota 0 0

以支持配额功能的方式挂载

[root@localhost ~]# mount -o usrquota,grpquota /dev/My_vg1/My_lv1 /My_lv1/ //或者mount -a,因为已经在/etc/fstab做过配置了 [root@localhost ~]# df -Th | grep /My_lv1 //注意不要grep /My_lv1/ /dev/mapper/My_vg1-My_lv1 xfs 40G 33M 40G 1% /My_lv1

编辑用户和组账号的配额设置

方法:软限制、硬限制

模式:容量、文件数量

容量软:bsoft 容量硬:bhard

数量软:isoft 数量硬:ihard

[root@localhost ~]# xfs_quota -x -c 'limit -u bsoft=10G bhard=15G isoft=100 ihard=150 amy' /My_lv1/ //硬限制容量15G,文件数量150个,软限制容量10G,文件数量100个,对amy用户进行配额

查看配额使用情况

[root@localhost ~]# xfs_quota -c 'quota -iuv amy' /My_lv1/ Disk quotas for User amy (1000) Filesystem Files Quota Limit Warn/Time Mounted on /dev/mapper/My_vg1-My_lv1 0 100 150 00 [--------] /My_lv1 [root@localhost ~]# xfs_quota -x -c 'report -a' /My_lv1 User quota on /My_lv1 (/dev/mapper/My_vg1-My_lv1) Blocks User ID Used Soft Hard Warn/Grace ---------- -------------------------------------------------- root 0 0 0 00 [--------] amy 0 10485760 15728640 00 [--------] Group quota on /My_lv1 (/dev/mapper/My_vg1-My_lv1) Blocks Group ID Used Soft Hard Warn/Grace ---------- -------------------------------------------------- root 0 0 0 00 [--------]

因为/My_lv1是root用户创建的,我们来看下这个文件夹的权限

[root@localhost ~]# ls -l / | grep My_lv1 drwxr-xr-x. 2 root root 6 Nov 2 01:41 My_lv1

可知,amy用户没有权限写入,下面我们来修改权限

[root@localhost ~]# chmod 777 /My_lv1 [root@localhost ~]# ls -l / | grep My_lv1 drwxrwxrwx. 2 root root 6 Nov 2 01:41 My_lv1

切换到amy用户进行验证

[root@localhost ~]# su amy [amy@localhost root]$ cd [amy@localhost ~]$ pwd /home/amy [amy@localhost ~]$ dd if=/dev/zero of=/My_lv1/demo01.txt bs=1M count=7070+0 records in 70+0 records out 73400320 bytes (73 MB) copied, 0.405906 s, 181 MB/s [amy@localhost ~]$ ls /My_lv1/ demo01.txt [amy@localhost ~]$ touch /My_lv1/test{1..150} //超过最大文件数量 touch: cannot touch ‘/My_lv1/test150’: Disk quota exceeded

看现象可知,amy用户不能创建150个以上的文件,配额配置完成

最新回复(0)