6

On macOS Catalina, a RAM disk with 512MB space can be created with the following command:

diskutil eraseVolume HFS+ "RAMDisk" `hdiutil attach -nomount ram://1048576`

On Ubuntu, a RAM disk and its read-only shadow can be created by:

mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk
mount -t aufs -o br:/mnt/ramdisk=ro none /mnt/readonly

So how can I create a read-only RAM disk on macOS Catalina with command line like this, or create a read-only shadow of existing RAM disk?

1
  • 3
    Since you're using macOS Catalina why not use APFS instead of HFS+? After all macOS Catalina uses APFS. The following command works for me: diskutil partitionDisk $(hdiutil attach -nomount ram://1048576) 1 GPTFormat APFS 'RAMDisk' '100%' One advantage, while somewhat negligible as a RAM disk, APFS is faster. It also has less initial overhead, e.g. on a 512 MB disk HFS+ already uses 12.6 MB and APFS only uses 25 K. Percentage wise that's a big difference. Commented Jan 21, 2020 at 13:53

1 Answer 1

11
  1. Get the device number of the RAM disk: diskutil list (e.g. disk3)

    Add some content to the RAM disk 😉.

  2. Unmount the volume: diskutil umount disk3
  3. Mount the RAM disk read-only: diskutil mount readOnly /dev/disk3.

Apply the disk number according to your environment!

  • unmount/mount works for an HFS+ disk (in case of a RAM disk)/volume or an APFS volume only
  • unmountDisk/mountDisk works for HFS+ disks and APFS container schemes.

If you format the RAMDisk as APFS container and one APFS volume

diskutil partitionDisk $(hdiutil attach -nomount ram://1048576) 1 GPTFormat APFS 'RAMDisk' '100%'

you can either mount the APFS container scheme or the APFS volume:

  1. Get the device number of the RAM disk's container scheme: diskutil list (e.g. disk4)
  2. Unmount the volume: diskutil umountDisk disk4
  3. Mount the RAM disk read-only: diskutil mountDisk readOnly /dev/disk4

or the RAM disk's APFS volume:

  1. Unmount the volume: diskutil umount disk4s1
  2. Mount the RAM disk read-only: diskutil mount readOnly /dev/disk4s1

As one-liner for an HFS+ volume with the unique name RAMDisk:

RD=$(diskutil list | awk '/RAMDisk/ { print $5 }'); diskutil umount $RD; diskutil mount readOnly $RD; exit

As one-liner for an APFS volume with the unique name RAMDisk:

RD=$(diskutil list | awk '/RAMDisk/ { print $7 }'); diskutil umount $RD; diskutil mount readOnly $RD; exit

Thanx to user3439894 for all his hints & comments...

2
  • There was a question posted May 17 2020 asking how to create a RAM disk so it can survive a restart. Now that comment or thread appears to be missing. If this is the response could you please reference the question in your answer. Thanks.
    – John
    Commented May 18, 2020 at 20:09
  • @John It was an answer (still visible to users with more than 10k rep) to the question here but deleted subsequently (This doesn‘t answer the question asked above, please use the „Ask Question“ button for new questions. – nohillside♦ yesterday) Body: Just curious if there is a way to create said RAMDisk so that it survives a restart, it would have great benefits of being able to eradicate an internal HD/SSD of Malware, various system problems. Feel free to ask this question! ;-)
    – klanomath
    Commented May 18, 2020 at 20:22

You must log in to answer this question.

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