-
Notifications
You must be signed in to change notification settings - Fork 1
/
init
338 lines (283 loc) · 9.43 KB
/
init
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
#!/bin/sh
# Copyright (C) 2020 UBports Foundation
#
# jumpercable is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# jumpercable is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with jumpercable. If not, see <http://www.gnu.org/licenses/>.
sync_dirs() {
base=$1
source=$2
target=$3
OLD_PWD=$PWD
cd $base
for file in $source/*; do
# Skip empty directories
[ ! -e "$base/$file" ] && continue
# If the target already exists as a file or link, there's nothing we can do
[ -e "$target/$file" -o -L "$target/$file" ] && [ ! -d "$target/$file" ] && continue
# If the target doesn't exist, just copy it over
if [ ! -e "$target/$file" -a ! -L "$target/$file" ]; then
cp -Ra "$base/$file" "$target/$file"
continue
fi
# That leaves us with directories and a recursive call
[ -d $file ] && sync_dirs $base $file $target
done
cd $OLD_PWD
}
process_bind_mounts() {
# Goes over /etc/system-image/writable-paths to create the correct fstab for
# the bind-mounts. Writes them into /run/image.fstab which is
# bind-mounted to /etc/fstab
if [ ! -e /etc/system-image/writable-paths ]; then
echo "This rootfs does not have any writable-paths defined"
return 0
fi
# Prepare the fstab
FSTAB=/etc/fstab
touch /run/image.fstab
mount -o bind /run/image.fstab $FSTAB ||halium_panic "Could not bind-mount fstab"
echo "/dev/root / rootfs defaults,ro 0 0" >>$FSTAB
echo "Adding bind-mounts to $FSTAB"
# Process the list of bind-mounts
# (but don't mount them, mountall will do it)
cat /etc/system-image/writable-paths | while read line; do
set -- $line
# Skip invalid/commented entries
([ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ]) && continue
[ "$1" = "#" ] && continue
# Skip invalid mount points
dstpath="/$1"
[ ! -e "$dstpath" ] && continue
if [ "$3" = "temporary" ]; then
# Temporary entries are simple, just mount a tmpfs
echo "tmpfs $1 tmpfs $5 0 0" >>$FSTAB
elif [ "$3" = "persistent" ] || [ "$3" = "synced" ]; then
# Figure out the source path
if [ "$2" = "auto" ]; then
srcpath="/userdata/system-data/$1"
path="/userdata/system-data/$1"
else
srcpath="/userdata/$2"
path="/userdata/$2"
fi
if [ ! -e "$srcpath" ]; then
# Process new persistent or synced paths
dstown=$(stat -c "%u:%g" $dstpath)
dstmode=$(stat -c "%a" $dstpath)
mkdir -p ${srcpath%/*}
if [ ! -d "$dstpath" ]; then
# Deal with redirected files
if [ "$4" = "transition" ]; then
cp -a $dstpath $srcpath
else
touch $srcpath
chown $dstown $srcpath
chmod $dstmode $srcpath
fi
else
# Deal with redirected directories
if [ "$4" = "transition" ] || [ "$3" = "synced" ]; then
cp -aR $dstpath $srcpath
else
mkdir $srcpath
chown $dstown $srcpath
chmod $dstmode $srcpath
fi
fi
elif [ "$3" = "synced" ]; then
# Process existing synced paths
sync_dirs $dstpath . $srcpath
fi
# Write the fstab entry
if [ "$5" = "none" ]; then
echo "$path $1 none bind 0 0" >>$FSTAB
else
echo "$path $1 none bind,$5 0 0" >>$FSTAB
fi
else
continue
fi
done
}
mount_android_partitions() {
fstab=$1
mount_root=$2
real_userdata=$3
echo "checking fstab $fstab for additional mount points"
# On systems with A/B partition layout, current slot is provided via cmdline parameter.
ab_slot_suffix=$(grep -o 'androidboot\.slot_suffix=..' /proc/cmdline | cut -d "=" -f2)
[ ! -z "$ab_slot_suffix" ] && echo "A/B slot system detected! Slot suffix is $ab_slot_suffix"
cat ${fstab} | while read line; do
set -- $line
# stop processing if we hit the "#endhalium" comment in the file
echo $1 | egrep -q "^#endhalium" && break
# Skip any unwanted entry
echo $1 | egrep -q "^#" && continue
([ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]) && continue
([ "$2" = "/system" ] || [ "$2" = "/data" ] || [ "$2" = "/" ]) && continue
label=$(echo $1 | awk -F/ '{print $NF}')
[ -z "$label" ] && continue
echo "checking mount label $label"
# In case fstab provides /dev/mmcblk0p* lines
path="/dev/$label"
for dir in by-partlabel by-name by-label by-path by-uuid by-partuuid by-id; do
# On A/B systems not all of the partitions are duplicated, so we have to check with and without suffix
if [ -e "/dev/disk/$dir/$label$ab_slot_suffix" ]; then
path="/dev/disk/$dir/$label$ab_slot_suffix"
break
elif [ -e "/dev/disk/$dir/$label" ]; then
path="/dev/disk/$dir/$label"
break
fi
done
[ ! -e "$path" ] && continue
mkdir -p ${mount_root}/$2
echo "mounting $path as ${mount_root}/$2"
mount $path ${mount_root}/$2 -t $3 -o $4
done
}
mount_halium_overlay() {
source=$1
target=$2
if [ -d ${source} ]; then
OLD_PWD=$PWD
cd ${source}
for overlay in $(find . -type f); do
[ -f ${target}/${overlay} ] && mount --bind ${source}/${overlay} ${target}/${overlay}
done
cd $OLD_PWD
fi
}
mount_userdata() {
partlist="userdata UDA DATAFS USERDATA"
# find the right partition
for partname in $partlist; do
part=$(find /dev -name $partname | tail -1)
[ -z "$part" ] && continue
path=$(readlink -f $part)
[ -n "$path" ] && break
done
# override with a possible cmdline parameter
if grep -q datapart= /proc/cmdline; then
for x in $(cat /proc/cmdline); do
case ${x} in
datapart=*)
path=${x#*=}
;;
esac
done
fi
if [ -z "$path" ]; then
echo "Couldn't find data partition."
fi
echo "checking filesystem integrity for the userdata partition"
# Mounting and umounting first, let the kernel handle the journal and
# orphaned inodes (faster than e2fsck). Then, just run e2fsck forcing -y.
# Also check the amount of time used by to check the filesystem.
fsck_start=$(date +%s)
mount -o errors=remount-ro $path /userdata
umount /userdata
e2fsck -y $path >/run/e2fsck.out 2>&1
fsck_end=$(date +%s)
echo "checking filesystem for userdata took (including e2fsck) $((fsck_end - fsck_start)) seconds"
# Mount the data partition to a temporary mount point
# FIXME: data=journal used on ext4 as a workaround for bug 1387214
[ `blkid $path -o value -s TYPE` = "ext4" ] && OPTIONS="data=journal,"
mount -o discard,$OPTIONS $path /userdata
}
mount_android_rootfs() {
mount -o loop /var/lib/lxc/android/android-rootfs.img /var/lib/lxc/android/rootfs
}
set_halium_version_properties() {
halium_system=$1
android_data=$2
channel_ini=$1/etc/system-image/channel.ini
def_language=$1/custom/default_language
halium="unknown"
device="unknown"
custom="unknown"
version="unknown"
channel="unknown"
def_lang="unknown"
if [ -f "$channel_ini" ]; then
IFS=','
for i in $(grep version_detail $channel_ini | awk -F ' ' '{print $2}'); do
id=${i%=*}
case $id in
halium) halium=${i#halium=} ;;
device) device=${i#device=} ;;
custom) custom=${i#custom=} ;;
version) version=${i#version=} ;;
esac
done
unset IFS
channel=$(grep channel $channel_ini | awk -F ' ' '{print $2}')
fi
if [ -f "$def_language" ]; then
lang=$(cat $def_language)
if [ -n "$lang" ]; then
def_lang=$lang
fi
fi
# Write down so the android property system can load them automatically
mkdir -p $android_data/property
chmod 700 $android_data/property
echo -n "$halium" >$android_data/property/persist.halium.version.rootfs
echo -n "$device" >$android_data/property/persist.halium.version.device
echo -n "$custom" >$android_data/property/persist.halium.version.custom
echo -n "$channel" >$android_data/property/persist.halium.version.channel
echo -n "$version" >$android_data/property/persist.halium.version
echo -n "$def_lang" >$android_data/property/persist.halium.default_language
chmod 600 $android_data/property/persist.halium*
}
# Make sure to set up everything only on first-stage boot.
if [ ! -e /proc/self/exe ]; then
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
# Put all of this script's output into /dev/kmsg
exec &>/dev/kmsg
# Mount proc early on for charging mode check
mount -t proc none /proc
# Switch into the Android rootfs for charging if requested
for x in $(cat /proc/cmdline); do
case ${x} in
androidboot.mode=*)
android_bootmode=${x#*=}
;;
esac
done
# No offline charging support for now, so just force a reboot
if [ "$android_bootmode" = "charger" ]; then
umount /proc
reboot -f
fi
# Mount a tmpfs in /run of rootfs to put the future image.fstab
mount -o rw,nosuid,noexec,relatime,mode=755 -t tmpfs tmpfs /run
mount -t devtmpfs devtmpfs /dev
mkdir /dev/pts
mount -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts || true
mount -t sysfs none /sys
# Distinguish between halium-boot & jumpercable boot process
touch /dev/.halium_jumpercable
chmod 000 /dev/.halium_jumpercable
mount_android_rootfs
# Bind mount rootfs to /android
mount -o bind /var/lib/lxc/android/rootfs /android
mount -o bind /mnt /var/lib/lxc/android/rootfs/mnt
mount_userdata
mount_android_partitions "/var/lib/lxc/android/rootfs/fstab*" /android /userdata
set_halium_version_properties / /userdata/android-data
process_bind_mounts
mount_halium_overlay /android/system/halium /
fi
# Execute actual init now
exec /sbin/init $@