]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
kernel-install: add helper for logging
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 6 Jul 2022 14:50:24 +0000 (16:50 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 14 Jul 2022 13:20:49 +0000 (22:20 +0900)
src/kernel-install/kernel-install.in

index a597e411336b89f717cade74ad647f389575a3f4..d44ed5030c14b9e20a71b7999811b4a90ec1d0a4 100755 (executable)
@@ -78,6 +78,9 @@ export KERNEL_INSTALL_VERBOSE=0
 if [ "$1" = "--verbose" ] || [ "$1" = "-v" ]; then
     shift
     export KERNEL_INSTALL_VERBOSE=1
+    log_verbose() { printf "%s\n" "$*"; }
+else
+    log_verbose() { :; }
 fi
 
 if [ "${0##*/}" = "installkernel" ]; then
@@ -121,32 +124,27 @@ else
 fi
 
 if [ -f "$install_conf" ]; then
-    [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Reading $install_conf…"
+    log_verbose "Reading $install_conf…"
     # shellcheck source=/dev/null
     . "$install_conf"
 fi
 
-[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && [ -n "$layout" ] && \
-    echo "$install_conf configures layout=$layout"
-[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && [ -n "$initrd_generator" ] && \
-    echo "$install_conf configures initrd_generator=$initrd_generator"
+[ -n "$layout" ] && log_verbose "$install_conf configures layout=$layout"
+[ -n "$initrd_generator" ] && \
+    log_verbose "$install_conf configures initrd_generator=$initrd_generator"
 
 if [ -n "$_MACHINE_ID_SAVED" ]; then
-     MACHINE_ID="$_MACHINE_ID_SAVED"
-     [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-         echo "MACHINE_ID=$MACHINE_ID set via environment"
+    MACHINE_ID="$_MACHINE_ID_SAVED"
+    log_verbose "MACHINE_ID=$MACHINE_ID set via environment"
 else
-    [ -n "$MACHINE_ID" ] && [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-        echo "MACHINE_ID=$MACHINE_ID set via install.conf"
+    [ -n "$MACHINE_ID" ] && log_verbose "MACHINE_ID=$MACHINE_ID set via install.conf"
 fi
 
 if [ -n "$_BOOT_ROOT_SAVED" ]; then
      BOOT_ROOT="$_BOOT_ROOT_SAVED"
-     [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-         echo "BOOT_ROOT=$BOOT_ROOT set via environment"
+     log_verbose "BOOT_ROOT=$BOOT_ROOT set via environment"
 else
-    [ -n "$BOOT_ROOT" ] && [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-        echo "BOOT_ROOT=$BOOT_ROOT set via install.conf"
+    [ -n "$BOOT_ROOT" ] && log_verbose "BOOT_ROOT=$BOOT_ROOT set via install.conf"
 fi
 
 # If /etc/machine-id is initialized we'll use it, otherwise we'll use a freshly
@@ -157,17 +155,17 @@ fi
 if [ -z "$MACHINE_ID" ] && [ -f /etc/machine-info ]; then
     . /etc/machine-info
     MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
-    [ -n "$MACHINE_ID" ] && [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-        echo "machine-id $MACHINE_ID acquired from /etc/machine-info"
+    [ -n "$MACHINE_ID" ] && \
+        log_verbose "machine-id $MACHINE_ID acquired from /etc/machine-info"
 fi
 if [ -z "$MACHINE_ID" ] && [ -f /etc/machine-id ]; then
     read -r MACHINE_ID </etc/machine-id
-    [ -n "$MACHINE_ID" ] && [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-        echo "machine-id $MACHINE_ID acquired from /etc/machine-id"
+    [ -n "$MACHINE_ID" ] && \
+        log_verbose "machine-id $MACHINE_ID acquired from /etc/machine-id"
 fi
 if [ -z "$MACHINE_ID" ]; then
     MACHINE_ID="$(systemd-id128 new)" || exit 1
-    [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "new machine-id $MACHINE_ID generated"
+    log_verbose "new machine-id $MACHINE_ID generated"
 fi
 
 # Now that we determined the machine ID to use, let's determine the "token" for
@@ -179,8 +177,7 @@ ENTRY_TOKEN_FILE="${KERNEL_INSTALL_CONF_ROOT:-/etc/kernel}/entry-token"
 
 if [ -z "$ENTRY_TOKEN" ] && [ -f "$ENTRY_TOKEN_FILE" ]; then
     read -r ENTRY_TOKEN <"$ENTRY_TOKEN_FILE"
-    [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-        echo "entry-token \"$ENTRY_TOKEN\" acquired from $ENTRY_TOKEN_FILE"
+    log_verbose "entry-token \"$ENTRY_TOKEN\" acquired from $ENTRY_TOKEN_FILE"
 fi
 if [ -z "$ENTRY_TOKEN" ]; then
     # If not configured explicitly, then use a few candidates: the machine ID,
@@ -195,7 +192,7 @@ if [ -z "$ENTRY_TOKEN" ]; then
 else
     ENTRY_TOKEN_SEARCH="$ENTRY_TOKEN"
 fi
-[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Entry-token candidates: $ENTRY_TOKEN_SEARCH"
+log_verbose "Entry-token candidates: $ENTRY_TOKEN_SEARCH"
 
 # NB: The $MACHINE_ID is guaranteed to be a valid machine ID, but
 #     $ENTRY_TOKEN can be any string that fits into a VFAT filename, though
@@ -214,8 +211,7 @@ for pref in $BOOT_ROOT_SEARCH; do
             [ -z "$BOOT_ROOT" ] && BOOT_ROOT="$pref"
             [ -z "$ENTRY_TOKEN" ] && ENTRY_TOKEN="$suff"
 
-            [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-                echo "$pref/$suff exists, using BOOT_ROOT=$BOOT_ROOT, ENTRY_TOKEN=$ENTRY_TOKEN"
+            log_verbose "$pref/$suff exists, using BOOT_ROOT=$BOOT_ROOT, ENTRY_TOKEN=$ENTRY_TOKEN"
             break 2
         else
             [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "$pref/$suff not found…"
@@ -223,11 +219,10 @@ for pref in $BOOT_ROOT_SEARCH; do
 
         if [ -d "$pref/loader/entries" ]; then
             [ -z "$BOOT_ROOT" ] && BOOT_ROOT="$pref"
-            [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-                echo "$pref/loader/entries exists, using BOOT_ROOT=$BOOT_ROOT"
+            log_verbose "$pref/loader/entries exists, using BOOT_ROOT=$BOOT_ROOT"
             break 2
         else
-            [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "$pref/loader/entries not found…"
+            log_verbose "$pref/loader/entries not found…"
         fi
     done
 done
@@ -235,11 +230,10 @@ done
 [ -z "$BOOT_ROOT" ] && for pref in "/efi" "/boot/efi"; do
     if mountpoint -q "$pref"; then
         BOOT_ROOT="$pref"
-        [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
-            echo "$pref is a mount point, using BOOT_ROOT=$BOOT_ROOT"
+        log_verbose "$pref is a mount point, using BOOT_ROOT=$BOOT_ROOT"
         break
     else
-        [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "$pref is not a mount point…"
+        log_verbose "$pref is not a mount point…"
     fi
 done
 
@@ -280,18 +274,18 @@ if [ -z "$layout" ]; then
         # the standard boot loader spec, too.
         layout="bls"
 
-        [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "$BOOT_ROOT/$ENTRY_TOKEN exists, using layout=$layout"
+        log_verbose "$BOOT_ROOT/$ENTRY_TOKEN exists, using layout=$layout"
     else
         # There's no metadata in $BOOT_ROOT, and apparently no entry token
         # directory installed? Then we really don't know anything.
         layout="other"
 
-        [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Entry-token directory not found, using layout=$layout"
+        log_verbose "Entry-token directory not found, using layout=$layout"
     fi
 fi
 
 ENTRY_DIR_ABS="$BOOT_ROOT/$ENTRY_TOKEN/$KERNEL_VERSION"
-[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Using ENTRY_DIR_ABS=$ENTRY_DIR_ABS"
+log_verbose "Using ENTRY_DIR_ABS=$ENTRY_DIR_ABS"
 
 # Provide a directory where to store generated initrds
 cleanup() {
@@ -354,7 +348,7 @@ case "$COMMAND" in
         fi
 
         for f in $KERNEL_INSTALL_PLUGINS; do
-            [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "+$f add $KERNEL_VERSION $ENTRY_DIR_ABS" "$@"
+            log_verbose "+$f add $KERNEL_VERSION $ENTRY_DIR_ABS" "$@"
             "$f" add "$KERNEL_VERSION" "$ENTRY_DIR_ABS" "$@"
 
             err=$?
@@ -365,7 +359,7 @@ case "$COMMAND" in
 
     remove)
         for f in $KERNEL_INSTALL_PLUGINS; do
-            [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "+$f remove $KERNEL_VERSION $ENTRY_DIR_ABS"
+            log_verbose "+$f remove $KERNEL_VERSION $ENTRY_DIR_ABS"
             "$f" remove "$KERNEL_VERSION" "$ENTRY_DIR_ABS"
             err=$?
             [ $err -eq $skip_remaining ] && break
@@ -373,7 +367,7 @@ case "$COMMAND" in
         done
 
         if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then
-            [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Removing $ENTRY_DIR_ABS/"
+            log_verbose "Removing $ENTRY_DIR_ABS/"
             rm -rf "$ENTRY_DIR_ABS"
         fi
         ;;