Created
April 8, 2016 00:36
-
-
Save afirth/6f14492558c2ad111bdd60c947940570 to your computer and use it in GitHub Desktop.
OSX ramdisk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# From http://spargelkohl.livejournal.com/65263.html - 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). | |
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/opt/local/bin:$HOME/bin | |
umask 077 | |
prog="${0##*/}" | |
if [ "$(uname -s)" != Darwin ]; then | |
echo "$prog: must be run under OS X" 1>&2 | |
exit 1 | |
fi | |
# The volume name will be based on the string "RAMFS" and the username. | |
VOLNAME="RAMFS-${LOGNAME:-$USER}" | |
mountpoint=/Volumes/${VOLNAME} | |
if [ -d "$mountpoint" ]; then | |
echo "$prog: $mountpoint already mounted." 1>&2 | |
exit 1 | |
fi | |
# 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 | |
fi | |
# The size of the RAMFS disk (in 512K blocks) [I think this is 4KB blocks for 512MB -AF] | |
NUMBLOCKS=128000 | |
# 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... | |
mydev="${mydev//[[:space:]]/}" | |
fi | |
case $mydev in | |
/dev/disk*) | |
;; | |
*) | |
echo "$prog: hdiutil returned device '"$mydev"', but this script expected something like /dev/disk*" 1>&2 | |
exit 1 | |
esac | |
# 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... | |
mypart="${mypart//[[:space:]]/}" | |
fi | |
case $mypart in | |
${mydev##*/}s*) | |
;; | |
*) | |
echo "$prog: diskutil returned partition '"$mypart"' for volume '"$VOLNAME"', but this script expected something like ${mydev##*/}s*" 1>&2 | |
exit 1 | |
esac | |
## 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 | |
fi | |
# 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 | |
fi | |
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