-
Notifications
You must be signed in to change notification settings - Fork 1
/
partgrator-installer
3543 lines (2862 loc) · 137 KB
/
partgrator-installer
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# partgrator-installer version 1.0.0
#
# partgrator-installer (un)installs partgrator scripts and makes compatible devices (USB)
#
# For more information, issues, or assistance visit: https://github.com/caribpa/partgrator
#
# Author: caribpa (https://github.com/caribpa)
#
# Copyright 2020 caribpa
#
# partgrator-installer is free to use under the Artistic License 2.0
#
# Find the Artistic License 2.0 here: https://spdx.org/licenses/Artistic-2.0.html
###### ENABLE AUTOMATIC EXPORT #######
set -a # This automatically exports every variable definition
# Needed for keeping the same environment if relaunching from memory
########### USER VARIABLES ###########
[ -z "${user_swapfile}" ] && user_swapfile="myswap.swp"
[ -z "${archive_swapfile}" ] && archive_swapfile=0 # 0 -> default ; 1 -> ask
[ -z "${jffs_scripts}" ] && jffs_scripts="/jffs/scripts"
[ -z "${jffs_addons}" ] && jffs_addons="/jffs/addons"
[ -z "${pre_mount_script}" ] && pre_mount_script="${jffs_scripts}/pre-mount"
[ -z "${post_mount_script}" ] && post_mount_script="${jffs_scripts}/post-mount"
[ -z "${disk_check_script}" ] && disk_check_script="${jffs_addons}/amtm/disk-check"
[ -z "${install_folder}" ] && install_folder="${jffs_addons}/partgrator"
[ -z "${partgrator_installer}" ] && partgrator_installer="${install_folder}/installer"
[ -z "${partgrator_helpers}" ] && partgrator_helpers="${install_folder}/helpers"
[ -z "${partgrator_pre}" ] && partgrator_pre="${install_folder}/pre"
[ -z "${partgrator_post}" ] && partgrator_post="${install_folder}/post"
[ -z "${download_tool}" ] && download_tool="curl"
[ "y" = "${download_retries-y}" ] && download_retries=3 # Unset for disabling
[ -z "${download_override}" ] && download_override="" # Set for enabling
[ -z "${ext_max_mount_count}" ] && ext_max_mount_count=5 # Times - Check tune2fs -c
[ -z "${ext_check_interval}" ] && ext_check_interval=7 # Days - Check tune2fs -i
[ -z "${ext_errors_behavior}" ] && ext_errors_behavior="continue" # Check tune2fs -e
[ -z "${parameters}" ] && parameters="$@" # The script's parameters passed
[ "0" = "${run_from_file:-0}" ] && run_from_file=0 # Values: 0 -> false ; 1 -> true
[ -z "${batch_mode-}" ] && [ -t 0 ] && batch_mode=0 # Values: 0 -> false ; 1 -> true
[ -z "${tmp}" ] && tmp="/tmp"
[ -z "${tmp_tars}" ] && tmp_tars="${tmp}/${install_folder##*/}-tars"
[ -z "${tar_exclude}" ] && \
tar_exclude="#############################################################################
#
# < Files and directories excluded by tar when backing up a mountpoint >
#
#############################################################################
#
# > Put one file or directory per line, or separated by a newline keyword: \n
# > Comments must be on their own line and start with #
# > Empty lines or lines with only spaces or tabs will be deleted
#
# > As tar doesn't distinguish files from directories (adding a trailing
# > slash to the pattern won't match anything; you can use a * wildcard after
# > the slash to exclude everything inside), I'll refer to both files and
# > directories when using the world 'file' in the rest of this description
#
# > POSIX Wildcards (glob) are supported
# > Escape the wildcard symbols with \ when you want to match them literally
# > Double quotes and symbols interpreted by the shell also need escaping
# > Prefix ./ to match files only on the mountpoint's root folder
#
# > Examples:
# > Exclude a file inside a specific folder from the root: ./path/to/file
# > Exclude a file anywhere but related to a hierarchy: path/to/file
# > Exclude a folder's content anywhere: folder/*
# > This WON'T exclude a folder (nor a file) anywhere: folder/
#
# > Consult -X or --exclude in busybox tar's manual for more information
#
#############################################################################
./.minidlna
./lost+found
#############################################################################
#
# > IMPORTANT: Setting the variable in the environment takes precedence
# > over this variable. So none of the previously listed files
# > will be excluded if you don't explicitly add them.
#
# > You may use a \n between files to simulate new lines:
# > tar_exclude='./.minidlna\n./lost+found'
#
############################################################################"
######################################
### ####
### USERS DO NOT NEED TO MODIFY ####
### BELOW THIS POINT ####
### ####
######################################
###### DISABLE AUTOMATIC EXPORT ######
set +a # Stop automatically exporting everything declared
########### SHELL VARIABLES ##########
LC_ALL=C
PATH=/sbin:/bin:/usr/sbin:/usr/bin
########## GLOBAL VARIABLES ##########
partgrator_installer_link='https://github.com/caribpa/partgrator/raw/master/partgrator-installer'
partgrator_helpers_link='https://github.com/caribpa/partgrator/raw/master/partgrator-helpers'
partgrator_pre_link='https://github.com/caribpa/partgrator/raw/master/partgrator-pre'
partgrator_post_link='https://github.com/caribpa/partgrator/raw/master/partgrator-post'
log_tag="partgrator-installer"
logfile="${tmp}/${install_folder##*/}.log"
logfile_date_format="+%T"
logfile_stamp=''
logger=':' # Needed to prevent logging to the system's log
logger_output='' # Show log_msg's tee output on the command line
logger_flags="-s" # Make logger output to the command line
tmp_installer="${tmp}/${install_folder##*/}-${partgrator_installer##*/}"
tmp_helpers="${tmp}/${install_folder##*/}-${partgrator_helpers##*/}"
installer="$0"
user_tar_exclude="$( printf -- '%s' "${tar_exclude}" \
| sed 's/^[[:space:]]*//;/^#/d;s/[[:space:]]*$//;/^$/d;
s/%/\\%/g ' )"
tar_preserve_code=10 # Return-code indicating to preserve ${tmp_tars} data
upgrading=0 # Values: 0 -> false ; 1 -> true
installation_started=0 # Values: 0 -> false ; 1 -> true
restore_backups=2 # Values: 0 -> false ; 1 -> true ; 2 -> skip
pre_init_tools_needed="date
echo
read
sed
tee
xargs "
tools_needed="[
awk
blkid
blockdev
cat
chmod
cp
cut
df
du
e2fsck
ejusb
expr
false
grep
kill
logger
mkdir
mke2fs
mount
mv
nvram
printf
ps
rm
rmdir
service
sleep
socat
swapoff
sync
tail
tar
touch
tune2fs
type
udevtrigger
umount
wc
${pre_init_tools_needed}"
##### OPERATIONS WITH VARIABLES ######
[ "${batch_mode}" != "0" ] && parameters="${parameters} -y" # Run with -y if in batch-mode
[ "${tmp_installer}" = "${installer}" ] && tmp_installer="${tmp_installer}.tmp"
############## HELPERS ###############
# Overriding logger with echo if it is set to : and it wasn't overridden before
if [ "${logger}" = ':' ] && ! type logger 2>&1 | grep -q 'function'; then
logger() {
local echo="echo"
[ "${logfile_newline}" = "0" ] && echo="printf %s"
# Remove all possible logger flags
${echo} "$*" | sed 's/^\(-*\([sc]\|[pt] *[^ ]*\) *\)\+//'
return $?
}
fi
# This ugly helper will be overridden by the one included in ${partgrator_helpers}
log_msg() (
set -o pipefail
logger ${logger_flags} -t "${log_tag}" "$@" 2>&1 \
| { if ! case "${logger_flags}" in *'-'[sc]*) false; esac
then [ -z "$*" ] && logfile='/dev/null'
xargs -- echo "$( [ "${logfile_stamp-1}" = "1" ] \
|| { sed 's/^'"${log_tag}"': //'; false; } \
&& date -- ${logfile_date_format} )" \
| tee -a -- "${logfile}" 1>&2; fi ; }
return $?
)
partgrator_help() {
local logfile='/dev/null'
log_msg ''
log_msg "Usage: ${installer} [install|uninstall|usb-creator] [-y|--yes]"
log_msg ''
log_msg " By default (no parameters) ${installer} performs 'install'" \
"followed by 'usb-creator'"
log_msg " Updates are automatically detected with 'install'"
log_msg " The optional -y/--yes flag skips the first prompt"
log_msg ''
return $?
}
relaunch_from_memory() {
local ret_code=0
# Don't relaunch from memory if only performing usb-creator procedure
# or if the current script (${installer}) is not ${partgrator_installer}
case "${parameters## }" in *'install'*|'-y'*|'--y'*|'') false;; esac \
|| [ "${installer}" != "${partgrator_installer}" ] \
&& return 0
if [ "${run_from_file}" = "0" ]; then
log_msg '[Relaunching from RAM]'
log_msg ''
# Relaunching from memory by exec-ing another shell which pipes the content of
# ${installer} (first cat) into yet another shell run with the same global
# variables (due to the set -a used in the first command of this script).
# The second cat is used to keep the piped shell interactive and will be
# interrupted by the first kill (which is echoed as last command to be executed
# by the piped shell) when normally ending the script.
# The third cat is used in the last pipe to keep showing the user the output
# of the piped shell.
# sed is used in the last pipe for just waiting until the piped shell starts
# outputting. This is crucial along with sleep to prevent the user input
# (from either deliberate key presses or a pipe) from reaching the piped shell
# while it hasn't read the whole file. When the piped shell starts outputting
# sed exits, the sleep is interrupted (second kill), and the user input is
# sent to the piped shell with the second cat.
# The process id of sleep is passed into the last pipe by echoing its value
# (actually the pid of the innermost shell which will be inherited by sleep due
# to exec) to stderr, and because of this, stderr is redirected into stdout for
# the whole sh chain in order for the last pipe to grab sleep's pid and the piped
# shell output (which everything goes into stderr due to logger's -s flag)
# It is possible to keep stdout and stderr separated by duplicating stdout into
# another file descriptor with 3>&1, but this is not performed here because
# asus merlin logger's tool only logs into the terminal if run with the -s flag
# which outputs into the stderr (-c outputs into the console, which i guess it is
# the serial console as it doesn't output into the terminal). So sending the inner
# pid into stderr and redirecting stderr into stdout for allowing everything (both
# the inner pid and the output of the piped sh) to be read by the last pipe is the
# simplest approach. Note that, the only change should be done in case the piped
# sh separates stdout from stderr is changing the file descriptor of the compound
# list (2>&1 to 3>&1), and redirect the output of the first echo to fd 3 instead.
# Then the last redirection from stdout to stderr shouldn't be necessary (the
# truth is that it was placed like that to preserve the original behavior of
# expecting the output from stderr due to logger's -s flag, which probably was
# chosen because stderr is usually unbuffered).
# Trap is used in the last pipe for preventing the last cat from being killed in
# case an interruption is issued (Ctrl+C). This allows the last cat to show the
# user the interrupt message returned by the installer before exiting.
# Finally, it is necessary to cat the installer inside a subshell for catching its
# exit statement and allowing to execute the kill command before exiting the shell
# Also the subshell is necessary the same way the compound-list before that is:
# {}, for preventing the piped shell from complaining about syntax errors if it
# is unable to read the end of a conditional (if-fi) due to (probably) buffering
# in the pipe.
exec sh -c ' { sh -c "sh -c \"echo \\\$\\\$ >&2 ; \
echo \{ \( ; \
cat "'"${installer}"'" ; \
echo \) \; ; \
echo kill -s INT \$\$ 2\>/dev/null\; \}; \
exec sleep 999 \";\
exec cat "\
| sh ; }\
2>&1 \
| ( read pid; sed "q"; kill -s INT ${pid}; \
trap "" EXIT KILL QUIT INT; exec cat ) >&2 '
log_msg ''
log_msg -p warning "Using fallback mechanisms for relaunching from RAM"
log_msg ''
# Check if sh complains about argument list too long
if sh -c "_wrap(){ $(sed '$d' "${installer}");};: ${parameters}; exit" 2>/dev/null
then
# Relaunching from memory by exec-ing another shell with a command consisting
# in a function call, composed of the same content of this script minus the
# EOF at the end, with ${parameters} ($@)
exec sh -c "_wrap(){ $(sed '$d' "${installer}"); }; _wrap ${parameters}"
ret_code=$?
else
# Executing the file from the ${tmp} folder
if [ "${installer}" != "${tmp_installer}" ]; then
cp -p -- "${installer}" "${tmp_installer}" \
|| { ret_code=$?
log_msg -p error "cp failed with error code ${ret_code}" \
"when trying to copy ${installer}" \
"into ${tmp_installer}" ; }
fi
if [ ${ret_code} -eq 0 ]; then
run_from_file=1 exec sh "${tmp_installer}" ${parameters}
ret_code=$?
fi
fi
log_msg 'There was an error when launching the script from memory'
log_msg ''
log_msg 'You may try again in case there is not enough memory'
log_msg ''
log_msg 'As a last resort, run it with $run_from_file set to true:'
log_msg " \$run_from_file=1 ${installer} ${parameters}"
log_msg ''
log_msg 'Note that overriding the installer when running with $run_from_file' \
'may result in unexpected behavior'
log_msg ''
else
log_msg -p warning "Performing an install operation that results in overriding" \
"the installer currently running (${installer}) may cause" \
"errors in execution and/or unexpected behavior"
log_msg ''
log_msg -p warning "Run ${installer} without setting the \$run_from_file flag," \
"or copy the installer elsewhere before running it if you" \
"want to avoid unexpected behavior"
if case "${parameters}" in '-y'*|'--y'*|*' -y'*|*' --y'*) false;; esac; then
log_msg ''
log_msg 'Press <Enter> to proceed'
read || return $?
fi
log_msg ''
fi
return ${ret_code}
}
setup_downloader() {
local download_retries="${download_retries}"
log_msg 'Locating tool for downloading the scripts'
log_msg ''
if ! type ${download_tool} >/dev/null 2>&1; then
log_msg -p warning "'${download_tool}' not found, locating another tool"
case "${download_tool}" in
curl) type wget >/dev/null 2>&1 && download_tool="wget";;
*) type curl >/dev/null 2>&1 && download_tool="curl";;
esac
if [ $? -ne 0 ]; then
log_msg -p error "No other tool for downloading (curl, wget) could be found"
return 1
fi
log_msg -p warning "'${download_tool}' will be used for downloading"
fi
log_msg "'${download_tool}' will be used to perform downloads"
if [ -n "${download_retries}" ]; then
case "${download_tool}" in
curl) download_retries="--retry ${download_retries}";;
wget) download_retries="--tries ${download_retries}";;
esac
fi
case "${download_tool}" in
curl) download_tool="curl -s -L ${download_retries} -o";;
wget) download_tool="wget -q ${download_retries} -O";;
esac
return 0
}
# This helper will be overridden by the one included in ${partgrator_helpers}
check_tools() {
local tools_needed="$1"
local tool='';
local IFS=$' \n\t\r'
log_msg "Checking if the necessary tools can be found"
log_msg ''
for tool in ${tools_needed}; do
if ! type "${tool}" >/dev/null 2>/dev/null; then
log_msg -p error "System does not include tool '${tool}'" \
"required by partgrator"
return 1
fi
done
log_msg "All necessary tools found"
return 0
}
downloader() {
local src_link="$1"
local dest_file="$2"
local dest_folder="${dest_file%/*}"
local ret_code=0
log_msg "Downloading ${src_link} as ${dest_file}"
log_msg ''
if [ ! -d "${dest_folder}" ]; then
log_msg -p error "Destination folder (${dest_folder}) does not exist"
return 1
fi
if [ -f "${dest_file}" ] && [ ${upgrading} -eq 0 ]; then
log_msg -p warning "File ${dest_file} already exists"
log_msg ''
if [ -z "${download_override}" ]; then
log_msg -p warning "Continuing installation with existing file"
return 0
fi
log_msg -p warning "Overriding ${dest_file}"
log_msg ''
fi
eval "${download_tool} '${dest_file}' -- '${src_link}'"
ret_code=$?
if [ ${ret_code} -ne 0 ]; then
log_msg -p error "${download_tool%% *} failed with error code ${ret_code}" \
"when trying to download ${src_link} as ${dest_file}"
else
log_msg "Download completed"
sync
# Check if the file was downloaded correctly by looking for EOF at its end
[ "$(tail -n -1 "${dest_file}")" = '# EOF' ] \
|| { ret_code=$?
log_msg -p error "${dest_file} wasn't downloaded correctly as" \
"'# EOF' is missing at its end" ; }
fi
return ${ret_code}
}
check_disk_check() {
log_msg "Checking if disk-check script (${disk_check_script}) is set up"
if [ ! -f "${pre_mount_script}" ]; then
log_msg -p error "No pre-mount script (${pre_mount_script}) found"
log_msg -p error "Please install disk-check or update the pre_mount_script" \
"environment variable so that disk-check can be located"
log_msg -p error "Note that you may override some of the script's variables" \
"using the environment"
return 1
elif ! grep -q -- "[^#]*${disk_check_script}" "${pre_mount_script}"; then
log_msg -p error "No disk-check (${disk_check_script}) could be found at" \
"${pre_mount_script}"
log_msg -p error "Please install it or make sure that it is uncommented and" \
"try again"
log_msg -p error "Note that you may override some of the script's variables" \
"using the environment"
return 2
fi
log_msg "disk-check script (${disk_check_script}) found in ${pre_mount_script}"
return 0
}
partgrator_check_tools() {
local IFS=$'\n'
log_msg $'\n'
log_msg "[Main phase]"
check_tools "${tools_needed}"
return $?
}
partgrator_banner() {
local answer=''
log_msg ""
log_msg '----------------------<Starting partgrator installer>----------------------'
log_msg ''
log_msg "For information, assistance or reporting an issue visit:"
log_msg " https://github.com/caribpa/partgrator"
log_msg $'\n'
log_msg "Tip: When answering prompts press the <Enter> key to choose" \
"the [default option]"
log_msg "Tip: You may answer the yes/no questions with y/n, or the numbers 1 or 2"
log_msg ''
log_msg '---------------------------------------------------------------------------'
if case "${parameters}" in '-y'*|'--y'*|*' -y'*|*' --y'*) false;; esac; then
log_msg ''
log_msg 'Press <Enter> to proceed'
read || return $?
fi
log_msg $'\n'
return 0
}
partgrator_recovery() {
local answer=''
log_msg $'\n'
log_msg "<Starting partgrator's recovery>"
log_msg ''
if { [ -e "${install_folder}/"*.bak ] 2> /dev/null; [ $? -ne 1 ]; }; then
log_msg -p info "Found backup scripts in ${install_folder}"
log_msg ''
verify_backup_scripts || return $?
{ [ -e "${install_folder}/"*.bak ] 2> /dev/null; [ $? -eq 1 ]; } && return 0
log_msg ''
answer="$(prompt_msg 'Do you wish to recover them?' '[yes]' 'no')" \
|| return $?
log_msg ''
if [ "${answer}" = 'yes' ]; then
restore_backups=1
restore_backup_scripts || return $?
else
log_msg -p warning "Skipping old recovery scripts"
fi
else
log_msg "Nothing to recover"
fi
return 0
}
partgrator_scripts_wizard() {
local re='re'
local IFS=$'\n'
local answer=''
local ret_code=0
case "${parameters}" in 'usb-creator'*) return 0;; esac
log_msg $'\n'
log_msg "<Starting partgrator's scripts wizard>"
log_msg ''
case "${parameters}" in
'uninstall'*|*' uninstall'*) uninstall_partgrator; return $?;;
'-y'*|*' -y'*|'--y'*|*' --y'*) :;;
*) log_msg "This wizard installs and uninstalls the partgrator scripts"
log_msg "You may run this installer with 'install' or 'uninstall'" \
"as parameter to only perform the chosen operation"
log_msg ''
esac
check_disk_check || return $?
log_msg $'\n'
check_partgrator
ret_code=$?
if [ ${ret_code} -eq 0 ]; then
log_msg ''
log_msg "ATTENTION: All the User variables defined in the old" \
"partgrator scripts will be overridden"
if case "${parameters}" in '-y'*|'--y'*|*' -y'*|*' --y'*) false;; esac; then
log_msg ''
log_msg 'Press <Enter> to proceed'
read || return $?
fi
fi
log_msg ''
[ ${ret_code} -eq 0 ] \
&& ! check_updates \
|| re=''
log_msg ''
if [ ${upgrading} -eq 0 ]; then
if ! case "${parameters}" in '-y'*|'--y'*|*' -y'*|*' --y'*) false;; esac; then
log_msg "Performing the ${re}installation of partgrator scripts"
answer='yes'
else
answer="$(prompt_msg "Would you like to ${re}install" \
"the partgrator scripts?" \
'yes' '[no]' )"\
|| return $?
fi
else
answer='yes'
fi
[ "${answer}" = "yes" ] \
&& { if [ -n "${re}" ] && [ ${upgrading} -eq 0 ]; then
log_msg ''
answer="$(prompt_msg "Would you like to override the" \
"existing partgrator scripts?" \
'yes' '[no]' )" \
|| return $?
[ "${answer}" = 'yes' ] && download_override=1
fi
log_msg ''
install_partgrator || return $?
log_msg ''
if ! check_partgrator; then
log_msg ''
log_msg -p error "partgrator couldn't be ${re}installed," \
"check previous lines for details"
return 1
fi
log_msg ''
log_msg "partgrator was ${re}installed successfully" ; }
return 0
}
check_partgrator() {
log_msg "Checking partgrator installation"
log_msg ''
if [ ! -d "${install_folder}" ]; then
log_msg -p error "Installation folder (${install_folder})" "does not exist"
elif [ ! -f "${partgrator_installer}" ]; then
log_msg -p error "Installer script (${partgrator_installer})""does not exist"
elif [ ! -f "${partgrator_helpers}" ]; then
log_msg -p error "Helper script (${partgrator_helpers})" "does not exist"
elif [ ! -f "${partgrator_pre}" ]; then
log_msg -p error "Pre-mount script (${partgrator_pre})" "does not exist"
elif [ ! -f "${partgrator_post}" ]; then
log_msg -p error "Post-mount script (${partgrator_post})" "does not exist"
elif ! grep -q -- "[^#]*${partgrator_pre}" "${pre_mount_script}"; then
log_msg -p error "Pre-mount script (${partgrator_pre}) not found in" \
"${pre_mount_script}"
log_msg ''
log_msg -p error "Please make sure that a '. ${partgrator_pre}' line exists" \
"and it is uncommented before trying again, otherwise" \
"you may need to reinstall partgrator"
elif ! grep -q -- "[^#]*${partgrator_post}" "${post_mount_script}"; then
log_msg -p error "Post-mount script (${partgrator_post}) not found in" \
"${post_mount_script}"
log_msg ''
log_msg -p error "Please make sure that a '. ${partgrator_post}' line exists" \
"and it is uncommented before trying again, otherwise" \
"you may need to reinstall partgrator"
else
log_msg "partgrator is installed"
return 0
fi
log_msg ''
log_msg -p error "partgrator is not installed or something is missing"
return 1
}
check_updates() {
local current_version=''
local remote_version=''
local current_version_segment=''
local remote_version_segment=''
local version=''
local answer=''
local ret_code=0
log_msg "Checking for updates"
log_msg ''
current_version="$(awk '/version/{print $NF; exit}' "${partgrator_pre}")" \
|| { ret_code=$?
log_msg -p error "awk failed with error code ${ret_code} when" \
"retrieving the version number of ${partgrator_pre}"
return ${ret_code} ; }
expr "${current_version}" : '[0-9]\+\.[0-9]\+\.[0-9]\+' > /dev/null \
|| { log_msg -p error "Unable to find a valid version in ${partgrator_pre}"
return 2 ; }
remote_version="$(set -o pipefail
eval ${download_tool} "- --header 'Range: bytes=0-51' \
-- '${partgrator_pre_link}'" \
| awk '/version/{print $NF; exit}' )" \
|| { ret_code=$?
log_msg -p error "${download_tool%% *} failed with error code ${ret_code}" \
"when downloading ${partgrator_pre_link}"
return ${ret_code} ; }
expr "${remote_version}" : '[0-9]\+\.[0-9]\+\.[0-9]\+' > /dev/null \
|| { log_msg -p error "Unable to find a valid version in ${partgrator_pre_link}"
return 2 ; }
while [ "${version}" != '[0-9]\+\.[0-9]\+\.[0-9]\+' ]; do
version="${version}${version:+\\.}"
current_version_segment="$(expr "${current_version}" : "${version}\([0-9]\+\)")"
remote_version_segment="$(expr "${remote_version}" : "${version}\([0-9]\+\)")"
if [ ${remote_version_segment} -gt ${current_version_segment} ]; then
log_msg ''
if ! case "${parameters}" in '-y'*|'--y'*|*' -y'*|*' --y'*) false;; esac; then
log_msg -p info "Upgrading scripts from ${current_version}" \
"to ${remote_version}"
else
log_msg -p info "Scripts can be updated from ${current_version}" \
"to ${remote_version}"
log_msg ''
answer="$(prompt_msg 'Do you wish to update?' '[yes]' 'no')" \
|| exit $?
[ "${answer}" = 'no' ] && break
fi
upgrading=1
download_override=1
return 0
fi
version="${version}"'[0-9]\+'
done
log_msg "No updates found"
return 1
}
backup_mount_scripts() {
local mount_script=''
local backup_script=''
local ret_code=0
log_msg "Backing up ${pre_mount_script} and ${post_mount_script}"
log_msg ''
for mount_script in "${pre_mount_script}" "${post_mount_script}"; do
backup_script="${install_folder}/${mount_script##*/}.bak"
cp -p -- "${mount_script}" "${backup_script}" \
|| { ret_code=$?
log_msg -p error "cp failed with error code ${ret_code}" \
"when copying ${mount_script}" \
"into ${backup_script}"
return ${ret_code} ; }
echo "# EOF" >> "${backup_script}" \
|| { ret_code=$?
log_msg -p error "echo failed with error code ${ret_code}" \
"when appending an EOF to ${backup_script}"
return ${ret_code} ; }
done
sync
log_msg "${pre_mount_script} and ${post_mount_script} successfully backed up"
restore_backups=1
return 0
}
backup_partgrator_scripts() {
local partgrator_script=''
local ret_code=0
# Return if there are no files in the folder
{ [ -e "${install_folder}/"* ] 2>/dev/null; [ $? -eq 1 ]; } && return 0
log_msg "Backing up scripts under ${install_folder}"
log_msg ''
log_msg "${install_folder} will contain the old scripts with an added '.bak'" \
"extension should the installer be interrupted while updating"
log_msg ''
for partgrator_script in "${install_folder}/"*; do
# Skip everything with an extension
[ "${partgrator_script#*.}" != "${partgrator_script}" ] && continue
cp -p -- "${partgrator_script}" "${partgrator_script}.bak" \
|| { ret_code=$?
log_msg -p error "cp failed with error code ${ret_code}" \
"when copying ${partgrator_script}" \
"into ${partgrator_script}.bak"
return ${ret_code} ; }
done
log_msg "Scripts under ${install_folder} successfully backed up"
restore_backups=1
return 0
}
verify_backup_scripts() {
local partgrator_script=''
local ret_code=0
log_msg "Verifying backup scripts under ${install_folder}"
log_msg ''
for partgrator_script in "${install_folder}/"*.bak; do
[ ! -e "${partgrator_script}" ] && continue
[ "$(tail -n -1 "${partgrator_script}")" = '# EOF' ] \
|| { ret_code=$?
log_msg -p error "${partgrator_script} wasn't backed up correctly" \
log_msg ''
log_msg -p warning "Removing ${partgrator_script}"
log_msg ''
rm -f -- "${partgrator_script}" ; }
done
if [ ${ret_code} -eq 0 ]; then
log_msg "Backup scripts under ${install_folder} successfully verified"
else
log_msg -p warning "Some backup scripts under ${install_folder} were corrupted" \
"and deleted"
log_msg ''
{ [ -e "${install_folder}/"*.bak ] 2>/dev/null; [ $? -eq 1 ]; } \
&& { log_msg -p warning "All backup files under ${install_folder} were deleted"
return 0 ; }
log_msg -p warning "Note that some backup files being corrupted means that none" \
"of the files (non-backup) were modified"
log_msg ''
answer="$(prompt_msg 'Do you wish to remove the remaining backup files?' \
'[yes]' 'no' )" \
|| return $?
[ "${answer}" = 'yes' ] \
&& { log_msg ''
rm "${install_folder}/"*.bak 2> /dev/null \
|| { ret_code=$?
log_msg -p error "rm failed with error code ${ret_code}" \
"when removing all the .bak files under" \
"${install_folder}"
return ${ret_code} ; }
log_msg -p warning "All backup files under ${install_folder} were deleted"; }
fi
return 0
}
restore_backup_scripts() {
local partgrator_script=''
local ret_code=0
local aux_ret_code=0
[ ${restore_backups} -eq 2 ] && return 0
if [ ${restore_backups} -ne 1 ]; then
rm -f "${install_folder}/"*.bak
sync
return 0
fi
log_msg "Restoring backed up scripts under ${install_folder}"
log_msg ''
for partgrator_script in "${install_folder}/"*.bak; do
partgrator_script="${partgrator_script%.bak}"
if ! case "${partgrator_script##*/}" in 'pre-mount'|'post-mount') false;; esac
then
# Note that here the file attributes are not preserved
# in the incredibly unlikely case this is an issue, just use
# cp -p before this sed (thus writing it twice)
sed '$d' "${partgrator_script}.bak" \
> "${jffs_scripts}/${partgrator_script##*/}"
else
cp -p -- "${partgrator_script}.bak" "${partgrator_script}"
fi
ret_code=$?
if [ ${ret_code} -ne 0 ]; then
aux_ret_code=${ret_code}
log_msg -p error "cp failed with error code ${ret_code}" \
"when restoring ${partgrator_script}"
log_msg ''
else
rm -f -- "${partgrator_script}.bak"
fi
done
sync
if [ ${aux_ret_code} -eq 0 ]; then
log_msg "Scripts under ${install_folder} successfully restored"
restore_backups=0
else
log_msg -p warning "Some ${install_folder} scripts couldn't be restored"
log_msg -p warning "The restore procedure will be tried again next time" \
"the installer is run"
restore_backups=2
fi
return ${aux_ret_code}
}
install_partgrator() {
local helpers_tools_check=''
local file_content=''
local IFS=$'\n'
local ret_code=0
log_msg "Installing partgrator"
log_msg ''
installation_started=1
if [ ! -d "${install_folder}" ]; then
log_msg "Creating installation folder (${install_folder})"
log_msg ''
mkdir -p -- "${install_folder}"
ret_code=$?
if [ ${ret_code} -ne 0 ]; then
log_msg -p error "mkdir failed with error code ${ret_code} when creating" \
"${install_folder}"
return ${ret_code}
fi
log_msg "Installation folder (${install_folder}) successfully created"
log_msg ''
elif [ -n "${download_override}" ]; then
backup_partgrator_scripts || return $?
log_msg ''
fi
backup_mount_scripts || return $?
if [ -f "${partgrator_helpers}" ] && [ -n "${download_override}" ]; then
log_msg ''
downloader "${partgrator_helpers_link}" "${tmp_helpers}" || return $?
log_msg ''
log_msg "Importing ${tmp_helpers} temporarily"
log_msg ''
helpers_tools_check='yes' # The new ones should be checked
. "${tmp_helpers}"
fi
if [ -f "${tmp_helpers}" ]; then
update_partgrator_dir "${tmp_helpers}" || return $?
update_jffs_scripts "${tmp_helpers}" || return $?
log_msg ''
log_msg "Copying ${tmp_helpers} into ${partgrator_helpers}"
cp -p -- "${tmp_helpers}" "${partgrator_helpers}"
ret_code=$?
if [ ${ret_code} -ne 0 ]; then
log_msg -p error "cp failed with error code ${ret_code} when copying" \
"${tmp_helpers} into ${partgrator_helpers}"
return ${ret_code}
fi