4

Is there any trick to limit only the total size of all mounted tmpfs file systems (without limiting them individually)?

I consider using tmpfs for /tmp, /var/lock, and /var/run, and I only care about restricting the total limit of the virtual memory used for all, I do not find it reasonable to restrict the size of each directory individually.

3
  • If we only consider those three (or any limited set), you could just mount one tmpfs and either symlink or bind-mount the three directories to it.
    – ilkkachu
    Commented Aug 7, 2016 at 18:38
  • Thanks, this is approximately what i am looking for, but i need more details: how is it possible to link different directories to the same partition so that their contents stay separate?
    – Alexey
    Commented Aug 8, 2016 at 7:45
  • Did you mean creating /tmp, /var/run, /var/lock on a single tmpfs? Could you suggest then, please, where i should mount the tmpfs itself, and how to create this directory hierarchy in it automatically every time?
    – Alexey
    Commented Aug 8, 2016 at 7:49

2 Answers 2

3

Tmpfs filesystems are independent. There's no feature to limit the size of a set of tmpfs filesystems.

Tmpfs filesystems are interchangeable apart from the memory limitation, so if you want to have a combined limits, you can just combine the filesystems. Make symbolic links once and for all:

ln -s var/run/tmp /tmp
ln -s ../run/lock /var/lock

When you mount the filesystem, immediately create the required subdirectories.

mount -t tmpfs tmpfs /var/run
mkdir -m 1777 /var/run/tmp /var/run/lock

I've created the subdirectories directly inside /var/run because that directory normally has tame file names and is only accessible to root. Do not create subdirectories inside a world-writable directory: a user could preempt you and create a directory they can write to, which would result in them owning /var/run and so being able to create and remove files there.

mount -t tmpfs mode=1777 tmpfs /tmp
mkdir /tmp/run
2
  • Could you tell me please how to automatically create the directory structure on the tmpfs partition when it is mounted during startup?
    – Alexey
    Commented Aug 9, 2016 at 7:07
  • @Alexey Run the mkdir command(s) immediately after the mount command. Or, if you rely on your distribution/systemd to do the mounting, run a service to make the mkdir calls immediately after the mounting service runs. How to do this depends on your distribution and your init system. If you can't figure it out, ask a new question about it and be sure to say what distribution and what init system you're using. Commented Aug 9, 2016 at 7:41
0

/var/lock and /var/run should be of minimal size and there should be little advantage is limiting their size.

/tmp is the only one that is likely to be of any size. If you make its size to small, you may encounter unexpected errors.

Is there any reason you feel the need to limit their size? If any of these get too big, their content will likely be paged out to swap.

3
  • Could you provide any references or explanations of how you know that applications are using only a very small amount of /var/run, please? In any case, i see no reason to limit those sizes individually if i only care about the total size.
    – Alexey
    Commented Aug 8, 2016 at 8:06
  • Taking into account that some applications may store a whole DVD image or a downloaded movie in /tmp, i feel that even using the swap it is better to limit the maximal amount of memory used.
    – Alexey
    Commented Aug 8, 2016 at 8:08
  • @Alexey Years of experience and the contents of man hier tells me that /var/run and /var/lock will always be small. If you are worried about the memory consumed by tmp will be too much you can move it to disk, but you are likely better off increasing swap or using quotas if space becomes an issue. If applications can't store files in memory, they are likely to fail or hang trying to write to /tmp. Hangs may quickly lead to the issues you are trying to avoid.
    – BillThor
    Commented Aug 8, 2016 at 13:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .