slitaz-boot-scripts view etc/init.d/network.sh @ rev 431

network.sh: fix errors in boot log: not even try to show notifications while Xorg is not running
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon May 23 13:45:07 2016 +0300 (2016-05-23)
parents 018eeb31a351
children d1c11db6adfe
line source
1 #!/bin/sh
2 #
3 # /etc/init.d/network.sh : Network initialization boot script
4 # /etc/network.conf : Main SliTaz network configuration file
5 # /etc/wpa/wpa.conf : Wi-Fi networks configuration file
8 . /etc/init.d/rc.functions
10 CONF="${2:-/etc/network.conf}"
11 [ "$1" != 'netapplet' ] && echo "Loading network settings from $CONF"
12 . "$CONF"
15 # Change LXPanel Network applet settings
17 if [ "$1" == 'netapplet' ]; then
18 if [ "$WIFI" == 'yes' ]; then
19 interface="$WIFI_INTERFACE"
20 else
21 interface="$INTERFACE"
22 fi
24 for i in $(find ${XDG_CONFIG_HOME:-$HOME/.config}/lxpanel -name panel 2>/dev/null); do
25 fgrep -q netstatus "$i" || continue
26 sed -i '/iface/s|=.*$|='$interface'|' "$i"
27 done
28 exit 0
29 fi
31 WPA_CONF='/etc/wpa/wpa.conf'
32 [ ! -e "$WPA_CONF" ] && cp /etc/wpa/wpa_empty.conf $WPA_CONF 2>/dev/null
33 npid='/tmp/notify_pid'
35 # Migrate existing settings to a new format file
37 . /usr/share/slitaz/network.conf_migration
40 # Actions executing on boot time (running network.sh without parameters)
42 boot() {
43 # Set hostname
44 action "Setting hostname to: $(cat /etc/hostname)"
45 /bin/hostname -F /etc/hostname
46 status
48 # Configure loopback interface
49 action 'Configuring loopback...'
50 /sbin/ifconfig lo 127.0.0.1 up
51 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
52 status
54 [ -s /etc/sysctl.conf ] && sysctl -p /etc/sysctl.conf
55 }
58 # Freedesktop notification
60 notification() {
61 ps aux 2>/dev/null | grep -q [X]org || return
62 # FIXME: this valid only for lxde-session
63 local user="$(ps aux 2>/dev/null | grep [l]xde-session | awk 'END{print $2}')"
64 local icon="$1" rpid=''
65 [ -s "$npid" ] && rpid="-r $(cat $npid)"
66 which notify-send >/dev/null &&
67 su -c "notify-send $rpid -p -i $icon 'Network' \"$2\"" - $user | tail -n1 > $npid
68 }
71 # Change LXPanel Network applet interface
73 ch_netapplet() {
74 for user in $(awk -F: '$6 ~ "/home/" {print $1}' /etc/passwd); do
75 # need to be executed as user, due to different XDG variables
76 su -l -c "$0 netapplet" - "$user"
77 done
78 # restart if LXPanel running
79 which lxpanelctl > /dev/null && lxpanelctl restart
80 }
83 # Use ethernet
85 eth() {
86 if [ "$WIFI" != 'yes' ]; then
87 notification network-wired "$(_ 'Starting Ethernet interface %s...' "$INTERFACE")"
88 ifconfig $INTERFACE up
89 sleep 5
90 fi
91 }
94 # Start wpa_supplicant with prepared settings in wpa.conf
96 start_wpa_supplicant() {
97 echo "Starting wpa_supplicant for $1..."
98 wpa_supplicant -B -W -c$WPA_CONF -D$WIFI_WPA_DRIVER -i$WIFI_INTERFACE
99 }
102 # Reconnect to the given network
104 reconnect_wifi_network() {
105 if [ "$WIFI" == 'yes' ]; then
106 # Wpa_supplicant will auto-connect to the first network
107 # notwithstanding to priority when scan_ssid=1
108 current_ssid="$(wpa_cli list_networks 2>/dev/null | fgrep '[CURRENT]' | cut -f2)"
109 if [ "$current_ssid" != "$WIFI_ESSID" ]; then
110 notification network-wireless "$(_ 'Connecting to %s...' "$WIFI_ESSID")"
111 action 'Connecting to $WIFI_ESSID...'
112 for i in $(seq 5); do
113 index=$(wpa_cli list_networks 2>/dev/null | \
114 grep -m1 -F $'\t'$WIFI_ESSID$'\t' | head -n1 | cut -f1)
115 [ -z "$index" ] && echo -n '.' && sleep 1
116 done
117 wpa_cli select_network $index >/dev/null; status
118 fi
119 fi
120 }
123 # Remove selected network settings from wpa.conf
125 remove_network() {
126 mv -f $WPA_CONF $WPA_CONF.old
127 cat $WPA_CONF.old | tr '\n' '\a' | sed 's|[^#]\(network={\)|\n\1|g' | \
128 fgrep -v "ssid=\"$1\"" | tr '\a' '\n' > $WPA_CONF
129 }
132 # For Wi-Fi. Users just have to enable it through WIFI="yes" and usually
133 # ESSID="any" will work and the interface is autodetected.
135 wifi() {
136 if [ "$WIFI" == 'yes' ]; then
137 ifconfig $INTERFACE down
139 # Confirm if $WIFI_INTERFACE is the Wi-Fi interface
140 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
141 echo "$WIFI_INTERFACE is not a Wi-Fi interface, changing it."
142 WIFI_INTERFACE=$(iwconfig 2>/dev/null | awk 'NR==1{print $1}')
143 [ -n "$WIFI_INTERFACE" ] && sed -i \
144 "s|^WIFI_INTERFACE=.*|WIFI_INTERFACE=\"$WIFI_INTERFACE\"|" \
145 /etc/network.conf
146 fi
148 notification network-wireless "$(_ 'Starting Wi-Fi interface %s...' "$WIFI_INTERFACE")"
149 action 'Configuring Wi-Fi interface $WIFI_INTERFACE...'
150 ifconfig $WIFI_INTERFACE up 2>/dev/null
151 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
152 iwconfig $WIFI_INTERFACE txpower on
153 fi
154 status
156 IWCONFIG_ARGS=''
157 [ -n "$WIFI_WPA_DRIVER" ] || WIFI_WPA_DRIVER='wext'
158 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
159 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
160 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
162 # Use "any" network only when it is needed
163 [ "$WIFI_ESSID" != 'any' ] && remove_network 'any'
165 # Clean all / add / change stored networks settings
166 if [ "$WIFI_BLANK_NETWORKS" == 'yes' ]; then
167 echo "Creating new $WPA_CONF"
168 cat /etc/wpa/wpa_empty.conf > $WPA_CONF
169 else
170 if fgrep -q ssid=\"$WIFI_ESSID\" $WPA_CONF; then
171 echo "Change network settings in $WPA_CONF"
172 # Remove given existing network (it's to be appended later)
173 remove_network "$WIFI_ESSID"
174 else
175 echo "Append existing $WPA_CONF"
176 fi
177 fi
179 # Each new network has a higher priority than the existing
180 MAX_PRIORITY=$(sed -n 's|[\t ]*priority=\([0-9]*\)|\1|p' $WPA_CONF | sort -g | tail -n1)
181 PRIORITY=$(( ${MAX_PRIORITY:-0} + 1 ))
183 # Begin network description
184 cat >> $WPA_CONF <<EOT
185 network={
186 ssid="$WIFI_ESSID"
187 EOT
189 # For networks with hidden SSID: write its BSSID
190 [ -n "$WIFI_BSSID" ] && cat >> $WPA_CONF <<EOT
191 bssid=$WIFI_BSSID
192 EOT
193 # Allow probe requests (for all networks)
194 cat >> $WPA_CONF <<EOT
195 scan_ssid=1
196 EOT
198 case x$(echo -n $WIFI_KEY_TYPE | tr a-z A-Z) in
199 x|xNONE) # Open network
200 cat >> $WPA_CONF <<EOT
201 key_mgmt=NONE
202 priority=$PRIORITY
203 }
204 EOT
205 # start_wpa_supplicant NONE
206 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
207 ;;
209 xWEP) # WEP security
210 # Encryption key length: 64 bit (5 ASCII or 10 HEX)
211 # Encryption key length: 128 bit (13 ASCII or 26 HEX)
212 # ASCII key in "quotes", HEX key without quotes
213 case "${#WIFI_KEY}" in
214 10|26) Q='' ;;
215 *) Q='"' ;;
216 esac
217 cat >> $WPA_CONF <<EOT
218 key_mgmt=NONE
219 auth_alg=OPEN SHARED
220 wep_key0=$Q$WIFI_KEY$Q
221 priority=$PRIORITY
222 }
223 EOT
224 start_wpa_supplicant WEP ;;
226 xWPA) # WPA/WPA2-PSK security
227 cat >> $WPA_CONF <<EOT
228 psk="$WIFI_KEY"
229 key_mgmt=WPA-PSK
230 priority=$PRIORITY
231 }
232 EOT
233 start_wpa_supplicant WPA/WPA2-PSK ;;
235 xEAP) # 802.1x EAP security
236 {
237 cat <<EOT
238 key_mgmt=WPA-EAP IEEE8021X
239 eap=$WIFI_EAP_METHOD
240 EOT
241 if [ "$WIFI_EAP_METHOD" == 'PWD' ]; then
242 WIFI_PHASE2=''; WIFI_CA_CERT=''; WIFI_USER_CERT=''; WIFI_ANONYMOUS_IDENTITY=''
243 fi
244 [ -n "$WIFI_CA_CERT" ] && echo -e "\tca_cert=\"$WIFI_CA_CERT\""
245 [ -n "$WIFI_CLIENT_CERT" ] && echo -e "\tclient_cert=\"$WIFI_CLIENT_CERT\""
246 [ -n "$WIFI_IDENTITY" ] && echo -e "\tidentity=\"$WIFI_IDENTITY\""
247 [ -n "$WIFI_ANONYMOUS_IDENTITY" ] && echo -e "\tanonymous_identity=\"$WIFI_ANONYMOUS_IDENTITY\""
248 [ -n "$WIFI_KEY" ] && echo -e "\tpassword=\"$WIFI-KEY\""
249 [ -n "$WIFI_PHASE2" ] && echo -e "\tphase2=\"auth=$WIFI_PHASE2\""
250 echo }
251 } >> $WPA_CONF
252 start_wpa_supplicant '802.1x EAP' ;;
254 xANY)
255 cat >> $WPA_CONF <<EOT
256 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
257 group=CCMP TKIP WEP104 WEP40
258 pairwise=CCMP TKIP
259 psk="$WIFI_KEY"
260 password="$WIFI_KEY"
261 priority=$PRIORITY
262 }
263 EOT
264 start_wpa_supplicant 'any key type' ;;
266 esac
267 INTERFACE=$WIFI_INTERFACE
268 fi
269 }
272 # WPA DHCP script
274 wpa() {
275 wpa_cli -a"/etc/init.d/wpa_action.sh" -B
276 }
279 # For a dynamic IP with DHCP
281 dhcp() {
282 if [ "$DHCP" == 'yes' ]; then
283 echo "Starting udhcpc client on: $INTERFACE..."
284 # Is wpa wireless && wpa_ctrl_open interface up?
285 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" == 'yes' ]; then
286 wpa
287 else
288 # fallback on udhcpc: wep, eth
289 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
290 /var/run/udhcpc.$INTERFACE.pid
291 fi
292 fi
293 }
296 # For a static IP
298 static_ip() {
299 if [ "$STATIC" == 'yes' ]; then
300 echo "Configuring static IP on $INTERFACE: $IP..."
301 if [ -n "$BROADCAST" ]; then
302 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
303 else
304 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
305 fi
307 # Use ip to set gateways if iproute.conf exists
308 if [ -f /etc/iproute.conf ]; then
309 while read line; do
310 ip route add $line
311 done < /etc/iproute.conf
312 else
313 /sbin/route add default gateway $GATEWAY
314 fi
316 # wpa_supplicant waits for wpa_cli
317 [ -d /var/run/wpa_supplicant ] && wpa_cli -B
319 # Multi-DNS server in $DNS_SERVER
320 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
321 {
322 printf 'nameserver %s\n' $DNS_SERVER # Multiple allowed
323 [ -n "$DOMAIN" ] && echo "search $DOMAIN"
324 } >> /etc/resolv.conf
325 for HELPER in /etc/ipup.d/*; do
326 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
327 done
328 fi
329 }
332 # Stopping everything
334 stop() {
335 ch_netapplet
336 notification network-offline "$(_ 'Stopping all interfaces')"
337 echo 'Stopping all interfaces'
338 for iface in $(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d'); do
339 ifconfig $iface down
340 done
341 ifconfig $WIFI_INTERFACE down
343 echo 'Killing all daemons'
344 killall udhcpc
345 killall wpa_supplicant 2>/dev/null
347 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
348 echo 'Shutting down Wi-Fi card'
349 iwconfig $WIFI_INTERFACE txpower off
350 fi
351 }
354 start() {
355 ch_netapplet
356 # stopping only unspecified interfaces
357 interfaces="$(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d')"
358 case $WIFI in
359 # don't stop Wi-Fi Interface if Wi-Fi selected
360 yes) interfaces="$(echo "$interfaces" | sed -e "/^$WIFI_INTERFACE$/d")";;
361 esac
362 for iface in $interfaces; do
363 ifconfig $iface down
364 done
366 eth; wifi
367 dhcp; static_ip
368 reconnect_wifi_network
370 # change default LXPanel panel iface
371 if [ -f /etc/lxpanel/default/panels/panel ]; then
372 sed -i "s/iface=.*/iface=$INTERFACE/" /etc/lxpanel/default/panels/panel
373 fi
374 }
377 # Looking for arguments:
379 case "$1" in
380 '')
381 boot; start ;;
382 start)
383 start ;;
384 stop)
385 stop ;;
386 restart)
387 stop; sleep 2; start ;;
388 *)
389 cat <<EOT
391 $(boldify 'Usage:') /etc/init.d/$(basename $0) [start|stop|restart]
393 Default configuration file is $(boldify '/etc/network.conf')
394 You can specify another configuration file in the second argument:
395 /etc/init.d/$(basename $0) [start|stop|restart] file.conf
397 EOT
398 ;;
399 esac
401 [ -f "$npid" ] && rm "$npid"