1 | #!/bin/sh
|
---|
2 | ########################################################################
|
---|
3 | # Begin /sbin/ifdown
|
---|
4 | #
|
---|
5 | # Description : Interface Down
|
---|
6 | #
|
---|
7 | # Authors : DJ Lucas - dj@linuxfromscratch.org
|
---|
8 | #
|
---|
9 | # Version : 00.02
|
---|
10 | #
|
---|
11 | ########################################################################
|
---|
12 |
|
---|
13 | . /lib/lsb/init-functions
|
---|
14 |
|
---|
15 | function get_args()
|
---|
16 | {
|
---|
17 | if test -z "${1}" ; then
|
---|
18 | showhelp
|
---|
19 | exit 1
|
---|
20 | fi
|
---|
21 |
|
---|
22 | while test -n "${1}" ; do
|
---|
23 | case "${1}" in
|
---|
24 | -c | --configfile)
|
---|
25 | check_arg $1 $2
|
---|
26 | CONFIGFILE="${2}"
|
---|
27 | shift 2
|
---|
28 | ;;
|
---|
29 | -f | --force)
|
---|
30 | FORCE="1"
|
---|
31 | shift 1
|
---|
32 | ;;
|
---|
33 | eth* | iw* | wlan*)
|
---|
34 | INTERFACE="${1}"
|
---|
35 | shift 1
|
---|
36 | ;;
|
---|
37 | -h | --help)
|
---|
38 | showhelp
|
---|
39 | exit 0
|
---|
40 | ;;
|
---|
41 | *)
|
---|
42 | showhelp
|
---|
43 | echo "ERROR: '${1}' unknown argument"
|
---|
44 | echo ""
|
---|
45 | exit 2
|
---|
46 | ;;
|
---|
47 | esac
|
---|
48 | done
|
---|
49 | }
|
---|
50 |
|
---|
51 | function check_arg()
|
---|
52 | {
|
---|
53 | echo "${2}" | grep -v "^-" > /dev/null
|
---|
54 | if [ -z "${?}" -o ! -n "${2}" ]; then
|
---|
55 | echo "Error: ${1} requires a valid argument."
|
---|
56 | exit 2
|
---|
57 | fi
|
---|
58 | }
|
---|
59 |
|
---|
60 | function showhelp()
|
---|
61 | {
|
---|
62 | echo ""
|
---|
63 | echo "`/usr/bin/basename ${0}` brings down a valid network interface."
|
---|
64 | echo ""
|
---|
65 | echo "Options:"
|
---|
66 | echo " -c --configfile The path to an interface configuration file"
|
---|
67 | echo " If no configuration file is given, all files"
|
---|
68 | echo " listed in /etc/network/ifconfig.<int> will"
|
---|
69 | echo " be processed, regarless of the value of ONBOOT"
|
---|
70 | echo " -f --force Flush all IPs and force the interface down."
|
---|
71 | echo " -h --help Show this help message and exit."
|
---|
72 | echo ""
|
---|
73 | echo "Examples:"
|
---|
74 | echo " `/usr/bin/basename ${0}` eth0 -c /run/network/ifconfig.eth0/ipv4"
|
---|
75 | echo " `/usr/bin/basename ${0}` eth0 --force -c /run/network/ifconfig.eth0/ipv4"
|
---|
76 | echo " `/usr/bin/basename ${0}` eth0 --force"
|
---|
77 | echo " `/usr/bin/basename ${0}` eth0"
|
---|
78 | echo ""
|
---|
79 | echo ""
|
---|
80 | }
|
---|
81 |
|
---|
82 | # Intialize empty variables so that the shell does not polute the script
|
---|
83 | CONFIGFILE=""
|
---|
84 | CONFIGDIR=""
|
---|
85 | INTERFACE=""
|
---|
86 | FORCE=""
|
---|
87 | failed=0
|
---|
88 |
|
---|
89 | # Process command line arguments
|
---|
90 | get_args ${@}
|
---|
91 |
|
---|
92 | # Handle common errors - No need to account for bootscripts, this should not
|
---|
93 | # happen during boot or shutdown.
|
---|
94 | if [ "${CONFIGFILE}x" != "x" -a ! -f "${CONFIGFILE}" ]; then
|
---|
95 | echo "ERROR: ${CONFIGFILE} is not a valid network configuration file."
|
---|
96 | echo ""
|
---|
97 | exit 2
|
---|
98 | fi
|
---|
99 |
|
---|
100 | if [ "${INTERFACE}x" == "x" ]; then
|
---|
101 | echo "ERROR: No interface was given"
|
---|
102 | echo ""
|
---|
103 | exit 2
|
---|
104 | else
|
---|
105 | if ! grep "${INTERFACE}" /proc/net/dev 2>&1 > /dev/null; then
|
---|
106 | echo "ERROR: ${INTERFACE} is not a valid network interface."
|
---|
107 | echo ""
|
---|
108 | exit 2
|
---|
109 | fi
|
---|
110 | fi
|
---|
111 |
|
---|
112 | # If a configuration file is present, use it
|
---|
113 | if [ "${CONFIGFILE}x" != "x" ]; then
|
---|
114 | . "${CONFIGFILE}"
|
---|
115 | if [ -x "/lib/network-services/${SERVICE}" ]; then
|
---|
116 | # do the work
|
---|
117 | if IFCONFIG=${CONFIGFILE} \
|
---|
118 | /lib/network-services/${SERVICE} ${INTERFACE} down; then
|
---|
119 | rm "${CONFIGFILE}"
|
---|
120 | fi
|
---|
121 | else
|
---|
122 | echo "ERROR: Service '${SERVICE}' is not a valid service."
|
---|
123 | echo ""
|
---|
124 | exit 2
|
---|
125 | fi
|
---|
126 | # No interface configuration file was given
|
---|
127 | else
|
---|
128 | # Process all running interface configuration files
|
---|
129 | CONFIGDIR="/run/network/ifconfig.${INTERFACE}"
|
---|
130 | if [ -d "${CONFIGDIR}" ]; then
|
---|
131 | FILES=`ls "${CONFIGDIR}"`
|
---|
132 | for CONFIGFILE in ${FILES}
|
---|
133 | do
|
---|
134 | (
|
---|
135 | . "${CONFIGDIR}/${CONFIGFILE}"
|
---|
136 | # No error checking necessary if they are in /run
|
---|
137 | if IFCONFIG="${CONFIGDIR}/${CONFIGFILE}" \
|
---|
138 | /lib/network-services/${SERVICE} ${INTERFACE} down; then
|
---|
139 | rm "${CONFIGDIR}/${CONFIGFILE}"
|
---|
140 | fi
|
---|
141 | )
|
---|
142 | done
|
---|
143 | # all running config files processes, set the link down
|
---|
144 | message="Setting interface ${INTERFACE} down..."
|
---|
145 | /sbin/ip link set "${INTERFACE}" down
|
---|
146 | evaluate_retval standard
|
---|
147 | else
|
---|
148 | if [ "${FORCE}" != "1" ]; then
|
---|
149 | echo "ERROR: No configuration files found for ${INTERFACE}."
|
---|
150 | echo ""
|
---|
151 | exit 2
|
---|
152 | fi
|
---|
153 | fi
|
---|
154 | fi
|
---|
155 |
|
---|
156 | if [ "${FORCE}" == "1" ]; then
|
---|
157 | /sbin/ip addr flush dev "${INTERFACE}" 2>&1 > /dev/null || failed=1
|
---|
158 | if [ "${failed}" == "1" ]; then
|
---|
159 | log_failure_msg "Flushing IP addresses from interface ${INTERFACE}..."
|
---|
160 | echo ""
|
---|
161 | exit 1
|
---|
162 | else
|
---|
163 | log_success_msg "Flushing IP addresses from interface ${INTERFACE}..."
|
---|
164 | fi
|
---|
165 | /sbin/ip link set dev "${INTERFACE}" down 2>&1 > /dev/null || failed=1
|
---|
166 | if [ "${failed}" == "1" ]; then
|
---|
167 | log_failure_msg "Setting link down for interface ${INTERFACE}..."
|
---|
168 | echo ""
|
---|
169 | exit 1
|
---|
170 | else
|
---|
171 | log_success_msg "Setting link down for interface ${INTERFACE}..."
|
---|
172 | fi
|
---|
173 | fi
|
---|
174 |
|
---|
175 | exit "${failed}"
|
---|
176 |
|
---|