debian-config 15.7 KB
Newer Older
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
#
# Copyright (c) 2017 Igor Pecovnik, igor.pecovnik@gma**.com
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.

# check for root
#
if [[ $EUID != 0 ]]; then
12
	echo "Warning. This script requires root privileges. Exiting ..."
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
13
14
15
16
	sleep 3
	exit
fi

17
18
19
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
	read -n 1 -s -p "Warning. Configurator can't work properly without internet connection. Press CTRL C to stop to stop or any key to ignore and continue."
20
21
22
23
24
	else
	# Install basic stuff
	#
	echo "Downloading dependencies ..."
	apt-get -qq -y --no-install-recommends install bc expect rcconf  >> /dev/null
25
fi
26

27
28
29
30
function wlan_edit ()
{
source /etc/hostapd.conf
exec 3>&1
31
dialog --title "AP configuration" --backtitle "$BACKTITLE" --form "\nWPA2 enabled, advanced config: edit /etc/hostapd.conf\n " 12 58 0 \
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"SSID:"				1 1 "$ssid"				1 31 22 0 \
"Password:"			2 1 "$wpa_passphrase"	2 31 22 0 \
"Channel:"			3 1 "$channel"			3 31 3 0 \
2>&1 1>&3 | {
read -r ssid;read -r wpa_passphrase;read -r channel
if [[ $? = 0 ]]; then
		sed -i "s/^ssid=.*/ssid=$ssid/" /etc/hostapd.conf
		sed -i "s/^wpa_passphrase=.*/wpa_passphrase=$wpa_passphrase/" /etc/hostapd.conf
		sed -i "s/^channel=.*/channel=$channel/" /etc/hostapd.conf
		wpa_psk=$(wpa_passphrase $ssid $wpa_passphrase | grep '^[[:blank:]]*[^[:blank:]#;]' | grep psk | cut -d= -f2-)
		sed -i "s/^wpa_psk=.*/wpa_psk=$wpa_psk/" /etc/hostapd.conf
fi
}
}

