#!/bin/sh
# Blackbox: on an encrypted root the guest's own GRUB is never run again (the
# kernel and this initramfs are the release's) and /boot sits inside LUKS.
# Debian's grub-cloud-amd64 still runs grub-install on every configure and on
# every grub or shim update while /etc/grub.d/enable_cloud exists, grub-install
# refuses an encrypted /boot, and "apt upgrade" stops with the package
# half-configured. The package skips its install without that flag file, so it
# is removed here. fde-encrypt.sh removes it at encryption time; this covers a
# disk encrypted under an earlier release, on its first boot under this one.
# Opt out: create /etc/grub.d/blackbox-grub.keep in the guest.
PREREQ=""
prereqs() { echo "$PREREQ"; }
case "$1" in prereqs) prereqs; exit 0 ;; esac
. /scripts/functions

MAPPER=/dev/mapper/root_crypt
F="${rootmnt}/etc/grub.d/enable_cloud"
[ -b "$MAPPER" ] || exit 0
[ -e "$F" ] || exit 0
[ -e "${rootmnt}/etc/grub.d/blackbox-grub.keep" ] && exit 0
ro=0
grep -q " ${rootmnt} [^ ]* ro[, ]" /proc/mounts && ro=1
if [ $ro = 1 ]; then mount -o remount,rw "${rootmnt}" 2>/dev/null || exit 0; fi
rm -f "$F"
sync
if [ $ro = 1 ]; then mount -o remount,ro "${rootmnt}" 2>/dev/null || true; fi
exit 0
