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.

  • 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 Aug 7 '16 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 Aug 8 '16 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 Aug 8 '16 at 7:49

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
  • Could you tell me please how to automatically create the directory structure on the tmpfs partition when it is mounted during startup? – Alexey Aug 9 '16 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. – Gilles Aug 9 '16 at 7:41

/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.

  • 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 Aug 8 '16 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 Aug 8 '16 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 Aug 8 '16 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.