VirtualBox su Debian 12 BookWorm con Secure Boot
fonte: https://medium.com/@loice5/fix-virtualbox-and-secure-boot-on-debian-based-linux-d62965973c4f
Se si ha un Debian su un PC recente è facile ci sia Secure Boot attivo che inibisce il caricamento di moduli non firmati.
O si disabilita il Secure Boot o si deve firmare i moduli e caricarli nel boot del PC.
Procediamo a creare prima i certificati MOK per firmare poi i moduli:
sudo su -
mkdir /root/module-signing
cd /root/module-signing
# Replace USERNAME by your username on the machine
openssl req -new -x509 -newkey rsa:2048 -keyout MOK.priv -outform DER -out MOK.der -nodes -days 36500 -subj "/CN=USERNAME/"
chmod 600 MOK.priv
Verrà generata una coppia di chiavi MOK.priv e MOK.der
Si importa la chiave nel boot PC (la password impostata verrà richiesta dopo al boot nella fase di importazione, sarà richiesta solo una volta):
mokutil --import /root/module-signing/MOK.der
# You will be prompted for your password twice
# input the temporary one you just imagined
Riavviare il PC, alla schermata blu con la richiesta di importazione della chiave selezionare Enroll MOK e seguire le indicazioni (verrà richiesta la password di prima), riavviare poi il PC.
Ora il certificato è caricato nel boot e si può procedere alla firma dei moduli, l’esempio fornito nell’articolo fa riferimento ai moduli VirtualBox nel formato compresso ZSTD, nel mio caso erano in formato non compresso .ko, ho riadattato il codice dello script per il mio caso:
#!/bin/bash
# Ensure the script is running from /root/module-signing if that is required.
cd /root/module-signing || { echo "Failed to change directory to /root/module-signing"; exit 1; }
# Get the directory containing the vbox modules.
MODULE_DIR=$(dirname "$(modinfo -n vboxdrv)")
# Loop over every *.ko file in the module directory (not compressed).
for modfile in "$MODULE_DIR"/*.ko; do
# Check if the file exists (in case there are no .ko files)
if [ ! -e "$modfile" ]; then
echo "No .ko modules found in $MODULE_DIR"
break
fi
echo "----------------------------------------"
echo "Processing module: $modfile"
# Define the temporary decompressed filename (no need to decompress).
temp_ko="$modfile"
# Create a backup of the original module before any operations.
backup_modfile="${MODULE_DIR}/old-$(basename "$modfile")"
echo "Creating backup of original module: $modfile ..."
if cp "$modfile" "$backup_modfile"; then
echo "Backup created successfully."
else
echo "Failed to create backup for $modfile. Skipping..."
continue
fi
# Signing the module directly.
echo "Signing ${temp_ko} ..."
if /usr/src/linux-headers-$(uname -r)/scripts/sign-file sha256 \
/root/module-signing/MOK.priv \
/root/module-signing/MOK.der \
"$temp_ko"; then
echo "Signing successful."
else
echo "Signing failed for $temp_ko. Restoring backup and skipping..."
# Restore the backup if signing fails
mv "$backup_modfile" "$modfile"
continue
fi
echo "Finished processing $modfile"
# Extract the module name from the filename (remove path and .ko extension).
modname=$(basename "$modfile")
modname="${modname%.ko}"
echo "Attempting to load module: $modname"
# Try to modprobe the module.
if modprobe "$modname"; then
echo "modprobe $modname succeeded."
else
echo "modprobe $modname failed. Restoring backup..."
# Restore the backup if modprobe fails
mv "$backup_modfile" "$modfile"
continue
fi
done
echo "----------------------------------------"
echo "All modules processed."
Ora i moduli sono firmati e caricati con modprobe se tutto funziona si può caricare i moduli al boot del sistema:
echo -e "vboxdrv\nvboxnetadp\nvboxnetflt" | sudo tee /etc/modules-load.d/vboxmodules.conf