Get the device number of the RAM disk: diskutil list
(e.g. disk3)
Add some content to the RAM disk 😉.
- Unmount the volume:
diskutil umount disk3
- 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:
- Get the device number of the RAM disk's container scheme:
diskutil list
(e.g. disk4)
- Unmount the volume:
diskutil umountDisk disk4
- Mount the RAM disk read-only:
diskutil mountDisk readOnly /dev/disk4
or the RAM disk's APFS volume:
- Unmount the volume:
diskutil umount disk4s1
- 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...
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.