blob: 575b67f38310126104e665c2d5a00fcde5dfbe21 [file] [log] [blame]
Tom Cherry4590a2a2018-08-07 10:22:01 -07001/*
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 "switch_root.h"
18
Tom Cherry4590a2a2018-08-07 10:22:01 -070019#include <fcntl.h>
20#include <mntent.h>
21#include <sys/mount.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
25#include <android-base/logging.h>
26#include <android-base/strings.h>
27
28using android::base::StartsWith;
29
30using namespace std::literals;
31
32namespace android {
33namespace init {
34
35namespace {
36
Tom Cherry4590a2a2018-08-07 10:22:01 -070037std::vector<std::string> GetMounts(const std::string& new_root) {
38 auto fp = std::unique_ptr<std::FILE, decltype(&endmntent)>{setmntent("/proc/mounts", "re"),
39 endmntent};
40 if (fp == nullptr) {
41 PLOG(FATAL) << "Failed to open /proc/mounts";
42 }
43
44 std::vector<std::string> result;
45 mntent* mentry;
46 while ((mentry = getmntent(fp.get())) != nullptr) {
47 // We won't try to move rootfs.
48 if (mentry->mnt_dir == "/"s) {
49 continue;
50 }
51
52 // The new root mount is handled separately.
53 if (mentry->mnt_dir == new_root) {
54 continue;
55 }
56
57 // Move operates on subtrees, so do not try to move children of other mounts.
58 if (std::find_if(result.begin(), result.end(), [&mentry](const auto& older_mount) {
59 return StartsWith(mentry->mnt_dir, older_mount);
60 }) != result.end()) {
61 continue;
62 }
63
64 result.emplace_back(mentry->mnt_dir);
65 }
66
67 return result;
68}
69
70} // namespace
71
Tom Cherrye087a6a2018-11-09 18:47:26 +000072void SwitchRoot(const std::string& new_root) {
Tom Cherry4590a2a2018-08-07 10:22:01 -070073 auto mounts = GetMounts(new_root);
74
Tom Cherry866c08c2018-11-06 10:32:53 -080075 LOG(INFO) << "Switching root to '" << new_root << "'";
76
Tom Cherry4590a2a2018-08-07 10:22:01 -070077 for (const auto& mount_path : mounts) {
78 auto new_mount_path = new_root + mount_path;
Tom Cherry866c08c2018-11-06 10:32:53 -080079 mkdir(new_mount_path.c_str(), 0755);
Tom Cherry4590a2a2018-08-07 10:22:01 -070080 if (mount(mount_path.c_str(), new_mount_path.c_str(), nullptr, MS_MOVE, nullptr) != 0) {
81 PLOG(FATAL) << "Unable to move mount at '" << mount_path << "'";
82 }
83 }
84
Tom Cherry4590a2a2018-08-07 10:22:01 -070085 if (chdir(new_root.c_str()) != 0) {
86 PLOG(FATAL) << "Could not chdir to new_root, '" << new_root << "'";
87 }
88
Tom Cherrye087a6a2018-11-09 18:47:26 +000089 if (mount(new_root.c_str(), "/", nullptr, MS_MOVE, nullptr) != 0) {
90 PLOG(FATAL) << "Unable to move root mount to new_root, '" << new_root << "'";
Tom Cherry4590a2a2018-08-07 10:22:01 -070091 }
92
93 if (chroot(".") != 0) {
94 PLOG(FATAL) << "Unable to chroot to new root";
95 }
Tom Cherry4590a2a2018-08-07 10:22:01 -070096}
97
98} // namespace init
99} // namespace android