9

There are various resources about how to create a RAM disk, but they all deal with HFS+ RAM disks. But how does one create an APFS RAM disk?

I tried passing APFS as file system to the diskutil command like below, but that doesn't work. Since diskutil has a whole bunch of APFS-related subcommands I assume there's more magic necessary.

DISK_ID=`hdiutil attach -nomount ram://$((4 * 1024 * 2048))`
diskutil eraseVolume APFS "RAM Disk" "$DISK_ID"
2
  • is this faster than hfs?
    – Isaac
    Commented Nov 20, 2019 at 9:52
  • 1
    Interesting question! But I'm curious to what the benefits of APFS would be for a ramdisk
    – Volsk
    Commented Nov 20, 2019 at 17:46

2 Answers 2

7

It works if you create a JHFS+ volume first and convert it to APFS in a second step:

  1. DISK_ID=$(hdiutil attach -nomount ram://$((<number_of_blocks>)))
  2. diskutil eraseDisk JHFS+ "RAM Disk" $DISK_ID
  3. diskutil apfs convert $(tr -d ' '<<<${DISK_ID}s2)

If the RAM disk has a size of 2 GiB (4 * 1024 * 1024)(block_size) or smaller no EFI partition is created and the 3rd command is: diskutil apfs convert $(tr -d ' '<<<${DISK_ID}s1) or more generally:

DISK_ID=$(hdiutil attach -nomount ram://$((<number_of_blocks>)))
SIZE=$(diskutil info $DISK_ID | awk -F'[^0-9]*' '/Disk Size/ {print$4}')
diskutil eraseDisk JHFS+ "RAM Disk" $DISK_ID
if [ $SIZE -le 2147483648 ]; then diskutil apfs convert $(tr -d ' '<<<${DISK_ID}s1); else diskutil apfs convert $(tr -d ' '<<<${DISK_ID}s2); fi

Result:

...
/dev/disk2 (disk image):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        +4.3 GB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                 Apple_APFS Container disk3         4.0 GB     disk2s2

/dev/disk3 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +4.0 GB     disk3
                                 Physical Store disk2s2
   1:                APFS Volume RAM Disk                32.8 KB    disk3s1
1
  • Thanks! In my test, I needed to use s1 instead of s2 in the third command but it worked!
    – DarkDust
    Commented Sep 19, 2017 at 7:37
3

You need to create an APFS container and add your APFS volume inside.

DISK_ID=$(hdiutil attach -nomount ram://$((4 * 1024 * 2048)))
diskutil apfs create "$DISK_ID" "RAM Disk"

Due to what seems like a bug in High Sierra, this isn't possible, as you'll get the following error:

Error: -69802: A partition, not a whole disk, is required for this operation

This is the same error you receive if you try it in Disk Utility.

It works at least on macOS 10.15 Catalina, though.

3
  • 1
    So… this is how it's supposed to work, but it doesn't work?
    – DarkDust
    Commented Sep 18, 2017 at 13:01
  • @Dark Yes as far as I know
    – grg
    Commented Sep 18, 2017 at 14:04
  • I've just tried this on macOS 10.15 and it now works! But you need to pass the whole path to the device (e.g. /dev/disk4), so the command is now: diskutil apfs create "$DISK_ID" "RAM Disk"
    – DarkDust
    Commented Nov 20, 2019 at 11:22

You must log in to answer this question.

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