]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
kernel-install: allow overriding the path to config files
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 1 Jul 2022 08:58:01 +0000 (10:58 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 6 Jul 2022 14:33:11 +0000 (16:33 +0200)
It's pretty hard to write tests without this. I started out by adding separate
variables for each of the files we read, but there's a bunch, and in practice
it's good enough to just override the directory.

man/kernel-install.xml
src/kernel-install/90-loaderentry.install
src/kernel-install/kernel-install.in

index bb22443854b231dfe089f87846f28d135cc84c90..bde30ab16ae2faa079eee36b7138b5df449ef215 100644 (file)
     <refsect2>
       <title>Environment variables understood by <command>kernel-install</command></title>
 
+      <para><varname>$KERNEL_INSTALL_CONF_ROOT</varname> can be set to override the location of the
+      configuration files read by <command>kernel-install</command>. When set,
+      <filename>install.conf</filename>, <filename>entry-token</filename>, and other files will be
+      read from this directory.</para>
+
       <para><varname>$MACHINE_ID</varname> can be set for <command>kernel-install</command> to override
       <varname>$KERNEL_INSTALL_MACHINE_ID</varname>, the machine ID.</para>
 
       <para><varname>$BOOT_ROOT</varname> can be set for <command>kernel-install</command> to override
       <varname>$KERNEL_INSTALL_BOOT_ROOT</varname>, the installation location for boot entries.</para>
 
-      <para>Those variables may also be set in <filename>install.conf</filename>. Variables set in the
+      <para>The last two variables may also be set in <filename>install.conf</filename>. Variables set in the
       environment take precedence over the values specified in the config file.</para>
     </refsect2>
   </refsect1>
         </term>
           <listitem>
             <para>Read by <filename>90-loaderentry.install</filename>. The content of the file
-            <filename>/etc/kernel/cmdline</filename> specifies the kernel command line to use.  If that file does not
-            exist, <filename>/usr/lib/kernel/cmdline</filename> is used.  If that also does not exist,
-            <filename>/proc/cmdline</filename> is used.</para>
+            <filename>/etc/kernel/cmdline</filename> specifies the kernel command line to use. If that file
+            does not exist, <filename>/usr/lib/kernel/cmdline</filename> is used. If that also does not
+            exist, <filename>/proc/cmdline</filename> is used. <varname>$KERNEL_INSTALL_CONF_ROOT</varname>
+            may be used to override the path.</para>
           </listitem>
       </varlistentry>
       <varlistentry>
             <filename>$BOOT/loader/entries/<replaceable>MACHINE-ID</replaceable>-<replaceable>KERNEL-VERSION</replaceable>+<replaceable>TRIES</replaceable>.conf</filename>. This
             is useful for boot loaders such as
             <citerefentry><refentrytitle>systemd-boot</refentrytitle><manvolnum>7</manvolnum></citerefentry> which
-            implement boot attempt counting with a counter embedded in the entry file name.</para>
+            implement boot attempt counting with a counter embedded in the entry file name.
+            <varname>$KERNEL_INSTALL_CONF_ROOT</varname> may be used to override the path.</para>
           </listitem>
       </varlistentry>
       <varlistentry>
         </term>
           <listitem>
             <para>If this file exists it is read and used as "entry token" for this system, i.e. is used for
-            naming Boot Loader Specification entries, see
-            <varname>$KERNEL_INSTALL_ENTRY_TOKEN</varname> above for details.</para>
+            naming Boot Loader Specification entries, see <varname>$KERNEL_INSTALL_ENTRY_TOKEN</varname>
+            above for details. <varname>$KERNEL_INSTALL_CONF_ROOT</varname> may be used to override the
+            path.</para>
           </listitem>
       </varlistentry>
       <varlistentry>
             <citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
             <filename>/etc/kernel/install.conf</filename> will be read if present, and
             <filename>/usr/lib/kernel/install.conf</filename> otherwise. This file is optional.
+            <varname>$KERNEL_INSTALL_CONF_ROOT</varname> may be used to override the path.
             </para>
 
             <para>Currently, the following keys are supported:
index ee5596511006531484fca7d45badabaff06875f4..b700a7b2a603e9bccdd7d75889e0c130b44629df 100644 (file)
@@ -65,7 +65,11 @@ fi
 SORT_KEY="$IMAGE_ID"
 [ -z "$SORT_KEY" ] && SORT_KEY="$ID"
 
-if [ -f /etc/kernel/cmdline ]; then
+if [ -n "$KERNEL_INSTALL_CONF_ROOT" ]; then
+    if [ -f "$KERNEL_INSTALL_CONF_ROOT/cmdline" ]; then
+        BOOT_OPTIONS="$(tr -s "$IFS" ' ' <"$KERNEL_INSTALL_CONF_ROOT/cmdline")"
+    fi
+elif [ -f /etc/kernel/cmdline ]; then
     BOOT_OPTIONS="$(tr -s "$IFS" ' ' </etc/kernel/cmdline)"
 elif [ -f /usr/lib/kernel/cmdline ]; then
     BOOT_OPTIONS="$(tr -s "$IFS" ' ' </usr/lib/kernel/cmdline)"
@@ -83,10 +87,12 @@ if [ "$ENTRY_TOKEN" = "$MACHINE_ID" ]; then
     BOOT_OPTIONS="$BOOT_OPTIONS systemd.machine_id=$MACHINE_ID"
 fi
 
-if [ -f /etc/kernel/tries ]; then
-    read -r TRIES </etc/kernel/tries
+TRIES_FILE="${KERNEL_INSTALL_CONF_ROOT:-/etc/kernel}/tries"
+
+if [ -f "$TRIES_FILE" ]; then
+    read -r TRIES <"$TRIES_FILE"
     if ! echo "$TRIES" | grep -q '^[0-9][0-9]*$'; then
-        echo "/etc/kernel/tries does not contain an integer." >&2
+        echo "$TRIES_FILE does not contain an integer." >&2
         exit 1
     fi
     LOADER_ENTRY="$BOOT_ROOT/loader/entries/$ENTRY_TOKEN-$KERNEL_VERSION+$TRIES.conf"
index 044ba9f6f27a29b3e8e4081253c3527d019fd591..96595fa295ef1ab1643a5f41d17f2642e1f75029 100755 (executable)
@@ -108,7 +108,9 @@ initrd_generator=
 _MACHINE_ID_SAVED="$MACHINE_ID"
 _BOOT_ROOT_SAVED="$BOOT_ROOT"
 
-if [ -f "/etc/kernel/install.conf" ]; then
+if [ -n "$KERNEL_INSTALL_CONF_ROOT" ]; then
+    install_conf="$KERNEL_INSTALL_CONF_ROOT/install.conf"
+elif [ -f "/etc/kernel/install.conf" ]; then
     install_conf="/etc/kernel/install.conf"
 elif [ -f "/usr/lib/kernel/install.conf" ]; then
     install_conf="/usr/lib/kernel/install.conf"
@@ -116,7 +118,7 @@ else
     install_conf=
 fi
 
-if [ -n "$install_conf" ]; then
+if [ -f "$install_conf" ]; then
     [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Reading $install_conf…"
     # shellcheck source=/dev/null
     . "$install_conf"
@@ -171,10 +173,12 @@ fi
 # $BOOT where we want to place the kernel/initrd and related resources, as well
 # for naming the .conf boot loader spec entry. Typically this is just the
 # machine ID, but it can be anything else, too, if we are told so.
-if [ -z "$ENTRY_TOKEN" ] && [ -f /etc/kernel/entry-token ]; then
-    read -r ENTRY_TOKEN </etc/kernel/entry-token
+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 /etc/kernel/entry-token"
+        echo "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,