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# Mount Android Runtime and Core Libraries APEX packages required in the chroot directory.
18# This script emulates some the actions performed by `apexd`.
19
20green='\033[0;32m'
21nc='\033[0m'
22
23# Setup as root, as some actions performed here require it.
24adb root
25adb wait-for-device
26
27# Exit early if there is no chroot.
28[[ -n "$ART_TEST_CHROOT" ]] || exit
29
30# Check that ART_TEST_CHROOT is correctly defined.
31[[ "$ART_TEST_CHROOT" = /* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; }
32
33# Check that the "$ART_TEST_CHROOT/apex" directory exists.
34adb shell test -d "$ART_TEST_CHROOT/apex" \
35  || { echo "$ART_TEST_CHROOT/apex does not exist or is not a directory"; exit 1; }
36
37# Create a directory where we extract APEX packages' payloads (ext4 images)
38# under the chroot directory.
39apex_image_dir="/tmp/apex"
40adb shell mkdir -p "$ART_TEST_CHROOT$apex_image_dir"
41
42# activate_system_package APEX_PACKAGE APEX_NAME
43# ----------------------------------------------
44# Extract payload (ext4 image) from system APEX_PACKAGE and mount it as
45# APEX_NAME in `/apex` under the chroot directory.
46activate_system_package() {
47  local apex_package=$1
48  local apex_name=$2
49  local apex_package_path="/system/apex/$apex_package"
50  local abs_mount_point="$ART_TEST_CHROOT/apex/$apex_name"
51  local abs_image_filename="$ART_TEST_CHROOT$apex_image_dir/$apex_name.img"
52
53  # Make sure that the (absolute) path to the mounted ext4 image is less than
54  # 64 characters, which is a hard limit set in the kernel for loop device
55  # filenames (otherwise, we would get an error message from `losetup`, used
56  # by `mount` to manage the loop device).
57  [[ "${#abs_image_filename}" -ge 64 ]] \
58    && { echo "Filename $abs_image_filename is too long to be used with a loop device"; exit 1; }
59
60  echo -e "${green}Activating package $apex_package as $apex_name${nc}"
61
62  # Extract payload (ext4 image). As standard Android builds do not contain
63  # `unzip`, we use the one we built and sync'd to the chroot directory instead.
64  local payload_filename="apex_payload.img"
65  adb shell chroot "$ART_TEST_CHROOT" \
66    /system/bin/unzip -q "$apex_package_path" "$payload_filename" -d "$apex_image_dir"
67  # Rename the extracted payload to have its name match the APEX's name.
68  adb shell mv "$ART_TEST_CHROOT$apex_image_dir/$payload_filename" "$abs_image_filename"
69  # Check that the mount point is available.
70  adb shell mount | grep -q " on $abs_mount_point" && \
71    { echo "$abs_mount_point is already used as mount point"; exit 1; }
72  # Mount the ext4 image.
73  adb shell mkdir -p "$abs_mount_point"
74  adb shell mount -o loop,ro "$abs_image_filename" "$abs_mount_point"
75}
76
77# Activate the Android Runtime APEX.
78# Note: We use the Debug Runtime APEX (which is a superset of the Release Runtime APEX).
79activate_system_package com.android.runtime.debug.apex com.android.runtime
80