Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 1 | #!/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 Hodson | 1c5b1ea | 2020-01-22 15:34:33 +0000 | [diff] [blame] | 17 | # This script undoes (most of) the work done by tools/buildbot-setup-device.sh. |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 18 | # Make sure to keep these files in sync. |
| 19 | |
Ulya Trafimovich | 9597748 | 2021-11-09 14:05:14 +0000 | [diff] [blame] | 20 | . "$(dirname $0)/buildbot-utils.sh" |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 21 | |
| 22 | # Setup as root, as some actions performed here require it. |
| 23 | adb root |
| 24 | adb wait-for-device |
| 25 | |
| 26 | if [[ -n "$ART_TEST_CHROOT" ]]; then |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 27 | # Check that ART_TEST_CHROOT is correctly defined. |
| 28 | [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; } |
| 29 | |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 30 | if adb shell test -d "$ART_TEST_CHROOT"; then |
| 31 | # Display users of the chroot dir. |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 32 | |
Ulya Trafimovich | 5298bcd | 2021-11-09 14:46:22 +0000 | [diff] [blame] | 33 | msginfo "List open files under chroot dir $ART_TEST_CHROOT" |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 34 | adb shell lsof | grep "$ART_TEST_CHROOT" |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 35 | |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 36 | # for_all_chroot_process ACTION |
| 37 | # ----------------------------- |
| 38 | # Execute ACTION on all processes running from binaries located |
| 39 | # under the chroot directory. ACTION is passed two arguments: the |
| 40 | # PID of the process, and a string containing the command line |
| 41 | # that started this process. |
| 42 | for_all_chroot_process() { |
| 43 | local action=$1 |
Roland Levillain | 7d9052c | 2018-08-14 13:57:33 +0100 | [diff] [blame] | 44 | adb shell ls -ld "/proc/*/root" \ |
| 45 | | sed -n -e "s,^.* \\(/proc/.*/root\\) -> $ART_TEST_CHROOT\$,\\1,p" \ |
| 46 | | while read link; do |
| 47 | local dir=$(dirname "$link") |
| 48 | local pid=$(basename "$dir") |
| 49 | local cmdline=$(adb shell cat "$dir"/cmdline | tr '\000' ' ') |
| 50 | $action "$pid" "$cmdline" |
| 51 | done |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 52 | } |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 53 | |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 54 | # display_process PID CMDLINE |
| 55 | # --------------------------- |
| 56 | # Display information about process with given PID, that was started with CMDLINE. |
| 57 | display_process() { |
| 58 | local pid=$1 |
| 59 | local cmdline=$2 |
| 60 | echo "$cmdline (PID: $pid)" |
| 61 | } |
| 62 | |
Ulya Trafimovich | 5298bcd | 2021-11-09 14:46:22 +0000 | [diff] [blame] | 63 | msginfo "List processes running from binaries under chroot dir $ART_TEST_CHROOT" |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 64 | for_all_chroot_process display_process |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 65 | |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 66 | # Tear down the chroot dir. |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 67 | |
Ulya Trafimovich | 5298bcd | 2021-11-09 14:46:22 +0000 | [diff] [blame] | 68 | msginfo "Tear down the chroot set up in $ART_TEST_CHROOT" |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 69 | |
| 70 | # remove_filesystem_from_chroot DIR-IN-CHROOT FSTYPE REMOVE-DIR-IN-CHROOT |
| 71 | # ----------------------------------------------------------------------- |
| 72 | # Unmount filesystem with type FSTYPE mounted in directory DIR-IN-CHROOT |
| 73 | # under the chroot directory. |
| 74 | # Remove DIR-IN-CHROOT under the chroot if REMOVE-DIR-IN-CHROOT is |
| 75 | # true. |
| 76 | remove_filesystem_from_chroot() { |
| 77 | local dir_in_chroot=$1 |
| 78 | local fstype=$2 |
| 79 | local remove_dir=$3 |
| 80 | local dir="$ART_TEST_CHROOT/$dir_in_chroot" |
| 81 | adb shell test -d "$dir" \ |
Jiakai Zhang | 15f888e | 2022-07-25 23:29:56 +0100 | [diff] [blame] | 82 | && adb shell mount | grep -q " on $dir type $fstype " \ |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 83 | && if adb shell umount "$dir"; then |
| 84 | $remove_dir && adb shell rmdir "$dir" |
| 85 | else |
| 86 | echo "Files still open in $dir:" |
| 87 | adb shell lsof | grep "$dir" |
| 88 | fi |
| 89 | } |
| 90 | |
Martin Stjernholm | 8c7e219 | 2020-06-26 15:54:16 +0100 | [diff] [blame] | 91 | # Remove /bin symlink from chroot. |
| 92 | adb shell rm -f "$ART_TEST_CHROOT/bin" |
| 93 | |
Roland Levillain | 7b7ea79 | 2019-01-08 19:47:50 +0000 | [diff] [blame] | 94 | # Remove /apex from chroot. |
Roland Levillain | 0f9823e | 2019-06-18 16:49:24 +0100 | [diff] [blame] | 95 | adb shell rm -rf "$ART_TEST_CHROOT/apex" |
Roland Levillain | 7b7ea79 | 2019-01-08 19:47:50 +0000 | [diff] [blame] | 96 | |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 97 | # Remove /dev from chroot. |
Jiakai Zhang | cc74dec | 2022-08-01 19:38:44 +0100 | [diff] [blame] | 98 | remove_filesystem_from_chroot dev/cpuset cgroup false |
Peter Collingbourne | b2c68b3 | 2022-03-03 12:17:02 -0800 | [diff] [blame] | 99 | remove_filesystem_from_chroot dev/pts devpts false |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 100 | remove_filesystem_from_chroot dev tmpfs true |
| 101 | |
| 102 | # Remove /sys/kernel/debug from chroot. |
| 103 | # The /sys/kernel/debug directory under the chroot dir cannot be |
| 104 | # deleted, as it is part of the host device's /sys filesystem. |
| 105 | remove_filesystem_from_chroot sys/kernel/debug debugfs false |
| 106 | # Remove /sys from chroot. |
| 107 | remove_filesystem_from_chroot sys sysfs true |
| 108 | |
| 109 | # Remove /proc from chroot. |
| 110 | remove_filesystem_from_chroot proc proc true |
| 111 | |
| 112 | # Remove /etc from chroot. |
| 113 | adb shell rm -f "$ART_TEST_CHROOT/etc" |
| 114 | adb shell rm -rf "$ART_TEST_CHROOT/system/etc" |
| 115 | |
| 116 | # Remove directories used for ART testing in chroot. |
| 117 | adb shell rm -rf "$ART_TEST_CHROOT/data/local/tmp" |
| 118 | adb shell rm -rf "$ART_TEST_CHROOT/data/dalvik-cache" |
| 119 | adb shell rm -rf "$ART_TEST_CHROOT/tmp" |
| 120 | |
| 121 | # Remove property_contexts file(s) from chroot. |
| 122 | property_context_files="/property_contexts \ |
| 123 | /system/etc/selinux/plat_property_contexts \ |
| 124 | /vendor/etc/selinux/nonplat_property_context \ |
| 125 | /plat_property_contexts \ |
| 126 | /nonplat_property_contexts" |
| 127 | for f in $property_context_files; do |
| 128 | adb shell rm -f "$ART_TEST_CHROOT$f" |
| 129 | done |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 130 | |
| 131 | |
| 132 | # Kill processes still running in the chroot. |
| 133 | |
| 134 | # kill_process PID CMDLINE |
| 135 | # ------------------------ |
| 136 | # Kill process with given PID, that was started with CMDLINE. |
| 137 | kill_process() { |
| 138 | local pid=$1 |
| 139 | local cmdline=$2 |
| 140 | echo "Killing $cmdline (PID: $pid)" |
| 141 | adb shell kill -9 "$pid" |
| 142 | } |
| 143 | |
Ulya Trafimovich | 5298bcd | 2021-11-09 14:46:22 +0000 | [diff] [blame] | 144 | msginfo "Kill processes" \ |
| 145 | "still running from binaries under chroot dir $ART_TEST_CHROOT (if any)" |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 146 | for_all_chroot_process kill_process |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 147 | fi |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 148 | fi |