How big should /tmp be?

I have in /etc/fstab of my pxe client:

tmpfs /tmp tmpfs rw,mode=1777,size=128m 0 0
tmpfs /var/run tmpfs rw,mode=0755,size=20m 0 0

The second only to avoid booting errors due to locking.

But the small /tmp breaks pkg. I had to run:

mkdir /var/tmp/pkgtmp
env TMPDIR=/var/tmp/pkgtmp pkg update -f
env TMPDIR=/var/tmp/pkgtmp pkg ...
 
I generally tmpfs /tmp but the advice is not to do it for /var/tmp (even though its a symlink on some platform).

I originally read this on a NetBSD and Gentoo handbook but its echoed by the ArchLinux wiki:

Do not use it on /var/tmp, because that directory is meant for temporary files that are preserved across reboots.

As for /tmp, do you need to set a size? At least on OpenBSD (mfs), I can set it to 20G and it only uses memory as needed (or fails if too large).
 
Sounds like an interesting corner case. tmpfs causing in memory filesystem. sounds like the size parameter is the upper limit, so sparse allocation instead of preallocation of the full amount.
What amount of memory do the pxe clients have? it may be a good data point to not set sizes, and see how much is used to get you rough upper limits.
 
I just have "tmpmfs=YES' in /etc/rc.conf and leave it at that. Then again, it only ever has a bunch of small files in that directory, I never write anything large into it, so I haven't stress-tested it. The manpage for mdmfs doesn't state a default size for the '-s' argument if it's not specified.
 
I just tested it. By default the size is 20 megabytes, specifically 20480 1k-blocks, so it's not very large. And it doesn't expand if you write a large file to it, it fills up and you get a no space left error.
 
I tried setting 'tmpmfs=yes' and 'tmpsize=4g', and rebooted. Then df reports 4 gigabytes available in /tmp, however htop shows only 374 MB memory actually used, so it's space-efficient. If I then copy a 3.5 GB file to /tmp, df shows /tmp is almost full, and htop shows 3.6 GB memory used. So it appears that it doesn't allocate the memory from the kernel until you write to it.

Subsequently deleting the file does not result in a corresponding drop in how much top thinks is in use, however. But hopefully the driver is intelligent enough to mark that memory as available for the kerne to grab at some later time. It would be unfortunate if writing a single large file to /tmp, then subsequently deleting it, results in a large amount of memory permanently allocated to the mfs driver and never released back to the kernel.

So yes, the tmpsize parameter in rc.conf specifies the maximum size it is allowed to grow to, but by default it only uses as much memory as is needed to accomodate the files that are written into /tmp; it allocates memory on demand from the kernel as files are written to it. The default maximum size that you get if you don't specify 'tmpsize' is 20 MB. At least, that's what I observe on my 14.5R system here this evening.
 
Sounds like an interesting corner case. tmpfs causing in memory filesystem. sounds like the size parameter is the upper limit, so sparse allocation instead of preallocation of the full amount.
What amount of memory do the pxe clients have? it may be a good data point to not set sizes, and see how much is used to get you rough upper limits.

This filesystem has its file contents paged to swapspace. So you can make it quite big without taking too much RAM, assuming you have sufficient swapspace.
 
  • Like
Reactions: mer
Back
Top