Class: Kanrisuru::Core::Disk::Parser::Lsblk

Inherits:
Object
  • Object
show all
Defined in:
lib/kanrisuru/core/disk/parsers/lsblk.rb

Class Method Summary collapse

Class Method Details

.build_lsblk_devices_json(devices) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kanrisuru/core/disk/parsers/lsblk.rb', line 67

def build_lsblk_devices_json(devices)
  devices.map do |device|
    children = (build_lsblk_devices_json(device['children']) if device.key?('children'))

    maj_min = device['maj:min']

    Kanrisuru::Core::Disk::LsblkDevice.new(
      device['name'],
      maj_min ? maj_min.split(':')[0].to_i : nil,
      maj_min ? maj_min.split(':')[1].to_i : nil,
      device['rm'],
      device['ro'],
      device['owner'],
      device['group'],
      device['mode'],
      device['size'],
      device['type'],
      device['mountpoint'],
      device['fstype'],
      device['uuid'],
      children
    )
  end
end

.build_lsblk_devices_text(lines) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kanrisuru/core/disk/parsers/lsblk.rb', line 19

def build_lsblk_devices_text(lines)
  devices = []

  lines.each do |line|
    values = line.split

    current_device = Kanrisuru::Core::Disk::LsblkDevice.new

    values.each do |value|
      key, value = value.split('=')
      value = value.gsub(/"/, '')

      case key
      when 'NAME'
        current_device.name = value
      when 'FSTYPE'
        current_device.fs_type = value
      when 'MAJ:MIN'
        maj, min = value.split(':')
        current_device.maj_dev = maj ? maj.to_i : nil
        current_device.min_dev = min ? min.to_i : nil
      when 'MOUNTPOINT'
        current_device.mount_point = value
      when 'SIZE'
        current_device.fsize = value
      when 'UUID'
        current_device.uuid = value
      when 'RO'
        current_device.readonly_device = value
      when 'RM'
        current_device.removable_device = value
      when 'OWNER'
        current_device.owner = value
      when 'GROUP'
        current_device.group = value
      when 'MODE'
        current_device.mode = value
      when 'TYPE'
        current_device.type = value
      end
    end

    devices << current_device
  end

  devices
end

.parse(command, version) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/kanrisuru/core/disk/parsers/lsblk.rb', line 9

def parse(command, version)
  if version >= 2.27
    result  = command.to_json
    devices = result['blockdevices']
    build_lsblk_devices_json(devices)
  else
    build_lsblk_devices_text(command.to_a)
  end
end