Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save larrybolt/4313641 to your computer and use it in GitHub Desktop.
Save larrybolt/4313641 to your computer and use it in GitHub Desktop.
Trying to make it work for user-folders, allow storing the files added to the ramdisk so I can use it for "~/Library/Caches/Google/".
#!/bin/bash
# +----------------------------------------------------------------------+
# | |
# | Set up Mac OS X to store temporary files in RAM rather than on disk.|
# | |
# | By Philipp Klaus <http://blog.philippklaus.de> |
# | |
# | Originally by Ricardo Gameiro <http://blogs.nullvision.com/?p=357> |
# | Changes by Daniel Jenkins |
# | <http://blogs.nullvision.com/?p=357#comment-1140> |
# | Changes by CW Shue |
# | <http://blog.cwshue.com/2012/11/moving-tmp-folder-to-ram-disk.html>
# | |
# +----------------------------------------------------------------------+
cd /Library
sudo mkdir RamFS
sudo chown -R root:wheel RamFS
sudo chmod -R u+rwX,g+rX,o+rX RamFS
cat << "EOF" | sudo tee RamFS/RamFS > /dev/null
#!/bin/sh
# Create a RAM disk with same perms as mountpoint
RAMDisk() {
mntpt=$1
rdsize=$(($2*1024*1024/512))
echo "Creating RamFS for $mntpt"
# Create the RAM disk.
dev=`hdik -drivekey system-image=yes -nomount ram://$rdsize`
# Successfull creation...
if [ $? -eq 0 ] ; then
# Create HFS on the RAM volume.
newfs_hfs $dev
# Store permissions from old mount point.
eval `/usr/bin/stat -s $mntpt`
# Mount the RAM disk to the target mount point.
mount -t hfs -o union -o nobrowse $dev $mntpt
# Restore permissions like they were on old volume.
chown $st_uid:$st_gid $mntpt
chmod $st_mode $mntpt
# Rename the new disk to previous name
diskutil rename $mntpt `basename $mntpt`
fi
}
StoreRAMDisk() {
mntpt=$1
tmpmntpt=/tmp/rd_mntpt
# retreive the device path eg: /dev/disk4
dev=`mount|grep $mntpt|awk '{print $1 }'`
# unmount the disk from the dir, the previous files are left in the dir
umount -f $mntpt
# mount the dir to a temp dir
# Create..
mkdir $tmpmntpt
# Mount
mount -t hfs -o nobrowse $dev $tmpmntpt
# Move all files to the original dir
mv $tmpmntpt/* $mntpt
# (rsync seems better, but doesn't works?)
# rsync -a --remove-source-files $tmpmntpt/ $mntpt
# Unmount and remove the tmp mount dir
umount $tmpmntpt
rm -rf $tmpmntpt
# Remove the RamDisk to free up memory
hdiutil detach $dev
}
# Test for arguments.
if [ -z $1 ]; then
echo "Usage: $0 [start|stop|restart] "
exit 1
fi
# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
. /etc/rc.common
StartService () {
ConsoleMessage "Starting RamFS disks..."
RAMDisk /private/tmp 256
RAMDisk /var/run 64
#RAMDisk /var/db 1024
#mkdir -m 1777 /var/db/mds
# RAMDisk /Users/larrybolt/RamDisk 1024 # It doesn't works for a weird reason :(
}
StopService () {
ConsoleMessage "Stopping RamFS disks, nothing will be done here..."
# diskutil unmount /private/tmp /private/var/run
# diskutil unmount /private/var/run
# StoreRAMDisk /Users/larrybolt/RamDisk
}
RestartService () {
ConsoleMessage "Restarting RamFS disks, nothing will be done here..."
}
RunService "$1"
EOF
sudo chmod u+x,g+x,o+x RamFS/RamFS
cat << EOF | sudo tee LaunchDaemons/com.nullvision.RamFS.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>com.nullvision.RamFS</string>
<key>ProgramArguments</key>
<array>
<string>/Library/RamFS/RamFS</string>
<string>start</string>
</array>
<key>KeepAlive</key>
<false/>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>root</string>
<key>StandardErrorPath</key>
<string>/var/log/RamFS</string>
<key>StandardOutPath</key>
<string>/var/log/RamFS</string>
</dict>
</plist>
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment