blob: 3a75bbdb580cda3de7710f96e910669c7d6351dd [file] [log] [blame]
Roland Levillain56049382018-05-23 18:26:22 +01001#!/bin/bash
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Orion Hodson1c5b1ea2020-01-22 15:34:33 +000017# This script undoes (most of) the work done by tools/buildbot-setup-device.sh.
Roland Levillain56049382018-05-23 18:26:22 +010018# Make sure to keep these files in sync.
19
Orion Hodson5697fab2019-11-19 15:01:02 +000020if [ -t 1 ]; then
21 # Color sequences if terminal is a tty.
22 green='\033[0;32m'
23 nc='\033[0m'
24fi
Roland Levillain56049382018-05-23 18:26:22 +010025
26# Setup as root, as some actions performed here require it.
27adb root
28adb wait-for-device
29
30if [[ -n "$ART_TEST_CHROOT" ]]; then
Roland Levillain56049382018-05-23 18:26:22 +010031 # Check that ART_TEST_CHROOT is correctly defined.
32 [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; }
33
Roland Levillainf647c4c2018-06-12 14:44:20 +010034 if adb shell test -d "$ART_TEST_CHROOT"; then
35 # Display users of the chroot dir.
Roland Levillain56049382018-05-23 18:26:22 +010036
Roland Levillainf647c4c2018-06-12 14:44:20 +010037 echo -e "${green}List open files under chroot dir $ART_TEST_CHROOT${nc}"
38 adb shell lsof | grep "$ART_TEST_CHROOT"
Roland Levillain56049382018-05-23 18:26:22 +010039
Roland Levillain9ebb41f2018-06-15 17:50:58 +010040 # for_all_chroot_process ACTION
41 # -----------------------------
42 # Execute ACTION on all processes running from binaries located
43 # under the chroot directory. ACTION is passed two arguments: the
44 # PID of the process, and a string containing the command line
45 # that started this process.
46 for_all_chroot_process() {
47 local action=$1
Roland Levillain7d9052c2018-08-14 13:57:33 +010048 adb shell ls -ld "/proc/*/root" \
49 | sed -n -e "s,^.* \\(/proc/.*/root\\) -> $ART_TEST_CHROOT\$,\\1,p" \
50 | while read link; do
51 local dir=$(dirname "$link")
52 local pid=$(basename "$dir")
53 local cmdline=$(adb shell cat "$dir"/cmdline | tr '\000' ' ')
54 $action "$pid" "$cmdline"
55 done
Roland Levillain9ebb41f2018-06-15 17:50:58 +010056 }
Roland Levillain56049382018-05-23 18:26:22 +010057
Roland Levillain9ebb41f2018-06-15 17:50:58 +010058 # display_process PID CMDLINE
59 # ---------------------------
60 # Display information about process with given PID, that was started with CMDLINE.
61 display_process() {
62 local pid=$1
63 local cmdline=$2
64 echo "$cmdline (PID: $pid)"
65 }
66
67 echo -e "${green}List processes running from binaries under chroot dir $ART_TEST_CHROOT${nc}"
68 for_all_chroot_process display_process
Roland Levillain56049382018-05-23 18:26:22 +010069
Roland Levillainf647c4c2018-06-12 14:44:20 +010070 # Tear down the chroot dir.
Roland Levillain56049382018-05-23 18:26:22 +010071
Roland Levillainf647c4c2018-06-12 14:44:20 +010072 echo -e "${green}Tear down the chroot set up in $ART_TEST_CHROOT${nc}"
73
74 # remove_filesystem_from_chroot DIR-IN-CHROOT FSTYPE REMOVE-DIR-IN-CHROOT
75 # -----------------------------------------------------------------------
76 # Unmount filesystem with type FSTYPE mounted in directory DIR-IN-CHROOT
77 # under the chroot directory.
78 # Remove DIR-IN-CHROOT under the chroot if REMOVE-DIR-IN-CHROOT is
79 # true.
80 remove_filesystem_from_chroot() {
81 local dir_in_chroot=$1
82 local fstype=$2
83 local remove_dir=$3
84 local dir="$ART_TEST_CHROOT/$dir_in_chroot"
85 adb shell test -d "$dir" \
86 && adb shell mount | grep -q "^$fstype on $dir type $fstype " \
87 && if adb shell umount "$dir"; then
88 $remove_dir && adb shell rmdir "$dir"
89 else
90 echo "Files still open in $dir:"
91 adb shell lsof | grep "$dir"
92 fi
93 }
94
Martin Stjernholm8c7e2192020-06-26 15:54:16 +010095 # Remove /bin symlink from chroot.
96 adb shell rm -f "$ART_TEST_CHROOT/bin"
97
Roland Levillain7b7ea792019-01-08 19:47:50 +000098 # Remove /apex from chroot.
Roland Levillain0f9823e2019-06-18 16:49:24 +010099 adb shell rm -rf "$ART_TEST_CHROOT/apex"
Roland Levillain7b7ea792019-01-08 19:47:50 +0000100
Roland Levillainf647c4c2018-06-12 14:44:20 +0100101 # Remove /dev from chroot.
102 remove_filesystem_from_chroot dev tmpfs true
103
104 # Remove /sys/kernel/debug from chroot.
105 # The /sys/kernel/debug directory under the chroot dir cannot be
106 # deleted, as it is part of the host device's /sys filesystem.
107 remove_filesystem_from_chroot sys/kernel/debug debugfs false
108 # Remove /sys from chroot.
109 remove_filesystem_from_chroot sys sysfs true
110
111 # Remove /proc from chroot.
112 remove_filesystem_from_chroot proc proc true
113
114 # Remove /etc from chroot.
115 adb shell rm -f "$ART_TEST_CHROOT/etc"
116 adb shell rm -rf "$ART_TEST_CHROOT/system/etc"
117
118 # Remove directories used for ART testing in chroot.
119 adb shell rm -rf "$ART_TEST_CHROOT/data/local/tmp"
120 adb shell rm -rf "$ART_TEST_CHROOT/data/dalvik-cache"
121 adb shell rm -rf "$ART_TEST_CHROOT/tmp"
122
123 # Remove property_contexts file(s) from chroot.
124 property_context_files="/property_contexts \
125 /system/etc/selinux/plat_property_contexts \
126 /vendor/etc/selinux/nonplat_property_context \
127 /plat_property_contexts \
128 /nonplat_property_contexts"
129 for f in $property_context_files; do
130 adb shell rm -f "$ART_TEST_CHROOT$f"
131 done
Roland Levillain9ebb41f2018-06-15 17:50:58 +0100132
133
134 # Kill processes still running in the chroot.
135
136 # kill_process PID CMDLINE
137 # ------------------------
138 # Kill process with given PID, that was started with CMDLINE.
139 kill_process() {
140 local pid=$1
141 local cmdline=$2
142 echo "Killing $cmdline (PID: $pid)"
143 adb shell kill -9 "$pid"
144 }
145
146 echo -e "${green}Kill processes still running from binaries under" \
147 "chroot dir $ART_TEST_CHROOT (if any)${nc} "
148 for_all_chroot_process kill_process
Roland Levillainf647c4c2018-06-12 14:44:20 +0100149 fi
Roland Levillain56049382018-05-23 18:26:22 +0100150fi