modules.d: get rid of "tr"
[platform/upstream/dracut.git] / modules.d / 40network / parse-bond.sh
1 #!/bin/sh
2 #
3 # Format:
4 #       bond=<bondname>[:<bondslaves>:[:<options>]]
5 #
6 #       bondslaves is a comma-separated list of physical (ethernet) interfaces
7 #       options is a comma-separated list on bonding options (modinfo bonding for details) in format compatible with initscripts
8 #       if options include multi-valued arp_ip_target option, then its values should be separated by semicolon.
9 #
10 #       bond without parameters assumes bond=bond0:eth0,eth1:mode=balance-rr
11 #
12
13 # return if bond already parsed
14 [ -n "$bondname" ] && return
15
16 # Check if bond parameter is valid
17 if getarg bond= >/dev/null ; then
18     :
19 fi
20
21 # We translate list of slaves to space-separated here to mwke it easier to loop over them in ifup
22 # Ditto for bonding options
23 parsebond() {
24     local v=${1}:
25     set --
26     while [ -n "$v" ]; do
27         set -- "$@" "${v%%:*}"
28         v=${v#*:}
29     done
30
31     unset bondname bondslaves bondoptions
32     case $# in
33     0)  bondname=bond0; bondslaves="eth0 eth1" ;;
34     1)  bondname=$1; bondslaves="eth0 eth1" ;;
35     2)  bondname=$1; bondslaves=$(str_replace "$2" "," " ") ;;
36     3)  bondname=$1; bondslaves=$(str_replace "$2" "," " "); bondoptions=$(str_replace "$3" "," " ") ;;
37     *)  die "bond= requires zero to four parameters" ;;
38     esac
39 }
40
41 unset bondname bondslaves bondoptions
42
43 # Parse bond for bondname, bondslaves, bondmode and bondoptions
44 if getarg bond >/dev/null; then
45     # Read bond= parameters if they exist
46     bond="$(getarg bond=)"
47     if [ ! "$bond" = "bond" ]; then
48         parsebond "$(getarg bond=)"
49     fi
50     # Simple default bond
51     if [ -z "$bondname" ]; then
52         bondname=bond0
53         bondslaves="eth0 eth1"
54     fi
55     # Make it suitable for initscripts export
56     bondoptions=$(str_replace "$bondoptions" ";" ",")
57     echo "bondname=$bondname" > /tmp/bond.info
58     echo "bondslaves=\"$bondslaves\"" >> /tmp/bond.info
59     echo "bondoptions=\"$bondoptions\"" >> /tmp/bond.info
60     return
61 fi