class Diskman::RootDevice

Public Class Methods

choose() click to toggle source
# File lib/diskman/root_device.rb, line 13
def self.choose
    selection = Chooser.new(RootDevice.get_removable, item: 'removable device').select
    selection.ensure_not_mounted!
    selection
end
get_removable() click to toggle source
# File lib/diskman/root_device.rb, line 3
def self.get_removable
    Dir['/sys/block/*/removable'].select do |file|
        File.read(file).strip == '1'
    end.map do |path|
        path =~ %r[^/sys/block/(.*)/removable$]x && RootDevice.new($1)
    end.reject do |device|
        device.name =~ /sr\d+/
    end.sort
end

Public Instance Methods

choose_with_partitions() click to toggle source
# File lib/diskman/root_device.rb, line 19
def choose_with_partitions
    Chooser.new(get_with_partitions, item: 'block device').select
end
ensure_not_mounted!() click to toggle source
# File lib/diskman/root_device.rb, line 23
def ensure_not_mounted!
    if mounted?
        puts ('Warning: device appears to be mounted at ' + mount_point).yellow
        puts 'Not continuing'.red
        raise Interrupt
    end
end
to_s() click to toggle source
# File lib/diskman/root_device.rb, line 31
def to_s
    @path + ' [' + [size, label].reject(&:empty?).join(', ') + ']'
end

Private Instance Methods

get_int_prop(name) click to toggle source
# File lib/diskman/root_device.rb, line 43
def get_int_prop(name)
    get_prop(name).to_i
end
get_prop(name) click to toggle source
# File lib/diskman/root_device.rb, line 47
def get_prop(name)
    file = "/sys/class/block/%s/%s" % [@name, name]
    File.read(file).strip.gsub(/\s+/, ' ')
end
get_with_partitions() click to toggle source
# File lib/diskman/root_device.rb, line 37
def get_with_partitions
    Dir[@path + '*'].sort.map do |file|
        file =~ %r[/dev/(.*)] && Device.new($1)
    end.sort
end
label() click to toggle source
# File lib/diskman/root_device.rb, line 52
def label
    parts = [get_prop('device/vendor'), get_prop('device/model')]
    parts.reject(&:empty?).join(' ')
end
mount_point() click to toggle source
# File lib/diskman/root_device.rb, line 69
def mount_point
    Mtab.mount_point(@name)
end
mounted?() click to toggle source
# File lib/diskman/root_device.rb, line 65
def mounted?
    Mtab.mounted?(@name)
end
size() click to toggle source
# File lib/diskman/root_device.rb, line 61
def size
    System.bytes2human(size_bytes)
end
size_bytes() click to toggle source
# File lib/diskman/root_device.rb, line 57
def size_bytes
    get_int_prop('size') * get_int_prop('queue/logical_block_size')
end