Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jrnewell/ef9a6ed1448f041565c7 to your computer and use it in GitHub Desktop.
Save jrnewell/ef9a6ed1448f041565c7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# +----------------------------------------------------------------------+
# | |
# | Set up Mac OS X to store temporary files in RAM rather than on disk.|
# | Updated for Yosemite, 16 GB of RAM |
# | |
# | By James Newell <jrnewell@github> |
# | |
# | Originally by Ricardo Gameiro <http://blogs.nullvision.com/?p=357> |
# | Changes by Daniel Jenkins |
# | <http://blogs.nullvision.com/?p=357#comment-1140> |
# | Additional changes by Benjamin Chu <http://intechnicolor.net> |
# +----------------------------------------------------------------------+
# if you want a different location for startup script, change this variable
SCRIPT_DIR=/usr/local/sbin
SCRIPT=ram-temp-disks
if [ ! -d "$SCRIPT_DIR" ]; then
echo "$SCRIPT_DIR does not exist. Either create the directory or change SCRIPT_DIR in this script..."
exit 1
fi
cd "$SCRIPT_DIR"
cat << "EOF" | tee "$SCRIPT" > /dev/null
#!/bin/sh
# Create a RAM disk with same perms as mount point
RAMDisk() {
local mount_pt="$1"
local ramdisk_size=$(($2*1024*1024/512))
# create ram disk
echo "Creating RamFS for $mount_pt"
local ramdisk_dev="$(hdiutil attach -drivekey system-image=yes -nomount -noautoopen ram://${ramdisk_size} | sed -e 's/[[:space:]]*$//')"
# seems to help with preventing issue where some disks did not mount
sleep 1
# success
if [ $? -eq 0 ]; then
# Create HFS on the RAM volume.
sudo newfs_hfs -v "$3" $ramdisk_dev
# Store permissions from old mount point.
eval `/usr/bin/stat -s $mount_pt`
# Mount the RAM disk to the target mount point.
sudo mount -t hfs -o noatime -o union -o nobrowse "$ramdisk_dev" "$mount_pt"
# Restore permissions like they were on old volume.
sudo chown $st_uid:$st_gid "$mount_pt"
sudo chmod $st_mode "$mount_pt"
fi
}
# 512 MB for temp directory
RAMDisk "/private/tmp" 512 "RAM Temp"
# 128 MB for run directory
RAMDisk "/var/run" 128 "RAM Run"
EOF
sudo chmod u+x,g+x,o+x "$SCRIPT"
cd /Library/LaunchDaemons
cat << EOF | sudo tee ram-temp-disks.plist > /dev/null
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>RAM Temp Disks</string>
<key>ProgramArguments</key>
<array>
<string>${SCRIPT_DIR}/${SCRIPT}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
exit 0
@erixtekila
Copy link

Hi,

Strangely, only the run disk seems to appear in the mounted devices ?!

$ mount 
/dev/disk0s2 on / (hfs, local, journaled, noatime)
devfs on /dev (devfs, local, nobrowse)
/dev/disk1s2 on /Volumes/z (hfs, local, journaled)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
/dev/disk5 on /private/var/run (hfs, local, union, noatime, nobrowse)

Any clue ?

@jrnewell
Copy link
Author

jrnewell commented Aug 9, 2015

Yeah I just noticed it doesn't mount /private/tmp off and on for me. I found a blog that suggested putting a sleep 1 after the attach or putting a sleep 4 at the start that helps with this issue. However, I just disabled mine because it seems to cause a 20 sec timeout on my shutdown. Hope you have better luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment