-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Facter.add(:augeasprovider_grub_blscfg) do | ||
enabled = nil | ||
if File.exist?('/etc/default/grub') | ||
if File.foreach('/etc/default/grub').grep(/GRUB_ENABLE_BLSCFG=/) do |line| | ||
nospace = line.strip | ||
next if nospace.start_with?('#') | ||
nocomments = nospace[/[^#]+/].strip | ||
value = nocomments.split('=', 2).last | ||
enabled = if value.to_s.casecmp('true').zero? | ||
true | ||
else | ||
false | ||
end | ||
end | ||
end | ||
end | ||
|
||
setcode { enabled } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# frozen_string_literal: true | ||
|
||
# GRUB 2 support for kernel parameters, edits /boot/grub2/grubenv for BLSCFG | ||
# | ||
# Licensed under the Apache License, Version 2.0 | ||
|
||
raise('Missing augeasproviders_core dependency') if Puppet::Type.type(:augeasprovider).nil? | ||
|
||
Puppet::Type.type(:kernel_parameter).provide(:grub2, parent: Puppet::Type.type(:augeasprovider).provider(:default)) do | ||
desc "Uses Augeas API to update kernel parameters in GRUB2's /boot/grub2/grubenv for BLSCFG" | ||
|
||
default_file { '/boot/grub2/grubenv' } | ||
|
||
# puppet may not have grubenv lense | ||
lens { 'Simplevars.lns' } | ||
|
||
resource_path do |resource| | ||
"$target/#{section(resource)}/value[.=~regexp('^#{resource[:name]}(=.*)?$')]" | ||
end | ||
|
||
defaultfor osfamily: 'Redhat', operatingsystemmajrelease: ['8'] | ||
defaultfor osname: 'Fedora', operatingsystemmajrelease: ['30'] | ||
|
||
confine feature: :augeas | ||
confine augeasprovider_grub_blscfg: true | ||
defaultfor augeasprovider_grub_blscfg: true | ||
confine exists: '/boot/grub2/grubenv' | ||
|
||
# when both grub* providers match, prefer GRUB 2 | ||
def self.specificity | ||
super + 1 | ||
end | ||
|
||
def self.instances | ||
augopen do |aug| | ||
resources = [] | ||
|
||
# Grubenv.lns : params = aug.match("$target/*[name = 'kernelopts']/value").map do |pp| | ||
params = aug.match('$target/kernelopts').map do |pp| | ||
elements = {} | ||
aug.get(pp).strip.split(' ').each do |ele| | ||
key_val = ele.split('=', 2) | ||
|
||
value = if key_val.length < 2 | ||
nil | ||
else | ||
key_val[1] | ||
end | ||
if elements.key?(key_val[0]) | ||
elements[key_val[0]].push(value) | ||
else | ||
elements[key_val[0]] = [value] | ||
end | ||
end | ||
elements | ||
end[0] | ||
|
||
params.keys.sort.each do |param| | ||
param = { ensure: :present, name: param, value: params[param], bootmode: 'default' } | ||
resources << new(param) | ||
end | ||
resources | ||
end | ||
end | ||
|
||
def self.section(resource) | ||
case resource[:bootmode].to_s | ||
when 'all', 'default', 'normal' | ||
# Can we be sure every kernel is using $kernelopts? | ||
# BLS doesn't have 'recovery' mode | ||
'kernelopts' | ||
else | ||
raise("Unsupported bootmode for #{self.class} provider") | ||
end | ||
end | ||
|
||
def create | ||
# TODO: check does this work? | ||
self.value = (resource[:value]) | ||
end | ||
|
||
def value | ||
# TODO: check does this work? | ||
augopen do |aug| | ||
aug.match('$resource').map do |vp| | ||
aug.get(vp).split('=', 2)[1] | ||
end | ||
end | ||
end | ||
|
||
def value=(newval) | ||
# TODO: check does this work? | ||
augopen! do |aug| | ||
if newval && !newval.empty? | ||
vals = newval.clone | ||
else | ||
# If no value (e.g. "quiet") then clear the value from the first and | ||
# delete the rest | ||
vals = nil | ||
aug.set("#{resource_path}[1]", resource[:name]) | ||
aug.rm("#{resource_path}[position() > 1]") | ||
end | ||
|
||
# Set any existing parameters with this name, remove excess ones | ||
if vals | ||
aug.match('$resource').each do |ppath| | ||
val = vals.shift | ||
if val.nil? | ||
aug.rm(ppath) | ||
else | ||
aug.set(ppath, "#{resource[:name]}=#{val}") | ||
end | ||
end | ||
end | ||
|
||
# Add new parameters where there are more values than existing params | ||
if vals && !vals.empty? | ||
vals.each do |val| | ||
aug.set("$target/#{current_section}/value[last()+1]", "#{resource[:name]}=#{val}") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters