File disk.sh of Package openSUSE-MicroOS-UKI

#!/bin/bash
set -euxo pipefail

echo "####### BOOTLOADER INSTALL (disk.sh)"
# =============================================================================
# Put typecode to 8304 needed by the discovery tool by systemd (gpt-auto-root)
# =============================================================================
root_dev=$(basename "$(findmnt / -n --output SOURCE | cut -d '[' -f1)")
dev_path="/dev/${root_dev%*p?}"
root_part=${root_dev: -1}
echo "Change the partition n°${root_part} type of ${dev_path} in 8304"
sgdisk -t "$root_part:8304" "$dev_path"
# =============================================================================

# =============================================================================
# Install the UKI in the boot partition and add boot menu entry
# =============================================================================
chroot_uname=$(find /lib/modules/*/vmlinuz | head -n 1 \
    | sed -e 's|/lib/modules/\(.*\)/vmlinuz|\1|g')
efi_dir="/boot/efi/EFI/opensuse"
kernel_module_dir="/usr/lib/modules/$chroot_uname"
efi_file=$(basename "$kernel_module_dir"/uki-default-*.efi)
if [ -e "$efi_file" ]; then
    kernel_module_dir="/lib/modules/$chroot_uname"
    efi_file=$(basename "$kernel_module_dir"/uki-default-*.efi)
fi
if [ -e "$efi_file" ]; then
    echo "No efi file installed"
    exit 1
fi
if [[ ! -d "$efi_dir" ]]; then
    mkdir -p "$efi_dir"
fi
arch="$(uname -m)"
case "$arch" in
    aarch64) arch=aa64 ;;
    x86_64) arch=x64 ;;
    *) echo "Unknown arch $arch"; exit 1 ;;
esac
if [ -x /usr/bin/sdbootutil ] \
  && [ "$(sdbootutil --arch "$arch" bootloader)" == "systemd-boot" ]; then

	echo "install boot loader"
	loader_type="grub2-bls"
	rpm -q systemd-boot && loader_type="systemd-boot"
	if [ -s /etc/sysconfig/bootloader ]; then
		sed -i "s/^LOADER_TYPE=.*$/LOADER_TYPE=\"$loader_type\"/g" /etc/sysconfig/bootloader
	else
		echo "LOADER_TYPE=\"${loader_type}\"" > /etc/sysconfig/bootloader
	fi

	sdbootutil                  \
        -v                      \
        --no-random-seed        \
        --arch "$arch"          \
        --esp-path /boot/efi    \
        --entry-token=auto      \
        --no-variables install

	echo "add kernels"
	export hostonly_l=no # for dracut
	sdbootutil              \
        -v                  \
        --arch              \
        "$arch"             \
        --esp-path          \
        /boot/efi           \
        --entry-token=auto  \
        add-all-kernels
	# Set a 5s timeout, the "hold a key down" method doesn't work effectively.
	sdbootutil -v --arch "$arch" --esp-path /boot/efi set-timeout 5

	rm -f /boot/mbrid

	find /boot

    uki-tool sdboot         \
        --add               \
        -k "${chroot_uname}"\
        --uki "${efi_file}" \
        --arch "${arch}"    \
        --efi EFI/opensuse
else
    chmod -x /etc/grub.d/10_linux
    uki-tool grub2                                  \
        --add                                       \
        -k "${chroot_uname}"                        \
        --uki "${kernel_module_dir}/${efi_file}"    \
        --efi EFI/opensuse                          \
        --default
fi

echo "####### END BOOTLOADER INSTALL (disk.sh)"
openSUSE Build Service is sponsored by