summaryrefslogtreecommitdiffstats
path: root/modules.d/35network-legacy/parse-ip-opts.sh
blob: 8263321a15109275e4d02a03304f6b3919f55742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/sh
#
# Format:
#       ip=[dhcp|on|any|single-dhcp]
#
#       ip=<interface>:[dhcp|on|any][:[<mtu>][:<macaddr>]]
#
#       ip=<client-IP-number>:<server-IP-number>:<gateway-IP-number>:<netmask>:<client-hostname>:<interface>:{dhcp|on|any|none|off}[:[<mtu>][:<macaddr>]]
#
# When supplying more than only ip= line, <interface> is mandatory and
# bootdev= must contain the name of the primary interface to use for
# routing,dns,dhcp-options,etc.
#

# we really need to use `expr substr` with dash
# shellcheck disable=SC2003 disable=SC2308

command -v getarg > /dev/null || . /lib/dracut-lib.sh

if [ -n "$netroot" ] && [ -z "$(getarg ip=)" ] && [ -z "$(getarg BOOTIF=)" ]; then
    # No ip= argument(s) for netroot provided, defaulting to DHCP
    return
fi

# Count ip= lines to decide whether we need bootdev= or not
if [ -z "$NEEDBOOTDEV" ]; then
    count=0
    for p in $(getargs ip=); do
        case "$p" in
            ibft)
                continue
                ;;
        esac
        count=$((count + 1))
    done
    [ $count -gt 1 ] && NEEDBOOTDEV=1
fi
unset count

# If needed, check if bootdev= contains anything usable
BOOTDEV=$(getarg bootdev=)

if [ -n "$NEEDBOOTDEV" ] && getargbool 1 rd.neednet; then
    #[ -z "$BOOTDEV" ] && warn "Please supply bootdev argument for multiple ip= lines"
    echo "rd.neednet=1" > /etc/cmdline.d/dracut-neednet.conf
    info "Multiple ip= arguments: assuming rd.neednet=1"
else
    unset NEEDBOOTDEV
fi

# Check ip= lines
# XXX Would be nice if we could errorcheck ip addresses here as well
for p in $(getargs ip=); do
    ip_to_var "$p"

    # make first device specified the BOOTDEV
    if [ -n "$NEEDBOOTDEV" ] && [ -z "$BOOTDEV" ] && [ -n "$dev" ]; then
        BOOTDEV="$dev"
        info "Setting bootdev to '$BOOTDEV'"
    fi

    # skip ibft since we did it above
    [ "$autoconf" = "ibft" ] && continue

    # Empty autoconf defaults to 'dhcp'
    if [ -z "$autoconf" ]; then
        warn "Empty autoconf values default to dhcp"
        autoconf="dhcp"
    fi

    # Error checking for autoconf in combination with other values
    for autoopt in $(str_replace "$autoconf" "," " "); do
        case $autoopt in
            error) die "Error parsing option 'ip=$p'" ;;
            bootp | rarp | both) die "Sorry, ip=$autoopt is currently unsupported" ;;
            none | off)
                [ -z "$ip" ] \
                    && die "For argument 'ip=$p'\nValue '$autoopt' without static configuration does not make sense"
                [ -z "$mask" ] \
                    && die "Sorry, automatic calculation of netmask is not yet supported"
                ;;
            auto6 | link6) ;;
            either6) ;;
            dhcp | dhcp6 | on | any | single-dhcp)
                [ -n "$NEEDBOOTDEV" ] && [ -z "$dev" ] \
                    && die "Sorry, 'ip=$p' does not make sense for multiple interface configurations"
                [ -n "$ip" ] \
                    && die "For argument 'ip=$p'\nSorry, setting client-ip does not make sense for '$autoopt'"
                ;;
            *) die "For argument 'ip=$p'\nSorry, unknown value '$autoopt'" ;;
        esac
    done

    if [ -n "$dev" ]; then
        # We don't like duplicate device configs
        if [ -n "$IFACES" ]; then
            for i in $IFACES; do
                [ "$dev" = "$i" ] && die "For argument 'ip=$p'\nDuplication configurations for '$dev'"
            done
        fi
        # IFACES list for later use
        IFACES="$IFACES $dev"

        # Interface should exist
        if [ ! -e "/sys/class/net/$dev" ]; then
            warn "Network interface '$dev' does not exist"
        fi
    fi

    # Do we need to check for specific options?
    if [ -n "$NEEDDHCP" ] || [ -n "$DHCPORSERVER" ]; then
        # Correct device? (Empty is ok as well)
        [ "$dev" = "$BOOTDEV" ] || continue
        # Server-ip is there?
        [ -n "$DHCPORSERVER" ] && [ -n "$srv" ] && continue
        # dhcp? (It's simpler to check for a set ip. Checks above ensure that if
        # ip is there, we're static
        [ -z "$ip" ] && continue
        # Not good!
        die "Server-ip or dhcp for netboot needed, but current arguments say otherwise"
    fi

    if str_starts "$dev" "enx" && [ ${#dev} -eq 15 ]; then
        # shellcheck disable=SC2003
        printf -- "ifname=%s:%s:%s:%s:%s:%s:%s\n" \
            "$dev" \
            "$(expr substr "$dev" 3 2)" \
            "$(expr substr "$dev" 5 2)" \
            "$(expr substr "$dev" 7 2)" \
            "$(expr substr "$dev" 9 2)" \
            "$(expr substr "$dev" 11 2)" \
            "$(expr substr "$dev" 13 2)" \
            >> /etc/cmdline.d/80-enx.conf
    fi
done

# put BOOTIF in IFACES to make sure it comes up
if getargbool 1 "rd.bootif" && BOOTIF="$(getarg BOOTIF=)"; then
    BOOTDEV=$(fix_bootif "$BOOTIF")
    IFACES="$BOOTDEV $IFACES"
fi

# This ensures that BOOTDEV is always first in IFACES
if [ -n "$BOOTDEV" ] && [ -n "$IFACES" ]; then
    IFACES="${IFACES%"$BOOTDEV"*} ${IFACES#*"$BOOTDEV"}"
    IFACES="$BOOTDEV $IFACES"
fi

# Store BOOTDEV and IFACES for later use
[ -n "$BOOTDEV" ] && echo "$BOOTDEV" > /tmp/net.bootdev
[ -n "$IFACES" ] && echo "$IFACES" > /tmp/net.ifaces