teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0037-kernel-install-90-loaderentry-port-to-bin-sh.patch

aa0848
From 96310f2157e896a82de6df260926ac1ec66f65ea Mon Sep 17 00:00:00 2001
aa0848
From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= <nabijaczleweli@nabijaczleweli.xyz>
aa0848
Date: Thu, 16 Dec 2021 14:35:33 +0100
aa0848
Subject: [PATCH] kernel-install: 90-loaderentry: port to /bin/sh
aa0848
aa0848
Also, forward the rm -f exit code on removal instead of swallowing it
aa0848
aa0848
(cherry picked from commit 662f45e3ea9f6e933234b81bec532d584bda6ead)
aa0848
aa0848
Related: #2065061
aa0848
---
aa0848
 src/kernel-install/90-loaderentry.install | 110 +++++++++-------------
aa0848
 1 file changed, 45 insertions(+), 65 deletions(-)
aa0848
aa0848
diff --git a/src/kernel-install/90-loaderentry.install b/src/kernel-install/90-loaderentry.install
aa0848
index 044eced3f0..35324e69a9 100644
aa0848
--- a/src/kernel-install/90-loaderentry.install
aa0848
+++ b/src/kernel-install/90-loaderentry.install
aa0848
@@ -1,4 +1,4 @@
aa0848
-#!/usr/bin/env bash
aa0848
+#!/bin/sh
aa0848
 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
aa0848
 # ex: ts=8 sw=4 sts=4 et filetype=sh
aa0848
 # SPDX-License-Identifier: LGPL-2.1-or-later
aa0848
@@ -22,68 +22,53 @@ COMMAND="$1"
aa0848
 KERNEL_VERSION="$2"
aa0848
 ENTRY_DIR_ABS="$3"
aa0848
 KERNEL_IMAGE="$4"
aa0848
-INITRD_OPTIONS_START="5"
aa0848
+INITRD_OPTIONS_SHIFT=4
aa0848
 
aa0848
-if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
aa0848
-    exit 0
aa0848
-fi
aa0848
-
aa0848
-if [ "$KERNEL_INSTALL_LAYOUT" != "bls" ]; then
aa0848
-    exit 0
aa0848
-fi
aa0848
+[ "$KERNEL_INSTALL_LAYOUT" = "bls" ] || exit 0
aa0848
 
aa0848
 MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
aa0848
 BOOT_ROOT="$KERNEL_INSTALL_BOOT_ROOT"
aa0848
 
aa0848
 BOOT_MNT="$(stat -c %m "$BOOT_ROOT")"
aa0848
-if [[ "$BOOT_MNT" == '/' ]]; then
aa0848
+if [ "$BOOT_MNT" = '/' ]; then
aa0848
     ENTRY_DIR="$ENTRY_DIR_ABS"
aa0848
 else
aa0848
     ENTRY_DIR="${ENTRY_DIR_ABS#$BOOT_MNT}"
aa0848
 fi
aa0848
 
aa0848
-if [[ $COMMAND == remove ]]; then
aa0848
-    rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
aa0848
-    rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
aa0848
-    exit 0
aa0848
-fi
aa0848
-
aa0848
-if ! [[ $COMMAND == add ]]; then
aa0848
-    exit 1
aa0848
-fi
aa0848
-
aa0848
-if ! [[ $KERNEL_IMAGE ]]; then
aa0848
-    exit 1
aa0848
-fi
aa0848
+case "$COMMAND" in
aa0848
+    remove)
aa0848
+        exec rm -f \
aa0848
+            "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf" \
aa0848
+            "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
aa0848
+        ;;
aa0848
+    add)
aa0848
+        ;;
aa0848
+    *)
aa0848
+        exit 1
aa0848
+        ;;
aa0848
+esac
aa0848
 
aa0848
-if [[ -f /etc/os-release ]]; then
aa0848
+if [ -r /etc/os-release ]; then
aa0848
     . /etc/os-release
aa0848
-elif [[ -f /usr/lib/os-release ]]; then
aa0848
+elif [ -r /usr/lib/os-release ]; then
aa0848
     . /usr/lib/os-release
aa0848
 fi
aa0848
 
aa0848
-if ! [[ $PRETTY_NAME ]]; then
aa0848
-    PRETTY_NAME="Linux $KERNEL_VERSION"
aa0848
-fi
aa0848
+[ -n "$PRETTY_NAME" ] || PRETTY_NAME="Linux $KERNEL_VERSION"
aa0848
 
aa0848
-if [[ -f /etc/kernel/cmdline ]]; then
aa0848
-    read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
aa0848
-elif [[ -f /usr/lib/kernel/cmdline ]]; then
aa0848
-    read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
aa0848
+if [ -r /etc/kernel/cmdline ]; then
aa0848
+    BOOT_OPTIONS="$(tr -s "$IFS" ' ' 
aa0848
+elif [ -r /usr/lib/kernel/cmdline ]; then
aa0848
+    BOOT_OPTIONS="$(tr -s "$IFS" ' ' 
aa0848
 else