47
48
49
50
51
52
function wlan_exceptions ()
{
[[ -n "$(lsmod | grep -w dhd)" && $1 = "on" ]] && echo 'options dhd op_mode=2' >/etc/modprobe.d/ap6212.conf && rmmod dhd && modprobe dhd
[[ -n "$(lsmod | grep -w dhd)" && $1 = "off" ]] && rm /etc/modprobe.d/ap6212.conf && rmmod dhd && modprobe dhd
}

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function get_wlan_interface ()
{
# search for wlan interfaces and provide a selection menu if there are more than one
# selected is stored into variable WLAN_SELECTED
#
	IFS=$'\r\n' GLOBIGNORE='*' command eval  'WLAN_INTERFACES=($(iw dev | grep Interface | sed -e "s/\tInterface //" | sort))'
	local LIST=()
	for i in "${WLAN_INTERFACES[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
	LIST_LENGHT=$((${#LIST[@]}/2));
	if [ "$LIST_LENGHT" -eq 1 ]; then
			WLAN_SELECTED=${WLAN_INTERFACES[0]}
	else
			exec 3>&1
			WLAN_SELECTED=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse --title "Select interface" --clear --menu "" $((6+${LIST_LENGHT})) 30 15 "${LIST[@]}" 2>&1 1>&3)
			exec 3>&-
	fi
}

74
75
76
# make a bacup
cp /etc/network/interfaces /etc/network/interfaces.debian-config.backup

Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
77
78
79
80
81
82
83
# gather some info
#
[[ -f /etc/armbian-release ]] && source /etc/armbian-release && ARMBIAN="Armbian $VERSION $IMAGE_TYPE";
DISTRO=$(lsb_release -is)
DISTROID=$(lsb_release -rs)
BACKTITLE="$ARMBIAN $DISTRO $DISTROID configuration utility, http://www.armbian.com"
TITLE="Configuring $BOARD_NAME"
84
85
WIRELESS_ADAPTER="wlan0"
NETWORK_CONF="/etc/network/interfaces"
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
86
87
88
89

LIST=()
###########################################################################################################################################
# here we add new items to menu. with condition when needed
90

91
92
93
94
if [[ -f /etc/update-motd.d/41-armbian-config ]]; then
	[[ -x /etc/update-motd.d/41-armbian-config ]] && LIST+=( "Remove" "Remove armbian-config from welcome screen" )
	[[ ! -x /etc/update-motd.d/41-armbian-config ]] && LIST+=( "Add" "Add armbian-config to welcome screen" )
fi
95
[[ -f /usr/bin/h3disp && "$LINUXFAMILY" = "sun8i" && "$BRANCH" = "default" && -n $(bin2fex </boot/script.bin 2>/dev/null | grep -w "hdmi_used = 1") ]] && LIST+=( "Display" "set the display resolution" )
96
[[ -n $(nmcli -f DEVICE,TYPE device status | grep wifi) ]] && LIST+=( "Wireless" "Connect to your router" )
97
[[ -n $(grep -w apt /etc/apt/sources.list.d/armbian.list) ]]  && LIST+=( "Nightly" "Switch to daily builds" )
98
LIST+=( "Hotspot" "Manage wireless access point" )
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
99
100
101
LIST+=( "Network" "Edit network settings" )
[[ -f /boot/armbianEnv.txt ]] && LIST+=( "Booting" "Edit boot environment" )
[[ -f /usr/sbin/nand-sata-install ]] && LIST+=( "Install" "Install Armbian to SATA, eMMC, NAND, USB" )
Igor Pecovnik's avatar
Typos.    
Igor Pecovnik committed
102
103
LIST+=( "Services" "Toggle running services" )
[[ "$DISTRO" == "Ubuntu" && "$(modinfo overlay > /dev/null 2>&1; echo $?)" == "0" ]] && LIST+=( "Overlayroot" "Toggle virtual read-only root filesystem" )
104
[[ -f /usr/bin/armbianmonitor ]] && LIST+=( "Monitor" "Simple CLI monitoring" )
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
105
[[ -f /usr/bin/armbianmonitor ]] && LIST+=( "Diagnostics" "Send diagnostics" )
106
[[ -f /usr/bin/softy ]] && LIST+=( "Softy" "Install 3rd party applications" )
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
107
108
109
110
111
112
113
114
115
116
###########################################################################################################################################
LISTLENGHT="$((7+${#LIST[@]}/2))"


# main dialog routine
DIALOG_CANCEL=1
DIALOG_ESC=255

while true; do
	exec 3>&1
117
	selection=$(dialog --backtitle "$BACKTITLE" --title "$TITLE" --clear --cancel-label "Exit to shell" --menu "Please select:" $LISTLENGHT 70 15 \
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
118
119
120
121
122
	"${LIST[@]}" 2>&1 1>&3)
	exit_status=$?
	exec 3>&-
	case $exit_status in
	$DIALOG_CANCEL)
123
124
		#clear
		#echo -e "\n\e[0;33mThank you for using Armbian configuration tool! Support: \e[1m\e[39mwww.armbian.com\x1B[0m\n"
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
125
126
127
128
129
130
131
132
133
134
135
		exit
	;;
	$DIALOG_ESC)
		clear
		exit 1
		;;
	esac

	#######################################################################################################################################
	case $selection in

136
137
138
139
140
141
142
143
	"Remove" )
		chmod -x /etc/update-motd.d/41-armbian-config
	;;

	"Add" )
		chmod +x /etc/update-motd.d/41-armbian-config
	;;

144
145
146
147
	"Display" )
		h3disp
		exit
	;;
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
148
149
150
151
	"Services" )
		rcconf
	;;

152
153
154
155
156
	"Overlayroot" )
		if [[ -n $(mount | grep -w overlay) ]]; then
			dialog --title "Root overlay" --backtitle "$BACKTITLE" --yes-label "Disable" --no-label "Cancel" --yesno "\nYour system is already virtual read-only.\n\nDo you want to disable this feature and reboot?" 9 60
			[[ $? = 0 ]] && overlayroot-chroot sed -i "s/^overlayroot=.*/overlayroot=\"\"/" /etc/overlayroot.conf && reboot
		else
157
			debconf-apt-progress -- apt-get -y --no-install-recommends install overlayroot
158
			echo '#!/bin/bash' > /etc/update-motd.d/97-overlayroot
159
			echo 'echo -e "[\e[0m \e[1mremember: root is in virtual read only mode\e[0m ]\n"' >> /etc/update-motd.d/97-overlayroot
