Linux limit folder size

Limit total size of a directory in linux

I have a service daemon that creates a lot of temp files. Recently my server died, because a malicious user managed to flood /tmp and fill up the disk. I have taken some measures to actively clean up the temp dir, but additionally I would like to constrain the max size of this applications temp dir. Is there any way I can create dir, say, /apptmp that will never be larger than e.g. 10G? I know I can set disk limits by-user, but I just want to limit this tmpdir; the application should always be able to write elsewhere. I am running Ubuntu Linux 12.04. edit: All of this should eventually be wrapped up in an installable Ubuntu package though. So I don’t think I want to rely on modifying the partitions, unless I can somehow simulate it.

2 Answers 2

You can give /tmp it’s own partition. Then you will be sure that it will never exceed that amount. I suggest using LVM so you can increase and decrease partition size should you ever feel the need to do so.

Is was the reason in the «old days» that you had a separate volume/partition for /var; it kept logs from causing crashes.

Thanks. My experience with LVM is very limited. Could you illustrate your answer a bit with some example code on how to create such a partition? Could it be a virtual partition (in a file) as suggested below?

+1 You should have separate partitions for all the usual locations, such as /, /usr, /var, /tmp, /opt, /home. See also debian.org/doc/manuals/securing-debian-howto/ch3.en.html#s3.2

Источник

Простое квотирование директорий в Linux

Необходимость квотирования отдельно взятых директорий в Linux, на мой взгляд, очевидна — зачастую бывает потребность в ограничении объёма пронстранства для хранения чего-либо, которая не основывается на методе определения порогов квоты для пользователей или групп, а на общем объёме самой директории. К счастью, у нас уже имеются под руками все необходимые средства для реализации этой нетривиальной, но подчас очень востребованной задачи предложенным ниже способом.

Предположим, имеется некая группа пользователей, которая использует ресурсы офисного сервера в том числе и для хранения общих файлов для совместной, коллективной работы. Проблема состоит в том, что зачастую, ресурсы общего доступа для хранения рабочих файлов могут использоваться не по назначению, например, для информации частного или даже развлекательного характера, например, видео или аудио файлы, которые как правило имеют большой объём. Если не предпринимать никаких мер, то в скором времени пространство диска на котором хранится информация самого общего ресурса будет исчерпано, что может вызвать ряд определённых неудобств или даже стать причиной отказа функционирования некоторых служб.

Читайте также:  Резервная копия раздела linux

Одно из решений этой проблемы заключается в создании образа файловой системы для хранения нужной нам информации и последующего его подключения в точку монтирования. Преимущество данного метода в отличии от LVM или ряда других методов состоит в быстром решении и простоте использования. Давайте убедимся в этом сами:

Для начала, нам нужно создать каталог для точки монтирования нашего образа ФС.

dd if=/dev/zero of=/mnt/quota.img bs=1024 count=100000
mount -o loop /mnt/quota.img /mnt/quotadir

Всё! Более ничего не требуется! Мы осуществили всё, что требовалось.

И, наконец, если имеется необходимость в постоянном монтировании созданного нами образа, это необходимо сделать в файле /etc/fstab

/mnt/quota.img /mnt/quota ext3 loop 1 2

Не смотря на то, что файловая система на которой хранится нужная нам информация заключается а файле-образе, тем не менее, это всё-таки файловая система, над которой мы можем совершать вполне привычные действия, например, такие как:

Увеличение или уменьшение объёма ФС, тем самым изменяя объём самого хранилища

resize2fs -p quota.img 1000M

Источник

How to set a file size limit for a directory?

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. I am running Ubuntu GNOME 16.10 with GNOME 3.22.

1 Answer 1

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.

  • 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.
Читайте также:  Utf 8 linux and windows

Источник

How to set limit on directory size in Linux? [closed]

I have read about limiting size of directory — like creating big files, formatting,mount. etc. But this all very complicated. Does exist utility or something else to set limit on already existing directory?

The problem that I need limit to specific directories. There are many users that have access to e.g. direcotry1, directory2, directory3. I need set limit for log dir, for data dir, for applications dir.

Based on the accepted answer and the linked tutorial, I’ve put together a script to automate the process, which is actually was made for a related answer: askubuntu.com/a/1043139/295286

2 Answers 2

Quota is based upon filesystems, but you can always create a virtual filesystem and mount it on a specific (empty) directory with the usrquota and/or grpquota flags.

  1. create the mount point
  2. create a file full of /dev/zero, large enough to the maximum size you want to reserve for the virtual filesystem
  3. format this file with an ext3 filesystem (you can format a disk space even if it is not a block device, but double check the syntax of every — dangerous — formatting command)
  4. mount the newly formatted disk space in the directory you’ve created as mount point, e.g. Code: mount -o loop,rw,usrquota,grpquota /path/to/the/formatted/disk/space /path/of/mount/point
  5. Set proper permissions
  6. Set quotas and the trick is done.

Tutorial here. Original answer here

Источник

Directory size limit [closed]

Questions must demonstrate a minimal understanding of the problem being solved. Try including attempted solutions, why they didn’t work, and the expected results. See How can I ask better questions on Server Fault? for further guidance.

Читайте также:  Can linux read text files

In Linux, it’s possible to limit the size of a partition, for example with quota. But does a way to limit directory size exist? For example, I have a directory, let say /mnt/foo/bar , which is not a partition(e.g. not in df result). Is it possible? No mkfs possible on this directory.

2 Answers 2

Not directly, you could create a file of the size that you want to limit the directory to and then mount it using a loop mount into the tree.

  • Create a file to use as a filesystem e.g.
    • fallocate -l 100M disk.img ( this isn’t always compatible with an underlying FS.)
    • dd if=/dev/zero of=disk.img count=512k
    • mkfs disk.img
    • mount -o loop disk.img /path/to/mount/point (optionally you can enable quotas too)

    What you are asking for would be a nice feature, but I am not aware of any file system with such a feature. The best approximation without adding more file systems would be to use quotas, which limit storage space per user (or group).

    One problem with doing it per directory is that the semantics are not going to be obvious, once you consider the possibility that a file may be hardlinked.

    Consider the following sequence of events:

    • Administrator configure a system with 1GB quota per home directory
    • user1 run a program, which creates a logfile in ~user1
    • When the logfile is 1MB large user2 creates a hardlink in ~user2
    • user2 adds 900MB of other files to ~user2
    • user1 appends 900MB of data to the logfile
    • user1 deletes the logfile from ~user1

    There is not any one single of the above operations, you would expect to fail due to quota limits. Yet the outcome is that user2 is way above quota in the end.

    Источник

Оцените статью
Adblock
detector