blob: 20f67d5ad2c3e8dd7534444c6d911f8a03303973 (
plain)
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
|
#!/bin/bash
# ========================================================================== #
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2023 Maxim Devaev <[email protected]> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #
set -ex
if [ "$(whoami)" != root ]; then
echo "Only root can do that"
exit 1
fi
if [ "$1" != --do-the-thing ]; then
echo "This script will make some firstboot magic. Don't run it manually."
exit 1
fi
# ========== Preparing ==========
if [ ! -f /boot/pikvm.txt ]; then
exit 0
fi
# shellcheck disable=SC1090
source <(dos2unix < /boot/pikvm.txt)
rw
# ========== First boot configuration ==========
if [ -n "$FIRSTBOOT$FIRST_BOOT" ]; then
### stop regular kvmd-oled service and show first time setup status in oled
systemctl stop kvmd-oled
/usr/bin/kvmd-oled --interval=0 --text="First time setup\nDo NOT interrupt\nPlease Wait...\n"
( \
(umount /etc/machine-id || true) \
&& echo -n > /etc/machine-id \
&& systemd-machine-id-setup \
) || true
rm -f /etc/ssh/ssh_host_*
ssh-keygen -v -A
rm -f /etc/kvmd/nginx/ssl/*
rm -f /etc/kvmd/vnc/ssl/*
kvmd-gencert --do-the-thing
kvmd-gencert --do-the-thing --vnc
if grep -q 'X-kvmd\.otgmsd' /etc/fstab; then
part=$(grep 'X-kvmd\.otgmsd' /etc/fstab | awk '{print $1}')
# shellcheck disable=SC2206
splitted=(${part//=/ })
if [ "${splitted[0]}" == LABEL ]; then
label=${splitted[1]}
part=$(blkid -c /dev/null -L "$label")
else
label=PIMSD
fi
unset splitted
disk=/dev/$(lsblk -no pkname "$part")
npart=$(cat "/sys/class/block/${part//\/dev\//}/partition")
umount "$part"
parted "$disk" -a optimal -s resizepart "$npart" 100%
yes | mkfs.ext4 -L "$label" -F -m 0 "$part"
mount "$part"
unset disk part npart label
fi
# fc-cache is required for installed X server
# shellcheck disable=SC2015
which fc-cache && fc-cache || true
fi
# ========== OTG serial ==========
if [ -n "$ENABLE_OTG_SERIAL" ]; then
cat <<end_of_file > /etc/kvmd/override.d/0000-vendor-otg-serial.yaml
# Generated by kvmd-bootconfig. Do not edit this file!
otg:
devices:
serial:
enabled: true
end_of_file
grep '^ttyGS0$' /etc/securetty || echo ttyGS0 >> /etc/securetty
mkdir -p /etc/systemd/system/[email protected]
cat <<end_of_file > /etc/systemd/system/[email protected]/override.conf
[Service]
TTYReset=no
TTYVHangup=no
TTYVTDisallocate=no
end_of_file
systemctl enable [email protected]
touch /boot/pikvm-reboot.txt
fi
# ========== SSH ==========
if [ -n "$SSH_PORT" ]; then
sed -i -e "s/^\s*#*\s*Port\s\+.*$/Port $SSH_PORT/g" /etc/ssh/sshd_config
fi
# ========== Ethernet ==========
# If the ETH_DHCP is defined, configure eth0 for DHCP
if [ -n "$ETH_DHCP" ]; then
ETH_IFACE="${ETH_IFACE:-eth0}"
cat <<end_of_file > "/etc/systemd/network/$ETH_IFACE.network"
[Match]
Name=$ETH_IFACE
[Network]
DHCP=yes
DNSSEC=no
[DHCP]
# Use same IP by forcing to use MAC address for clientID
ClientIdentifier=mac
# https://github.com/pikvm/pikvm/issues/583
RouteMetric=10
end_of_file
fi
# If the ETH_ADDR is defined, configure a static address on eth0
if [ -n "$ETH_ADDR" ]; then
ETH_IFACE="${ETH_IFACE:-eth0}"
cat <<end_of_file > "/etc/systemd/network/$ETH_IFACE.network"
[Match]
Name=$ETH_IFACE
[Network]
Address=$ETH_ADDR
DNS=$ETH_DNS
DNSSEC=no
[Route]
Gateway=$ETH_GW
end_of_file
fi
# ========== Wi-Fi ==========
# Set the regulatory domain for wifi, if defined.
if [ -n "$WIFI_REGDOM" ]; then
sed -i \
-e 's/^\(WIRELESS_REGDOM=.*\)$/#\1/' \
-e 's/^#\(WIRELESS_REGDOM="'"$WIFI_REGDOM"'"\)/\1/' \
/etc/conf.d/wireless-regdom
fi
# If the WIFI_ESSID is defined, configure wlan0
if [ -n "$WIFI_ESSID" ]; then
WIFI_IFACE="${WIFI_IFACE:-wlan0}"
cat <<end_of_file > "/etc/systemd/network/$WIFI_IFACE.network"
[Match]
Name=$WIFI_IFACE
[Network]
DHCP=yes
DNSSEC=no
# Use same IP by forcing to use MAC address for clientID
[DHCP]
ClientIdentifier=mac
end_of_file
wpa_passphrase "$WIFI_ESSID" "$WIFI_PASSWD" > "/etc/wpa_supplicant/wpa_supplicant-$WIFI_IFACE.conf"
systemctl enable "wpa_supplicant@$WIFI_IFACE.service" || true
touch /boot/pikvm-reboot.txt
fi
# ========== Custom scripts ==========
if [ -d /boot/pikvm-scripts.d ]; then
run-parts --regex='^.+$' /boot/pikvm-scripts.d || true
fi
# ========== Finish ==========
rm -f /boot/pikvm.txt
if [ -f /boot/pikvm-reboot.txt ]; then
rm -f /boot/pikvm-reboot.txt
ro
echo "kvmd-bootconfig: Reboot after 5 seconds" | tee /dev/kmsg
sleep 2
reboot
sleep 3
else
ro
# critical tasks have completed so start kvmd-oled service as an indicator that first boot tasks are complete
systemctl restart kvmd-oled
fi
|