forked from MichaIng/DietPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dietpi-update
515 lines (353 loc) · 15.8 KB
/
dietpi-update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/bin/bash
#Load the whole script into memory.
{
#////////////////////////////////////
# DietPi Update
#
#////////////////////////////////////
# Created by Daniel Knight / [email protected] / dietpi.com
#
#////////////////////////////////////
#
# Info:
# - Updates DietPi from Git or dietpi.com repo
# - Uses patch_file for incremental online patching
# - Uses pre-patch_file for critical fixes and update related changes
#
# Usage:
# - dietpi-update = Normal
# - dietpi-update 1 = noninteractive update
# - dietpi-update 2 = Check for updates. print server_version to /DietPi/dietpi/.update_available (-1=new image required)
# - dietpi-update -1 = Include reapplying the current subversion patch, e.g. to update dev branch
#////////////////////////////////////
#Import DietPi-Globals ---------------------------------------------------------------
. /DietPi/dietpi/func/dietpi-globals
G_PROGRAM_NAME='DietPi-Update'
G_CHECK_ROOT_USER
G_CHECK_ROOTFS_RW
G_INIT
#Import DietPi-Globals ---------------------------------------------------------------
INPUT=0
disable_error=1 G_CHECK_VALIDINT $1 && INPUT=$1
#/////////////////////////////////////////////////////////////////////////////////////
#UPDATE Vars
#/////////////////////////////////////////////////////////////////////////////////////
FP_LOG='/var/tmp/dietpi/logs/dietpi-update.log'
FP_TMP_LOG='dietpi-update.log'
DIETPIUPDATE_VERSION_CORE=6 # Version of dietpi-update / set server_version-6 line one to value++ and obsolete previous dietpi-update scripts
CHANGELOG_DOWNLOADED=0 #Prevent redownload of changelog if already done in this session
SERVER_ONLINE=0
UPDATE_AVAILABLE=0
UPDATE_REQUIRESNEWIMAGE=0
RUN_UPDATE=0
GITOWNER_TARGET="$(grep -m1 '^[[:blank:]]*DEV_GITOWNER=' /DietPi/dietpi.txt | sed 's/^[^=]*=//')"
GITOWNER_TARGET="${GITOWNER_TARGET:-Fourdee}"
GITBRANCH_TARGET="$(grep -m1 '^[[:blank:]]*DEV_GITBRANCH=' /DietPi/dietpi.txt | sed 's/^[^=]*=//')"
GITBRANCH_TARGET="${GITBRANCH_TARGET:-master}"
COREVERSION_SERVER=0
SUBVERSION_SERVER=0
RCVERSION_SERVER=0
INFO_CURRENT_VERSION=''
INFO_SERVER_VERSION=''
INFO_VERSIONS_UPDATE(){
local git_current_info=''
local git_server_info=''
if [[ $G_GITBRANCH != $GITBRANCH_TARGET || $G_GITOWNER != $GITOWNER_TARGET ]]; then
git_current_info=" (branch: $G_GITOWNER/$G_GITBRANCH)"
git_server_info=" (branch: $GITOWNER_TARGET/$GITBRANCH_TARGET)"
fi
INFO_CURRENT_VERSION="Current version : v$G_DIETPI_VERSION_CORE.$G_DIETPI_VERSION_SUB.$G_DIETPI_VERSION_RC$git_current_info"
INFO_SERVER_VERSION="Latest version : v$COREVERSION_SERVER.$SUBVERSION_SERVER.$RCVERSION_SERVER$git_server_info"
}
URL_MIRROR_INDEX=0
URL_MIRROR_SERVERVERSION=(
"https://raw.githubusercontent.com/$GITOWNER_TARGET/DietPi/$GITBRANCH_TARGET/dietpi/server_version-6"
"https://dietpi.com/downloads/dietpi-update_mirror/$GITBRANCH_TARGET/server_version-6"
)
URL_MIRROR_PREPATCH=(
"https://raw.githubusercontent.com/$GITOWNER_TARGET/DietPi/$GITBRANCH_TARGET/dietpi/pre-patch_file"
"https://dietpi.com/downloads/dietpi-update_mirror/$GITBRANCH_TARGET/pre-patch_file"
)
URL_MIRROR_ZIP=(
"https://github.com/$GITOWNER_TARGET/DietPi/archive/$GITBRANCH_TARGET.zip"
"https://dietpi.com/downloads/dietpi-update_mirror/$GITBRANCH_TARGET/DietPi-$GITBRANCH_TARGET.zip"
)
URL_MIRROR_CHANGELOG=(
"https://raw.githubusercontent.com/$GITOWNER_TARGET/DietPi/$GITBRANCH_TARGET/CHANGELOG.txt"
"https://dietpi.com/downloads/dietpi-update_mirror/$GITBRANCH_TARGET/CHANGELOG.txt"
)
Get_Server_Version(){
#Get server version file
for ((i=0; i<${#URL_MIRROR_SERVERVERSION[@]}; i++))
do
G_DIETPI-NOTIFY 2 "Checking mirror: ${URL_MIRROR_SERVERVERSION[$i]}"
if curl -sL "${URL_MIRROR_SERVERVERSION[$i]}" > server_version; then
#Get server version info
COREVERSION_SERVER=$(sed -n 1p server_version)
SUBVERSION_SERVER=$(sed -n 2p server_version)
RCVERSION_SERVER=$(sed -n 3p server_version)
#Check if server_version contains valid intergers.
if disable_error=1 G_CHECK_VALIDINT $COREVERSION_SERVER &&
disable_error=1 G_CHECK_VALIDINT $SUBVERSION_SERVER &&
disable_error=1 G_CHECK_VALIDINT $RCVERSION_SERVER; then
SERVER_ONLINE=1
G_DIETPI-NOTIFY 0 "Using update server: ${URL_MIRROR_SERVERVERSION[$i]}"
URL_MIRROR_INDEX=$i
INFO_VERSIONS_UPDATE
break
else
G_DIETPI-NOTIFY 2 "Invalid server version string: ${COREVERSION_SERVER:-NULL}.${SUBVERSION_SERVER:-NULL}.${RCVERSION_SERVER:-NULL}"
fi
else
G_DIETPI-NOTIFY 2 "No response from: ${URL_MIRROR_SERVERVERSION[$i]} ($(<server_version))"
fi
done
}
Check_Update_Available(){
#Server online?
if (( $SERVER_ONLINE )); then
#Clear previous .update_available file
[[ -f /DietPi/dietpi/.update_available ]] && rm /DietPi/dietpi/.update_available
#Update Requires new image?
if (( $DIETPIUPDATE_VERSION_CORE < $COREVERSION_SERVER )); then
UPDATE_REQUIRESNEWIMAGE=1
echo '-1' > /DietPi/dietpi/.update_available
#Update available
elif (( $G_DIETPI_VERSION_SUB < $SUBVERSION_SERVER || $G_DIETPI_VERSION_RC < $RCVERSION_SERVER )); then
UPDATE_AVAILABLE=1
echo ''
G_DIETPI-NOTIFY 0 'Update available'
G_DIETPI-NOTIFY 2 "$INFO_CURRENT_VERSION"
G_DIETPI-NOTIFY 2 "$INFO_SERVER_VERSION"
#Write update available version to file.
echo "$COREVERSION_SERVER.$SUBVERSION_SERVER.$RCVERSION_SERVER" > /DietPi/dietpi/.update_available
fi
else
G_DIETPI-NOTIFY 1 'Unable to access update servers. Please check your connection, then run dietpi-update again.'
exit 1
fi
}
Get_View_Changelog(){
local fp_changelog='CHANGELOG.txt'
if (( ! $CHANGELOG_DOWNLOADED )); then
for ((i=0; i<${#URL_MIRROR_CHANGELOG[@]}; i++))
do
G_DIETPI-NOTIFY 2 "Checking mirror: ${URL_MIRROR_CHANGELOG[$i]}"
if wget "${URL_MIRROR_CHANGELOG[$i]}" -O $fp_changelog; then
CHANGELOG_DOWNLOADED=1
break
else
G_DIETPI-NOTIFY 2 "No response from: ${URL_MIRROR_CHANGELOG[$i]}"
fi
done
fi
if (( $CHANGELOG_DOWNLOADED )); then
G_WHIP_VIEWFILE $fp_changelog
else
G_WHIP_MSG 'Failed to download the changelog, please try again.'
fi
}
#/////////////////////////////////////////////////////////////////////////////////////
# MENUS
#/////////////////////////////////////////////////////////////////////////////////////
Menu_Update(){
while :
do
G_WHIP_BUTTON_CANCEL_TEXT='Exit'
G_WHIP_MENU_ARRAY=(
'' '●─ Update DietPi '
'Update' ": Apply $COREVERSION_SERVER.$SUBVERSION_SERVER.$RCVERSION_SERVER update."
'' '●─ Additional Options '
'Changelog' ': View the changelog and patch notes.'
'Backup' ': Create a system backup, before updating.'
)
G_WHIP_DEFAULT_ITEM='Update'
G_WHIP_MENU "Update available:
- Installed version : v$G_DIETPI_VERSION_CORE.$G_DIETPI_VERSION_SUB.$G_DIETPI_VERSION_RC
- Latest version : v$COREVERSION_SERVER.$SUBVERSION_SERVER.$RCVERSION_SERVER
Please select 'Update' option to apply the update."
if (( $? == 0 )); then
if [[ ${G_WHIP_RETURNED_VALUE,,} == 'update' ]]; then
G_WHIP_YESNO "
------------------------Notice------------------------
- A benefit of DietPi is: We use standard Linux (Debian) configurations and commands.
- A potential downside is: We can't possibly accommodate or predict all modification to Linux configurations files by the end user, outside of DietPi programs, during updates.
Although we test the updates thoroughly, if you have made any custom changes to Linux configuration files outside of the DietPi programs, an update may trigger a potential issue.
------------------------------------------------------
Do you wish to continue and update DietPi to v$COREVERSION_SERVER.$SUBVERSION_SERVER.$RCVERSION_SERVER?"
if (( $? == 0 )); then
RUN_UPDATE=1
break
fi
elif [[ ${G_WHIP_RETURNED_VALUE,,} == 'changelog' ]]; then
Get_View_Changelog
elif [[ ${G_WHIP_RETURNED_VALUE,,} == 'backup' ]]; then
G_PROMPT_BACKUP
fi
else
break
fi
done
}
#/////////////////////////////////////////////////////////////////////////////////////
# Update DietPi
#/////////////////////////////////////////////////////////////////////////////////////
Run_Update(){
#Applying pre-patch
G_RUN_CMD wget "${URL_MIRROR_PREPATCH[$URL_MIRROR_INDEX]}" -O pre-patch_file
chmod +x pre-patch_file
if ! ./pre-patch_file $G_DIETPI_VERSION_SUB; then
G_DIETPI-NOTIFY 1 "An error occured during pre-patch $?. Please check terminal messages for errors, or $FP_LOG, and retry dietpi-update in case of follow-up errors."
exit 1
fi
#Update APT packages
G_AGUP
G_AGUG
#Git clone Zip method (no need to install Git)
if curl -kL "${URL_MIRROR_ZIP[$URL_MIRROR_INDEX]}" > update.zip; then
l_message='Unpack update archive' G_RUN_CMD unzip update.zip
rm update.zip
#Remove setting files/info from Git that are not to be updated on client
rm -R DietPi-"$GITBRANCH_TARGET"/dietpi/.??*
rm DietPi-"$GITBRANCH_TARGET"/dietpi/server_version*
rm DietPi-"$GITBRANCH_TARGET"/dietpi/pre-patch_file
#Remove folders of "non-critical scripts" before updating them. (eg: so we dont need to patch for /conf/* file removals)
# rm -R /DietPi/dietpi/conf #:https://github.com/Fourdee/DietPi/issues/905#issuecomment-298241622
# rm -R /DietPi/dietpi/func
# rm -R /DietPi/dietpi/misc
l_message='Copy DietPi core files to RAMdisk' G_RUN_CMD cp -Rf DietPi-"$GITBRANCH_TARGET"/dietpi /DietPi/
l_message='Copy rootfs files in place' G_RUN_CMD cp -Rf DietPi-"$GITBRANCH_TARGET"/rootfs/. /
l_message='Set execute permissions for DietPi scripts' G_RUN_CMD chmod -R +x /DietPi /var/lib/dietpi/services /etc/cron.*/dietpi /etc/profile.d/dietpi-*.sh /etc/bashrc.d/dietpi-*.sh
systemctl daemon-reload
#Verify/update dietpi.txt entries:
/DietPi/dietpi/func/dietpi-set_software verify_dietpi.txt
G_DIETPI-NOTIFY 3 "$G_PROGRAM_NAME" 'Running incremental patching'
G_DIETPI-NOTIFY 2 "$INFO_CURRENT_VERSION"
G_DIETPI-NOTIFY 2 "$INFO_SERVER_VERSION"
# - Subversion update, run patch
while (( $G_DIETPI_VERSION_SUB < $SUBVERSION_SERVER )); do
G_DIETPI-NOTIFY 2 "Patching $G_DIETPI_VERSION_CORE.$G_DIETPI_VERSION_SUB.$G_DIETPI_VERSION_RC to $G_DIETPI_VERSION_CORE.$(( $G_DIETPI_VERSION_SUB + 1 ))"
/DietPi/dietpi/patch_file $G_DIETPI_VERSION_SUB
G_DIETPI_VERSION_SUB=$(( $G_DIETPI_VERSION_SUB + 1 ))
G_DIETPI_VERSION_RC=$RCVERSION_SERVER #Cancel any RC updates, as subversions override it
done
# - RC update, rerun current subversion as patch.
while (( $G_DIETPI_VERSION_RC < $RCVERSION_SERVER )); do
G_DIETPI-NOTIFY 2 "Patching $G_DIETPI_VERSION_CORE.$G_DIETPI_VERSION_SUB.$G_DIETPI_VERSION_RC to $G_DIETPI_VERSION_CORE.$G_DIETPI_VERSION_SUB.$RCVERSION_SERVER"
/DietPi/dietpi/patch_file $(( $G_DIETPI_VERSION_SUB - 1 )) #Rerun current subversion patch for RC's
G_DIETPI_VERSION_RC=$RCVERSION_SERVER
done
G_DIETPI-NOTIFY 0 "Patching to v$G_DIETPI_VERSION_CORE.$G_DIETPI_VERSION_SUB.$G_DIETPI_VERSION_RC completed\n"
#Remove patch_file
rm /DietPi/dietpi/patch_file
#Update local version ID
# - Update current Git owner/branch info
G_GITOWNER="$GITOWNER_TARGET"
G_GITBRANCH="$GITBRANCH_TARGET"
# - Save current version and Git owner/branch info
G_VERSIONDB_SAVE
# - Update info strings
INFO_VERSIONS_UPDATE
#Unable to download file.
else
G_DIETPI-NOTIFY 1 'Download failed, unable to run update. Please try running dietpi-update again.'
exit 1
fi
}
#/////////////////////////////////////////////////////////////////////////////////////
# Main Loop
#/////////////////////////////////////////////////////////////////////////////////////
#----------------------------------------------------------------
#Inform user
G_DIETPI-NOTIFY 3 "$G_PROGRAM_NAME" 'Checking for DietPi updates'
#----------------------------------------------------------------
#Get versions
Get_Server_Version
#----------------------------------------------------------------
#Check if update is available
# Reapply current subversion patch, e.g. when using dev branch.
if (( $INPUT == -1 )); then
if (( $G_DIETPI_VERSION_SUB < 0 )); then
G_DIETPI-NOTIFY 1 'Repatch was requested, but will be skipped, as your device seems to be on lowest subversion already'
else
G_DIETPI-NOTIFY 0 'Repatch was requested: Update will reapply the current subversion patch'
G_DIETPI_VERSION_SUB=$(( $G_DIETPI_VERSION_SUB - 1 ))
fi
INPUT=1
fi
Check_Update_Available
#----------------------------------------------------------------
# $INPUT == 2: Check for updates only. Send result to global file for use by dietpi-banner.
if (( $INPUT != 2 )); then
#Server offline
if (( ! $SERVER_ONLINE )); then
G_WHIP_MSG 'Error:\n\Update servers are either offline, or, unable to connect\n\nNo updates applied.'
#Update requires new DietPi image
elif (( $UPDATE_REQUIRESNEWIMAGE )); then
G_WHIP_MSG "New image required\n\nThe installed version of DietPi is now obsolete and cannot be updated.\n
Please download the latest DietPi image:\n - https://dietpi.com/#download \n\n - $INFO_CURRENT_VERSION\n - $INFO_SERVER_VERSION"
G_DIETPI-NOTIFY 1 "The installed version of DietPi is now obsolete and cannot be updated.
Please download the latest DietPi image:\n - https://dietpi.com/#download \n\n - $INFO_CURRENT_VERSION\n - $INFO_SERVER_VERSION\n"
#Update available
elif (( $UPDATE_AVAILABLE )); then
#Verify userdata location
G_CHECK_USERDATA
#Insufficient free space
G_CHECK_FREESPACE / 100 || exit 1
#Noninteractive update
if (( $INPUT == 1 )); then
RUN_UPDATE=1
G_DIETPI-NOTIFY 0 'Update is being applied, please wait...'
#Ask for update
else
Menu_Update
fi
#No updates
else
echo ''
G_DIETPI-NOTIFY 2 "$INFO_CURRENT_VERSION"
G_DIETPI-NOTIFY 2 "$INFO_SERVER_VERSION"
G_DIETPI-NOTIFY 0 'No updates required, your DietPi installation is up to date.\n'
fi
#----------------------------------------------------------------
#Run Update
if (( $RUN_UPDATE )); then
#Stop Services
/DietPi/dietpi/dietpi-services stop
#Run update and patcher
[[ -f $FP_LOG ]] && rm $FP_LOG
#Run_Update #: https://github.com/Fourdee/DietPi/issues/1877#issuecomment-403866204
# - Pre-estimate and override terminal size variables, since they cannot be estimated, if STOUT and STERR are redirected: https://github.com/Fourdee/DietPi/issues/2105
export G_WHIP_SIZE_X_OVERRIDE=$(tput cols)
export G_WHIP_SIZE_Y_OVERRIDE=$(tput lines)
Run_Update > >(tee $FP_TMP_LOG) 2>&1
unset G_WHIP_SIZE_X_OVERRIDE G_WHIP_SIZE_Y_OVERRIDE
mv $FP_TMP_LOG $FP_LOG
#.update file stage (only used on 1st run of dietpi-software to check/apply updates, 0 tells dietpi-software to reboot)
echo 0 > /DietPi/dietpi/.update_stage
#Remove update_available file
[[ -f /DietPi/dietpi/.update_available ]] && rm /DietPi/dietpi/.update_available
#Update DietPi-Survey
/DietPi/dietpi/dietpi-survey 1
#Sync to disk now: https://dietpi.com/phpbb/viewtopic.php?f=9&t=2591
sync
#Done
G_DIETPI-NOTIFY 3 "$G_PROGRAM_NAME" 'Completed'
G_DIETPI-NOTIFY 2 "$INFO_CURRENT_VERSION"
G_DIETPI-NOTIFY 2 "$INFO_SERVER_VERSION"
# - Reboot prompt, if system is already installed (else, dietpi-software handles this for 1st run)
if (( $G_DIETPI_INSTALL_STAGE == 1 )); then
G_WHIP_YESNO "Update applied:\n\n - $INFO_CURRENT_VERSION\n\nA system reboot is required to finalize the update. Would you like to reboot the system now?"
(( $? == 0 )) && reboot
fi
fi
#----------------------------------------------------------------
#Desktop Run, exit key prompt
if pgrep 'lxsession' &> /dev/null; then
read -p 'Press any key to exit DietPi-Update...'
fi
fi
#----------------------------------------------------------------
exit 0
#----------------------------------------------------------------
}