2022-08-11
Ramfs On Macos, Linux And Windows

linux

simply use /dev/shm

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

create bounded tmpfs:

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

or yoy labor yourself to mount some additional ramfs:

1
2
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/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

1
2
3
4
5
6
7
8
9
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?)

Read More