Class: CFA::Grub2::DeviceMap

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/cfa/grub2/device_map.rb

Overview

Represents grub device map in /boot/grub2/device_map for details see www.gnu.org/software/grub/manual/html_node/Device-map.html Main features:

  • Do not overwrite files

  • When setting value first try to just change value if key already exists

  • When grub key is not there, then add to file

  • checks and raise exception if number of mappings exceed limit 8. Limitation is caused by BIOS Int 13 used by grub2 for selecting boot device.

Constant Summary collapse

PATH =
"/boot/grub2/device.map"

Instance Method Summary collapse

Constructor Details

#initialize(file_handler: nil) ⇒ DeviceMap

Returns a new instance of DeviceMap.



23
24
25
26
# File 'lib/cfa/grub2/device_map.rb', line 23

def initialize(file_handler: nil)
  super(AugeasParser.new("device_map.lns"), PATH,
    file_handler: file_handler)
end

Instance Method Details

#add_mapping(grub_device, system_device) ⇒ Object

Note:

if mapping for given grub device is already defined, it will be overwritten

Appends to configuration mapping between grub_device and system_device



50
51
52
# File 'lib/cfa/grub2/device_map.rb', line 50

def add_mapping(grub_device, system_device)
  generic_set(grub_device, system_device)
end

#grub_device_for(system_dev) ⇒ String

Returns grub device name for given system device.

Returns:

  • (String)

    grub device name for given system device



35
36
37
38
39
40
# File 'lib/cfa/grub2/device_map.rb', line 35

def grub_device_for(system_dev)
  matcher = Matcher.new(value_matcher: system_dev)
  entry = data.select(matcher)

  entry.empty? ? nil : entry.first[:key]
end

#grub_devicesArray<String>

Returns list of all grub devices which have mapping. If there is no mapping, then it return empty list.

Returns:

  • (Array<String>)

    list of all grub devices which have mapping. If there is no mapping, then it return empty list.



61
62
63
64
65
66
# File 'lib/cfa/grub2/device_map.rb', line 61

def grub_devices
  matcher = Matcher.new { |k, _v| k !~ /comment/ }
  entries = data.select(matcher)

  entries.map { |e| e[:key] }
end

#remove_mapping(grub_device) ⇒ Object

Removes mapping for given grub device



55
56
57
# File 'lib/cfa/grub2/device_map.rb', line 55

def remove_mapping(grub_device)
  data.delete(grub_device)
end

#saveObject



28
29
30
31
32
# File 'lib/cfa/grub2/device_map.rb', line 28

def save
  raise "Too many grub devices. Limit is 8." if grub_devices.size > 8

  super
end

#system_device_for(grub_device) ⇒ String

Returns system device name for given grub device.

Returns:

  • (String)

    system device name for given grub device



43
44
45
# File 'lib/cfa/grub2/device_map.rb', line 43

def system_device_for(grub_device)
  data[grub_device]
end