|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# eos-ensure-sd-boot: Ensure systemd-boot is installed |
| 4 | +# |
| 5 | +# Copyright © 2023 Endless OS Foundation LLC |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation; either version 2 of the License, or (at |
| 10 | +# your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, but |
| 13 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with this program; if not, write to the Free Software |
| 19 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | +# 02110-1301, USA. |
| 21 | + |
| 22 | +# Ensure systemd-boot is considered installed by bootctl (when |
| 23 | +# appropriate) so that bootctl update works. |
| 24 | + |
| 25 | +set -e |
| 26 | +set -o pipefail |
| 27 | + |
| 28 | +LOADER_INFO_VAR=LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f |
| 29 | +LOADER_INFO_PATH="/sys/firmware/efi/efivars/$LOADER_INFO_VAR" |
| 30 | + |
| 31 | +# Read the LoaderInfo EFI variable. |
| 32 | +get_loader_info() { |
| 33 | + [ -f "$LOADER_INFO_PATH" ] || return 0 |
| 34 | + |
| 35 | + # The LoaderInfo variable is a NUL terminated UTF-16 string. The |
| 36 | + # first 4 bytes of any EFI variable are attributes. |
| 37 | + dd if="$LOADER_INFO_PATH" ibs=1 skip=4 status=none \ |
| 38 | + | iconv -f UTF16LE | tr -d '\0' |
| 39 | +} |
| 40 | + |
| 41 | +# We only use systemd-boot on x86_64. If we add any other architectures |
| 42 | +# later, they'll install it correctly out of the box. |
| 43 | +arch=$(uname -m) |
| 44 | +if [ "$arch" != x86_64 ]; then |
| 45 | + exit 0 |
| 46 | +fi |
| 47 | + |
| 48 | +# If it's already considered installed, there's nothing to do. |
| 49 | +if bootctl --quiet is-installed; then |
| 50 | + exit 0 |
| 51 | +fi |
| 52 | + |
| 53 | +# Only make changes when the boot loader is systemd-boot. |
| 54 | +echo "Reading $LOADER_INFO_VAR EFI variable" |
| 55 | +loader_info=$(get_loader_info) |
| 56 | +if ! [[ $loader_info =~ ^systemd-boot ]]; then |
| 57 | + echo "Boot loader is not systemd-boot" |
| 58 | + exit 0 |
| 59 | +fi |
| 60 | + |
| 61 | +# Install systemd-boot but don't touch the EFI Boot* variables since we |
| 62 | +# assume those are already working. |
| 63 | +echo "Installing systemd-boot" |
| 64 | +bootctl --no-variables install |
| 65 | + |
| 66 | +# Validate that systemd-boot is now considered installed. |
| 67 | +if ! bootctl --quiet is-installed; then |
| 68 | + echo "bootctl does not consider systemd-boot installed" >&2 |
| 69 | + exit 1 |
| 70 | +fi |
0 commit comments