forked from RichardMidnight/pi-safe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpisafe_beta
2966 lines (2389 loc) · 91.5 KB
/
pisafe_beta
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/bash
COPYRIGHT="By Richard Reed 2018 - 2022"
# This script basically
# 1- copies a media-device with 'dd'
# 2- shrinks the last partition with 'pishrink'
# 3- compresses with 'zip'
# references
# https://www.raspberrypi.org/documentation/installation/installing-images/linux.md
# https://www.instructables.com/id/How-to-BackUp-and-Shrink-Your-Raspberry-Pi-Image/
# https://github.com/Drewsif/PiShrink
# https://jfearn.fedorapeople.org/fdocs/en-US/Documentation/0.1/html/Fedora_Multiboot_Guide/freespace-ntfs.html
SCRIPTNAME="${0##*/}"
SCRIPTVER="1.2.12c" # 2023/12/24
PRODUCTNAME="PiSafe"
PRODUCTCOMMENT="Raspberry Pi Imaging App"
PRODUCTHOME="https://github.com/RichardMidnight/pi-safe"
CURRENT_DIR=$(pwd)
USER=$(whoami)
CONFIG="/home/$USER/.config/pisafe/pisafe.conf"
LOG_FILE="/home/$USER/.config/pisafe/pisafe.log"
LOG=on
REQUIRED_TOOLS="dd pishrink.sh zip unzip pv bc whiptail file nano"
OPTIONAL_TOOLS="xz pigz zstd"
INDEV=
OUTDEV=
INFILE=
OUTFILE=
BACKTITLE="$PRODUCTNAME ver $SCRIPTVER === $PRODUCTCOMMENT === "
INTERFACE="cli"
YESNO=-n
WHITE='\033[1;37m'
RED='\033[1;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
LTBLUE='\033[1;34m'
NC='\033[0m' # No Color, standard text
echo_white() { (echo -e "${WHITE}$*${NC}") }
echo_red() { (echo -e "${RED}$*${NC}") }
echo_blue() { (echo -e "${LTBLUE}$*${NC}") }
echo_green() { (echo -e "${GREEN}$*${NC}") }
echo_debug() {
echo_blue DEBUG:$* > /dev/stderr
}
notes_desktop_environment(){
# this is just a reference of tested OSs from 2021
DISTRO TERMINAL TEXT EDITOR
RaspberryPiOS-stretch lxterminal leafpad?
RaspberrypiOS-buster lxterminal mousepad
Debian-GNOME gnome-terminal gedit
Debian Xfce xfce4-terminal mousepad
Debian KDE Plasma konsole kwrite
Debian Cinnamon xterm / uxterm gedit
Debian MATE mate-terminal pluma
Debian LXDE lxterminal mousepad
Debian LXQt qterminal featherpad
ubuntu gnome gnome-terminal gedit
lubuntu LXQt qterminal featherpad
LMDE-4 Cinnamon gnome-terminal xed
mint cinnamon gnome-terminal xed
mint MATE mate-terminal xed
Mint Xfce xfce4-terminal Xed
bionic puppy - ppm lxterminal geany
arch / manjaro - pacman -S
zorin - ubuntu based
manjaro kde konsole kate
manjaro xfce xfce-terminal mousepad
kali 2022 -apt xterm mousepad -ver 1.2.5
Tested with
--- ARM ---
Raspios Buster good
Raspbian Stretch good
Raspbian Jessy can work with limitations
Manjaro good no whiptail on lite ver
Ubuntu good
Note: overlay filesystem cannot be turned on
--- x86-64 ---
debian Strech, Buster no shrink, last is logical swap.
RPD Buster no shrink, last is logical swap.
mint 20.1 no shrink, last is ext logical.
LMDE 4 good
ubuntu good
lubuntu good
kali 2022 good, noshrink, swap
Note: if you custom partition with ext4 as last, it shrinks.
}
notes_performance(){
Creating image from raspi-os image expanded on an 8GB SD card. Size=837mb
SETTING SIZE GB SECONDS mb removed/sec
img 3.99 210
zip 1 1.71 511 4.59
pigz 1 1.71 340 6.90
xz 1 1.38 783 3.43
zst 1 1.68 321 7.40
zip 5 1.62 651 3.74
pigz 5 1.62 361 6.75
xz 5 1.24 1588 1.78
zst 5 1.52 407 6.24
zip 9 1.61 1446 1.69
pigz 9 1.61 586 4.18
xz 8 1.2 1990 1.44
zst 9 1.42 527 4.92
}
run_command(){
local CMD=$1
local LINE=$2
local QUIET=${3:-n}
ui_echo "~ Running: $CMD" blue
eval $CMD
ES=$?
if (( $ES )); then
ui_echo "~ Exit_Status $ES running '$CMD'" red log
return $ES
fi
}
pisafe_about(){
echo "$PRODUCTNAME was started in 2017 as 'sd' by 'RichardMidnight on github'"\
"while working on a Raspberry Pi project and needing to make restore-points."\
"The image writers available at the time did not run on the pi itself"\
"and were not able to create a new image file."
echo
echo "It was originally called 'sd' and only had a CLI."
echo
echo "Then in 2021, the menu front-end was added to make it more usable by others."\
"It was then renamed it 'PiSafe' which is a reference to the 'ventilated cupboards for storing"\
"pies while protecting them from insects and vermin'. "
echo
echo "It is pretty clean bash code so it works on many debian and arch distros."
echo
echo "It's home is $PRODUCTHOME"
echo
echo "Use at your own risk."
echo
echo "Hopfully $PRODUCTNAME is useful to others."
echo
echo "Peace"
}
pisafe_help() {
echo "$PRODUCTNAME v$SCRIPTVER - Designed for Raspberry Pi"
echo " - Backup media (SD card) to an image file"
echo " - Restore media (SD card) from an image file"
echo
echo "Usage: "
echo " $SCRIPTNAME [function] [media/file] [file/media] [-y]"
echo
echo "CLI Function is:"
echo " - with no arguments it launches the PiSafe menu (recommended)"
echo " list - list media and image-files "
echo " backup [media] [file] [-y] - backup media to image-file (-y also supresses check-for-updates)"
echo " restore [file] [media] [-y] - restore image-file to media"
echo " details [media] - show media details"
echo " erase [media] [format] [-y] - format media as fat32 exfat ntfs or ext4"
echo " install [-y] - install this script"
echo " update [-y] - update script from website"
echo " uninstall [-y] - uninstall this script"
echo " settings - edit the settings file with nano"
echo " defaults - restore settings to defaults"
echo " log - view the log file with nano"
echo " -v - display version"
echo " about - display about file"
echo " help | -h | --h - help"
echo
echo "Notes: "
echo " - Supports .img .zip .xz .gz and .zst files. Appends '.img.$DEFAULT_EXTENSION' if no extension is specified "
echo " - Specifying an '.img' extension is faster but twice the size because it is not compressed."
echo " - -y answers 'y' to prompts."
echo " - There are a number of settings that can be changed in the settings file. "
echo
echo
echo "Examples:"
echo "$SCRIPTNAME "
echo "$SCRIPTNAME list"
echo "$SCRIPTNAME backup sda newimage"
echo "$SCRIPTNAME backup /dev/sdb newimage.xz -y"
echo "$SCRIPTNAME restore newimage.img.zip sda"
echo "$SCRIPTNAME backup /dev/sda /home/pi/Downloads/backup_\$(date +%Y-%d-%m_%I%M%p).img.gz -y"
echo "$SCRIPTNAME format /dev/sda fat32"
echo "$SCRIPTNAME settings"
echo "$SCRIPTNAME log"
echo
}
pisafe_install(){
SILENT=${1:-"-n"}
if [[ $SILENT != -y ]] && [[ $(ui_yesno INSTALL "Install $PRODUCTNAME $SCRIPTVER?") != y ]]; then
echo
ui_msg NOTICE "~ $PRODUCTNAME not installed" "" "" log
return
fi
echo
ui_echo "Installing $PRODUCTNAME v $SCRIPTVER..." white
# Install the CLI script
sudo cp --backup=numbered $SCRIPTNAME /usr/local/bin/$SCRIPTNAME
sudo chmod +x /usr/local/bin/$SCRIPTNAME
echo Installed ver=$(/usr/local/bin/$SCRIPTNAME -v)
# download the icon
wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe_icon.png -O pisafe_icon.png
mv pisafe_icon.png /home/$USER/.config/pisafe/pisafe_icon.png
# create the desktop file
TERMINAL1=$(env_terminal)
echo "\
[Desktop Entry]
Type=Application
Terminal=false
Version=1.0
Name=$PRODUCTNAME
Comment=$PRODUCTCOMMENT
#Icon=rpi-imager
#Icon=media-removable
Icon=/home/$USER/.config/pisafe/pisafe_icon.png
#Exec=lxterminal --geometry=110x40 -e $SCRIPTNAME
Exec=$TERMINAL1 $SCRIPTNAME
Categories=Utility
StartupNotify=false" > "$PRODUCTNAME.desktop"
# for lxde, MATE and xfce put it in /usr/share/applications
sudo mv "$PRODUCTNAME.desktop" "/usr/share/applications"
# for just this user, put it in $HOME/.local/share/applications/
#mv "$PRODUCTNAME.desktop" "$HOME/.local/share/applications/"
if [[ -f /usr/local/bin/$SCRIPTNAME ]]; then
MSG="$PRODUCTNAME $SCRIPTVER installed.
\nSelect it from the 'Accessories' menu.
\n\nOr you can execute it from any directory by typing '$SCRIPTNAME'."
if [[ $INTERFACE = cli ]]; then
MSG="$PRODUCTNAME $SCRIPTVER installed. Select it from the 'Accessories' menu or type '$SCRIPTNAME' in a terminal."
fi
else
MSG="$PRODUCTNAME $SCRIPTVER not installed. '/usr/local/bin/$SCRIPTNAME' not found. "
fi
ui_msg INSTALL "~ $MSG" "" "" log
}
pisafe_install_tool() {
local TOOL=$1
#SILENT=${1:-"-n"}
local INSTALL=$(env_installer)
if [[ -z $(env_which $TOOL) ]] ; then
case $TOOL in
xz)
ui_echo "Installing xz-utils..." white
sudo $INSTALL xz-utils
if [[ -z $(env_which xz) ]]; then
ui_msg_warning $LINENO "xz not installed."
fi
;;
pishrink.sh)
ui_echo "Installing pishrink..." white
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
sudo chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin
if [[ -z $(env_which pishrink.sh) ]] ; then
ui_msg_warning $LINENO "pishrink not installed."
fi
;;
*)
#echo_white Installing $TOOL ...
ui_echo "Installing $TOOL..." white
sudo $INSTALL $TOOL
if (( $? )); then
ui_msg_warning $LINENO "$TOOL not installed."
fi
;;
esac
fi
}
pisafe_install_tools() {
local MISSING_TOOLS=
#scan for missing tools
for TOOL in $REQUIRED_TOOLS ; do
if [[ -z $(env_which $TOOL) ]]; then
MISSING_TOOLS="$MISSING_TOOLS $TOOL"
fi
done
# if nothing missing, return; else silent or ask
if [[ -z $MISSING_TOOLS ]]; then
return
elif [[ $SILENT = -n ]] && [[ $(ui_yesno "Install dependencies:'$MISSING_TOOLS '") = n ]]; then
echo
return
fi
echo
for TOOL in $REQUIRED_TOOLS ; do
pisafe_install_tool $TOOL
done
# if whiptail did not install then try installing newt...
if ! [[ $(env_which whiptail) ]]; then
local INSTALL=$(env_installer)
run_command "sudo $INSTALL newt" # for fedora
run_command "sudo $INSTALL libnewt" # for arch, manjaro
fi
}
pisafe_update() {
INTERFACE=${1:-"cli"}
SUMMARY=${2:-"yes"}
local YES=${3:-"-n"}
if [[ $SUMMARY = yes ]]; then
ui_echo "Checking for $PRODUCTNAME $SCRIPTVER updates..." white
fi
local SERVER_VER=
wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/$SCRIPTNAME -O $SCRIPTNAME.tmp 2> /dev/null
ES=$?
if (( $ES )); then
echo_if_cli "ERROR: Can't connect to server..."
return $ES
fi
# did we get the new version from the server?
if [[ -f $SCRIPTNAME.tmp ]]; then
SERVER_VER=$(bash $SCRIPTNAME.tmp -v)
else
return 1
fi
if [[ $(get_ver_to_int $SERVER_VER) -gt $(get_ver_to_int $SCRIPTVER) ]]; then
if [[ $YES = -y ]]; then
RESULT="y"
else
RESULT=$(ui_yesno "UPDATE AVAILABLE" "Update $PRODUCTNAME from '$SCRIPTVER' to '$SERVER_VER' ")
echo
fi
if [[ $RESULT = y ]]; then
ui_echo "Updating $PRODUCTNAME to v $SERVER_VER..." white
sudo mv $SCRIPTNAME.tmp /usr/local/bin/$SCRIPTNAME
sudo chmod +x /usr/local/bin/$SCRIPTNAME
echo "$($SCRIPTNAME -v) installed. Press any key to exit... "
read -s -n 1
exit 0
fi
else
rm $SCRIPTNAME.tmp
if [[ $SUMMARY = yes ]]; then
ui_msg UPDATE "~ $PRODUCTNAME $SCRIPTVER is up to date. Server ver is '$SERVER_VER' " "" "" log
fi
fi
}
pisafe_uninstall(){
SILENT=${1:-"-n"}
if [[ -f /usr/local/bin/$SCRIPTNAME ]]; then
INSTALLED_VER=$(/usr/local/bin/$SCRIPTNAME -v)
fi
if [[ $SILENT != -y ]] && [[ $(ui_yesno UNINSTALL "Uninstall $PRODUCTNAME $INSTALLED_VER" "--defaultno") != y ]] ; then
echo
ui_msg UNINSTALL "~ $PRODUCTNAME $INSTALLED_VER not uninstalled" "" "" log
return
fi
echo
ui_echo "Uninstalling $PRODUCTNAME v $INSTALLED_VER..." white
# remove from the menu
sudo rm -f "/usr/share/applications/$PRODUCTNAME.desktop"
# next line cleans up an old configuration
rm -f "$HOME/.local/share/applications/$PRODUCTNAME.desktop"
# remove the script and config
sudo rm -f "/usr/local/bin/$SCRIPTNAME"
rm -f $CONFIG
ui_msg UNINSTALL "~ $PRODUCTNAME $INSTALLED_VER uninstalled. $LOG_FILE not removed." "" "" log
}
# ---------- Config_var functions
config_var_init_configfile(){
if [[ ! -d $(file_path $CONFIG) ]]; then
mkdir -p $(file_path $CONFIG)
fi
if [[ ! -f $CONFIG ]]; then
touch $CONFIG
echo "# $CONFIG" > $CONFIG
config_var_set_defaults
else
# update any missing values. false means dont force the update
config_var_set_defaults false
fi
}
config_var_set() {
SETTING=$1
VAL=$2
local FORCE=${3:-true} # if false, will only update if var not set.
#dont update the setting if thre is a value and we are not forcing new values
if [[ $FORCE = false ]] && [[ ! -z $(config_var_get $SETTING) ]] ; then
return
fi
case $(config_var_get $SETTING) in
'') # add setting to end of file
echo "$SETTING"="$VAL" >> $CONFIG
;;
*) #update setting to val
sed -i 's~'$SETTING'=.*$~'$SETTING'='$VAL'~' $CONFIG
;;
esac
}
config_var_clear(){
SETTING=$1
VAL=$2
sed -i 's~'$SETTING'=.*$~'$VAR'=~' $CONFIG
}
config_var_get(){
SETTING=$1"="
cat $CONFIG | grep $SETTING | sed 's~'$SETTING'~~'
}
config_var_set_defaults(){
FORCE=${1:-true} # if false, will only update if var not set.
# config_var_init_configfile
config_var_set settings_script_ver $SCRIPTVER
config_var_set default_path_cli_from_settings off $FORCE
config_var_set default_path $HOME/Downloads $FORCE
config_var_set check_for_updates_on_startup on $FORCE
config_var_set check_dependencies_on_startup on $FORCE
config_var_set debug_mode off $FORCE
config_var_set sound on $FORCE
config_var_set log on $FORCE
config_var_set text_editor $(env_texteditor) $FORCE
#backup settings
config_var_set hide_root_device on $FORCE
config_var_set shrink_fs on $FORCE
config_var_set auto_expand_fs on $FORCE
config_var_set default_extension zip $FORCE
config_var_set compression_level 1 $FORCE
config_var_set parallel_compression on $FORCE
config_var_set large_device_read_warning 16gb $FORCE
config_var_set skip_freespace on $FORCE
#restore settings
config_var_set large_device_write_warning 16gb $FORCE
config_var_set safety on $FORCE
}
config_var_get_settings(){
SETTINGS_SCRIPT_VER=$(config_var_get settings_script_ver)
DEFAULT_PATH_CLI_FROM_SETTINGS=$(config_var_get default_path_cli_from_settings)
DEFAULT_PATH=$(config_var_get default_path)
HIDE_ROOT_DEVICE=$(config_var_get hide_root_device)
CHECK_FOR_UPDATES_ON_STARTUP=$(config_var_get check_for_updates_on_startup)
CHECK_DEPENDENCIES_ON_STARTUP=$(config_var_get check_dependencies_on_startup)
DEBUG_MODE=$(config_var_get debug_mode)
SOUND=$(config_var_get sound)
LOG=$(config_var_get log)
VERIFY=off
TEXT_EDITOR=$(config_var_get text_editor)
# backup settings
SHRINK_FS=$(config_var_get shrink_fs)
AUTO_EXPAND_FS=$(config_var_get auto_expand_fs)
DEFAULT_EXTENSION=$(config_var_get default_extension)
COMPRESSION_LEVEL=$(config_var_get compression_level)
LARGE_DEVICE_READ_WARNING=$(config_var_get large_device_read_warning)
SKIP_FREESPACE=$(config_var_get skip_freespace)
# restore settings
LARGE_DEVICE_WRITE_WARNING=$(config_var_get large_device_write_warning)
SAFETY=$(config_var_get safety)
if [[ $INTERFACE = cli ]] && [[ $DEFAULT_PATH_CLI_FROM_SETTINGS != on ]]; then
DEFAULT_PATH=$(pwd)
fi
mkdir -p "$DEFAULT_PATH"
}
# ---------- Enviroment functions
env_installer(){
local INSTALLERS="apt pacman dnf"
local INSTALLER=
for INSTALLER in $INSTALLERS; do
if [[ $(env_which $INSTALLER) ]]; then
break
fi
done
case $INSTALLER in
apt|dnf) INSTALLER="$INSTALLER install -y" ;;
pacman) INSTALLER="pacman -S --noconfirm" ;;
esac
echo "$INSTALLER"
}
env_root_device() {
local ROOT_PARTITION
local ROOT_DRIVE
ROOT_MAJ=$(findmnt -n -e -o MAJ:MIN / | cut -d: -f1)
ROOT_DEV=$(lsblk -p | grep $ROOT_MAJ:0 | cut -d" " -f1)
echo $ROOT_DEV
# new simpler way
# ROOT_PARTITION=$(findmnt -no source /)
}
env_terminal(){
local TERMINAL1=
#if [ -f /usr/bin/lxterminal ];then
if [ $(env_which lxterminal) ];then
TERMINAL1="lxterminal --geometry=110x40 --title='PiSafe' -e bash -c "
elif [ $(env_which xfce4-terminal) ];then
TERMINAL1="xfce4-terminal --geometry=110x40 --title='PiSafe' -x bash -c "
elif [ $(env_which mate-terminal) ];then
TERMINAL1="mate-terminal --geometry=110x40 --title='PiSafe' -x bash -c "
elif [ $(env_which gnome-terminal) ];then
TERMINAL1="gnome-terminal --geometry=110x40 --title='PiSafe' -- bash -c "
elif [ $(env_which xterm) ];then
TERMINAL1="xterm -geometry 110x40 -T 'PiSafe' -e bash -c "
elif [ $(env_which terminator) ];then
TERMINAL1="terminator --geometry=110x40 -T 'PiSafe' -x bash -c "
elif [ $(env_which konsole) ];then
TERMINAL1="konsole -e bash -c "
elif [ $(env_which qterminal) ];then
TERMINAL1="qterminal -e bash -c "
elif [ $(env_which x-terminal-emulator) ];then
TERMINAL1="$(readlink -f /usr/bin/x-terminal-emulator) -e bash -c "
else
echo "Failed to locate any terminal emulators. "
return 1
fi
echo "$TERMINAL1"
}
env_texteditor(){
TEXT_EDITORS=" leafpad xed gedit featherpad kwrite pluma kate mousepad geany nano "
for TEXT_EDITOR in $TEXT_EDITORS ; do
if [[ $(env_which $TEXT_EDITOR) ]] ; then
echo $TEXT_EDITOR
return
fi
done
}
env_which () {
#fixes manjaro's 'which' command
local FILE=$1
# if which not installed, try to install it
which which 1>/dev/null 2>/dev/null
if (( $? )); then
sudo apt install which -y
sudo pacman -S which --noconfirm
sudo dnf install which -y
fi
which $FILE 1>/dev/null 2>/dev/null
if (( $? )); then
#echo false
return 1
else
echo 1
return 0
fi
}
env_sysinfo(){
uname -a
echo $(env_terminal)
echo $(env_installer)
echo $(env_texteditor)
echo $(env_root_device)
}
# ---------- Misc functions
do_beep(){
FREQ=${1:-600}
TIME=${2:-.5}
if [[ $SOUND = on ]] && [[ $(env_which speaker-test) ]]; then
(speaker-test -t sign -f $FREQ > /dev/null & sleep $TIME && kill -9 $! ) > /dev/null
# sleep .5 # to allow the beep to end
fi
}
do_beep_up(){
do_beep 500 .3
do_beep 600 .6
}
do_beep_down(){
do_beep 600 .3
do_beep 500 .6
}
get_elapsed_time() {
# paramaters are in seconds
local BEG=$1
local END=$2
local SECONDS=$(( $END-$BEG ))
local MIN=$(( $SECONDS/60 ))
local SEC=$(( $SECONDS%60 ))
echo $MIN min $SEC sec
#echo $(( $(( $END-$BEG ))/60)) min $(( $(( $END-$BEG ))%60 )) sec
#minutes decimal
# echo $(($END-$BEG)) seconds
}
do_countdown(){
local MAX=${1:-10}
local MSG=${2:-"Pausing for $MAX seconds... Press y to continue immediately or any other key to stop"}
local DEF=${3:-"y"}
# echo "Pausing for $MAX seconds... Press y to continue immediately or any other key to stop"
echo "$MSG"
echo -n $MAX
sleep 1
for number in $(seq 1 $MAX) ; do
echo -n ".$(($MAX-$number))"
read -s -t 1 -N 1 INPUT
if [[ ! -z $INPUT ]]; then
if [[ $INPUT = y ]]; then
echo Continuing...
return
else
echo " $INPUT Stopping countdown..."
return 1
fi
fi
MSG=$(echo "$MSG.$i")
done
if [[ $DEF = n ]]; then
return 1
fi
echo ...
}
get_ver_to_int() {
local IFS=.
parts=($1)
let val=1000000*parts[0]+1000*parts[1]+parts[2]
echo $val
unset IFS
}
get_args(){
#sets the local variables of the calling function
local ARGS=$*
for ARG in $ARGS; do
#echo_debug "$LINENO testing arg '$ARG'"
case $ARG in
-y|--yes) YESNO=-y ;;
# -v|--verbose) VERBOSE=-v ;;
-1) COMPRESSION_LEVEL=1 ;;
-2) COMPRESSION_LEVEL=2 ;;
-3) COMPRESSION_LEVEL=3 ;;
-4) COMPRESSION_LEVEL=4 ;;
-5) COMPRESSION_LEVEL=5 ;;
-6) COMPRESSION_LEVEL=6 ;;
-7) COMPRESSION_LEVEL=7 ;;
-8) COMPRESSION_LEVEL=8 ;;
-9) COMPRESSION_LEVEL=9 ;;
--updates=on) CHECK_FOR_UPDATES_ON_STARTUP=on ;;
--updates=off) CHECK_FOR_UPDATES_ON_STARTUP=off ;;
--dependencies=on) CHECK_DEPENDENCIES_ON_STARTUP=on;;
--dependencies=off) CHECK_DEPENDENCIES_ON_STARTUP=off;;
--sound=on) SOUND=on ;;
--sound=off) SOUND=off ;;
--log=on) LOG=on ;;
--log=off) LOG=off ;;
# --hide_root=on) HIDE_ROOT_DEVICE=yes ;;
# --hide_root=off) HIDE_ROOT_DEVICE=no ;;
--shrink=on) SHRINK_FS=on ;;
--shrink=off) SHRINK_FS=off ;;
--auto_expand=on) AUTO_EXPAND_FS=on ;;
--auto_expand=off) AUTO_EXPAND_FS=off ;;
--skip_freespace=on) SKIP_FREESPACE=on ;;
--skip_freespace=off) SKIP_FREESPACE=off ;;
--debug=on) DEBUG_MODE=on ;;
--debug=off) DEBUG_MODE=off ;;
# *) ;;
esac
done
}
get_bytes(){
# input bytes or human and returns human or bytes
# currently shows 3 significant digits.
local BYTES=$1
local OUTPUT=${2:-"-h"}; #human or bytes
local BASE=$(echo $BYTES | tr -cd '[[:digit:]]' )
local SUFFIX=$(echo $BYTES | tr -cd '[[kmgtbKMGTB]]' )
if ! [[ $BASE =~ ^[0-9]+$ ]] ; then # not a number
ui_msg_warning $LINENO "$BASE not a number"
return 0
fi
# if no translation needed... then just echo the input and leave
if [[ $OUTPUT != -h ]] && [[ -z $SUFFIX ]] ; then
echo $BYTES
return
fi
if [[ $OUTPUT = -h ]] && [[ ! -z $SUFFIX ]] ; then
echo $BYTES
return
fi
if [[ $OUTPUT = -h ]] ; then # translate to human readable
if [[ -z $SUFFIX ]]; then
local LEN=${#BYTES}
local k_ilo=1024;
local m_ega=$k_ilo*$k_ilo;
local g_iga=$m_ega*$k_ilo;
local t_era=$g_iga*$k_ilo;
local p_eta=$t_era*$k_ilo;
[[ -z $BYTES ]] && return # null, so exit
if ! [[ $BYTES =~ ^[0-9]+$ ]] ; then # not a number
echo 0
return 1
fi
case $LEN in
4) echo $(echo "scale=2; $BYTES/($k_ilo)" | bc)kb ;;
5) echo $(echo "scale=1; $BYTES/($k_ilo)" | bc)kb ;;
6) echo $(echo "scale=0; $BYTES/($k_ilo)" | bc)kb ;;
7) echo $(echo "scale=2; $BYTES/($m_ega)" | bc)mb ;;
8) echo $(echo "scale=1; $BYTES/($m_ega)" | bc)mb ;;
9) echo $(echo "scale=0; $BYTES/($m_ega)" | bc)mb ;;
10) echo $(echo "scale=2; $BYTES/($g_iga)" | bc)gb ;;
11) echo $(echo "scale=1; $BYTES/($g_iga)" | bc)gb ;;
12) echo $(echo "scale=0; $BYTES/($g_iga)" | bc)gb ;;
13) echo $(echo "scale=2; $BYTES/($t_era)" | bc)tb ;;
14) echo $(echo "scale=1; $BYTES/($t_era)" | bc)tb ;;
15) echo $(echo "scale=0; $BYTES/($t_era)" | bc)tb ;;
*) echo $BYTES ;;
esac
else
echo $BYTES
fi
else # translate to bytes
case $SUFFIX in
b|B|"") echo $BASE ;;
k|K|kb|KB) echo $(($BASE*1024)) ;;
m|M|mb|MB) echo $(($BASE*1024*1024)) ;;
g|G|gb|GB) echo $(($BASE*1024*1024*1024)) ;;
t|T|tb|TB) echo $(($BASE*1024*1024*1024*2014)) ;;
*) ui_msg_error $LINENO "bad byte suffix 'SUFFIX'" ;;
esac
fi
}
# ---------- file functions
file_base(){
#returns the path and filename minus all extensions
local DIR=$(dirname "$1")
local BASE=$(basename "$1")
# the %%. removes everything after the first . found
BASE="${BASE%%.*}"
echo "$DIR/$BASE"
}
file_device(){
MOUNT_POINT=$(df -a "$1" | sed 's/ */ /g' | grep -v Mounted | cut -d ' ' -f 6 )
BLK_DEV=$(lsblk -n -l -p -o PKNAME,MOUNTPOINT,NAME | sed 's/ */ /g' | grep " $MOUNT_POINT " | cut -d " " -f 1)
echo $BLK_DEV
}
file_ext(){
echo $(basename "$*") | grep \\. | sed 's#.*\.##g'
}
file_folder_size() {
local FOLDER=$*
# returns human readable
if [[ -d $FOLDER ]] ; then
sudo du -sh "$FOLDER" | cut -d'/' -f1 | sed 's/\s\s*/ /g'
ES=$?
if (( $ES )); then
return $ES
fi
else
echo 0
fi
}
file_fs_freespace() {
local FILENAME=${1:-"."}
local FORMAT=${2:-"-B1"} # -B1 for bytes -h for humanreadable
local FREESPACE
if [[ -d $FILENAME ]]; then
#FREESPACE=$(df "$FILENAME" -B1 --output=avail | grep -v Avail)
FREESPACE=$(df "$FILENAME" -B1 --output=avail | tr -cd '[[:digit:]]' )
ES=$?
else
#FREESPACE=$(df $(dirname "$FILENAME") -B1 --output=avail | grep -v Avail)
FREESPACE=$(df $(dirname "$FILENAME") -B1 --output=avail | tr -cd '[[:digit:]]' )
ES=$?
fi
if (( $ES )) ; then
return $ES
fi
if [[ $FORMAT = -h ]]; then
FREESPACE=$(get_bytes $FREESPACE -h)
fi
echo $FREESPACE
}
file_image_size() {
local INFILE="$1"
local HUMAN=${2:-"no"} # or -h
case $(file_ext "$INFILE") in
img | iso) SIZE_BYTES=$(echo $(( $(ls -s "$INFILE" | cut -d' ' -f1 ) * 1024 )) ) ;;
zip) SIZE_BYTES=$(zipinfo -t "$INFILE" 2> /dev/null | grep "%" | cut -d, -f2 | cut -d" " -f2 ) ;;
xz) SIZE_BYTES=$(xz -l -v "$INFILE" 2> /dev/null | grep Uncompressed | sed 's/\s\s*/ /g' | cut -d'(' -f 2 | cut -d ' ' -f1 | sed 's/,//g') ;;
gz) SIZE_BYTES=$(pigz -l "$INFILE" 2> /dev/null | grep -v compressed | sed 's/\s\s*/ /g' | sed -e 's/^[ \t]*//' | cut -d' ' -f 2 | sed 's/?/0/g') ;;
# gz) SIZE_BYTES=$(pigz -l "$INFILE" 2> /dev/null | grep -v compressed | sed 's/\s\s*/ /g' | sed -e 's/^[ \t]*//' | cut -d' ' -f 2 ) ;;
zst) SIZE_BYTES=$(zstd -v -l "$INFILE" 2> /dev/null | grep Decompressed | cut -d"(" -f2 | cut -d" " -f1) ;;
*) echo 0
return 1 ;;
esac
if [[ -z $SIZE_BYTES ]] ; then
echo 0
return
fi
if [[ $HUMAN = -h ]]; then
echo $(get_bytes $SIZE_BYTES -h)
else
echo $SIZE_BYTES
fi
}
file_list_image_files(){
# local DIR=$1
local DIR=$DEFAULT_PATH
OLD_PWD=$(pwd)
cd $DIR
FILES="FILE_NAME FILE_SIZE (IMAGE_SIZE) \n"
IFS=$'\t\n'
for FILE in $(ls *.img *.zip *.xz *.gz *.zst *.iso 2>/dev/null) ; do
FILE_NS=$(echo "$FILE" | sed 's/ /_/g')
FILES="$FILES $FILE_NS $(file_size "$FILE" -h) ($(file_image_size "$FILE" -h)) \n "
done
printf $FILES | column -t
unset IFS
cd $OLD_PWD
}
file_path(){
dirname "$*"
}
file_size() {
local FILE="$1"
local HUMAN=${2:-"no"}
# $2 can be -h
if [[ -f "$FILE" ]] ; then
SIZE_BYTES="$(($(ls -s "$FILE" | cut -d' ' -f1) * 1024))"
if (( $? )); then
return 1
fi
if [[ $HUMAN = -h ]]; then
echo $(get_bytes $SIZE_BYTES -h)
else
echo $SIZE_BYTES
fi
else
echo 0
fi
}