160
161
162
163
164
			dialog --title "Root overlay" --backtitle "$BACKTITLE" --yes-label "Reboot" --no-label "Cancel" --yesno "\nEnable virtual read-only root and reboot." 7 45
			[[ $? = 0 ]] && sed -i "s/^overlayroot=.*/overlayroot=\"tmpfs\"/" /etc/overlayroot.conf && reboot
		fi
	;;

Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
165
	"Network" )
166
		dialog --backtitle "$BACKTITLE" --title "Edit network configuration" --no-collapse --editbox /etc/network/interfaces 30 0 2> /etc/network/interfaces.out
167
		[[ $? = 0 ]] && mv /etc/network/interfaces.out /etc/network/interfaces && service network-manager restart
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
168
169
	;;

170
	"Hotspot" )
171
		CURRENT_UUID=$(nmcli -f UUID,TYPE connection show --active | grep wireless | awk '{print $1}')
172
		if [[ -n $(service hostapd status | grep -w active | grep -w running) ]]; then
173
			dialog --title "Hostapd service is running" --backtitle "$BACKTITLE" --help-button --help-label "Cancel" --yes-label "Stop" --no-label "Edit" --yesno "\nStop: stop and disable\n\nEdit: change basic parameters: SSID, password and channel" 9 70
174
175
176
			exitstatus=$?;
			if [[ $exitstatus = 0 ]]; then service hostapd stop ; sed -i "s/^DAEMON_CONF=.*/DAEMON_CONF=/" /etc/init.d/hostapd; fi
			if [[ $exitstatus = 1 ]]; then wlan_edit; service hostapd stop; sleep 1; service hostapd start; fi
177
178
179
180
		elif [[ -n $CURRENT_UUID ]]; then
				dialog --title "Info" --backtitle "$BACKTITLE" --no-collapse \
				--yesno "\nWireless connection is in use.\n\nDo you want to disconnect?" 9 57
				[[ $? = 0 ]] && nmcli connection down uuid $CURRENT_UUID
181
		else
182
			# change special adapters to AP mode
Igor Pecovnik's avatar
Igor Pecovnik committed
183
			wlan_exceptions "on"
184
185
186
			# check for WLAN interfaces
			get_wlan_interface
			# display dialog
187
			dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nChecking nl80211 hostapd driver compatibility." 5 50
188
189
190
			apt-get -o Dpkg::Options::="--force-confnew" -y -qq --no-install-recommends install hostapd > /dev/null
			# change to selected interface
			sed -i "s/^interface=.*/interface=$WLAN_SELECTED/" /etc/hostapd.conf
191
192
193
194
195
196
			# add hostapd.conf to services
			sed -i "s/^DAEMON_CONF=.*/DAEMON_CONF=\/etc\/hostapd.conf/" /etc/init.d/hostapd
			# check both options
			CHECK=$(systemctl daemon-reload;service hostapd restart;service hostapd status|grep fail)
			if [[ -n "$CHECK" ]]; then
				dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nChecking Realtek hostapd driver compatibility." 5 50
