Skip to content

Instantly share code, notes, and snippets.

@Matt3o12
Created October 22, 2014 21:17
Show Gist options
  • Save Matt3o12/e154ed4c22d444079858 to your computer and use it in GitHub Desktop.
Save Matt3o12/e154ed4c22d444079858 to your computer and use it in GitHub Desktop.
Simple gist for creating a RAM Disk on Mac OS X (only tested on Yosemite, but it should work on Mavericks, too).
#!/usr/bin/env python3.4
import argparse
import sys
import subprocess
class ErrorToleranteParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write("error: {}\n".format(message))
self.print_help()
sys.exit(2)
def isInt(value):
try:
int(value)
except:
return False
else:
return True
def main(size, name="RAM Disk", verbose=False):
if isInt(size):
size = int(size)
else:
if size[-1] == "g":
size = int(size[:-1]) * 1024
else:
raise ValueError("value needs to be a number or end with g")
command = "diskutil erasevolume HFS+ '{name}' `hdiutil attach -nomount ram://{size}`"
command = command.format(
name=name,
size=size * 2048
)
if verbose:
print("Calling: '{}'".format(command))
subprocess.check_call(command, shell=True)
if __name__ == "__main__":
parser = ErrorToleranteParser(
description='Creates RAM Disks. Proceed with caution. This program has not been tested '
'and thus it is probably vulnerable!',
)
parser.add_argument("size", help="The size of the disk in megabytes. Append 'g' if you'd like to user GBs instead.")
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose, debug output.")
parser.add_argument("--name", "-n", default="RAM Disk", help="The new of the new RAM Disk.")
args = parser.parse_args()
code = main(**vars(args))
try:
code = int(code)
except (ValueError, TypeError):
code = 0
sys.exit(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment