Updated ramfs script for OS X

Aug 29, 2013 18:03


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.

#!/bin/bash

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

computers, mac

Previous post Next post
Up