Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
OSX ramdisk
# From - 2016-04-08 -AF]
# This script creates a RAMFS disk with an HFS+ partition on it and
# mounts it so it is visible in the OS X filesystem (including the
# Finder).
umask 077
if [ "$(uname -s)" != Darwin ]; then
echo "$prog: must be run under OS X" 1>&2
exit 1
# The volume name will be based on the string "RAMFS" and the username.
if [ -d "$mountpoint" ]; then
echo "$prog: $mountpoint already mounted." 1>&2
exit 1
# Do not check for sudo access until after the script has determined
# that there is no existing volume mounted.
if ! sudo -v; then
echo "$prog: must be able to run sudo" 1>&2
exit 1
# The size of the RAMFS disk (in 512K blocks) [I think this is 4KB blocks for 512MB -AF]
# Use hdiutil to create the raw RAMDISK
mydev=$(hdiutil attach -mount suppressed ram://${NUMBLOCKS})
if [[ "$mydev" =~ [[:space:]] ]]; then
# hdiutil returned device with whitespace; trim it...
case $mydev in
echo "$prog: hdiutil returned device '"$mydev"', but this script expected something like /dev/disk*" 1>&2
exit 1
# do this in a trap so the user sees it even if there's an error
trap "echo run \\\"hdiutil detach ${mydev}\\\" when done or use Finder to eject partition \\\"${VOLNAME}\\\"" 0 EXIT
# Use diskutil to partition the RAMDISK and create an HFS+ filesystem
# on the only partition. Get the partition name so we can enable
# ownership on it.
mypart=$(diskutil quiet partitionDisk ${mydev##*/} 1 HFS+ "${VOLNAME}" "100%" | awk '/'"$VOLNAME"'/ {print $NF}')
if [[ "$mypart" =~ [[:space:]] ]]; then
# hdiutil returned partition with whitespace; trim it...
case $mypart in
echo "$prog: diskutil returned partition '"$mypart"' for volume '"$VOLNAME"', but this script expected something like ${mydev##*/}s*" 1>&2
exit 1
## not usually needed--disk is automatically attached after
## partitioning, but we want the device to be available to run the next
## command on it...
#hdiutil attach -owners on "${mydev}"
# Use diskutil enable ownership (requires sudo; attaching with
# "-owners on" has no effect)
sudo diskutil quiet enableOwnership "${mypart}"
if (($? != 0)); then
echo "$prog: there was a problem enabling ownership on partition '"$mypart"'." 1>&2
exit 1
# Make the mounted filesystem private. (Any other ACLs to munge?)
# (Can we get the mount point some reliable way?)
chmod 0700 "${mountpoint}"
if (($? != 0)); then
echo "$prog: there was a problem setting the mode to 0700 on the mountpoint '"$mountpoint"'." 1>&2
exit 1
echo "$prog: volume has been mounted at '"$mountpoint"' (with ownership enabled)." 1>&2
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.