在Linux系统下限制指定目录的大小以及文件/文件夹数量

背景说明

在Linux操作系统下有时需要限制一个指定文件夹的大小和文件夹内可存储的文件数量,有可能是出于安全的考量或者定制化的配置,这里我们提供了一种方案:用dd创建一个空的img镜像,进行格式化的配置,然后将其绑定到指定的文件夹上可以限制该文件夹的一些属性。

分配空置的img镜像

通过dd指令可以分配一个全为0的10M大小的img镜像:

[dechin-manjaro limits-test]# dd if=/dev/zero of=test.img bs=10M count=1
记录了1+0 的读入
记录了1+0 的写出
10485760字节(10 MB,10 MiB)已复制,0.0390058 s,269 MB/s
[dechin-manjaro limits-test]# ll
总用量 10240
-rw-r--r-- 1 root root 10485760  1月 11 17:11 test.img

注:如果这里使用/dev/urandom的话产生的img镜像则是一个随机的块,可用于模拟一个存储满各种数据资源的磁盘。

绑定一个循环分区

首先查看可用的循环分区:

[dechin-manjaro limits-test]# losetup -f
/dev/loop0

我们发现可用的是loop0这个分区,然后用该分区来初始化img镜像:

[dechin-manjaro limits-test]# losetup /dev/loop0 test.img               
[dechin-manjaro limits-test]# mkfs.ext4 /dev/loop0 -N 5
创建含有 10240 个块(每块 1k)和 16 个inode的文件系统
文件系统UUID:7448390b-a205-41bd-bdc7-42fbf29c00ec
超级块的备份存储于下列块: 
        8193

正在分配组表: 完成                            
正在写入inode表: 完成                            
创建日志(1024 个块)完成
写入超级块和文件系统账户统计信息: 已完成
[dechin-manjaro limits-test]# losetup -d /dev/loop0

创建一个目录并绑定已创建镜像

[dechin-manjaro limits-test]# mkdir test-dir
[dechin-manjaro limits-test]# mount -o loop test.img test-dir/

查看与测试配置生效情况

执行命令以查看该绑定目录的挂载情况,可发现该目录已挂载在/dev/loop0下:

[dechin-manjaro test-dir]# cd test-dir/
[dechin-manjaro test-dir]# df -h
文件系统        容量  已用  可用 已用% 挂载点
dev              20G     0   20G    0% /dev
run              20G  1.6M   20G    1% /run
/dev/nvme0n1p9  144G   41G   95G   31% /
tmpfs            20G     0   20G    0% /dev/shm
tmpfs           4.0M     0  4.0M    0% /sys/fs/cgroup
tmpfs            20G   69M   20G    1% /tmp
/dev/nvme0n1p1  300M   31M  269M   11% /boot/efi
tmpfs           3.9G  104K  3.9G    1% /run/user/1000
/dev/loop0      9.0M  172K  8.2M    3% /home/dechin/projects/2021-security/limits-test/test-dir
[dechin-manjaro test-dir]# df -i
文件系统       Inodes 已用(I) 可用(I) 已用(I)% 挂载点
dev              4.9M     545    4.9M       1% /dev
run              4.9M     949    4.9M       1% /run
/dev/nvme0n1p9   9.2M    833K    8.4M       9% /
tmpfs            4.9M       1    4.9M       1% /dev/shm
tmpfs            1.0K      18    1006       2% /sys/fs/cgroup
tmpfs            400K     199    400K       1% /tmp
/dev/nvme0n1p1      0       0       0        - /boot/efi
tmpfs            998K      92    998K       1% /run/user/1000
/dev/loop0         16      11       5      69% /home/dechin/projects/2021-security/limits-test/test-dir

从以上执行结果,我们还可以看到这个目录还有剩余9M的可用空间以及5个的可用文件innode数量。这里的innode数量是Linux系统对于文件和文件夹的一个标识符号,每一个文件或者文件夹都有这个标识,如果只给这个挂载盘分配5个可用的innode,这表示在该目录下最多只能存在5个的文件或者文件夹,这里让我们用实际的案例来测试一下:

[dechin-manjaro test-dir]# touch 1
[dechin-manjaro test-dir]# touch 2
[dechin-manjaro test-dir]# touch 3
[dechin-manjaro test-dir]# touch 4
[dechin-manjaro test-dir]# touch 5
[dechin-manjaro test-dir]# touch 6
touch: 无法创建 '6': 设备上没有空间

在上面这个测试中我们发现,对于innode数量的限制已经生效,接下来把这些文件都删除后,测试一下文件夹大小的限制:

[dechin-manjaro test-dir]# dd if=/dev/urandom of=test.img bs=10M count=1
dd: 写入 'test.img' 出错: 设备上没有空间
记录了1+0 的读入
记录了0+0 的写出
9039872字节(9.0 MB,8.6 MiB)已复制,0.289899 s,31.2 MB/s
[dechin-manjaro test-dir]# ll
总用量 8841
drwx------ 2 root root   12288  1月 11 17:24 lost+found
-rw-r--r-- 1 root root 9039872  1月 11 17:54 test.img

这里我们尝试去创建一个10M的文件,但是由于目录本身的限制,以及lost+found这个目录已经占用了一部分的资源,因此最终只有9M的文件大小能够分配成功。到这里为止,我们的测试就已经结束了,经过验证配置是完全生效的。

取消该文件的挂载

首先返回到绑定文件夹所在的目录层,然后执行umount指令:

[dechin-manjaro limits-test]# ll
总用量 9969
drwxr-xr-x 3 root root     1024  1月 11 17:54 test-dir
-rw-r--r-- 1 root root 10485760  1月 11 17:54 test.img
[dechin-manjaro limits-test]# umount test-dir/
[dechin-manjaro limits-test]# df -h
文件系统        容量  已用  可用 已用% 挂载点
dev              20G     0   20G    0% /dev
run              20G  1.6M   20G    1% /run
/dev/nvme0n1p9  144G   41G   95G   31% /
tmpfs            20G     0   20G    0% /dev/shm
tmpfs           4.0M     0  4.0M    0% /sys/fs/cgroup
tmpfs            20G   69M   20G    1% /tmp
/dev/nvme0n1p1  300M   31M  269M   11% /boot/efi
tmpfs           3.9G  104K  3.9G    1% /run/user/1000

umount之后,我们发现在挂载目录中的test-dir已经消失,这表示取消挂载成功。

总结概要

通过以上的方案可以很好的定制化一个特殊的目录,更好的用于管理和规范一个用于执行任务的文件系统,避免资源的恶意占用等问题。

版权声明

本文首发链接为:https://www.cnblogs.com/dechinphy/p/limits.html
作者ID:DechinPhy
更多原著文章请参考:https://www.cnblogs.com/dechinphy/

posted @ 2021-01-11 18:01  DECHIN  阅读(7757)  评论(0编辑  收藏  举报