30

usually creating RAM disks works with the following commands

hdid -nomount ram://<blocksize>

Returns e.g. /dev/disk2 Then I would format the disk, with say

newfs_hfs /dev/disk2

followed by mounting it:

mount -t hfs /dev/disk2 /some/mount/target

This procedure doesn't seem to work with APFS. I'm on High Sierra beta 9. The mount command doesn't output any error, but the path is not mounted.

In my case, after the hdid command finished, newfs_apfs -i /dev/disk2 yields

nx_kernel_mount:1364: checkpoint search: largest xid 1, best xid 1 @ 1
nx_kernel_mount:1422: sanity checking all nx state... please be patient.
spaceman_metazone_init:278: no metazone for device 0, of size 209715200 bytes, block_size 4096
apfs_newfs:18075: FS will NOT be encrypted.

When I then enter mount -t apfs /dev/disk2 /some/target/path then the mount commands seems to work for 2 seconds, doesn't give any output and the mount was NOT succesful.

Can anyone tell me how to actually make a APFS RAM disk s.t. it works? :p

PS: I've also tried something like diskutil partitionDisk /dev/disk2 GPT APFS myvolumename 0b which does mount the volume to /Volumes/myvolumename but creates yet another disk (disk3 in this case) which seems odd to me!

MShekow
  • 1,526
  • 2
  • 14
  • 28
  • `disk3` in this case is a Core Storage container, which is more or less a required component of APFS. https://en.wikipedia.org/wiki/Core_Storage – Glyph Nov 15 '17 at 22:14

3 Answers3

31

@Glyph provided the best answer in a comment to the accepted answer, but it deserves its own answer:

diskutil partitionDisk $(hdiutil attach -nomount ram://$((2048*sizeInMB))) 1 GPTFormat APFS 'Ramdisk' '100%'

Change sizeInMB to your desired size.

I've updated Glyph's answer to simplify the volume name a little.

ivanzoid
  • 5,952
  • 2
  • 34
  • 43
  • Would love a way to sync this with a folder on the ssd! Starting the sync will 1. Do the initial load of the folder on ssd into the ramdisk, 2. Sync changes from ramdisk to ssd. Two additional nice features would be 3. Help you open the ramdisk folder with your program of choice, once it's copied over; and 4. Prevent programs from changing the folder on the ssd/or at least warn when that happens. @Glyph – Devin Rhode Aug 19 '20 at 20:42
  • Does this not delete some of my data? Disk "1" does not exist. How do I know what disk to pick? –  Dec 11 '20 at 00:37
20

Found a solution:

hdid -nomount ram://<blocksize>
diskutil erasedisk <format> <diskname> <output path of previous hdid command>

where <format> is taken from diskutil listFilesystems from the "Personality" column. Yes, it seems weird to me too that you may have to quote this parameter, e.g. when specifying case-sensitive variants, but oh well...

<blocksize> is 2048 * desired size in megabytes

The last command formats the RAM disk and mounts it to /Volumes/<diskname>

It seems to be the case that when now entering diskutil list that you will see two new disks, the one hdid created, and a synthesized one.

To destroy the RAM disk again, call diskutil eject <output path of previous hdid command>, e.g. diskutil eject /dev/disk2

This will do all the work for you, unmounting the /Volumes/<diskname> path and destroy the two disks, releasing your memory.

Keep in mind that the minimum/maximum values for <blocksize> depend on the chosen <format>. Also, <diskname> cannot always be chosen arbitrarily. Exemplary, FAT32 requires it to consist of upper-case letters!

Cheers!

MShekow
  • 1,526
  • 2
  • 14
  • 28
  • 1
    Following these instructions (albeit using `hdiutil attach` rather than `hdid` since `hdid` is deprecated) I always get `Mounting disk Could not mount disk2 after erase` , whereas the same command line with `HFS+` instead of `APFS` results in success. What are the acceptable minimum/maximum values for APFS volumes? – Glyph Nov 15 '17 at 22:04
  • 15
    `diskutil partitionDisk $(hdiutil attach -nomount ram://2097152) 1 GPTFormat APFS 'APFS RAM Disk' '100%'` worked for me though :) – Glyph Nov 15 '17 at 22:13
0

Also, info to delete/destroy the RAM disk needs to be corrected. ramdisk will be created at path /Volumes/'ramdisk', so the command is:

diskutil eject /Volumes/'ramdisk'
slfan
  • 8,950
  • 115
  • 65
  • 78
sw.smayer97
  • 365
  • 3
  • 9