From 81355c4124416275504be34803bd3f0094572401 Mon Sep 17 00:00:00 2001
From: Julia Kreger <juliaashleykreger@gmail.com>
Date: Thu, 20 Jun 2024 17:32:25 -0700
Subject: [PATCH] remove console entries when console is disabled

A huge problem with latency sensitive baremetal worklaods
is interrupts being triggered spuriously. Unfortunately
when we start with cloud images, often they default to
logging to a serial console which means every write is
an interrupt, which is far from ideal and can result
in packet loss and service degredation quite quickly.

So instead, if the console logging has been disabled,
and no virtual terminal has been defined, we now strip
the console entries from the resulting image.

In testing with Centos9 Stream, with booting a VM, the console
in this scenario jumps to the framebuffer once started, and
after thousands of lines being written to the console, even with
delays, locally I'm at 219 interrupts, with a bulk of the entries
coming from before I interacted with the console (~135 from just
boot).

Change-Id: Id9b19c4d9804b88e6db05a20e26c8264bb357734
---
 .../elements/bootloader/finalise.d/50-bootloader    | 13 ++++++++++++-
 ...es-from-bootloader-element-f96efa2ab5ab41f6.yaml |  8 ++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 releasenotes/notes/remove-consoles-from-bootloader-element-f96efa2ab5ab41f6.yaml

diff --git a/diskimage_builder/elements/bootloader/finalise.d/50-bootloader b/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
index b9a53801d..3fcc5f68c 100755
--- a/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
+++ b/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
@@ -232,7 +232,7 @@ if [ -n "${EFI_BOOT_DIR:-}" ] && [ -d /boot/efi/$EFI_BOOT_DIR ] ; then
 fi
 
 # Ensure paths in BLS entries account for /boot being a partition or part of the
-# root partition
+# root partition and perform any required entry removal.
 if [[ -e /boot/loader/entries ]]; then
     pushd /boot/loader/entries
     set +e
@@ -250,6 +250,17 @@ if [[ -e /boot/loader/entries ]]; then
     done
     set -e
     popd
+
+    # Since we are already aware we're using BLS, we can go ahead and
+    # do any cleanup to disable embedded serial console settings if they
+    # already exist.
+    if [[ "True" != "${DIB_BOOTLOADER_USE_SERIAL_CONSOLE:-True}" ]] && [[ "${VIRTUAL_TERMINAL}" == "" ]]; then
+        # NOTE(TheJulia): This removes any console arguments and allows grub
+        # to use it's default. It will also remove any embedded consoles in
+        # source images, which may be highly desirable if your running
+        # interrupt sensitive workloads, such as NFV workloads.
+        grubby --update-kernel ALL --remove-args=console
+    fi
     # Print resulting grubby output for debug purposes
     grubby --info=ALL
 fi
diff --git a/releasenotes/notes/remove-consoles-from-bootloader-element-f96efa2ab5ab41f6.yaml b/releasenotes/notes/remove-consoles-from-bootloader-element-f96efa2ab5ab41f6.yaml
new file mode 100644
index 000000000..bf7fd9f20
--- /dev/null
+++ b/releasenotes/notes/remove-consoles-from-bootloader-element-f96efa2ab5ab41f6.yaml
@@ -0,0 +1,8 @@
+---
+features:
+  - |
+    Adds logic to permit the removal of consoles when the bootloader element
+    when no configuration has been expressed which requests it, and serial
+    console is disabled. This involves ``DIB_BOOTLOADER_VIRTUAL_TERMINAL``
+    being set to an empty string and ``DIB_BOOTLOADER_USE_SERIAL_CONSOLE``
+    is set to ``False``.