How to create a RAM disk with the contents of a disk image preloaded
-
hdiutil attach -nomount ram://num_sectors
One sector is 512 bytes. The output of this command is a BSD device path, such as “/dev/disk3”.
This is the same step 1 as the usual process.
-
hdiutil attach -nomount path/to/image
For example, hdiutil attach -nomount Disk\ Images/Adium_1.2.dmg
The output for a typical disk image is rather longer:
/dev/disk4 Apple_partition_scheme /dev/disk4s1 Apple_partition_map /dev/disk4s2 Apple_HFS
You want the Apple_HFS one—in this case, /dev/disk4s2.
-
dd if=disk_image_device_path of=ram_disk_device_path
Continuing our example: dd if=/dev/disk4s2 of=/dev/disk3
This copies the bytes from one device to the other. Be very careful which paths you input! You can overwrite your hard disk if you specify the wrong number! The safest way is to assemble the command in a text editor first, then paste it into your terminal.
-
diskutil mount disk3
You should see the volume from the disk image mount in Finder—only this time, it will be writable (most disk images, of course, aren’t). That’s because this volume is not actually the original disk image; it’s the RAM disk, with the disk image’s contents in it.
-
diskutil eject disk4
This is just clean-up. You don’t need the original disk image attached anymore, so now you eject it.
You can use hdiutil detach instead of diskutil eject, with the same device path as its argument.
January 11th, 2008 at 22:07:48
“The safest way is copy and paste.”
Actually the safest way is probably to copy, paste into an editor, build and check the command there, then copy it and paste it into Terminal.
I’ve occasionally had a copy/paste go wrong when I accidentally include a CR or LF or something, resulting in Terminal acting as if I’d hit Return. If that happened preceded by a bad selection and copy, you could end up pasting a wrong drive identifier and kicking off the copy.
January 12th, 2008 at 02:10:11
Jon H: Good point. Fixed. Thanks.