Class: CFA::Grub2::Default

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

Overview

Represents grub configuration in /etc/default/grub Main features:

  • Do not overwrite files

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

  • When key is not set, then try to find commented out line with key and replace it with real config

  • When even commented out code is not there, then append configuration to the end of file

Defined Under Namespace

Classes: KernelParams

Constant Summary collapse

PATH =
"/etc/default/grub".freeze
VALID_TERMINAL_OPTIONS =
[:serial, :console, :gfxterm].freeze

Instance Method Summary collapse

Constructor Details

#initialize(file_handler: nil) ⇒ Default

Returns a new instance of Default.



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

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

Instance Method Details

#cryptodiskObject



100
101
102
103
# File 'lib/cfa/grub2/default.rb', line 100

def cryptodisk
  @cryptodisk ||= BooleanValue.new("GRUB_ENABLE_CRYPTODISK", self,
    true_value: "y", false_value: "n")
end

#kernel_paramsObject



65
66
67
68
69
# File 'lib/cfa/grub2/default.rb', line 65

def kernel_params
  @kernel_params ||= KernelParams.new(
    value_for("GRUB_CMDLINE_LINUX_DEFAULT"), "GRUB_CMDLINE_LINUX_DEFAULT"
  )
end

#loadObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/cfa/grub2/default.rb', line 46

def load
  super

  kernels = [kernel_params, xen_hypervisor_params, xen_kernel_params,
             recovery_params]
  kernels.each do |kernel|
    param_line = value_for(kernel.key)
    kernel.replace(param_line) if param_line
  end
end

#os_proberObject



57
58
59
60
61
62
63
# File 'lib/cfa/grub2/default.rb', line 57

def os_prober
  @os_prober ||= BooleanValue.new(
    "GRUB_DISABLE_OS_PROBER", self,
    # grub key is disable, so use reverse logic
    true_value: "false", false_value: "true"
  )
end

#recovery_entryObject



92
93
94
95
96
97
98
# File 'lib/cfa/grub2/default.rb', line 92

def recovery_entry
  @recovery ||= BooleanValue.new(
    "GRUB_DISABLE_RECOVERY", self,
    # grub key is disable, so use reverse logic
    true_value: "false", false_value: "true"
  )
end

#recovery_paramsObject



85
86
87
88
89
90
# File 'lib/cfa/grub2/default.rb', line 85

def recovery_params
  @recovery_params ||= KernelParams.new(
    value_for("GRUB_CMDLINE_LINUX_RECOVERY"),
    "GRUB_CMDLINE_LINUX_RECOVERY"
  )
end

#save(changes_only: false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cfa/grub2/default.rb', line 34

def save(changes_only: false)
  # serialize kernel params object before save
  kernels = [@kernel_params, @xen_hypervisor_params, @xen_kernel_params,
             @recovery_params]
  kernels.each do |params|
    # FIXME: this empty prevent writing explicit empty kernel params.
    generic_set(params.key, params.serialize) if params && !params.empty?
  end

  super
end

#serial_consoleObject



131
132
133
# File 'lib/cfa/grub2/default.rb', line 131

def serial_console
  value_for("GRUB_SERIAL_COMMAND")
end

#serial_console=(value) ⇒ Object



126
127
128
129
# File 'lib/cfa/grub2/default.rb', line 126

def serial_console=(value)
  self.terminal = :serial
  generic_set("GRUB_SERIAL_COMMAND", value)
end

#terminalObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cfa/grub2/default.rb', line 105

def terminal
  value = value_for("GRUB_TERMINAL")
  case value
  when "", nil   then nil
  when "console" then :console
  when "serial"  then :serial
  when "gfxterm" then :gfxterm
  else
    raise "unknown GRUB_TERMINAL option #{value.inspect}"
  end
end

#terminal=(value) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/cfa/grub2/default.rb', line 118

def terminal=(value)
  if !VALID_TERMINAL_OPTIONS.include?(value)
    raise ArgumentError, "invalid value #{value.inspect}"
  end

  generic_set("GRUB_TERMINAL", value.to_s)
end

#xen_hypervisor_paramsObject



71
72
73
74
75
76
# File 'lib/cfa/grub2/default.rb', line 71

def xen_hypervisor_params
  @xen_hypervisor_params ||= KernelParams.new(
    value_for("GRUB_CMDLINE_XEN_DEFAULT"),
    "GRUB_CMDLINE_XEN_DEFAULT"
  )
end

#xen_kernel_paramsObject



78
79
80
81
82
83
# File 'lib/cfa/grub2/default.rb', line 78

def xen_kernel_params
  @xen_kernel_params ||= KernelParams.new(
    value_for("GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT"),
    "GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT"
  )
end