197
198
199
				apt-get -o Dpkg::Options::="--force-confnew" -y -qq --no-install-recommends install hostapd-realtek > /dev/null
				# change to selected interface
				sed -i "s/^interface=.*/interface=$WLAN_SELECTED/" /etc/hostapd.conf
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
			fi
			CHECK=$(systemctl daemon-reload;service hostapd restart;service hostapd status|grep fail)
			# if both fails there is other problem
			if [[ -n "$CHECK" ]]; then
				dialog --backtitle "$BACKTITLE" --title "Warning" --infobox "\nNo compatible hostapd driver found." 5 39
				sed -i "s/^DAEMON_CONF=.*/DAEMON_CONF=/" /etc/init.d/hostapd
				systemctl daemon-reload;service hostapd restart
				sleep 3
				exit
			fi

			dialog --title "Choose a AP mode" --backtitle "$BACKTITLE" --help-button --help-label "Manual" --yes-label "Bridge" --no-label "NAT" --yesno "\nBridge: wireless clients will use your routers DHCP server\n\nNAT: with own DHCP server, out of your primary network\n\nManual: edit configuration manually" 11 70
			response=$?
			DEFAULT_ADAPTER=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')

			TEMP_CONF=/etc/network/interfaces.out
			cp $NETWORK_CONF $TEMP_CONF

			case $response in
				# bridge
				0)
					sed -i '/^#/ d' $TEMP_CONF
					apt-get -qq -y --no-install-recommends install bridge-utils >> /dev/null
					sed -i "s/^auto lo.*/auto lo br0/" $TEMP_CONF
					# if we have dhcp on default adapter
					if [[ -n $(grep dhcp $TEMP_CONF | grep "$DEFAULT_ADAPTER" | grep -v br) ]]; then
						sed -i "s/^iface $DEFAULT_ADAPTER inet dhcp/iface $DEFAULT_ADAPTER inet manual/" $TEMP_CONF
						echo -e "\niface br0 inet dhcp\nbridge_ports $DEFAULT_ADAPTER $WIRELESS_ADAPTER" >> $TEMP_CONF
						echo -e "\nauto $WIRELESS_ADAPTER\niface $WIRELESS_ADAPTER inet manual" >> $TEMP_CONF
					#fi
					# if we have static on default adapter
					#if [[ -n $(grep static $TEMP_CONF | grep "$DEFAULT_ADAPTER" | grep -v br) ]]; then
					else
						sed -i "s/^iface $DEFAULT_ADAPTER inet static/iface $DEFAULT_ADAPTER inet manual/" $TEMP_CONF
						sed -i "/^iface $DEFAULT_ADAPTER inet manual/a iface br0 inet static" $TEMP_CONF
						echo -e "bridge_ports $DEFAULT_ADAPTER $WIRELESS_ADAPTER" >> $TEMP_CONF
						echo -e "\nauto $WIRELESS_ADAPTER\niface $WIRELESS_ADAPTER inet manual" >> $TEMP_CONF
					fi
					sed -i 's/^bridge=.*/bridge=br0/' /etc/hostapd.conf
				;;

				# NAT
				1)
					sed -i '/^#/ d' $TEMP_CONF
244
245
246
247
248
249
250
251
252
253
					apt-get -qq -y --no-install-recommends install dnsmasq iptables
					echo -e "\nallow-hotplug $WIRELESS_ADAPTER\niface $WIRELESS_ADAPTER inet static\naddress 172.24.1.1\nnetmask 255.255.255.0\nnetwork 172.24.1.0\nbroadcast 172.24.1.255" >> $TEMP_CONF
					# create new configuration
					echo "interface=$WIRELESS_ADAPTER				# Use interface $WIRELESS_ADAPTER" > /etc/dnsmasq.conf
					echo "listen-address=172.24.1.1					# Explicitly specify the address to listen on" >> /etc/dnsmasq.conf
					echo "bind-interfaces							# Bind to the interface to make sure we aren't sending things elsewhere" >> /etc/dnsmasq.conf
					echo "server=8.8.8.8							# Forward DNS requests to Google DNS" >> /etc/dnsmasq.conf
					echo "domain-needed								# Don't forward short names" >> /etc/dnsmasq.conf
					echo "bogus-priv								# Never forward addresses in the non-routed address spaces" >> /etc/dnsmasq.conf
					echo "dhcp-range=172.24.1.50,172.24.1.150,12h	# Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time" >> /etc/dnsmasq.conf
254
255
256
257
258
259
260
261
262
					# - Enable IPv4 forwarding
					sed -i "/net.ipv4.ip_forward=/c\net.ipv4.ip_forward=1" /etc/sysctl.conf
					echo 1 > /proc/sys/net/ipv4/ip_forward
					# - Apply iptables
					iptables -t nat -A POSTROUTING -o $DEFAULT_ADAPTER -j MASQUERADE
					iptables -A FORWARD -i $DEFAULT_ADAPTER -o $WIRELESS_ADAPTER -m state --state RELATED,ESTABLISHED -j ACCEPT
					iptables -A FORWARD -i $WIRELESS_ADAPTER -o $DEFAULT_ADAPTER -j ACCEPT
					# - Save IP tables, applied during ifup in /etc/network/interfaces.
					iptables-save > /etc/iptables.ipv4.nat
263
264
265
266
267
					service dnsmasq restart
					sed -i 's/^bridge=.*/#&/' /etc/hostapd.conf
					sed -e 's/exit 0//g' -i /etc/rc.local
					echo "iptables-restore < /etc/iptables.ipv4.nat" >> /etc/rc.local
					echo "exit 0" >> /etc/rc.local
268
269
270
271
				;;
			3) exit;;
			255) exit;;
			esac
