richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
Blob Blame History Raw
From 96310f2157e896a82de6df260926ac1ec66f65ea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= <nabijaczleweli@nabijaczleweli.xyz>
Date: Thu, 16 Dec 2021 14:35:33 +0100
Subject: [PATCH] kernel-install: 90-loaderentry: port to /bin/sh

Also, forward the rm -f exit code on removal instead of swallowing it

(cherry picked from commit 662f45e3ea9f6e933234b81bec532d584bda6ead)

Related: #2065061
---
 src/kernel-install/90-loaderentry.install | 110 +++++++++-------------
 1 file changed, 45 insertions(+), 65 deletions(-)

diff --git a/src/kernel-install/90-loaderentry.install b/src/kernel-install/90-loaderentry.install
index 044eced3f0..35324e69a9 100644
--- a/src/kernel-install/90-loaderentry.install
+++ b/src/kernel-install/90-loaderentry.install
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+#!/bin/sh
 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
 # ex: ts=8 sw=4 sts=4 et filetype=sh
 # SPDX-License-Identifier: LGPL-2.1-or-later
@@ -22,68 +22,53 @@ COMMAND="$1"
 KERNEL_VERSION="$2"
 ENTRY_DIR_ABS="$3"
 KERNEL_IMAGE="$4"
-INITRD_OPTIONS_START="5"
+INITRD_OPTIONS_SHIFT=4
 
-if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
-    exit 0
-fi
-
-if [ "$KERNEL_INSTALL_LAYOUT" != "bls" ]; then
-    exit 0
-fi
+[ "$KERNEL_INSTALL_LAYOUT" = "bls" ] || exit 0
 
 MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
 BOOT_ROOT="$KERNEL_INSTALL_BOOT_ROOT"
 
 BOOT_MNT="$(stat -c %m "$BOOT_ROOT")"
-if [[ "$BOOT_MNT" == '/' ]]; then
+if [ "$BOOT_MNT" = '/' ]; then
     ENTRY_DIR="$ENTRY_DIR_ABS"
 else
     ENTRY_DIR="${ENTRY_DIR_ABS#$BOOT_MNT}"
 fi
 
-if [[ $COMMAND == remove ]]; then
-    rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
-    rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
-    exit 0
-fi
-
-if ! [[ $COMMAND == add ]]; then
-    exit 1
-fi
-
-if ! [[ $KERNEL_IMAGE ]]; then
-    exit 1
-fi
+case "$COMMAND" in
+    remove)
+        exec rm -f \
+            "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf" \
+            "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
+        ;;
+    add)
+        ;;
+    *)
+        exit 1
+        ;;
+esac
 
-if [[ -f /etc/os-release ]]; then
+if [ -r /etc/os-release ]; then
     . /etc/os-release
-elif [[ -f /usr/lib/os-release ]]; then
+elif [ -r /usr/lib/os-release ]; then
     . /usr/lib/os-release
 fi
 
-if ! [[ $PRETTY_NAME ]]; then
-    PRETTY_NAME="Linux $KERNEL_VERSION"
-fi
+[ -n "$PRETTY_NAME" ] || PRETTY_NAME="Linux $KERNEL_VERSION"
 
-if [[ -f /etc/kernel/cmdline ]]; then
-    read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
-elif [[ -f /usr/lib/kernel/cmdline ]]; then
-    read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
+if [ -r /etc/kernel/cmdline ]; then
+    BOOT_OPTIONS="$(tr -s "$IFS" ' ' </etc/kernel/cmdline)"
+elif [ -r /usr/lib/kernel/cmdline ]; then
+    BOOT_OPTIONS="$(tr -s "$IFS" ' ' </usr/lib/kernel/cmdline)"
 else
