blob: 1372511a766a1d9e91def5113b506e45911a8ce6 [file] [log] [blame]
Justin Yun6bab0a92018-10-29 18:31:48 +09001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <dirent.h>
18#include <selinux/selinux.h>
19#include <sys/mount.h>
20#include <unistd.h>
21
22#include <memory>
23#include <string>
24#include <vector>
25
26#include <android-base/logging.h>
27#include <android-base/properties.h>
28#include <fs_mgr_overlayfs.h>
29#include <fs_mgr_vendor_overlay.h>
30#include <fstab/fstab.h>
31
32#include "fs_mgr_priv.h"
33
34using namespace std::literals;
35
36namespace {
37
Justin Yundc5a3272018-11-21 17:37:57 +090038// The order of the list means the priority to show the files in the directory.
39// The last one has the highest priority.
40const std::vector<const std::string> kVendorOverlaySourceDirs = {
41 "/system/vendor_overlay/",
42 "/product/vendor_overlay/",
43};
Justin Yun6bab0a92018-10-29 18:31:48 +090044const auto kVndkVersionPropertyName = "ro.vndk.version"s;
45const auto kVendorTopDir = "/vendor/"s;
46const auto kLowerdirOption = "lowerdir="s;
47
Justin Yundc5a3272018-11-21 17:37:57 +090048std::vector<std::pair<std::string, std::string>> fs_mgr_get_vendor_overlay_dirs(
49 const std::string& vndk_version) {
50 std::vector<std::pair<std::string, std::string>> vendor_overlay_dirs;
51 for (const auto& vendor_overlay_source : kVendorOverlaySourceDirs) {
52 const auto overlay_top = vendor_overlay_source + vndk_version;
53 std::unique_ptr<DIR, decltype(&closedir)> vendor_overlay_top(opendir(overlay_top.c_str()),
54 closedir);
55 if (!vendor_overlay_top) continue;
Justin Yun6bab0a92018-10-29 18:31:48 +090056
Justin Yundc5a3272018-11-21 17:37:57 +090057 // Vendor overlay root for current vendor version found!
58 LINFO << "vendor overlay root: " << overlay_top;
Justin Yun6bab0a92018-10-29 18:31:48 +090059
Justin Yundc5a3272018-11-21 17:37:57 +090060 struct dirent* dp;
61 while ((dp = readdir(vendor_overlay_top.get())) != nullptr) {
62 if (dp->d_type != DT_DIR || dp->d_name[0] == '.') {
63 continue;
64 }
65 vendor_overlay_dirs.emplace_back(overlay_top, dp->d_name);
Justin Yun6bab0a92018-10-29 18:31:48 +090066 }
Justin Yun6bab0a92018-10-29 18:31:48 +090067 }
Justin Yun6bab0a92018-10-29 18:31:48 +090068 return vendor_overlay_dirs;
69}
70
Justin Yundc5a3272018-11-21 17:37:57 +090071bool fs_mgr_vendor_overlay_mount(const std::pair<std::string, std::string>& mount_point) {
72 const auto [overlay_top, mount_dir] = mount_point;
73 const auto vendor_mount_point = kVendorTopDir + mount_dir;
Justin Yun6bab0a92018-10-29 18:31:48 +090074 LINFO << "vendor overlay mount on " << vendor_mount_point;
75
Justin Yunad550052018-11-02 17:45:24 +090076 const auto target_context = fs_mgr_get_context(vendor_mount_point);
77 if (target_context.empty()) {
78 PERROR << " failed: cannot find the target vendor mount point";
79 return false;
80 }
Justin Yundc5a3272018-11-21 17:37:57 +090081 const auto source_directory = overlay_top + "/" + mount_dir;
Justin Yunad550052018-11-02 17:45:24 +090082 const auto source_context = fs_mgr_get_context(source_directory);
83 if (target_context != source_context) {
84 LERROR << " failed: source and target contexts do not match (source:" << source_context
85 << ", target:" << target_context << ")";
Justin Yun6bab0a92018-10-29 18:31:48 +090086 return false;
87 }
88
Mark Salyzyn6a116942018-11-05 08:53:30 -080089 auto options = kLowerdirOption + source_directory + ":" + vendor_mount_point;
90 if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kOverrideCredsRequired) {
91 options += ",override_creds=off";
92 }
Justin Yun6bab0a92018-10-29 18:31:48 +090093 auto report = "__mount(source=overlay,target="s + vendor_mount_point + ",type=overlay," +
94 options + ")=";
Yi-Yo Chiang7cbed5a2021-05-18 17:11:35 +080095 auto ret = mount("overlay", vendor_mount_point.c_str(), "overlay", MS_RDONLY | MS_NOATIME,
Justin Yun6bab0a92018-10-29 18:31:48 +090096 options.c_str());
97 if (ret) {
98 PERROR << report << ret;
99 return false;
100 } else {
101 LINFO << report << ret;
102 return true;
103 }
104}
105
106} // namespace
107
108// Since the vendor overlay requires to know the version of the vendor partition,
109// it is not possible to mount vendor overlay at the first stage that cannot
110// initialize properties.
111// To read the properties, vendor overlay must be mounted at the second stage, right
112// after "property_load_boot_defaults()" is called.
113bool fs_mgr_vendor_overlay_mount_all() {
Justin Yundc5a3272018-11-21 17:37:57 +0900114 // To read the property, it must be called at the second init stage after the default
115 // properties are loaded.
116 static const auto vndk_version = android::base::GetProperty(kVndkVersionPropertyName, "");
117 if (vndk_version.empty()) {
Justin Yun6bab0a92018-10-29 18:31:48 +0900118 LINFO << "vendor overlay: vndk version not defined";
119 return false;
120 }
Justin Yundc5a3272018-11-21 17:37:57 +0900121
122 const auto vendor_overlay_dirs = fs_mgr_get_vendor_overlay_dirs(vndk_version);
Justin Yun6bab0a92018-10-29 18:31:48 +0900123 if (vendor_overlay_dirs.empty()) return true;
Mark Salyzyn6a116942018-11-05 08:53:30 -0800124 if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) {
Justin Yun6bab0a92018-10-29 18:31:48 +0900125 LINFO << "vendor overlay: kernel does not support overlayfs";
126 return false;
127 }
128
Justin Yundc5a3272018-11-21 17:37:57 +0900129 // Mount each directory in /(system|product)/vendor_overlay/<ver> on /vendor
Justin Yun6bab0a92018-10-29 18:31:48 +0900130 auto ret = true;
131 for (const auto& vendor_overlay_dir : vendor_overlay_dirs) {
Justin Yundc5a3272018-11-21 17:37:57 +0900132 if (!fs_mgr_vendor_overlay_mount(vendor_overlay_dir)) {
Justin Yun6bab0a92018-10-29 18:31:48 +0900133 ret = false;
134 }
135 }
136 return ret;
137}