1#!/bin/bash
2#
3# Copyright (C) 2019 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
17# Unmount Android Runtime and Core Libraries APEX packages required in the chroot directory.
18# This script emulates some the actions performed by `apexd`.
19
20# This script undoes the work done by tools/mount-buildbot-apexes.sh.
21# Make sure to keep these files in sync.
22
23green='\033[0;32m'
24nc='\033[0m'
25
26# Setup as root, as some actions performed here require it.
27adb root
28adb wait-for-device
29
30# Exit early if there is no chroot.
31[[ -n "$ART_TEST_CHROOT" ]] || exit
32
33# Check that ART_TEST_CHROOT is correctly defined.
34[[ "$ART_TEST_CHROOT" = /* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; }
35
36# Directory containing extracted APEX packages' payloads (ext4 images) under
37# the chroot directory.
38apex_image_dir="/tmp/apex"
39
40# deactivate_system_package APEX_NAME
41# -----------------------------------
42# Unmount APEX_NAME in `/apex` under the chroot directory and delete the
43# corresponding APEX package payload (ext4 image).
44deactivate_system_package() {
45  local apex_name=$1
46  local abs_image_filename="$ART_TEST_CHROOT$apex_image_dir/$apex_name.img"
47  local abs_mount_point="$ART_TEST_CHROOT/apex/$apex_name"
48
49  echo -e "${green}Deactivating package $apex_name${nc}"
50
51  # Unmount the package's payload (ext4 image).
52  if adb shell mount | grep -q "^/dev/block/loop[0-9]\+ on $abs_mount_point type ext4"; then
53    adb shell umount "$abs_mount_point"
54    adb shell rmdir "$abs_mount_point"
55    # Delete the ext4 image.
56    adb shell rm "$abs_image_filename"
57  fi
58}
59
60# Deactivate the Android Runtime APEX.
61deactivate_system_package com.android.runtime
62
63# Delete the image's directory.
64adb shell rmdir "$ART_TEST_CHROOT$apex_image_dir"
65