Last active
April 28, 2016 11:34
-
-
Save nyx/8244543 to your computer and use it in GitHub Desktop.
user-friendly (and quick-and-dirty) Ruby script to generate RAM disks on Mac OSX
This file contains hidden or 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
BYTES_PER_SECTOR=512 # AFAIK this is constant for Mac OSX RAM disks | |
BYTES_PER_KILOBYTE=1024 | |
BYTES_PER_MEGABYTE=1024*1024 | |
BYTES_PER_GIGABYTE=1024*1024*1024 | |
def bytes_to_sectors(bytes) | |
sectors = (bytes / BYTES_PER_SECTOR).ceil | |
return sectors | |
end | |
if 2 != ARGV.size | |
puts('Usage: ruby make-ram-disk.rb NAME CAPACITY<kb|mb|gb>') | |
puts('NAME may not contain spaces,') | |
puts('CAPACITY must be a positive integer,') | |
puts('and a valid CAPACITY unit suffix must be specified') | |
exit(1) | |
end | |
name_arg = ARGV[0] | |
capacity_arg = ARGV[1] | |
capacity_arg_length = capacity_arg.length | |
if capacity_arg_length < 3 | |
puts("CAPACITY argument too short: must be at least three characters") | |
exit(1) | |
end | |
name = name_arg | |
capacity = (capacity_arg[0...-2]).to_i # strip off two character unit suffix | |
capacity_unit = capacity_arg[capacity_arg_length-2..capacity_arg_length] | |
capacity_unit.downcase! | |
capacity_bytes = 0 | |
case capacity_unit | |
when 'kb' | |
capacity_bytes = capacity * BYTES_PER_KILOBYTE | |
when 'mb' | |
capacity_bytes = capacity * BYTES_PER_MEGABYTE | |
when 'gb' | |
capacity_bytes = capacity * BYTES_PER_GIGABYTE | |
else | |
puts("invalid capacity unit suffix: valid suffixes are 'kb','mb','gb'") | |
exit(1) | |
end | |
puts("Creating RAM Disk: #{name} #{capacity} #{capacity_unit}") | |
sector_count = bytes_to_sectors(capacity_bytes) | |
puts("Sector Count: #{sector_count}") | |
def shellout(cmd) | |
puts("Executing: #{cmd}") | |
output = `#{cmd}`.strip | |
puts("Output: #{output}") | |
exit(1) unless $?.success? | |
return output | |
end | |
mydev = shellout("hdiutil attach -nomount ram://#{sector_count}") | |
newfs_out = shellout("newfs_hfs -v #{name} #{mydev}") | |
mount_point = "/tmp/#{name}" | |
mkdir_out = shellout("mkdir -p #{mount_point}") | |
mount_out = shellout("mount -t hfs #{mydev} #{mount_point}") | |
puts('Done!') | |
exit(0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment