Class: LinuxAdmin::Disk

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

Constant Summary collapse

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

Constants inherited from LinuxAdmin

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#cmd, #run, #run!

Constructor Details

#initialize(args = {}) ⇒ Disk

Returns a new instance of Disk.



61
62
63
# File 'lib/linux_admin/disk.rb', line 61

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

Instance Attribute Details

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.localObject



55
56
57
58
59
# File 'lib/linux_admin/disk.rb', line 55

def self.local
  Dir.glob(['/dev/[vhs]d[a-z]', '/dev/xvd[a-z]']).collect do |d|
    Disk.new :path => d
  end
end

Instance Method Details

#clear!Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/linux_admin/disk.rb', line 176

def clear!
  @partitions = []

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

  self
end

#create_partition(partition_type, *args) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/linux_admin/disk.rb', line 136

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)
  run!(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



126
127
128
# File 'lib/linux_admin/disk.rb', line 126

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

#create_partitions(partition_type, *args) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/linux_admin/disk.rb', line 168

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)


130
131
132
133
134
# File 'lib/linux_admin/disk.rb', line 130

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

  result_indicates_partition_table?(result)
end

#partitionsObject



79
80
81
82
83
84
# File 'lib/linux_admin/disk.rb', line 79

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

#sizeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/linux_admin/disk.rb', line 65

def size
  @size ||= begin
    size = nil
    out = run!(cmd(:fdisk), :params => {"-l" => nil}).output
    out.each_line { |l|
      if l =~ /Disk #{path}: ([0-9\.]*) ([KMG])B.*/
        size = str_to_bytes($1, $2)
        break
      end
    }
    size
  end
end