xref: /aosp_15_r20/system/extras/postinst/postinst.sh (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker#!/system/bin/sh
2*288bf522SAndroid Build Coastguard Worker
3*288bf522SAndroid Build Coastguard Worker#
4*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2015 The Android Open Source Project
5*288bf522SAndroid Build Coastguard Worker#
6*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
7*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
8*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at
9*288bf522SAndroid Build Coastguard Worker#
10*288bf522SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
11*288bf522SAndroid Build Coastguard Worker#
12*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
13*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
14*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
16*288bf522SAndroid Build Coastguard Worker# limitations under the License.
17*288bf522SAndroid Build Coastguard Worker#
18*288bf522SAndroid Build Coastguard Worker
19*288bf522SAndroid Build Coastguard Worker# This is an example post-install script. This script will be executed by the
20*288bf522SAndroid Build Coastguard Worker# update_engine right after finishing writing all the partitions, but before
21*288bf522SAndroid Build Coastguard Worker# marking the new slot as active. To enable running this program, insert these
22*288bf522SAndroid Build Coastguard Worker# lines in your product's .mk file (without the # at the beginning):
23*288bf522SAndroid Build Coastguard Worker
24*288bf522SAndroid Build Coastguard Worker# AB_OTA_POSTINSTALL_CONFIG += \
25*288bf522SAndroid Build Coastguard Worker#   RUN_POSTINSTALL_system=true \
26*288bf522SAndroid Build Coastguard Worker#   POSTINSTALL_PATH_system=bin/postinst_example \
27*288bf522SAndroid Build Coastguard Worker#   FILESYSTEM_TYPE_system=ext4 \
28*288bf522SAndroid Build Coastguard Worker
29*288bf522SAndroid Build Coastguard Worker# This script receives no arguments. argv[0] will include the absolute path to
30*288bf522SAndroid Build Coastguard Worker# the script, including the directory where the new partition was mounted.
31*288bf522SAndroid Build Coastguard Worker#
32*288bf522SAndroid Build Coastguard Worker# The script will run from the "postinstall" SELinux domain, from the old system
33*288bf522SAndroid Build Coastguard Worker# environment (kernel, SELinux rules, etc). New rules and domains introduced by
34*288bf522SAndroid Build Coastguard Worker# the new system won't be available when this script runs, instead, all the
35*288bf522SAndroid Build Coastguard Worker# files in the mounted directory will have the attribute "postinstall_file". All
36*288bf522SAndroid Build Coastguard Worker# the files accessed from here would need to be allowed in the old system or
37*288bf522SAndroid Build Coastguard Worker# those accesses will fail. For example, the absolute path used in the first
38*288bf522SAndroid Build Coastguard Worker# line of this script (/system/bin/sh) is indeed the old system's sh binary. If
39*288bf522SAndroid Build Coastguard Worker# you use a compiled program, you might want to link it statically or use a
40*288bf522SAndroid Build Coastguard Worker# wrapper script to use the new ldso to run your program (see the
41*288bf522SAndroid Build Coastguard Worker# --generate-wrappers option in lddtree.py for an example).
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard Worker# We get called with two parameters: <target_slot> <status_fd>
44*288bf522SAndroid Build Coastguard Worker# * <target_slot> is the slot where the new system was just copied. This is
45*288bf522SAndroid Build Coastguard Worker#   normally either 0 or 1. You can get the target suffix running
46*288bf522SAndroid Build Coastguard Worker#   `bootctl get-suffix ${target_slot}`
47*288bf522SAndroid Build Coastguard Worker# * <status_fd> is a file descriptor number where this script can write to to
48*288bf522SAndroid Build Coastguard Worker#   report the progress of the process. See examples below.
49*288bf522SAndroid Build Coastguard Worker
50*288bf522SAndroid Build Coastguard Workertarget_slot="$1"
51*288bf522SAndroid Build Coastguard Workerstatus_fd="$2"
52*288bf522SAndroid Build Coastguard Worker
53*288bf522SAndroid Build Coastguard Workermy_dir=$(dirname "$0")
54*288bf522SAndroid Build Coastguard Worker
55*288bf522SAndroid Build Coastguard Worker# We can notify the updater of the progress of our program by writing to the
56*288bf522SAndroid Build Coastguard Worker# status file descriptor "global_progress <frac>\n".
57*288bf522SAndroid Build Coastguard Workerprint -u${status_fd} "global_progress 0"
58*288bf522SAndroid Build Coastguard Worker
59*288bf522SAndroid Build Coastguard Workerecho "The output of this program will show up in the logs." >&2
60*288bf522SAndroid Build Coastguard Worker
61*288bf522SAndroid Build Coastguard Worker# We are half way done, so we set 0.5.
62*288bf522SAndroid Build Coastguard Workerprint -u${status_fd} "global_progress 0.5"
63*288bf522SAndroid Build Coastguard Worker
64*288bf522SAndroid Build Coastguard Workerecho "Note that this program runs from ${my_dir}"
65*288bf522SAndroid Build Coastguard Worker
66*288bf522SAndroid Build Coastguard Worker# Actually, we were done.
67*288bf522SAndroid Build Coastguard Workerprint -u${status_fd} "global_progress 1.0"
68*288bf522SAndroid Build Coastguard Worker
69*288bf522SAndroid Build Coastguard Worker# If the exit code of this program is an error code (different from 0), the
70*288bf522SAndroid Build Coastguard Worker# update will fail and the new slot will not be marked as active.
71*288bf522SAndroid Build Coastguard Worker
72*288bf522SAndroid Build Coastguard Workerexit 0
73