←back to thread

62 points hiAndrewQuinn | 1 comments | | HN request time: 0s | source
Show context
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 #
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 #
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 #
1. AdieuToLogic ◴[] No.44392859[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.