Nutritious Blue Spray

Part XXXVI of Fear

Previous Entry Share Next Entry
Updated ramfs script for OS X

It has come to my attention that it's been a very long time since I've posted anything here. Let me do something about that...

Just over 4 years ago I posted "ramfs script for MacOS X" which was probably of little interest except to me.  Over the years I've made several improvements to the script, including error handling, naming the volume after the user (instead of the Julian day number), and making the volume private by enabling owners.  In order to enable owners I resorted to running sudo, but maybe all I really need to do is detach the parition and reattach it with "-owners on"--that will be an investigation for another day.


# 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)

# 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
Tags: ,


Log in