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