summaryrefslogtreecommitdiffstats
path: root/modules.d/35network-legacy/net-genrules.sh
blob: 686db593f4e4323f4d4e72b76ca146379a72da63 (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
#!/bin/sh

getargbool 0 rd.neednet && NEEDNET=1

# Don't continue if we don't need network
if [ -z "$netroot" ] && [ ! -e "/tmp/net.ifaces" ] && [ "$NEEDNET" != "1" ]; then
    return
fi

command -v fix_bootif > /dev/null || . /lib/net-lib.sh

# Write udev rules
{
    # bridge: attempt only the defined interface
    for i in /tmp/bridge.*.info; do
        [ -e "$i" ] || continue
        unset bridgeslaves
        unset bridgename
        # shellcheck disable=SC1090
        . "$i"
        RAW_IFACES="$RAW_IFACES $bridgeslaves"
        MASTER_IFACES="$MASTER_IFACES $bridgename"
    done

    # bond: attempt only the defined interface (override bridge defines)
    for i in /tmp/bond.*.info; do
        [ -e "$i" ] || continue
        unset bondslaves
        unset bondname
        # shellcheck disable=SC1090
        . "$i"
        # It is enough to fire up only one
        RAW_IFACES="$RAW_IFACES $bondslaves"
        MASTER_IFACES="$MASTER_IFACES ${bondname}"
    done

    for i in /tmp/team.*.info; do
        [ -e "$i" ] || continue
        unset teamslaves
        unset teammaster
        # shellcheck disable=SC1090
        . "$i"
        RAW_IFACES="$RAW_IFACES ${teamslaves}"
        MASTER_IFACES="$MASTER_IFACES ${teammaster}"
    done

    for i in /tmp/vlan.*.phy; do
        [ -e "$i" ] || continue
        unset phydevice
        # shellcheck disable=SC1090
        . "$i"
        RAW_IFACES="$RAW_IFACES $phydevice"
        for j in /tmp/vlan.*".${phydevice}"; do
            [ -e "$j" ] || continue
            unset vlanname
            # shellcheck disable=SC1090
            . "$j"
            MASTER_IFACES="$MASTER_IFACES ${vlanname}"
        done
    done

    MASTER_IFACES="$(trim "$MASTER_IFACES")"
    RAW_IFACES="$(trim "$RAW_IFACES")"

    if [ -z "$IFACES" ]; then
        [ -e /tmp/net.ifaces ] && read -r IFACES < /tmp/net.ifaces
    fi

    if [ -e /tmp/net.bootdev ]; then
        read -r bootdev < /tmp/net.bootdev
    fi

    # shellcheck disable=SC2016
    ifup='/sbin/ifup $name'

    runcmd="RUN+=\"/sbin/initqueue --name ifup-\$name --unique --onetime $ifup\""

    # We have some specific interfaces to handle
    if [ -n "${RAW_IFACES}${IFACES}" ]; then
        echo 'SUBSYSTEM!="net", GOTO="net_end"'
        echo 'ACTION!="add|change|move", GOTO="net_end"'
        for iface in $IFACES $RAW_IFACES; do
            case "$iface" in
                ??:??:??:??:??:??) # MAC address
                    cond="ATTR{address}==\"$iface\""
                    echo "$cond, $runcmd, GOTO=\"net_end\""
                    ;;
                ??-??-??-??-??-??) # MAC address in BOOTIF form
                    cond="ATTR{address}==\"$(fix_bootif "$iface")\""
                    echo "$cond, $runcmd, GOTO=\"net_end\""
                    ;;
                *) # an interface name
                    cond="ENV{INTERFACE}==\"$iface\""
                    echo "$cond, $runcmd, GOTO=\"net_end\""
                    cond="NAME==\"$iface\""
                    echo "$cond, $runcmd, GOTO=\"net_end\""
                    ;;
            esac
            # The GOTO prevents us from trying to ifup the same device twice
        done
        echo 'LABEL="net_end"'

        for iface in $IFACES; do
            if [ "$bootdev" = "$iface" ] || [ "$NEEDNET" = "1" ]; then
                if [ -n "$netroot" ] && [ -n "$DRACUT_SYSTEMD" ]; then
                    echo "systemctl is-active initrd-root-device.target || [ -f /tmp/net.${iface}.did-setup ]"
                else
                    echo "[ -f /tmp/net.${iface}.did-setup ]"
                fi > "$hookdir"/initqueue/finished/wait-"$iface".sh
            fi
        done
    # Default: We don't know the interface to use, handle all
    # Fixme: waiting for the interface as well.
    else
        cond='ACTION=="add", SUBSYSTEM=="net", ENV{DEVTYPE}!="wlan|wwan"'
        # if you change the name of "91-default-net.rules", also change modules.d/80cms/cmssetup.sh
        echo "$cond, $runcmd" > /etc/udev/rules.d/91-default-net.rules
        if [ "$NEEDNET" = "1" ]; then
            # shellcheck disable=SC2016
            echo 'for i in /tmp/net.*.did-setup; do [ -f "$i" ]  && exit 0; done; exit 1' > "$hookdir"/initqueue/finished/wait-network.sh
        fi
    fi

    # if you change the name of "90-net.rules", also change modules.d/80cms/cmssetup.sh
} > /etc/udev/rules.d/90-net.rules