Ramfs On Macos, Linux And Windows

ramfs
system manage
This article offers step-by-step instructions on how to create and mount a RAM disk on different operating systems such as Linux, macOS, and Windows. It covers various methods including the use of imdisk and eram tools.
Published

August 11, 2022


linux

simply use /dev/shm

the only difference is that ramfs on linux is unbounded while tmpfs is bounded

create bounded tmpfs:

mount -t tmpfs -o size=2g tmpfs /mnt/tmp

or yoy labor yourself to mount some additional ramfs:

mount -t ramfs -o size=2g ramfs /mnt/tmp

macos

/private/tmp is not ramdisk. it is just a directory cleared by startup.

ram-shell: A simple script to create a in-memory filesystem on macOS

RAM-backed filesystem mounter for Mac OS X

create and mount ramdisk:

#!/bin/sh
ramfs_size_mb=2100
mount_point=/tmp/rdisk
mkramdisk() {
ramfs_size_sectors=$((${ramfs_size_mb}*1024*1024/512))
ramdisk_dev=`hdid -nomount ram://${ramfs_size_sectors}`
newfs_hfs -v 'ram disk' ${ramdisk_dev}
mkdir -p ${mount_point}
mount -o noatime -t hfs ${ramdisk_dev} ${mount_point}
echo "remove with:"
echo "umount ${mount_point}"
echo "diskutil eject ${ramdisk_dev}"
}

to replace /private/tmp with ramfs and registered as launchd service

calculate sector numbers by hand:

disk_size(MiB)* 1024KiB/MiB * 1024B/KiB / 512B/sector = #sectors

hdiutil attach -nomount ram://#sectors
#get returned value afterwards!
newfs_hfs -v 'Ramdisk' <returned_disk_device_id>
# maybe you should create apfs case sensitive instead?
#mount disk
mkdir -p ~/Ramdisk
# may change fs type accordingly when using apfs
mount -o noatime -t hfs <returned_disk_device_id> ~/Ramdisk

windows

a curated ramdisk software list

use third-party ramdisk tool like imdisk, eram with kernel driver installed, secure boot disabled (no uefi maybe?)