Skip to content

Instantly share code, notes, and snippets.

@ixti
Last active October 23, 2022 00:34
Show Gist options
  • Save ixti/ab2dc4f444c14ef1d7c33e750f7efc4f to your computer and use it in GitHub Desktop.
Save ixti/ab2dc4f444c14ef1d7c33e750f7efc4f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash -x
# Creates RAM disk.
#
# SYNOPSIS
# create_ramdisk [size [label]]
#
# OPTIONS
# size: RAM disk size in megabytes. Default: 1024
# label: RAM disk volume label. Default: UUID
#
# RETURN VALUE
# Created RAM disk device path.
create_ramdisk() {
# Digits after `ram://` denote amount of 512B sectors.
local sectors=$((${1:-1024} * 1024 * 1024 / 512))
local ramdisk=$(hdiutil attach -nomount ram://$sectors)
diskutil eraseVolume "HFS+" "${2:-$(uuidgen)}" "$ramdisk"
echo "$ramdisk"
}
mount_ramdisk() {
local ramdisk="$1"
local mount_point="$2"
diskutil unmount "$ramdisk"
mkdir -p "$mount_point"
mount -t hfs -o noatime "$ramdisk" "$mount_point"
chflags hidden "$mount_point"
}
start_mongo() {
sudo ifconfig lo0 alias 127.0.0.2 up
sudo mongod --bind_ip "127.0.0.2" --dbpath "$1"
}
main() {
local mount_point="/Users/${USER}/mongodb-ramdisk"
local ramdisk=$(create_ramdisk 256)
mount_ramdisk "$ramdisk" "$mount_point"
start_mongo "$mount_point"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment