debian-config-functions 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
#!/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.

9
10
# Functions:
# main
11
# exceptions
12
13
14
15
16
17
18
19
20
21
22
23
# check_if_installed
# is_package_manager_running
# display_qr_code
# beta_disclaimer
# show_box
# description
# aval_kernel
# aval_dtbs
# get_a20modes
# get_h3modes
# add_choose_user
# configure_desktop
24
25

#
26
27
# gather info about the board and start with loading menu
#
28
function main(){
29

30
31
	DIALOG_CANCEL=1
	DIALOG_ESC=255
32

33
34
35
36
37
38
39
40
41
42
43
	[[ -f /etc/armbian-release ]] && source /etc/armbian-release && ARMBIAN="Armbian $VERSION $IMAGE_TYPE";
	DISTRO=$(lsb_release -is)
	DISTROID=$(lsb_release -sc)
	KERNELID=$(uname -r)
	[[ -z "${ARMBIAN// }" ]] && ARMBIAN="$DISTRO $DISTROID"
	BACKTITLE="Configuration utility, $ARMBIAN, https://www.armbian.com"
	TITLE="$BOARD_NAME "
	DEFAULT_ADAPTER=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')
	[[ -z "${DEFAULT_ADAPTER// }" ]] && DEFAULT_ADAPTER="lo"
	OVERLAYDIR="/boot/dtb/overlay";
	[[ "$LINUXFAMILY" == "sunxi64" ]] && OVERLAYDIR="/boot/dtb/allwinner/overlay";
44
45
46
47
48
49
	# detect desktop
	DISPLAY_MANAGER=""; DESKTOP_INSTALLED=""
	check_if_installed nodm && DESKTOP_INSTALLED="nodm";
	check_if_installed lightdm && DESKTOP_INSTALLED="lightdm";
	[[ -n $(service lightdm status 2> /dev/null | grep -w active) ]] && DISPLAY_MANAGER="lightdm"
	[[ -n $(service nodm status 2> /dev/null | grep -w active) ]] && DISPLAY_MANAGER="nodm"
50
51
	dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nLoading Armbian configuration utility ... " 5 45
	sleep 1
52

53
54
55
56
57
}




58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
# naming exceptions for packages
#
function exceptions ()
{

	TARGET_FAMILY=$LINUXFAMILY
	UBOOT_BRANCH=$TARGET_BRANCH # uboot naming is different

	if [[ $TARGET_BRANCH == "default" ]]; then TARGET_BRANCH=""; else TARGET_BRANCH="-"$TARGET_BRANCH; fi
	# pine64
	if [[ $TARGET_FAMILY == pine64 ]]; then
		TARGET_FAMILY="sunxi64"
	fi
	# allwinner legacy kernels
	if [[ $TARGET_FAMILY == sun*i ]]; then
		TARGET_FAMILY="sunxi"
		if [[ $UBOOT_BRANCH == "default" ]]; then
			TARGET_FAMILY=$(cat /proc/cpuinfo | grep "Hardware" | sed 's/^.*Allwinner //' | awk '{print $1;}')
		fi
	fi

}




85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#
# check dpkg status of $1 -- currently only 'not installed at all' case catched
#
check_if_installed (){

	local DPKG_Status="$(dpkg -s "$1" 2>/dev/null | awk -F": " '/^Status/ {print $2}')"
	if [[ "X${DPKG_Status}" = "X" || "${DPKG_Status}" = *deinstall* ]]; then
		return 1
	else
		return 0
	fi
}




#
102
103
104
# check if package manager is doing something
#
function is_package_manager_running() {
105

106
107
108
109
110
111
112
113
114
115
  fuser -s /var/lib/dpkg/lock
  if [[ $? = 0 ]]; then
    # 0 = true
	dialog --colors --title " \Z1Error\Z0 " --backtitle "$BACKTITLE" --no-collapse --msgbox \
	"\n\Z1Package manager is running in the background. \n\nCan't proceed. Try again later." 9 53
    return 0
  else
    # 1 = false
    return 1
  fi
116
117
118
119
120
121

}




122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#
# display qr code for google authemtication method
#
function display_qr_code()
{
		clear
		SECRET=$(head -1 /root/.google_authenticator)
		qrencode -d 9 -8 -t UTF8 "otpauth://totp/test?secret=$SECRET"
		echo -e "\nSetting up your OTP-generator\
		\nInstall Google Authenticator generator application on your mobile phone from Android market (e.g. FreeOTP) or from F-Droid.\
		\nIn the applications menu click the corresponding button to create a new account and either scan the QR code, or enter the secret key manually:\
		\n\n$SECRET \n\nNow you should see a new passcode token being generated every 60 seconds on your phone.\n"  | fold -sw 38
		read -n 1 -s -r -p "Press any key to continue"
}




140
141
142
143
144
145
146
#
# show disclaimer
#
function beta_disclaimer ()
{
exec 3>&1
	ACKNOWLEDGEMENT=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse --title " Warning " \
147
	--clear \--radiolist "\n$1\n \n" 11 56 5 "Yes, I understand" "" off	 2>&1 1>&3)
