Can I put /tmp and /var/log in a ramdisk on OS X?

For non-critical Linux systems, I often move things like /tmp and /var/log to tmpfs to save on some disk writing. I've been doing this for a year or so and if I ever need the logs across reboots, I just comment out a line in /etc/fstab and then start debugging.

In any case, I would like to do the same thing on OS X. I've seen posts on creating a ramdisk for OS X, but I'm looking for a more permanent solution that works on every boot. I always want /tmp and /var/log mounted in a ramdisk, with the ability to turn that off with a bit of command-line editing in vi if I have to.


Here is a script to create ramdisks on OS X. Sorry, it doesn't answer your question. You could use this to build up something that runs on boot and then mounts /tmp and /var/log.

#!/bin/bash  
ramfs_size_mb=1024  
mount_point=~/volatile  

ramfs_size_sectors=$((${ramfs_size_mb}*1024*1024/512))  
ramdisk_dev=`hdid -nomount ram://${ramfs_size_sectors}`  
newfs_hfs -v 'Volatile' ${ramdisk_dev}  
mkdir -p ${mount_point}  
mount -o noatime -t hfs ${ramdisk_dev} ${mount_point}  

echo "remove with:"  
echo "umount ${mount_point}"  
echo "diskutil eject ${ramdisk_dev}"  

From @salvatore http://pastie.textmate.org/pastes/1417478/text?key=igcxuzqqvlmlbavxooj2uw


EDIT: I'm just going to accept my own answer, as it did solve one part of the problem for me. If someone posts something more like --bind in Linux, I'll accept that answer.

In an effort to spur more answers, I'll begin answering my own question with what I have found out.

Step 1 is to get a ramdisk mounted at boot every time. To do this, I create a bash script and then a launchd entry to call the bash script on boot.

Write a bash script like this:

RD=ramdisk
if [ ! -e "/Volumes/$RD" ];  then
    diskutil erasevolume HFS+ "$RD" `hdiutil attach -nomount`
fi

mkdir -p /Volumes/$RD/private/tmp
mkdir -p /Volumes/$RD/private/var/log
mkdir -p /Volumes/$RD/private/var/tmp

Then get it called on boot by adding it to launchd by creating a file called /Library/LaunchDaemons/com.my.ramdisk.plist with the following contents:

<?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>com.my.ramdisk</string>
    <key>ProgramArguments</key>
    <array>
            <string>/usr/local/sbin/ramdisk.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Where I am stuck is a way to symlink or mount directories inside the ramdisk at /tmp, /var/log, and /var/tmp. These three directories are all symlinked on my system to /private/tmp, /private/var/log and /private/var/tmp. When I changed the symlinks to point into /Volumes/ramdisk/..., the system won't boot. I expect this is because at boot time, something wants /tmp and /var/log BEFORE the my com.my.ramdisk script mounts the ramdisk. I need a way to mount the ramdisk right after root is mounted, before anything else runs.

Note If you mount /var/log (kernel, daemon, and other critical user-space logs) in temporary space, you will lose its contents in the next reboot. This might inhibit your ability to diagnose.