Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Two ways to create RAM disks System
Creating a RAM disk was easy in 9, and, until now, "undocumented" in X. There is a utility called ramBunctious that brings this functionality back to X (well, at least it puts a GUI on it).

Rambunctious has excellent documentation on the state of RAM disks in X and when you should/should not use them. The reason I mention it here is because it could have a huge positive impact on the functionality of a bootable X CD.

Read the rest of the article if you'd like to know how to create your own RAM disk from the command line...

For those of you interested in how this works from the command line, it's pretty simple. First, create a read/write disk image (you can do this in Disk Copy). Next, create the /dev entry for RAM using the hdid tool. Finally, copy the contents of the disk image to the RAM /dev entry and mount it. Because the disk image is mounted from the /dev entry, any changes occur in RAM. Here are the Terminal commands:

First, calculate the number of 512K blocks you need: numblocks = (image size in MB) * 2048; for example: 5MB = 10240 blocks. Open a terminal and type:
% hdid -nomount ram://numblocks
[result: /dev/disk1]
Replace numblocks with the number you calculated in the first step. Next type:
% dd if=/path/to/image/file.dmg of=/dev/disk1 bs=512
[result:2866+0 records in
2866+0 records out
1467392 bytes transferred in 1 secs (1467392 bytes/sec)]
% hdiutil mount /dev/rdisk1
You need to change the "1" in disk1 and rdisk1 to whatever number is returned by the first command.

[Editor's note: I have not tried this myself.]
    •    
  • Currently 2.33 / 5
  You rated: 1 / 5 (3 votes cast)
 
[41,423 views]  

Two ways to create RAM disks | 23 comments | Create New Account
Click here to return to the 'Two ways to create RAM disks' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Great news! Plus an easier way.
Authored by: Anonymous on May 30, '02 01:44:50PM
This is fantastic. Good work.

I've found (based on bombich's findings) easier ways of doing this.

Easiest way: from the terminal, run the command:
hdid ram://disksize
In the finder, a message pops up "This disk is unreadable"
Click "Initialize..." and then use disk utility to format the new ram disk you've created. Now just drag files onto the ram disk that you need.

Here's an all-terminal way of doing the same: (output is in bold).
hdid -nomount ram://disksize
/dev/diskN
newfs_hfs /dev/rdiskN
Initialized /dev/rdiskN as a [disksize]MB HFS Plus volume
hdiutil mount /dev/diskN


where N is the disk number returned by hdid in step 1.
Note the change to rdiskN from disk in in the newfs step: this is because rdisks are "raw disks" that can be formatted.
/dev/disk entries are only useful after the corresponding raw entry has been formatted.

Note that I have had problems in getting the ram disks to actually go away after ejecting them in the finder. On a few occasions I have had to use Disk Utility to eject the ram disks, even though their icons were gone after I "ejected" them from within the finder.

