How to mount a RAM disk on startup?

Solution 1:

You can delay the execution of the diskutil command in your ramdisk.sh script by prepending the line sleep 10.

Sleep suspend the execution for an interval of time, in seconds.

Your new ramdisk.sh becomes:

sleep 10
diskutil erasevolume HFS+ "RamDisk" `hdiutil attach -nomount ram://800000`

Update#1: launching the ramdisk.sh every 10 seconds and only creating the RAM disk when it is not there, is a workaround. See this example script for such a conditional check:

sleep 10
RD=RamDisk
if [ ! -e "/Volumes/$RD" ];  then
    diskutil erasevolume HFS+ "$RD" `hdiutil attach -nomount ram://800000`
fi

Your error might be caused because /Volumes is not already created in the boot process at the moment your launchd job is executed. Thefore you could first check for the existence of /Volumes before even further executing the script, like:

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

And when the creation succeeds you might want to unload your com.aram.ramdisk.plist from launchctl until the next boot.

Solution 2:

Using Pro Backup's great answer I put this together and it works:

#!/bin/bash
NAME="RamDisk"
while [ ! -d /Volumes ]
do
    echo "waiting..."
    sleep 2
done
if [ ! -d /Volumes/$NAME ]; then
    echo "creating ramdisk..."
    diskutil erasevolume HFS+ $NAME `hdiutil attach -nomount ram://800000`
fi

I needed this so that my browser wouldn't cache to my SSD, so I deleted this:

rm -rf /Users/Aram/Library/Caches/Google/Chrome/Default

And created a symlink:

ln -s /Volumes/RamDisk/ /Users/Aram/Library/Caches/Google/Chrome/Default

EDIT:

I just added the script as a startup application in user settings on my mac, always works, though leaves a terminal open. No big deal for me.