-
-
Save ORBAT/df150a3c8464557481c4147d3916ab19 to your computer and use it in GitHub Desktop.
ramdisk create/delete/list on macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# From http://tech.serbinn.net/2010/shell-script-to-create-ramdisk-on-mac-os-x/ | |
E_BADARGS=99 | |
baseName=$(basename $0) | |
function printHelp() { | |
>&2 echo -e "\nUsage: " | |
>&2 echo "${baseName} create SIZE_IN_MB [VOLUME_NAME]" | |
>&2 echo -e "\tcreate a new ramdisk" | |
>&2 echo -e "\tVOLUME_NAME defaults to 'ramdisk' if not provided" | |
>&2 echo -e "\te.g create a 100MB volume under /Volumes/Flerb: ${baseName} create 100 Flerb" | |
>&2 echo " " | |
>&2 echo "${baseName} destroy DEVICE_PATH" | |
>&2 echo -e "\tunmount and detach device at DEVICE_PATH (DOES NOT CHECK IF IT'S ACTUALLY A RAMDISK)" | |
>&2 echo -e "\tDEVICE_PATH can be found with 'mount'. Usually /dev/diskN where N is a number" | |
>&2 echo -e "\te.g. ${baseName} destroy /dev/disk3" | |
>&2 echo " " | |
>&2 echo "${baseName} list" | |
>&2 echo -e "\tlist ramdisk device paths. Format is" | |
>&2 echo -e "\t/dev/disk3 (mounted at /Volumes/Flerb)" | |
>&2 echo " " | |
} | |
function charg() { | |
if [[ -z "$1" ]] | |
then | |
>&2 echo expected argument missing | |
printHelp | |
exit $E_BADARGS | |
fi | |
} | |
function createDisk() { | |
charg "$1" | |
>&2 echo "Creating ramdisk" | |
RAMDISK_SIZE_MB=$1 | |
RAMDISK_SECTORS=$((2048 * $RAMDISK_SIZE_MB)) | |
DEVICE_PATH=$(hdiutil attach -nomount ram://$RAMDISK_SECTORS) | |
>&2 echo "Disk ID is:" | |
echo ${DEVICE_PATH} | |
diskutil erasevolume HFS+ "${3:-ramdisk}" ${DEVICE_PATH} | |
} | |
function listRamdisks() { | |
hdiutil info -plist | /usr/bin/xmllint --xpath "//key[.='images']/following-sibling::array/dict/key[.='image-path']/\ | |
following-sibling::string[contains(., 'ram')]/parent::*/key[.='system-entities']/following-sibling::array/dict/key[.='dev-entry' \ | |
or .='mount-point']/following-sibling::string[1]/text()" - | sed -E $'s!/dev/!\\\n&!g'|sed -E 's!/(Volumes/.+)! (mounted at &)!g'|tail -n+2 | |
} | |
function destroyDisk() { | |
charg "$1" | |
>&2 echo "unmounting and detaching $1" | |
umount -f $1 | |
hdiutil detach $1 | |
} | |
case "$1" in | |
create) createDisk $2 $3 ;; | |
destroy) destroyDisk $2 ;; | |
list) listRamdisks ;; | |
*) >&2 echo "'${1}' ain't no command I've ever heard of";printHelp;exit $E_BADARGS ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 3 has an error: Not possible to change the name of the mount in /Volumes/
Very easy fix: Just change
$3
to$2
in https://gist.github.com/ORBAT/df150a3c8464557481c4147d3916ab19#file-ramdisk-sh-L44