Skip to content
Snippets Groups Projects
Forked from pkg / systemd
81 commits behind the upstream repository.
Remove-bashisms-from-the-UEFI-entries-generator.patch 5.04 KiB
From 48877ecd94ff71c03e3d880601fb0ba067f11a35 Mon Sep 17 00:00:00 2001
From: Denis Pynkin <denis.pynkin@collabora.com>
Date: Mon, 3 May 2021 02:28:43 +0300
Subject: Remove bashisms from the UEFI entries generator

- Use [ not [[ and -z to test for non-emptiness
- Use shell lists instead of arrays

Signed-off-by: Denis Pynkin <denis.pynkin@collabora.com>
Signed-off-by: Frederic Dalleau <frederic.dalleau@collabora.com>
---
 src/kernel-install/90-loaderentry.install | 75 +++++++++++++----------
 1 file changed, 42 insertions(+), 33 deletions(-)

diff --git a/src/kernel-install/90-loaderentry.install b/src/kernel-install/90-loaderentry.install
index d096745..12d7b6e 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
 
@@ -8,11 +8,11 @@ ENTRY_DIR_ABS="$3"
 KERNEL_IMAGE="$4"
 INITRD_OPTIONS_START="5"
 
-if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
+if [ -z "$KERNEL_INSTALL_MACHINE_ID" ]; then
     exit 0
 fi
 
-if ! [[ -d "$ENTRY_DIR_ABS" ]]; then
+if ! [ -d "$ENTRY_DIR_ABS" ]; then
     exit 0
 fi
 
@@ -22,48 +22,47 @@ BOOT_ROOT=${ENTRY_DIR_ABS%/$MACHINE_ID/$KERNEL_VERSION}
 BOOT_MNT=$(stat -c %m $BOOT_ROOT)
 ENTRY_DIR=${ENTRY_DIR_ABS#$BOOT_MNT}
 
-if [[ $COMMAND == remove ]]; then
+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
+if ! [ $COMMAND = "add" ]; then
     exit 1
 fi
 
-if ! [[ $KERNEL_IMAGE ]]; then
+if [ -z "$KERNEL_IMAGE" ]; then
     exit 1
 fi
 
-if [[ -f /etc/os-release ]]; then
+if [ -f /etc/os-release ]; then
     . /etc/os-release
-elif [[ -f /usr/lib/os-release ]]; then
+elif [ -f /usr/lib/os-release ]; then
     . /usr/lib/os-release
 fi
 
-if ! [[ $PRETTY_NAME ]]; then
+if [ -z "$PRETTY_NAME" ]; then
     PRETTY_NAME="Linux $KERNEL_VERSION"
 fi
 
-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
-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
+if [ -f /etc/kernel/cmdline ]; then
+    read -r BOOT_OPTIONS < /etc/kernel/cmdline
+elif [ -f /usr/lib/kernel/cmdline ]; then
+    read -r BOOT_OPTIONS < /usr/lib/kernel/cmdline
+elif [ -f "/proc/cmdline" ]; then
+    BOOT_OPTIONS=$(
+        cat /proc/cmdline | tr ' ' '\n' | \
+        while read -r i; do
+            [ "${i#initrd=*}" != "$i" ] && continue
+            echo -n " $i"
+        done
+     )
 fi
 
-if [[ -f /etc/kernel/tries ]]; then
+if [ -f /etc/kernel/tries ]; then
     read -r TRIES </etc/kernel/tries
-    if ! [[ "$TRIES" =~ ^[0-9]+$ ]] ; then
+    if [ -z "${TRIES##*[!0-9]*}" ] ; then
         echo "/etc/kernel/tries does not contain an integer." >&2
         exit 1
     fi
@@ -79,11 +78,20 @@ cp "$KERNEL_IMAGE" "$ENTRY_DIR_ABS/linux" &&
     exit 1
 }
 
-INITRD_OPTIONS=( "${@:${INITRD_OPTIONS_START}}" )
+shift $INITRD_OPTIONS_START
+INITRD_OPTIONS=""
+while [ $# -gt 0 ] ; do
+ if [ -z "$INITRD_OPTIONS" ] ; then
+   INITRD_OPTIONS="$1"
+ else
+   INITRD_OPTIONS="$INITRD_OPTIONS\n$1"
+ fi
+ shift
+done
 
-for initrd in "${INITRD_OPTIONS[@]}"; do
-    if [[ -f "${initrd}" ]]; then
-        initrd_basename="$(basename ${initrd})"
+echo "${INITRD_OPTIONS}" | while read initrd; do
+    if [ -f "${initrd}" ]; then
+        initrd_basename=$(basename "${initrd}")
         [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
             echo "Installing $ENTRY_DIR_ABS/${initrd_basename}"
         cp "${initrd}" "$ENTRY_DIR_ABS/${initrd_basename}" &&
@@ -97,7 +105,7 @@ done
 
 # 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 )
+[ -z "${INITRD_OPTIONS}" ] && INITRD_OPTIONS=initrd
 
 mkdir -p "${LOADER_ENTRY%/*}" || {
     echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
@@ -110,11 +118,12 @@ mkdir -p "${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})"
+    echo "${INITRD_OPTIONS}" | while read initrd; do
+        initrd_basename=$(basename "${initrd}")
+        [ -f "$ENTRY_DIR_ABS/${initrd_basename}" ] && \
+            echo "initrd     $ENTRY_DIR/${initrd_basename}"
     done
     :
 } > "$LOADER_ENTRY" || {
-- 
2.29.3