forked from joweisberg/openwrt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopkg-install.sh
1721 lines (1514 loc) · 56.2 KB
/
opkg-install.sh
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
#
# ssh root@openwrt
# /root/opkg-install.sh 2>&1 | tee /var/log/opkg-install.log
#
# Generate backup config:
# source /etc/os-release && rm -f /mnt/data/backup-$VERSION-$HOSTNAME* && sysupgrade -b /mnt/data/backup-$VERSION-$HOSTNAME.$(uci get dhcp.@dnsmasq[0].domain)-$(date +%F).tar.gz
#
# Restore backup config:
# cp -p /mnt/data/openwrt-owncloud*.tar.gz /tmp && sysupgrade -r /tmp/openwrt-owncloud*.tar.gz
#
# Soft factory reset:
# firstboot -y && reboot now
#
# Flash the new OpenWrt firmware:
# mkdir /mnt/data; mount /dev/sda3 /mnt/data
# sysupgrade -v /mnt/data/openwrt-19.07.7-ath79-generic-tplink_archer-c7-v2-squashfs-sysupgrade.bin
#
FILE_PATH=$(readlink -f $(dirname $0)) #/root
FILE_NAME=$(basename $0) #opkg-install.sh
FILE_NAME=${FILE_NAME%.*} #opkg-install
FILE_DATE=$(date +'%Y%m%d-%H%M%S')
FILE_LOG="/var/log/$FILE_NAME.log"
HELP=0
sleep 1 # Time to write on disk the file log
if [ ! -f $FILE_LOG ] || [ $(cat $FILE_LOG | wc -l) -gt 0 ] || [ "$(ls --full-time $FILE_LOG | awk '{print $6" "$7}')" != "$(date +'%Y-%m-%d %H:%M:%S' --date="@$(($(date +%s) - 1))")" ]; then
HELP=1
echo "* "
echo "* $FILE_LOG file not found!"
fi
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ $HELP -eq 1 ]; then
echo "* "
echo "* Usage:"
echo "* $FILE_PATH/$FILE_NAME.sh 2>&1 | tee $FILE_LOG"
exit 1
fi
###############################################################################
##### Default environment variables
###############################################################################
# Do not interprate space in variable
SAVEIFS=$IFS
IFS=$'\n'
source /etc/os-release
ENV=0
DOMAIN="${1:-sub.domain.com}" ## This domain must actually point to your router
LOCAL_DOMAIN="${DOMAIN%%.*}"
WIFI_SSID="Box-$(cat /dev/urandom | tr -dc A-Z | head -c4)"
WIFI_KEY="$(cat /dev/urandom | tr -dc A-Za-z0-9 | head -c13)"
WIFI_GUEST_KEY="Guest$(date +'%Y')"
IPADDR="192.168.1.1"
NETADDR=${IPADDR%.*}
NETADDR_GUEST="10.10.10"
80211R=0 ## Enable 802.11r Fast Transition
MESH=0 ## Enable Mesh support like with dedicated SSID to connect wifi repeater
FBXTV=0 ## QoS advices Smart TV for Freebox
UWAN=0 ## USB tethering connection
WWAN=0 ## USB Modem 4G/LTE with NCM protocol
AD_REBOOT=0
SQM=0
STATS=0
FW_FWD_NAS_CERTS=0
USBREBOOT=0
USBWIPE=0
USBBUILT=0
# Source under this script directory
cd $(readlink -f $(dirname $0))
if [ -f .env ]; then
ENV=1
source ./.env
LOCAL_DOMAIN="${DOMAIN%%.*}"
NETADDR=${IPADDR%.*}
fi
###############################################################################
### Pre-Script
###############################################################################
echo "* Set access rights on uploaded files"
find /root -type d -exec chmod 755 "{}" \;
find /root -type f -exec chmod 644 "{}" \;
find /root -type f -name "*.sh" -exec chmod +x "{}" \;
mkdir -p /etc/acme
find /etc/acme -type d -exec chmod 755 "{}" \;
find /etc/acme -type f -exec chmod 644 "{}" \;
find /etc/acme -type f -name "*.sh" -exec chmod +x "{}" \;
chmod 644 /etc/shadow
###############################################################################
### Functions
###############################################################################
function fCmd() {
local cmd=$@
$cmd > /dev/null
if [ $? -ne 0 ]; then
echo "* "
echo "* $cmd" | xargs
echo -n "* Do you want to retry? [Y/n] "
read answer
[ -n "$(echo $answer | grep -i '^y')" ] || [ -z "$answer" ] && fCmd $cmd
fi
}
function fInstallUsbPackages() {
if [ -z "$(opkg list-installed | grep 'block-mount')" ]; then
echo "* Checking for updates, please wait..."
fCmd opkg update
echo "* Package USB 3.0 disk management"
fCmd opkg install kmod-usb-core kmod-usb2 kmod-usb3 kmod-usb-storage kmod-usb-storage-uas
echo "* Package ext4/FAT"
fCmd opkg install kmod-fs-ext4 kmod-fs-vfat
echo "* Package mounted partitions"
fCmd opkg install block-mount
echo "* Package exFAT/ntfs"
# echo "* Do not install packages WPA3, SQM QoS, Acme, uHTTPd, IKEv2/IPsec with strongSwan, Collectd/Stats, Adblock, Watchcat, mSMTP!"
fCmd opkg install kmod-fs-exfat libblkid ntfs-3g
echo "* Package hd-idle"
fCmd opkg install luci-app-hd-idle
# if [ $WWAN -eq 1 ]; then
# echo "* Package USB Huawei Modem 4G/LTE with NCM protocol"
# opkg install kmod-usb-net-rndis usb-modeswitch
# opkg install comgt-ncm kmod-usb-net-huawei-cdc-ncm luci-proto-ncm usb-modeswitch
# fi
# if [ $WPA3 -eq 1 ]; then
# echo "* Package WPA2/WPA3 Personal (PSK/SAE) mixed mode"
# opkg remove --autoremove wpad-basic > /dev/null 2>&1
# opkg install wpad-openssl
# fi
echo "* Package SFTP fileserver"
fCmd opkg install openssh-sftp-server
# opkg install luci-app-samba4
# opkg install luci-app-ddns
# opkg install ipset
# opkg install kmod-ipt-nathelper-rtsp kmod-ipt-raw
# if [ $SQM -eq 1 ]; then
# echo "* Package SQM QoS (aka Smart Queue Management)"
# opkg install luci-app-sqm
# fi
# if [ $STATS -eq 1 ]; then
# echo "* Package Satistics with collectd"
# opkg install luci-app-statistics collectd-mod-rrdtool collectd-mod-processes collectd-mod-sensors
# fi
# opkg install luci-ssl-openssl curl ca-bundle
# opkg install luci-app-acme
# opkg install luci-app-uhttpd
# opkg install strongswan-full
# opkg install luci-app-adblock
# opkg install luci-app-watchcat
# opkg install msmtp
echo "* Package wget"
fCmd opkg install wget
fi
}
function fMountPartitions() {
local USBDEV="$1" DEVSWAP="${USBDEV}1" DEVROOT="${USBDEV}2" DEVDATA="${USBDEV}3"
echo "* UCI config fstab"
uci -q del fstab.@swap[-1]
uci add fstab swap
uci set fstab.@swap[-1]=swap
uci set fstab.@swap[-1].enabled='1'
uci set fstab.@swap[-1].device="$DEVSWAP"
# fstab.@mount[1].target='/overlay'
if [ -n "$(uci show | grep 'fstab.*/overlay')" ]; then
I=$(echo "$(uci show | grep 'fstab.*/overlay')" | awk -F'[][]' '{print $2}')
uci -q del fstab.@mount[$I]
fi
eval $(block info "$DEVROOT" | grep -o -e "UUID=\S*")
uci add fstab mount
uci set fstab.@mount[-1]=mount
uci set fstab.@mount[-1].enabled='1'
#uci set fstab.@mount[-1].device="$DEVROOT"
uci set fstab.@mount[-1].uuid="$UUID"
uci set fstab.@mount[-1].target='/overlay'
uci set fstab.@mount[-1].options='rw,sync,noatime'
uci set fstab.@mount[-1].enabled_fsck='1'
# fstab.@mount[2].target='/mnt/data'
if [ -n "$(uci show | grep -E 'fstab.*/mnt/data')" ]; then
I=$(echo "$(uci show | grep -E 'fstab.*/mnt/data')" | awk -F'[][]' '{print $2}')
uci -q del fstab.@mount[$I]
fi
eval $(block info "$DEVDATA" | grep -o -e "UUID=\S*")
uci add fstab mount
uci set fstab.@mount[-1]=mount
uci set fstab.@mount[-1].enabled='1'
#uci set fstab.@mount[-1].device="$DEVDATA"
uci set fstab.@mount[-1].uuid="$UUID"
uci set fstab.@mount[-1].target="/mnt/data"
uci set fstab.@mount[-1].options='rw,noatime'
uci commit fstab
echo "* Enable all mounted partitions"
for L in $(uci show fstab); do
# fstab.@swap[0].enabled='0'
# fstab.@mount[1].enabled='0'
I=$(echo "$L" | awk -F'[][]' '{print $2}')
if [ $(echo "$L" | grep 'swap' | grep 'enable') ]; then
uci set fstab.@swap[$I].enabled='1'
elif [ $(echo "$L" | grep 'mount' | grep 'enable') ]; then
uci set fstab.@mount[$I].enabled='1'
fi
done
uci commit fstab
echo "* Please check mounted partitions http://openwrt/cgi-bin/luci/admin/system/mounts"
}
###############################################################################
##### Check internet connection
###############################################################################
H_WIFI_SSID="${H_WIFI_SSID:-AndroidAP}"
H_WIFI_KEY="${H_WIFI_KEY:-android}"
wget -q --spider --timeout=5 http://www.google.com 2> /dev/null
if [ $? -eq 0 ]; then # if Google website is available we update
echo "* "
echo "* You are connected to the internet."
echo "* "
else
echo "* "
echo "* You are not connected to the internet, default wan interface is down!"
echo "* "
echo -n "* Enter Hotspot SSID <$H_WIFI_SSID>? "
read answer
if [ -n "$answer" ]; then
H_WIFI_SSID=$answer
fi
echo -n "* Enter Hotspot key <$H_WIFI_KEY>? "
read answer
if [ -n "$answer" ]; then
H_WIFI_KEY=$answer
fi
# /etc/config/firewall
# Connect Hotspot client from radio1 to wan zone
#for L in $(uci show firewall); do
# # firewall.@zone[1].name='wan'
# if [ -n "$(echo "$L" | grep 'zone' | grep 'name' | grep 'wan')" ]; then
# I=$(echo "$L" | awk -F'[][]' '{print $2}')
# uci add_list firewall.@zone[$I].network='hwan'
# break
# fi
#done
#uci add_list firewall.@zone[1].network='hwan'
sed -i 's/wan wan6/wan wan6 hwan/g' /etc/config/firewall
uci commit firewall
# /etc/config/network
uci set network.hwan=interface
uci set network.hwan.proto='dhcp'
uci commit network
# /etc/config/wireless
uci set wireless.wifinet10=wifi-iface
uci set wireless.wifinet10.device='radio1'
uci set wireless.wifinet10.mode='sta'
uci set wireless.wifinet10.network='hwan'
uci set wireless.wifinet10.ssid="$H_WIFI_SSID"
uci set wireless.wifinet10.key="$H_WIFI_KEY"
uci set wireless.wifinet10.encryption='psk-mixed'
# Enable radio1 devices for hotspot connection
uci set wireless.radio1.disabled='0'
# uci set wireless.default_radio1.disabled='0'
# uci set wireless.wifinet0.disabled='0'
for UCI_DEV in $(uci show wireless | grep ".device='radio1'" | cut -d'=' -f1 | sed 's/.device//g'); do uci set $UCI_DEV.disabled='0'; done
uci commit wireless
wifi down radio1 && sleep 3 && wifi up radio1
echo "* Hotspot <$H_WIFI_SSID> as of wan zone is setup."
echo "* Please check wireless network http://openwrt/cgi-bin/luci/admin/network/wireless"
echo "* "
echo -n "* Press <enter> to test internet connection..."
read answer
wget -q --spider --timeout=5 http://www.google.com 2> /dev/null
if [ $? -eq 0 ]; then # if Google website is available we update
echo "* "
echo "* You are connected to the internet."
echo "* "
else
echo "* "
echo "* Please check internet connection and try again!"
echo "* "
exit 0
fi
fi
###############################################################################
##### Create and moving Rootfs & Swap on USB storage (create partitions, format, copy, mount)
###############################################################################
if [ $USBREBOOT -eq 1 ]; then
echo "* Create and moving Rootfs & Swap on new USB storage"
answer="y"
else
echo -n "* Create and moving Rootfs & Swap on new USB storage? [y/N] "
read answer
fi
if [ -n "$(echo $answer | grep -i '^y')" ]; then
echo -n "* Please unplug USB storage <enter to continue>..."
read answer
if [ -z "$(opkg list-installed | grep lsblk)" ]; then
fInstallUsbPackages
echo "* Package disk utilities"
fCmd opkg install usbutils e2fsprogs dosfstools wipefs fdisk lsblk
fi
echo -n "* Please plug back in USB storage <enter to continue>..."
read answer
echo "* "
echo "* List of available USB devices: "
echo "* "
fdisk -l /dev/sd[a-d] | grep -e "^Disk" -e "^Device" -e "^\/" | grep -v "identifier"
echo "* "
lsblk -f /dev/sd[a-d]
echo "* "
if [ -z "$USBDEV" ]; then
USBDEV="/dev/sda"
fi
echo -n "* Enter USB device? <$USBDEV> "
read answer
if [ -n "$answer" ]; then
USBDEV=$answer
fi
DEVSWAP="${USBDEV}1"
DEVROOT="${USBDEV}2"
DEVDATA="${USBDEV}3"
FSRAM=512
FSROOT=4
# Disk size - space for root partition (ignore swap space, too small)
FSDATA=$(($(fdisk -l $USBDEV | grep "^Disk $USBDEV" | cut -d' ' -f3 | cut -d'.' -f1) - $FSROOT))
echo "* Unmount all 3 partitions on $USBDEV"
uci -q set fstab.@swap[0].enabled='0'
# fstab.@mount[1].target='/overlay'
if [ -n "$(uci show | grep 'fstab.*/overlay')" ]; then
I=$(echo "$(uci show | grep 'fstab.*/overlay')" | awk -F'[][]' '{print $2}')
uci set fstab.@mount[$I].enabled='0'
fi
# fstab.@mount[2].target='/mnt/data'
if [ -n "$(uci show | grep 'fstab.*/mnt/data')" ]; then
I=$(echo "$(uci show | grep 'fstab.*/mnt/data')" | awk -F'[][]' '{print $2}')
uci set fstab.@mount[$I].enabled='0'
fi
uci commit fstab
block umount > /dev/null
if [ $USBREBOOT -eq 1 ]; then
echo "* Built-in USB device for $USBDEV"
answer="y"
else
echo -n "* Built-in USB device for $USBDEV? [y/N] "
read answer
fi
if [ -n "$(echo $answer | grep -i '^y')" ]; then
if [ $USBWIPE -eq 0 ]; then
echo "* Wiping all signatures for $USBDEV"
wipefs --all --force $USBDEV > /dev/null
sleep 2
# echo "* Delete all 3 partitions on $USBDEV"
# (
# echo d # Delete a partition
# echo # Partition number
# echo d # Delete a partition
# echo # Partition number
# echo d # Delete a partition
# echo # Partition number
# echo w # Write changes
# ) | fdisk $USBDEV > /dev/null
# sleep 2
echo "* "
echo "* "
echo "* "
echo -n "* Reboot to complete wipefs on $USBDEV? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
echo "USBREBOOT=1" >> .env
echo "USBWIPE=1" >> .env
echo "USBDEV=$USBDEV" >> .env
reboot
exit 0
else
echo -n "* Please unplug and plug back in $USBDEV <enter to continue>..."
read answer
fi
fi
if [ $USBBUILT -eq 0 ]; then
(
echo o # Create a new empty DOS partition table
echo w # Write changes
) | fdisk $USBDEV > /dev/null
sleep 2
SIZE=$(($(free | grep Mem | awk '{print $2}') / 1024))
echo "* Info: Double RAM for machines with 512MB of RAM or less than, and same with more."
echo "* Current RAM: ${SIZE}MB"
if [ $SIZE -lt 499 ]; then
SIZE=$((SIZE * 2))
else
SIZE=512
fi
echo -n "* Enter swap partition size? <${SIZE}MB> "
read answer
if [ -n "$answer" ]; then
SIZE=$answer
fi
FSRAM=$SIZE
(
echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo p # Primary partition
echo # Partition number
echo # First sector (Accept default: 1)
echo "+${SIZE}M" # Last sector (Accept default: varies)
echo w # Write changes
) | fdisk $USBDEV > /dev/null
sleep 2
SIZE=4
echo -n "* Enter root partition size? <${SIZE}GB> "
read answer
if [ -n "$answer" ]; then
SIZE=$answer
fi
FSROOT=$SIZE
SIZE=$((SIZE * 1024))
(
echo n # Add a new partition
echo p # Primary partition
echo # Partition number
echo # First sector (Accept default: 1)
echo "+${SIZE}M" # Last sector (Accept default: varies)
echo w # Write changes
) | fdisk $USBDEV > /dev/null
sleep 2
FSDATA=$(($(fdisk -l $USBDEV | grep "^Disk $USBDEV" | cut -d' ' -f3 | cut -d'.' -f1) - $FSROOT))
echo "* Create data partition of <${FSDATA}GB>"
(
echo n # Add a new partition
echo p # Primary partition
echo # Partition number
echo # First sector (Accept default: 1)
echo # Last sector (Accept default: varies)
echo w # Write changes
) | fdisk $USBDEV > /dev/null
sleep 2
echo "* "
echo "* Partitions detail for $USBDEV:"
fdisk -l $USBDEV | grep -e "^Disk" -e "^Device" -e "^\/" | grep -v "identifier"
echo "* "
echo "* "
echo "* "
echo "* "
echo -n "* Reboot to complete partitions creation on $USBDEV? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
echo "USBBUILT=1" >> .env
reboot
exit 0
else
echo -n "* Please unplug and plug back in $USBDEV <enter to continue>..."
read answer
fi
fi
# Remove temporary variables
sed -i '/^USBREBOOT=/d' .env
sed -i '/^USBWIPE=/d' .env
sed -i '/^USBBUILT=/d' .env
sed -i '/^USBDEV=/d' .env
echo "* "
echo "* Format partitions with swap/ext4/fat32"
mkswap $DEVSWAP > /dev/null 2>&1
mkfs.ext4 -F -L "rootfs" $DEVROOT > /dev/null 2>&1
mkfs.fat -F 32 -n "data" $DEVDATA > /dev/null 2>&1
echo "* "
echo "* Partitions detail for $USBDEV:"
lsblk -f $USBDEV
echo "* "
echo "* Remove Package disk utilities"
opkg remove --autoremove usbutils e2fsprogs dosfstools wipefs fdisk lsblk > /dev/null 2>&1
echo "* "
echo "* Add swap of ${FSRAM}MB on $DEVSWAP"
echo "* Move overlayfs:/overlay to ${FSROOT}GB on $DEVROOT"
echo "* Add free storage of ${FSDATA}GB on $DEVDATA"
echo "* "
# Rollback overlay partition
# /dev/ubi0_1: UUID="e14f77d3-5564-4d4d-b708-842837dc9905" VERSION="w4r0" MOUNT="/overlay" TYPE="ubifs"
#mount -t ubifs /dev/ubi0_1 /overlay
# Mount swap partition
swapon $DEVSWAP
# Mount data partition
mkdir -p /mnt/data
mount -t vfat $DEVDATA /mnt/data > /dev/null
fMountPartitions $USBDEV
# Copy rootfs partition
echo "* Copy /overlay on $DEVROOT partition..."
mkdir -p /mnt/rootfs
mount -t ext4 $DEVROOT /mnt/rootfs > /dev/null
# Remove existing data
rm -Rf /mnt/rootfs/*
#tar -C /overlay -cvf - . | tar -C /mnt/rootfs -xf -
cp -a -f /overlay/. /mnt/rootfs
umount /mnt/rootfs
block umount > /dev/null
echo "* "
echo "* "
echo "* "
echo -n "* Reboot to complete \"Rootfs & Swap on USB Storage\" <enter to continue>..."
read answer
reboot
exit 0
fi
else
echo -n "* Rebuild Rootfs on existing USB storage? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
echo -n "* Please unplug USB storage <enter to continue>..."
read answer
if [ -z "$(opkg list-installed | grep lsblk)" ]; then
fInstallUsbPackages
echo "* Package disk utilities"
fCmd opkg install usbutils e2fsprogs dosfstools wipefs fdisk lsblk
fi
echo -n "* Please plug back in USB storage <enter to continue>..."
read answer
echo "* "
echo "* List of available USB devices: "
echo "* "
lsblk -f /dev/sd[a-d]
echo "* "
DEVSWAP=$(block info | grep 'swap' | cut -d':' -f1)
echo -n "* Enter swap device? <$DEVSWAP> "
read answer
if [ -n "$answer" ]; then
DEVSWAP=$answer
fi
DEVROOT=$(block info | grep 'rootfs' | cut -d':' -f1)
echo -n "* Enter rootfs device? <$DEVROOT> "
read answer
if [ -n "$answer" ]; then
DEVROOT=$answer
fi
# Remove last character
USBDEV=${DEVROOT%?}
echo "* "
echo "* Format partitions with swap/ext4"
mkswap $DEVSWAP > /dev/null 2>&1
mkfs.ext4 -F -L "rootfs" $DEVROOT > /dev/null 2>&1
echo "* Remove Package disk utilities"
opkg remove --autoremove usbutils e2fsprogs dosfstools wipefs fdisk lsblk > /dev/null 2>&1
fMountPartitions $USBDEV
# Copy rootfs partition
echo "* Copy /overlay on $DEVROOT partition..."
mkdir -p /mnt/rootfs
mount -t ext4 $DEVROOT /mnt/rootfs > /dev/null
# Remove existing data
rm -Rf /mnt/rootfs/*
#tar -C /overlay -cvf - . | tar -C /mnt/rootfs -xf -
cp -a -f /overlay/. /mnt/rootfs
umount /mnt/rootfs
block umount > /dev/null
echo "* "
echo "* "
echo "* "
echo -n "* Reboot to complete \"Rootfs & Swap on USB Storage\" <enter to continue>..."
read answer
reboot
exit 0
fi
fi
rm -Rf /mnt/rootfs
###############################################################################
##### Environment variables (loaded or entered)
###############################################################################
if [ $ENV -eq 1 ]; then
echo "* "
echo "* The current setup: "
echo "* "
cat .env | grep -v "^#"
echo "* "
echo -n "* Do you accept this setup? [Y/n] "
read answer
if [ -n "$(echo $answer | grep -i '^n')" ]; then
ENV=0
echo "* "
fi
fi
if [ $ENV -eq 0 ]; then
echo -n "* Enter domain name? <$DOMAIN> "
read answer
if [ -n "$answer" ]; then
DOMAIN=$answer
LOCAL_DOMAIN="${DOMAIN%%.*}"
fi
echo -n "* Enter Wi-Fi name? <$WIFI_SSID> "
read answer
if [ -n "$answer" ]; then
WIFI_SSID=$answer
fi
echo -n "* Enter Wi-Fi key? <$WIFI_KEY> "
read answer
if [ -n "$answer" ]; then
WIFI_KEY=$answer
fi
echo -n "* Enter Wi-Fi Guest key? <$KEY_GUEST> "
read answer
if [ -n "$answer" ]; then
WIFI_GUEST_KEY=$answer
fi
echo -n "* Enter this router ip address? <$IPADDR> "
read answer
if [ -n "$answer" ]; then
IPADDR=$answer
NETADDR=${IPADDR%.*}
fi
echo -n "* Enter Guest ip address mask? <$NETADDR_GUEST> "
read answer
if [ -n "$answer" ]; then
NETADDR_GUEST=$answer
fi
echo -n "* Enable Freebox TV QoS advices config? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
FBXTV=1
fi
echo -n "* Enable usb tethering config? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
UWAN=1
fi
echo -n "* Enable wwan config? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
WWAN=1
fi
echo -n "* Enable Advanced Reboot? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
AD_REBOOT=1
fi
echo -n "* Enable SQM QoS? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
SQM=1
echo "* "
echo "* Please check internet speed with https://www.speedtest.net/"
SQM_DL=500
echo -n "* Enter max donwload speed? <${SQM_DL}Mbps> "
read answer
if [ -n "$answer" ]; then
SQM_DL=$answer
fi
#SQM_DL=$(($SQM_DL * 1000 * 95/100))
SQM_DL=$(($SQM_DL * 1000))
SQM_UL=500
echo -n "* Enter max upload speed? <${SQM_UL}Mbps> "
read answer
if [ -n "$answer" ]; then
SQM_UL=$answer
fi
#SQM_UL=$(($SQM_UL * 1000 * 95/100))
SQM_UL=$(($SQM_UL * 1000))
fi
echo -n "* Enable statistics collectd? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
STATS=1
fi
echo -n "* Get ACME certificates with NAS by default? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
FW_FWD_NAS_CERTS=1
fi
# Save environment variables
cat << EOF > .env
DOMAIN="$DOMAIN"
WIFI_SSID="$WIFI_SSID"
WIFI_KEY="$WIFI_KEY"
WIFI_GUEST_KEY="$WIFI_GUEST_KEY"
IPADDR="$IPADDR"
FBXTV=$FBXTV
UWAN=$UWAN
WWAN=$WWAN
AD_REBOOT=$AD_REBOOT
SQM=$SQM
SQM_DL=$SQM_DL
SQM_UL=$SQM_UL
STATS=$STATS
FW_FWD_NAS_CERTS=$FW_FWD_NAS_CERTS
EOF
fi
runstart=$(date +%s)
echo "* "
echo "* Start time: $(date)"
echo "* "
###############################################################################
##### Base config luci/system/interface/dhcp/network/firewall/wireless
###############################################################################
echo "* UCI config luci"
uci set luci.main.mediaurlbase='/luci-static/bootstrap'
uci commit luci
echo "* UCI config hostname"
HOSTNAME=${HOSTNAME:-OpenWrt}
uci set system.@system[0].hostname="$HOSTNAME"
uci commit system
echo "* UCI config timezone"
uci set system.@system[0].zonename="$TZ_NAME"
uci set system.@system[0].timezone="$TZ"
uci commit system
echo "* UCI config lan network"
uci set network.lan.ipaddr="$IPADDR"
uci set network.lan.netmask='255.255.255.0'
# Cloudflare and APNIC
# Primary DNS: 1.1.1.1
# Secondary DNS: 1.0.0.1
# Malware Blocking Only
# Primary DNS: 1.1.1.2
# Secondary DNS: 1.0.0.2
# Malware and Adult Content
# Primary DNS: 1.1.1.3
# Secondary DNS: 1.0.0.3
uci -q del network.lan.dns
uci add_list network.lan.dns='1.1.1.3'
uci add_list network.lan.dns='1.0.0.3'
uci -q del network.lan.ip6assign
uci set network.wan.metric='10'
uci commit network
echo "* UCI config Guest network"
uci -q del network.guest_dev
uci set network.guest_dev=device
uci set network.guest_dev.type=bridge
uci set network.guest_dev.name=br-guest
uci -q del network.guest
uci set network.guest=interface
uci set network.guest.device='br-guest'
uci set network.guest.proto='static'
uci set network.guest.ipaddr="$NETADDR_GUEST.1"
uci set network.guest.netmask='255.255.255.0'
uci -q del network.guest.dns
uci add_list network.guest.dns='1.1.1.3'
uci add_list network.guest.dns='1.0.0.3'
uci commit network
echo "* UCI config dhcp"
uci set dhcp.@dnsmasq[0].local="/$LOCAL_DOMAIN/"
uci set dhcp.@dnsmasq[0].domain="$LOCAL_DOMAIN"
uci set dhcp.lan.start='100'
uci set dhcp.lan.limit='150'
uci set dhcp.lan.leasetime='12h'
uci set dhcp.lan.force='1'
# Disable DHCPv6 Server
uci -q del dhcp.lan.ra
uci -q del dhcp.lan.dhcpv6
uci -q del dhcp.lan.ra_management
uci set dhcp.guest=dhcp
uci set dhcp.guest.interface='guest'
uci set dhcp.guest.start='100'
uci set dhcp.guest.limit='50'
uci set dhcp.guest.leasetime='1h'
uci commit dhcp
echo "* UCI config firewall"
uci set firewall.@defaults[0].synflood_protect='1'
uci set firewall.@defaults[0].drop_invalid='1'
uci set firewall.@defaults[0].input='DROP'
uci set firewall.@defaults[0].output='ACCEPT'
uci set firewall.@defaults[0].forward='REJECT'
uci set firewall.@defaults[0].flow_offloading='1'
uci set firewall.@defaults[0].flow_offloading_hw='1'
# Remove existing config
for L in $(uci show firewall | grep "=zone"); do
uci -q del firewall.@zone[-1]
done
uci add firewall zone
uci set firewall.@zone[-1]=zone
uci set firewall.@zone[-1].name='lan'
uci set firewall.@zone[-1].network='lan'
uci set firewall.@zone[-1].input='ACCEPT'
uci set firewall.@zone[-1].output='ACCEPT'
uci set firewall.@zone[-1].forward='ACCEPT'
uci add firewall zone
uci set firewall.@zone[-1]=zone
uci set firewall.@zone[-1].name='wan'
uci set firewall.@zone[-1].network='wan wan6'
uci set firewall.@zone[-1].input='DROP'
uci set firewall.@zone[-1].output='ACCEPT'
uci set firewall.@zone[-1].forward='REJECT'
uci set firewall.@zone[-1].masq='1'
uci set firewall.@zone[-1].mtu_fix='1'
uci add firewall zone
uci set firewall.@zone[-1]=zone
uci set firewall.@zone[-1].name='guest'
uci set firewall.@zone[-1].network='guest'
uci set firewall.@zone[-1].input='DROP'
uci set firewall.@zone[-1].output='ACCEPT'
uci set firewall.@zone[-1].forward='REJECT'
# Remove existing config
for L in $(uci show firewall | grep "=forwarding"); do
uci -q del firewall.@forwarding[-1]
done
uci add firewall forwarding
uci set firewall.@forwarding[-1].src='lan'
uci set firewall.@forwarding[-1].dest='wan'
uci add firewall forwarding
uci set firewall.@forwarding[-1].src='guest'
uci set firewall.@forwarding[-1].dest='wan'
# Remove existing config
for L in $(uci show firewall | grep "=redirect"); do
uci -q del firewall.@redirect[-1]
done
# Add automatically firewall forward redirection
if [ -f .env ]; then
echo "* UCI config firewall redirect"
# FW_FWD="name|proto|src_dport|dest_ip|dest_port|enabled"
# FW_FWD="Allow-http|tcp-udp|80|$NETADDR.10|8080|off"
for L in $(cat .env | grep "^FW_FWD="); do
# Get the value after =
V=${L#*=}
# Evaluate variable inside the line
V=$(eval echo $V)
# Remove " from string
#V=${V//\"}
uci add firewall redirect
uci set firewall.@redirect[-1]=redirect
uci set firewall.@redirect[-1].name="$(echo $V | cut -d'|' -f1)"
uci set firewall.@redirect[-1].target='DNAT'
if [ "$(echo $V | cut -d'|' -f2)" == "tcp" ]; then
uci set firewall.@redirect[-1].proto='tcp'
elif [ "$(echo $V | cut -d'|' -f2)" == "udp" ]; then
uci set firewall.@redirect[-1].proto='udp'
elif [ "$(echo $V | cut -d'|' -f2)" == "tcp-udp" ]; then
uci -q del firewall.@redirect[-1].proto
uci add_list firewall.@redirect[-1].proto='tcp'
uci add_list firewall.@redirect[-1].proto='udp'
else
uci set firewall.@redirect[-1].proto='all'
fi
uci set firewall.@redirect[-1].src='wan'
uci set firewall.@redirect[-1].dest='lan'
uci set firewall.@redirect[-1].src_dport="$(echo $V | cut -d'|' -f3)"
uci set firewall.@redirect[-1].dest_ip="$(echo $V | cut -d'|' -f4)"
if [ -n "$(echo $V | cut -d'|' -f5)" ]; then
uci set firewall.@redirect[-1].dest_port="$(echo $V | cut -d'|' -f5)"
fi
if [ -n "$(echo $V | cut -d'|' -f6)" ] && [ "$(echo $V | cut -d'|' -f6)" == "off" ]; then
uci set firewall.@redirect[-1].enabled='0'
fi
done
fi
uci commit firewall
echo "* UCI config firewall rule"
uci add firewall rule
uci set firewall.@rule[-1]=rule
uci set firewall.@rule[-1].name='Guest-DHCP'
uci set firewall.@rule[-1].src='guest'
uci set firewall.@rule[-1].dest_port='67-68'
uci set firewall.@rule[-1].proto='udp'
uci set firewall.@rule[-1].target='ACCEPT'
uci add firewall rule
uci set firewall.@rule[-1]=rule
uci set firewall.@rule[-1].name='Guest-DNS'
uci set firewall.@rule[-1].src='guest'
uci set firewall.@rule[-1].dest_port='53'
uci -q del firewall.@rule[-1].proto
uci add_list firewall.@rule[-1].proto='tcp'
uci add_list firewall.@rule[-1].proto='udp'
uci set firewall.@rule[-1].target='ACCEPT'
uci commit firewall
echo "* UCI config wireless"
uci set wireless.radio0.hwmode='11a'
uci set wireless.radio0.htmode='VHT80'
uci -q del wireless.radio0.legacy_rates
uci set wireless.radio0.country='FR'
uci set wireless.radio0.bursting='1'
uci set wireless.radio0.ff='1'
uci set wireless.radio0.compression='1'
uci set wireless.radio0.turbo='1'
uci set wireless.radio0.channel='auto'
uci set wireless.radio0.channels='116 120 124 128 132'
uci set wireless.radio0.cell_density='0'
uci set wireless.radio0.disabled='0'
uci set wireless.default_radio0.mode='ap'
uci set wireless.default_radio0.ssid="$WIFI_SSID"
uci set wireless.default_radio0.key="$WIFI_KEY"
uci set wireless.default_radio0.encryption='psk-mixed+ccmp'
uci set wireless.default_radio0.network='lan'
uci set wireless.radio1.hwmode='11g'
uci set wireless.radio1.htmode='HT40'
uci -q del wireless.radio1.legacy_rates
uci set wireless.radio1.country='FR'
uci set wireless.radio1.bursting='1'
uci set wireless.radio1.ff='1'
uci set wireless.radio1.compression='1'
uci set wireless.radio1.turbo='1'
uci set wireless.radio1.channel='auto'
uci set wireless.radio1.cell_density='0'
uci set wireless.radio1.disabled='0'
uci set wireless.default_radio1.mode='ap'
uci set wireless.default_radio1.ssid="$WIFI_SSID"
uci set wireless.default_radio1.key="$WIFI_KEY"
uci set wireless.default_radio1.encryption='psk-mixed+ccmp'
uci set wireless.default_radio1.network='lan'
if [ $MESH -eq 1 ]; then
uci set wireless.wifinet0=wifi-iface
uci set wireless.wifinet0.device='radio0'
uci set wireless.wifinet0.mode='ap'