blob: 01e84bf7e1e1c3b6c2d00dfac2a3d72653c6ae7e [file] [log] [blame]
kmobse051cf32010-06-24 22:36:11 -05001#!/sbin/sh
2#
Warren Togamib1637c82012-03-03 22:37:42 -10003# Backup and restore addon /system files
kmobse051cf32010-06-24 22:36:11 -05004#
5
Warren Togamib1637c82012-03-03 22:37:42 -10006export C=/tmp/backupdir
Jackeaglee7ed2772021-10-10 05:27:28 -04007export V=12.0
Davide Garberie40c3182019-09-24 20:18:11 +02008export SYSDEV="$(readlink -nf "$2")"
9export SYSFS="$3"
kmobse051cf32010-06-24 22:36:11 -050010
Alessandro Astone42aa1fa2020-12-29 18:38:28 +010011export ADDOND_VERSION=3
12
13# Partitions to mount for backup/restore in V3
14export all_V3_partitions="vendor product system_ext"
15
Tom Marshall65aff662014-12-12 11:56:04 -080016# Scripts in /system/addon.d expect to find backuptool.functions in /tmp
17cp -f /tmp/install/bin/backuptool.functions /tmp
18
Alessandro Astone42aa1fa2020-12-29 18:38:28 +010019get_script_version() {
20 version=$(grep "^# ADDOND_VERSION=" $1 | cut -d= -f2)
21 [ -z "$version" ] && version=1
22 echo $version
23}
24
Warren Togamib1637c82012-03-03 22:37:42 -100025# Preserve /system/addon.d in /tmp/addon.d
26preserve_addon_d() {
LuK133757490e62018-12-05 20:51:21 +010027 if [ -d $S/addon.d/ ]; then
Andreas Schneider9cfe9682015-09-03 11:04:58 +020028 mkdir -p /tmp/addon.d/
LuK133757490e62018-12-05 20:51:21 +010029 cp -a $S/addon.d/* /tmp/addon.d/
Andreas Schneider9cfe9682015-09-03 11:04:58 +020030 chmod 755 /tmp/addon.d/*.sh
31 fi
Warren Togamib1637c82012-03-03 22:37:42 -100032}
33
Andreas Schneider9cfe9682015-09-03 11:04:58 +020034# Restore /system/addon.d from /tmp/addon.d
Warren Togamib1637c82012-03-03 22:37:42 -100035restore_addon_d() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020036 if [ -d /tmp/addon.d/ ]; then
LuK133757490e62018-12-05 20:51:21 +010037 mkdir -p $S/addon.d/
38 cp -a /tmp/addon.d/* $S/addon.d/
Andreas Schneider9cfe9682015-09-03 11:04:58 +020039 rm -rf /tmp/addon.d/
40 fi
Warren Togamib1637c82012-03-03 22:37:42 -100041}
42
Chirayu Desai700fc8f2012-12-13 19:36:37 +053043# Proceed only if /system is the expected major and minor version
kmobse051cf32010-06-24 22:36:11 -050044check_prereq() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020045# If there is no build.prop file the partition is probably empty.
LuK133757490e62018-12-05 20:51:21 +010046if [ ! -r $S/build.prop ]; then
z3DD3r284ec832020-03-04 18:07:40 +030047 echo "Backup/restore is not possible. Partition is probably empty"
48 return 1
Andreas Schneider9cfe9682015-09-03 11:04:58 +020049fi
Jackeagleee73e6c2020-04-22 17:32:53 +020050if ! grep -q "^ro.bliss.static.version=$V.*" $S/build.prop; then
z3DD3r284ec832020-03-04 18:07:40 +030051 echo "Backup/restore is not possible. Incompatible ROM version: $V"
52 return 2
Adrian DC29fca642018-05-17 23:06:24 +020053fi
z3DD3r284ec832020-03-04 18:07:40 +030054return 0
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010055}
56
Alessandro Astone42aa1fa2020-12-29 18:38:28 +010057# Execute /system/addon.d/*.sh scripts with each $@ parameter
58run_stages() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020059if [ -d /tmp/addon.d/ ]; then
60 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
Alessandro Astone42aa1fa2020-12-29 18:38:28 +010061 v=$(get_script_version $script)
62 if [ $v -ge 3 ]; then
63 mount_extra $all_V3_partitions
64 else
65 umount_extra $all_V3_partitions
66 fi
67
68 for stage in $@; do
69 if [ $v -ge 3 ]; then
70 $script $stage
71 else
Alessandro Astone145be422021-03-26 12:52:38 +010072 ADDOND_VERSION= $script $stage
Alessandro Astone42aa1fa2020-12-29 18:38:28 +010073 fi
74 done
Andreas Schneider9cfe9682015-09-03 11:04:58 +020075 done
76fi
kmobse051cf32010-06-24 22:36:11 -050077}
78
Alessandro Astone42aa1fa2020-12-29 18:38:28 +010079#####################
80### Mount helpers ###
81#####################
Davide Garberie40c3182019-09-24 20:18:11 +020082determine_system_mount() {
83 if grep -q -e"^$SYSDEV" /proc/mounts; then
84 umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2)
85 fi
86
87 if [ -d /mnt/system ]; then
88 SYSMOUNT="/mnt/system"
89 elif [ -d /system_root ]; then
90 SYSMOUNT="/system_root"
91 else
92 SYSMOUNT="/system"
93 fi
94
95 export S=$SYSMOUNT/system
96}
97
98mount_system() {
99 mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard
100}
101
102unmount_system() {
103 umount $SYSMOUNT
104}
105
Alessandro Astone42aa1fa2020-12-29 18:38:28 +0100106get_block_for_mount_point() {
Erik Jensen08d29c42021-04-30 05:42:41 +0000107 grep -v "^#" /etc/recovery.fstab | grep "[[:blank:]]$1[[:blank:]]" | tail -n1 | tr -s [:blank:] ' ' | cut -d' ' -f1
Alessandro Astone42aa1fa2020-12-29 18:38:28 +0100108}
109
110find_block() {
111 local name="$1"
112 local fstab_entry=$(get_block_for_mount_point "/$name")
113 # P-SAR hacks
114 [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/")
115 [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/system_root")
116
117 local dev
118 if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
119 if [ -n "$fstab_entry" ]; then
120 dev="${BLK_PATH}/${fstab_entry}"
121 else
122 dev="${BLK_PATH}/${name}"
123 fi
124 else
125 if [ -n "$fstab_entry" ]; then
126 dev="$fstab_entry"
127 else
128 dev="${BLK_PATH}/${name}"
129 fi
130 fi
131
132 if [ -b "$dev" ]; then
133 echo "$dev"
134 fi
135}
136
Davide Garberie40c3182019-09-24 20:18:11 +0200137determine_system_mount
138
Alessandro Astone42aa1fa2020-12-29 18:38:28 +0100139DYNAMIC_PARTITIONS=$(getprop ro.boot.dynamic_partitions)
Tim Zimmermannc8a52592021-05-08 16:17:18 +0200140if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
141 BLK_PATH="/dev/block/mapper"
142else
143 BLK_PATH=$(dirname "$SYSDEV")
144fi
Alessandro Astone42aa1fa2020-12-29 18:38:28 +0100145
146mount_extra() {
147 for partition in $@; do
148 mnt_point="/$partition"
Alessandro Astoneb0086302021-04-02 00:18:16 +0200149 mountpoint "$mnt_point" >/dev/null 2>&1 && continue
150 [ -L "$SYSMOUNT/$partition" ] && continue
Alessandro Astone42aa1fa2020-12-29 18:38:28 +0100151
152 blk_dev=$(find_block "$partition")
153 if [ -e "$blk_dev" ]; then
154 [ "$DYNAMIC_PARTITIONS" = "true" ] && blockdev --setrw "$blk_dev"
155 mkdir -p "$mnt_point"
156 mount -o rw "$blk_dev" "$mnt_point"
157 fi
158 done
159}
160
161umount_extra() {
162 for partition in $@; do
163 umount -l "/$partition" 2>/dev/null
164 done
165}
166
kmobse051cf32010-06-24 22:36:11 -0500167case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -1000168 backup)
Davide Garberie40c3182019-09-24 20:18:11 +0200169 mount_system
z3DD3r284ec832020-03-04 18:07:40 +0300170 if check_prereq; then
171 mkdir -p $C
172 preserve_addon_d
Alessandro Astone42aa1fa2020-12-29 18:38:28 +0100173 run_stages pre-backup backup post-backup
z3DD3r284ec832020-03-04 18:07:40 +0300174 fi
Davide Garberie40c3182019-09-24 20:18:11 +0200175 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000176 ;;
177 restore)
Davide Garberie40c3182019-09-24 20:18:11 +0200178 mount_system
z3DD3r284ec832020-03-04 18:07:40 +0300179 if check_prereq; then
Alessandro Astone42aa1fa2020-12-29 18:38:28 +0100180 run_stages pre-restore restore post-restore
181 umount_extra $all_V3_partitions
z3DD3r284ec832020-03-04 18:07:40 +0300182 restore_addon_d
183 rm -rf $C
184 sync
185 fi
Davide Garberie40c3182019-09-24 20:18:11 +0200186 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000187 ;;
188 *)
189 echo "Usage: $0 {backup|restore}"
190 exit 1
kmobse051cf32010-06-24 22:36:11 -0500191esac
192
193exit 0