From e96619ce628d72f293d04ebde9090a56da793a40 Mon Sep 17 00:00:00 2001
From: Thomas Bellman <bellman@nsc.liu.se>
Date: Wed, 19 May 2021 18:37:20 +0200
Subject: [PATCH] Allow configuring options to serial console getty.

Add parameter 'options' to the console::serial::login definition,
allowing users to change which options get sent to the agetty process.

The default is intended to be the same as what the operating system
normally does; under systemd we thus pass --keep-baud by default, but
on RHEL-6 we default to no options.  (This is also the same behaviour
as we had before, so users that don't set the 'options' parameter will
not get any changes.)

Signed-off-by: Thomas Bellman <bellman@lysator.liu.se>
---
 manifests/serial/login.pp | 46 ++++++++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 10 deletions(-)

diff --git a/manifests/serial/login.pp b/manifests/serial/login.pp
index 8275dc8..870fba4 100644
--- a/manifests/serial/login.pp
+++ b/manifests/serial/login.pp
@@ -1,4 +1,4 @@
-# Copyright © 2018-2020   Thomas Bellman, Linköping, Sweden
+# Copyright © 2018-2021   Thomas Bellman, Linköping, Sweden
 # Licensed under the GNU LGPL v3+; see the README file for more information.
 
 
@@ -7,6 +7,8 @@
  *
  * Parameters:
  *  - name		Name of serial port device (without /dev/ prefix).
+ *  - getty_options	Options to pass to the getty process.
+ *			The defaults depend on the operating system.
  *  - ensure		One of 'enabled' (the default) or 'disabled'.
  *  - speeds		(List of) speeds in bits/second.
  *  - termtype		Default terminal type connected to the port.
@@ -15,6 +17,7 @@
  */
 define console::serial::login(
 	$ensure='enabled',
+	$getty_options=undef,
 	$speeds=[115200,38400,9600],
 	$termtype='vt100',
 	$rootlogin=undef,
@@ -34,7 +37,9 @@ define console::serial::login(
 	    console::serial::login::systemd {
 		$name:
 		    ensure => $ensure,
-		    speeds => $speeds, termtype => $termtype;
+		    getty_options => $getty_options,
+		    speeds => $speeds,
+		    termtype => $termtype;
 	    }
 	}
 
@@ -42,7 +47,9 @@ define console::serial::login(
 	    console::serial::login::rhel_6 {
 		$name:
 		    ensure => $ensure,
-		    speeds => $speeds, termtype => $termtype;
+		    getty_options => $getty_options,
+		    speeds => $speeds,
+		    termtype => $termtype;
 	    }
 	}
 
@@ -67,11 +74,26 @@ define console::serial::login(
 #
 define console::serial::login::systemd(
 	$ensure,
+	$getty_options=['--keep-baud'],	# This is the systemd default
 	$speeds,
 	$termtype,
 )
 {
     $svcdir = "/etc/systemd/system/serial-getty@${name}.service.d"
+    # Systemd cmdline quoting is not *exactly* like shell, but close enough.
+    $agetty_cmdline = shellquote(
+	# agetty(8) doc says tty name should be before speeds, but default
+	# systemd service definition puts %I after; do it like systemd does.
+	'/sbin/agetty',
+	$getty_options ? { undef => [], default => $getty_options, },
+	inline_template('<%= [@speeds].flatten.join(",") %>'),
+	'%I',
+	$termtype
+    )
+    $svc_params_cfg = (
+	"[Service]\nExecStart=\nExecStart=-${agetty_cmdline}\n"
+    )
+
     file {
 	$svcdir:
 	    ensure => $ensure ? {
@@ -83,11 +105,7 @@ define console::serial::login::systemd(
 	    ensure => $ensure ? {
 		'enabled' => file, 'disabled' => absent,
 	    },
-	    content => inline_template(
-		"[Service]\n",
-		"ExecStart=\n",
-		"ExecStart=-/sbin/agetty --keep-baud <%= [@speeds].flatten.join(',') %> %I <%= @termtype %>\n"
-	    ),
+	    content => $svc_params_cfg,
 	    owner => 'root', group => 'root', mode => '0444',
 	    notify => Service["serial-getty@${name}"];
     }
@@ -110,12 +128,20 @@ define console::serial::login::systemd(
 #
 define console::serial::login::rhel_6(
 	$ensure,
+	$getty_options=[],
 	$speeds,
 	$termtype
 )
 {
     $svcname = "serial-${name}"
-    $xspeeds = inline_template('<%= [@speeds].flatten.join(",") %>')
+    # Upstart uses shell if special characters in command so use shellquote().
+    $agetty_cmdline = shellquote(
+	'/sbin/agetty',
+	$getty_options ? { undef => [], default => $getty_options, },
+	"/dev/${name}",
+	inline_template('<%= [@speeds].flatten.join(",") %>'),
+	$termtype
+    )
 
     if ($ensure == 'enabled')
     {
@@ -125,7 +151,7 @@ define console::serial::login::rhel_6(
 	    'start on stopped rc RUNLEVEL=[2345]',
 	    'stop on starting runlevel [016]',
 	    'respawn',
-	    "exec /sbin/agetty /dev/${name} ${xspeeds} ${termtype}",
+	    "exec ${agetty_cmdline}",
 	]
 	file {
 	    "/etc/init/${svcname}.conf":
-- 
GitLab