148
exec 3>&-
149
150
}

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222



#
# show box
#
function show_box ()
{
	dialog --colors --backtitle "$BACKTITLE" --no-collapse --title " $1 " --clear --msgbox "\n$2\n \n" $3 56
}




#-----------------------------------------------------------------------------------------------------------------------------------------#
#
# show description for MOTD files
#
function description
{
	case $1 in
		*header*)
			echo "Big board logo and kernel info"
		;;
		*sysinfo*)
			echo "Sysinfo - load, ip, memory, uptime, ..."
		;;
		*tips*)
			echo "Shows tip of the day"
		;;
		*updates*)
			echo "Display number of avaliable updates"
		;;
		*armbian-config*)
			echo "Show command for system configuration"
		;;
		*autoreboot-warn*)
			echo "Show warning when reboot is needed"
		;;
		*uk.armbian.com*)
			echo "United Kingdom"
		;;
		*.armbian.com*)
			echo "Estonia"
		;;
		*)
		echo ""
		;;
	esac
}




#
# check if board has alternative kernels
#
function aval_kernel ()
{

	IFS=$'\r\n'
	GLOBIGNORE='*'
	AVAL_KERNEL=($(apt-cache search --names-only '^linux-'$(lsb_release	 -cs)'-root.*.'$BOARD'*' \
	| grep -w "$BOARD " | sed 's/.*(\(.*\))/\1/' | awk '{print $1}' | grep -v "$BRANCH" ))
	local LIST=()
	for i in "${AVAL_KERNEL[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
	LIST_LENGHT=$((${#LIST[@]}/2));
	if [ "$LIST_LENGHT" -eq 1 ]; then
			TARGET_BRANCH=${AVAL_KERNEL[0]}
223
			dialog --backtitle "$BACKTITLE" --title " Info " --msgbox  "\nNo alternative kernels available!" 7 38
224
	else
225
226
227
228
229
230
231
232
			beta_disclaimer "Switching between kernels might change functionality of your board or it might fail to boot."
			if [[ -n $ACKNOWLEDGEMENT ]]; then
				exec 3>&1
				TARGET_BRANCH=$(dialog --cancel-label "Cancel" --backtitle "$BACKTITLE" --no-collapse \
				--title "Upgrade from $BRANCH to:" --clear --menu "" $((6+${LIST_LENGHT})) 40 15 "${LIST[@]}" 2>&1 1>&3)
				exitstatus=$?;
				exec 3>&-
			fi
233
234
235
236
237
238
239
240
241
242
	fi

}




#
# check if board has alternative kernels
#
243
function aval_dtbs ()
244
245
{

246
247
	if [[ $LINUXFAMILY == cubox ]]; then
	local width=80
248
249
250
251
252
253
	LIST=("imx6dl-hummingboard.dtb" "HB Solo/DualLite" "imx6dl-hummingboard-emmc-som-v15.dtb" "HB Solo/DualLite v1.5 with eMMC" "imx6dl-hummingboard-som-v15.dtb" "HB Solo/DualLite v1.5" \
	"imx6dl-hummingboard2.dtb" "HB2 Solo/DualLite" "imx6dl-hummingboard2-emmc-som-v15.dtb" "HB2 Solo/DualLite v1.5 with eMMC" "imx6dl-hummingboard2-som-v15.dtb" "HB2 Solo/DualLite v1.5" \
	"imx6q-hummingboard.dtb" "HB Dual/Quad" "imx6q-hummingboard-emmc-som-v15.dtb" "HB Dual/Quad v1.5 with eMMC" "imx6q-hummingboard-som-v15.dtb" "HB Dual/Quad v1.5" \
	"imx6q-hummingboard2.dtb" "HB2 Dual/Quad" "imx6q-hummingboard2-emmc-som-v15.dtb" "HB2 Dual/Quad v1.5 with eMMC" "imx6q-hummingboard2-som-v15.dtb" "HB2 Dual/Quad v1.5" \
	"imx6dl-cubox-i.dtb" "Cubox-i Solo/DualLite" "imx6dl-cubox-i-emmc-som-v15.dtb" "Cubox-i Solo/DualLite v1.5 with eMMC" "imx6dl-cubox-i-som-v15.dtb" "Cubox-i Solo/DualLite v1.5" \
	"imx6q-cubox-i.dtb" "Cubox-i Dual/Quad" "imx6q-cubox-i-emmc-som-v15.dtb" "Cubox-i Dual/Quad v1.5 with eMMC" "imx6q-cubox-i-som-v15.dtb" "Cubox-i Dual/Quad v1.5")
254
255
256
257
	else
	local width=52
	LIST=("xu4" "Odroid XU4" "xu3" "Odroid XU3" "xu3l" "Odroid XU3 Lite" "hc1" "Odroid HC1/HC2")
	fi
258
259
260
261
262
263
264

	LIST_LENGHT=$((${#LIST[@]}/2));
	if [ "$LIST_LENGHT" -eq 1 ]; then
			TARGET_BOARD=${AVAL_KERNEL[0]}
	else
			exec 3>&1
			TARGET_BOARD=$(dialog --cancel-label "Cancel" --backtitle "$BACKTITLE" --no-collapse \
265
			--title "Select optimised board configuration" --clear --menu "" $((6+${LIST_LENGHT})) ${width} 25 "${LIST[@]}" 2>&1 1>&3)
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
			exitstatus=$?;
			exec 3>&-
	fi

}




#
# select video modes for a10 and a20
#
function get_a20modes ()
{

	IFS=$'\r'
	GLOBIGNORE='*'
	SCREEN_RESOLUTION=("1920x1080p60" "1280x720p60" "1920x1080p50" "1280x1024p60" "1024x768p60" "800x600p60" "640x480p60" "1360x768p60" "1440x900p60" "1680x1050p60")
	local LIST=()
	for i in "${SCREEN_RESOLUTION[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
	LIST_LENGHT=$((${#LIST[@]}/2));
	#echo $LIST_LENGHT
	#exit
	if [ "$LIST_LENGHT" -eq 1 ]; then
			SCREEN_RESOLUTION=${SCREEN_RESOLUTION[0]}
	else
			exec 3>&1
			SCREEN_RESOLUTION=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
			--title "Select video mode" --clear --menu "" $((6+${LIST_LENGHT})) 25 $((1+${LIST_LENGHT})) "${LIST[@]}" 2>&1 1>&3)
			exec 3>&-
	fi

}




#
# select video modes for h3
#
function get_h3modes ()
{

	IFS=$'\r\n'
	GLOBIGNORE='*'
	SCREEN_RESOLUTION=($(h3disp -i))
	local LIST=()
	for i in "${SCREEN_RESOLUTION[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
	LIST_LENGHT=$((${#LIST[@]}/2));
	#echo $LIST_LENGHT
	#exit
	if [ "$LIST_LENGHT" -eq 1 ]; then
			SCREEN_RESOLUTION=${SCREEN_RESOLUTION[0]}
	else
			exec 3>&1
			SCREEN_RESOLUTION=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
			--title "Select video mode" --clear --menu "" $((6+${LIST_LENGHT})) 25 $((1+${LIST_LENGHT})) "${LIST[@]}" 2>&1 1>&3)
			exec 3>&-
	fi

}



#
# create or pick unprivileged user
#
function add_choose_user ()
{

	IFS=$'\r\n'
	GLOBIGNORE='*'

	local USERS=($(awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd))
	local LIST=()
	for i in "${USERS[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
	LIST_LENGHT=$((${#LIST[@]}/2));

	if [ "$LIST_LENGHT" -eq 0 ]; then
		dialog --backtitle "$BACKTITLE" --title " Notice " --msgbox "\nWe didn't find any unprivileged user with sudo rights which is required to run this service.\
		\n\nPress enter to create one!" 10 48
		read -t 0 temp
		echo -e "\nPlease provide a username (eg. your forename) or leave blank for canceling user creation: \c"
		read -e username
		CHOSEN_USER="$(echo "$username" | tr '[:upper:]' '[:lower:]' | tr -d -c '[:alnum:]')"
		[ -z "$CHOSEN_USER" ] && return
		echo "Trying to add user $CHOSEN_USER"
		adduser $CHOSEN_USER || return
	elif [ "$LIST_LENGHT" -eq 1 ]; then
			CHOSEN_USER=${USERS[0]}
	else
			exec 3>&1
			CHOSEN_USER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
			--title "Select unprivileged user" --clear --menu "" $((6+${LIST_LENGHT})) 40 15 "${LIST[@]}" 2>&1 1>&3)
			exec 3>&-
	fi

}




#
# configure armbian desktop
#
function configure_desktop ()
{

	add_choose_user

	if [ -n "$CHOSEN_USER" ]; then
		# install desktop and nodm
		debconf-apt-progress -- apt-get update
		debconf-apt-progress -- apt-get -y install armbian-${DISTROID}-desktop
		DEBIAN_FRONTEND=noninteractive debconf-apt-progress -- apt-get -y -qq install nodm
		# add user to groups
		for additionalgroup in sudo netdev audio video dialout plugdev bluetooth systemd-journal ssh; do
				usermod -aG ${additionalgroup} ${CHOSEN_USER} 2>/dev/null
		done

		# fix for gksu in Xenial
		touch /home/${CHOSEN_USER}/.Xauthority
		cp -R /etc/skel/. /home/${CHOSEN_USER}

		# set up profile sync daemon on desktop systems
		which psd >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "${CHOSEN_USER} ALL=(ALL) NOPASSWD: /usr/bin/psd-overlay-helper" >> /etc/sudoers
			touch /home/${CHOSEN_USER}/.activate_psd
		fi

		sed -i "s/NODM_USER=\(.*\)/NODM_USER=${CHOSEN_USER}/" /etc/default/nodm
		sed -i "s/NODM_ENABLED=\(.*\)/NODM_ENABLED=true/g" /etc/default/nodm
		chown -R ${CHOSEN_USER}:${CHOSEN_USER} /home/${CHOSEN_USER}/.
		sleep 3
		service nodm stop
		sleep 1
		service nodm start
	fi

}