Using RAM disks in Mac OS X

Tagged:  

Just the other day, I was confronted with a problem at the university where disk I/O operations were unavoidable. It is a fact that the HDD is slower than RAM, of course, so a mounted FS in RAM was used instead. If one has tried this in FreeBSD or Linux before then this is quite similar and just as easy.

To create a RAM disk the following is done:

% hdid -nomount ram://SECTORS

Here SECTORS is the amount of bytes wanted divided by 512 (as that is the size of a sector) and the "-nomount" flag is necessary as we want to mount ourselves later on. Note that the command returns the name (will be referred to as DISK) of the newly created disk (typically /dev/disk1 if no other has been created).

Then we create our FS on the RAM disk (using HFS Plus):

% newfs_hfs DISK

And create our mount-point (if this hasn't been done yet):

% mkdir -p MOINTPOINT

Here MOINTPOINT is some path to some directory from which the RAM disk should be accessible.

Finally, we mount the disk:

% mount -t hfs DISK MOUNTPOINT

When we are done with the disk we simply unmount and detach:

% umount MOUNTPOINT
% hdiutil detach DISK

I wrote two scripts to encapsulate the above behavior: ramdisk_create.sh and ramdisk_destroy.sh.