#!/bin/sh
# Blancco PXE Loader - init script for custom initramfs
# Boot chain: iPXE -> GRUB EFI -> Ubuntu kernel + this initramfs -> switch_root to Blancco
#
# Blancco's own kernel freezes / lacks NIC drivers for some Dell Precision
# hardware during PXE boot. Workaround: boot Ubuntu kernel (which has a wider
# NIC driver set), download Blancco rootfs (squashfs), overlay-mount, and
# switch_root into Blancco's userspace.
#
# Verbose trace + shell-on-NIC-failure because silent hangs during Blancco
# PXE boot are painful to debug. set -x goes to /dev/console so the screen
# shows every step; if no NIC appears after the modprobe sweep, we dump
# dmesg / lspci / /proc/modules and drop to sh so the operator can
# investigate without re-imaging.

exec >/dev/console 2>&1
set -x

echo ""
echo "============================================"
echo "  Blancco PXE loader (verbose)"
echo "============================================"
echo ""

export PATH=/bin:/sbin
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev 2>/dev/null || mount -t tmpfs tmpfs /dev
mkdir -p /tmp /run /run/lower /run/upper /run/work /run/newroot
KVER=$(uname -r)
echo "Running kernel: $KVER"
ls /lib/modules/ 2>/dev/null

echo "[1/5] Loading NIC drivers via modprobe (resolves deps automatically)..."
# Throw the full common-NIC driver list at the wall. modprobe resolves the
# deps from /lib/modules/$KVER/modules.dep (built by prepare-boot-tools).
# Anything missing is silently ignored; whatever matches PCI IDs will bind.
for drv in \
    mii libphy ptp \
    e1000 e1000e igb igc ixgbe ixgbevf i40e ice iavf \
    tg3 bnx2 bnx2x bnxt_en b44 \
    r8169 r8152 atlantic \
    vmxnet3 virtio_net virtio_pci \
    pcnet32 8139too 8139cp \
    sfc sfc_ef100 mlx4_en mlx5_core \
    alx atl1c atl1e atl2 \
    via_rhine via_velocity forcedeth \
    pegasus dm9601 asix ax88179_178a cdc_ether cdc_ncm rndis_host; do
    modprobe -v "$drv" 2>/dev/null && echo "  OK $drv" || true
done
sleep 3

echo "[2/5] /sys/class/net after driver load:"
ls /sys/class/net/ || true
ip link || true

echo "  Waiting up to 60s for non-lo interface..."
IFACE=""
for i in $(seq 1 60); do
    for n in /sys/class/net/*; do
        name="${n##*/}"
        [ "$name" = "lo" ] && continue
        [ -d "$n" ] && IFACE="$name" && break 2
    done
    sleep 1
    echo -n "."
done
echo ""

if [ -z "$IFACE" ]; then
    echo "ERROR: No network interface after 60s"
    echo "=== dmesg tail ==="; dmesg | tail -40
    echo "=== PCI devices (sysfs) ==="; ls /sys/bus/pci/devices/ 2>/dev/null
    echo "=== loaded modules ==="; cat /proc/modules
    echo "Dropping to shell - type 'exit' to reboot."
    exec /bin/sh
fi

echo "  IFACE=$IFACE, bringing up..."
ip link set "$IFACE" up || ifconfig "$IFACE" up
sleep 2

SERVER=10.9.100.1
ifconfig "$IFACE" 10.9.100.250 netmask 255.255.255.0 up
sleep 1
echo "  IP: 10.9.100.250  SERVER: $SERVER"
ip addr

echo "[3/5] Downloading airootfs.sfs (~756 MB)..."
wget -O /tmp/airootfs.sfs http://$SERVER/blancco/arch/x86_64/airootfs.sfs 2>&1
[ -s /tmp/airootfs.sfs ] || { echo "ERROR: download failed"; exec /bin/sh; }

echo "[4/5] Mounting rootfs + overlay..."
modprobe overlay 2>/dev/null || insmod /lib/modules/$KVER/kernel/fs/overlayfs/overlay.ko 2>/dev/null
modprobe squashfs 2>/dev/null || insmod /lib/modules/$KVER/kernel/fs/squashfs/squashfs.ko 2>/dev/null
modprobe loop 2>/dev/null
losetup /dev/loop0 /tmp/airootfs.sfs
mount -t squashfs -o ro /dev/loop0 /run/lower
mount -t tmpfs -o size=50% tmpfs /run/upper
mkdir -p /run/upper/upper /run/upper/work
mount -t overlay overlay -o lowerdir=/run/lower,upperdir=/run/upper/upper,workdir=/run/upper/work /run/newroot

echo "[5/5] Fetching kmod tarball + config..."
wget -O /tmp/kmod.tar.gz http://$SERVER/blancco/kmod.tar.gz 2>&1
[ -s /tmp/kmod.tar.gz ] && (cd /run/newroot && gunzip -c /tmp/kmod.tar.gz | tar xf - && rm -f /tmp/kmod.tar.gz)

mkdir -p /run/newroot/albus
wget -O /run/newroot/albus/config.xml http://$SERVER/blancco/config-clean.xml 2>&1 || true
wget -O /run/newroot/albus/preferences.xml http://$SERVER/blancco/preferences.xml 2>&1 || true
cp -f /run/newroot/albus/preferences.xml /run/newroot/albus/preferences.save 2>/dev/null || true

mkdir -p /run/newroot/etc/X11/xorg.conf.d
cat > /run/newroot/etc/X11/xorg.conf.d/20-failsafeDriver.conf << XEOF
Section "Device"
    Identifier "Failsafe Video Device"
    Driver "modesetting"
EndSection
XEOF

mkdir -p /run/newroot/proc /run/newroot/sys /run/newroot/dev /run/newroot/run /run/newroot/tmp
mount --move /proc /run/newroot/proc
mount --move /sys /run/newroot/sys
mount --move /dev /run/newroot/dev

echo "Switching root..."
exec switch_root /run/newroot /sbin/init
