11

I am using a command like so to create a ram disk:

diskutil erasevolume HFS+ "ram disk" `hdiutil attach -nomount ram://307200`

This works succesfully, and I get a /Volumes/ram disk mounted on my system that I can use that is mounted from /dev/disk5 or some such place.

I would like to be able to control where this goes, to be able to mount to /tmp/my_dir or where-ever. I have tried many combinations of changing parameters in hdiutil and diskutil without success. What is the right way to do this?

  • Use a symbolic link to make it appear where you desire. – martineau Aug 2 '12 at 17:21
  • I figured out a way to get this done, but I had to do it in more than one step in a bash script – Derek Aug 2 '12 at 18:12
  • You can accept your own answer here (and share the details with the rest of us in the process ;-). – martineau Aug 3 '12 at 17:32
15
#!/bin/sh
ramfs_size_mb=2100
mount_point=/tmp/rdisk

mkramdisk() {
  ramfs_size_sectors=$((${ramfs_size_mb}*1024*1024/512))
  ramdisk_dev=`hdid -nomount ram://${ramfs_size_sectors}`

  newfs_hfs -v 'ram disk' ${ramdisk_dev}
  mkdir -p ${mount_point}
  mount -o noatime -t hfs ${ramdisk_dev} ${mount_point}

  echo "remove with:"
  echo "umount ${mount_point}"
  echo "diskutil eject ${ramdisk_dev}"
}
| improve this answer | |
  • For macOS Sierra it didn't work – Sasho Jul 7 '17 at 9:49
  • 1
    The code snippet does work on Sierra. – Rafa Jan 4 '18 at 15:56
  • 1
    One thing to be aware of is that hdiutil right-pads with spaces its output to a length of 54, so if you quote ${ramdisk_dev}, you'll run into issues. – zneak Feb 23 '18 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.