samba服务器与xfs_quota磁盘配额的结合使用

实验规划

OSIP工作
openEuler22.03192.168.96.250客户端
Centos stream 8192.168.96.249服务端

实验所需软件包

openEuler

samba-client 【提供samba客户端服务】

cifs-utils 【提供文件共享服务】

centos

samba及其依赖软件包 【提供samba服务端服务】

xfsprogs 【提供xfs磁盘配额的功能】

配置实验

centos

配置软件仓库

[BaseOS]
name=BaseOS
baseurl=file:///mnt/cdrom/BaseOS
enabled=1
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:///mnt/cdrom/AppStream
enabled=1
gpgcheck=0

挂载

[root@localhost yum.repos.d]# echo "/dev/cdrom /mnt/cdrom iso9660 defaults   0 0" **>>** /etc/fstab

注意,此处为追加>>,不是覆盖>

验证是否可用

[root@localhost yum.repos.d]# dnf makecache
BaseOS                                                                                                                                              3.3 MB/s | 3.9 kB     00:00
AppStream                                                                                                                                           3.6 MB/s | 4.4 kB     00:00
Metadata cache created.

安装所需软件包

[root@localhost yum.repos.d]# dnf install samba* 

【如果此时,你记不住xfs_quota的包名怎么办?】

[root@localhost yum.repos.d]# dnf provides xfs_quota
Last metadata expiration check: 0:04:20 ago on Tue 08 Oct 2024 09:40:22 AM CST.
**xfsprogs-5.0.0-10.el8.i686** : Utilities for managing the XFS filesystem
Repo        : BaseOS
Matched from:
Filename    : /usr/sbin/xfs_quota

xfsprogs-5.0.0-10.el8.x86_64 : Utilities for managing the XFS filesystem
Repo        : @System
Matched from:
Filename    : /usr/sbin/xfs_quota

xfsprogs-5.0.0-10.el8.x86_64 : Utilities for managing the XFS filesystem
Repo        : BaseOS
Matched from:
Filename    : /usr/sbin/xfs_quota

安装

[root@localhost yum.repos.d]# dnf install xfsprogs*

编写samba配置文件

[global]
        workgroup = SAMBA

关注一下当前内容,如果workgroup = SAMBA,并且windows还要访问samba服务,推荐改为WORKGROUP



添加以下内容
[public]                       #共享名
        comment =public        #描述信息
        path= /share           #要共享出去的目录
        writable= yes          #允许读写
        read only = no         #关闭只读
        browseable = yes       #信息网络可见
        valid users = samba01  #审核访问用户
 

在smb.conf中,我们已经规划好了我们需要的目录和用户,现在来创建他们

[root@localhost ~]# useradd samba01
[root@localhost ~]# smbpasswd -a samba01
New SMB password:
Retype new SMB password:
Added user samba01.
[root@localhost ~]# mkdir /share


在/share文件上,我们也需要做一些操作,以便于他可以正常的被访问和使用

[root@localhost ~]# touch /share/c-test
[root@localhost ~]# chmod -R 777 /share/c-test 【-R 递归给予777权限】
[root@localhost ~]# chown samba01:samba01 /share/c-test -R    【-R 递归修改文件及目录拥有人拥有组】
[root@localhost ~]# ll /share/c-test
-rwxrwxrwx. 1 samba01 samba01 0 Oct  8 09:53 /share/c-test


关闭防火墙或者放行服务

关闭

[root@localhost samba]# systemctl stop firewalld
[root@localhost samba]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

放行

[root@localhost samba]# firewall-cmd --add-service=samba --permanent
success
[root@localhost samba]# firewall-cmd --reload
success

关闭Selinux,或者修改文件标签

关闭

[root@localhost ~]# setenforce 0


修改标签

[root@localhost ~]# chcon -t samba_share_t /share/c-test  -R

重启samba服务

[root@localhost ~]# systemctl restart smb
[root@localhost ~]# systemctl enable --now smb
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.

OpenEuler

配置软件仓库

[dvd]
name=dvd
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0

挂载

[root@localhost yum.repos.d]# echo "/dev/cdrom /mnt/cdrom   iso9660   defaults 0 0" >> /etc/fstab

安装软件包

[root@localhost ~]# dnf install samba-client cifs-utils -y

配置挂载

[root@localhost ~]# mount -t cifs //192.168.96.250/public  /mnt/smbshare  -o username=samba01
🔐 Password for samba01@//192.168.96.250/public:  ******

