network 2.89 KB
Newer Older
1
#! /bin/bash
James Rayner's avatar
James Rayner committed
2

3
. /usr/lib/netctl/globals
4
. "$SUBR_DIR/interface"
5

6

7
8
## Indicate that the network stack for the profile is up
network_ready() {
9
    if ! is_yes "${WaitOnline:-no}" && ! is_yes "${NETWORK_READY:-no}"; then
10
11
12
13
14
15
16
17
18
        do_debug systemd-notify --ready
        NETWORK_READY=yes
    fi
}

## Describe the status of the service for the profile
# $1: status string, should be "online" when the profile gets connected
network_status() {
    do_debug systemd-notify --status="$1"
19
20
21
    if [[ $1 == "online" ]] && is_yes "${WaitOnline:-no}"; then
        WaitOnline=no network_ready
    fi
22
23
}

Jouke Witteveen's avatar
Jouke Witteveen committed
24
25
## Start the loaded profile
network_start() {
26
27
28
29
    report_notice "Starting network profile '$Profile'..."
    if is_interface "$Interface" && interface_is_up "$Interface" && \
       ! is_yes "${ForceConnect:-no}"; then
        report_error "The interface of network profile '$Profile' is already up"
Jim Pryor's avatar
Jim Pryor committed
30
31
        exit 1
    fi
32
33
    if ! "${Connection}_up"; then
        report_error "Failed to bring the network up for profile '$Profile'"
Jim Pryor's avatar
Jim Pryor committed
34
        exit 1
35
    fi
36
    network_ready
Jouke Witteveen's avatar
Jouke Witteveen committed
37
    # Sandbox the eval
38
    if ! ( do_debug eval "$ExecUpPost" ); then
39
40
41
        report_error "ExecUpPost failed for network profile '$Profile'"
        # Failing ExecUpPost will take the connection down
        "${Connection}_down"
42
        exit 1
James Rayner's avatar
James Rayner committed
43
    fi
44
    network_status "online"
45
    report_notice "Started network profile '$Profile'"
Jouke Witteveen's avatar
Jouke Witteveen committed
46
47
48
49
}

## Stop the loaded profile
network_stop() {
50
    report_notice "Stopping network profile '$Profile'..."
Jouke Witteveen's avatar
Jouke Witteveen committed
51
    # Sandbox the eval
52
    if ! ( do_debug eval "$ExecDownPre" ); then
53
54
        report_error "ExecDownPre failed for network profile '$Profile'"
        # Failing ExecDownPre will leave the profile active
Jim Pryor's avatar
Jim Pryor committed
55
56
        exit 1
    fi
57
58
    if ! "${Connection}_down"; then
        report_error "Failed to bring the network down for profile '$Profile'"
59
        exit 1
James Rayner's avatar
James Rayner committed
60
    fi
61
    network_status ""
62
63
64
    if is_interface "$Interface" && interface_is_up "$Interface" && \
       ! is_yes "${ForceConnect:-no}"; then
        report_error "The interface of network profile '$Profile' did not go down"
65
        exit 1
James Rayner's avatar
James Rayner committed
66
    fi
67
    report_notice "Stopped network profile '$Profile'"
Jouke Witteveen's avatar
Jouke Witteveen committed
68
69
70
71
}

## Wait for all enabled profiles to come online within a single timeout
network_wait_online() {
72
73
74
75
76
77
    mapfile -t Profiles < <(list_profiles)
    i=0
    timeout_wait "${TIMEOUT_ONLINE:-120}" \
                 '! until [[ $(sd_call is-enabled "${Profiles[i]}") == "enabled" &&
                             $(sd_status_text "${Profiles[i]}") != "online" ]]; do
                      (( ++i < ${#Profiles[@]} )) || return 0; done'
Jouke Witteveen's avatar
Jouke Witteveen committed
78
79
80
81
82
83
84
85
86
87
}


ensure_root netctl
# Ensure we are not in a transient directory
cd /

if [[ $# -eq 1 && $1 == wait-online ]]; then
    network_wait_online
elif [[ $# -eq 2 && $1 == @(start|stop) ]]; then
88
    # Expose the command
89
    Command=$1
90
    load_profile "$2"
Jouke Witteveen's avatar
Jouke Witteveen committed
91
92
93
94
    "network_$1"
else
    exit_error "Usage: $0 {start|stop|wait-online} [profile]"
fi
95

96

Jim Pryor's avatar
Jim Pryor committed
97
# vim: ft=sh ts=4 et sw=4: