connman: nfsroot: support multiple IPs, use link status
[opendreambox.git] / meta-opendreambox / recipes-connectivity / connman / connman / connmand-env
1 #!/bin/sh
2
3 ip=
4 nfsroot=
5 root=
6
7 set -- $(cat /proc/cmdline)
8 for arg do
9         key=${arg%%=*}
10         if [ "$arg" = "$key" ]; then
11                 val=
12         else
13                 val=${arg#*=}
14         fi
15         case "$key" in
16                 ip)
17                         ip=${val%%:*}
18                         ;;
19                 nfsroot)
20                         nfsroot=$val
21                         ;;
22                 root)
23                         root=$val
24                         ;;
25         esac
26 done
27
28 options=
29
30 if [ "$root" = "/dev/nfs" -o -n "$nfsroot" -a -n "$ip" ]; then
31         set -- $(ip -4 -o addr show | awk '{print $2}')
32         for dev do
33                 if ip -o link show $dev | awk '{print $3}' | grep -qvw -e 'LOOPBACK' -e 'NO-CARRIER'; then
34                         case "$ip" in
35                                 any|bootp|both|dhcp|on|rarp)
36                                         options="-I $dev $options"
37                                         ;;
38                                 *)
39                                         for dev_addr in $(ip -4 -o addr show dev $dev | awk '{print $4}' | grep -o '^[0-9.]\+'); do
40                                                 if [ "$ip" = "$dev_addr" ]; then
41                                                         options="-I $dev"
42                                                         break
43                                                 fi
44                                         done
45                                         ;;
46                         esac
47                 fi
48         done
49 fi
50
51 [ -d /run/connmand ] || mkdir -p /run/connmand
52 echo "CONNMAND_OPTS=$options" >/run/connmand/connmand.env