Skip to content

Instantly share code, notes, and snippets.

@petrowsky
Created October 17, 2017 23:57
Show Gist options
  • Save petrowsky/6f64a064622630504e253a2a094c86fe to your computer and use it in GitHub Desktop.
Save petrowsky/6f64a064622630504e253a2a094c86fe to your computer and use it in GitHub Desktop.
A shell function for creating a RAM disk of arbitrary size. Add to your bash (or other shell) profile.
ramdisk () {
test "$1" || 1=2
DISK_NAME="RamDisk"
MAX_SIZE=6
DISK_SIZE=$((${1}*1024*1024*1024/512))
DISK_FOUND=`df | grep -o $DISK_NAME`
if [[ $1 -gt $MAX_SIZE ]]
then
echo "NOTICE: Can't create a ram disk larger than $MAX_SIZE GB"
return
fi
if [[ $DISK_FOUND = $DISK_NAME ]]
then
echo "NOTICE: A disk named $DISK_NAME already exists!"
return
fi
if ! [[ $1 =~ '^[0-9]+$' ]]
then
echo "NOTICE: Supply a number between 1 and $MAX_SIZE"
return
fi
diskutil erasevolume HFS+ $DISK_NAME `hdiutil attach -nomount ram://$DISK_SIZE`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment