/
setup-teslausb
266 lines (206 loc) · 6.5 KB
/
setup-teslausb
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
#!/bin/bash -eu
REPO=${REPO:-cimryan}
BRANCH=${BRANCH:-master}
HEADLESS_SETUP=${HEADLESS_SETUP:-false}
USE_LED_FOR_SETUP_PROGRESS=true
CONFIGURE_ARCHIVING=${CONFIGURE_ARCHIVING:-true}
UPGRADE_PACKAGES=${UPGRADE_PACKAGES:-true}
TESLAUSB_HOSTNAME=${TESLAUSB_HOSTNAME:-teslausb}
export campercent=${campercent:-90}
function setup_progress () {
local setup_logfile=/boot/teslausb-headless-setup.log
if [ $HEADLESS_SETUP = "true" ]
then
echo "$( date ) : $1" >> "$setup_logfile"
fi
echo $1
}
if ! [ $(id -u) = 0 ]
then
setup_progress "STOP: Run sudo -i."
exit 1
fi
function headless_setup_populate_variables () {
# Pull in the conf file variables to make avail to this script and subscripts
if [ -e /boot/teslausb_setup_variables.conf ] && [ $HEADLESS_SETUP = "true" ]
then
source /boot/teslausb_setup_variables.conf
fi
}
function headless_setup_mark_setup_failed () {
if [ $HEADLESS_SETUP = "true" ]
then
setup_progress "ERROR: Setup Failed."
touch /boot/TESLAUSB_SETUP_FAILED
fi
}
function headless_setup_mark_setup_success () {
if [ $HEADLESS_SETUP = "true" ]
then
if [ -e /boot/TESLAUSB_SETUP_FAILED ]
then
rm /boot/TESLAUSB_SETUP_FAILED
fi
rm /boot/TESLAUSB_SETUP_STARTED
touch /boot/TESLAUSB_SETUP_FINISHED
# This sed shouldn't be needed, but double checking just to be sure.
sed -i'.bak' -e "s/TEMPARCHIVESERVER/$archiveserver/g" /etc/rc.local
setup_progress "Main setup completed. Remounting file systems read only."
fi
}
function headless_setup_progress_flash () {
if [ $USE_LED_FOR_SETUP_PROGRESS = "true" ] && [ $HEADLESS_SETUP = "true" ]
then
/etc/stage_flash $1
fi
}
function setup_led_off () {
if [ $USE_LED_FOR_SETUP_PROGRESS = "true" ] && [ $HEADLESS_SETUP = "true" ]
then
echo "none" | sudo tee /sys/class/leds/led0/trigger > /dev/null
echo 1 | sudo tee /sys/class/leds/led0/brightness > /dev/null
fi
}
function setup_led_on () {
if [ $USE_LED_FOR_SETUP_PROGRESS = "true" ] && [ $HEADLESS_SETUP = "true" ]
then
echo 0 | sudo tee /sys/class/leds/led0/brightness > /dev/null
fi
}
function check_variable () {
local var_name="$1"
if [ -z "${!var_name+x}" ]
then
setup_progress "STOP: Define the variable $var_name like this: export $var_name=value"
exit 1
fi
}
function check_available_space () {
setup_progress "Verifying that there is sufficient space available on the MicroSD card..."
local available_space="$( parted -m /dev/mmcblk0 u b print free | tail -1 | cut -d ":" -f 4 | sed 's/B//g' )"
if [ "$available_space" -lt 4294967296 ]
then
setup_progress "STOP: The MicroSD card is too small."
exit 1
fi
setup_progress "There is sufficient space available."
}
function verify_configuration () {
get_script /tmp verify-configuration.sh setup/pi
/tmp/verify-configuration.sh
}
function get_script () {
local local_path="$1"
local name="$2"
local remote_path="${3:-}"
curl --fail -o "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name"
# wget -O "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name"
chmod +x "$local_path/$name"
setup_progress "Downloaded $local_path/$name ..."
}
function get_ancillary_setup_scripts () {
get_script /tmp create-backingfiles-partition.sh setup/pi
get_script /tmp create-backingfiles.sh setup/pi
get_script /tmp make-root-fs-readonly.sh setup/pi
get_script /root configure.sh setup/pi
}
function fix_cmdline_txt_modules_load ()
{
setup_progress "Fixing the modules-load parameter in /boot/cmdline.txt..."
cp /boot/cmdline.txt ~
cat ~/cmdline.txt | sed 's/ modules-load=dwc2,g_ether/ modules-load=dwc2/' > /boot/cmdline.txt
rm ~/cmdline.txt
setup_progress "Fixed cmdline.txt."
}
BACKINGFILES_MOUNTPOINT=/backingfiles
MUTABLE_MOUNTPOINT=/mutable
function create_usb_drive_backing_files () {
if [ ! -e "$BACKINGFILES_MOUNTPOINT" ]
then
mkdir "$BACKINGFILES_MOUNTPOINT"
fi
if [ ! -e "$MUTABLE_MOUNTPOINT" ]
then
mkdir "$MUTABLE_MOUNTPOINT"
fi
if [ ! -e /dev/mmcblk0p3 ]
then
setup_progress "Starting to create backing files partition..."
/tmp/create-backingfiles-partition.sh "$BACKINGFILES_MOUNTPOINT" "$MUTABLE_MOUNTPOINT"
fi
if ! findmnt --mountpoint $BACKINGFILES_MOUNTPOINT
then
setup_progress "Mounting the partition for the backing files..."
mount $BACKINGFILES_MOUNTPOINT
setup_progress "Mounted the partition for the backing files."
fi
if [ ! -e $BACKINGFILES_MOUNTPOINT/*.bin ]
then
setup_progress "Creating backing disk files."
/tmp/create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT"
fi
}
function configure_hostname () {
# Headless image already has hostname set
if [ ! $HEADLESS_SETUP = "true" ]
then
setup_progress "Configuring the hostname..."
local new_host_name="$TESLAUSB_HOSTNAME"
cp /etc/hosts ~
sed "s/raspberrypi/$new_host_name/g" ~/hosts > /etc/hosts
rm ~/hosts
cp /etc/hostname ~
sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname
setup_progress "Configured the hostname."
rm ~/hostname
fi
}
function make_root_fs_readonly () {
/tmp/make-root-fs-readonly.sh
}
function update_package_index () {
setup_progress "Updating package index files..."
apt-get update
}
function upgrade_packages () {
if [ "$UPGRADE_PACKAGES" = true ]
then
setup_progress "Upgrading installed packages..."
apt-get --assume-yes upgrade
else
setup_progress "Skipping package upgrade."
fi
}
export -f setup_progress
export HEADLESS_SETUP
update_package_index
headless_setup_populate_variables
# If USE_LED_FOR_SETUP_PROGRESS = true.
setup_led_off
# Flash for stage 2 headless (verify requested configuration)
headless_setup_progress_flash 1
setup_progress "Verifying that the requested configuration is valid..."
verify_configuration
# Flash for Stage 3 headless (grab scripts)
headless_setup_progress_flash 2
setup_progress "Downloading additional setup scripts."
get_ancillary_setup_scripts
pushd ~
fix_cmdline_txt_modules_load
echo "" >> /etc/fstab
# Flash for stage 4 headless (Create backing files)
headless_setup_progress_flash 3
create_usb_drive_backing_files
configure_hostname
# Flash for stage 5 headless (Mark success, FS readonly)
headless_setup_progress_flash 4
headless_setup_mark_setup_success
if [ "$CONFIGURE_ARCHIVING" = true ]
then
/root/configure.sh
fi
make_root_fs_readonly
upgrade_packages
# If USE_LED_FOR_SETUP_PROGRESS = true.
setup_led_on
setup_progress "All done."