Created
February 21, 2011 10:37
-
-
Save kaineer/836908 to your computer and use it in GitHub Desktop.
~/bin/mkram - creating temporary 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 ruby | |
module MkRam | |
class Path | |
def initialize | |
@path = File.expand_path( "~/ram" ) | |
end | |
def to_s | |
@path | |
end | |
end | |
class Options | |
def initialize( args ) | |
@unmount_only = args.include?( "-u" ) | |
@size = ( args.first || 16 ).to_i | |
@quiet = args.include?( "-q" ) | |
end | |
attr_reader :unmount_only, :size, :quiet | |
end | |
class Runner | |
def initialize( args ) | |
@path = Path.new | |
@options = Options.new( args ) | |
end | |
def run | |
result = if @options.unmount_only | |
self.unmount | |
else | |
self.mount | |
end | |
self.lsof unless result | |
end | |
protected | |
def unmount | |
if mounted? | |
log( "Trying to unmount #{path}.." ) | |
system( "sudo umount #{path} 2>&1 >/dev/null" ) | |
if mounted? | |
log( "Failed to unmount" ) | |
false | |
else | |
log( "Unmounted successfully" ) | |
true | |
end | |
else | |
true | |
end | |
end | |
def mount | |
if !mounted? || self.unmount | |
log( "Mounting #{size}M into #{path}.." ) | |
system( "sudo mount -t tmpfs -o size=#{size}M #{path} #{path}" ) | |
true | |
else | |
false | |
end | |
end | |
def mounted? | |
( `mount | grep #{path} | wc -l` ).to_i > 0 | |
end | |
def path | |
@path.to_s | |
end | |
def size | |
@options.size | |
end | |
def lsof | |
system( "lsof #{path}" ) unless @options.quiet | |
end | |
def log( msg ) | |
puts msg unless @options.quiet | |
end | |
end | |
end | |
if $0 == __FILE__ | |
MkRam::Runner.new( ARGV ).run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment