#!/bin/sh
# Blackbox: the kernel is supplied by the host-side release, not by the guest,
# so the guest's own /lib/modules may not match it. Lend it this initramfs's
# (pruned) module tree for the running kernel when that happens.
PREREQ=""
prereqs() { echo "$PREREQ"; }
case "$1" in prereqs) prereqs; exit 0 ;; esac
. /scripts/functions

ver=$(uname -r)
[ -d "/lib/modules/$ver" ] || exit 0
if [ ! -d "${rootmnt}/lib/modules/$ver/kernel" ]; then
    mkdir -p /run/bb/modules
    cp -a "/lib/modules/$ver" /run/bb/modules/ 2>/dev/null || exit 0
    # The root is still mounted read-only here (the "ro" on the command line);
    # the mount point has to exist, so make the root writable for that one
    # directory. systemd remounts it read-write moments later anyway.
    if ! mkdir -p "${rootmnt}/lib/modules/$ver" 2>/dev/null; then
        mount -o remount,rw "${rootmnt}" 2>/dev/null || exit 0
        mkdir -p "${rootmnt}/lib/modules/$ver" 2>/dev/null || exit 0
    fi
    mount --bind "/run/bb/modules/$ver" "${rootmnt}/lib/modules/$ver" || true
fi
