forked from GrapheneOS/adevtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mountimages.bash
executable file
·100 lines (81 loc) · 2.07 KB
/
mountimages.bash
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
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
cd "$(dirname "$0")"
[[ $# -eq 2 ]] || exit 1
DEVICE=$1
BUILD=$2
MOUNT_ROOT="mount"
DL_ROOT="dl"
blob_partitions=(system product system_ext vendor)
# Prints an info message in light green
function msg() {
echo -e "\e[1;32m$*\e[0m"
}
# Prints an error in light red
function err() {
echo -e "\e[1;31m$*\e[0m"
}
# Prints a warning in light yellow
function warn() {
echo -e "\e[1;33m$*\e[0m"
}
function kill_jobs() {
jobs -p | xargs -I{} kill -- {}
}
function pushd() {
command pushd "$@" > /dev/null
}
function popd() {
command popd "$@" > /dev/null
}
trap kill_jobs EXIT
function download_factory() {
local device="$1"
local build_id="$2"
local dl_dir="$3"
echo " - Factory $BUILD image for $DEVICE"
../android-prepare-vendor/scripts/download-nexus-image.sh -y -d "$device" -b "$build_id" -o "$dl_dir"
}
(
msg "Downloading $DEVICE $BUILD images"
dl_dir="$DL_ROOT"
mkdir -p "$dl_dir"
if [[ "$(ls "$dl_dir" | grep -e "$DEVICE-$BUILD" | wc -l)" -ge 1 ]]; then
echo "Skipping download, already present"
else
download_factory "$DEVICE" "$BUILD" "$dl_dir"
fi
) &
wait
dl_dir="$DL_ROOT"
msg "Extracting images for $DEVICE $BUILD"
dev_mount="$MOUNT_ROOT"
# Extract zip (and delete if necessary)
if [[ ! -d "$dl_dir/$DEVICE-$BUILD" ]]; then
unzip -d "$dl_dir" "$dl_dir/$DEVICE-"*.zip
fi
pushd "$dl_dir/$DEVICE-"*/
build_parts=("${blob_partitions[@]}")
for part in "${build_parts[@]}"
do
msg " - $part"
if [[ ! -f "$part.img" ]]; then
unzip "image-$DEVICE"*.zip "$part.img"
fi
if [[ ! -f "$part.img.raw" ]]; then
if file "$part.img" | grep -q 'Android sparse image'; then
simg2img "$part.img" "$part.img.raw"
else
ln "$part.img" "$part.img.raw"
fi
fi
mountpoint="$dev_mount/$part"
sudo mkdir -p "$mountpoint"
# Unmount if already mounted
if mountpoint -q "$mountpoint"; then
sudo umount "$mountpoint"
fi
sudo mount -o ro "$part.img.raw" "$mountpoint"
done
popd