Created
July 17, 2015 14:05
-
-
Save daumiller/7b4b5fde99feb81e0f03 to your computer and use it in GitHub Desktop.
OSX RAM Disk
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
#!/usr/bin/env node | |
"use strict"; | |
const shell = require("child_process").exec; | |
function byteSize(strSize){ | |
if(!strSize.match(/[0-9]+[bBkKMmGgTt]/)) { throw "Invalid Size Parameter: byteSize(\"" + strSize + "\")"; } | |
if(strSize.length < 2){ return strSize; } | |
const unit = strSize[strSize.length-1].toLowerCase(); | |
const amount = parseInt(strSize.substr(0, strSize.length-1)); | |
switch(unit){ | |
case "b": return amount; | |
case "k": return amount * 1024; | |
case "m": return amount * 1048576; | |
case "g": return amount * 1073741824; | |
case "t": return amount * 1099511627776; | |
default: return parseInt(strSize); | |
} | |
} | |
function sectorSize(strSize){ | |
return byteSize(strSize) / 512; | |
} | |
function _rdEject(){ | |
shell("hdiutil eject " + global.device, function(err){ | |
if(err !== null){ | |
console.error("### Error Ejecting Device \"" + global.device + "\"; May Need Manual Ejection. ###"); | |
console.error(err); | |
} | |
process.exit(-1); | |
}); | |
} | |
function _rdFormat(){ | |
shell("diskutil erasevolume HFS+ \"" + global.label + "\" " + global.device, function(err){ | |
if(err !== null){ | |
console.error("### Error Formatting RAM Disk; Attempting Cleanup. ###"); | |
console.error(err); | |
_rdEject(); | |
} | |
console.log("### Mounted New RAM Disk Device \"" + global.device + "\", Named \"" + global.label + "\". ###"); | |
process.exit(0); | |
}); | |
} | |
function rdCreate(){ | |
shell("hdiutil attach -nomount ram://" + global.sectors, function(err, out){ | |
if(err !== null){ | |
console.error("### Error Creating RAM Disk. ###"); | |
console.error(err); | |
process.exit(-1); | |
} | |
global.device = out.trim(); | |
_rdFormat(); | |
}); | |
} | |
if(process.argv.length < 3){ | |
console.error("### Error: No Size Specified. ###"); | |
process.exit(-1); | |
} | |
global.sectors = sectorSize(process.argv[2]); | |
global.label = (process.argv.length < 4) ? "RamDisk" : process.argv[3]; | |
rdCreate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment