Class: LinuxAdmin::Disk
- Inherits:
-
Object
- Object
- LinuxAdmin::Disk
- 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
-
#model ⇒ Object
Returns the value of attribute model.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.local ⇒ Object
Collect local disk information via the lsblk command.
Instance Method Summary collapse
- #clear! ⇒ Object
- #create_partition(partition_type, *args) ⇒ Object
- #create_partition_table(type = "msdos") ⇒ Object
- #create_partitions(partition_type, *args) ⇒ Object
- #has_partition_table? ⇒ Boolean
-
#initialize(args = {}) ⇒ Disk
constructor
A new instance of Disk.
- #partition_path(id) ⇒ Object
- #partitions ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Disk
Returns a new instance of Disk.
24 25 26 27 28 |
# File 'lib/linux_admin/disk.rb', line 24 def initialize(args = {}) @path = args[:path] @model = args[:model] || "unknown" @size = args[:size] end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
9 10 11 |
# File 'lib/linux_admin/disk.rb', line 9 def model @model end |
#path ⇒ Object
Returns the value of attribute path.
9 10 11 |
# File 'lib/linux_admin/disk.rb', line 9 def path @path end |
Class Method Details
.local ⇒ Object
Collect local disk information via the lsblk command. Only disks with a size greater than zero are returned.
14 15 16 17 18 19 20 21 22 |
# File 'lib/linux_admin/disk.rb', line 14 def self.local result = Common.run!(Common.cmd("lsblk"), :params => {:b => nil, :d => nil, :n => nil, :p => nil, :o => "NAME,SIZE,TYPE,MODEL"}) result.output.split("\n").collect do |string| path, size, type, *model = string.split if type.casecmp?('disk') && size.to_i > 0 self.new(:path => path, :size => size.to_i, :model => model.join(' ')) end end.compact end |
Instance Method Details
#clear! ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/linux_admin/disk.rb', line 101 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
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 |
# File 'lib/linux_admin/disk.rb', line 61 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) = ('mkpart', '-a', 'opt', partition_type, start, finish) Common.run!(Common.cmd(:parted), :params => {nil => }) 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
51 52 53 |
# File 'lib/linux_admin/disk.rb', line 51 def create_partition_table(type = "msdos") Common.run!(Common.cmd(:parted), :params => {nil => ("mklabel", type)}) end |
#create_partitions(partition_type, *args) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/linux_admin/disk.rb', line 93 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
55 56 57 58 59 |
# File 'lib/linux_admin/disk.rb', line 55 def has_partition_table? result = Common.run(Common.cmd(:parted), :params => {nil => ("print")}) result_indicates_partition_table?(result) end |
#partition_path(id) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/linux_admin/disk.rb', line 112 def partition_path(id) case model when "nvme" "#{path}p#{id}" else "#{path}#{id}" end end |
#partitions ⇒ Object
44 45 46 47 48 49 |
# File 'lib/linux_admin/disk.rb', line 44 def partitions @partitions ||= parted_output.collect { |disk| partition_from_parted(disk) } end |
#size ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/linux_admin/disk.rb', line 30 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 |