aa0848
-    declare -a BOOT_OPTIONS
aa0848
-
aa0848
-    read -r -d '' -a line < /proc/cmdline
aa0848
-    for i in "${line[@]}"; do
aa0848
-        [[ "${i#initrd=*}" != "$i" ]] && continue
aa0848
-        [[ "${i#BOOT_IMAGE=*}" != "$i" ]] && continue
aa0848
-        BOOT_OPTIONS+=("$i")
aa0848
-    done
aa0848
+    BOOT_OPTIONS="$(tr -s "$IFS" '\n' 
aa0848
 fi
aa0848
+BOOT_OPTIONS="${BOOT_OPTIONS% }"
aa0848
 
aa0848
-if [[ -f /etc/kernel/tries ]]; then
aa0848
+if [ -r /etc/kernel/tries ]; then
aa0848
     read -r TRIES 
aa0848
-    if ! [[ "$TRIES" =~ ^[0-9]+$ ]] ; then
aa0848
+    if ! echo "$TRIES" | grep -q '^[0-9][0-9]*$'; then
aa0848
         echo "/etc/kernel/tries does not contain an integer." >&2
aa0848
         exit 1
aa0848
     fi
aa0848
@@ -106,43 +91,38 @@ install -g root -o root -m 0644 "$KERNEL_IMAGE" "$ENTRY_DIR_ABS/linux" || {
aa0848
     exit 1
aa0848
 }
aa0848
 
aa0848
-INITRD_OPTIONS=( "${@:${INITRD_OPTIONS_START}}" )
aa0848
-
aa0848
-for initrd in "${INITRD_OPTIONS[@]}"; do
aa0848
-    if [[ -f "${initrd}" ]]; then
aa0848
-        initrd_basename="$(basename ${initrd})"
aa0848
-        [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
aa0848
-            echo "Installing $ENTRY_DIR_ABS/${initrd_basename}"
aa0848
-        install -g root -o root -m 0644 "${initrd}" "$ENTRY_DIR_ABS/${initrd_basename}" || {
aa0848
-            echo "Could not copy '${initrd}' to '$ENTRY_DIR_ABS/${initrd_basename}'." >&2
aa0848
-            exit 1
aa0848
-        }
aa0848
-    fi
aa0848
-done
aa0848
+shift "$INITRD_OPTIONS_SHIFT"
aa0848
+for initrd; do
aa0848
+    [ -f "$initrd" ] || continue
aa0848
 
aa0848
-# If no initrd option is supplied, fall back to "initrd" which is
aa0848
-# the name used by dracut when generating it in its kernel-install hook
aa0848
-[[ ${#INITRD_OPTIONS[@]} == 0 ]] && INITRD_OPTIONS=( initrd )
aa0848
+    initrd_basename="${initrd##*/}"
aa0848
+    [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Installing $ENTRY_DIR_ABS/$initrd_basename"
aa0848
+    install -g root -o root -m 0644 "$initrd" "$ENTRY_DIR_ABS/$initrd_basename" || {
aa0848
+        echo "Could not copy '$initrd' to '$ENTRY_DIR_ABS/$initrd_basename'." >&2
aa0848
+        exit 1
aa0848
+    }
aa0848
+done
aa0848
 
aa0848
 mkdir -p "${LOADER_ENTRY%/*}" || {
aa0848
     echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
aa0848
     exit 1
aa0848
 }
aa0848
 
aa0848
-[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
aa0848
-    echo "Creating $LOADER_ENTRY"
aa0848
+# Try "initrd", generated by dracut in its kernel-install hook, if no initrds were supplied
aa0848
+[ $# -eq 0 ] && set -- "initrd"
aa0848
+
aa0848
+[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Creating $LOADER_ENTRY"
aa0848
 {
aa0848
     echo "title      $PRETTY_NAME"
aa0848
     echo "version    $KERNEL_VERSION"
aa0848
     echo "machine-id $MACHINE_ID"
aa0848
-    echo "options    ${BOOT_OPTIONS[*]}"
aa0848
+    echo "options    $BOOT_OPTIONS"
aa0848
     echo "linux      $ENTRY_DIR/linux"
aa0848
-    for initrd in "${INITRD_OPTIONS[@]}"; do
aa0848
-        [[ -f $ENTRY_DIR_ABS/$(basename ${initrd}) ]] && \
aa0848
-            echo "initrd     $ENTRY_DIR/$(basename ${initrd})"
aa0848
+    for initrd; do
aa0848
+        [ -f "$ENTRY_DIR_ABS/${initrd##*/}" ] && echo "initrd     $ENTRY_DIR/${initrd##*/}"
aa0848
     done
aa0848
     :
aa0848
-} > "$LOADER_ENTRY" || {
aa0848
+} >"$LOADER_ENTRY" || {
aa0848
     echo "Could not create loader entry '$LOADER_ENTRY'." >&2
aa0848
     exit 1
aa0848
 }