debian-config-functions 22.6 KB
Newer Older
1
2
#!/bin/bash
#
3
# Copyright (c) Authors: http://www.armbian.com/authors, info@armbian.com
4
5
6
7
8
#
# 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
# check_desktop
12
# exceptions
13
14
15
16
17
18
# check_if_installed
# is_package_manager_running
# display_qr_code
# beta_disclaimer
# show_box
# description
19
# generic_select
20
# reload_bsp
21
# other_kernel_version
22
23
24
25
# aval_dtbs
# get_a20modes
# get_h3modes
# add_choose_user
26
# google_token_allusers
27
# configure_desktop
28

29

30
#
31
32
# gather info about the board and start with loading menu
#
33
function main(){
34

35
36
	DIALOG_CANCEL=1
	DIALOG_ESC=255
37

Igor Pečovnik's avatar
Igor Pečovnik committed
38
39
40
41
42
43
44
45
46
	# we have our own lsb_release which does not use Python. Others shell install it here
	if [[ ! -f /usr/bin/lsb_release ]]; then
		if is_package_manager_running; then
			sleep 3
		fi
		debconf-apt-progress -- apt-get update
		debconf-apt-progress -- apt -y -qq --allow-downgrades --no-install-recommends install lsb-release
	fi

47
48
49
50
51
	[[ -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"
Igor Pečovnik's avatar
bugfix    
Igor Pečovnik committed
52
	DEFAULT_ADAPTER=$(ip -4 route ls | grep default | tail -1 | grep -Po '(?<=dev )(\S+)')
Igor Pecovnik's avatar
Igor Pecovnik committed
53
	LOCALIPADD=$(ip -4 addr show dev $DEFAULT_ADAPTER | awk '/inet/ {print $2}' | cut -d'/' -f1)
54
55
	BACKTITLE="Configuration utility, $ARMBIAN"
	[[ -n "$LOCALIPADD" ]] && BACKTITLE=$BACKTITLE", "$LOCALIPADD
Igor Pecovnik's avatar
Igor Pecovnik committed
56
	TITLE="$BOARD_NAME "
57
58
59
	[[ -z "${DEFAULT_ADAPTER// }" ]] && DEFAULT_ADAPTER="lo"
	OVERLAYDIR="/boot/dtb/overlay";
	[[ "$LINUXFAMILY" == "sunxi64" ]] && OVERLAYDIR="/boot/dtb/allwinner/overlay";
60
	[[ "$LINUXFAMILY" == "meson64" ]] && OVERLAYDIR="/boot/dtb/amlogic/overlay";
61
	# detect desktop
62
	check_desktop
63
64
	dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nLoading Armbian configuration utility ... " 5 45
	sleep 1
65

66
67
68
69
}



Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
70

71
72
73
74
#
# compare two strings in dot separated version format
#
vercomp () {
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

	if [[ $1 == $2 ]]
	then
		return 0
	fi
	local IFS=.
	local i ver1=($1) ver2=($2)
	# fill empty fields in ver1 with zeros
	for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
	do
		ver1[i]=0
	done
	for ((i=0; i<${#ver1[@]}; i++))
	do
		if [[ -z ${ver2[i]} ]]
		then
			# fill empty fields in ver2 with zeros
			ver2[i]=0
		fi
		if ((10#${ver1[i]} > 10#${ver2[i]}))
		then
			return 1
		fi
		if ((10#${ver1[i]} < 10#${ver2[i]}))
		then
			return 2
		fi
	done
	return 0

105
106
107
108
109
110
111
112
113
}




#
# test compare two strings $1="3.4.12" $2="5.4.2" $3="<" returns 0 if relation is correct
#
testvercomp () {
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
114
115
116
117
118
119
120
121
122

	vercomp $1 $2
	case $? in
		0) op='=';;
		1) op='>';;
		2) op='<';;
	esac
	if [[ $op != $3 ]]
	then
123
		return 1
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
124
	else
125
		return 0
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
126
127
	fi

128
129
130
131
}



132

133
134
135
136
137
#
# read desktop parameters
#
function check_desktop()
{
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
138
139
140
141
142
143
144
145
146

	DISPLAY_MANAGER=""; DESKTOP_INSTALLED=""
	check_if_installed nodm && DESKTOP_INSTALLED="nodm";
	check_if_installed lightdm && DESKTOP_INSTALLED="lightdm";
	check_if_installed lightdm && DESKTOP_INSTALLED="gnome";
	[[ -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"
	[[ -n $(service gdm status 2> /dev/null | grep -w active) ]] && DISPLAY_MANAGER="gdm"

147
148
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
#
# 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

}




179
#
JC Staudt's avatar
JC Staudt committed
180
# check dpkg status of $1 -- currently only 'not installed at all' case caught
181
182
183
184
185
186
187
188
189
#
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
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
190

191
192
193
194
195
196
}




#
197
198
199
# check if package manager is doing something
#
function is_package_manager_running() {
200

201
	if ps -C apt-get,apt,dpkg >/dev/null ; then
Aristo Chen's avatar
Aristo Chen committed
202
203
		[[ -z $scripted ]] && dialog --colors --title " \Z1Error\Z0 " --backtitle "$BACKTITLE" --no-collapse --msgbox \
		"\n\Z0Package manager is running in the background. \n\nCan't install dependencies. Try again later." 9 53
204
205
206
207
		return 0
	else
		return 1
	fi
208
209
210
211
212

}



Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
213

Igor Pecovnik's avatar
Igor Pecovnik committed
214
215
216
217
218
#
# wget with dialog progress bar $1=URL $2=parameters
#
function fancy_wget()
{
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
219
220
221
222
223

	LANG=C wget $2 --progress=bar:force:noscroll $1 2>&1 | stdbuf -i0 -o0 -e0 tr '>' '\n' | \
	stdbuf -i0 -o0 -e0 sed -rn 's/^.*\<([0-9]+)%\[.*$/\1/p' | dialog --backtitle "$BACKTITLE" --title " Downloading " \
	--gauge "Please wait" 7 70 0

Igor Pecovnik's avatar
Igor Pecovnik committed
224
225
226
}


227

Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
228

229
#
JC Staudt's avatar
JC Staudt committed
230
# display qr code for authentication method
231
232
233
#
function display_qr_code()
{
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
234

Aristo Chen's avatar
Aristo Chen committed
235
236
237
238
239
	clear
	SECRET=$(head -1 /root/.google_authenticator)
	qrencode -m 2 -d 9 -8 -t ANSI256 "otpauth://totp/test?secret=$SECRET"
	echo -e "\nHow to setup your one type password generator?\n"
	echo -e "\nInstall a one-time password authenticator on your mobile device (e.g. FreeOTP) from the Android \
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
240
241
242
market or F-Droid.	\n\nIn the application 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\nYou should now see a new passcode token being \
generated every 60 seconds on your phone.\n"  | fold -sw 60
Aristo Chen's avatar
Aristo Chen committed
243
	read -n 1 -s -r -p "Press any key to continue"
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
244

245
246
247
248
249
}




250
251
252
253
254
#
# show disclaimer
#
function beta_disclaimer ()
{
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
255
256
257
258
259
260

	exec 3>&1
	ACKNOWLEDGEMENT=$(dialog --colors --nocancel --backtitle "$BACKTITLE" --no-collapse --title " Warning " \
	--clear \--radiolist "\n$1\n \n" 0 56 5 "Yes, I understand" "" off	 2>&1 1>&3)
	exec 3>&-

261
262
}

263
264
265
266
267
268
269
270



#
# show box
#
function show_box ()
{
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
271

272
	dialog --colors --backtitle "$BACKTITLE" --no-collapse --title " $1 " --clear --msgbox "\n$2\n \n" $3 56
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
273

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
}




#
# 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*)
Chris Rohlfs's avatar
Chris Rohlfs committed
295
			echo "Display number of available updates"
296
297
298
299
300
301
302
303
304
305
		;;
		*armbian-config*)
			echo "Show command for system configuration"
		;;
		*autoreboot-warn*)
			echo "Show warning when reboot is needed"
		;;
		*uk.armbian.com*)
			echo "United Kingdom"
		;;
Igor Pečovnik's avatar
Igor Pečovnik committed
306
307
308
		*mirrors.tuna.tsinghua.edu.cn/armbian/*)
			echo "China"
		;;
Igor Pečovnik's avatar
bugfix    
Igor Pečovnik committed
309
		*mirrors.netix.net/armbian/apt/*)
310
			echo "Bulgarija"
Aristo Chen's avatar
Aristo Chen committed
311
		;;
Igor Pečovnik's avatar
Igor Pečovnik committed
312
313
		*mirrors.dotsrc.org/armbian-apt/*)
			echo "Denmark"
Aristo Chen's avatar
Aristo Chen committed
314
		;;
315
316
317
318
		*.armbian.com*)
			echo "Estonia"
		;;
		*)
Aristo Chen's avatar
Aristo Chen committed
319
			echo ""
320
321
322
323
324
		;;
	esac
}


325
326
327
328
329
#
# Generic select box
#
function generic_select()
{
Aristo Chen's avatar
Aristo Chen committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
	IFS=$' '
	PARAMETER=($1)
	local LIST=()
	for i in "${PARAMETER[@]}"
	do
		if [[ -n $3 ]]; then
			[[ ${i[0]} -ge $3 ]] && \
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
		else
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
		fi
	done
	LIST_LENGTH=$((${#LIST[@]}/2));
	if [ "$LIST_LENGTH" -eq 1 ]; then
		PARAMETER=${PARAMETER[0]}
	else
		exec 3>&1
		PARAMETER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
		--title "$2" --clear --menu "" $((6+${LIST_LENGTH})) 0 $((1+${LIST_LENGTH})) "${LIST[@]}" 2>&1 1>&3)
		exec 3>&-
	fi
351
352
353
}


354
355


356
357
358
359
#
# reload kernel, bsp and armbian-config
#
function reload_bsp(){
360

361
362
363
	# switch to defined branch
	[[ -n "$1" ]] && BRANCH=$1

364
365
366
367
368
	# deal with exceptions
	if [[ $BRANCH == current || $BRANCH == dev ]]; then
		[[ ${LINUXFAMILY} == rk3399 ]] && LINUXFAMILY=rockchip64
	fi

369
	clear
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
370
	debconf-apt-progress -- apt-get update
Igor Pecovnik's avatar
Igor Pecovnik committed
371
372

	# must exits
373
374
	PACKAGE_INSTALL="linux-image-${BRANCH}-${LINUXFAMILY} linux-dtb-${BRANCH}-${LINUXFAMILY}"
	PACKAGE_PURGE="linux-image* linux-dtb*"
Igor Pecovnik's avatar
Igor Pecovnik committed
375

376
	# create install and remove logic
Igor Pecovnik's avatar
Igor Pecovnik committed
377
378
	command=$(LC_ALL=C apt-get --download-only --simulate --allow-downgrades -y --no-install-recommends install linux-${DISTROID}-root-${BRANCH}-${BOARD} 2>/dev/null)
	if [[ $? -eq 0 && -z $(echo $command | grep "not possible") ]]; then
379
380
381
382
		PACKAGE_INSTALL+=" linux-${DISTROID}-root-${BRANCH}-${BOARD}"
		PACKAGE_PURGE+=" linux-${DISTROID}-root*"
	fi

Igor Pecovnik's avatar
Igor Pecovnik committed
383
384
	command=$(LC_ALL=C apt-get --download-only --simulate --reinstall --allow-downgrades -y --no-install-recommends install linux-u-boot-${BOARD}-${BRANCH} 2>/dev/null)
	if [[ $? -eq 0 && -z $(echo $command | grep "not possible") ]]; then
385
386
387
388
389
390
391
392
393
394
395
396
397
		PACKAGE_INSTALL+=" linux-u-boot-${BOARD}-${BRANCH}"
		PACKAGE_PURGE+=" linux-u-boot-${BOARD}-*"
	fi

	if check_if_installed armbian-${DISTROID}-desktop ; then
		PACKAGE_INSTALL+=" armbian-${DISTROID}-desktop"
		PACKAGE_PURGE+=" armbian-${DISTROID}-desktop*"
	fi

	if check_if_installed linux-headers-${BRANCH}-${FAMILY} ; then
		PACKAGE_INSTALL+=" linux-headers-${BRANCH}-${FAMILY}"
		PACKAGE_PURGE+=" linux-headers*"
	fi
Igor Pečovnik's avatar
Igor Pečovnik committed
398

Igor Pecovnik's avatar
Igor Pecovnik committed
399
	debconf-apt-progress -- apt-get --download-only --allow-downgrades -y --no-install-recommends install $PACKAGE_INSTALL
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
400
401
	# if download is ok, remove old packages
	if [[ $? = 0 ]]; then
402

403
		debconf-apt-progress -- apt-get -y -qq purge $PACKAGE_PURGE
404
405
406
407
		find "/boot/" -name "System.map*" -type f -delete
		find "/boot/" -name "config*" -type f -delete
		find "/boot/" -name "vmlinuz*" -type f -delete
		find "/boot/" -name "*nitrd*" -type f -delete
Igor Pecovnik's avatar
Igor Pecovnik committed
408
		find "/boot/" -name "*Image" -type l -delete
409

Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
410
		# install packages
411
		echo $PACKAGE_INSTALL >> /var/log/upgrade.log
412
		debconf-apt-progress -- apt-get -y -qq --allow-downgrades --no-install-recommends --reinstall \
413
		-o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install $PACKAGE_INSTALL
414

415
		if [[ $? -eq 1 ]]; then
416
			echo "Something went wrong ... check logs."; exit;
Igor Pečovnik's avatar
Igor Pečovnik committed
417
		else
418
			apt clean
419
		fi
420

Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
421
422
	fi

423
424
425
}


Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
426
427


428
429
430
function other_kernel_version ()
{

Igor Pečovnik's avatar
Igor Pečovnik committed
431
432
	IFS=$'\r\n'
	GLOBIGNORE='*'
433
	local HIDDEN='hidden'
Igor Pečovnik's avatar
Igor Pečovnik committed
434
435
436
437

	# get current kernel information
	CURRENT_VERSION_TEMP=$(dpkg -l | grep '^ii' | grep linux-image)
	CURRENT_VERSION=$(echo $CURRENT_VERSION_TEMP | awk '{print $2}')"="$(echo $CURRENT_VERSION_TEMP | awk '{print $3}')
Igor Pečovnik's avatar
Igor Pečovnik committed
438

439
	# Merge families and handle exceptions
440
441
442
	[[ ${LINUXFAMILY} == sun*i ]] && LINUXFAMILY=sunxi
	[[ ${LINUXFAMILY} == pine64 ]] && LINUXFAMILY=sunxi64
	[[ ${LINUXFAMILY} == sun*iw* ]] && LINUXFAMILY=sunxi64
443
	[[ ${LINUXFAMILY} == cubox || ${LINUXFAMILY} == udoo ]] && LINUXFAMILY=imx6
Igor Pečovnik's avatar
Igor Pečovnik committed
444
445
	[[ ${BOARD} == odroidc2 || ${BOARD} == kvim1 || ${BOARD} == lafrite || ${BOARD} == lepotato || ${BOARD} == nanopik2-s905 ]] && HIDDEN="legacy"
	[[ ${LINUXFAMILY} == odroidn2 ]] && LINUXFAMILY=meson64
446

Igor Pečovnik's avatar
Igor Pečovnik committed
447
	# check what is available from the repository
Igor Pecovnik's avatar
Bugfix    
Igor Pecovnik committed
448
	debconf-apt-progress -- apt-get update
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
449
450
	LIST=($(apt-cache show linux-image*${LINUXFAMILY} | grep -E  "Package:|Version:|version:" \
	| grep -v "Config-Version" | sed -n -e 's/^.*: //p' | sed 's/\.$//g'))
451
	new_list=()
Igor Pečovnik's avatar
Igor Pečovnik committed
452
453

	# create a human readable menu
454
455
	for ((n=0;n<$((${#LIST[@]}));n++));
	do
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
456
457
458
459
460
		m=$(( $n + 1 ))
		prvi=$((3*$m - 3))
		drugi=$((3*$m - 2))
		tretji=$((3*$m - 1))
		[[ -z ${LIST[$prvi]} ]] && break
461
		if [[ $CURRENT_VERSION != "${LIST[$prvi]}=${LIST[$drugi]}" && ${LIST[$prvi]} != *${HIDDEN}* ]]; then
Igor Pečovnik's avatar
Igor Pečovnik committed
462
463
464
			new_list+=( "${LIST[$prvi]}=${LIST[$drugi]}" )
			new_list+=( ${LIST[$tretji]} )
		fi
Igor Pečovnik's avatar
Igor Pečovnik committed
465
466
		local tmp="${LIST[$prvi]}=${LIST[$drugi]}${LIST[$tretji]}"
		[[ ${#tmp} -gt $chrlen ]] && local chrlen=${#tmp}
467
468
469
470
	done

	# copy back to main array
	LIST=("${new_list[@]}")
JC Staudt's avatar
JC Staudt committed
471
	LIST_LENGTH=$((${#LIST[@]}/2));
472

JC Staudt's avatar
JC Staudt committed
473
	if [ "$LIST_LENGTH" -eq 0 ]; then
Igor Pečovnik's avatar
Igor Pečovnik committed
474
		dialog --backtitle "$BACKTITLE" --title " Warning " --msgbox  "\nNo other kernels available!" 7 32
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
475
476
477
	else
		beta_disclaimer "Switching between kernels might change functionality of your board or it might fail to boot. \
		\n\n\Z1In case of troubles expect no help!\Z0"
Igor Pečovnik's avatar
Igor Pečovnik committed
478
479
		if [[ -n $ACKNOWLEDGEMENT ]]; then
			exec 3>&1
Igor Pečovnik's avatar
Igor Pečovnik committed
480
481
			TARGET_VERSION=$(dialog --ok-label "Reboot" --cancel-label "Cancel" --backtitle "$BACKTITLE" --no-collapse \
			--title "Select kernel and reboot" --clear --menu "" $((6+${LIST_LENGTH})) $((7+${chrlen})) 25 "${LIST[@]}" 2>&1 1>&3)
Igor Pečovnik's avatar
Igor Pečovnik committed
482
483
484
485
			exitstatus=$?;
			exec 3>&-
			if [[ $exitstatus = 0 ]]; then
				IFS=" "
Igor Pečovnik's avatar
Igor Pečovnik committed
486

487
488
489
490
491
492
493
				# determine upgrade packages
				UPGRADE_ROOT="default"
				UPGRADE_UBOOT=""
				[[ $TARGET_VERSION == *legacy* ]] && UPGRADE_ROOT="-legacy" && UPGRADE_UBOOT="legacy"
				[[ $TARGET_VERSION == *next* ]] && UPGRADE_ROOT="-next"  && UPGRADE_UBOOT="next"
				[[ $TARGET_VERSION == *current* ]] && UPGRADE_ROOT="-current" && UPGRADE_UBOOT="current"
				[[ $TARGET_VERSION == *dev* ]] && UPGRADE_ROOT="-dev" && UPGRADE_UBOOT="dev"
Igor Pečovnik's avatar
Igor Pečovnik committed
494

Igor Pečovnik's avatar
Igor Pečovnik committed
495
496
497
498
499
				# install packages
				PACKAGE_LIST="$TARGET_VERSION"
				TARGET_VERSION_DTB=${TARGET_VERSION/image/dtb}
				TARGET_VERSION_PRE=$(echo $TARGET_VERSION_DTB | cut -f1 -d"=")
				TARGET_VERSION_SUB=$(echo $TARGET_VERSION_DTB | cut -f2 -d"=")
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
500
				[[ -n $(apt-cache madison "$TARGET_VERSION_PRE" | grep $TARGET_VERSION_SUB ) ]] && \
Igor Pečovnik's avatar
Igor Pečovnik committed
501
502
503
				# installs u-boot only if exits
				apt-cache show linux-u-boot-${BOARD}-${UPGRADE_UBOOT} 2> /dev/null
				[[ $? -eq 0 ]] && PACKAGE_LIST=$PACKAGE_LIST" linux-u-boot-${BOARD}-${UPGRADE_UBOOT}"
Igor Pečovnik's avatar
Igor Pečovnik committed
504

Igor Pečovnik's avatar
Igor Pečovnik committed
505
				PACKAGE_LIST=$PACKAGE_LIST" $TARGET_VERSION_DTB"
Igor Pečovnik's avatar
Igor Pečovnik committed
506
				echo $PACKAGE_LIST > /tmp/switch_kernel.log 2>&1
507
				debconf-apt-progress -- apt --download-only --allow-downgrades -y --no-install-recommends install $PACKAGE_LIST
Igor Pečovnik's avatar
Igor Pečovnik committed
508

Igor Pečovnik's avatar
Igor Pečovnik committed
509
510
				if [[ $? = 0 ]]; then
					dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nRemoving current kernel ..." 5 36
Igor Pečovnik's avatar
Igor Pečovnik committed
511

512
					# remove old kernel
Igor Pečovnik's avatar
Igor Pečovnik committed
513
514
					debconf-apt-progress -- apt -y -qq purge linux-u-boot* linux-image* linux-dtb* linux-headers* linux-${DISTROID}-root*

515
					# cleanup
Igor Pečovnik's avatar
Igor Pečovnik committed
516
517
					find "/boot/" -name "System.map*" -type f -delete;	find "/boot/" -name "config*" -type f -delete
					find "/boot/" -name "vmlinuz*" -type f -delete;	find "/boot/" -name "*nitrd*" -type f -delete
Igor Pecovnik's avatar
Igor Pecovnik committed
518
					apt clean
Igor Pečovnik's avatar
Igor Pečovnik committed
519
520
521
					# BSP must be installed separate otherwise it won't work
					debconf-apt-progress -- apt -y -qq --allow-downgrades --no-install-recommends install linux-${DISTROID}-root${UPGRADE_ROOT}-${BOARD}
					debconf-apt-progress -- apt -y -qq --allow-downgrades --no-install-recommends install $PACKAGE_LIST
522
523
524
525
526
527
528
529
530
531
					if [[ $? = 0 ]]; then
						# update boot loader
						[[ -f /usr/lib/u-boot/platform_install.sh ]] && source /usr/lib/u-boot/platform_install.sh
						#recognize_root
						root_uuid=$(sed -e 's/^.*root=//' -e 's/ .*$//' < /proc/cmdline)
						root_partition=$(blkid | tr -d '":' | grep "${root_uuid}" | awk '{print $1}')
						root_partition_device="${root_partition::-2}"
						write_uboot_platform "$DIR" "${root_partition_device}"
						reboot
					fi
Igor Pečovnik's avatar
Igor Pečovnik committed
532
				else
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
533
534
					dialog --backtitle "$BACKTITLE" --title "Warning" --msgbox \
					"\nTest install failed. Can't change firmware \n\nCheck /tmp/switch_kernel.log" 9 48
535
				fi
536
			fi
Igor Pečovnik's avatar
Igor Pečovnik committed
537
		fi
538
539
540
541
542
	fi
}



Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
543

544
545
546
#
# check if board has alternative kernels
#
547
function aval_dtbs ()
548
549
{

550
	if [[ $LINUXFAMILY == cubox ]]; then
Aristo Chen's avatar
Aristo Chen committed
551
552
553
554
555
556
557
		local width=80
		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")
558
	else
Aristo Chen's avatar
Aristo Chen committed
559
560
		local width=52
		LIST=("xu4" "Odroid XU4" "xu3" "Odroid XU3" "xu3l" "Odroid XU3 Lite" "hc1" "Odroid HC1/HC2")
561
	fi
562

JC Staudt's avatar
JC Staudt committed
563
564
	LIST_LENGTH=$((${#LIST[@]}/2));
	if [ "$LIST_LENGTH" -eq 1 ]; then
Aristo Chen's avatar
Aristo Chen committed
565
		TARGET_BOARD=${AVAL_KERNEL[0]}
566
	else
Aristo Chen's avatar
Aristo Chen committed
567
568
569
570
571
572
		exec 3>&1
		TARGET_BOARD=$(dialog --cancel-label "Cancel" --backtitle "$BACKTITLE" --no-collapse \
		--title "Select optimised board configuration" --clear --menu \
		"" $((6+${LIST_LENGTH})) ${width} 25 "${LIST[@]}" 2>&1 1>&3)
		exitstatus=$?;
		exec 3>&-
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
	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
Aristo Chen's avatar
Aristo Chen committed
592
		LIST+=( "${i[0]//[[:blank:]]/}" "" )
593
	done
JC Staudt's avatar
JC Staudt committed
594
595
	LIST_LENGTH=$((${#LIST[@]}/2));
	#echo $LIST_LENGTH
596
	#exit
JC Staudt's avatar
JC Staudt committed
597
	if [ "$LIST_LENGTH" -eq 1 ]; then
Aristo Chen's avatar
Aristo Chen committed
598
		SCREEN_RESOLUTION=${SCREEN_RESOLUTION[0]}
599
	else
Aristo Chen's avatar
Aristo Chen committed
600
601
602
603
604
		exec 3>&1
		SCREEN_RESOLUTION=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
		--title "Select video mode" --clear --menu \
		"" $((6+${LIST_LENGTH})) 25 $((1+${LIST_LENGTH})) "${LIST[@]}" 2>&1 1>&3)
		exec 3>&-
605
606
607
608
609
610
611
	fi

}




612
613
614
615
616
617
#
# select video modes for odroid c1/c2
#
function get_odroidmodes ()
{

Aristo Chen's avatar
Aristo Chen committed
618
619
620
	IFS=$'\r\n'
	GLOBIGNORE='*'
	SCREEN_RESOLUTION=($(cat /boot/boot.ini | grep -w "# setenv" | grep "hz" | cut -d'"' -f 2))
621
	SCREEN_RESOLUTION=($(cat /boot/boot.ini | grep "Progressive" | grep -v "setenv" | cut -d'"' -f 2))
Aristo Chen's avatar
Aristo Chen committed
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
	local LIST=()
	for i in "${SCREEN_RESOLUTION[@]}"
	do
		LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
	LIST_LENGTH=$((${#LIST[@]}/2));
	#echo $LIST_LENGTH
	#exit
	if [ "$LIST_LENGTH" -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_LENGTH})) 25 $((1+${LIST_LENGTH})) "${LIST[@]}" 2>&1 1>&3)
		exec 3>&-
	fi
639
640
641
642
643
644

}




645
646
647
648
649
650
651
652
#
# select video modes for h3
#
function get_h3modes ()
{

	IFS=$'\r\n'
	GLOBIGNORE='*'
Igor Pečovnik's avatar
Igor Pečovnik committed
653
	SCREEN_RESOLUTION=($(h3disp -i clean))
654
655
656
	local LIST=()
	for i in "${SCREEN_RESOLUTION[@]}"
	do
Aristo Chen's avatar
Aristo Chen committed
657
		LIST+=( "${i[0]//[[:blank:]]/}" "" )
658
	done
JC Staudt's avatar
JC Staudt committed
659
660
	LIST_LENGTH=$((${#LIST[@]}/2));
	#echo $LIST_LENGTH
661
	#exit
JC Staudt's avatar
JC Staudt committed
662
	if [ "$LIST_LENGTH" -eq 1 ]; then
Aristo Chen's avatar
Aristo Chen committed
663
		SCREEN_RESOLUTION=${SCREEN_RESOLUTION[0]}
664
	else
Aristo Chen's avatar
Aristo Chen committed
665
666
667
668
669
		exec 3>&1
		SCREEN_RESOLUTION=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
		--title "Select video mode" --clear --menu \
		"" $((6+${LIST_LENGTH})) 25 $((1+${LIST_LENGTH})) "${LIST[@]}" 2>&1 1>&3)
		exec 3>&-
670
671
672
673
674
675
	fi

}



676

677
678
679
680
681
682
683
684
685
686
687
688
689
#
# 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
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
690
		LIST+=( "${i[0]//[[:blank:]]/}" "" )
691
	done
JC Staudt's avatar
JC Staudt committed
692
	LIST_LENGTH=$((${#LIST[@]}/2));
693

JC Staudt's avatar
JC Staudt committed
694
	if [ "$LIST_LENGTH" -eq 0 ]; then
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
695
696
		dialog --backtitle "$BACKTITLE" --title " Notice " --msgbox \
		"\nWe didn't find any unprivileged user with sudo rights which is required to run this service.\
697
698
699
700
701
702
703
704
		\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
JC Staudt's avatar
JC Staudt committed
705
	elif [ "$LIST_LENGTH" -eq 1 ]; then
Aristo Chen's avatar
Aristo Chen committed
706
		CHOSEN_USER=${USERS[0]}
707
	else
Aristo Chen's avatar
Aristo Chen committed
708
709
710
711
		exec 3>&1
		CHOSEN_USER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
		--title "Select unprivileged user" --clear --menu "" $((6+${LIST_LENGTH})) 40 15 "${LIST[@]}" 2>&1 1>&3)
		exec 3>&-
712
713
714
715
716
717
718
	fi

}




719
720
721
722
723
724
#
# Copy Google token to all local users.
#
function google_token_allusers ()
{

Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
725
726
727
728
729
730
731
732
733
734
735
736
	if [[ -f /root/.google_authenticator ]]; then
		local USERS=($(awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd))
		for i in "${USERS[@]}"
		do
			USER=${i[0]//[[:blank:]]/}
			if [[ -d /home/$USER ]]; then
				cp /root/.google_authenticator /home/$USER/
				chown ${USER}:${USER} /home/${USER}/.google_authenticator
			fi
		done
	fi

737
738
739
740
741
}




742
743
744
745
746
747
748
749
750
#
# configure armbian desktop
#
function configure_desktop ()
{

	add_choose_user

	if [ -n "$CHOSEN_USER" ]; then
751
752

		# update packages
753
		debconf-apt-progress -- apt-get update
754

Igor Pečovnik's avatar
Igor Pečovnik committed
755
756
757
758
		# install new package if exists
		unset PACKAGE_SUFIX
		[[ -n $(apt-cache search --names-only "^armbian-${DISTROID}-desktop-xfce$") ]] && PACKAGE_SUFIX="-xfce"

759
760
		# remove desktop package to secure proper install
		if check_if_installed armbian-${DISTROID}-desktop ; then
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
761
762
			debconf-apt-progress -- apt-get -y \
			remove armbian-${DISTROID}-desktop${PACKAGE_SUFIX} lightdm lightdm-gtk-greeter
763
764
765
		fi

		# install desktop package
Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
766
767
768
		debconf-apt-progress -- apt-get --reinstall -o Dpkg::Options::="--force-confdef" \
		-o Dpkg::Options::="--force-confold" -y \
		install $1 armbian-${DISTROID}-desktop${PACKAGE_SUFIX} lightdm lightdm-gtk-greeter
root's avatar
root committed
769

Igor Pečovnik's avatar
Igor Pečovnik committed
770
771
		# in case previous install was interrupted
		[[ $? -eq 130 ]] && dpkg --configure -a
root's avatar
root committed
772

773
		# clean apt cache
Igor Pecovnik's avatar
Igor Pecovnik committed
774
		apt clean
775

776
		# add user to groups
777
		for additionalgroup in sudo netdev audio video dialout plugdev input bluetooth systemd-journal ssh; do
778
779
780
				usermod -aG ${additionalgroup} ${CHOSEN_USER} 2>/dev/null
		done

Igor Pecovnik's avatar
Cleanup    
Igor Pecovnik committed
781
782
		# Prevent loading paralel printer port drivers which we don't need here.
		# suppress boot error if kernel modules are absent
783
		if [[ -f /etc/modules-load.d/cups-filters.conf ]]; then
Igor Pečovnik's avatar
Igor Pečovnik committed
784
785
786
			sed "s/^lp/#lp/" -i /etc/modules-load.d/cups-filters.conf
			sed "s/^ppdev/#ppdev/" -i /etc/modules-load.d/cups-filters.conf
			sed "s/^parport_pc/#parport_pc/" -i /etc/modules-load.d/cups-filters.conf
787
788
		fi

789
790
791
792
793
794
795
		# enable show windows content on stronger boards
		cpu_cores=$(grep -c '^processor' /proc/cpuinfo | sed 's/^0$/1/')
		if [[ ${cpu_cores} -gt 2 && -f /etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml ]]; then
			sed -i 's/<property name="box_move" type="bool" value=".*/<property name="box_move" type="bool" value="false"\/>/g' \
			/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml
		fi

796
797
798
799
800
801
		# 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
Igor Pečovnik's avatar
Igor Pečovnik committed
802
		if [[ $? -eq 0 && -z $(grep overlay-helper /etc/sudoers) ]]; then
803
804
805
806
			echo "${CHOSEN_USER} ALL=(ALL) NOPASSWD: /usr/bin/psd-overlay-helper" >> /etc/sudoers
			touch /home/${CHOSEN_USER}/.activate_psd
		fi

Igor Pečovnik's avatar
Igor Pečovnik committed
807
808
809
810
811
812
		mkdir -p /etc/lightdm/lightdm.conf.d
		echo "[Seat:*]" > /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
		echo "autologin-user=$CHOSEN_USER" >> /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
		echo "autologin-user-timeout=0" >> /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
		echo "user-session=xfce" >> /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
		ln -s /lib/systemd/system/lightdm.service /etc/systemd/system/display-manager.service >/dev/null 2>&1
Igor Pečovnik's avatar
bugfix    
Igor Pečovnik committed
813
814
		# fix permissions
		chown -R ${CHOSEN_USER}:${CHOSEN_USER} /home/${CHOSEN_USER}/.
Igor Pečovnik's avatar
Igor Pečovnik committed
815
		service lightdm start >/dev/null 2>&1
816
817
	fi

818
}