From e9ed9c7a41bc03cc917a9a73bf3c5420de62ec9e Mon Sep 17 00:00:00 2001
From: Thomas Bellman <bellman@lysator.liu.se>
Date: Tue, 19 Dec 2017 15:21:00 +0100
Subject: [PATCH] Definition for managing kernel commandline options.

This adds a definition bootloader::kernel_option for managing kernel
command line options.  The implementation is currently rudimentary,
and only support removing options when Grub 0.x is used; Grub 2.x or
setting options is not yet implemented.
---
 manifests/grub0/kernel_option.pp | 43 ++++++++++++++++++++++++++++++++
 manifests/kernel_option.pp       | 42 +++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 manifests/grub0/kernel_option.pp
 create mode 100644 manifests/kernel_option.pp

diff --git a/manifests/grub0/kernel_option.pp b/manifests/grub0/kernel_option.pp
new file mode 100644
index 0000000..578442f
--- /dev/null
+++ b/manifests/grub0/kernel_option.pp
@@ -0,0 +1,43 @@
+# Copyright © 2017   Thomas Bellman, Linköping, Sweden
+# Licensed under the GNU LGPL v3+; see the README file for more information.
+
+
+/*
+ * Internal helper for the bootloader::kernel_option definition.
+ */
+define bootloader::grub0::kernel_option($ensure, $value)
+{
+    $prefix = '^(\s*kernel\s(|.*\s))'
+    $suffix = '(|\s.*)$'
+    $qname = regexp_quote($name)
+
+    if ($ensure == 'absent')
+    {
+	regexp_replace_lines {
+	    "bootloader::grub0::kernel_option::${name}":
+		file => '/boot/grub/grub.conf',
+		pattern => "${prefix}${qname}(=\\S*)?${suffix}",
+		# \1 is prefix, \4 is suffix
+		replacement => '\1\4';
+	}
+    }
+    elsif ($ensure != 'present')
+    {
+	fail("Bootloader::Grub0::Kernel_Option[${title}]: ",
+	     "Bad ensure parameter, ${ensure}")
+    }
+    elsif ($value == true or $value == false)
+    {
+	# Detecting if the option is already present is not easily done
+	# using regexps...
+	fail("Bootloader::Grub0::Kernel_option[${title}]: ",
+	     "Setting a kernel option is not yet implemented.")
+    }
+    else
+    {
+	# Detecting if the option is already present is not easily done
+	# using regexps...
+	fail("Bootloader::Grub0::Kernel_option[${title}]: ",
+	     "Setting a kernel option is not yet implemented.")
+    }
+}
diff --git a/manifests/kernel_option.pp b/manifests/kernel_option.pp
new file mode 100644
index 0000000..5c896fb
--- /dev/null
+++ b/manifests/kernel_option.pp
@@ -0,0 +1,42 @@
+# Copyright © 2017   Thomas Bellman, Linköping, Sweden
+# Licensed under the GNU LGPL v3+; see the README file for more information.
+
+
+/*
+ * Set or remove a kernel commandline option.
+ *
+ * Which bootloader is in use will be guessed.  The heuristics for that
+ * are not very good.
+ *
+ * Parameters:
+ *  - name	Kernel commandline option to set or remove.
+ *
+ *  - ensure	One of 'present' or 'absent'.
+ *		Currently only 'absent' is implemented!
+ *
+ *  - value	The value to set the option to.  If set to true or false,
+ *		it will be a "bare" option, i.e. something like "single".
+ *		If a string, a valued option will be set, i.e. something
+ *		like "console=ttyS0".
+ *		Since only ensure => absent is implemented, the value
+ *		parameter is currently ignored.
+ */
+define bootloader::kernel_option($ensure='present', $value=undef)
+{
+    include bootloader::guess
+
+    $bootloader = $bootloader::guess::bootloader
+    case $bootloader
+    {
+	'grub0': {
+	    bootloader::grub0::kernel_option {
+		$title:
+		    name => $name, ensure => $ensure, value => $value;
+	    }
+	}
+	default: {
+	    fail("Bootloader::Kernel_option[${title}]: ",
+		 "Unsupported bootloader, ${bootloader}")
+	}
+    }
+}
-- 
GitLab