-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbbb_upgrader.sh
executable file
·334 lines (242 loc) · 6.16 KB
/
bbb_upgrader.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
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
#!/bin/sh
MOUNT_DIR=/mnt/upgrade
FLAGS_PARTITION=/dev/mmcblk0p5
if [ "x${1}" = "x" ]; then
echo "Usage: ${0} <full-path-to-new-image-file>"
exit 1
fi
if [ ! -f "${1}" ]; then
echo "Image file not found: ${1}"
exit 1
fi
echo -e -n "Checking for an eMMC : "
ls /dev/mmc* | grep -q mmcblk0
if [ $? -eq 1 ]; then
echo "FAIL"
echo "No /dev/mmcblk0. Can't handle this."
exit 1
fi
echo "OK"
echo -e -n "Checking that there is no SD card : "
ls /dev/mmc* | grep -q mmcblk1
if [ $? -eq 0 ]; then
echo "FAIL"
echo "An SD card is present. Please remove and try again."
exit 1
fi
echo "OK"
echo -e -n "Finding the current root partition : "
cat /proc/cmdline | grep -q mmcblk0p2
if [ $? -eq 0 ]; then
CURRENT_ROOT=/dev/mmcblk0p2
else
cat /proc/cmdline | grep -q mmcblk0p3
if [ $? -eq 0 ]; then
CURRENT_ROOT=/dev/mmcblk0p3
else
echo "FAIL"
echo "Current root device is not mmcblk0p2 or mmcblk0p3"
exit 1
fi
fi
echo "${CURRENT_ROOT}"
if [ "${CURRENT_ROOT}" == "/dev/mmcblk0p2" ]; then
NEW_ROOT=/dev/mmcblk0p3
else
NEW_ROOT=/dev/mmcblk0p2
fi
echo "The new root will be : ${NEW_ROOT}"
echo -e -n "Checking the new root partition size : "
SECTORS=`fdisk -l /dev/mmcblk0 | grep $NEW_ROOT | awk '{ print $4 }'`
# since it's more work to parse the Size units, use Sectors
# 2097152 sectors * 512 bytes/sector = 1GB
if [ ${SECTORS} -lt 2000000 ]; then
echo "FAIL"
echo "The new root partition [ ${NEW_ROOT} ] is too small, at least 1GB is required."
echo ""
echo "Here is the current partitioning of [ /dev/mmcblk0 ]"
echo ""
fdisk -l /dev/mmcblk0
exit 1
fi
echo "OK"
echo -e -n "Checking for a ${FLAGS_PARTITION} partition : "
fdisk -l /dev/mmcblk0 | grep -q ${FLAGS_PARTITION}
if [ $? -eq 1 ]; then
echo "FAIL"
echo "There is no ${FLAGS_PARTITION} partition"
exit 1
fi
echo "OK"
echo -e -n "Checking the ${FLAGS_PARTITION} flag partition size : "
SECTORS=`fdisk -l /dev/mmcblk0 | grep ${FLAGS_PARTITION} | awk '{ print $4 }'`
if [ $SECTORS -ne 131072 ]; then
echo "FAIL"
echo "The size of the flag partition ${FLAGS_PARTITION} is unexpected."
echo ""
echo "Here is the current partitioning of [ /dev/mmcblk0 ]"
echo ""
fdisk -l /dev/mmcblk0
exit 1
fi
echo "OK"
echo -e -n "Check that ${FLAGS_PARTITION} is not in use : "
mount | grep -q ${FLAGS_PARTITION}
if [ $? -eq 0 ]; then
echo "FAIL"
echo "${FLAGS_PARTITION} is already mounted"
exit 1
fi
echo "OK"
echo -e -n "Checking if ${MOUNT_DIR} mount point exists : "
if [ ! -d ${MOUNT_DIR} ]; then
echo "NO"
echo -e -n "Attempting to create mount point ${MOUNT_DIR} :"
mkdir ${MOUNT_DIR}
if [ $? -eq 1 ]; then
echo "FAIL"
exit 1
else
echo "OK"
fi
else
echo "OK"
echo -e -n "Checking that ${MOUNT_DIR} is not in use : "
mount | grep -q ${MOUNT_DIR}
if [ $? -eq 0 ]; then
echo "FAIL"
echo "${MOUNT_DIR} is in use by another mounted filesystem"
exit 1
fi
echo "OK"
fi
echo -e -n "Formatting partition ${NEW_ROOT} as ext4 : "
mkfs.ext4 -q ${NEW_ROOT}
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error formatting the new root partition [ ${NEW_ROOT} ]"
exit 1
fi
echo "OK"
echo -e -n "Mounting ${NEW_ROOT} on ${MOUNT_DIR} : "
mount ${NEW_ROOT} ${MOUNT_DIR}
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error mounting partition ${NEW_ROOT} on ${MOUNT_DIR}"
exit 1
fi
echo "OK"
echo -e -n "Extracting new root filesystem ${1} to ${MOUNT_DIR} : "
tar -C ${MOUNT_DIR} -xJf ${1}
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error extracting the root filesystem"
umount ${NEW_ROOT}
exit 1
fi
echo "OK"
echo -e -n "Copying config files from current system : "
cp /etc/fstab ${MOUNT_DIR}/etc/fstab
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error copying /etc/fstab to new system"
umount ${NEW_ROOT}
exit 1
fi
cp /etc/hostname ${MOUNT_DIR}/etc/hostname
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error copying /etc/hostname to new system"
umount ${NEW_ROOT}
exit 1
fi
if [ ! -d ${MOUNT_DIR}/data ]; then
mkdir ${MOUNT_DIR}/data
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error creating /data directory on new system"
umount ${NEW_ROOT}
exit 1
fi
fi
mkdir ${MOUNT_DIR}/mnt/upgrade
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error creating /mnt/upgrade directory on new system"
umount ${NEW_ROOT}
exit 1
fi
mkdir ${MOUNT_DIR}/bootflags
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error creating /mnt/bootflags directory on new system"
umount ${NEW_ROOT}
exit 1
fi
echo "OK"
echo -e -n "Unmounting ${NEW_ROOT} : "
umount ${NEW_ROOT}
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Error unmounting ${NEW_ROOT}"
exit 1
fi
echo "OK"
echo -e -n "Mounting the flag partition ${FLAGS_PARTITION} on ${MOUNT_DIR} : "
mount ${FLAGS_PARTITION} ${MOUNT_DIR}
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Failed to mount ${FLAGS_PARTITION}";
exit 1
fi
echo "OK"
if [ "${NEW_ROOT}" == "/dev/mmcblk0p3" ]; then
echo -e -n "Creating file ${MOUNT_DIR}/three' : "
touch ${MOUNT_DIR}/three
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Failed to create flag file /three"
exit 1
fi
echo "OK"
# might not exist since p2 is the default
if [ -f ${MOUNT_DIR}/two ]; then
# not necessary to delete /two since /three is checked first
# so this could fail and it would be okay, but it still shouldn't
echo -e -n "Deleting file ${MOUNT_DIR}/two : "
rm ${MOUNT_DIR}/two
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Failed to delete old flag file ${MOUNT_DIR}/two"
exit 1
fi
echo "OK"
fi
else
echo -e -n "Creating file ${MOUNT_DIR}/two : "
touch ${MOUNT_DIR}/two
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Failed to create flag file /two"
exit 1
fi
echo "OK"
echo -e -n "Deleting file ${MOUNT_DIR}/three : "
rm ${MOUNT_DIR}/three
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Failed to delete old flag file ${MOUNT_DIR}/three"
exit 1
fi
echo "OK"
fi
echo -e -n "Unmounting ${FLAGS_PARTITION} from ${MOUNT_DIR} : "
umount ${FLAGS_PARTITION}
if [ $? -ne 0 ]; then
echo "FAIL"
echo "Fail to unmount ${FLAGS_PARTITION}"
exit 1
fi
echo "OK"
echo -e "\nA new system was installed onto : ${NEW_ROOT}"
echo -e "\nReboot to use the new system."