Class: LinuxAdmin::Disk
- Inherits:
-
LinuxAdmin
- Object
- LinuxAdmin
- LinuxAdmin::Disk
- Defined in:
- lib/linux_admin/disk.rb
Constant Summary
Constants inherited from LinuxAdmin
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #clear! ⇒ Object
- #create_partition(partition_type, size) ⇒ Object
-
#initialize(args = {}) ⇒ Disk
constructor
A new instance of Disk.
- #partitions ⇒ Object
- #size ⇒ Object
Methods included from Common
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
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/linux_admin/disk.rb', line 10 def path @path end |
Class Method Details
Instance Method Details
#clear! ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/linux_admin/disk.rb', line 115 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, size) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/linux_admin/disk.rb', line 95 def create_partition(partition_type, size) id, start = partitions.empty? ? [1, 0] : [(partitions.last.id + 1), partitions.last.end_sector] run(cmd(:parted), :params => { nil => [path, 'mkpart', partition_type, start, start + size]}) partition = Partition.new(:disk => self, :id => id, :start_sector => start, :end_sector => start+size, :size => size, :partition_type => partition_type) partitions << partition partition end |
#partitions ⇒ Object
45 46 47 48 49 50 51 52 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 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/linux_admin/disk.rb', line 45 def partitions @partitions ||= begin partitions = [] # requires sudo out = run(cmd(:parted), :return_exitstatus => true, :return_output => true, :params => { nil => [@path, 'print'] }) return [] if out.kind_of?(Fixnum) out.each_line do |l| if l =~ /^ [0-9].*/ p = l.split args = {:disk => self} fields = [:id, :start_sector, :end_sector, :size, :partition_type, :fs_type] fields.each_index do |i| val = p[i] case fields[i] when :start_sector, :end_sector, :size if val =~ /([0-9\.]*)([KMG])B/ val = case $2 when 'K' then $1.to_f.kilobytes when 'M' then $1.to_f.megabytes when 'G' then $1.to_f.gigabytes end end when :id val = val.to_i end args[fields[i]] = val end partitions << Partition.new(args) end end partitions end end |
#size ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/linux_admin/disk.rb', line 22 def size @size ||= begin size = nil out = run(cmd(:fdisk), :return_output => true, :params => {"-l" => nil}) out.each_line { |l| if l =~ /Disk #{path}: ([0-9\.]*) ([KMG])B.*/ size = case $2 when 'K' then $1.to_f.kilobytes when 'M' then $1.to_f.megabytes when 'G' then $1.to_f.gigabytes end break end } size end end |