#!/bin/bash
# make a ramdisk on OSX

[ "$(uname)" = "Darwin" ] || exit

ramfs_size_mb=16
mount_point=~/.volatile

# create ramdisk
ramfs_size_sectors=$(($ramfs_size_mb*2048))
ramdisk_dev="$(hdid -nomount ram://$ramfs_size_sectors)"
newfs_hfs -v 'Volatile' "$ramdisk_dev"
mkdir -p "${mount_point}"
mount -o noatime -t hfs "$ramdisk_dev" "$mount_point"

# do stuff

# cleanup
umount "$mount_point"
diskutil eject "$ramdisk_dev"
rmdir "$mount_point"