查看是否挂载成功

[root@localhost ~]# df
Filesystem                 1K-blocks     Used Available Use% Mounted on
devtmpfs                        4096        0      4096   0% /dev
tmpfs                        1728540        0   1728540   0% /dev/shm
tmpfs                         691416     9108    682308   2% /run
tmpfs                           4096        0      4096   0% /sys/fs/cgroup
/dev/mapper/openeuler-root  65617452  1419416  60832356   3% /
tmpfs                        1728540        0   1728540   0% /tmp
/dev/sda1                     996780   154084    773884  17% /boot
/dev/mapper/openeuler-home  32009000       40  30357456   1% /home
/dev/sr0                    17965626 17965626         0 100% /mnt/cdrom
//192.168.96.250/public    101684728  3345176  98339552   4% /mnt/smbshare

到这里,samba的服务就全部完毕了,那么我们该如何在这之中,详细规划samba01用户可以创建多少文件,使用多少空间呢?

这里便要引入磁盘配额的概念

磁盘配额

centos上添加一块磁盘

[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk
sr0          11:0    1 10.9G  0 rom  /mnt/cdrom
nvme0n1     259:0    0  100G  0 disk
├─nvme0n1p1 259:1    0  500M  0 part /boot/efi
├─nvme0n1p2 259:2    0  500M  0 part /boot
├─nvme0n1p3 259:3    0    2G  0 part [SWAP]
└─nvme0n1p4 259:4    0   97G  0 part /

格式化磁盘

[root@localhost ~]# mkfs.xfs /dev/sda1 -f
meta-data=/dev/sda1              isize=512    agcount=4, agsize=1310656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=5242624, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

配置磁盘挂载

[root@localhost ~]# echo "/dev/sda1   /share  auto defaults 0 0" >> /etc/fstab
[root@localhost ~]# mount -a


查看

[root@localhost ~]# df -Th | grep /dev/sda1
/dev/sda1      xfs        20G  175M   20G   1% /share

由于此处我们将/share目录挂载了,刚刚创建的c-test则会消失,但并不影响使用,当你取消挂载的时候,他又会回来,只是相当于隐藏了而已。

xfs配额允许管理员控制用户或者组对文件系统空间使用,而配额限制也分为“软限制”和“硬限制”。

  • 软限制(soft):警告用户超出配额,但允许他们继续写入直到硬限制。
  • 硬限制(hard):严格限制用户空间使用,超出后无法写入。

启用配额

查看磁盘是否支持配额

[root@localhost ~]# xfs_info /dev/sda1
meta-data=/dev/sda1              isize=512    agcount=4, agsize=1310656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=5242624, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

命令格式

xfs_quota -x -c "CMND" [挂载点]

选项/指令含义
-x专家模式,后面可以加-c来执行命令
-c与-x同时使用
print单纯的列出主机内的文件系统参数等信息
df与原本的df功能一样,可以加上-b(block)-i(inode)-h(单位humanreadable)等
[root@localhost ~]# xfs_quota -x -c "print"   /dev/sda1
Filesystem          Pathname
/share              /dev/sda1

[root@localhost ~]# xfs_quota -x -c "df -h" /share
Filesystem     Size   Used  Avail Use% Pathname
/dev/sda1     20.0G 175.0M  19.8G   1% /share


限制值的设置方式

# quota限定的设置语法
# block和inode的限定
xfs_quota -x -c "limit [-ug] b[soft|hard]=N i[soft|hard]=M name" 
# grace time的限定
xfs_quota -x- c "timer [-ug] [-bir] Ndays"


limit:可以针对user/group来限制,限制的项目有
    bsoft/bhard:**block**的soft与hard值,可以加单位,M,G等
    isfot/ihard:**inode**的soft与hard值
    name:用户/群组的名称
timer:用来设置grace time,同上,也是可以针对user/group,以及block/inode来设定


这里的block 和 inode 是什么?

block 是块 一个block的大小是4k 【1k=1024字节】所以,此处是设置使用大小

[root@localhost ~]# stat  file1
  File: file1
  Size: 7               **Blocks: 8 **         **IO Block: 4096 **  regular file
Device: 10304h/66308d   Inode: 135118296   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2024-10-08 11:10:46.343302865 +0800
Modify: 2024-10-08 11:10:46.343302865 +0800
Change: 2024-10-08 11:10:46.343302865 +0800
 Birth: 2024-10-08 11:10:46.343302865 +0800

inode是编号,每一个文件和目录在本质上,系统读取的都是inode号,而名字都是给人看的。

[root@localhost ~]# ll file1 -i
135118296 -rw-r--r--. 1 root root 7 Oct  8 11:10 file1
[root@localhost ~]# ll file2 -i
135118297 -rw-r--r--. 1 root root 0 Oct  8 11:11 file2
[root@localhost ~]# ll file3 -i
135118298 -rw-r--r--. 1 root root 0 Oct  8 11:12 file3

在开始配置xfs配额之前,还有至关重要的一步,在/etc/fstab中添加usrquota和grpquota

/dev/sda1   /share  auto defaults,usrquota,grpquota 0 0

由于修改了配置文件,我们将重新挂载一次,以便于修改内容生效

[root@localhost ~]# systemctl stop smb
[root@localhost ~]# fuser /share
[root@localhost ~]# umount /share
[root@localhost ~]# mount -a
[root@localhost ~]# systemctl start  smb


给samba01 一个空间限制

[root@localhost ~]# xfs_quota -x -c "limit -u bsoft=250M bhard=300M samba01"  /share

创建一个275M的文件

[root@localhost smbshare]# dd if=/dev/zero  of=./file1 bs=275M count=1
1+0 records in
1+0 records out
288358400 bytes (288 MB, 275 MiB) copied, 1.63458 s, 176 MB/s
[root@localhost smbshare]# ll file1
-rwxr-xr-x. 1 root root 288358400 Oct  8 11:21 file1
[root@localhost smbshare]# ll file1  -h
-rwxr-xr-x. 1 root root 275M Oct  8 11:21 file1

我们发现他可以创建,因为他只是超过了软限制,还没有达到硬限制的标准

创建一个375m的文件

[root@localhost smbshare]# dd if=/dev/zero  of=./file1 bs=375M count=1
dd: closing output file './file1': No space left on device

他给我们提示,没有空间,为什么会没有空间呢,我磁盘给的是20G啊

因为375M已经超过硬限制的标准了,xfs配额限制了他的大小

给samba01 inode号限制

[root@localhost ~]# xfs_quota -x -c "limit -u isoft=25 ihard=30 samba01"  /share

创建27个文件

[root@localhost smbshare]# touch file{1..27}
[root@localhost smbshare]# ls
file1   file11  file13  file15  file17  file19  file20  file22  file24  file26  file3  file5  file7  file9
file10  file12  file14  file16  file18  file2   file21  file23  file25  file27  file4  file6  file8

我们发现可以创建,因为没有超过既定的软限制

创建35个文件

[root@localhost smbshare]# touch file{1..35}
touch: cannot touch 'file30': No space left on device
touch: cannot touch 'file31': No space left on device
touch: cannot touch 'file32': No space left on device
touch: cannot touch 'file33': No space left on device
touch: cannot touch 'file34': No space left on device
touch: cannot touch 'file35': No space left on device
[root@localhost smbshare]# ls
file1   file11  file13  file15  file17  file19  file20  file22  file24  file26  file28  file3  file5  file7  file9
file10  file12  file14  file16  file18  file2   file21  file23  file25  file27  file29  file4  file6  file8

可以看到从30开始,后面的文件都没有空间了,但实际上空间还有非常多,因为inode号被分配完了,所以空间就没有了。

查看限制

[root@localhost ~]# xfs_quota -x -c "quota -bi -uv  samba01" /share
Disk quotas for User samba01 (1000)
Filesystem              Blocks      Quota      Limit  Warn/Time            Files      Quota      Limit  Warn/Time      Mounted on
/dev/sda1                    0     256000     307200   00 [--------]           1         25         30   00 [--------] /share

移除限制

[root@localhost ~]# xfs_quota -x -c "off -up" /share   #完全关闭quota功能,只有重新挂载才可开启

移除后,samba01便可以随意创建文件了
[root@localhost smbshare]# touch file100
[root@localhost smbshare]# dd if=/dev/zero  of=./file1 bs=375M count=1
1+0 records in
1+0 records out
393216000 bytes (393 MB, 375 MiB) copied, 0.754881 s, 521 MB/s

posted @ 2024-10-08 12:09  super派大星  阅读(48)  评论(0)    收藏  举报  来源