ramfs script for MacOS X

Jun 22, 2009 21:32

This script creates a RAMFS disk with an HFS+ partition on it and mounts it so it is visible in the MacOS X filesystem (including the Finder).

The example script in the hdid(8) man page did not work for me on MacOS 10.5.7. Making the required minimal changes to make it work at all resulted in a volume that could not be cleanly unmounted.

If you are searching for such a script, feel free to use this one for any purpose with no strings attached (and no warranty). Let me know if you improve it.

I was unable to find a working script to do this and only have basic Mac OS X programming knowledge, so this was a learning exercise as well.

Enjoy.

#!/bin/bash

# The volume name will be based on the string "RAMFS" and the number
# of days since the epoch.
DAY=$(($(date +%s)/86400))
VOLNAME="RAMFS-${DAY}"

# The size of the RAMFS disk (in 512K blocks)
NUMBLOCKS=128000

# Use hdid to create the raw RAMDISK
myrdev=`hdid -nomount ram://${NUMBLOCKS}`

# Use diskutil to partition the RAMDISK and create an HFS+ filesystem
# on the only partition. (Noisy output)
diskutil partitionDisk $(basename ${myrdev}) 1 HFS+ "${VOLNAME}" "100%"

# Use hdiutil to mount (attach) the partition on the RAMDISK
mydev=`echo $myrdev | sed -e 's#/rdisk#/disk#g;'`
hdiutil attach $mydev

# Make the mounted filesytem private. (Any other ACLs to munge?)
# (Can we get the mount point some reliable way?)
mountpoint=/Volumes/${VOLNAME}
chmod 0700 ${mountpoint}

echo "run \"hdiutil detach ${mydev}\" when done or use Finder to \"eject\" ${VOLNAME}"

computers, mac

Next post
Up