4
4
5
5
set -euo pipefail
6
6
7
+ # Path to this script
8
+ toltecctl_path=$( realpath " $0 " )
9
+
7
10
# Path where Toltec resides (will be mounted to $toltec_dest)
8
11
toltec_src=/home/root/.entware
9
12
@@ -15,7 +18,7 @@ toltec_share=/home/root/.local/share/toltec
15
18
# Path to static opkg build
16
19
opkg_path=/home/root/.local/bin/opkg
17
20
# Path to opkg install status file
18
- opkg_status=" /opt /lib/opkg/status"
21
+ opkg_status=" $toltec_dest /lib/opkg/status"
19
22
20
23
# Path to Opkg configuration
21
24
opkg_conf=" $toltec_src " /etc/opkg.conf
@@ -123,7 +126,7 @@ get-release-version() {
123
126
# 3 - unable to install standalone wget
124
127
check-version () {
125
128
local wget
126
- if [[ " $( install-state) " != " yes" ]] || [[ " $( command -v wget) " != " /opt /bin/wget" ]]; then
129
+ if [[ " $( install-state) " != " yes" ]] || [[ " $( command -v wget) " != " $toltec_dest /bin/wget" ]]; then
127
130
if ! install-standalone-wget; then
128
131
return 2
129
132
fi
@@ -197,9 +200,9 @@ add-bind-mount() {
197
200
unit_name=" $( basename " $unit_path " ) "
198
201
199
202
if [[ -e $unit_path ]]; then
200
- echo " Bind mount configuration for '$2 ' already exists, updating"
203
+ log INFO " Bind mount configuration for '$2 ' already exists, updating"
201
204
else
202
- echo " Mounting '$1 ' over '$2 '"
205
+ log INFO " Mounting '$1 ' over '$2 '"
203
206
fi
204
207
205
208
cat > " $unit_path " << UNIT
@@ -234,11 +237,11 @@ remove-bind-mount() {
234
237
unit_name=" $( basename " $unit_path " ) "
235
238
236
239
if [[ ! -e $unit_path ]]; then
237
- echo " No existing bind mount for '$1 '"
240
+ log INFO " No existing bind mount for '$1 '"
238
241
return
239
242
fi
240
243
241
- echo " Removing mount over '$1 '"
244
+ log INFO " Removing mount over '$1 '"
242
245
systemctl disable " $unit_name "
243
246
systemctl stop " $unit_name "
244
247
if mountpoint -q " $1 " ; then
@@ -460,7 +463,7 @@ reinstall-root() {
460
463
if [[ -v " on_root_packages[$pkgname ]" ]]; then
461
464
reinstall_packages[$pkgname ]=1
462
465
fi
463
- done < <( gunzip -c /opt /var/opkg-lists/* | grep " ^Package:" | awk ' {print $2}' )
466
+ done < <( gunzip -c $toltec_dest /var/opkg-lists/* | grep " ^Package:" | awk ' {print $2}' )
464
467
465
468
# Workaround: Checking the size of an empty array when the nounset option
466
469
# is active may throw an error on some Bash versions, so we disable it
@@ -480,6 +483,7 @@ clean-path() {
480
483
sed -i " /^$bashrc_start_marker \$ /,/^$bashrc_end_marker \$ /d" " $bashrc_path "
481
484
sed -i " /^$bashrc_old_start_marker \$ /!b;n;d" " $bashrc_path "
482
485
sed -i " /^$bashrc_old_start_marker \$ /d" " $bashrc_path "
486
+ # TODO - rewrite this to use $toltec_dest instead of /opt
483
487
sed -i ' /^\(export \)\?PATH="\?\.*\/opt\/bin:\/opt\/sbin.*"\?$/d' " $bashrc_path "
484
488
fi
485
489
}
@@ -492,7 +496,7 @@ set-path() {
492
496
clean-path
493
497
cat >> " $bashrc_path " << SHELL
494
498
$bashrc_start_marker
495
- PATH=" /opt/ bin:/opt /sbin:/home/root/.local/bin:\$ PATH"
499
+ PATH=" $toltec_dest / bin:$toltec_dest /sbin:/home/root/.local/bin:\$ PATH"
496
500
$bashrc_end_marker
497
501
SHELL
498
502
@@ -541,9 +545,9 @@ generate-opkg-conf() {
541
545
# then run \` toltecctl generate-opkg-conf\` to regenerate this file
542
546
543
547
dest root /
544
- dest ram /opt /tmp
545
- lists_dir ext /opt /var/opkg-lists
546
- option tmp_dir /opt /tmp
548
+ dest ram $toltec_dest /tmp
549
+ lists_dir ext $toltec_dest /var/opkg-lists
550
+ option tmp_dir $toltec_dest /tmp
547
551
548
552
CONF
549
553
@@ -672,23 +676,23 @@ CONF
672
676
673
677
# Re-enable Toltec install after system update
674
678
reenable () {
675
- log INFO " Mounting /opt "
679
+ log INFO " Mounting $toltec_dest "
676
680
add-bind-mount " $toltec_src " " $toltec_dest "
677
681
switch-branch " $( get-branch) "
678
- log INFO " Generating /opt /etc/opkg.conf"
682
+ log INFO " Generating $toltec_dest /etc/opkg.conf"
679
683
generate-opkg-conf || true
680
684
log INFO " Opkg update"
681
685
opkg update
682
686
log INFO " Reinsalling base packages"
683
687
reinstall-base
684
688
log INFO " Reinstalling packages with files on the root partition"
685
689
reinstall-root
686
- if [ -d /opt /share/toltec/reenable.d ]; then
687
- find /opt /share/toltec/reenable.d -maxdepth 1 -mindepth 1 -print0 \
690
+ if [ -d $toltec_dest /share/toltec/reenable.d ]; then
691
+ find $toltec_dest /share/toltec/reenable.d -maxdepth 1 -mindepth 1 -print0 \
688
692
| xargs -0rn1 basename \
689
693
| while read -r pkg; do
690
694
local script
691
- script=" /opt /lib/opkg/info/${pkg} .postinst"
695
+ script=" $toltec_dest /lib/opkg/info/${pkg} .postinst"
692
696
if [ -f " $script " ]; then
693
697
log INFO " Reconfiguring ${pkg} "
694
698
" $script " configure || true
@@ -720,7 +724,7 @@ list-installed-ordered() {
720
724
# Install standalone opkg binary
721
725
install-standalone-opkg () {
722
726
local wget
723
- if [[ " $( install-state) " != " yes" ]] || [[ " $( command -v wget) " != " /opt /bin/wget" ]]; then
727
+ if [[ " $( install-state) " != " yes" ]] || [[ " $( command -v wget) " != " $toltec_dest /bin/wget" ]]; then
724
728
if ! install-standalone-wget; then
725
729
return 2
726
730
fi
@@ -775,12 +779,49 @@ install-standalone-wget() {
775
779
776
780
# Remove Toltec completely
777
781
uninstall () {
782
+ if ! [ -d " $toltec_src " ] && ! [ -d " $toltec_dest " ]; then
783
+ log INFO " Toltec does not appear to be installed"
784
+ exit 0
785
+ fi
786
+
778
787
# Fetch standalone opkg used to uninstall packages
779
788
if ! install-standalone-opkg; then
780
789
return 1
781
790
fi
782
-
783
791
local clean=true
792
+ local success=true
793
+
794
+ broken-state () {
795
+ echo " Your system may be in a broken state. Please review the logs"
796
+ echo " before rebooting your device. If you need assistance, you can"
797
+ echo " get help on the community discord channel."
798
+ }
799
+
800
+ cleanup () {
801
+ local error_code=$?
802
+ log ERROR " $BASH_LINENO : $BASH_COMMAND "
803
+ log ERROR " Uninstall did not complete properly"
804
+ broken-state
805
+ exit $error_code
806
+ }
807
+ trap cleanup ERR
808
+
809
+ if [[ " $toltecctl_path " != * _backup ]]; then
810
+ log INFO " Creating backup of toltecctl at ${toltecctl_path} _backup"
811
+ rm -f " ${toltecctl_path} _backup"
812
+ cp " $toltecctl_path " " ${toltecctl_path} _backup"
813
+ fi
814
+
815
+ if ! [ -f " $toltec_dest " /etc/opkg.conf ]; then
816
+ if [ -f " $toltec_src " /etc/opkg.conf ] && ! mountpoint -q " $toltec_dest " ; then
817
+ log INFO " Mounting $toltec_dest "
818
+ add-bind-mount " $toltec_src " " $toltec_dest "
819
+ fi
820
+ log ERROR " Current install seems to be partially removed, unable to automatically removed."
821
+ broken-state
822
+ exit 1
823
+ fi
824
+
784
825
# Remove installed packages in reverse dependency order
785
826
while read -r pkgname; do
786
827
if ! " $opkg_path " remove --force-depends " $pkgname " ; then
@@ -792,28 +833,54 @@ uninstall() {
792
833
$( list-installed-ordered)
793
834
EOF
794
835
795
- systemctl daemon-reload
836
+ if ! systemctl daemon-reload; then
837
+ log WARN " Failed to reload systemd state"
838
+ fi
796
839
rm -f " $opkg_path "
797
840
798
841
# Remove mount point
799
842
remove-bind-mount " $toltec_dest "
800
- rmdir " $toltec_dest "
843
+
844
+ case " $( install-state) " in
845
+ no) ;;
846
+ * )
847
+ log ERROR " $toltec_dest bind mount still is active"
848
+ success=false
849
+ ;;
850
+ esac
851
+
852
+ if ! rmdir " $toltec_dest " ; then
853
+ log ERROR " Failed to remove mount point"
854
+ success=false
855
+ fi
801
856
802
857
# Unset PATH
803
858
clean-path
804
859
805
860
# Remove Toltec data
806
- rm -r " $toltec_src "
861
+ if ! rm -r " $toltec_src " ; then
862
+ log ERROR " Failed to remove toltec data directory"
863
+ success=false
864
+ fi
807
865
808
866
# Re-enable xochitl if needed
809
- systemctl enable xochitl
810
-
867
+ if ! systemctl enable xochitl; then
868
+ log ERROR " Failed to enable the user interface."
869
+ success=false
870
+ fi
811
871
if ! $clean ; then
812
- echo " Warning: There were errors removing some of the toltec packages."
813
- echo " Your system may be in a broken state. Please review the logs"
814
- echo " before rebooting your device. If you need assistance, you can"
815
- echo " get help on the community discord channel."
872
+ log ERROR " There were errors removing some of the toltec packages."
873
+ success=false
874
+ fi
875
+ if ! $success ; then
876
+ log ERROR " Uninstall failed"
877
+ broken-state
878
+ exit 1
879
+ fi
880
+ if [[ " $toltecctl_path " != * _backup ]]; then
881
+ rm -f " $toltecctl_path "
816
882
fi
883
+ rm -f " ${toltecctl_path} _backup"
817
884
}
818
885
819
886
# The current toltec install state
0 commit comments