[ Reply to This | # ]
Great news! Plus an easier way.
Authored by: deleted_user17 on May 30, '02 04:36:32PM

There is no point for a RAM disk in a Virtual Memory System like OS X!



[ Reply to This | # ]
Great news! Plus an easier way. BUT...
Authored by: cleofus on May 30, '02 04:58:15PM

It is true that in a high speed virtual memory system there is no NEED for a RAM disk, but there is a USE for one! I still turn my Mac off when I'm not home (where it is) so I use a RAM disk for my cache for Netscape or IE. That way when I turn the computer off, my cache simply dissappears instead of remaining on the machine. When you create a RAM disk, you can point your cache to that "disk" and then the cache will run at RAM speeds, which is still faster than the virtual memory that ANY system has, and it will not stay with the computer and eventually need clearing out. If you simply quit Netscape or IE, you can clear it out manually (or with some script) as well as the history, and any other database that gets created whenever you run the web browsers. So for a security reason, it is easier to delete lots of unwanted tracking files with a RAM disk.



[ Reply to This | # ]
Great news! Plus an easier way. BUT...
Authored by: geohar on May 31, '02 04:41:39AM

I see the point for killing the IE cache, but, it might well not be faster than doing it other ways. The point is, that all file i/o in unix is buffered via memory. This is done in such a way that with a good quantity of ram, the disk is very rarely written to. The vm system takes care of paging the 'dirty' file buffers to disk. I very much doubt if in osx the ram disk gets guaranteed ram residency. Therefore, when the system has need for extra pages, it too will be paged out to disk (and will sit there until paged in again). I may be wrong, but I think the only non-pagable part of the memory will be a set of core kernel code.

Cool hint though. I've been asked that a couple of times and couldn't work it out.



[ Reply to This | # ]
Great news! Plus an easier way.
Authored by: robh on May 30, '02 06:22:25PM

Ram disks improve efficiency enormously for applications that manipulate hundreds or thousands of temporary files, when disk I/O becomes a bottleneck due to the limited hardware bandwidth.



[ Reply to This | # ]
There is no point???
Authored by: paulio on May 31, '02 01:56:33PM
Just startup in single user mode (command-s at startup) and try using the man command:
man ls
You will get an error message because /tmp is not writable. This is the perfect time for a ramdisk. Linux does this for installation and for a bootable CD.

[ Reply to This | # ]
There is no point???
Authored by: sjk on Jun 03, '02 07:16:21PM

/tmp and /var/run are candidates to mount as RAM disks.



[ Reply to This | # ]
Sad news! UMA still has no bootable/persistent RAMDISKS!
Authored by: Anonymous on May 30, '02 07:44:36PM

Some while ago , Apple started using cheap memory parts -- and thus obsoleted a VERY cool feature that has been an essential life-saver many times when a system became toally unbootable otherwise (from cdrom or dasd or zipdisk) ...

namely a persistent/bootable ramdisk!

it is a shame that apple cant restore thi feature, considering the premium they charge over comodity hardware!

not the fault of rambunctios at all.



[ Reply to This | # ]
Sad news! UMA still has no bootable/persistent RAMDISKS!
Authored by: THEM on May 30, '02 11:27:37PM

I thought it was because the newer memory systems lose contents a lot faster when not getting power than the old memory. Not "cheaper" parts.



[ Reply to This | # ]
Sad news! UMA still has no bootable/persistent RAMDISKS!
Authored by: geohar on May 31, '02 04:47:18AM

I'm fairly certain that bootable ram disks were never preserved in ram over a shutdown. Instead, they got written to disk and read back into ram at startup by bootstrap code. I remember seeing the files (invisible on early SE/ Plus systems). New ram technology does loose information faster than old technology, but then its also faster to access and bigger capacity and higher bandwidth. And whilst it is cheaper than ram used to be this is due to increased quality production processes (which create higher chip yeild, and hence are lower overhead in terms of cost) and because ram is comoditized (and the market has surplus - remember when a fire in Japan wiped out a whole crop, ram went ceiling high then).



[ Reply to This | # ]
Great news! Plus an easier way.
Authored by: vonleigh on May 31, '02 04:22:43AM

That's a really easy way to make the ram disk, only one command (hdid ram://disksize). Couple of questions though

1. In what format is the size? I put in 50 and it created a 25KB image.
2. How do you remove it once you're done? I used hdid eject /dev/disk3 (what the first command gave back).


Vonleigh



[ Reply to This | # ]
Great news! Plus an ApleScript way!!
Authored by: dashard on May 31, '02 04:23:35PM
I took the hints from 'zpincus' and modified them to AppleScript.
Here's what I came up with (you may have to reformat the script from the copy/paste).
tell application "Finder"
activate
display dialog "This script will create a RAM Disk on your desktop.
How many megabytes would you like it to be?" default answer "" buttons {"OK", "Cancel"} default button 1
set diskSize to (text returned of the result)
set diskSize to diskSize * 2048
do shell script "hdid -nomount ram://" & diskSize
set dskImg to the result

set prevTextDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set myDevDisk to the last text item of dskImg
log result
set myShellCmd to "/sbin/newfs_hfs /dev/r" & myDevDisk as text
do shell script myShellCmd
display dialog result
do shell script "hdiutil mount /dev/" & myDevDisk
set AppleScript's text item delimiters to prevTextDelims
end tell

Hope it works for you.
For those of you who may have been getting an error at the 'new_hfs'... part, I added the full path to the command, as mine was giving me an error.

Please also note that you will very likely have to unmount the disk from the Disk Utility. It disappeared fine from my desktop with the drag-to-trash method, but didn't really go away. FWIW.

Feel free to email me with questions.
Enjoy.

[ Reply to This | # ]
AppleScript To Create RAM Disks
Authored by: nick2588 on May 31, '02 04:26:47PM
You can download an AppleScript Application to create a RAM Disk at my web site:
http://homepage.mac.com/nick2588/

It is pretty much uses the code above, but an AppleScript is nice because it is all automated. :)

[ Reply to This | # ]
Great news! Plus an easier way.
Authored by: GaelicWizard on Jun 01, '02 08:29:15PM

this is a great tip, but here's my problem:
how can I make this all happen automatically?
and not with an applescript, from the command line. I want to make this happen at boot, so I can set my browsers to default ot this and so I can build on it all without having to do it manually beforehand.
Thanx in advance.

JP



[ Reply to This | # ]
UNIX script --This is what happens when i'm REALLY bored.
Authored by: GaelicWizard on Jun 02, '02 01:55:59AM

Me again, I answered my own question. I don't know how efficient it is, but here's my script:


#!/bin/sh

echo -n "/dev/r" > /tmp/RamDisk.raw
hdid -nomount ram://10240 | awk -F / '{print $3}' > /tmp/RamDisk
cat /tmp/RamDisk >> /tmp/RamDisk.raw
newfs_hfs `cat /tmp/RamDisk.raw`
echo -n "/dev/" > /tmp/RamDisk.mount
cat /tmp/RamDisk >> /tmp/RamDisk.mount
hdiutil mount `cat /tmp/RamDisk.mount`



obviously, replace the 10240 with the size you want for your disk.

JP



[ Reply to This | # ]
UNIX script --This is what happens when i'm REALLY bored.
Authored by: robh on Jun 03, '02 09:28:03AM
I've made a Perl script to call the necessary commands: download it here Unlike the other suggestions so far, this one also lets you specify the name of the disk and does some error simple error checking. Usage: ramdisk MB_size [name] e.g. ramdisk 10 mydisk ramdisk 20 If you don't give a name, it defaults to "untitled".

[ Reply to This | # ]
BootCD already updated with this
Authored by: gameraboy on May 31, '02 06:46:13AM
When rambunctious first came out I emailed the author of BootCD about this. He quietly updated BootCD with ram disk support, making it so you can boot up OS X and mount your hard drive. His first version of BootCD couldn't mount disks. I gave it a try, and it works! There is a crash on restarting though, that he was still working on.

You can get the latest BootCD at http://homepage.mac.com/csrstka/

[ Reply to This | # ]

Ram Disk Usage
Authored by: Frankie on Jun 18, '02 05:54:31PM

Thanks for the info on creating a ram disk. My problem is that I want to do distributed computing on my machine without having the hard drive grinding away continuously. In OS 9, I could load the SETI-at-Home files onto a ram disk and shut down the hard drive while it was running data sets. In trying to run Distributed Folding (which I recommend as a worthy enterprise) from the Terminal, the Terminal doesn't seem to want to access, or even to recognize, the ram disk or its contents.
Does anyone know how to get the OS X Terminal program to read/write files that are not on the primary HD?



[ Reply to This | # ]
Better Script
Authored by: sgi_oh_too on Jun 20, '03 01:02:31PM

Hello! this is a revised script that is a bit better
than the aforementioned scripts. If you make
any revisions, please post them here so we can
all benefit. Thanks!

#!/bin/sh

if [ $# -ne 1 ]
then
echo " Usage: newRamDisk.sh <size in MB>"
echo "Example: newRamDisk.sh 20"
exit
fi

if [ $1 -lt 4 ]
then
echo "Error: Size must be greater than or equal to four megabytes"
exit
fi

ramDiskSize=`echo "${1}*2048" | bc`
echo "Allocating $ramDiskSize blocks"
ramDiskPath="/dev/r"`hdid -nomount ram://${ramDiskSize} | awk -F / '{print $3}'`
echo -n "Creating HFS Plus filesystem on $ramDiskPath"
newfs_hfs -v "Ram Disk" $ramDiskPath
echo "Mounting $ramDiskPath"
hdiutil mount $ramDiskPath

exit



[ Reply to This | # ]
Reclaim
Authored by: sgi_oh_too on Jun 20, '03 01:09:42PM

How does one go about reclaiming the used memory after the ram disk is ejected? the /dev/r* device is still around consuming memory ... what to do? ... what to do?



[ Reply to This | # ]
Archaic methodology
Authored by: nyarlathotep on Jun 21, '03 12:28:04PM

I've never understood why ram disks still exist. Caching completely replaces the need for ram disks in the read-only world, i.e. everyone knows that you should never put an application in a ram disk. Likewize, improving caching technology to handle the complexities of the read-write world would seem to be the intelegent place to direct your efforts. For example, programs like ProjectBuilder could label all their intermediate build files as "unimortent / never save to disk" and label all the basic source files as "critical / cache in emergency crash resistant ram or flash." You could also have a "high preformance file" checkbox in the file info editor, but this is probably a waist of the users time. Anyway once the OS know what the use really cared about, it could just happily go about not spinning up the harddrive for extended periods of time.

Anyway, whats really needed is a block of memory which is safe from crashes, once you have that bit of hardware, the caching technology to distinguish between critical source files and unimportent build phase files will develope. Heck, you could even have running applications label some data as critical and the OS would back that data up into this safe memory (and provide facilities for recovering from it). With that little feature, no one would even need to run their HD on a 9 hour plane flight.. or worry about what files were on a ramdisk.



[ Reply to This | # ]
Useful in the real world
Authored by: JamieF on Apr 19, '04 07:37:57PM

So what you're saying is, we should redesign the OS's cache management and filesystem semantics and rewrite all applications, to avoid what you feel is an improper technique (using a ramdisk). O-kay.

In the real world, there's software that writes temporary files that aren't needed (or which fails to access its files in such a way as to allow the OS-level filesystem cache to be as efficient as it could be), and ramdisks empower users to make that software do the right thing for their particular usage scenario.

Sometimes a simple, pragamatic solution that isn't academically pure is the Right Thing To Do because the net amount of effort that would go into the "pure" solution isn't worth that last 2% of efficiency that it would yield.



[ Reply to This | # ]
a simple way
Authored by: ramdisk on Jun 17, '08 04:45:34PM
There is a simpler way to create a ram disk described here:
http://mac.wikia.com/wiki/RAM_disk#Creating_a_RAM_disk
You can do in a single line.


[ Reply to This | # ]