blob: 7c5fffac531c0021bf7fc7b18f1d47ccd70efe5a [file] [log] [blame]
Dan Pasanen894b3522017-01-16 19:26:50 -06001#!/system/bin/sh
2#
3# Backup and restore addon /system files
4#
5
6export S=/system
7export C=/postinstall/tmp/backupdir
maxwen5b158882022-08-21 00:02:47 +02008export V=13
Dan Pasanen894b3522017-01-16 19:26:50 -06009
Alessandro Astone09bec2c2020-12-29 18:38:28 +010010export ADDOND_VERSION=3
11
12# Partitions to mount for backup/restore in V3
Alessandro Astone9c8972e2021-02-18 14:04:13 +010013export all_V3_partitions="vendor product system_ext"
Dan Pasanen8320eb12018-01-26 10:26:47 -060014
Dan Pasanen894b3522017-01-16 19:26:50 -060015# Scripts in /system/addon.d expect to find backuptool.functions in /tmp
16mkdir -p /postinstall/tmp/
Alessandro Astone3b6b6b82020-12-29 14:16:20 +010017mountpoint /postinstall/tmp >/dev/null 2>&1 || mount -t tmpfs tmpfs /postinstall/tmp
Dan Pasanen894b3522017-01-16 19:26:50 -060018cp -f /postinstall/system/bin/backuptool_ab.functions /postinstall/tmp/backuptool.functions
19
Alessandro Astone09bec2c2020-12-29 18:38:28 +010020get_script_version() {
21 version=$(grep "^# ADDOND_VERSION=" $1 | cut -d= -f2)
22 [ -z "$version" ] && version=1
23 echo $version
24}
25
Dan Pasanen894b3522017-01-16 19:26:50 -060026# Preserve /system/addon.d in /tmp/addon.d
27preserve_addon_d() {
28 if [ -d /system/addon.d/ ]; then
29 mkdir -p /postinstall/tmp/addon.d/
30 cp -a /system/addon.d/* /postinstall/tmp/addon.d/
Marko Man4e254982018-10-17 16:08:23 +020031 rm -f /postinstall/tmp/addon.d/70-gapps.sh
Dan Pasanen8320eb12018-01-26 10:26:47 -060032
Alessandro Astone09bec2c2020-12-29 18:38:28 +010033 # Discard any version 1 script, as it is not compatible with a/b
Dan Pasanen8320eb12018-01-26 10:26:47 -060034 for f in /postinstall/tmp/addon.d/*sh; do
Alessandro Astone09bec2c2020-12-29 18:38:28 +010035 if [ $(get_script_version $f) = 1 ]; then
Dan Pasanen8320eb12018-01-26 10:26:47 -060036 rm $f
37 fi
38 done
Dan Pasanen894b3522017-01-16 19:26:50 -060039 chmod 755 /postinstall/tmp/addon.d/*.sh
40 fi
41}
42
43# Restore /postinstall/system/addon.d from /postinstall/tmp/addon.d
44restore_addon_d() {
45 if [ -d /postinstall/tmp/addon.d/ ]; then
46 mkdir -p /postinstall/system/addon.d/
47 cp -a /postinstall/tmp/addon.d/* /postinstall/system/addon.d/
48 rm -rf /postinstall/tmp/addon.d/
49 fi
50}
51
52# Proceed only if /system is the expected major and minor version
53check_prereq() {
54# If there is no build.prop file the partition is probably empty.
55if [ ! -r /system/build.prop ]; then
z3DD3r31285e52020-03-04 18:07:40 +030056 echo "Backup/restore is not possible. Partition is probably empty"
57 return 1
Dan Pasanen894b3522017-01-16 19:26:50 -060058fi
59
z3DD3r8ca31e62020-03-04 12:42:10 +030060if ! grep -q "^ro.omni.version=$V.*" /product/build.prop; then
z3DD3r31285e52020-03-04 18:07:40 +030061 echo "Backup/restore is not possible. Incompatible ROM version: $V"
62 return 2
z3DD3r8ca31e62020-03-04 12:42:10 +030063fi
z3DD3r31285e52020-03-04 18:07:40 +030064return 0
Dan Pasanen894b3522017-01-16 19:26:50 -060065}
66
Alessandro Astone09bec2c2020-12-29 18:38:28 +010067# Execute /system/addon.d/*.sh scripts with each $@ parameter
68run_stages() {
maxwen1e534e82020-03-29 16:52:37 +020069log -t "update_engine" $1
70
Dan Pasanen894b3522017-01-16 19:26:50 -060071if [ -d /postinstall/tmp/addon.d/ ]; then
72 for script in $(find /postinstall/tmp/addon.d/ -name '*.sh' |sort -n); do
maxwen1e534e82020-03-29 16:52:37 +020073 log -t "update_engine" $script
74
Dan Pasanen894b3522017-01-16 19:26:50 -060075 # we have no /sbin/sh in android, only recovery
76 # use /system/bin/sh here instead
77 sed -i '0,/#!\/sbin\/sh/{s|#!/sbin/sh|#!/system/bin/sh|}' $script
Alessandro Astone3b6b6b82020-12-29 14:16:20 +010078 # we can't count on /tmp existing on an A/B device, so utilize /postinstall/tmp as tmpfs
Dan Pasanen894b3522017-01-16 19:26:50 -060079 sed -i 's|. /tmp/backuptool.functions|. /postinstall/tmp/backuptool.functions|g' $script
Alessandro Astone09bec2c2020-12-29 18:38:28 +010080 v=$(get_script_version $script)
81 if [ $v -ge 3 ]; then
82 mount_extra $all_V3_partitions
83 else
84 umount_extra $all_V3_partitions
85 fi
86
87 for stage in $@; do
88 if [ $v -ge 3 ]; then
89 $script $stage
90 else
91 ADDOND_VERSION=2 $script $stage
92 fi
93 done
Dan Pasanen894b3522017-01-16 19:26:50 -060094 done
95fi
96}
97
Alessandro Astone09bec2c2020-12-29 18:38:28 +010098#####################
99### Mount helpers ###
100#####################
101get_block_for_mount_point() {
102 grep -v "^#" /vendor/etc/fstab.$(getprop ro.boot.hardware) | grep " $1 " | tail -n1 | tr -s ' ' | cut -d' ' -f1
103}
104
105find_block() {
106 local name="$1"
107 local fstab_entry=$(get_block_for_mount_point "/$name")
108 # P-SAR hacks
109 [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/")
110 [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/system_root")
111
112 local dev
113 if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
114 if [ -n "$fstab_entry" ]; then
115 dev="${BLK_PATH}/${fstab_entry}${SLOT_SUFFIX}"
116 else
117 dev="${BLK_PATH}/${name}${SLOT_SUFFIX}"
118 fi
119 else
120 if [ -n "$fstab_entry" ]; then
121 dev="${fstab_entry}${SLOT_SUFFIX}"
122 else
123 dev="${BLK_PATH}/${name}${SLOT_SUFFIX}"
124 fi
125 fi
126
127 if [ -b "$dev" ]; then
128 echo "$dev"
129 fi
130}
131
132DYNAMIC_PARTITIONS=$(getprop ro.boot.dynamic_partitions)
133if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
134 BLK_PATH="/dev/block/mapper"
135else
136 BLK_PATH=/dev/block/bootdevice/by-name
137fi
138
139CURRENTSLOT=$(getprop ro.boot.slot_suffix)
140if [ ! -z "$CURRENTSLOT" ]; then
141 if [ "$CURRENTSLOT" = "_a" ]; then
142 # Opposite slot
143 SLOT_SUFFIX="_b"
144 else
145 SLOT_SUFFIX="_a"
146 fi
147fi
148
149mount_extra() {
150 for partition in $@; do
151 mnt_point="/postinstall/$partition"
152 mountpoint "$mnt_point" >/dev/null 2>&1 && break
153
154 blk_dev=$(find_block "$partition")
155 if [ -n "$blk_dev" ]; then
156 [ "$DYNAMIC_PARTITIONS" = "true" ] && blockdev --setrw "$blk_dev"
157 mount -o rw "$blk_dev" "$mnt_point"
158 fi
159 done
160}
161
162umount_extra() {
163 for partition in $@; do
164 # Careful with unmounting. If the update has a partition less than the current system,
165 # /postinstall/$partition is a symlink to /system/$partition, which on the active slot
166 # is a symlink to /$partition which is a mountpoint we would end up unmounting!
167 [ ! -L "/postinstall/$partition" ] && umount -l "/postinstall/$partition" 2>/dev/null
168 done
169}
170
Dan Pasanen894b3522017-01-16 19:26:50 -0600171case "$1" in
172 backup)
z3DD3r31285e52020-03-04 18:07:40 +0300173 if check_prereq; then
174 mkdir -p $C
175 preserve_addon_d
Alessandro Astone09bec2c2020-12-29 18:38:28 +0100176 run_stages pre-backup backup post-backup
Dan Pasanen894b3522017-01-16 19:26:50 -0600177 fi
maxwen1e534e82020-03-29 16:52:37 +0200178 log -t "update_engine" "backuptool_ab.sh backup"
Dan Pasanen894b3522017-01-16 19:26:50 -0600179 ;;
180 restore)
z3DD3r31285e52020-03-04 18:07:40 +0300181 if check_prereq; then
Alessandro Astone09bec2c2020-12-29 18:38:28 +0100182 run_stages pre-restore restore post-restore
183 umount_extra $all_V3_partitions
z3DD3r31285e52020-03-04 18:07:40 +0300184 restore_addon_d
185 rm -rf $C
Alessandro Astone3b6b6b82020-12-29 14:16:20 +0100186 umount /postinstall/tmp
z3DD3r31285e52020-03-04 18:07:40 +0300187 rm -rf /postinstall/tmp
188 sync
maxwen1e534e82020-03-29 16:52:37 +0200189 log -t "update_engine" "backuptool_ab.sh restore"
Dan Pasanen894b3522017-01-16 19:26:50 -0600190 ;;
191 *)
192 echo "Usage: $0 {backup|restore}"
193 exit 1
194esac
195
196exit 0