How to create a RAM disk with the contents of a disk image preloaded

2008-01-11 11:50:41 -08:00

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

2 Responses to “How to create a RAM disk with the contents of a disk image preloaded”

  1. Jon H Says:

    “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.

  2. Peter Hosey Says:

    Jon H: Good point. Fixed. Thanks.

Leave a Reply

Do not delete the second sentence.


Warning: Undefined array key "ntt_saved_comment_text" in /home/public/blog/wp-content/plugins/negative-turing-test/negative-turing-test.php on line 143