Developing lightweight computation at the DSG edge

qmp_autoconf 3.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/sh /etc/rc.common
#    Copyright (C) 2011 Fundacio Privada per a la Xarxa Oberta, Lliure i Neutral guifi.net
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#    The full GNU General Public License is included in this distribution in
#    the file called "COPYING".

START=99
22
23
STOP=99
SERVICE_DAEMONIZE=1
24
CONTROL_FILE="/qmp_configured"
p4u's avatar
p4u committed
25
BIRTH_FILE="/etc/qmp/.birth"
26
27
QMP_DIR="/etc/qmp"
QMP_KEY="/tmp/qmp_key"
p4u's avatar
p4u committed
28
QMP_LOG="/root/qmp_autoconf.log"
29

30
31
32
log() {
	echo "$@" > /dev/console
	echo "$@"
p4u's avatar
p4u committed
33
	echo "$@" >> $QMP_LOG
34
35
}

p4u's avatar
p4u committed
36
37
38
exechooks() {
	local stage="$1"
	stage=${stage:-anyboot}
39
40
41
	local device="$(cat /tmp/sysinfo/board_name)"
	local hooksdir="/etc/qmp/hooks/$device"

p4u's avatar
p4u committed
42
	[ -n "$device" ] && [ -d "$hooksdir" ] && {
43
44
	for h in $hooksdir/*; do
		echo "Executing hook: $h"
p4u's avatar
p4u committed
45
		sh $h $stage
46
47
48
49
	done
	}
}

50
51
prepare_wifi() {
	# Wifi is complicated...
52

53
	mv /etc/config/wireless /tmp/wireless.old
p4u's avatar
p4u committed
54

55
56
57
	# It is a quick hack, because each time ath9k is loaded the phyX counter increases
	# and openwrt wifi scripts take the device namw (i.e wlan0) from this counter
	# It only works for ath9k	
p4u's avatar
p4u committed
58
59
60
	rmmod ath9k
	insmod ath9k
	sleep 1
61
62

	local index=0
p4u's avatar
p4u committed
63
64
	iw phy | grep Wiphy | awk '{print $2}' | while read d
		do
65
66
			iw phy $d set name $index
			index=$(($index+1))
p4u's avatar
p4u committed
67
68
		done

69
	wifi detect | sed s/"disabled 1"/"country $country"/g > /etc/config/wireless
70
71
	wifi down
	wifi up
p4u's avatar
p4u committed
72
73
74
75
76
77

	log WiFi devices:
	log --------------------------------------------------
	log $(iwinfo | egrep "ESSID|Mode|Type")
	log --------------------------------------------------

78
79
	#To be sure channel is set...
	iw reg set $country || iw reg set 00
80
81
82
83
84
85
86
87
}

configure() {
	log Redirecting stderr to $QMP_LOG
	exec 2>>$QMP_LOG
	log Starting qmp configuration...
	log Waiting 10 seconds to be sure all is configured
	sleep 10
88
	
89
90
91
92
93
94
95
96
97
98
99
100
101
	# Execute birth hooks if it is the first boot of the device
	[ ! -e "$BIRTH_FILE" ] && exechooks birth

	# Execute firstboot hooks 
	log "[Executing hooks]"
	exechooks firstboot

	#Generating default wifi configuration
	log "[Configuring wifi]"
	country="$(uci get qmp.wireless.country 2>/dev/null)"
	country="${country:-00}"
	log Using countrycode $country

102
	#Configuring wifi using QMP
103
	prepare_wifi
104
105
106
107
	. $QMP_DIR/qmp_wireless.sh
	qmp_configure_wifi_initial
	qmp_configure_wifi

108
	#Reloading network to avoid problems with next functions
p4u's avatar
p4u committed
109
	log Reloading network
110
111
	/etc/init.d/network reload
	
112
	#Configuring networking
p4u's avatar
p4u committed
113
	log "[Configuring networking]"
114
	. $QMP_DIR/qmp_functions.sh
115
	qmp_configure_initial
116
	qmp_configure
117

p4u's avatar
p4u committed
118
119
120
	log ==================================================
	log                   Rebooting
	log ==================================================
121
	touch "$CONTROL_FILE"
p4u's avatar
p4u committed
122
	[ -e "$CONTROL_FILE" ] && reboot || log CRITICAL, cannot write file $CONTROL_FILE
123
124
}

p4u's avatar
p4u committed
125
# This function is executed in each boot
126
127
128
129
130
startup() {
	# Set HostName
	. $QMP_DIR/qmp_functions.sh
	qmp_set_hosts

p4u's avatar
p4u committed
131
132
133
	# Run hooks
	exechooks anyboot

134
135
136
137
138
139
140
141
	# Start/Stop netserver
	$QMP_DIR/qmp_control.sh apply_netserver

	# Generate current qmp key
	logread | md5sum | awk '{print $1}' > $QMP_KEY
}

start() {
p4u's avatar
p4u committed
142
	if [ ! -e "$CONTROL_FILE" ]; then
143
144
		configure
	else
145
		log QMP configured. Remove $CONTROL_FILE to force reconfiguration
146
	fi
147
148

	startup
149
150
151
}

stop() {
152
	log Nothing to do
153
154
155
156
157
158
}

restart() {
	stop
	start
}