Skip to content

Instantly share code, notes, and snippets.

@Benabik
Created October 14, 2010 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benabik/626901 to your computer and use it in GitHub Desktop.
Save Benabik/626901 to your computer and use it in GitHub Desktop.
OS X ramdisk script
This file creates and mounts a ramdisk on OS X (10.5+, I believe)
Handy for setting up test environments. I use it for git like so:
device=`ramdisk 250 Git_Test`
export GIT_TEST_OPTS='--root=/Volumes/Git_Test'
make test
hdiutil detach $device
#!/bin/sh
program=`basename $0`
if [ $# -lt 1 -o $# -gt 2 ]; then
echo $? $#
echo "Usage: $program <size in MiB> [<disk name>]"
exit 1
fi
size=$1
name=$2
[ "$name" ] || name=ramdisk
if [ -e "/Volumnes/$name" ]; then
echo "Disk named \"$name\" already mounted."
exit -1
fi
size=$(($size * 2048))
device=`hdiutil attach -nomount ram://$size`
status=$?
if [ $status != 0 ]; then
echo -n "$device"
exit $status
fi
device=`echo "$device" | tr -d '\n\t '`
diskutil=`diskutil erasevolume HFS+ "$name" "$device"`
status=$?
if [ $status != 0 ]; then
echo -n "$diskutil"
exit $status
fi
if ! [ -d "/Volumes/$name" ]; then
echo "Warning: \"/Volumes/$name\" not found. Ramdisk $device not mounted?" >&2
fi
echo $device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment