Class: LinuxAdmin::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/linux_admin/disk.rb

Constant Summary collapse

PARTED_FIELDS =
[:id, :start_sector, :end_sector,
:size, :partition_type, :fs_type]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Disk

Returns a new instance of Disk.



18
19
20
# File 'lib/linux_admin/disk.rb', line 18

def initialize(args = {})
  @path = args[:path]
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/linux_admin/disk.rb', line 9

def path
  @path
end

Class Method Details

.localObject



11
12
13
14
15
16
# File 'lib/linux_admin/disk.rb', line 11

def self.local
  result = Common.run!(Common.cmd("lsblk"), :params => {:d => nil, :n => nil, :p => nil, :o => "NAME"})
  result.output.split.collect do |d|
    Disk.new :path => d
  end
end

Instance Method Details

#clear!Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/linux_admin/disk.rb', line 93

def clear!
  @partitions = []

  # clear partition table
  Common.run!(Common.cmd(:dd),
      :params => { 'if=' => '/dev/zero', 'of=' => @path,
                   'bs=' => 512, 'count=' => 1})

  self
end

#create_partition(partition_type, *args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/linux_admin/disk.rb', line 53

def create_partition(partition_type, *args)
  create_partition_table unless has_partition_table?

  start = finish = size = nil
  case args.length
  when 1 then
    start  = partitions.empty? ? 0 : partitions.last.end_sector
    size   = args.first
    finish = start + size

  when 2 then
    start  = args[0]
    finish = args[1]

  else
    raise ArgumentError, "must specify start/finish or size"
  end

  id = partitions.empty? ? 1 : (partitions.last.id + 1)
  options = parted_options_array('mkpart', '-a', 'opt', partition_type, start, finish)
  Common.run!(Common.cmd(:parted), :params => {nil => options})

  partition = Partition.new(:disk           => self,
                            :id             => id,
                            :start_sector   => start,
                            :end_sector     => finish,
                            :size           => size,
                            :partition_type => partition_type)
  partitions << partition
  partition
end

#create_partition_table(type = "msdos") ⇒ Object



43
44
45
# File 'lib/linux_admin/disk.rb', line 43

def create_partition_table(type = "msdos")
  Common.run!(Common.cmd(:parted), :params => {nil => parted_options_array("mklabel", type)})
end

#create_partitions(partition_type, *args) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/linux_admin/disk.rb', line 85

def create_partitions(partition_type, *args)
  check_if_partitions_overlap(args)

  args.each { |arg|
    self.create_partition(partition_type, arg[:start], arg[:end])
  }
end

#has_partition_table?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/linux_admin/disk.rb', line 47

def has_partition_table?
  result = Common.run(Common.cmd(:parted), :params => {nil => parted_options_array("print")})

  result_indicates_partition_table?(result)
end

#partitionsObject



36
37
38
39
40
41
# File 'lib/linux_admin/disk.rb', line 36

def partitions
  @partitions ||=
    parted_output.collect { |disk|
      partition_from_parted(disk)
    }
end

#sizeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/linux_admin/disk.rb', line 22

def size
  @size ||= begin
    size = nil
    out = Common.run!(Common.cmd(:fdisk), :params => {"-l" => nil}).output
    out.each_line do |l|
      /Disk #{path}: .*B, (\d+) bytes/.match(l) do |m|
        size = m[1].to_i
        break
      end
    end
    size
  end
end