RAM disks - can I use APFS and/or compression?
I use a 200MB Ram disk to write some throwaway files that I use repeatedly but can forget from run to run and don't mind losing on a shutdown. I often run out of space however and have to clean house.
This comes from a typical recommendation to mount a RAM disk until Macos:
can I use APFS instead? Would there be much benefit in a Ramdisk? Would it compress?
can I activate compression by default? I am dumping out a bunch of throw-away diagnostic HTML files that would take a lot less space if there was transparent file system compression like you can do on NTFS.
old-school, what about APFS?
👇
diskutil erasevolume HFS+ 'RAMDisk' `hdiutil attach -nomount ram://409600`
Diskutil itself doesn't help much:
Not one mention of APFS.
$ diskutil erasevolume -h
Usage: diskutil eraseVolume format name MountPoint|DiskIdentifier|DeviceNode
Erase a single disk partition or whole, laying down a new file system volume
that will be empty of files. Format is the specific file system personality
name of the new volume, e.g. "Journaled HFS+" or a common alias e.g. "jhfs+".
Name is the new volume name (subject to file system naming restrictions) or
can be specified as %noformat% to skip initialization (to skip newfs). You
cannot erase the boot volume. A pseudo-format of "free" or "Free Space" will
remove the partition altogether, leaving a free space gap in the partition map.
Ownership of the affected disk is required.
Examples: diskutil eraseVolume JHFS+ UntitledHFS /Volumes/SomeDisk
diskutil eraseVolume "Journaled HFS+" FooWholeVolEgRaid disk7
diskutil eraseVolume ms-dos FOO disk0s5
diskutil eraseVolume free free disk0s5
Not much documentation from Apple - it's terminal stuff.
Looking at man page we still only have old file systems.
Here's what using the GUI has to say:
Compressed Disk Images. what's that?
I have also seen the term compressed disk image with regards to macos. Does it have any relevance here?
giving it a try didn't help much:
Mount a 50MB
diskutil erasevolume APFS 'ram2'
hdiutil attach -nomount ram://102400``
Started erase on disk2
Unmounting disk
Erasing
Mounting disk
Could not mount disk2 after erase 👈 not good
Finished erase on disk2
ls /Volumes
doesn't show a ram2
disk
total 0
drwxr-xr-x+ 4 root wheel 128 Feb 26 16:03 .
drwxr-xr-x 28 root wheel 896 Sep 30 13:10 ..
lrwxr-xr-x 1 root wheel 1 Feb 8 12:51 MacHD -> /
drwxrwxr-x 8 jluc staff 340 Feb 14 23:01 RAMDisk 👈 existing one
Solution 1:
@klanomath has the correct command in the comments above.
diskutil partitionDisk $(hdiutil attach -nomount ram://SIZE) 1 GPTFormat APFS 'RAMDisk' '100%'
My results:
$ diskutil partitionDisk $(hdiutil attach -nomount ram://12128430) 1 GPTFormat APFS 'RAMDisk' '100%'
Started partitioning on disk15
Unmounting disk
Creating the partition map
Waiting for partitions to activate
Formatting disk15s2 as APFS with name RAMDisk
Mounting disk
Finished partitioning on disk15
/dev/disk15 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme +6.2 GB disk15
1: EFI EFI 209.7 MB disk15s1
2: Apple_APFS Container disk16 6.0 GB disk15s2
Solution 2:
@klanomath's answer will work, but it's needlessly complicated. There's no need for a RAM disk to have partitions, or a partitioning table, or an EFI boot partition. (Go ahead, try booting from a RAM disk! I'll wait.)
All that's needed here is a partitionless disk image, formatted in APFS and consisting of a single APFS container, enclosing a single APFS volume. That's easy with the "diskutil apfs create" verb. Furthermore, we should add a tweak to prevent macOS from wasting cycles trying to Spotlight-index that volume. Putting those two things together yields the following single command line (note that I'm using "SIZE" as a placeholder):
diskutil apfs create $(hdiutil attach -nomount ram://SIZE) RAMDisk && touch /Volumes/RAMDisk/.metadata_never_index
That's it. "SIZE" will be the integer number of 512-byte blocks you want the RAM disk to comprise. For example, a RAM disk of 1GiB (1,073,741,824 bytes) would be specified with the number "2097152" (without the quote marks).