-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathBBB-FlashMMC.sh
executable file
·267 lines (216 loc) · 7.72 KB
/
BBB-FlashMMC.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
#!/bin/bash -e
export PARTSIZE=""
export DOREBOOT="y"
export CLEARFPP="n"
if [ "$1" == "-s" ]; then
PARTSIZE=$2
shift; shift;
fi
if [ "$1" == "-noreboot" ]; then
DOREBOOT="n"
shift;
fi
if [ "$1" == "-clear" ]; then
CLEARFPP="y"
shift;
fi
DEVICE=$2
if [ "x${DEVICE}" == "x" ]; then
DEVICE="/dev/mmcblk1"
fi
cylon_leds () {
if [ -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
BASE=/sys/class/leds/beaglebone\:green\:usr
echo none > ${BASE}0/trigger
echo none > ${BASE}1/trigger
echo none > ${BASE}2/trigger
echo none > ${BASE}3/trigger
STATE=1
while : ; do
case $STATE in
1) echo 255 > ${BASE}0/brightness
echo 0 > ${BASE}1/brightness
STATE=2
;;
2) echo 255 > ${BASE}1/brightness
echo 0 > ${BASE}0/brightness
STATE=3
;;
3) echo 255 > ${BASE}2/brightness
echo 0 > ${BASE}1/brightness
STATE=4
;;
4) echo 255 > ${BASE}3/brightness
echo 0 > ${BASE}2/brightness
STATE=5
;;
5) echo 255 > ${BASE}2/brightness
echo 0 > ${BASE}3/brightness
STATE=6
;;
6) echo 255 > ${BASE}1/brightness
echo 0 > ${BASE}2/brightness
STATE=1
;;
*) echo 255 > ${BASE}0/brightness
echo 0 > ${BASE}1/brightness
STATE=2
;;
esac
sleep 0.1
done
fi
}
prepareBTRFSPartitions() {
echo "---------------------------------------"
echo "Creating partitions"
echo ""
# create partitions
sfdisk --force ${DEVICE} <<-__EOF__
4M,96M,,*
100M,${PARTSIZE},,-
__EOF__
blockdev --rereadpt ${DEVICE} || true
fdisk -l ${DEVICE} || true
echo "---------------------------------------"
echo "Formating partitions"
echo ""
#format partitions
mkfs.ext4 -F -O ^metadata_csum,^64bit ${DEVICE}p1 -L bootfs
mkfs.btrfs -f ${DEVICE}p2 -L rootfs
echo "---------------------------------------"
echo "Creating mount points and mounting rootfs"
echo ""
#mount
mkdir -p /tmp/rootfs
mount -t btrfs -o noatime,nodiratime,compress-force=zstd ${DEVICE}p2 /tmp/rootfs
mkdir -p /tmp/rootfs/boot
mount -t ext4 -o noatime,nodiratime ${DEVICE}p1 /tmp/rootfs/boot
}
prepareEXT4Partitions() {
echo "---------------------------------------"
echo "Creating partitions"
echo ""
# create partitions
sfdisk --force ${DEVICE} <<-__EOF__
4M,${PARTSIZE},,-
__EOF__
blockdev --rereadpt ${DEVICE} || true
fdisk -l ${DEVICE} || true
echo "---------------------------------------"
echo "Formating partitions"
echo ""
#format partitions
mkfs.ext4 -F -O ^metadata_csum,^64bit ${DEVICE}p1 -L rootfs
echo "---------------------------------------"
echo "Creating mount points and mounting rootfs"
echo ""
#mount
mkdir -p /tmp/rootfs
mount -t ext4 -o noatime,nodiratime ${DEVICE}p1 /tmp/rootfs
}
adjustEnvBTRFS() {
cd /opt/source/bb.org-overlays
make clean
make
mkdir -p /tmp/rootfs/boot/lib
mkdir -p /tmp/rootfs/boot/lib/firmware
cp /opt/source/bb.org-overlays/src/arm/* /tmp/rootfs/boot/lib/firmware
echo "" >> /tmp/rootfs/boot/uEnv.txt
echo "mmcrootfstype=btrfs" >> /tmp/rootfs/boot/uEnv.txt
echo "rootfstype=btrfs" >> /tmp/rootfs/boot/uEnv.txt
echo "mmcpart=2" >> /tmp/rootfs/boot/uEnv.txt
echo "" >> /tmp/rootfs/boot/uEnv.txt
echo "${DEVICE}p2 / btrfs noatime,nodiratime,compress=zstd 0 1" > /tmp/rootfs/etc/fstab
echo "${DEVICE}p1 /boot ext4 defaults,noatime,nodiratime 0 2" >> /tmp/rootfs/etc/fstab
}
adjustEnvEXT4() {
echo "${DEVICE}p1 / ext4 defaults,noatime,nodiratime 0 1" > /tmp/rootfs/etc/fstab
}
if [ "$DOREBOOT" == "y" ]; then
cylon_leds & CYLON_PID=$!
fi
set -o pipefail
echo "---------------------------------------"
echo "Installing bootloader "
echo ""
#install bootloader
if [ -f /opt/fpp/bin.bbb/bootloader/install.sh ]; then
/opt/fpp/bin.bbb/bootloader/install.sh
fi
if [ "$1" = "btrfs" ]; then
prepareBTRFSPartitions
else
prepareEXT4Partitions
fi
echo "---------------------------------------"
echo "Copy files rootfs"
echo ""
#copy files
time rsync -aAXxv /ID.txt /bin /boot /dev /etc /home /lib /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var /tmp/rootfs --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/run/* --exclude=/mnt/* --exclude=/media/* --exclude=/lost+found --exclude=/uEnv.txt || true
echo "---------------------------------------"
echo "Configure /boot"
echo ""
#configure /boot
cd /tmp/rootfs/boot
#remove btrfs stuff from uEnv.txt (may be re-added later)
sed -i '/mmcpart=2/d' uEnv.txt
sed -i '/rootfstype=btrfs/d' uEnv.txt
sed -i '/mmcrootfstype=btrfs/d' uEnv.txt
rm -f boot
ln -sf . boot
cd /
if [ "$1" = "btrfs" ]; then
adjustEnvBTRFS
else
adjustEnvEXT4
fi
echo "---------------------------------------"
echo "Configure /etc/fstab"
echo ""
#configure fstab
echo "debugfs /sys/kernel/debug debugfs defaults 0 0" >> /tmp/rootfs/etc/fstab
echo "tmpfs /tmp tmpfs nodev,nosuid,size=50M 0 0" >> /tmp/rootfs/etc/fstab
echo "tmpfs /var/tmp tmpfs nodev,nosuid,size=50M 0 0" >> /tmp/rootfs/etc/fstab
echo "#####################################" >> /tmp/rootfs/etc/fstab
echo "#/dev/sda1 /home/fpp/media auto defaults,noatime,nodiratime,exec,nofail,flush,uid=500,gid=500 0 0" >> /tmp/rootfs/etc/fstab
echo "#####################################" >> /tmp/rootfs/etc/fstab
echo "---------------------------------------"
echo "Cleaning up"
echo ""
rm -rf /tmp/rootfs/etc/ssh/*key*
rm -rf /tmp/rootfs/var/lib/connman/eth*
find /tmp/rootfs/var/log/ -type f -delete
rm -rf /tmp/rootfs/home/fpp/media/logs/*
rm -f /tmp/rootfs/root/.bash_history
rm -f /tmp/rootfs/root/.wget-hsts
echo "uninitialized" > /tmp/rootfs/etc/machine-id
if [ "$CLEARFPP" == "y" ]; then
rm -f /tmp/rootfs/home/fpp/.bash_history
rm -f /tmp/rootfs/home/fpp/media/settings
rm -rf /tmp/rootfs/home/fpp/media/backups/*
rm -rf /tmp/rootfs/home/fpp/media/config/backups
rm -f /tmp/rootfs/home/fpp/media/config/*
rm -f /tmp/rootfs/home/fpp/media/sequences/*
rm -f /tmp/rootfs/home/fpp/media/music/*
rm -f /tmp/rootfs/home/fpp/media/logs/*
rm -f /tmp/rootfs/home/fpp/media/tmp/*
rm -f /tmp/rootfs/home/fpp/media/images/*
rm -f /tmp/rootfs/home/fpp/media/effects/*
rm -f /tmp/rootfs/home/fpp/media/cache/*
rm -f /tmp/rootfs/home/fpp/media/playlists/*
rm -f /tmp/rootfs/home/fpp/media/plugins/*
rm -f /tmp/rootfs/home/fpp/media/scripts/*
rm -f /tmp/rootfs/home/fpp/media/videos/*
rm -f /tmp/rootfs/home/fpp/media/uploads/*
fi
echo "---------------------------------------"
echo "Unmounting filesystems"
echo ""
umount -R /tmp/rootfs
echo "---------------------------------------"
echo "Done flashing eMMC, powering down"
echo ""
if [ "$DOREBOOT" == "y" ]; then
systemctl poweroff || halt
fi