1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
#!/bin/sh
# SPDX-License-Identifier: LGPL-2.1-or-later
# depthcharge-tools kernel-install plugin
# Copyright (C) 2022 Alper Nebi Yasak <alpernebiyasak@gmail.com>
# See COPYRIGHT and LICENSE files for full copyright information.
# This is a modified copy of 90-loaderentry.install from 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 <https://www.gnu.org/licenses/>.
set -e
COMMAND="${1:?}"
KERNEL_VERSION="${2:?}"
ENTRY_DIR_ABS="${3:?}"
KERNEL_IMAGE="$4"
INITRD_OPTIONS_SHIFT=4
[ "$KERNEL_INSTALL_LAYOUT" = "depthcharge-tools" ] || exit 0
log() {
if [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ]; then
echo "$@"
fi
} >&2
maybe_error() {
# Ignore errors if we're not booted with depthcharge
if grep "cros_secure" /proc/cmdline >/dev/null 2>&1; then
if [ -n "$1" ]; then
echo "Error: $1"
fi
echo "Error: Failed to update depthcharge partitions, system may be unbootable."
exit 1
else
if [ -n "$1" ]; then
log "Error: $1"
fi
log "Not booted with depthcharge, so ignoring that."
exit 0
fi
} >&2
# Disable if our package is not installed.
if ! command -v depthchargectl >/dev/null 2>&1; then
log "Not running depthcharge plugin, depthchargectl is missing."
exit 0
fi
run_depthchargectl() {
if [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ]; then
log "Running depthchargectl $@:"
depthchargectl --verbose "$@"
else
depthchargectl "$@" 2>/dev/null
fi
}
MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
ENTRY_TOKEN="$KERNEL_INSTALL_ENTRY_TOKEN"
BOOT_ROOT="$KERNEL_INSTALL_BOOT_ROOT"
BOOT_MNT="$(stat -c %m "$BOOT_ROOT")"
if [ "$BOOT_MNT" = '/' ]; then
BOOT_DIR="$ENTRY_DIR_ABS"
else
BOOT_DIR="${ENTRY_DIR_ABS#"$BOOT_MNT"}"
fi
case "$COMMAND" in
remove)
ENABLED="$(
depthchargectl config \
--section depthchargectl/remove \
--default False \
enable-system-hooks 2>/dev/null
)" || maybe_error
# Disable based on package configuration
if [ "$ENABLED" != "True" ]; then
log "Not removing depthcharge image, disabled by config."
exit 0
fi
IMAGES_DIR="$(
depthchargectl config \
--section depthchargectl/remove \
images-dir 2>/dev/null
)" || maybe_error
if [ -f "$IMAGES_DIR/$KERNEL_VERSION.img" ]; then
# Assuming kernel-install handles warnings about removing the running kernel
run_depthchargectl remove --force "$KERNEL_VERSION" >/dev/null \
|| maybe_error
else
log "Not removing depthcharge image, already doesn't exist."
fi
exit 0
;;
add)
;;
*)
exit 0
;;
esac
ENABLED="$(
depthchargectl config \
--section depthchargectl/write \
--default False \
enable-system-hooks
)" || maybe_error
if [ "$ENABLED" != "True" ]; then
log "Not writing depthcharge image, disabled by config."
exit 0
fi
IMAGES_DIR="$(
depthchargectl config \
--section depthchargectl/write \
images-dir
)" || maybe_error
BOARD="$(depthchargectl config board)" || maybe_error
if [ "$BOARD" = "none" ]; then
maybe_error "Cannot build depthcharge images when no board is specified."
fi
KERNEL_CMDLINE="$(
depthchargectl config \
--section depthchargectl/write \
kernel-cmdline 2>/dev/null
)" || KERNEL_CMDLINE=""
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)"
elif [ -n "$KERNEL_CMDLINE" ]; then
BOOT_OPTIONS="$KERNEL_CMDLINE"
else
BOOT_OPTIONS="$(tr -s "$IFS" '\n' </proc/cmdline | grep -ve '^BOOT_IMAGE=' -e '^initrd=' | tr '\n' ' ')"
fi
BOOT_OPTIONS="${BOOT_OPTIONS% }"
# If the boot entries are named after the machine ID, then suffix the kernel
# command line with the machine ID we use, so that the machine ID remains
# stable, even during factory reset, in the initrd (where the system's machine
# ID is not directly accessible yet), and if the root file system is volatile.
if [ "$ENTRY_TOKEN" = "$MACHINE_ID" ] && ! echo "$BOOT_OPTIONS" | grep -q "systemd.machine_id="; then
BOOT_OPTIONS="$BOOT_OPTIONS systemd.machine_id=$MACHINE_ID"
fi
# All files listed as arguments, and staged files starting with "initrd" are installed as initrds.
shift "$INITRD_OPTIONS_SHIFT"
for initrd in "${KERNEL_INSTALL_STAGING_AREA}"/initrd*; do
if [ "$initrd" = "${KERNEL_INSTALL_STAGING_AREA}/initrd*" ]; then
continue
elif [ ! -f "$initrd" ]; then
maybe_error "Initrd '$initrd' is not a file."
fi
set -- "$@" "$initrd"
done
INITRD=""
if [ "$#" -eq 0 ]; then
# Find an existing initramfs if we were given nothing
for initrd in \
"$ENTRY_DIR_ABS/initrd" \
"$BOOT_ROOT/initramfs-$KERNEL_VERSION.img" \
"$BOOT_ROOT/initrd-$KERNEL_VERSION.img" \
"$BOOT_ROOT/initramfs.img-$KERNEL_VERSION" \
"$BOOT_ROOT/initrd.img-$KERNEL_VERSION" \
"$BOOT_ROOT/initramfs-$KERNEL_VERSION" \
"$BOOT_ROOT/initrd-$KERNEL_VERSION" \
"usr/lib/modules/$KERNEL_VERSION/initramfs.img" \
"usr/lib/modules/$KERNEL_VERSION/initrd.img" \
"usr/lib/modules/$KERNEL_VERSION/initramfs" \
"usr/lib/modules/$KERNEL_VERSION/initrd" \
"lib/modules/$KERNEL_VERSION/initramfs.img" \
"lib/modules/$KERNEL_VERSION/initrd.img" \
"lib/modules/$KERNEL_VERSION/initramfs" \
"lib/modules/$KERNEL_VERSION/initrd" \
"$BOOT_ROOT/initramfs.img" \
"$BOOT_ROOT/initrd.img" \
"$BOOT_ROOT/initramfs" \
"$BOOT_ROOT/initrd" \
;
do
if [ -f "$initrd" ]; then
INITRD="$initrd"
break
fi
done
elif [ "$#" -eq 1]; then
INITRD="$1"
set --
elif [ "$#" -gt 1 ]; then
# Depthcharge-tools can't handle multiple initramfs files yet.
log "Merging initrd files"
cat "$@" >"$KERNEL_INSTALL_STAGING_AREA/merged-initrd.img" \
|| maybe_error "Could not merge initrd files for depthchargectl."
INITRD="$KERNEL_INSTALL_STAGING_AREA/merged-initrd.img"
set --
fi
# Check possible dtbs paths
FDTDIR=""
for fdtdir in \
"$BOOT_ROOT/dtbs/$KERNEL_VERSION" \
"$BOOT_ROOT/dtb/$KERNEL_VERSION" \
"$BOOT_ROOT/dtbs-$KERNEL_VERSION" \
"$BOOT_ROOT/dtb-$KERNEL_VERSION" \
"/usr/lib/linux-image-$KERNEL_VERSION" \
"/usr/lib/modules/$KERNEL_VERSION/dtbs" \
"/usr/lib/modules/$KERNEL_VERSION/dtb" \
"/lib/modules/$KERNEL_VERSION/dtbs" \
"/lib/modules/$KERNEL_VERSION/dtb" \
"$BOOT_ROOT/dtbs" \
"$BOOT_ROOT/dtb" \
"/usr/share/dtbs" \
"/usr/share/dtb" \
;
do
if [ -d "$fdtdir" ]; then
FDTDIR="$fdtdir"
break
fi
done
# Depthchargectl write doesn't take custom files, so build image first
IMAGE="$(
run_depthchargectl build \
--kernel "$KERNEL_IMAGE" \
${INITRD:+--initramfs "$INITRD"} \
${FDTDIR:+--fdtdir "$FDTDIR"} \
--kernel-cmdline "$BOOT_OPTIONS" \
--kernel-release "$KERNEL_VERSION" \
)" || maybe_error
PART_COUNT="$(depthchargectl list -c 2>/dev/null)" || maybe_error
if [ "$PART_COUNT" -gt 1 ]; then
run_depthchargectl write "$IMAGE" >/dev/null \
|| maybe_error
elif [ "$PART_COUNT" -eq 1 ]; then
run_depthchargectl write --allow-current "$IMAGE" >/dev/null \
|| maybe_error
else
maybe_error "No usable Chrome OS Kernel partition found."
fi
exit 0
|