594a6d6
commit 42e04f36f5f8eea0017915bb35e56ee285b394d7
594a6d6
Merge: 7c6296db 940fede7
594a6d6
Author: Johannes Meixner <jsmeix@suse.com>
594a6d6
Date:   Tue Jan 31 13:13:56 2023 +0100
594a6d6
594a6d6
    Merge pull request #2915 from rear/jsmeix-serial-devs-fail-safe
594a6d6
    
594a6d6
    In lib/serial-functions.sh make the
594a6d6
    get_serial_console_devices() function
594a6d6
    fail-safe if no serial device node exists, see
594a6d6
    https://github.com/rear/rear/issues/2914
594a6d6
594a6d6
diff --git a/usr/share/rear/lib/serial-functions.sh b/usr/share/rear/lib/serial-functions.sh
594a6d6
index aac5dc88..3d421b52 100644
594a6d6
--- a/usr/share/rear/lib/serial-functions.sh
594a6d6
+++ b/usr/share/rear/lib/serial-functions.sh
594a6d6
@@ -1,13 +1,24 @@
594a6d6
 
594a6d6
 # Get available serial devices:
594a6d6
 function get_serial_console_devices () {
594a6d6
-    test "$SERIAL_CONSOLE_DEVICES" && echo $SERIAL_CONSOLE_DEVICES || ls /dev/ttyS[0-9]* /dev/hvsi[0-9]* | sort
594a6d6
+    # Via SERIAL_CONSOLE_DEVICES the user specifies which ones to use (and no others):
594a6d6
+    if test "$SERIAL_CONSOLE_DEVICES" ; then
594a6d6
+        echo $SERIAL_CONSOLE_DEVICES
594a6d6
+        return 0
594a6d6
+    fi
594a6d6
+    # Test if there is /dev/ttyS[0-9]* or /dev/hvsi[0-9]*
594a6d6
+    # because when there is neither /dev/ttyS[0-9]* nor /dev/hvsi[0-9]*
594a6d6
+    # the ls command below would become plain 'ls' because of 'nullglob'
594a6d6
+    # cf. "Beware of the emptiness" in https://github.com/rear/rear/wiki/Coding-Style
594a6d6
+    # see https://github.com/rear/rear/issues/2914#issuecomment-1396659184
594a6d6
+    # and return 0 because it is no error when no serial device node exists
594a6d6
+    test "$( echo -n /dev/ttyS[0-9]* /dev/hvsi[0-9]* )" || return 0
594a6d6
     # Use plain 'sort' which results /dev/ttyS0 /dev/ttyS1 /dev/ttyS10 ... /dev/ttyS19 /dev/ttyS2 /dev/ttyS20 ...
594a6d6
     # to get at least /dev/ttyS0 and /dev/ttyS1 before the other /dev/ttyS* devices because
594a6d6
     # we cannot use "sort -V" which would result /dev/ttyS0 /dev/ttyS1 ... /dev/ttyS9 /dev/ttyS10 ...
594a6d6
     # because in older Linux distributions 'sort' does not support '-V' e.g. SLES10 with GNU coreutils 5.93
594a6d6
     # (SLES11 with GNU coreutils 8.12 supports 'sort -V') but if 'sort' fails there is no output at all
594a6d6
-    # cf. "Maintain backward compatibility" at https://github.com/rear/rear/wiki/Coding-Style
594a6d6
+    # cf. "Maintain backward compatibility" in https://github.com/rear/rear/wiki/Coding-Style
594a6d6
     # Furthermore 'sort' results that /dev/hvsi* devices appear before /dev/ttyS* devices
594a6d6
     # so the create_grub2_serial_entry function in lib/bootloader-functions.sh
594a6d6
     # which uses by default the first one and skips the rest will result that
594a6d6
@@ -16,7 +27,8 @@ function get_serial_console_devices () {
594a6d6
     # that have the HVSI driver loaded (a console driver for IBM's p5 servers)
594a6d6
     # cf. https://lwn.net/Articles/98442/
594a6d6
     # and it seems right that when special console drivers are loaded
594a6d6
-    # then their devices should be preferred by default.
594a6d6
+    # then their devices should be preferred by default:
594a6d6
+    ls /dev/ttyS[0-9]* /dev/hvsi[0-9]* | sort
594a6d6
 }
594a6d6
 
594a6d6
 # Get the serial device speed for those device nodes that belong to actual serial devices.
594a6d6
594a6d6
commit 07da02143b5597b202e66c187e53103561018255
594a6d6
Merge: 91942f63 75a23e79
594a6d6
Author: Johannes Meixner <jsmeix@suse.com>
594a6d6
Date:   Fri Oct 20 14:45:23 2023 +0200
594a6d6
594a6d6
    Merge pull request #2961 from rear/jsmeix-copy-serial-console
594a6d6
    
594a6d6
    For the automated serial console setup for the recovery system
594a6d6
    use only the 'console=...' kernel arguments from the original system
594a6d6
    which fixes https://github.com/rear/rear/issues/2843
594a6d6
    and it means that no longer all "real serial devices" get
594a6d6
    auto-enabled as serial consoles in the recovery system, see
594a6d6
    https://github.com/rear/rear/pull/2749#issuecomment-1196650631
594a6d6
    That new default behaviour is described in default.conf.
594a6d6
    In particular prep/GNU/Linux/200_include_serial_console.sh
594a6d6
    and lib/serial-functions.sh were overhauled which results
594a6d6
    that rescue/GNU/Linux/400_use_serial_console.sh is obsolete.
594a6d6
594a6d6
    Cherry-picked-by: Lukáš Zaoral <lzaoral@redhat.com>
594a6d6
594a6d6
diff --git a/usr/share/rear/conf/default.conf b/usr/share/rear/conf/default.conf
594a6d6
index ec1691b6..76355fb8 100644
594a6d6
--- a/usr/share/rear/conf/default.conf
594a6d6
+++ b/usr/share/rear/conf/default.conf
594a6d6
@@ -3320,39 +3320,90 @@ SIMPLIFY_TEAMING=no
594a6d6
 SIMPLIFY_TEAMING=no
594a6d6
 
594a6d6
 # Serial console support for the ReaR rescue/recovery system:
594a6d6
-# By default serial consoles get enabled if serial devices are found and
594a6d6
-# then matching kernel command line parameters like 'console=ttyS0,9600 console=ttyS1,9600'
594a6d6
-# are set when booting the rescue/recovery system (see KERNEL_CMDLINE above).
594a6d6
 # IA64 platforms require it and people need it when no VGA console is available.
594a6d6
-# Specify 'yes' or 'no' or leave it empty to use ReaR's automatism:
594a6d6
+# By default there is serial console support in the ReaR recovery system
594a6d6
+# when 'getty' or 'agetty' and 'stty' can be found,
594a6d6
+# otherwise USE_SERIAL_CONSOLE is automatically set to 'no'.
594a6d6
+# When 'getty' or 'agetty' and 'stty' can be found
594a6d6
+# and there is a 'console=...' option in /proc/cmdline,
594a6d6
+# then USE_SERIAL_CONSOLE is automatically set to 'yes'.
594a6d6
+# With USE_SERIAL_CONSOLE="no" no serial console gets set up,
594a6d6
+# neither for the recovery system kernel nor for the recovery system bootloader.
594a6d6
+# With USE_SERIAL_CONSOLE="yes" plus appropriate SERIAL_CONSOLE_DEVICE... settings
594a6d6
+# serial consoles can be specified for the recovery system kernel and bootloader
594a6d6
+# for example when there is no 'console=...' option in /proc/cmdline
594a6d6
+# or when serial consoles for the recovery system kernel and bootloader
594a6d6
+# should differ from what 'console=...' options in /proc/cmdline tell.
594a6d6
+# By default (when empty) the automatism further described below is used:
594a6d6
 USE_SERIAL_CONSOLE=
594a6d6
-# If you do not want to use all serial devices found as console you can specify the ones to use
594a6d6
-# e.g. SERIAL_CONSOLE_DEVICES="/dev/ttyS0 /dev/ttyS1" provided USE_SERIAL_CONSOLE is turned on.
594a6d6
-# By default (when empty) all serial devices found are used:
594a6d6
+#
594a6d6
+# Devices to be used in general for serial console setup
594a6d6
+# unless explicitly specified via SERIAL_CONSOLE_DEVICES_KERNEL
594a6d6
+# or SERIAL_CONSOLE_DEVICE_SYSLINUX or SERIAL_CONSOLE_DEVICE_GRUB:
594a6d6
+# E.g. SERIAL_CONSOLE_DEVICES="/dev/ttyS0 /dev/ttyS1"
594a6d6
 SERIAL_CONSOLE_DEVICES=
594a6d6
-# Serial consoles for the kernel of the recovery system:
594a6d6
+#
594a6d6
+# Serial consoles for the kernel of the recovery system
594a6d6
+# provided USE_SERIAL_CONSOLE is not 'no':
594a6d6
 # SERIAL_CONSOLE_DEVICES_KERNEL can be device nodes like "/dev/ttyS0 /dev/ttyS1"
594a6d6
 # or 'console=...' kernel parameters like "console=ttyS1,9600 console=tty0" or both like
594a6d6
 # SERIAL_CONSOLE_DEVICES_KERNEL="/dev/ttyS0 console=ttyS1,9600 console=tty0"
594a6d6
-# provided USE_SERIAL_CONSOLE is turned on.
594a6d6
-# By default (when empty) the SERIAL_CONSOLE_DEVICES are used for the kernel:
594a6d6
+# When SERIAL_CONSOLE_DEVICES_KERNEL is empty but SERIAL_CONSOLE_DEVICES is specified
594a6d6
+# then the specified SERIAL_CONSOLE_DEVICES are used for the kernel.
594a6d6
+# By default (when SERIAL_CONSOLE_DEVICES_KERNEL and SERIAL_CONSOLE_DEVICES are empty)
594a6d6
+# serial consoles get enabled for the recovery system kernel via COPY_KERNEL_PARAMETERS
594a6d6
+# for all 'console=...' options that are found in /proc/cmdline.
594a6d6
 SERIAL_CONSOLE_DEVICES_KERNEL=
594a6d6
-# Serial console for SYSLINUX/EXTLINUX when it is used as bootloader for the recovery system:
594a6d6
+#
594a6d6
+# Serial console for SYSLINUX/EXTLINUX when it is used as bootloader for the recovery system
594a6d6
+# provided USE_SERIAL_CONSOLE is 'yes' (automatically with 'console=...' in /proc/cmdline):
594a6d6
 # SYSLINUX supports only one serial device (because the last SYSLINUX 'serial' directive wins).
594a6d6
 # The right /dev/ttyS* can be specified like SERIAL_CONSOLE_DEVICE_SYSLINUX="/dev/ttyS0"
594a6d6
 # or a whole SYSLINUX 'serial' directive can be specified e.g. for /dev/ttyS1 like
594a6d6
 # SERIAL_CONSOLE_DEVICE_SYSLINUX="serial 1 9600"
594a6d6
-# provided USE_SERIAL_CONSOLE is turned on.
594a6d6
-# By default (when empty) the first one of SERIAL_CONSOLE_DEVICES is used for SYSLINUX:
594a6d6
+# When SERIAL_CONSOLE_DEVICE_SYSLINUX is empty but SERIAL_CONSOLE_DEVICES is specified
594a6d6
+# then the first one of SERIAL_CONSOLE_DEVICES is used for SYSLINUX.
594a6d6
+# By default (when SERIAL_CONSOLE_DEVICE_SYSLINUX and SERIAL_CONSOLE_DEVICES are empty)
594a6d6
+# the devices of the 'console=...' options in /proc/cmdline
594a6d6
+# that exist as /dev/ttyS* or /dev/hvsi* character device nodes are used
594a6d6
+# (which excludes /dev/tty0 when there is 'console=tty0' in /proc/cmdline).
594a6d6
 SERIAL_CONSOLE_DEVICE_SYSLINUX=
594a6d6
-# Serial console for GRUB when it is used as bootloader for the recovery system:
594a6d6
+#
594a6d6
+# Serial console for GRUB when it is used as bootloader for the recovery system
594a6d6
+# provided USE_SERIAL_CONSOLE is 'yes' (automatically with 'console=...' in /proc/cmdline):
594a6d6
 # GRUB supports only one serial device (because the last GRUB 'serial' command wins).
594a6d6
 # The right /dev/ttyS* can be specified like SERIAL_CONSOLE_DEVICE_GRUB="/dev/ttyS0"
594a6d6
 # or a whole GRUB 'serial' command can be specified e.g. for /dev/ttyS1 like
594a6d6
 # SERIAL_CONSOLE_DEVICE_GRUB="serial --unit=1 --speed=9600"
594a6d6
-# provided USE_SERIAL_CONSOLE is turned on.
594a6d6
-# By default (when empty) the first one of SERIAL_CONSOLE_DEVICES is used for GRUB:
594a6d6
+# When SERIAL_CONSOLE_DEVICE_GRUB is empty but SERIAL_CONSOLE_DEVICES is specified
594a6d6
+# then the first one of SERIAL_CONSOLE_DEVICES is used for GRUB.
594a6d6
+# By default (when SERIAL_CONSOLE_DEVICE_GRUB and SERIAL_CONSOLE_DEVICES are empty)
594a6d6
+# the devices of the 'console=...' options in /proc/cmdline
594a6d6
+# that exist as /dev/ttyS* or /dev/hvsi* character device nodes are used
594a6d6
+# (which excludes /dev/tty0 when there is 'console=tty0' in /proc/cmdline).
594a6d6
 SERIAL_CONSOLE_DEVICE_GRUB=
594a6d6
+#
594a6d6
+# Examples
594a6d6
+# (provided USE_SERIAL_CONSOLE is not 'no'
594a6d6
+# and 'getty' or 'agetty' and 'stty' can be found):
594a6d6
+#
594a6d6
+# Default behaviour
594a6d6
+# when there is no 'console=...' option in /proc/cmdline
594a6d6
+# then no serial console is set up for the recovery system.
594a6d6
+#
594a6d6
+# Default behaviour when there are for example
594a6d6
+# 'console=ttyS0,9600' and 'console=tty0' in /proc/cmdline
594a6d6
+# then 'console=ttyS0,9600' and 'console=tty0'
594a6d6
+# are used for the recovery system kernel
594a6d6
+# and only /dev/ttyS0 is used as serial console
594a6d6
+# for the recovery system bootloader (SYSLINUX or GRUB).
594a6d6
+#
594a6d6
+# On a headless machine without VGA card but with serial device /dev/ttyS0
594a6d6
+# the kernel would choose /dev/ttyS0 as its console automatically
594a6d6
+# so no 'console=...' kernel option needs to be used and then the automatism described above
594a6d6
+# would not set up a console for the recovery system (in particular not for the bootloader).
594a6d6
+# In this case USE_SERIAL_CONSOLE="yes" and appropriate SERIAL_CONSOLE_DEVICE... settings
594a6d6
+# are needed to manually specify the right console setup for the recovery system.
594a6d6
594a6d6
 # Say "y", "Yes", etc, to enable or "n", "No" etc. to disable the DHCP client protocol or leave empty to autodetect.
594a6d6
 # When enabled, lets the rescue/recovery system run dhclient to get an IP address
594a6d6
 # instead of using the same IP address as the original system:
594a6d6
diff --git a/usr/share/rear/lib/serial-functions.sh b/usr/share/rear/lib/serial-functions.sh
594a6d6
index 3d421b52..a586d357 100644
594a6d6
--- a/usr/share/rear/lib/serial-functions.sh
594a6d6
+++ b/usr/share/rear/lib/serial-functions.sh
594a6d6
@@ -6,29 +6,34 @@ function get_serial_console_devices () {
594a6d6
         echo $SERIAL_CONSOLE_DEVICES
594a6d6
         return 0
594a6d6
     fi
594a6d6
-    # Test if there is /dev/ttyS[0-9]* or /dev/hvsi[0-9]*
594a6d6
-    # because when there is neither /dev/ttyS[0-9]* nor /dev/hvsi[0-9]*
594a6d6
-    # the ls command below would become plain 'ls' because of 'nullglob'
594a6d6
-    # cf. "Beware of the emptiness" in https://github.com/rear/rear/wiki/Coding-Style
594a6d6
-    # see https://github.com/rear/rear/issues/2914#issuecomment-1396659184
594a6d6
-    # and return 0 because it is no error when no serial device node exists
594a6d6
-    test "$( echo -n /dev/ttyS[0-9]* /dev/hvsi[0-9]* )" || return 0
594a6d6
-    # Use plain 'sort' which results /dev/ttyS0 /dev/ttyS1 /dev/ttyS10 ... /dev/ttyS19 /dev/ttyS2 /dev/ttyS20 ...
594a6d6
-    # to get at least /dev/ttyS0 and /dev/ttyS1 before the other /dev/ttyS* devices because
594a6d6
-    # we cannot use "sort -V" which would result /dev/ttyS0 /dev/ttyS1 ... /dev/ttyS9 /dev/ttyS10 ...
594a6d6
-    # because in older Linux distributions 'sort' does not support '-V' e.g. SLES10 with GNU coreutils 5.93
594a6d6
-    # (SLES11 with GNU coreutils 8.12 supports 'sort -V') but if 'sort' fails there is no output at all
594a6d6
-    # cf. "Maintain backward compatibility" in https://github.com/rear/rear/wiki/Coding-Style
594a6d6
-    # Furthermore 'sort' results that /dev/hvsi* devices appear before /dev/ttyS* devices
594a6d6
-    # so the create_grub2_serial_entry function in lib/bootloader-functions.sh
594a6d6
-    # which uses by default the first one and skips the rest will result that
594a6d6
-    # the first /dev/hvsi* device becomes used for the GRUB serial console by default
594a6d6
-    # which looks right because /dev/hvsi* devices should exist only on systems
594a6d6
-    # that have the HVSI driver loaded (a console driver for IBM's p5 servers)
594a6d6
-    # cf. https://lwn.net/Articles/98442/
594a6d6
-    # and it seems right that when special console drivers are loaded
594a6d6
-    # then their devices should be preferred by default:
594a6d6
-    ls /dev/ttyS[0-9]* /dev/hvsi[0-9]* | sort
594a6d6
+    # Scan the kernel command line of the currently running original system
594a6d6
+    # for 'console=<device>[,<options>]' settings e.g. 'console=ttyS1,9600n8 ... console=ttyS3 ... console=tty0'
594a6d6
+    # and extract the specified serial device nodes e.g. ttyS1 -> /dev/ttyS1 ... ttyS3 -> /dev/ttyS3
594a6d6
+    local kernel_option console_option_value console_option_device
594a6d6
+    for kernel_option in $( cat /proc/cmdline ) ; do
594a6d6
+        # Continue with next kernel option when the option name (part before leftmost "=") is not 'console':
594a6d6
+        test "${kernel_option%%=*}" = "console" || continue
594a6d6
+        # Get the console option value (part after leftmost "=") e.g. 'ttyS1,9600n8' 'ttyS3' 'tty0'
594a6d6
+        console_option_value="${kernel_option#*=}"
594a6d6
+        # Get the console option device (part before leftmost optional ',' separator) e.g. 'ttyS1' 'ttyS3' 'tty0'
594a6d6
+        console_option_device="${console_option_value%%,*}"
594a6d6
+        # Continue with next kernel option when the current console option device is no serial device (exclude 'tty0').
594a6d6
+        # The special /dev/hvsi* devices should exist only on systems that have the HVSI driver loaded
594a6d6
+        # (a console driver for IBM's p5 servers) cf. https://lwn.net/Articles/98442/
594a6d6
+        [[ $console_option_device == ttyS* ]] || [[ $console_option_device == hvsi* ]] || continue
594a6d6
+        # Test that the matching serial device node e.g. ttyS1 -> /dev/ttyS1 and ttyS3 -> /dev/ttyS3' exists
594a6d6
+        # to avoid that this automated serial console setup may not work in the ReaR recovery system
594a6d6
+        # when serial device nodes get specified for the recovery system that do not exist
594a6d6
+        # in the currently running original system because the default assumption is
594a6d6
+        # that the replacement system has same hardware as the original system,
594a6d6
+        # cf. https://github.com/rear/rear/pull/2749#issuecomment-1196650631
594a6d6
+        # (if needed the user can specify what he wants via SERIAL_CONSOLE_DEVICES, see above):
594a6d6
+        if ! test -c "/dev/$console_option_device" ; then
594a6d6
+            LogPrintError "Found '$kernel_option' in /proc/cmdline but '/dev/$console_option_device' is no character device"
594a6d6
+            continue
594a6d6
+        fi
594a6d6
+        echo /dev/$console_option_device
594a6d6
+    done
594a6d6
 }
594a6d6
 
594a6d6
 # Get the serial device speed for those device nodes that belong to actual serial devices.
594a6d6
@@ -39,64 +44,15 @@ function get_serial_device_speed () {
594a6d6
     # Run it in a subshell so that 'set -o pipefail' does not affect the current shell and
594a6d6
     # it can run in a subshell because the caller of this function only needs its stdout
594a6d6
     # cf. the function get_root_disk_UUID in lib/bootloader-functions.sh
594a6d6
-    # so when stty fails the get_serial_device_speed return code is the stty exit code and not the awk exit code
594a6d6
-    # therefore one can call get_serial_device_speed with error checking for example like
594a6d6
+    # so when stty fails the get_serial_device_speed return code is the stty exit code and not the awk exit code.
594a6d6
+    # Therefore one can call get_serial_device_speed with error checking for example like
594a6d6
     # speed=$( get_serial_device_speed $serial_device ) && COMMAND_WITH_speed || COMMAND_WITHOUT_speed
594a6d6
-    # because the return code of variable=$( PIPE ) is the return code of the pipe,
594a6d6
-    # cf. how get_serial_device_speed is called in cmdline_add_console below.
594a6d6
+    # because the return code of variable=$( PIPE ) is the return code of the pipe
594a6d6
+    # cf. how get_serial_device_speed is called in lib/bootloader-functions.sh
594a6d6
+    # and output/USB/Linux-i386/300_create_extlinux.sh
594a6d6
     # Suppress stty stderr output because for most /dev/ttyS* device nodes the result is
594a6d6
     #   stty: /dev/ttyS...: Input/output error
594a6d6
     # when the device node does not belong to an actual serial device (i.e. to real serial hardware)
594a6d6
     # so get_serial_device_speed is also used to get those device nodes that belong to real serial devices:
594a6d6
     ( set -o pipefail ; stty -F $devnode 2>/dev/null | awk '/^speed / { print $2 }' )
594a6d6
 }
594a6d6
-
594a6d6
-# Add serial console to kernel cmdline:
594a6d6
-function cmdline_add_console {
594a6d6
-    # Nothing to do when using serial console is not wanted:
594a6d6
-    is_true "$USE_SERIAL_CONSOLE" || return 0
594a6d6
-
594a6d6
-    # Strip existing 'console=...' kernel cmd parameters:
594a6d6
-    local param cmdline=""
594a6d6
-    for param in $KERNEL_CMDLINE ; do
594a6d6
-        case "$param" in
594a6d6
-            (console=*) ;;
594a6d6
-            (*) cmdline+=" $param";;
594a6d6
-        esac
594a6d6
-    done
594a6d6
-
594a6d6
-    # Add serial console config to kernel cmd line:
594a6d6
-    local devnode speed=""
594a6d6
-    if test "$SERIAL_CONSOLE_DEVICES_KERNEL" ; then
594a6d6
-        # When the user has specified SERIAL_CONSOLE_DEVICES_KERNEL use only that (no automatisms):
594a6d6
-        for devnode in $SERIAL_CONSOLE_DEVICES_KERNEL ; do
594a6d6
-            # devnode can be a character device node like "/dev/ttyS0" or "/dev/lp0" or "/dev/ttyUSB0"
594a6d6
-            # cf. https://www.kernel.org/doc/html/latest/admin-guide/serial-console.html
594a6d6
-            # or devnode can be a 'console=...' kernel cmd parameter like "console=ttyS1,9600"
594a6d6
-            if test -c "$devnode" ; then
594a6d6
-                if speed=$( get_serial_device_speed $devnode ) ; then
594a6d6
-                    cmdline+=" console=${devnode##/dev/},$speed"
594a6d6
-                else
594a6d6
-                    cmdline+=" console=${devnode##/dev/}"
594a6d6
-                fi
594a6d6
-            else
594a6d6
-                # When devnode is a 'console=...' kernel cmd parameter use it as specified:
594a6d6
-                cmdline+=" $devnode"
594a6d6
-            fi
594a6d6
-        done
594a6d6
-    else
594a6d6
-        local real_consoles=""
594a6d6
-        for devnode in $( get_serial_console_devices ) ; do
594a6d6
-            # Only add for those device nodes that belong to actual serial devices:
594a6d6
-            speed=$( get_serial_device_speed $devnode ) && real_consoles+=" console=${devnode##/dev/},$speed"
594a6d6
-        done
594a6d6
-        cmdline+=" $real_consoles"
594a6d6
-
594a6d6
-        # Add fallback console if no real serial device was found:
594a6d6
-        test "$real_consoles" || cmdline+=" console=tty0"
594a6d6
-    fi
594a6d6
-
594a6d6
-    # Have a trailing space to be on the safe side
594a6d6
-    # so that more kernel cmd parameters could be "just appended" by other scripts:
594a6d6
-    echo "$cmdline "
594a6d6
-}
594a6d6
diff --git a/usr/share/rear/prep/GNU/Linux/200_include_serial_console.sh b/usr/share/rear/prep/GNU/Linux/200_include_serial_console.sh
594a6d6
index 214af3c4..14ec113e 100644
594a6d6
--- a/usr/share/rear/prep/GNU/Linux/200_include_serial_console.sh
594a6d6
+++ b/usr/share/rear/prep/GNU/Linux/200_include_serial_console.sh
594a6d6
@@ -1,31 +1,115 @@
594a6d6
 
594a6d6
-# If possible auto-enable serial console when not specified:
594a6d6
-if [[ -z "$USE_SERIAL_CONSOLE" ]] ; then
594a6d6
-    local devnode speed=""
594a6d6
-    for devnode in $( get_serial_console_devices ) ; do
594a6d6
-        # Enable serial console when there is at least one real serial device:
594a6d6
-        if speed=$( get_serial_device_speed $devnode ) ; then
594a6d6
-            USE_SERIAL_CONSOLE="yes"
594a6d6
-            break
594a6d6
-        fi
594a6d6
-    done
594a6d6
-fi
594a6d6
+# This script prep/GNU/Linux/200_include_serial_console.sh
594a6d6
+# is the first ...console... script that is run for "rear mkrescue/mkbackup".
594a6d6
+#
594a6d6
+# When USE_SERIAL_CONSOLE is empty then this script decides
594a6d6
+# if USE_SERIAL_CONSOLE is kept empty
594a6d6
+# or (provided there is sufficient reason)
594a6d6
+# it sets USE_SERIAL_CONSOLE to 'no' or 'yes'.
594a6d6
+# It sets USE_SERIAL_CONSOLE to 'no'
594a6d6
+# when serial consoles cannot work in the recovery system.
594a6d6
+# It sets USE_SERIAL_CONSOLE to 'yes'
594a6d6
+# when a serial console will be set up for the recovery system kernel.
594a6d6
+# Only when a serial console will be actually set up for the recovery system kernel,
594a6d6
+# then it makes sense to also set up a serial console for the recovery system bootloader.
594a6d6
+# So serial console setup for the recovery system bootloader is only done if USE_SERIAL_CONSOLE="yes"
594a6d6
+# because an empty USE_SERIAL_CONSOLE must not result serial console setup for the recovery system bootloader
594a6d6
+# (without actual serial console setup for the recovery system kernel).
594a6d6
 
594a6d6
-# Always include getty or agetty as we don't know in advance whether they are needed
594a6d6
+# Always try to include getty or agetty as we do not know in advance whether they are needed
594a6d6
 # (the user may boot the recovery system with manually specified kernel options
594a6d6
 # to get serial console support in his recovery system).
594a6d6
-# For serial support we need to include the agetty binary,
594a6d6
-# but Debian distro's use getty instead of agetty:
594a6d6
-local getty_binary=""
594a6d6
+# For serial console support we need to include 'getty' or 'agetty'.
594a6d6
+# Debian distributions (in particular Ubuntu) use 'getty'.
594a6d6
+# Fedora, RHEL, SLES,... use 'agetty'.
594a6d6
 if has_binary getty ; then
594a6d6
-    # Debian, Ubuntu,...
594a6d6
-    getty_binary="getty"
594a6d6
+    PROGS+=( getty )
594a6d6
 elif has_binary agetty ; then
594a6d6
-    # Fedora, RHEL, SLES,...
594a6d6
-    getty_binary="agetty"
594a6d6
+    PROGS+=( agetty )
594a6d6
+else
594a6d6
+    is_true "$USE_SERIAL_CONSOLE" && Error "Failed to find 'getty' or 'agetty' (USE_SERIAL_CONSOLE is 'true')"
594a6d6
+    LogPrintError "No serial console support (failed to find 'getty' or 'agetty')"
594a6d6
+    USE_SERIAL_CONSOLE="no"
594a6d6
+fi
594a6d6
+
594a6d6
+# Also try to include 'stty' which is (currently) only needed for serial console support
594a6d6
+# in skel/default/etc/scripts/system-setup.d/45-serial-console.sh
594a6d6
+# and lib/serial-functions.sh
594a6d6
+if has_binary stty ; then
594a6d6
+    PROGS+=( stty )
594a6d6
 else
594a6d6
-    # The user must have the programs in REQUIRED_PROGS installed on his system:
594a6d6
-    Error "Failed to find 'getty' or 'agetty' for serial console"
594a6d6
+    is_true "$USE_SERIAL_CONSOLE" && Error "Failed to find 'stty' (USE_SERIAL_CONSOLE is 'true')"
594a6d6
+    LogPrintError "No serial console support (failed to find 'stty')"
594a6d6
+    USE_SERIAL_CONSOLE="no"
594a6d6
 fi
594a6d6
 
594a6d6
-REQUIRED_PROGS+=( "$getty_binary" stty )
594a6d6
+# Auto-enable serial console support for the recovery system
594a6d6
+# provided console support is not impossible because there is no getty or agetty and stty
594a6d6
+# and unless the user specified to not have serial console support:
594a6d6
+is_false "$USE_SERIAL_CONSOLE" && return 0
594a6d6
+
594a6d6
+# When the user has specified SERIAL_CONSOLE_DEVICES_KERNEL use only that,
594a6d6
+# otherwise use SERIAL_CONSOLE_DEVICES if the user has specified it:
594a6d6
+local serial_console_devices=""
594a6d6
+test "$SERIAL_CONSOLE_DEVICES" && serial_console_devices="$SERIAL_CONSOLE_DEVICES"
594a6d6
+test "$SERIAL_CONSOLE_DEVICES_KERNEL" && serial_console_devices="$SERIAL_CONSOLE_DEVICES_KERNEL"
594a6d6
+if test "$serial_console_devices" ; then
594a6d6
+    local serial_console speed="" cmdline_add_console=""
594a6d6
+    for serial_console in $serial_console_devices ; do
594a6d6
+        # serial_console can be a character device node like "/dev/ttyS0" or "/dev/lp0" or "/dev/ttyUSB0"
594a6d6
+        # cf. https://www.kernel.org/doc/html/latest/admin-guide/serial-console.html
594a6d6
+        # or serial_console can be a 'console=...' kernel cmd parameter like "console=ttyS1,9600"
594a6d6
+        if test -c "$serial_console" ; then
594a6d6
+            if speed=$( get_serial_device_speed $serial_console ) ; then
594a6d6
+                cmdline_add_console+=" console=${serial_console##/dev/},$speed"
594a6d6
+            else
594a6d6
+                cmdline_add_console+=" console=${serial_console##/dev/}"
594a6d6
+            fi
594a6d6
+        else
594a6d6
+            # When serial_console is not a character device
594a6d6
+            # it should be a 'console=...' kernel cmd parameter
594a6d6
+            # that is used as specified ("final power to the user"):
594a6d6
+            cmdline_add_console+=" $serial_console"
594a6d6
+        fi
594a6d6
+    done
594a6d6
+    if test "$cmdline_add_console" ; then
594a6d6
+        KERNEL_CMDLINE+="$cmdline_add_console"
594a6d6
+        DebugPrint "Appended '$cmdline_add_console' to KERNEL_CMDLINE"
594a6d6
+        USE_SERIAL_CONSOLE="yes"
594a6d6
+        # No further automatisms when a 'console=...' kernel cmd parameter was set
594a6d6
+        # via SERIAL_CONSOLE_DEVICES_KERNEL or SERIAL_CONSOLE_DEVICES:
594a6d6
+        return
594a6d6
+    fi
594a6d6
+    LogPrintError "SERIAL_CONSOLE_DEVICES_KERNEL or SERIAL_CONSOLE_DEVICES specified but none is a character device"
594a6d6
+fi
594a6d6
+
594a6d6
+# Auto-enable serial console support for the recovery system kernel:
594a6d6
+# The below auto-enable serial console support for the recovery system kernel
594a6d6
+# does not auto-enable serial console support for the recovery system bootloader.
594a6d6
+# Currently auto-enable serial console support for the recovery system bootloader
594a6d6
+# happens for the first real serial device from get_serial_console_devices()
594a6d6
+# in lib/bootloader-functions.sh in make_syslinux_config() and create_grub2_cfg() 
594a6d6
+# and in output/USB/Linux-i386/300_create_extlinux.sh
594a6d6
+# The auto-enable serial console support for the recovery system bootloader should be
594a6d6
+# auto-aligned with the auto-enable serial console support for the recovery system kernel.
594a6d6
+# Things are auto-aligned when the first 'console=...' device in /proc/cmdline
594a6d6
+# is also the first real serial device from get_serial_console_devices().
594a6d6
+# When current auto-alignment does not result what the user needs, what is needed can be specified
594a6d6
+# via SERIAL_CONSOLE_DEVICES_KERNEL and SERIAL_CONSOLE_DEVICE_SYSLINUX or SERIAL_CONSOLE_DEVICE_GRUB.
594a6d6
+
594a6d6
+# Scan the kernel command line of the currently running original system
594a6d6
+# and auto-enable serial console for the recovery system kernel
594a6d6
+# only if there is at least one 'console=...' option:
594a6d6
+local kernel_option
594a6d6
+for kernel_option in $( cat /proc/cmdline ) ; do
594a6d6
+    # Get the kernel option name (part before leftmost "="):
594a6d6
+    if test "${kernel_option%%=*}" = "console" ; then
594a6d6
+        USE_SERIAL_CONSOLE="yes"
594a6d6
+        # Get all 'console=...' kernel command line options
594a6d6
+        # copied from the currently running original system
594a6d6
+        # via rescue/GNU/Linux/290_kernel_cmdline.sh that runs later:
594a6d6
+        COPY_KERNEL_PARAMETERS+=( console )
594a6d6
+        return
594a6d6
+    fi
594a6d6
+done
594a6d6
+DebugPrint "No 'console=...' setting for recovery system kernel (none in /proc/cmdline)"
594a6d6
diff --git a/usr/share/rear/rescue/GNU/Linux/400_use_serial_console.sh b/usr/share/rear/rescue/GNU/Linux/400_use_serial_console.sh
594a6d6
deleted file mode 100644
594a6d6
index fc935844..00000000
594a6d6
--- a/usr/share/rear/rescue/GNU/Linux/400_use_serial_console.sh
594a6d6
+++ /dev/null
594a6d6
@@ -1,6 +0,0 @@
594a6d6
-
594a6d6
-# Nothing to do when using serial console is not wanted:
594a6d6
-is_true "$USE_SERIAL_CONSOLE" || return 0
594a6d6
-
594a6d6
-KERNEL_CMDLINE=$( cmdline_add_console )
594a6d6
-Log "Modified kernel commandline to: '$KERNEL_CMDLINE'"