/
firstrun
executable file
·301 lines (259 loc) · 11.1 KB
/
firstrun
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
#!/bin/bash
### BEGIN INIT INFO
# Provides: firstrun
# Required-Start: $all
# Required-Stop:
# Should-Start: armhwinfo
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Armbian first run tasks
# Description: Something needs to be done when is
# starting at first time.
### END INIT INFO
. /etc/armbian-release
. /etc/os-release
. /lib/init/vars.sh
. /lib/lsb/init-functions
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Log=/var/log/armhwinfo.log
do_firstrun_automated_user_configuration()
{
#-----------------------------------------------------------------------------
#Notes:
# - See /boot/armbian_first_run.txt for full list of available variables
# - Variable names here must here must match ones in config/armbian_first_run.txt
#-----------------------------------------------------------------------------
#Config FP
local fp_config='/boot/armbian_first_run.txt'
#-----------------------------------------------------------------------------
#Grab user requested settings
if [[ -f $fp_config ]]; then
# Convert line endings to Unix from Dos
sed -i $'s/\r$//' "$fp_config"
# check syntax
bash -n "$fp_config" || return
# Load vars directly from file
source "$fp_config"
#-----------------------------------------------------------------------------
# - Remove configuration file
if [[ $FR_general_delete_this_file_after_completion == 1 ]]; then
dd if=/dev/urandom of="$fp_config" bs=16K count=1
sync
rm "$fp_config"
fi
#-----------------------------------------------------------------------------
# Set Network
if [[ $FR_net_change_defaults == 1 ]]; then
# - Get 1st index of available wlan and eth adapters
local fp_ifconfig_tmp='/tmp/.ifconfig'
ifconfig -a > "$fp_ifconfig_tmp" #export to file, should be quicker in loop than calling ifconfig each time.
# find eth[0-9]
for ((i=0; i<=9; i++))
do
if (( $(cat "$fp_ifconfig_tmp" | grep -ci -m1 "eth$i") )); then
eth_index=$i
break
fi
done
# find wlan[0-9]
for ((i=0; i<=9; i++))
do
if (( $(cat "$fp_ifconfig_tmp" | grep -ci -m1 "wlan$i") )); then
wlan_index=$i
break
fi
done
rm "$fp_ifconfig_tmp"
# - Kill dhclient
killall -w dhclient
# - Drop Connections
ifdown eth$eth_index --force
ifdown wlan$wlan_index --force
# - Wifi enable
if [[ $FR_net_wifi_enabled == 1 ]]; then
#Enable Wlan, disable Eth
FR_net_ethernet_enabled=0
sed -i "/allow-hotplug wlan$wlan_index/c\allow-hotplug wlan$wlan_index" /etc/network/interfaces
sed -i "/allow-hotplug eth$eth_index/c\#allow-hotplug eth$eth_index" /etc/network/interfaces
#Set SSid (covers both WEP and WPA)
sed -i "/wireless-essid /c\ wireless-essid $FR_net_wifi_ssid" /etc/network/interfaces
sed -i "/wpa-ssid /c\ wpa-ssid $FR_net_wifi_ssid" /etc/network/interfaces
#Set Key (covers both WEP and WPA)
sed -i "/wireless-key /c\ wireless-key $FR_net_wifi_key" /etc/network/interfaces
sed -i "/wpa-psk /c\ wpa-psk $FR_net_wifi_key" /etc/network/interfaces
#Set wifi country code
iw reg set "$FR_net_wifi_countrycode"
#Disable powersaving for known chips that suffer from powersaving features causing connection dropouts.
# This is espically true for the 8192cu and 8188eu.
#FOURDEE: This may be better located as default in ARMbian during build (eg: common), as currently, not active until after a reboot.
# - Realtek | all use the same option, so create array.
local realtek_array=(
"8192cu"
"8188eu"
)
for ((i=0; i<${#realtek_array[@]}; i++))
do
echo -e "options ${realtek_array[$i]} rtw_power_mgnt=0" > /etc/modprobe.d/realtek_"${realtek_array[$i]}".conf
done
unset realtek_array
# - Ethernet enable
elif [[ $FR_net_ethernet_enabled == 1 ]]; then
#Enable Eth, disable Wlan
FR_net_wifi_enabled=0
sed -i "/allow-hotplug eth$eth_index/c\allow-hotplug eth$eth_index" /etc/network/interfaces
#sed -i "/allow-hotplug wlan$wlan_index/c\#allow-hotplug wlan$wlan_index" /etc/network/interfaces
fi
# - Static IP enable
if [[ $FR_net_use_static == 1 ]]; then
if [[ $FR_net_wifi_enabled == 1 ]]; then
sed -i "/iface wlan$wlan_index inet/c\iface wlan$wlan_index inet static" /etc/network/interfaces
elif [[ $FR_net_ethernet_enabled == 1 ]]; then
sed -i "/iface eth$eth_index inet/c\iface eth$eth_index inet static" /etc/network/interfaces
fi
#This will change both eth and wlan entries, however, as only 1 adapater is enabled by this feature, should be fine.
sed -i "/^#address/c\address $FR_net_static_ip" /etc/network/interfaces
sed -i "/^#netmask/c\netmask $FR_net_static_mask" /etc/network/interfaces
sed -i "/^#gateway/c\gateway $FR_net_static_gateway" /etc/network/interfaces
sed -i "/^#dns-nameservers/c\dns-nameservers $FR_net_static_dns" /etc/network/interfaces
fi
# - Restart Networking
if [[ "$DISTRIBUTION" == "wheezy" || "$DISTRIBUTION" == "trusty" ]]; then
service networking restart
else
systemctl daemon-reload
systemctl restart networking
fi
# - Manually bring up adapters (just incase)
if [[ $FR_net_wifi_enabled == 1 ]]; then
ifup wlan$wlan_index
elif [[ $FR_net_ethernet_enabled == 1 ]]; then
ifup eth$eth_index
fi
fi
fi
} #do_firstrun_automated_user_configuration
case "$1" in
start)
# Run a q&d benchmark to be able to identify cards way too slow easily
echo -e "\n### quick iozone test:$(cd /root; iozone -e -I -a -s 1M -r 4k -i 0 -i 1 -i 2 | grep '^ 1024' | sed 's/ 1024 //')" >> $Log
# workaround for AP6212a under mainline kernel
if [ -n "$(lsmod | grep brcmfmac)" ] && [ -z "$(dmesg | grep brcmf | grep Firm)" ]; then
cp /lib/firmware/brcm/brcmfmac43430-sdio.bin /lib/firmware/brcm/brcmfmac43430-sdio.bin.bak
cp /lib/firmware/ap6212/fw_bcm43438a1.bin /lib/firmware/brcm/brcmfmac43430-sdio.bin
rmmod brcmfmac
modprobe brcmfmac
fi
# BT tweaks
case ${BOARD} in
cubietruck)
systemctl enable brcm40183-patch && service brcm40183-patch start
;;
bananapim2plus|nanopiair|nanopim1plus|orangepizeroplus2*)
update-rc.d ap6212-bluetooth defaults
;;
cubox-i)
update-rc.d brcm4330-patch defaults && /etc/init.d/brcm4330-patch start
;;
esac
# update console info
setupcon --save
# SSH Keys creation
rm -f /etc/ssh/ssh_host*
read entropy_before </proc/sys/kernel/random/entropy_avail
dpkg-reconfigure openssh-server >/dev/null 2>&1
service sshd restart
read entropy_after </proc/sys/kernel/random/entropy_avail
echo -e "\n### [firstrun] Recreated SSH keys (entropy: ${entropy_before} ${entropy_after})" >>${Log}
FREESIZE=$(( $(findmnt --target / -n -o AVAIL -b) / 1048576 )) # MiB
SWAPFILE=/var/swap
if [[ ! -f $SWAPFILE && $FREESIZE -gt 132 ]]; then
fallocate -l 128M $SWAPFILE || dd if=/dev/zero of=$SWAPFILE bs=1M count=128 status=noxfer
chown root:root $SWAPFILE
chmod 0600 $SWAPFILE
mkswap $SWAPFILE
swapon $SWAPFILE
grep -q swap /etc/fstab || echo "$SWAPFILE none swap sw 0 0" >> /etc/fstab
grep -q swap /etc/sysctl.conf || echo "vm.swappiness=0" >> /etc/sysctl.conf
echo -e "\n### [firstrun] Created 128MB emergency swap as $SWAPFILE" >>${Log}
fi
# some hardware workarounds
case $LINUXFAMILY in
sun7i|sun8i)
# set some mac address for BT
[[ -n $(lsmod | grep dhd) ]] && \
(MACADDR=$(printf '43:29:B1:%02X:%02X:%02X\n' $[RANDOM%256] $[RANDOM%256] $[RANDOM%256]) ; \
sed -i "s/^MAC_ADDR=.*/MAC_ADDR=${MACADDR}/" /etc/default/brcm40183 ;\
sed -i "s/^MAC_ADDR=.*/MAC_ADDR=${MACADDR}/" /etc/default/ap6212 \
echo -e "\n### [firstrun] Use MAC address ${MACADDR} for Bluetooth from now" >>${Log})
case $BOARD_NAME in
"NanoPi Air"|"NanoPi M1 Plus")
# change serial port for BT on FriendlyELEC H3 boards
sed -i "s/^PORT=.*/PORT=ttyS3/" /etc/default/ap6212
;;
"Orange Pi Lite"|"Orange Pi Zero Plus 2"|"NanoPi Air")
# relink /etc/network/interfaces on Wi-Fi only boards
cd /etc/network/ && ln -sf interfaces.network-manager interfaces
;;
"Orange Pi Zero")
# set OPi Zero WiFi address permanently based on a random address using Allwinner's MAC prefix
MACADDR=$(printf 'DC:44:6D:%02X:%02X:%02X\n' $[RANDOM%256] $[RANDOM%256] $[RANDOM%256])
echo "options xradio_wlan macaddr=${MACADDR}" >/etc/modprobe.d/xradio_wlan.conf
echo -e "\n### [firstrun] Use MAC address ${MACADDR} for Wi-Fi from now" >>${Log}
cd /etc/network/ && ln -sf interfaces.network-manager interfaces
(modprobe -r xradio_wlan && sleep 1 && modprobe xradio_wlan) &
;;
esac
# trigger red or blue LED as user feedback
echo heartbeat >/sys/class/leds/*red*/trigger 2>/dev/null || echo heartbeat >/sys/class/leds/*blue*/trigger 2>/dev/null
;;
pine64)
if [[ -z $(grep ethaddr /boot/armbianEnv.txt) && -f /sys/class/net/eth0/address ]]; then
echo "ethaddr=$(cat /sys/class/net/eth0/address)" >> /boot/armbianEnv.txt
fi
;;
rockchip)
# set MAC for eth0
MACADDR=$(printf 'da:42:ea:%02X:%02X:%02X\n' $[RANDOM%256] $[RANDOM%256] $[RANDOM%256])
sed -i "s/^\#[^ tab]\+hwaddress ether/\thwaddress ether $MACADDR/" /etc/network/interfaces
;;
esac
chage -d 0 root
mv /usr/bin/newaliases.bak /usr/bin/newaliases
export DEBIAN_FRONTEND=noninteractive
apt-get install -f || exit 0
/usr/sbin/omv-initsystem
mv /etc/default/cpufrequtils.bak /etc/default/cpufrequtils
. /etc/default/cpufrequtils
echo -e "OMV_CPUFREQUTILS_GOVERNOR=${GOVERNOR}" >>/etc/default/openmediavault
echo -e "OMV_CPUFREQUTILS_MINSPEED=${MIN_SPEED}" >>/etc/default/openmediavault
echo -e "OMV_CPUFREQUTILS_MAXSPEED=${MAX_SPEED}" >>/etc/default/openmediavault
/usr/sbin/omv-rpc -u admin "perfstats" "set" '{"enable":false}'
/usr/sbin/omv-rpc -u admin "config" "applyChanges" '{ "modules": ["monit","rrdcached","collectd"],"force": true }'
/usr/local/bin/create-raspihealth-log.sh
apt-get purge -f -qq -y armbian-firmware linux-dtb-next-mvebu linux-image-next-mvebu linux-u-boot-clearfogbase-next linux-headers-next-mvebu
wget https://archive.raspberrypi.org/debian/raspberrypi.gpg.key -O - | sudo apt-key add -
apt update
apt-get -f -qq -y install firmware-brcm80211 raspberrypi-bootloader raspberrypi-kernel libraspberrypi-bin python-pip openmediavault-netatalk
pip install -U tzupdate
tzupdate
AFP_Options="mimic model = Macmini"
xmlstarlet ed -L -u "/config/services/afp/extraoptions" -v "$(echo -e "${AFP_Options}")" /etc/openmediavault/config.xml
read TZ </etc/timezone
xmlstarlet ed -L -u "/config/system/time/timezone" -v "${TZ}" /etc/openmediavault/config.xml
xmlstarlet ed -L -u "/config/system/time/ntp/enable" -v "1" /etc/openmediavault/config.xml
xmlstarlet ed -L -u "/config/system/network/dns/hostname" -v "raspberrypi" /etc/openmediavault/config.xml
for i in ntp timezone monit rrdcached collectd netatalk ; do
/usr/sbin/omv-mkconf $i
done
FILE=$(mktemp)
wget https://mirrors.kernel.org/ubuntu/pool/universe/z/zram-config/zram-config_0.5_all.deb -qO $FILE && dpkg -i $FILE
/usr/sbin/omv-update
systemctl disable firstrun
sync && reboot
exit 0
;;
*)
echo "Usage: $0 start"
exit 0
;;
esac