Developing lightweight computation at the DSG edge

qmp_autoconf 3.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/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".
20
21
22
23
#
# Contributors:
#	Simó Albert i Beltran
#
24
25

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

34
log() {
35
36
	echo -e "$@" > /dev/console
	echo -e "$@"
37
38
}

p4u's avatar
p4u committed
39
exechooks() {
40
41
42
	local stage=${1:-anyboot}
	. $QMP_DIR/qmp_common.sh
	qmp_hooks_exec $stage
43
44
}

45
46
47
48
49
50
51
stop_services() {
	/etc/init.d/dnsmasq stop
	/etc/init.d/gwck stop
	/etc/init.d/bmx6 stop
	/etc/init.d/uhttpd stop
}

52
prepare_wifi() {
53
	i=0
54
	radios=$(uci show wireless | grep wireless\.radio.*wifi-device -c)
55
	log "Detected $radios WiFi device(s)"
56
	while [ $i -lt $radios ]; do
57
		log "Enabling radio${i}"
58
59
60
61
		uci set wireless.radio${i}.disabled="0" 2>/dev/null
		uci set wireless.radio${i}.country="$country" 2>/dev/null
		i=$(($i+1))
	done
62

63
	uci commit wireless
64
	log "Enabling WiFi and waiting 10 seconds to come up..."
65
	wifi up
66
	sleep 10
67
	log ""
p4u's avatar
p4u committed
68

69
70

	log "Detected WiFi devices:"
p4u's avatar
p4u committed
71
	log --------------------------------------------------
72
	log "$(iwinfo)"
p4u's avatar
p4u committed
73
	log --------------------------------------------------
74
75
76
}

configure() {
77
	stop_services
78
	log Redirecting stderr to $QMP_LOG
79
	exec 2>>$QMP_LOG 1>&2
80
81
82
83
84
	log "[Starting qMp autoconfiguration]"
	log "Waiting 15 seconds to make sure everything else is ready..."
	sleep 15
	log "... done!"
	log ""
85

86
	log "[WiFi devices detection]"
87
	country="$(uci get qmp.wireless.country 2>/dev/null)"
88
	country="${country:-US}"
89
90
	log "Using country code $country."
	log "Preparing wifi devices"
91
	prepare_wifi
92
93
	log "... done!"
	log ""
94
95

	# Execute birth hooks if it is the first boot of the device
96
	log "[Birth hooks]"
97
	[ ! -e "$BIRTH_FILE" ] && exechooks birth && touch "$BIRTH_FILE"
98
99
	log "... done!"
	log ""
100
101

	# Configure system and reboot
102
	log "[Loading qMp functions]"
103
	. $QMP_DIR/qmp_functions.sh
104
105
106
107
	log "... done!"
	log ""

	log "[Initial qMp system configuration]"
108
	qmp_configure_initial
109
110
111
112
	log "... done!"
	log ""

	log "[Full qMp system configuration]"
113
	qmp_configure
114
115
	log "... done!"
	log ""
116

117
	log "[Finishing qMp autoconfiguration]"
118
	touch "$CONTROL_FILE"
119
	[ -f "$CONTROL_FILE" ] && log "Rebooting..." && reboot || log "CRITICAL ERROR, cannot write file $CONTROL_FILE"
120
121
}

122
# This function is executed on each boot
123
124
startup() {
	# Set HostName
125
	. $QMP_DIR/qmp_system.sh
126
	qmp_set_hosts
127
	[ $(qmp_uci_get services.bwtest) -eq 1 ] && qmp_enable_netserver
128

p4u's avatar
p4u committed
129
130
131
	# Run hooks
	exechooks anyboot

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

135
	# Start firewall
136
	sh /etc/firewall.user
137
138
139
}

start() {
p4u's avatar
p4u committed
140
	if [ ! -e "$CONTROL_FILE" ]; then
141
142
		echo "qMp is not configured. Starting autoconfiguration..."
		echo ""
143
144
		configure
	else
145
		echo "qMp is already configured. Delete file $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
}