forked from anadahz/raspi-config_lepidopter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspi-config
executable file
·921 lines (825 loc) · 26.1 KB
/
raspi-config
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
#!/bin/sh
# Original release of raspi-config http://github.com/asb/raspi-config
#
# See LICENSE file for copyright and license details
#
INTERACTIVE=True
ASK_TO_REBOOT=0
calc_wt_size() {
# NOTE: it's tempting to redirect stderr to /dev/null, so supress error
# output from tput. However in this case, tput detects neither stdout or
# stderr is a tty and so only gives default 80, 24 values
WT_HEIGHT=17
WT_WIDTH=$(tput cols)
if [ -z "$WT_WIDTH" ] || [ "$WT_WIDTH" -lt 60 ]; then
WT_WIDTH=80
fi
if [ "$WT_WIDTH" -gt 178 ]; then
WT_WIDTH=120
fi
WT_MENU_HEIGHT=$(($WT_HEIGHT-8))
}
do_about() {
whiptail --msgbox "\
This tool provides a straight-forward way of doing initial
configuration of the Raspberry Pi. Although it can be run
at any time, some of the options may have difficulties if
you have heavily customised your installation.\
" 20 70 1
}
# Package 'parted' required
do_expand_rootfs() {
if ! [ -h /dev/root ]; then
whiptail --msgbox "/dev/root does not exist or is not a symlink. Don't know how to expand" 20 60 2
return 0
fi
ROOT_PART=$(readlink /dev/root)
PART_NUM=${ROOT_PART#mmcblk0p}
if [ "$PART_NUM" = "$ROOT_PART" ]; then
whiptail --msgbox "/dev/root is not an SD card. Don't know how to expand" 20 60 2
return 0
fi
# NOTE: the NOOBS partition layout confuses parted. For now, let's only
# agree to work with a sufficiently simple partition layout
if [ "$PART_NUM" -ne 2 ]; then
whiptail --msgbox "Your partition layout is not currently supported by this tool. You are probably using NOOBS, in which case your root filesystem is already expanded anyway." 20 60 2
return 0
fi
LAST_PART_NUM=$(parted /dev/mmcblk0 -ms unit s p | tail -n 1 | cut -f 1 -d:)
if [ "$LAST_PART_NUM" != "$PART_NUM" ]; then
whiptail --msgbox "/dev/root is not the last partition. Don't know how to expand" 20 60 2
return 0
fi
# Get the starting offset of the root partition
PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d:)
[ "$PART_START" ] || return 1
# Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted
fdisk /dev/mmcblk0 <<EOF
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START
p
w
EOF
ASK_TO_REBOOT=1
# now set up an init.d script
cat <<\EOF > /etc/init.d/resize2fs_once &&
#!/bin/sh
### BEGIN INIT INFO
# Provides: resize2fs_once
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5 S
# Default-Stop:
# Short-Description: Resize the root filesystem to fill partition
# Description:
### END INIT INFO
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting resize2fs_once" &&
resize2fs /dev/root &&
rm /etc/init.d/resize2fs_once &&
update-rc.d resize2fs_once remove &&
log_end_msg $?
;;
*)
echo "Usage: $0 start" >&2
exit 3
;;
esac
EOF
chmod +x /etc/init.d/resize2fs_once &&
update-rc.d resize2fs_once defaults &&
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Root partition has been resized.\nThe filesystem will be enlarged upon the next reboot" 20 60 2
fi
}
set_config_var() {
lua - "$1" "$2" "$3" <<EOF > "$3.bak"
local key=assert(arg[1])
local value=assert(arg[2])
local fn=assert(arg[3])
local file=assert(io.open(fn))
local made_change=false
for line in file:lines() do
if line:match("^#?%s*"..key.."=.*$") then
line=key.."="..value
made_change=true
end
print(line)
end
if not made_change then
print(key.."="..value)
end
EOF
mv "$3.bak" "$3"
}
get_config_var() {
lua - "$1" "$2" <<EOF
local key=assert(arg[1])
local fn=assert(arg[2])
local file=assert(io.open(fn))
for line in file:lines() do
local val = line:match("^#?%s*"..key.."=(.*)$")
if (val ~= nil) then
print(val)
break
end
end
EOF
}
# $1 is 0 to disable overscan, 1 to disable it
set_overscan() {
# Stop if /boot is not a mountpoint
if ! mountpoint -q /boot; then
return 1
fi
[ -e /boot/config.txt ] || touch /boot/config.txt
if [ "$1" -eq 0 ]; then # disable overscan
sed /boot/config.txt -i -e "s/^overscan_/#overscan_/"
set_config_var disable_overscan 1 /boot/config.txt
else # enable overscan
set_config_var disable_overscan 0 /boot/config.txt
fi
}
do_overscan() {
whiptail --yesno "What would you like to do with overscan" 20 60 2 \
--yes-button Disable --no-button Enable
RET=$?
if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
ASK_TO_REBOOT=1
set_overscan $RET;
else
return 1
fi
}
do_change_pass() {
whiptail --msgbox "You will now be asked to enter a new password for the root user" 20 60 1
passwd root &&
whiptail --msgbox "Password changed successfully" 20 60 1
}
# Package 'keyboard-configuration' required
do_configure_keyboard() {
dpkg-reconfigure keyboard-configuration &&
printf "Reloading keymap. This may take a short while\n" &&
invoke-rc.d keyboard-setup start
}
do_change_locale() {
dpkg-reconfigure locales
}
do_change_timezone() {
dpkg-reconfigure tzdata
}
do_change_hostname() {
whiptail --msgbox "\
Please note: RFCs mandate that a hostname's labels \
may contain only the ASCII letters 'a' through 'z' (case-insensitive),
the digits '0' through '9', and the hyphen.
Hostname labels cannot begin or end with a hyphen.
No other symbols, punctuation characters, or blank spaces are permitted.\
" 20 70 1
CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
NEW_HOSTNAME=$(whiptail --inputbox "Please enter a hostname" 20 60 "$CURRENT_HOSTNAME" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
echo $NEW_HOSTNAME > /etc/hostname
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/g" /etc/hosts
ASK_TO_REBOOT=1
fi
}
do_memory_split() { # Memory Split
if [ -e /boot/start_cd.elf ]; then
# New-style memory split setting
if ! mountpoint -q /boot; then
return 1
fi
## get current memory split from /boot/config.txt
CUR_GPU_MEM=$(get_config_var gpu_mem /boot/config.txt)
[ -z "$CUR_GPU_MEM" ] && CUR_GPU_MEM=64
## ask users what gpu_mem they want
NEW_GPU_MEM=$(whiptail --inputbox "How much memory should the GPU have? e.g. 16/32/64/128/256" \
20 70 -- "$CUR_GPU_MEM" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
set_config_var gpu_mem "$NEW_GPU_MEM" /boot/config.txt
ASK_TO_REBOOT=1
fi
else # Old firmware so do start.elf renaming
get_current_memory_split
MEMSPLIT=$(whiptail --menu "Set memory split.\n$MEMSPLIT_DESCRIPTION" 20 60 10 \
"240" "240MiB for ARM, 16MiB for VideoCore" \
"224" "224MiB for ARM, 32MiB for VideoCore" \
"192" "192MiB for ARM, 64MiB for VideoCore" \
"128" "128MiB for ARM, 128MiB for VideoCore" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
set_memory_split ${MEMSPLIT}
ASK_TO_REBOOT=1
fi
fi
}
get_current_memory_split() {
# Stop if /boot is not a mountpoint
if ! mountpoint -q /boot; then
return 1
fi
AVAILABLE_SPLITS="128 192 224 240"
MEMSPLIT_DESCRIPTION=""
for SPLIT in $AVAILABLE_SPLITS;do
if [ -e /boot/arm${SPLIT}_start.elf ] && cmp /boot/arm${SPLIT}_start.elf /boot/start.elf >/dev/null 2>&1;then
CURRENT_MEMSPLIT=$SPLIT
MEMSPLIT_DESCRIPTION="Current: ${CURRENT_MEMSPLIT}MiB for ARM, $((256 - $CURRENT_MEMSPLIT))MiB for VideoCore"
break
fi
done
}
set_memory_split() {
cp -a /boot/arm${1}_start.elf /boot/start.elf
sync
}
do_overclock() {
whiptail --msgbox "\
Be aware that overclocking may reduce the lifetime of your
Raspberry Pi. If overclocking at a certain level causes
system instability, try a more modest overclock. Hold down
shift during boot to temporarily disable overclock.
See http://elinux.org/RPi_Overclocking for more information.\
" 20 70 1
OVERCLOCK=$(whiptail --menu "Chose overclock preset" 20 60 10 \
"None" "700MHz ARM, 250MHz core, 400MHz SDRAM, 0 overvolt" \
"Modest" "800MHz ARM, 250MHz core, 400MHz SDRAM, 0 overvolt" \
"Medium" "900MHz ARM, 250MHz core, 450MHz SDRAM, 2 overvolt" \
"High" "950MHz ARM, 250MHz core, 450MHz SDRAM, 6 overvolt" \
"Turbo" "1000MHz ARM, 500MHz core, 600MHz SDRAM, 6 overvolt" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
case "$OVERCLOCK" in
None)
set_overclock None 700 250 400 0
;;
Modest)
set_overclock Modest 800 250 400 0
;;
Medium)
set_overclock Medium 900 250 450 2
;;
High)
set_overclock High 950 250 450 6
;;
Turbo)
whiptail --msgbox "Warning: some people have reported SD card corruption with this level of overclock." \
20 70 1
[ $? -ne 0 ] && return 1
set_overclock Turbo 1000 500 600 6
;;
*)
whiptail --msgbox "Programmer error, unrecognised overclock preset" 20 60 2
return 1
;;
esac
ASK_TO_REBOOT=1
fi
}
set_overclock() {
set_config_var arm_freq $2 /boot/config.txt &&
set_config_var core_freq $3 /boot/config.txt &&
set_config_var sdram_freq $4 /boot/config.txt &&
set_config_var over_voltage $5 /boot/config.txt &&
# now set up an init.d script
cat <<\EOF > /etc/init.d/switch_cpu_governor &&
#!/bin/sh
### BEGIN INIT INFO
# Provides: switch_cpu_governor
# Required-Start: udev mountkernfs $remote_fs
# Required-Stop:
# Default-Start: S
# Default-Stop:
# Short-Description: Switch to ondemand cpu governor (unless shift key is pressed)
# Description:
### END INIT INFO
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Checking if shift key is held down"
timeout 1 thd --dump /dev/input/event* | grep -q "LEFTSHIFT\|RIGHTSHIFT"
if [ $? -eq 0 ]; then
printf " Yes. Not switching scaling governor"
log_end_msg 0
else
SYS_CPUFREQ_GOVERNOR=/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
[ -e $SYS_CPUFREQ_GOVERNOR ] && echo "ondemand" > $SYS_CPUFREQ_GOVERNOR
echo 70 > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold
printf " No. Switching to ondemand scaling governor"
log_end_msg 0
fi
;;
*)
echo "Usage: $0 start" >&2
exit 3
;;
esac
EOF
chmod +x /etc/init.d/switch_cpu_governor &&
update-rc.d switch_cpu_governor defaults &&
whiptail --msgbox "Set overclock to preset '$1'" 20 60 2
}
do_ssh() {
if [ -e /var/log/regen_ssh_keys.log ] && ! grep -q "^finished" /var/log/regen_ssh_keys.log; then
whiptail --msgbox "Initial ssh key generation still running. Please wait and try again." 20 60 2
return 1
fi
whiptail --yesno "Would you like the SSH server enabled or disabled?" 20 60 2 \
--yes-button Enable --no-button Disable
RET=$?
if [ $RET -eq 0 ]; then
update-rc.d ssh enable &&
invoke-rc.d ssh start &&
whiptail --msgbox "SSH server enabled" 20 60 1
elif [ $RET -eq 1 ]; then
update-rc.d ssh disable &&
whiptail --msgbox "SSH server disabled" 20 60 1
else
return $RET
fi
}
disable_raspi_config_at_boot() {
if [ -e /etc/profile.d/raspi-config.sh ]; then
rm -f /etc/profile.d/raspi-config.sh
sed -i /etc/inittab \
-e "s/^#\(.*\)#\s*RPICFG_TO_ENABLE\s*/\1/" \
-e "/#\s*RPICFG_TO_DISABLE/d"
telinit q
fi
}
do_finish() {
disable_raspi_config_at_boot
if [ $ASK_TO_REBOOT -eq 1 ]; then
whiptail --yesno "Would you like to reboot now?" 20 60 2
if [ $? -eq 0 ]; then # yes
sync
reboot
fi
fi
exit 0
}
# $1 = filename, $2 = key name
get_json_string_val() {
sed -n -e "s/^[[:space:]]*\"$2\"[[:space:]]*:[[:space:]]*\"\(.*\)\"[[:space:]]*,$/\1/p" $1
}
do_apply_os_config() {
[ -e /boot/os_config.json ] || return 0
NOOBSFLAVOUR=$(get_json_string_val /boot/os_config.json flavour)
NOOBSLANGUAGE=$(get_json_string_val /boot/os_config.json language)
NOOBSKEYBOARD=$(get_json_string_val /boot/os_config.json keyboard)
if [ -n "$NOOBSFLAVOUR" ]; then
printf "Setting flavour to %s based on os_config.json from NOOBS. May take a while\n" "$NOOBSFLAVOUR"
if printf "%s" "$NOOBSFLAVOUR" | grep -q "Scratch"; then
disable_raspi_config_at_boot
enable_boot_to_scratch
else
printf "Unrecognised flavour. Ignoring\n"
fi
fi
# TODO: currently ignores en_gb settings as we assume we are running in a
# first boot context, where UK English settings are default
case "$NOOBSLANGUAGE" in
"en")
if [ "$NOOBSKEYBOARD" = "gb" ]; then
DEBLANGUAGE="" # UK english is the default, so ignore
else
DEBLANGUAGE="en_US.UTF-8"
fi
;;
"de")
DEBLANGUAGE="de_DE.UTF-8"
;;
"fi")
DEBLANGUAGE="fi_FI.UTF-8"
;;
"fr")
DEBLANGUAGE="fr_FR.UTF-8"
;;
"hu")
DEBLANGUAGE="hu_HU.UTF-8"
;;
"ja")
DEBLANGUAGE="ja_JP.UTF-8"
;;
"nl")
DEBLANGUAGE="nl_NL.UTF-8"
;;
"pt")
DEBLANGUAGE="pt_PT.UTF-8"
;;
"ru")
DEBLANGUAGE="ru_RU.UTF-8"
;;
"zh_CN")
DEBLANGUAGE="zh_CN.UTF-8"
;;
*)
printf "Language '%s' not handled currently. Run sudo raspi-config to set up" "$NOOBSLANGUAGE"
;;
esac
if [ -n "$DEBLANGUAGE" ]; then
printf "Setting language to %s based on os_config.json from NOOBS. May take a while\n" "$DEBLANGUAGE"
cat << EOF | debconf-set-selections
locales locales/locales_to_be_generated multiselect $DEBLANGUAGE UTF-8
EOF
rm /etc/locale.gen
dpkg-reconfigure -f noninteractive locales
update-locale LANG="$DEBLANGUAGE"
cat << EOF | debconf-set-selections
locales locales/default_environment_locale select $DEBLANGUAGE
EOF
fi
if [ -n "$NOOBSKEYBOARD" -a "$NOOBSKEYBOARD" != "gb" ]; then
printf "Setting keyboard layout to %s based on os_config.json from NOOBS. May take a while\n" "$NOOBSKEYBOARD"
sed -i /etc/default/keyboard -e "s/^XKBLAYOUT.*/XKBLAYOUT=\"$NOOBSKEYBOARD\"/"
dpkg-reconfigure -f noninteractive keyboard-configuration
invoke-rc.d keyboard-setup start
fi
return 0
}
#
# Command line options for non-interactive use
#
for i in $*
do
case $i in
--memory-split)
OPT_MEMORY_SPLIT=GET
printf "Not currently supported\n"
exit 1
;;
--memory-split=*)
OPT_MEMORY_SPLIT=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
printf "Not currently supported\n"
exit 1
;;
--expand-rootfs)
INTERACTIVE=False
do_expand_rootfs
printf "Please reboot\n"
exit 0
;;
--apply-os-config)
INTERACTIVE=False
do_apply_os_config
exit $?
;;
*)
# unknown option
;;
esac
done
#if [ "GET" = "${OPT_MEMORY_SPLIT:-}" ]; then
# set -u # Fail on unset variables
# get_current_memory_split
# echo $CURRENT_MEMSPLIT
# exit 0
#fi
# Everything else needs to be run as root
if [ $(id -u) -ne 0 ]; then
printf "Script must be run as root. Try 'sudo raspi-config'\n"
exit 1
fi
if [ -n "${OPT_MEMORY_SPLIT:-}" ]; then
set -e # Fail when a command errors
set_memory_split "${OPT_MEMORY_SPLIT}"
exit 0
fi
do_internationalisation_menu() {
FUN=$(whiptail --title "Raspberry Pi Software Configuration Tool (raspi-config)" --menu "Internationalisation Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
"I1 Change Locale" "Set up language and regional settings to match your location" \
"I2 Change Timezone" "Set up timezone to match your location" \
"I3 Change Keyboard Layout" "Set the keyboard layout to match your keyboard" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
I1\ *) do_change_locale ;;
I2\ *) do_change_timezone ;;
I3\ *) do_configure_keyboard ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
do_advanced_menu() {
FUN=$(whiptail --title "Raspberry Pi Software Configuration Tool (raspi-config)" --menu "Advanced Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
"A1 Overscan" "You may need to configure overscan if black bars are present on display" \
"A2 Hostname" "Set the visible name for this Pi on a network" \
"A3 Memory Split" "Change the amount of memory made available to the GPU" \
"A4 SSH" "Enable/Disable remote command line access to your Pi using SSH" \
"A5 Update" "Update this tool to the latest version" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
A1\ *) do_overscan ;;
A2\ *) do_change_hostname ;;
A3\ *) do_memory_split ;;
A4\ *) do_ssh ;;
A5\ *) do_update ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
# [Upgrade http://github.com/sentfanwyaerda/raspi-config/ ]
do_fsroot_resize_menu() {
FUN=$(whiptail --title "Raspberry Pi Software Configuration Tool (raspi-config)" --menu "Expand Filesystem" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
"R0 Expand Filesystem (default)" "Ensures that all of the SD card (mmcblk0p1) storage is available to the OS" \
"R1 Expand Home Filesystem" "Expands /home (mmcblk0p5) to all of the SD card" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
R0\ *) do_expand_rootfs ;;
R1\ *) do_expand_mmcblk0p5 ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
do_expand_mmcblk0p5() {
fdisk /dev/mmcblk0 <<EOF
p
d
5
d
3
p
n
e
3
n
l
p
w
EOF
ASK_TO_REBOOT=1
apply_resize2fs_once
echo "resize2fs /dev/mmcblk0p5" >> /etc/init.d/resize2fs_once
}
apply_resize2fs_once() {
if [ ! -f /etc/init.d/resize2fs_once ]; then
# now set up an init.d script
cat <<\EOF > /etc/init.d/resize2fs_once &&
#!/bin/sh
### BEGIN INIT INFO
# Provides: resize2fs_once
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5 S
# Default-Stop:
# Short-Description: Resize the root filesystem to fill partition
# Description:
### END INIT INFO
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting resize2fs_once" &&
resize2fs /dev/root &&
rm /etc/init.d/resize2fs_once &&
update-rc.d resize2fs_once remove &&
log_end_msg $?
;;
*)
echo "Usage: $0 start" >&2
exit 3
;;
esac
EOF
chmod +x /etc/init.d/resize2fs_once &&
update-rc.d resize2fs_once defaults
fi
}
do_additional_menu() {
FUN=$(whiptail --title "Raspberry Pi Software Configuration Tool (raspi-config)" --menu "Additional Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
"0 Add user" "" \
"1 Set the Clock" "you can set ntpd to synchronize with the internet" \
"2 RaspberryPi Firmware Update" ".." \
"3 Upgrade all installed programs" ".." \
"4 Setup Wifi" "Configure Wifi settings" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
0\ *) do_add_user ;;
1\ *) do_ntp_install ;;
2\ *) do_rpi_update ;;
3\ *) do_system_upgrade ;;
4\ *) do_mikerr_wifi ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
do_add_user() {
NAME=$(whiptail --inputbox "Which username do you want to add to your system?" \
20 70 -- "$NAME" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
adduser $NAME
fi
# PASSWD=$(whiptail --inputbox "And the password you want to use?" \
# 20 70 -- "$PASSWD" 3>&1 1>&2 2>&3)
# if [ $? -eq 0 ]; then
# passwd $NAME $PASSWD
# fi
#+ sudoer $NAME
echo "$NAME ALL=(ALL:ALL) ALL\n\r" >> /etc/sudoers
}
do_ntp_install() {
apt-get install ntp fake-hwclock ntpdate;
# @/etc/rc.local "service ntp restart"
# ntpq -p && hwclock -w ;;
ntpdate -u ntp.ubuntu.com
}
do_rpi_update(){
wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update && chmod +x /usr/bin/rpi-update
whiptail --yesno "Hexxeh's rpi-update has been installed.\n\
Would you like to run rpi-update now?" 8 60 2 \
--yes-button Yes --no-button No
RET=$?
if [ $RET -eq 0 ]; then
rpi-update;
ASK_TO_REBOOT=1
#else return 1
fi
whiptail --msgbox "in the future run 'rpi-update' to update the firmware manually" 8 40 2
}
do_system_upgrade() {
apt-get update && apt-get upgrade && apt-get autoclean;
whiptail --msgbox "Now, all programs are up to date!" 8 40 2
}
do_update() {
# Backup first
cp -a /sbin/raspi-config{,.bak}
wget https://raw.github.com/anadahz/raspi-config_lepidopter/master/raspi-config\
-O /sbin/raspi-config
chmod +x /sbin/raspi-config
printf "Sleeping 5 seconds before reloading raspi-config\n" &&
sleep 5 &&
exec raspi-config
}
# Packages: 'wireless-tools wpasupplicant' required
do_mikerr_wifi() {
wifidevice=wlan0
WPAFILE=/etc/wpa_supplicant/wpa_supplicant.conf
{
iwlist $wifidevice scan | grep 'ESSID' | sed -e 's/.*ESSID:"\([^"]\+\)".*/ \1/' > /tmp/ap_list.txt
} | whiptail --gauge "Scanning Wifi, please wait" 5 50 10
echo "whiptail --title \"Choose SSID, or enter manually\" \\" > /tmp/choose_ap.sh
echo "--radiolist \"Choose SSID\" \\" >> /tmp/choose_ap.sh
LINES=`wc -l < /tmp/ap_list.txt`
LINES=$((${LINES}+1))
echo "10 60 ${LINES} \\" >> /tmp/choose_ap.sh
for LINE in `cat /tmp/ap_list.txt`
do
echo "$LINE '' off \\" >> /tmp/choose_ap.sh
done
echo "Enter\ manually '' on 2>/tmp/ssid.ans" >>/tmp/choose_ap.sh
chmod 777 /tmp/choose_ap.sh
. /tmp/choose_ap.sh
if [ $? -ne 0 ]; then return 0; fi
SSID=`cat /tmp/ssid.ans`
if [ "$SSID" = "Enter manually" ]; then
SSID=$(whiptail --inputbox "SSID Name" 20 70 3>&1 1>&2 2>&3)
if [ -z $SSID ]; then return 0 ; fi
fi
ENCRYPTION=$(whiptail --title "Encryption" --menu "Encryption for network: $SSID " $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
"1" "WPA/WPA2" \
"2" "WEP (hex)" \
"3" "WEP (ascii)" \
"4" "None" \
3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then return 0; fi
WPANEW=/tmp/wpanew.conf
WPATMP=/tmp/wpa.conf
case $ENCRYPTION in
'1')
PASSPHRASE=$(whiptail --inputbox "WPA Passphrase for $SSID" 20 70 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then return 0; fi
CRYPTPASS=$(wpa_passphrase $SSID $PASSPHRASE | grep -v "^#" | grep -v "#psk=")
PASSERROR="Passphrase must be 8..63 characters"
if [ "$CRYPTPASS" != "$PASSERROR" ]; then
echo "$CRYPTPASS" > $WPANEW
else
whiptail --msgbox "ERROR: $PASSERROR" 20 70 1
return 0;
fi
;;
'2')
PASSPHRASE=$(whiptail --inputbox "WEP key (Hex)" 20 70 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then return 0; fi
echo "network={
ssid=\"$SSID\"
key_mgmt=NONE
wep_key0=$PASSPHRASE
}" > $WPANEW
;;
'3')
PASSPHRASE=$(whiptail --inputbox "WEP key (ascii)" 20 70 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then return 0; fi
echo "network={
ssid=\"$SSID\"
key_mgmt=NONE
wep_key0=\"$PASSPHRASE\"
}" > $WPANEW
;;
'4')
echo "network={
ssid=\"$SSID\"
key_mgmt=NONE
}" > $WPANEW
;;
esac
# Remove previous matching SSID config first
perl -00 -pe "s/network=\{[^}]*ssid=\"$SSID\"*[^}]*\}//g" $WPAFILE > $WPATMP
# append /merge new config at end of config file
cat $WPATMP $WPANEW > $WPAFILE
whiptail --msgbox "Wifi settings updated for SSID: $SSID" 20 60 1
# connect right now ?
#wpa_supplicant -B -i$wifidevice -c $WPAFILE
#dhclient $wifidevice -1
}
do_ooniprobe_menu() {
FUN=$(whiptail --title "Raspberry Pi Software Configuration Tool (raspi-config)" --menu "ooniprobe related configuration" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
"0 Update ooniprobe" "" \
"1 Add ooniprobe cronjob" ""\
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
0\ *) do_update_ooniprobe ;;
1\ *) do_add_ooniprobe_cronjob ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
do_update_ooniprobe() {
OONIPROBEGITDIR=$(whiptail --inputbox "Current ooniprobe git folder?" \
20 70 -- "/root/ooni-probe" 3>&1 1>&2 2>&3)
if [ -d $OONIPROBEGITDIR ]; then
cd $OONIPROBEGITDIR
git pull origin
python setup.py install
else
mkdir $OONIPROBEGITDIR &&
git clone https://github.com/TheTorProject/ooni-probe.git $OONIPROBEGITDIR
cd $OONIPROBEGITDIR
python setup.py install
fi
}
do_add_ooniprobe_cronjob() {
whiptail --msgbox "Adding ooniprobe cronjob is not implemented yet." 20 60 2
# TODO: Add ooniprobe cronjob
# echo "0 0 * * * ooniprobe -i /usr/share/ooni/decks/basic.deck" \
# | sudo crontab - ooniprobe -i /usr/share/ooni/decks/basic.deck
}
#
# Interactive use loop
#
calc_wt_size
while true; do
FUN=$(whiptail --title "Raspberry Pi Software Configuration Tool (raspi-config)" --menu "Setup Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Finish --ok-button Select \
"1 Expand Filesystem" "Ensures that all of the SD card storage is available to the OS" \
"2 Change User Password" "Change password for the default user (root)" \
"3 Internationalisation Options" "Set up language and regional settings to match your location" \
"4 Overclock" "Configure overclocking for your Pi" \
"5 Advanced Options" "Configure advanced settings" \
"6 Extra Options" "Extra settings" \
"7 ooniprobe Options" "ooniprobe related settings" \
"i About raspi-config" "Information about this configuration tool" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
do_finish
elif [ $RET -eq 0 ]; then
case "$FUN" in
1\ *) do_expand_rootfs ;;
2\ *) do_change_pass ;;
3\ *) do_internationalisation_menu ;;
4\ *) do_overclock ;;
5\ *) do_advanced_menu ;;
6\ *) do_additional_menu ;;
7\ *) do_ooniprobe_menu ;;
i\ *) do_about ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
else
exit 1
fi
done