-
Notifications
You must be signed in to change notification settings - Fork 5
/
patch.sh
executable file
·368 lines (286 loc) · 8.95 KB
/
patch.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
#!/bin/bash
#
# patcher
#
set -e
export LANG=C
# Try to naively infer the base directory
PATCHER_DIR="$(dirname $0)"
[ "${PATCHER_DIR}" == "." ] && PATCHER_DIR="$PWD"
# The default BASE_TEMP_DIR is /tmp/sailfish-image-patcher
BASE_TEMP_DIR="/tmp/sailfish-image-patcher"
ARCH="$(uname -m)"
ATRUNCATE_PATH="${PATCHER_DIR}/atruncate.py"
#PATCH_KERNEL="yes"
#REPACK_SCRIPT="${PATCHER_DIR}/droid-config-f5121/kickstart/pack/f5121/hybris"
#PACKAGE="pattern:droid-compat-f5321"
#REPOSITORY_URI="http://repo.merproject.org/obs/home:/eugenio:/compat-f5321/sailfish_latest_armv7hl/"
DISABLE_HOME_ENCRYPTION_AT_BOOT="no"
FORCE_DEFAULTUSER_NEMO="no"
info() {
echo "I: $@"
}
warning() {
echo "W: $@"
}
error() {
echo "E: $@" >&2
exit 1
}
check_application() {
### Checks for the existence of an application
###
### param: $@: the file to check
for application in $@; do
if ! which $application &> /dev/null; then
error "$application is missing. Please install the relative package and retry"
fi
done
}
help() {
cat <<EOF
patcher - patches a Sailfish X image
Arguments:
-a ADAPTATION the adaptation script to use (e.g. f5321 for X Compact)
-b BASE_TEMP_DIR base temp dir, defaults to ${BASE_TEMP_DIR}
-i INPUT_FILE the zipfile to patch
EOF
}
if [ "$UID" != 0 ]; then
warning "You must be root to use this script! Trying with sudo..."
exec sudo $0 $@
fi
# Get options
while getopts "a:b:i:h" option; do
case "${option}" in
a)
# Adaptation
ADAPTATION="${OPTARG}"
;;
b)
# Base temp dir
BASE_TEMP_DIR="${OPTARG}"
;;
i)
# Input file
INPUT_FILE="${OPTARG}"
;;
h)
# Help
help
exit 0
;;
?)
# Someting bad occurred
error "Use -h to show the help message."
;;
esac
done
[ -n "${ADAPTATION}" ] || error "Adaptation is missing. See the help for details"
[ -n "${INPUT_FILE}" ] || error "Input file is missing. See the help for details"
# Source the adaptation file
[ -e "${PATCHER_DIR}/devices/${ADAPTATION}" ] || error "Adaptation source file is missing. Exiting..."
. ${PATCHER_DIR}/devices/${ADAPTATION}
# Load an eventual configuration file
if [ -e "${PATCHER_DIR}/config.sh" ]; then
. ${PATCHER_DIR}/config.sh
fi
if [ "$ARCH" != "armv7l" ] || [ "$ARCH" != "aarch64" ]; then
WITH_QEMU_STATIC="yes"
check_application qemu-arm-static
fi
check_application \
simg2img \
img2simg \
rsync \
pigz \
losetup \
pvcreate \
vgcreate \
e2fsck \
resize2fs
# Let's go!
WORKDIR=${BASE_TEMP_DIR}
cleanup() {
info "Cleaning up..."
for mpoint in "$(mount | awk '{ print $3 }' | grep ${WORKDIR} | sort -r)"; do
if [ -n "${mpoint}" ]; then
info "Umounting ${mpoint}..."
umount ${mpoint}
fi
done
}
trap cleanup EXIT
if [ -e "${WORKDIR}" ]; then
rm -rf "${WORKDIR}"
fi
mkdir -p "${WORKDIR}"
info "Unzipping the Sailfish X archive"
unzip \
${INPUT_FILE} \
-d ${WORKDIR} \
-x */sailfish.img001
cd ${WORKDIR}/Sailfish*
mkdir -p patcher-tmp
mkdir -p patcher-tmp/work
mkdir -p patcher-tmp/work/fimage
mkdir -p patcher-tmp/work/tree
mkdir -p patcher-result
### add f5321 to VALID_PRODUCTS for flash.sh
sed -i 's/"F5121"/"F5121"\n"F5321"/' flash-config.sh
###
FIMAGE_MPOINT="$(mktemp -d -p ${BASE_TEMP_DIR})"
ROOT_MPOINT="$(mktemp -d -p ${BASE_TEMP_DIR})"
# Turn the fimage into a raw image
info "Converting the fimage sparse image to a raw image..."
simg2img fimage.img001 patcher-tmp/fimage.img
rm -f fimage.img001
# Mount the image and copy the contents to fimage
info "Copying the fimage contents"
mount -o loop patcher-tmp/fimage.img ${FIMAGE_MPOINT}
cp -Rav ${FIMAGE_MPOINT}/Sailfish*/*.gz patcher-tmp/work/fimage
umount ${FIMAGE_MPOINT}
rm -f patcher-tmp/fimage.img
info "Uncompressing the contents"
gunzip patcher-tmp/work/fimage/home.img.gz
gunzip patcher-tmp/work/fimage/root.img.gz
# Mount root.img and rsync its contents to work/
info "Copying the Sailfish root contents..."
mount -o loop patcher-tmp/work/fimage/root.img ${ROOT_MPOINT}
rsync --archive -H -A -X ${ROOT_MPOINT}/* patcher-tmp/work/tree
umount ${ROOT_MPOINT}
# Get ROOT_SIZE; this will be useful later...
ROOT_SIZE=$(du -sm patcher-tmp/work/fimage/root.img | awk '{ print $1 }')
rm -f patcher-tmp/work/fimage/root.img
# Copy qemu-arm-static if we should...
if [ "$WITH_QEMU_STATIC" == "yes" ]; then
cp $(which qemu-arm-static) patcher-tmp/work/tree/usr/bin
fi
# Then create the script to execute inside the chroot
cat > patcher-tmp/work/tree/patch.sh <<EOF
#!/bin/bash
# Add repository
ssu ar tmp-compat-$ADAPTATION $REPOSITORY_URI
ssu ur
# Fetch zypper
ZYPPER_PACKAGES="augeas-libs readline zypper"
JOLLA_REPO=\$(ssu lr 2> /dev/null | grep -o "https://releases.jolla.com/releases/[0-9\.]*/jolla/.*")
if [ -z "\$JOLLA_REPO" ]; then
echo "E: unable to get jolla repository"
exit 1
fi
tmpdir=\$(mktemp -d)
# Try to obtain the primary.xml.gz file
potential_primary=\$(curl -L \$JOLLA_REPO/repodata/repomd.xml | grep -oE "repodata/[0-9A-Za-z]+\-primary.xml.gz")
if [ -n \$potential_primary ]; then
curl -L \$JOLLA_REPO/\$potential_primary > \$tmpdir/primary.xml.gz
else
curl -L \$JOLLA_REPO/repodata/primary.xml.gz > \$tmpdir/primary.xml.gz
fi
gunzip \$tmpdir/primary.xml.gz
for pkg in \$ZYPPER_PACKAGES; do
# This is pretty ugly
pkg_path=\$(grep -oE "(core|oss)\/.*\/\$pkg-[0-9\.\-\_]+.*\.rpm" \$tmpdir/primary.xml | grep -v "\/src\/")
curl -L \$JOLLA_REPO/\$pkg_path > \$tmpdir/\$pkg.rpm
done
# This is pretty ugly
#cd /var/cache/zypp/packages
cd \$tmpdir
find . -iname \*.rpm | xargs rpm -ivh
# Refresh
zypper refresh tmp-compat-$ADAPTATION
zypper refresh jolla
# Install the compatibility layer
CACHE_DIR=\$(mktemp -d)
zypper -n --no-refresh --cache-dir \$CACHE_DIR install $PACKAGE
# Disable the temp
ssu dr tmp-compat-$ADAPTATION
ssu rr tmp-compat-$ADAPTATION
ssu er compat-$ADAPTATION
ssu ur
# Obtain a custom kernel image
if [ "$PATCH_KERNEL" == "yes" ]; then
# Divert /usr/sbin/flash-partition as we're not going to flash
# anything
rpm-divert add \
droid-compat-tmp \
/usr/sbin/flash-partition \
/usr/sbin/flash-partition.diverted \
--action symlink \
--replacement /bin/true
rpm-divert apply --package droid-compat-tmp
/var/lib/platform-updates/flash-bootimg.sh
# The result should be in /tmp
# Restore the diversion
rpm-divert unapply --package droid-compat-tmp
rpm-divert remove droid-compat-tmp /usr/sbin/flash-partition
fi
### encryption is mandatory starting in 3.4
if [ "$DISABLE_HOME_ENCRYPTION_AT_BOOT" == "yes" ]; then
rm /var/lib/sailfish-device-encryption/encrypt-home
fi
if [ "$FORCE_DEFAULTUSER_NEMO" == "yes" ]; then
sed -i s/defaultuser/nemo/g /usr/lib/startup/start-autologin
fi
# Terminate what we started
kill -9 \$(ps aux | grep qemu-arm | grep -v "bash\|grep" | awk '{ print \$2 }' | sort -r) &> /dev/null
# Cleanup
rm -f /patch.sh
rm -f /var/run/connman/resolv.conf
rm -f /var/run/messagebus.pid
if [ "$WITH_QEMU_STATIC" == "yes" ]; then
rm -f /usr/bin/qemu-arm-static
fi
# FIXME: leave zypper in for now
echo "DONE!"
EOF
# Prepare the work directory
info "Preparing for the patch..."
mount --bind /dev patcher-tmp/work/tree/dev
mount --bind /dev/pts patcher-tmp/work/tree/dev/pts
mount --bind /sys patcher-tmp/work/tree/sys
mount --bind /proc patcher-tmp/work/tree/proc
cp /etc/resolv.conf patcher-tmp/work/tree/var/run/connman/resolv.conf
# Finally patch
info "Patching the image..."
chroot patcher-tmp/work/tree /bin/bash /patch.sh
# Umount
umount patcher-tmp/work/tree/dev/pts
umount patcher-tmp/work/tree/dev
umount patcher-tmp/work/tree/sys
umount patcher-tmp/work/tree/proc
# The home image is untouched, move from the fimage directory
mv patcher-tmp/work/fimage/home.img patcher-tmp/work
# Copy the files from the $INPUT_DIR to the work directory
cp ./{*.dll,*.exe,flash*,*.img,*.urls,*.bat} patcher-tmp/work
# Copy the kernel if we should
if [ "$PATCH_KERNEL" == "yes" ]; then
mv patcher-tmp/work/tree/tmp/hybris-boot-patched.img patcher-tmp/work/hybris-boot.img
fi
# Cleanup tmp/
rm -rf patcher-tmp/work/tree/tmp/*
# Extract UUIDs. We will use the originals to avoid modifying the fstab.
# The UUIDs are already shared between Sailfish images, so don't actually
# bother to change them.
ROOT_UUID=$(cat patcher-tmp/work/tree/etc/fstab | grep "/ " | awk '{ print $1 }' | cut -d"=" -f2)
FIMAGE_UUID=$(cat patcher-tmp/work/tree/etc/fstab | grep "/fimage " | awk '{ print $1 }' | cut -d"=" -f2)
if [ -z "$FIMAGE_UUID" ]; then
#fimage uuid was removed from fstab in rokua, so just an arbitrary one
FIMAGE_UUID="f6fddb2c-8a37-11ea-96ca-27ee2e012f1d"
fi
# Re-create the root.img
info "Creating an empty root image"
SIZE=$(( ${ROOT_SIZE} + 20 )) # 20MB contingency
dd if=/dev/zero of=patcher-tmp/work/root.img bs=1M count=${SIZE}
mkfs.ext4 -U ${ROOT_UUID} patcher-tmp/work/root.img
# Mount and sync the image contents
mount -o loop patcher-tmp/work/root.img ${ROOT_MPOINT}
info "Syncing back the patched tree"
rsync --archive -H -A -X patcher-tmp/work/tree/* ${ROOT_MPOINT}
sync
umount ${ROOT_MPOINT}
info "Repacking"
repack
# Moving the image
mv patcher-tmp/work/Sailfish*.zip ${PATCHER_DIR}
info "Done! Enjoy!"