Skip to content

Instantly share code, notes, and snippets.

@stevecooperorg
Created January 10, 2021 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevecooperorg/db02626204a2de95309858d2f2669b56 to your computer and use it in GitHub Desktop.
Save stevecooperorg/db02626204a2de95309858d2f2669b56 to your computer and use it in GitHub Desktop.
#!/bin/bash
# create a RAMDISK like so `create-ram-disk 6` to create a 6Gb ramdisk.
# diagnostic information is written to STDERR: the name of the newly-created disk is written to STDOUT
# from https://stackoverflow.com/a/47354885/6722
set -e
FILEPATH="${BASH_SOURCE[0]}"
FILE=$(basename "${FILEPATH}")
DIR="$(cd "$(dirname "${FILEPATH}")" && pwd)"
SECTOR_SIZE=512
FILE_SYSTEM="APFS"
VOLUME_NAME="RAMDISK"
VOLUME_PATH="/Volumes/${VOLUME_NAME}/"
# grab the command line arg
SIZE_IN_GB=$1; shift || true
SYMLINK_POINT=$1; shift || true
function show_help {
cat <<EOF1
Name
$FILE
Synposis
$FILE <size-in-gb>
Description
creates a ramdisk on macos, and prints the file path of the new volume to
STDOUT.
diagnostic info is written to STDERR.
Examples
$FILE 5
Options
<size-in-gb> size in gigabytes of the disk.
EOF1
}
if [[ "" == "${SIZE_IN_GB}" ]]; then
show_help
exit 2
fi
if [[ "" == "${SYMLINK_POINT}" ]]; then
show_help
exit 2
fi
# calculate the number of sectors by calculating the bytes in SIZE_IN_GB bytes and dividing by sector size
NUM_SECTORS=$(perl -e "print $SIZE_IN_GB*1024*1024*1024/$SECTOR_SIZE")
IMAGE="ram://${NUM_SECTORS}"
cat <<EOF 1>&2
$FILE
ramdisk size - ${SIZE_IN_GB} Gigabytes
sector size - ${SECTOR_SIZE} bytes
number of sectors - $NUM_SECTORS
image - $IMAGE
EOF
DEVICE=$(hdiutil attach -nomount "${IMAGE}" | xargs)
echo "Device: $DEVICE"
1>&2 diskutil partitionDisk "${DEVICE}" 1 GPTFormat "${FILE_SYSTEM}" "${VOLUME_NAME}" '100%'
1>&2 hdiutil imageinfo "${DEVICE}"
echo "Volume: ${VOLUME_PATH}"
ln -s "${VOLUME_PATH}" "${SYMLINK_POINT}"
echo "Symlinked: ${SYMLINK_POINT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment