class Mool::Disk

Attributes

block_free[RW]
block_used[RW]
devname[RW]
devtype[RW]
file_system[RW]
free_size[RW]
logical_name[RW]
major[RW]
minor[RW]
mount_point[RW]
partitions[RW]
path[RW]
percentage_used[RW]
size[RW]
slaves[RW]
swap[RW]
total_block[RW]
total_size[RW]
unity[RW]
used_size[RW]

Public Class Methods

all() click to toggle source
# File lib/mool/disk.rb, line 238
def self.all
  disks = []

  proc_partitions_hash.each do |disk_name, disk_opts|
    all_partitions = []
    disk = Mool::Disk.new(disk_name, disk_opts[:path])

    disk_opts[:partitions].map do |partition_name, partition_opts|
      partition = Mool::Disk.new(
        partition_name,
        partition_opts[:path]
      )

      all_partitions << partition

      partition.slaves = partition_opts[:slaves].map do |slave_name, slave_opts|
        Mool::Disk.new(
          slave_name,
          slave_opts[:path]
        )
      end
    end

    disk.partitions = all_partitions

    disk.slaves = disk_opts[:slaves].map do |slave_name, slave_opts|
      Mool::Disk.new(
        slave_name,
        slave_opts[:path]
      )
    end

    disks << disk
  end

  disks.each do |disk|
    disk.partitions.each do |partition|
      partition.slaves.each do |slave|
        partition.total_size += slave.total_size
        partition.used_size += slave.used_size
        partition.free_size += slave.free_size
        partition.block_free += slave.block_free
        partition.block_used += slave.block_used
        partition.total_block += slave.total_block
      end
      disk.total_size += partition.total_size
      disk.used_size += partition.used_size
      disk.free_size += partition.free_size
      disk.block_free += partition.block_free
      disk.block_used += partition.block_used
      disk.total_block += partition.total_block
    end
  end

  disks
end
find_especific_devname(obj, key) click to toggle source
# File lib/mool/disk.rb, line 204
def self.find_especific_devname(obj, key)
  if obj.respond_to?(:key?) && obj.key?(key)
    obj[key]
  elsif obj.respond_to?(:each)
    r = nil
    obj.find { |*a| r = find_especific_devname(a.last, key) }
    r
  end
end
find_partitions(disk, all_partitions) click to toggle source
# File lib/mool/disk.rb, line 184
def self.find_partitions(disk, all_partitions)
  parts = Mool::Command.partitions(disk)

  result = {}

  parts.each do |part|
    all_partitions.each do |partition|
      devname = partition[3]
      next unless part.include?(devname)
      all_partitions.delete(partition)
      result[devname] = {
        path: part,
        slaves: find_slaves(part, all_partitions)
      }
    end
  end

  result
end
find_slaves(path, all_partitions) click to toggle source
# File lib/mool/disk.rb, line 167
def self.find_slaves(path, all_partitions)
  slaves = Mool::Command.holders(path)

  result = {}

  slaves.each do |slave|
    all_partitions.each do |partition|
      devname = partition[3]
      next unless slave.include?(devname)
      all_partitions.delete(partition)
      result[devname] = { path: path + "/holders/#{slave}" }
    end
  end

  result
end
new(devname, path = nil) click to toggle source
# File lib/mool/disk.rb, line 24
def initialize(devname, path = nil)
  @unity = Mool::BYTES
  @devname = devname

  @path = if path.nil?
            Mool::Disk.proc_partitions_hash(@devname)[:path]
          else
            path
          end

  raise "Does't exist #{devname} on #{@path}" if @path.nil? || @devname.nil?
  lname = Mool::Command.logical_name(@path)
  @logical_name = lname.blank? ? @devname : lname
  read_uevent
  capacity
end
proc_partitions_hash(especific_devname = nil) click to toggle source
# File lib/mool/disk.rb, line 214
def self.proc_partitions_hash(especific_devname = nil)
  all_partitions = Mool::Command.all_partitions.scan(/(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/)
  hash_disks = {}

  all_partitions.each do |partition|
    if partition[3].include?('ram') || partition[3].include?('sr')
      all_partitions.delete(partition)
      next
    end
    devname = partition[3]
    path = "/sys/block/#{devname}"
    next unless Mool::Command.root_block_device?(devname)
    all_partitions.delete(partition)
    hash_disks[devname] = {
      path: path,
      partitions: find_partitions(devname, all_partitions),
      slaves: find_slaves(path, all_partitions)
    }
  end

  return hash_disks if especific_devname.nil?
  find_especific_devname(hash_disks, especific_devname)
end
swap() click to toggle source
# File lib/mool/disk.rb, line 162
def self.swap
  result = Mool::Command.swap.scan(%r{/.*\n\/dev\/(\S+)/}).flatten.first
  Mool::Disk.new(result) unless result.nil?
end

Public Instance Methods

capacity() click to toggle source
# File lib/mool/disk.rb, line 84
def capacity
  return if defined?(@total_block) && defined?(@block_used) && defined?(@block_free)
  result = Mool::Command.df.scan(
    /(#{@logical_name}|#{@devname})\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/
  ).flatten

  # @total_block = Mool::Command.capacity_partition_command(@path).chomp.to_f
  @total_block = result[1].to_f
  @total_size = result[1].to_f * Mool::BLOCK_SIZE
  @block_used  = result[2].to_f
  @block_free  = result[3].to_f
  @used_size = result[2].to_f * Mool::BLOCK_SIZE
  @free_size = result[3].to_f * Mool::BLOCK_SIZE
  return if result.empty?
  @percentage_used = result[4].delete('%')
end
dev() click to toggle source
# File lib/mool/disk.rb, line 68
def dev
  @major + @minor
end
disk?() click to toggle source
# File lib/mool/disk.rb, line 72
def disk?
  @devtype == 'disk'
end
free_percent() click to toggle source
# File lib/mool/disk.rb, line 126
def free_percent
  @block_free / @total_block
end
mounting_point() click to toggle source
# File lib/mool/disk.rb, line 41
def mounting_point
  @mount_point = nil
  Mool::Command.mount.include?(@devname) &&
    @mount_point ||= Mool::Command.mount.scan(
    /#{@devname} (\S+)/
  ).flatten.first
end
partition?() click to toggle source
# File lib/mool/disk.rb, line 76
def partition?
  @devtype == 'partition'
end
read_uevent() click to toggle source
# File lib/mool/disk.rb, line 58
def read_uevent
  @major,
  @minor,
  @devname,
  @devtype =
  Mool::Command.uevent(@path).split("\n").map do |result|
    result.split('=').last
  end
end
to_b() click to toggle source
# File lib/mool/disk.rb, line 130
def to_b
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::BYTES)
end
to_gb() click to toggle source
# File lib/mool/disk.rb, line 154
def to_gb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::GBYTES)
end
to_kb() click to toggle source
# File lib/mool/disk.rb, line 138
def to_kb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::KBYTES)
end
to_mb() click to toggle source
# File lib/mool/disk.rb, line 146
def to_mb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::MBYTES)
end
used_percent() click to toggle source
# File lib/mool/disk.rb, line 122
def used_percent
  @block_used / @total_block
end