8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer#!/bin/bash
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer# ex: ts=8 sw=4 sts=4 et filetype=sh
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald HoyerCOMMAND="$1"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald HoyerKERNEL_VERSION="$2"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald HoyerBOOT_DIR_ABS="$3"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald HoyerKERNEL_IMAGE="$4"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif [[ -f /etc/machine-id ]]; then
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer read MACHINE_ID < /etc/machine-id
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif ! [[ $MACHINE_ID ]]; then
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer exit 1
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald HoyerBOOT_DIR="/$MACHINE_ID/$KERNEL_VERSION"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald HoyerLOADER_ENTRY="/boot/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif [[ $COMMAND == remove ]]; then
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer exec rm -f "$LOADER_ENTRY"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif ! [[ $COMMAND == add ]]; then
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer exit 1
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif ! [[ $KERNEL_IMAGE ]]; then
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer exit 1
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif [[ -f /etc/os-release ]]; then
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer . /etc/os-release
5ae4d543cb9b45ad6c6b82b78da1d6abc2291cdbLennart Poetteringelif [[ -f /usr/lib/os-release ]]; then
5ae4d543cb9b45ad6c6b82b78da1d6abc2291cdbLennart Poettering . /usr/lib/os-release
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif ! [[ $PRETTY_NAME ]]; then
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer PRETTY_NAME="Linux $KERNEL_VERSION"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
6c10d3997019c4f09dad5650209b49c5ccd11551Harald Hoyerdeclare -a BOOT_OPTIONS
6c10d3997019c4f09dad5650209b49c5ccd11551Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif [[ -f /etc/kernel/cmdline ]]; then
6fd2ccc957f21a423e1457c66e53b1697944d72aHarald Hoyer read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerif ! [[ ${BOOT_OPTIONS[*]} ]]; then
6fd2ccc957f21a423e1457c66e53b1697944d72aHarald Hoyer read -r -d '' -a line < /proc/cmdline
2f3a215f61d758cd59b3be5b65976a12401ac4ffHarald Hoyer for i in "${line[@]}"; do
2f3a215f61d758cd59b3be5b65976a12401ac4ffHarald Hoyer [[ "${i#initrd=*}" != "$i" ]] && continue
6fd2ccc957f21a423e1457c66e53b1697944d72aHarald Hoyer BOOT_OPTIONS+=("$i")
6c10d3997019c4f09dad5650209b49c5ccd11551Harald Hoyer done
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
6c10d3997019c4f09dad5650209b49c5ccd11551Harald Hoyerif ! [[ ${BOOT_OPTIONS[*]} ]]; then
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "Could not determine the kernel command line parameters." >&2
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer exit 1
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerfi
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8b179a830a789746cce0be6671e2de235e3b0ea9Tom Gundersencp "$KERNEL_IMAGE" "$BOOT_DIR_ABS/linux" &&
8b179a830a789746cce0be6671e2de235e3b0ea9Tom Gundersen chown root:root "$BOOT_DIR_ABS/linux" &&
8b179a830a789746cce0be6671e2de235e3b0ea9Tom Gundersen chmod 0644 "$BOOT_DIR_ABS/linux" || {
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "Could not copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'." >&2
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer exit 1
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer}
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyermkdir -p "${LOADER_ENTRY%/*}" || {
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer exit 1
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer}
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer{
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "title $PRETTY_NAME"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "version $KERNEL_VERSION"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "machine-id $MACHINE_ID"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "options ${BOOT_OPTIONS[*]}"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "linux $BOOT_DIR/linux"
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer [[ -f $BOOT_DIR_ABS/initrd ]] && \
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "initrd $BOOT_DIR/initrd"
852752fca2f73323e3c25b33348b3c92458665aeHarald Hoyer :
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer} > "$LOADER_ENTRY" || {
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer echo "Could not create loader entry '$LOADER_ENTRY'." >&2
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer exit 1
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyer}
8f51399e75e5d0d0741ecb18c549a57840bd1cc3Harald Hoyerexit 0