2018年10月18日 星期四

[Linux 常見問題] How to set a file size limit for a directory?

Source From Here 
Question 
I have a directory on my system which is used for a specific reason by applications and users, but I don't want its size to be allowed to exceed 2GB, is there a way of setting up some sort of limit which just doesn't allow the file size to exceed that or any other amount I decide to set for it in the future? When the size limit is exceeded it should undo the last change (though there should be an option to have it so that it just stops the operation and doesn't care if half a file was copied and left there) and then display a warning to the user. 

How-To 
Usual filesystem quota on ext4 is per-user/group, not per-directory. ZFS can sort-of set a directory quota, by creating a filesystem of a fixed size off a ZFS volume. A simple trick, though, is to create a 2GB file, create a filesystem on it, and mount it at the desired folder: 
$ touch 2gbarea 
$ truncate -s 2G 2gbarea 
$ mke2fs -t ext4 -F 2gbarea 
mke2fs 1.43.3 (04-Sep-2016) 
Discarding device blocks: done 
Creating filesystem with 524288 4k blocks and 131072 inodes 
Filesystem UUID: bf1b2ee8-a7df-4a57-9d05-a8b60323e2bf 
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912 

Allocating group tables: done 
Writing inode tables: done 
Creating journal (16384 blocks): done 
Writing superblocks and filesystem accounting information: done 


$ sudo mount 2gbarea up 
$ df -h up 
Filesystem Size Used Avail Use% Mounted on 
/dev/loop0 2.0G 6.0M 1.8G 1% /home/muru/up

In any case, filesystem quotas (or methods like this) aren't as user friendly as you want. This method is one-way flexible, in that you can increase the size online, but decreasing it would be hard. 

The commands: 
* touch: touch 2gbarea creates an empty file named 2gbarea. 
* truncate: truncate is used to resize files (in this case, I resize the currently empty 2gbarea file to 2 GB using -s 2G). 
* mke2fs: mke2fs creates ext2/3/4 filesystems (in this case, ext4). 
* mount: mounts the filesystem on the given directory. 
* df is used to list filesystem usage.


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...