4

I am trying to create a ram disk on MacOS using the following command

diskutil erasevolume HFS+ RAM_Disk_512MB `hdiutil attach -nomount ram://16384`

This seems to work fine on my desktop but when I try to run it on my laptop I get the following error

Unable to find disk for hdiutil attach -nomount ram://16384

Googling hasn't turned up much. Does anyone have an idea what I might be doing wrong or how I might resolve this?

This system that is giving me problems is running MacOS 10.13.6

4
  • 1
    That error looks like you have the wrong quotes in the laptop version of the command. This is one of many reasons I recommend using $( ) instead of backquotes to do command substitution. Also, it's generally better to put double-quotes around any sort of substitution. So try: diskutil erasevolume HFS+ RAM_Disk_512MB "$(hdiutil attach -nomount ram://16384)" Sep 8, 2019 at 2:52
  • 1
    Nope didn't work. But for some reason when I broke the command up into two different commands it worked so the error must be related to some kind of syntax error as you hinted at. What I did is ran "temp=$(hdiutil attach -nomount ram://16384)" followed by "diskutil erasevolume HFS+ RAM_Disk_512MB $temp" and that did work.
    – HXSP1947
    Sep 8, 2019 at 3:30
  • 1
    Hah, it looks like this is one of those rare cases where you don't want double-quotes. The hdiutil command prints the /dev entry for the RAM disk followed by a bunch of spaces; without double-quotes (like in your version), the spaces get ignored, but with them they're passed on to the diskutil command and confuse it. So try without the double-quotes. Sep 8, 2019 at 3:47
  • Yep, that did it @GordonDavisson! If you want to write up an answer I'll accept it.
    – HXSP1947
    Sep 8, 2019 at 20:48

2 Answers 2

7

Short answer:

diskutil erasevolume HFS+ RAM_Disk_512MB $(hdiutil attach -nomount ram://16384)

Explanation of the original problem: I'm pretty sure the original problem was due to using the wrong quotes (single-quotes instead of backquotes). Compare these commands:

diskutil erasevolume HFS+ RAM_Disk_512MB `hdiutil attach -nomount ram://16384`
diskutil erasevolume HFS+ RAM_Disk_512MB 'hdiutil attach -nomount ram://16384'

They look really similar, right? But they do very different things; the one with backquotes runs hdiutil ... as a command, and uses its output as an parameter to diskutil. The second treats hdiutil ... as a literal string, and passes that to diskutil, which isn't what you want at all. The two kinds of quotes look very similar (there was at least one book on Unix that used a font where they were identical), so this is an easy (and common) mistake to make.

$( ) does essentially the same thing as backquotes, but is visually and syntactically clearer, so use it instead.

What this actually does: the command hdiutil attach -nomount ram://16384 creates a RAM disk, and prints the path to the device file corresponding to it. Something like "/dev/disk4" (except that it prints it followed by a bunch of spaces). The $( ) then treats that as an argument to the main command, so it runs something like diskutil erasevolume HFS+ RAM_Disk_512MB /dev/disk4, which formats (and implicitly mounts) the newly-created RAM disk.

I'd originally suggested putting double-quotes around the $( ) part, but this turned out to cause trouble. Without double-quotes, the output from the command in $( ) will be parsed by the shell (in a kind of weird way), which usually causes more problems than it helps. In this case, however, the output includes a bunch of spaces at the end, and those can confuse hdiutil if it's told they're part of the device entry's path. The no-quote parsing removes the spaces, so it works that way.

This should be safe from the other things that tend to go wrong with unquoted command expansions, with one big warning: if you have messed with the value of IFS (the shell's "internal field separator") variable, it may do completely weird things.

1
  • 1
    Thanks for the great explanation! I was having trouble getting this to work in Fish shell (which uses plain parentheses for commands substitution) until I read "...followed by a bunch of spaces." That was the key to getting it to work: diskutil erasevolume HFS+ "RAMDisk" (string trim (hdiutil attach -nomount ram://16384))
    – phatblat
    May 2, 2020 at 21:52
2

You might want to consider formatting the RAM disk using APFS instead of HFS+. Here are two speed measurements of a 64 GB RAM disk (out of 128 GB on the machine) formatted with HFS+, made using Amorphous Disk Mark 4.0. The following syntax was used to create the RAM disk:

diskutil erasevolume HFS+ "RAMDisk" `hdiutil attach -nomount ram://20971520`&& touch /Volumes/RAMDisk/.metadata_never_index

The touch command at the end tells Spotlight not to needlessly index it; see source below. I observed no difference in measured speeds with and without this command.

You can see the random write speeds are quite poor:

enter image description here

By contrast, here are two measurements of an APFS-formatted RAM disk, created with the following:

diskutil apfs create $(hdiutil attach -nomount ram://131072000) RAMDisk && touch /Volumes/RAMDisk/.metadata_never_index

Credit for syntax: user jdmc at https://apple.stackexchange.com/questions/383202/ram-disks-can-i-use-apfs-and-or-compression

Compared with the HFS+ RAM disk, the APFS one has much better random write performance. Sequential reads and writes between the two formats are the same (to within observed variation).

enter image description here

Curiously, the HFS+ RAM disk does perform somewhat better than the APFS one for QD1 random reads (the difference between ≈290 MB/s for HFS+ and ≈250 MB/s for APFS was seen to be consistent across multiple runs).

Note 1: These are not artifacts caused by the large size of the RAM disk. The same results, within observed varaition, were seen when RAM disk size was reduced to 10 GB (for both HFS+ and APFS).

Note 2: System details: 2019 27" i9 iMac with 128 GB RAM, MacOS Monterey 12.6.3

Also interesting is that my 2TB Western Digital SN850 SSD outperforms both RAM Disks for QD64 random reads:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .