forked from DanB0908/RTAndroidNOOBS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.sh
executable file
·527 lines (432 loc) · 14 KB
/
install.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/bin/bash
#
# Copyright (C) 2017 RTAndroid Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Android image installation script for Raspberry Pi 3
# Orignal Author: Igor Kalkov
# Modified By: Dan B.
# https://github.com/RTAndroid/android_vendor_brcm_rpi3_scripts/blob/aosp-7.1/scripts/install.sh
#
DEVICE_LOCATION=""
DEVICE_NAME=""
DEVICE_SIZE=""
DEVICE_SUFFIX=""
PARTITION=false
PARTITION_NEEDED=false
FORMAT=false
SIZE_P1=512 # exact size of the partition 1 (boot) in MB
SIZE_P2=2048 # exact size of the partition 2 (system) in MB
SIZE_P3=512 # exact size of the partition 3 (cache) in MB
SIZE_P4=1024 # minimum size of the partition 4 (userdata) in MB
SIZE_NOOBS=2060 # size required for NOOBS install (boot & system.img) in MB
# ------------------------------------------------
# Helping functions
# ------------------------------------------------
show_help()
{
cat << EOF
USAGE:
$0 [-f] [-h] [-n] [-p] /dev/NAME
OPTIONS:
-f Format userdata and cache
-h Show help
-n Copy to drive in NOOBS format
-p (Re-)partition the sdcard
EOF
}
check_dependency()
{
which $1 > /dev/null
if (($? != 0)); then
echo "ERR: $1 not found. Please install: \"$2\""
exit 1
fi
}
check_device()
{
echo " * Checking access permissions..."
if [ "$(sudo id -u)" != "0" ]; then
echo "ERR: please make sure you are allowed to run 'sudo'!"
exit 1
fi
echo " * Checking the device in $DEVICE_LOCATION..."
if [[ -z "$DEVICE_LOCATION" ]]; then
echo ""
echo "ERR: device location not valid."
exit 1
fi
if [[ ! -b "$DEVICE_LOCATION" ]]; then
echo ""
echo "ERR: no block device was found in $DEVICE_LOCATION!"
exit 1
fi
echo " * Validating the device's size..."
DEVICE_NAME=${DEVICE_LOCATION##*/}
SIZE_FILE="/sys/block/$DEVICE_NAME/size"
DEVICE_SIZE_SECTORS=$(cat $SIZE_FILE)
if [[ ! -f "$SIZE_FILE" ]]; then
echo ""
echo "ERR: can't detect the size of the sdcard!"
fi
REQUIRED_SIZE_MB=$((SIZE_P1 + SIZE_P2 + SIZE_P3 + SIZE_P4))
echo " - minimum size: $REQUIRED_SIZE_MB MB"
# DEVICE_SIZE [Sector] * 512 [Byte/Sector] / 1024 [Byte/KB] / 1024 [KB/MB] = SIZE [MB]
DEVICE_SIZE_MB=$(($DEVICE_SIZE_SECTORS*512/1024/1024))
echo " - detected size: $DEVICE_SIZE_MB MB"
if [[ $DEVICE_SIZE_MB -lt $REQUIRED_SIZE_MB ]]; then
echo ""
echo "ERR: please use an sdcard with more than $SIZE_SD MB."
exit 1
fi
# some card readers mount the sdcard as /dev/mmcblkXp? instead of /dev/sdX?
if [[ $DEVICE_NAME == "mmcblk"* ]]; then
echo " * Using device suffix 'p' (mmcblk device)"
DEVICE_SUFFIX="p"
fi
}
check_partitions()
{
echo " * Listing all available partitions..."
PARTITION_LIST=$(sudo fdisk -l ${DEVICE_LOCATION} | grep "^/dev/")
echo ""
echo "$PARTITION_LIST"
echo ""
PARTITION_COUNT=$(wc -l <<< "${PARTITION_LIST}")
echo " * Detected $PARTITION_COUNT partitions on $DEVICE_LOCATION"
# allow all numbers if we are going to re-partition it anyways
if [ "$PARTITION" = true ]; then
echo " - ignoring this count due to upcoming partitioning"
if [ "$NOOBS_INSTALL" = true ]; then # Modified for NOOBS
PARTITION_COUNT=1
else
PARTITION_COUNT=4
fi
fi
if [ "$NOOBS_INSTALL" = true ]; then # Modified for NOOBS
if [ "${PARTITION_COUNT:-0}" -lt "1" ]; then
echo "ERR: bad device in $DEVICE_LOCATION!"
exit 1
fi
else
if [ "${PARTITION_COUNT:-0}" -ne "4" ]; then
echo "ERR: bad device in $DEVICE_LOCATION!"
exit 1
fi
fi
}
check_sizes()
{
echo " * Validating partition sizes..."
sleep 1
PARTITION1_SIZE_SECTORS=$(cat "/sys/block/${DEVICE_NAME}/${DEVICE_NAME}${DEVICE_SUFFIX}1/size")
if [[ -z "$PARTITION1_SIZE_SECTORS" ]]; then
echo "ERR: can't detect the size of the boot partition!"
exit 1
fi
if [ "$NOOBS_INSTALL" = true ]; then # Modified for NOOBS
PARTITION1_SIZE_MB=$(($PARTITION1_SIZE_SECTORS*512/1024/1024))
if [[ $PARTITION1_SIZE_MB -lt $SIZE_NOOBS ]];
then
echo ""
echo "ERR: the partition doesn't provide enough space!"
exit 1
fi
else
PARTITION1_SIZE_MB=$(($PARTITION1_SIZE_SECTORS*512/1024/1024))
echo " - boot) available: $PARTITION1_SIZE_MB MB, required: $SIZE_P1 MB"
if [[ $PARTITION1_SIZE_MB -lt $SIZE_P1 ]];
then
echo ""
echo "ERR: the 'boot' partition doesn't provide enough space!"
exit 1
fi
PARTITION2_SIZE_SECTORS=$(cat "/sys/block/${DEVICE_NAME}/${DEVICE_NAME}${DEVICE_SUFFIX}2/size")
if [[ -z "$PARTITION2_SIZE_SECTORS" ]]; then
echo "ERR: can't detect the size of the system partition!"
exit 1
fi
PARTITION2_SIZE_MB=$(($PARTITION2_SIZE_SECTORS*512/1024/1024))
echo " - system) available: $PARTITION2_SIZE_MB MB, required: $SIZE_P2 MB"
if [[ $PARTITION2_SIZE_MB -lt $SIZE_P2 ]];
then
echo ""
echo "ERR: the 'system' partition doesn't provide enough space!"
exit 1
fi
PARTITION3_SIZE_SECTORS=$(cat "/sys/block/${DEVICE_NAME}/${DEVICE_NAME}${DEVICE_SUFFIX}3/size")
if [[ -z "$PARTITION3_SIZE_SECTORS" ]]; then
echo "ERR: can't detect the size of the cache partition!"
exit 1
fi
PARTITION3_SIZE_MB=$(($PARTITION3_SIZE_SECTORS*512/1024/1024))
echo " - cache) available: $PARTITION3_SIZE_MB MB, required: $SIZE_P3 MB"
if [[ $PARTITION3_SIZE_MB -lt $SIZE_P3 ]];
then
echo ""
echo "ERR: the 'cache' partition doesn't provide enough space!"
exit 1
fi
PARTITION4_SIZE_SECTORS=$(cat "/sys/block/${DEVICE_NAME}/${DEVICE_NAME}${DEVICE_SUFFIX}4/size")
if [[ -z "$PARTITION4_SIZE_SECTORS" ]]; then
echo "ERR: can't detect the size of the data partition!"
exit 1
fi
PARTITION4_SIZE_MB=$(($PARTITION4_SIZE_SECTORS*512/1024/1024))
echo " - data) available: $PARTITION4_SIZE_MB MB, required: $SIZE_P4 MB"
if [[ $PARTITION4_SIZE_MB -lt $SIZE_P4 ]];
then
echo ""
echo "ERR: the 'data' partition doesn't provide enough space!"
exit 1
fi
fi
}
wait_for_device()
{
sleep 1
sudo partprobe $DEVICE_LOCATION
}
create_partitions()
{
local TEST=0
# no partitioning was requested
if [ "$PARTITION" = false ]; then
echo " * Skipping partitioning..."
return
fi
echo " * Destroying old partition table..."
wait_for_device
sudo dd if=/dev/zero of=$DEVICE_LOCATION bs=1024 count=1 conv=notrunc > /dev/null 2>&1
((TEST+=$?))
echo " * Create a new partition table..."
wait_for_device
printf "o\nw\n" | sudo fdisk $DEVICE_LOCATION > /dev/null 2>&1
((TEST+=$?))
if [[ $TEST -gt 0 ]]; then
echo "ERR: failed to recreate the partition table!"
exit 1
fi
# re-read the partition table
wait_for_device
echo " * Start partitioning..."
if [ "$NOOBS_INSTALL" = true ]; then # Modified for NOOBS
# 1. partition -> NOOBS
echo ""
echo " - creating 'boot'"
printf "n\np\n1\n\n\nt\nb\nw\n" | sudo fdisk $DEVICE_LOCATION
wait_for_device
else
# 1. partition -> boot
echo ""
echo " - creating 'boot'"
printf "n\np\n1\n\n+${SIZE_P1}M\nw\n" | sudo fdisk $DEVICE_LOCATION
wait_for_device
# 2. partition -> system
echo ""
echo " - creating 'system'"
printf "n\np\n2\n\n+${SIZE_P2}M\nw\n" | sudo fdisk $DEVICE_LOCATION
wait_for_device
# 3. partition -> cache
echo ""
echo " - creating 'cache'"
printf "n\np\n3\n\n+${SIZE_P3}M\nw\n" | sudo fdisk $DEVICE_LOCATION
wait_for_device
# 4. partition -> userdata
echo ""
echo " - creating 'userdata'"
printf "n\np\n\n\nw\n" | sudo fdisk $DEVICE_LOCATION
wait_for_device
# 5. set the partition type to "W95 FAT32 (LBA)"
echo ""
echo " - setting correct partition type"
printf "t\n1\nc\nw\n" | sudo fdisk $DEVICE_LOCATION
wait_for_device
# 6. set the first partition as bootable
echo ""
echo " - setting bootable flag"
printf "a\n1\nw\n" | sudo fdisk $DEVICE_LOCATION
wait_for_device
fi
echo ""
echo " * Printing the new partition table..."
printf "p\nq\n" | sudo fdisk $DEVICE_LOCATION
echo ""
}
unmount_all()
{
echo " * Unmounting mounted partitions..."
sync
if [ "$NOOBS_INSTALL" = true ]; then # Modified for NOOBS
sudo umount -l ${DEVICE_LOCATION}${DEVICE_SUFFIX}1 > /dev/null 2>&1
else
sudo umount -l ${DEVICE_LOCATION}${DEVICE_SUFFIX}1 > /dev/null 2>&1
sudo umount -l ${DEVICE_LOCATION}${DEVICE_SUFFIX}2 > /dev/null 2>&1
sudo umount -l ${DEVICE_LOCATION}${DEVICE_SUFFIX}3 > /dev/null 2>&1
sudo umount -l ${DEVICE_LOCATION}${DEVICE_SUFFIX}4 > /dev/null 2>&1
fi
}
format_data()
{
# no partitioning was requested
if [ "$FORMAT" = false ]; then
echo " * Skipping data format..."
return
fi
echo " * Formatting data partitions..."
local TEST=0
echo " - formatting 'cache'"
echo ""
sudo mkfs.ext4 -F -L cache ${DEVICE_LOCATION}${DEVICE_SUFFIX}3
((TEST+=$?))
echo " - formatting 'userdata'"
echo ""
sudo mkfs.ext4 -F -L userdata ${DEVICE_LOCATION}${DEVICE_SUFFIX}4
((TEST+=$?))
if [[ $TEST -gt 0 ]]; then
echo "ERR: an error occured while formatting data partitions."
exit 1
fi
}
format_system()
{
echo " * Formatting system partitions..."
local TEST=0
if [ "$NOOBS_INSTALL" = true ]; then # Modified for NOOBS
# no partitioning was requested
if [ "$FORMAT" = false ]; then
echo " * Skipping NOOBS format..."
return
fi
echo " * Formatting NOOBS partition..."
local TEST=0
echo ""
sudo mkfs.vfat -n boot -F 32 ${DEVICE_LOCATION}${DEVICE_SUFFIX}1
((TEST+=$?))
else
echo " * Formatting system partitions..."
local TEST=0
echo " - formatting 'boot'"
echo ""
sudo mkfs.vfat -n boot -F 32 ${DEVICE_LOCATION}${DEVICE_SUFFIX}1
((TEST+=$?))
echo ""
echo " - formatting 'system'"
echo ""
sudo mkfs.ext4 -F -L system ${DEVICE_LOCATION}${DEVICE_SUFFIX}2
((TEST+=$?))
fi
if [[ $TEST -gt 0 ]]; then
echo "ERR: an error occured while formatting system partitions."
exit 1
fi
}
copy_files()
{
echo " * Copying new system files..."
DIR_NAME="/media/rpi-sd"
BOOT_DIR="./boot"
if [ ! -d $BOOT_DIR ]; then
echo "ERR: boot directory not found!"
exit 1
fi
SYSTEM_IMG="system.img"
if [ ! -f $SYSTEM_IMG ]; then
echo "ERR: system image not found!"
exit 1
fi
NOOBS_DIR="./noobs"
if [ ! -d $NOOBS_DIR ]; then
echo "ERR: noobs directory not found!"
exit 1
fi
echo " - mounting the boot partition to $DIR_NAME"
sudo rm -rf $DIR_NAME > /dev/null 2>&1
sudo mkdir -p $DIR_NAME
sudo mount -t vfat -o rw ${DEVICE_LOCATION}${DEVICE_SUFFIX}1 $DIR_NAME
if [ "$NOOBS_INSTALL" = true ]; then # Modified for NOOBS
echo " - creating NOOBS install tar"
sudo mkdir -p $DIR_NAME/os/Android
sudo tar -cvpf $DIR_NAME/os/Android/boot.tar ./$BOOT_DIR/*
echo " - copying the NOOBS files"
sudo cp -fr $NOOBS_DIR/* $DIR_NAME/os/Android/
echo " - copying the system.img"
sudo cp -r $SYSTEM_IMG $DIR_NAME/os/Android/
echo " - unmounting the NOOBS partition"
sync
sudo umount -l $DIR_NAME
sudo rm -rf $DIR_NAME
else
echo " - copying boot files"
sudo cp -fr $BOOT_DIR/* $DIR_NAME/
echo " - unmounting the boot partition"
sync
sudo umount -l $DIR_NAME
sudo rm -rf $DIR_NAME
echo " - writing the system image"
sudo dd if=$SYSTEM_IMG of=${DEVICE_LOCATION}${DEVICE_SUFFIX}2 bs=1M oflag=direct
fi
}
# --------------------------------------
# Script entry point
# --------------------------------------
# save the passed options
while getopts ":fhnp" flag; do
case $flag in
"h") SHOW_HELP=true ;;
"n") NOOBS_INSTALL=true;;
"p") PARTITION=true ;;
"f") FORMAT=true ;;
*)
echo ""
echo "ERR: invalid option (-$flag $OPTARG)"
echo ""
show_help
exit 1
esac
done
# don't do anything else
if [[ "$SHOW_HELP" = true ]]; then
show_help
exit 1
fi
# what left after the parameters has to be the device
shift $(($OPTIND - 1))
DEVICE_LOCATION="$1"
# no target provided
if [[ -z "$DEVICE_LOCATION" ]]; then
echo ""
echo "ERR: missing the path to the sdcard!"
echo ""
show_help
exit 1
fi
echo "Installation script for RPi started."
echo "Target device: $DEVICE_LOCATION"
echo "Perform partitioning: $PARTITION"
echo "Perform formatting: $FORMAT"
echo ""
check_dependency partprobe parted
check_device
unmount_all
check_partitions
create_partitions
check_sizes
unmount_all
format_data
format_system
copy_files
echo ""
echo "Installation successful. You can now put your sdcard in the RPi."