Command-line RAM Disk creation and removal on OSX

Going a bit further with the loopback files, I decided to experiment with RAM filesystems on OS X.  Quite simple, and I created a couple of scripts to automate creation and removal of RAM disks.  Nothing fancy, but figured I’d share.

ramfs-create.sh – first parameter is amount of ram to use in mb, second parameter is optional and is the disk name. (no spaces)  By default it’s called “RAMDisk”.

ramfs-remove.sh – first parameter is optional, and is the disk name.  It defaults to “RAMDisk” if no parameter is provided.

ramfs-create.sh

#!/bin/sh

if [ -z $1 ]; then
echo “parameters necessary.”
echo “$0 [ ram use in Mb ] <Disk Name>”
exit 1
fi
NAME=”RAMDisk”

if [ ! -z $2 ]; then
echo “Name being set to something different”
NAME=$2
fi
RAM=$( expr $1 \* 2048 );
diskutil erasevolume HFS+ “${NAME}” `hdiutil attach -nomount ram://${RAM}`

ramfs-remove.sh

#!/bin/sh

NAME=RAMDisk

if [ ! -z $1 ]; then
NAME=$1
fi

diskutil unmount “${NAME}”
DISK=`diskutil list | grep -i ${NAME} | awk ‘{ print $5 }’`
hdiutil detach /dev/${DISK}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.