File: net-interface-handler

package info (click to toggle)
open-iscsi 2.1.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,888 kB
  • sloc: ansic: 49,790; perl: 2,481; sh: 2,247; makefile: 660; python: 622; yacc: 219; lex: 74
file content (80 lines) | stat: -rwxr-xr-x 2,615 bytes parent folder | download | duplicates (3)
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
#!/bin/sh -e
# suppress configuration of network interface used
# by iSCSI root device
#
# If the root filesystem is on iSCSI, then we must take care to avoid
# changing the state of its network interface. To this end, the initramfs
# leaves a note for us which interface was used, and we mangle
# /run/network/ifstate manually to stop it being brought up or down
# automatically. This is a slight layering violation, but, unfortunately,
# ifupdown appears to have no way to do this without also running
# /etc/network/*.d/ scripts.

assert_interface() {
    # udev sets INTERFACE to the name of the currently-processed nic.
    [ -n "$INTERFACE" ] && return 0
    echo "environment variable INTERFACE not set." 1>&2;
    return 1
}

start() {
    CR="
"
    assert_interface || return
    ifile=/run/initramfs/open-iscsi.interface

    [ -f "$ifile" ] && read iface < "$ifile" || return 0
    [ "$INTERFACE" = "$iface" ] || return

    if ! grep -qs "^$iface=" /run/network/ifstate; then
        mkdir -p /run/network
        echo "$iface=$iface" >>/run/network/ifstate

        if [ -f /run/net-$iface.conf ]; then
            conf=/run/net-$iface.conf
        elif [ -f /run/net6-$iface.conf ]; then
            conf=/run/net6-$iface.conf
        else
            conf=""
        fi
        if command -v resolvconf >/dev/null &&
           [ -n "$conf" ]; then
            . "$conf"
            R=""
            [ -n "$DOMAINSEARCH" ] && R="$R${CR}search $DOMAINSEARCH"
            [ -n "$IPV6DOMAINSEARCH" ] && R="$R${CR}search $IPV6DOMAINSEARCH"
            for ns in "$IPV4DNS0" "$IPV4DNS1" "$IPV6DNS0" "$IPV6DNS1"; do
                [ -n "$ns" -a "$ns" != "0.0.0.0" ] && R="$R${CR}nameserver $ns"
            done
            if [ -n "$R" ]; then
                # create the dir in case resolvconf did not start yet
                mkdir -p /run/resolvconf/interface
                resolvconf -a $iface.iscsi-network <<EOF
${R#${CR}}
EOF
            fi
        fi
    fi
}

stop() {
    assert_interface || return
    ifile=/run/initramfs/open-iscsi.interface
    [ -f "$ifile" ] && read iface < "$ifile" || return 0
    [ "$INTERFACE" = "$iface" ] || return

    if grep -qs "^$iface=" /run/network/ifstate; then
        grep -v "^$iface=" /run/network/ifstate >/run/network/.ifstate.tmp || true
        mv /run/network/.ifstate.tmp /run/network/ifstate

        if command -v resolvconf >/dev/null; then
            resolvconf -d $iface.iscsi-network
        fi
    fi
}

case "$1" in
    start) start ;;
    stop) stop ;;
    *) echo "ERROR: must be called with 'start' or 'stop'" >&2; exit 1 ;;
esac