#!/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 # # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # systemd is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see . skip_remaining=77 set -e usage() { echo "Usage:" echo " kernel-install [OPTIONS...] add KERNEL-VERSION KERNEL-IMAGE [INITRD-FILE...]" echo " kernel-install [OPTIONS...] remove KERNEL-VERSION" echo " kernel-install [OPTIONS...] inspect" echo "Options:" echo " -h, --help Print this help and exit" echo " --version Print version string and exit" echo " -v, --verbose Increase verbosity" } dropindirs_sort() { suffix="$1" shift for d; do for i in "$d/"*"$suffix"; do [ -e "$i" ] && echo "${i##*/}" done done | sort -Vu | while read -r f; do for d; do if [ -e "$d/$f" ]; then [ -x "$d/$f" ] && echo "$d/$f" continue 2 fi done done } export LC_COLLATE=C for i; do if [ "$i" = "--help" ] || [ "$i" = "-h" ]; then usage exit 0 fi done for i; do if [ "$i" = "--version" ]; then echo "kernel-install {{PROJECT_VERSION}} ({{GIT_VERSION}})" exit 0 fi done if [ "$KERNEL_INSTALL_BYPASS" = "1" ]; then echo "kernel-install: Skipping execution because KERNEL_INSTALL_BYPASS=1" exit 0 fi 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 COMMAND=add # kernel's install.sh invokes us as # /sbin/installkernel # We ignore the last two arguments. set -- "${1:?}" "${2:?}" else COMMAND="$1" [ $# -ge 1 ] && shift fi if [ "$COMMAND" = "inspect" ]; then KERNEL_VERSION="" else if [ $# -lt 1 ]; then echo "Error: not enough arguments" >&2 exit 1 fi KERNEL_VERSION="$1" shift fi # These two settings are only settable via install.conf layout= initrd_generator= # These two settings can be inherited from the environment _MACHINE_ID_SAVED="$MACHINE_ID" _BOOT_ROOT_SAVED="$BOOT_ROOT" 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" else install_conf= fi if [ -f "$install_conf" ]; then log_verbose "Reading $install_conf…" # shellcheck source=/dev/null . "$install_conf" fi [ -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" log_verbose "MACHINE_ID=$MACHINE_ID set via environment" else [ -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" log_verbose "BOOT_ROOT=$BOOT_ROOT set via environment" else [ -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 # generated one. If the user configured an explicit machine ID to use in # /etc/machine-info to use for our purpose, we'll use that instead (for # compatibility). # shellcheck source=/dev/null if [ -z "$MACHINE_ID" ] && [ -f /etc/machine-info ]; then . /etc/machine-info MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID" [ -n "$MACHINE_ID" ] && \ log_verbose "machine-id $MACHINE_ID acquired from /etc/machine-info" fi if [ -z "$MACHINE_ID" ] && [ -s /etc/machine-id ]; then read -r MACHINE_ID &2 exit 1 fi if ! [ -f "$1" ]; then echo "Error: kernel image argument $1 not a file" >&2 exit 1 fi if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then # Compatibility with earlier versions that used the presence of $BOOT_ROOT/$ENTRY_TOKEN # to signal to 00-entry-directory to create $ENTRY_DIR_ABS # to serve as the indication to use or to not use the BLS if [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ]; then echo "+mkdir -v -p $ENTRY_DIR_ABS" mkdir -v -p "$ENTRY_DIR_ABS" || exit 1 else mkdir -p "$ENTRY_DIR_ABS" || exit 1 fi fi for f in $KERNEL_INSTALL_PLUGINS; do log_verbose "+$f add $KERNEL_VERSION $ENTRY_DIR_ABS" "$@" err=0 "$f" add "$KERNEL_VERSION" "$ENTRY_DIR_ABS" "$@" || err=$? [ $err -eq $skip_remaining ] && break [ $err -ne 0 ] && exit $err done ;; remove) for f in $KERNEL_INSTALL_PLUGINS; do log_verbose "+$f remove $KERNEL_VERSION $ENTRY_DIR_ABS" err=0 "$f" remove "$KERNEL_VERSION" "$ENTRY_DIR_ABS" || err=$? [ $err -eq $skip_remaining ] && break [ $err -ne 0 ] && exit $err done if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then log_verbose "Removing $ENTRY_DIR_ABS/" rm -rf "$ENTRY_DIR_ABS" fi ;; inspect) echo "KERNEL_INSTALL_MACHINE_ID: $KERNEL_INSTALL_MACHINE_ID" echo "KERNEL_INSTALL_ENTRY_TOKEN: $KERNEL_INSTALL_ENTRY_TOKEN" echo "KERNEL_INSTALL_BOOT_ROOT: $KERNEL_INSTALL_BOOT_ROOT" echo "KERNEL_INSTALL_LAYOUT: $KERNEL_INSTALL_LAYOUT" echo "KERNEL_INSTALL_INITRD_GENERATOR: $KERNEL_INSTALL_INITRD_GENERATOR" echo "ENTRY_DIR_ABS: $KERNEL_INSTALL_BOOT_ROOT/$ENTRY_TOKEN/\$KERNEL_VERSION" # Assert that ENTRY_DIR_ABS actually matches what we are printing here [ "${ENTRY_DIR_ABS%/*}" = "$KERNEL_INSTALL_BOOT_ROOT/$ENTRY_TOKEN" ] || { echo "Assertion didn't pass." >&2; exit 1; } ;; *) echo "Error: unknown command '$COMMAND'" >&2 exit 1 ;; esac exit "$ret"