Most active commenters
  • AdieuToLogic(6)
  • frollogaston(5)
  • hiAndrewQuinn(3)
  • quotemstr(3)
  • pkulak(3)
  • buckle8017(3)

←back to thread

62 points hiAndrewQuinn | 40 comments | | HN request time: 0.43s | source | bottom
1. hackyhacky ◴[] No.44392515[source]
Rather than re-write your scripts to store temp files into /dev/shm, you can just mount /tmp using the tmpfs file system and get the same benefit for all your programs. Some distros do this by default.

The relevant line from fstab is:

    tmpfs /tmp            tmpfs    noatime 0       2
Now any program that writes to /tmp will be writing to a RAM disk, thus sparing unnecessary wear on my SSD.
replies(7): >>44392526 #>>44392690 #>>44392745 #>>44392789 #>>44392847 #>>44393129 #>>44393836 #
2. hiAndrewQuinn ◴[] No.44392526[source]
I do mention this offhand in the article: "The existence of /dev/shm is a boon for me mostly because it means I never have to worry about whether /tmp is really RAM-based again."
replies(2): >>44392561 #>>44392655 #
3. quotemstr ◴[] No.44392561[source]
Now you have to worry about whether you can access /dev/shm. Please encourage people to use supported interfaces instead of random voodoo (anything under /dev that wasn't there in 1995) for day-to-day tasks.
replies(2): >>44392587 #>>44392713 #
4. hiAndrewQuinn ◴[] No.44392587{3}[source]
/dev/shm is typically world-writable by default:

    $ ls -ld /dev/shm
    drwxrwxrwt 3 root root 120 Jun 32 02:47 /dev/shm/
Incidentally, "30 years ago" is the cutoff date for music being considered the oldies. This just made me realize Nevermind is now an oldie, and soon The Lonesome Crowded West will be too.
replies(3): >>44392630 #>>44392642 #>>44392682 #
5. quotemstr ◴[] No.44392630{4}[source]
> /dev/shm is typically world-writable by default:

You are relying on random implementation details instead of universal APIs that work across OSes and environments. Please stop.

So help me God, if I make a Linux system, I will make it _not_ have a /dev/shm just to avoid people relying on non-standard stuff for no good reason. Honestly, it's because of stuff like this that we need Docker.

replies(3): >>44392650 #>>44392659 #>>44392663 #
6. chaps ◴[] No.44392642{4}[source]
A past role in a past life had me installing security services on servers. One server had incredibly awkward permission sets across its common directories so our deployment script failed. The fix? Just throw it into /dev/shm and install it directly from there. It worked great.
7. ◴[] No.44392650{5}[source]
8. frollogaston ◴[] No.44392655[source]
"virtually every Unix system already has it mounted as a tmpfs by default" might be true if you say Linux instead, but Mac doesn't have /dev/shm
replies(3): >>44392705 #>>44392793 #>>44392859 #
9. half-kh-hacker ◴[] No.44392659{5}[source]
file-hierarchy(7) states /dev/shm is tmpfs and that "all users have write access to this directory", so I think you'd have to be making a non-systemd distro
replies(1): >>44392718 #
10. frollogaston ◴[] No.44392663{5}[source]
/tmp isn't a standard place for RAM disk either, all it says is: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s18.htm...

I'm not really seeing a right or wrong here anyway unless you're distributing a script that's meant to run on all sorts of Linux systems. In which case you probably aren't concerned with the physical storage medium being used.

replies(1): >>44392723 #
11. throwaway992673 ◴[] No.44392682{4}[source]
"And it's been a long time, which agrees with this watch of mine"
12. chrisdeso ◴[] No.44392690[source]
This is the first linux "thing" I've understood after a first read on hacker news. Love you all and will give this a whirl.
13. loeg ◴[] No.44392705{3}[source]
I may misremember, but I think it's also common in the BSDs? (Whereas /var/tmp is persisted.)
replies(1): >>44400967 #
14. wredcoll ◴[] No.44392713{3}[source]
This is a ridiculous comment but it did make me curious, when did /dev/shm become a common thing?

My current understanding is kernel 2.6, i.e. 2004.

replies(1): >>44392749 #
15. hackyhacky ◴[] No.44392718{6}[source]
It also says:

> Usually, it is a better idea to use memory mapped files in /run/ (for system programs) or $XDG_RUNTIME_DIR (for user programs) instead of POSIX shared memory segments, since these directories are not world-writable and hence not vulnerable to security-sensitive name clashes.

$XDG_RUNTIME_DIR usually points to /run/user/${uid}, so you're guaranteed that other users won't write there, and possibly won't even be able to read there.

16. quotemstr ◴[] No.44392723{6}[source]
/tmp is literally POSIX:

https://pubs.opengroup.org/onlinepubs/9799919799/

It doesn't get more standard than that.

It's because of people doing random nonstandard shit that we need to Docker-ize a lot of software these days. People refuse to lift a single finger to adhere to conventions that let programs co-exist without simulating a whole god damn computational universe for each damn program.

replies(3): >>44392751 #>>44392770 #>>44392902 #
17. pkulak ◴[] No.44392745[source]
I did this for a while, but writing files to ram can be dangerous, since most things assume unlimited disk space. I noticed that updates would fail on machines that had 16 gigs of ram unless I logged out of my window manager and did it from the TTY. Took quite a long time to realize it was because of all the compiles writing to /tmp. Much easier to just let the SSD get used.
replies(1): >>44392802 #
18. esseph ◴[] No.44392749{4}[source]
2.4 in 2001 is when it was released with kernel support
19. frollogaston ◴[] No.44392751{7}[source]
I meant the author specifically wants to write files to RAM and nowhere else. There isn't a standard place for that.
20. gyesxnuibh ◴[] No.44392770{7}[source]
> /tmp A directory made available for applications that need a place to create temporary files. Applications shall be allowed to create files in this directory, but shall not assume that such files are preserved between invocations of the application.

It doesn't say anything about what it's backed by.

21. frollogaston ◴[] No.44392789[source]
If I already have /tmp and it's not tmpfs, honestly I'm not gonna bother remapping it.
22. hiAndrewQuinn ◴[] No.44392793{3}[source]
Mea culpa, you're right. I should not have assumed that just because POSIX was mentioned in the orbit of this thing that everyone else had this too.

The article has been corrected.

23. buckle8017 ◴[] No.44392802[source]
This is why having swap even when you have plenty of memory for normal usage is good.

Swap on an SSD isn't even that slow.

replies(2): >>44392866 #>>44392886 #
24. bbarnett ◴[] No.44392847[source]
A decade ago yes, but these days, SSD wear isn't an issue for desktop users.
25. AdieuToLogic ◴[] No.44392859{3}[source]
OS-X/macOS supports RAM drives and a script which defines one for use as /private/tmp (which /tmp is symbolically linked to) is:

  #!/bin/bash
  ramfs_size_mb=1024
  mount_point=/private/tmp
  
  counter=0
  ramfs_size_sectors=$((${ramfs_size_mb}*2048))
  ramdisk_dev=`hdiutil attach -nomount ram://${ramfs_size_sectors}`
  
  while [[ ! -d "/Volumes" ]]
  do
   sleep 1
   counter=$((counter + 1))
  
   if [[ $counter -gt 10 ]]
   then
    echo "$O: /Volumes never created"
    exit 1
   fi
  done
  
  diskutil eraseVolume HFS+ 'RAM Disk' ${ramdisk_dev} || {
   echo "$O: unable to create RAM Disk on: ${ramdisk_dev}"
   exit 2
  }
  
  umount '/Volumes/RAM Disk'
  
  mkdir -p ${mount_point} 2>/dev/null
  mount -o noatime -t hfs ${ramdisk_dev} ${mount_point} || {
   echo "$0: unable to mount ${ramdisk_dev} ${mount_point}"
   exit 3
  }
  
  chown root:wheel ${mount_point}
  chmod 1777 ${mount_point}
Adding a plist definition to /Library/LaunchDaemons can ensure the above is executed when the system starts.
26. pkulak ◴[] No.44392866{3}[source]
You know what, your comment actually reminds me that this happened when I also had a bug in my configuration that was causing me to not actually use swap. I assume running out of tmpfs uses swap like anything else? I might give tmpfs another try.
replies(2): >>44393113 #>>44393717 #
27. ◴[] No.44392886{3}[source]
28. fluidcruft ◴[] No.44392902{7}[source]
/tmp is not specified to be a RAM disk by POSIX. Just that things in there are considered to be not persistent after a program stops (with implications for backups and disaster recovery). Sure, RAM disks work if the amount of /tmp space you need is less than your free physical RAM but sometimes that's not the case, either.

Back in the day you might place /tmp in a good spot for random access of small files on a disk platter. /var is vaguely similar but intended for things that need to be persistent.

Anyway it's not uncommon for systems to persist /tmp and clean it periodically from cron using various retention heuristics.

Ultimately POSIX concepts of mountpoints are strongly tied to optimizing spinning rust performance and maintenance and not necessarily relevant for SSD/NVME.

29. AdieuToLogic ◴[] No.44393113{4}[source]
> I assume running out of tmpfs uses swap like anything else?

This is not the case. RAM-based file system capacities are unrelated to process memory usage, of which "swap space" is for the latter.

replies(3): >>44393245 #>>44393314 #>>44393779 #
30. pm2222 ◴[] No.44393129[source]
Systemd clears /tmp from time to time. Just saying.
31. pkulak ◴[] No.44393245{5}[source]
Interesting, thank you. I stand by my original point, downvotes be damned.
replies(2): >>44393330 #>>44400450 #
32. tatref ◴[] No.44393314{5}[source]
That's why on some configurations (RHEL 7 I think), journald will happily fill up your ram via /run/
replies(1): >>44393476 #
33. AdieuToLogic ◴[] No.44393330{6}[source]
> Interesting, thank you.

Glad to help out. Here[0] is more information regarding Linux swap space as it relates to processes and the VMM subsystem.

> I stand by my original point, downvotes be damned.

:-D

0 - https://phoenixnap.com/kb/swap-space

34. AdieuToLogic ◴[] No.44393476{6}[source]
> That's why on some configurations (RHEL 7 I think), journald will happily fill up your ram via /run/

I do not run systemd-based distros, so cannot relate.

35. buckle8017 ◴[] No.44393717{4}[source]
Sibling is wrong tmpfs will swap.

Maybe some other ram disk things won't.

36. buckle8017 ◴[] No.44393779{5}[source]
tmpfs will swap.
replies(1): >>44400424 #
37. godelski ◴[] No.44393836[source]
Another thing you can do is use systemd and use the privatetmp option. You really should be doing this on all your services
38. AdieuToLogic ◴[] No.44400424{6}[source]
> tmpfs will swap.

We are both wrong to a degree, but you are more correct than I was.

According to the docs[0]:

  tmpfs ... is able to swap unneeded pages out to swap
  space, if swap was enabled for the tmpfs mount.
So `tmpfs` does not unconditionally use swap, but can use it if possible. What I was thinking about is `ramfs`, which doesn't support swap, but that is not the topic of the question to which I replied.

0 - https://www.kernel.org/doc/html/latest/filesystems/tmpfs.htm...

39. AdieuToLogic ◴[] No.44400450{6}[source]
I was wrong in my unconditional assertion that `tmpfs` does not use swap. It can, depending on conditions described here[0].

What I was thinking about is `ramfs`, which does not use/support swap and has other limitations not present in `tmpfs`.

Sorry for confusing the topic.

0 - https://www.kernel.org/doc/html/latest/filesystems/tmpfs.htm...

40. frollogaston ◴[] No.44400967{4}[source]
Yeah, Mac is probably the odd one out, but it's also maybe the most common Unix-based/Unix-like desktop OS. Anyway, both are POSIX, unlike Linux.