-    declare -a BOOT_OPTIONS
-
-    read -r -d '' -a line < /proc/cmdline
-    for i in "${line[@]}"; do
-        [[ "${i#initrd=*}" != "$i" ]] && continue
-        [[ "${i#BOOT_IMAGE=*}" != "$i" ]] && continue
-        BOOT_OPTIONS+=("$i")
-    done
+    BOOT_OPTIONS="$(tr -s "$IFS" '\n' </proc/cmdline | grep -ve '^BOOT_IMAGE=' -e '^initrd=' | tr '\n' ' ')"
 fi
+BOOT_OPTIONS="${BOOT_OPTIONS% }"
 
-if [[ -f /etc/kernel/tries ]]; then
+if [ -r /etc/kernel/tries ]; then
     read -r TRIES </etc/kernel/tries
-    if ! [[ "$TRIES" =~ ^[0-9]+$ ]] ; then
+    if ! echo "$TRIES" | grep -q '^[0-9][0-9]*$'; then
         echo "/etc/kernel/tries does not contain an integer." >&2
         exit 1
     fi
@@ -106,43 +91,38 @@ install -g root -o root -m 0644 "$KERNEL_IMAGE" "$ENTRY_DIR_ABS/linux" || {
     exit 1
 }
 
-INITRD_OPTIONS=( "${@:${INITRD_OPTIONS_START}}" )
-
-for initrd in "${INITRD_OPTIONS[@]}"; do
-    if [[ -f "${initrd}" ]]; then
-        initrd_basename="$(basename ${initrd})"
-        [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-            echo "Installing $ENTRY_DIR_ABS/${initrd_basename}"
-        install -g root -o root -m 0644 "${initrd}" "$ENTRY_DIR_ABS/${initrd_basename}" || {
-            echo "Could not copy '${initrd}' to '$ENTRY_DIR_ABS/${initrd_basename}'." >&2
-            exit 1
-        }
-    fi
-done
+shift "$INITRD_OPTIONS_SHIFT"
+for initrd; do
+    [ -f "$initrd" ] || continue
 
-# If no initrd option is supplied, fall back to "initrd" which is
-# the name used by dracut when generating it in its kernel-install hook
-[[ ${#INITRD_OPTIONS[@]} == 0 ]] && INITRD_OPTIONS=( initrd )
+    initrd_basename="${initrd##*/}"
+    [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Installing $ENTRY_DIR_ABS/$initrd_basename"
+    install -g root -o root -m 0644 "$initrd" "$ENTRY_DIR_ABS/$initrd_basename" || {
+        echo "Could not copy '$initrd' to '$ENTRY_DIR_ABS/$initrd_basename'." >&2
+        exit 1
+    }
+done
 
 mkdir -p "${LOADER_ENTRY%/*}" || {
     echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
     exit 1
 }
 
-[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-    echo "Creating $LOADER_ENTRY"
+# Try "initrd", generated by dracut in its kernel-install hook, if no initrds were supplied
+[ $# -eq 0 ] && set -- "initrd"
+
+[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Creating $LOADER_ENTRY"
 {
     echo "title      $PRETTY_NAME"
     echo "version    $KERNEL_VERSION"
     echo "machine-id $MACHINE_ID"
-    echo "options    ${BOOT_OPTIONS[*]}"
+    echo "options    $BOOT_OPTIONS"
     echo "linux      $ENTRY_DIR/linux"
-    for initrd in "${INITRD_OPTIONS[@]}"; do
-        [[ -f $ENTRY_DIR_ABS/$(basename ${initrd}) ]] && \
-            echo "initrd     $ENTRY_DIR/$(basename ${initrd})"
+    for initrd; do
+        [ -f "$ENTRY_DIR_ABS/${initrd##*/}" ] && echo "initrd     $ENTRY_DIR/${initrd##*/}"
     done
     :
-} > "$LOADER_ENTRY" || {
+} >"$LOADER_ENTRY" || {
     echo "Could not create loader entry '$LOADER_ENTRY'." >&2
     exit 1
 }