Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create APFS RAM disk on macOS High Sierra

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!

like image 855
MShekow Avatar asked Sep 14 '17 16:09

MShekow


People also ask

Does High Sierra support Apfs?

APFS is not officially supported for macOS versions prior to 10.13 (High Sierra), and will not mount on 10.12 and earlier.

How do I create a new volume on my Mac?

Choose Edit > Add APFS Volume from the menu bar, or click the add volume button (+) in the Disk Utility toolbar. These options are available only when an APFS-formatted volume is selected. Type any name for the new volume, then click Add. Quit Disk Utility.

What is Apple Apfs media?

Apple File System (APFS), the default file system for Mac computers using macOS 10.13 or later, features strong encryption, space sharing, snapshots, fast directory sizing, and improved file system fundamentals.


2 Answers

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

like image 73
unrivaledcreations Avatar answered Nov 12 '22 05:11

unrivaledcreations


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!

like image 36
MShekow Avatar answered Nov 12 '22 07:11

MShekow