272
273
			dialog --title "Manually adjust network configuration if needed" --backtitle "$BACKTITLE" \
			--ok-label "Reboot to apply new settings" --no-collapse --editbox $TEMP_CONF 30 0 2> $TEMP_CONF".tmp"
274
275
276
277
			if [[ $? = 0 ]]; then
				mv $TEMP_CONF $NETWORK_CONF
				reboot
			fi
278
279
280
		fi
	;;

Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
281
282
283
284
285
	"Booting" )
		dialog --title "Edit u-boot environment" --no-collapse --editbox /boot/armbianEnv.txt 30 0 2> /boot/armbianEnv.txt.out
		[[ $? = 0 ]] && mv /boot/armbianEnv.txt.out /boot/armbianEnv.txt
	;;

286
287
288
	"Nightly" )
		sed -i 's/apt.armbian.com/beta.armbian.com/' /etc/apt/sources.list.d/armbian.list
		debconf-apt-progress -- apt-get update
289
		debconf-apt-progress -- apt-get -y upgrade
290
291
292
		dialog --title "Switching to nightly" --backtitle "$BACKTITLE" --yes-label "Reboot" --no-label "Cancel" --yesno \
		"\nReboot to apply new settings?" 7 34
		if [[ $? = 0 ]]; then reboot; fi
293
	;;
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
294
295
296
297
298
	"Install" )
		nand-sata-install
	;;

	"Wireless" )
299
		# scan for wifi modules
300
		array=( $(nmcli dev | grep "wifi" | awk '{print $1}') )
301
302
303
304
305
306
307
		declared_wlans=0;
		for i in "${array[@]}"
			do
			if grep --quiet "^iface $i" /etc/network/interfaces; then
				let declared_wlans+=1
			fi
		done
308
309
310
311
312
		if [[ -n $(service hostapd status | grep -w active | grep -w running) ]]; then
			DIALOG_CANCEL=1
			dialog --title "Hostapd service is running" --backtitle "$BACKTITLE" --help-button --help-label "Cancel" --yes-label "Stop" \
			--no-label "Edit" --yesno "\nStop: stop and disable\n\nEdit: change basic parameters: SSID, password and channel" 9 70
			exitstatus=$?;
Igor Pecovnik's avatar
Igor Pecovnik committed
313
			if [[ $exitstatus = 0 ]]; then service hostapd stop ; sed -i "s/^DAEMON_CONF=.*/DAEMON_CONF=/" /etc/init.d/hostapd; systemctl daemon-reload; fi
314
315
316
317
			if [[ $exitstatus = 1 ]]; then wlan_edit; service hostapd stop; sleep 1; service hostapd start; fi
		elif [ $declared_wlans = ${#array[@]} ]; then
			dialog --title "Error" --backtitle "$BACKTITLE" --no-collapse \
			--msgbox "\nWireless network is in use by if-up service. Remove it from config." 8 57
318
		else
319
			CURRENT_UUID=$(nmcli -f UUID,TYPE connection show --active | grep wireless | awk '{print $1}')
320
			if [[ -n $(service hostapd status | grep -w active | grep -w running) ]]; then
321
322
				dialog --title "Error" --backtitle "$BACKTITLE" --no-collapse \
				--msgbox "\nHostapd service is running. Disable it and try again." 7 57
323
				exit
324
			elif [[ -n $CURRENT_UUID ]]; then
325
326
				dialog --title "Info" --backtitle "$BACKTITLE" --no-collapse \
				--yesno "\nAlready connected via wireless.\n\nDo you want to disconnect?" 9 57
327
				[[ $? = 0 ]] && nmcli connection down uuid $CURRENT_UUID
328
			else
329
330
			# disable AP mode on certain adapters
			wlan_exceptions "off"
331
332
			nmtui-connect
			fi
333
		fi
Igor Pecovnik's avatar
RFC #2    
Igor Pecovnik committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
	;;

	"Diagnostics" )
		clear
		armbianmonitor -u
		echo ""
		read -n 1 -s -p "Press any key to continue"
	;;

	"Softy" )
		softy
	;;

	"Monitor" )
		clear
		armbianmonitor -m | dialog --backtitle "$BACKTITLE" --title "Simple CLI monitoring $BOARD" --progressbox 15 64
	;;

	esac
	#######################################################################################################################################
done