#!/bin/sh

# Basic initramfs support for AoE root.
# To use it, install and configure the vblade package on the server,
# and specify "root=/dev/etherd/e0.0" as a kernel parameter for the client.

set -e

PREREQ=""

prereqs()
{
    echo "$PREREQ"
}

case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

# If an AoE root device wasn't specified, exit
grep -qs "root=/dev/etherd/e" /proc/cmdline || exit 0
. /scripts/functions

# Wait for the network interfaces to become available
i=0
while i=$(($i+1)); do
    interfaces=$(ip -oneline link show | sed -n '/ether/s/[0-9 :]*\([^:]*\).*/\1/p')
    if [ -n "$interfaces" ]; then
        break
    elif [ $i -ge 10 ]; then
        # After a while, give a shell to the user in case he can fix it
        panic "No network interfaces found"
        i=0
    else
        sleep 1
    fi
done

# For AoE to work, interfaces need to be up, but don't need IPs
for i in $interfaces; do
    ip link set dev "$i" up
done

# Wait for a network interface to be up
i=0
while i=$(($i+1)); do
    if ip -oneline link show up | grep -vw lo | grep -q LOWER_UP; then
        break
    elif [ $i -ge 4 ]; then
        # After a while, give a shell to the user in case he can fix it
        panic "No network interfaces are up"
        i=0
    else
        sleep 1
    fi
done
modprobe aoe
udevadm settle || true
