Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

How to create a RAM disk on OS X (See related posts)

32768 = size in sectors (16MB in this case, a sector is 512 bytes)

$ hdid -nomount ram://32768
/dev/disk1
$ newfs_hfs /dev/disk1
$ mkdir /tmp/ramdisk1
$ mount -t hfs /dev/disk1 /tmp/ramdisk1


To unmount:

hdiutil detach /dev/disk1

Comments on this post

h3rbz posts on Apr 23, 2007 at 08:44
Yo :)

I have thrown together a quick script for anybody that wants to create a ramdisk regularly. It's flexible enough for me, but can be edited to offer more options (if u know a little .sh scripting). The disk is dynamicly named and will show up on the desktop when the script is finished. Unmounting can be done as any drive in the finder. The major differences between my and the above approach is that I use diskutil to mount the created drive, so Finder likes it better, and i give the volume a label, so it can be better recognized by the user ...

Here it is:

Leila:~ samynew[14:30:55]$ cat ramdisk
#!/bin/bash
if [ -n "$2" ]; then ARG_ERR=ERR; fi
if [ -z "$1" ]; then ARG_ERR=ERR; fi
if [ -n "$ARG_ERR" ];
then
echo 1 argument: size in MB
exit
fi
MB_SIZE=$1
let "MB_SIZE *= 2048"
echo Creating ${MB_SIZE} 512-blocks ramdisk
CREATED_RAMDISK=`hdid -nomount ram://${MB_SIZE}`
echo New block device: ${CREATED_RAMDISK}
DISK_NAME=`basename ${CREATED_RAMDISK}`
echo Creating volume with label: ${DISK_NAME}
newfs_hfs -v ${DISK_NAME} /dev/r$CREATED_RAMDISK
echo Mounting in /Volumes/${DISK_NAME}
mkdir /Volumes/${DISK_NAME}
diskutil mount ${CREATED_RAMDISK}

Make sure it's chmodded to be executable: chmod u+x ramdisk
Run as: ./ramdisk <size-in-MB>

I hope u like it.. :)

Grtz
--
H3rbz

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts