Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Last active October 1, 2024 08:01
Show Gist options
  • Save mikeschinkel/85f1745fb860818787daf0d2253856c1 to your computer and use it in GitHub Desktop.
Save mikeschinkel/85f1745fb860818787daf0d2253856c1 to your computer and use it in GitHub Desktop.
Script to create and remove a RAM disk on macOS

How to Create a RAM Disk on macOS

This script creates a RAM disk either in a ./data subdirectory off the root of the current Git repo, or a ./data subdirectory of the current directory if not in a Git repo.

To Attach a RAM Disk

./ramdisk-macos.sh 

To Detatch the RAM disk

./ramdisk-macos.sh -d
#!/usr/bin/env bash
set -eo pipefail
# Expect "-d" for delete or "" to create
DETACH_DISK="$1"
# Number of 512 byte blocks
BLOCKS=1048576
# Mount point for ramdisk
DISK_PATH="data"
function attach_ramdisk() {
hdiutil attach -nomount "ram://${blocks}" 2>&1 | awk '{print $1}'
}
function has_ramdisk() {
local disk_path="$1"
local result
result="$(ramdisk_device "${disk_path}")"
test "${result}" != ""
}
function ramdisk_device() {
local disk_path="$1"
hdiutil info | grep "${disk_path}" | awk '{print $1}'
}
function root_dir() {
local dir
dir="$(git rev-parse --show-toplevel 2>/dev/null||true)"
if [ "" != "${dir}" ]; then
echo "${dir}"
return
fi
pwd
}
function main() {
local device
local blocks="$1"
local disk_path="$2"
local detach="$3"
disk_path="$(root_dir)/${disk_path}"
if [ "${detach}" == "-d" ] ; then
device="$(ramdisk_device "${disk_path}")"
echo "Detaching ramdisk at ${device} from path ${disk_path}"
hdiutil detach "${device}"
exit $?
fi
if has_ramdisk "${disk_path}"; then
echo "Ramdisk path already exists: ${disk_path}"
exit 1
fi
mkdir -p "${disk_path}"
device="$(attach_ramdisk "${blocks}")"
# shellcheck disable=SC2181
if [ $? -ne 0 ] ; then
echo "ERROR: ${device}"
exit 1
fi
echo "Ramdisk attached as ${device}"
newfs_hfs -v data "${device}"
mount -t hfs "${device}" "${disk_path}"
test_file="${disk_path}/.ramdisk"
touch "${test_file}"
echo "Ramdisk created at ${disk_path}"
}
main $BLOCKS "${DISK_PATH}" "${DETACH_DISK}"
@Darsh0307
Copy link

Where can i find the /data subdirectory ?

@mikeschinkel
Copy link
Author

mikeschinkel commented Jan 17, 2024

@Darsh0307 — How did you find this?!? You commented literally 4 minutes after I posted it and before I showed it to anyone.

Anyway, the directory was intended to be <git root>/data, not /data; IOW, a subdirectory of whatever Git directory you happen to be in. If you are not in a git repo it seems it would try to create in the root, which I will update to fix.

@Darsh0307
Copy link

Regarding for first one , I dont know how i came acroos it , but i found a nice way since the Title was Eye catching.

Secondly , I thought this will increase the performance of the laptop overall , But anyways really thanks for this help.

@mikeschinkel
Copy link
Author

@Darsh0307 — Well, thank you for helping me recognize a bug that I have since fixed.

Now it creates it either in a subdirectory of the current directory, or a subdirectory of the root of the Git repo, whichever is the case for the current directory.

I created it so I could run tests on a program I wrote in Go that writes to a Sqlite database as well as large logs to both improve performance but also not cause the SSD on my 2015 MacBook Pro to see premature failure.

@mikeschinkel
Copy link
Author

FYI, here is another Gist on the same topic: https://gist.github.com/htr3n/344f06ba2bb20b1056d7d5570fe7f596

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment