From edee8361d75715b9bc020d76fa8f38b005a6e912 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 16 May 2018 13:43:22 -0700 Subject: [PATCH] recovery: add --fsck_unshare_blocks option for adb remount Allow "adb remount" on deduplicated filesystems to reboot into recovery and run e2fsck to undo deduplication. The e2fsck binary is copied from the system partition into tmpfs, and the system partition is unmounted so e2fsck can run safely. Bug: 64109868 Test: recovery with --fsck_unshare_blocks; adb remount Change-Id: I7558749b018b58f3c4339e51a95831dbd5be1ae3 --- Android.mk | 8 ++ fsck_unshare_blocks.cpp | 163 ++++++++++++++++++++++++++++++++++++++++ fsck_unshare_blocks.h | 22 ++++++ recovery.cpp | 11 ++- 4 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 fsck_unshare_blocks.cpp create mode 100644 fsck_unshare_blocks.h diff --git a/Android.mk b/Android.mk index 6aa91ea2..24da8b28 100644 --- a/Android.mk +++ b/Android.mk @@ -132,6 +132,7 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ adb_install.cpp \ + fsck_unshare_blocks.cpp \ fuse_sdcard_provider.cpp \ install.cpp \ recovery.cpp \ @@ -192,6 +193,13 @@ LOCAL_REQUIRED_MODULES += \ endif endif +# e2fsck is needed for adb remount -R. +ifeq ($(BOARD_EXT4_SHARE_DUP_BLOCKS),true) +ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT))) +LOCAL_REQUIRED_MODULES += e2fsck_static +endif +endif + ifeq ($(BOARD_CACHEIMAGE_PARTITION_SIZE),) LOCAL_REQUIRED_MODULES += \ recovery-persist \ diff --git a/fsck_unshare_blocks.cpp b/fsck_unshare_blocks.cpp new file mode 100644 index 00000000..a100368e --- /dev/null +++ b/fsck_unshare_blocks.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fsck_unshare_blocks.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "roots.h" + +static constexpr const char* SYSTEM_E2FSCK_BIN = "/system/bin/e2fsck_static"; +static constexpr const char* TMP_E2FSCK_BIN = "/tmp/e2fsck.bin"; + +static bool copy_file(const char* source, const char* dest) { + android::base::unique_fd source_fd(open(source, O_RDONLY)); + if (source_fd < 0) { + PLOG(ERROR) << "open %s failed" << source; + return false; + } + + android::base::unique_fd dest_fd(open(dest, O_CREAT | O_WRONLY, S_IRWXU)); + if (dest_fd < 0) { + PLOG(ERROR) << "open %s failed" << dest; + return false; + } + + for (;;) { + char buf[4096]; + ssize_t rv = read(source_fd, buf, sizeof(buf)); + if (rv < 0) { + PLOG(ERROR) << "read failed"; + return false; + } + if (rv == 0) { + break; + } + if (write(dest_fd, buf, rv) != rv) { + PLOG(ERROR) << "write failed"; + return false; + } + } + return true; +} + +static bool run_e2fsck(const std::string& partition) { + Volume* volume = volume_for_mount_point(partition); + if (!volume) { + LOG(INFO) << "No fstab entry for " << partition << ", skipping."; + return true; + } + + LOG(INFO) << "Running e2fsck on device " << volume->blk_device; + + std::vector args = { TMP_E2FSCK_BIN, "-p", "-E", "unshare_blocks", + volume->blk_device }; + std::vector argv(args.size()); + std::transform(args.cbegin(), args.cend(), argv.begin(), + [](const std::string& arg) { return const_cast(arg.c_str()); }); + argv.push_back(nullptr); + + pid_t child; + char* env[] = { nullptr }; + if (posix_spawn(&child, argv[0], nullptr, nullptr, argv.data(), env)) { + PLOG(ERROR) << "posix_spawn failed"; + return false; + } + + int status = 0; + int ret = TEMP_FAILURE_RETRY(waitpid(child, &status, 0)); + if (ret < 0) { + PLOG(ERROR) << "waitpid failed"; + return false; + } + if (!WIFEXITED(status)) { + LOG(ERROR) << "e2fsck exited abnormally: " << status; + return false; + } + int return_code = WEXITSTATUS(status); + if (return_code >= 8) { + LOG(ERROR) << "e2fsck could not unshare blocks: " << return_code; + return false; + } + + LOG(INFO) << "Successfully unshared blocks on " << partition; + return true; +} + +static const char* get_system_root() { + if (android::base::GetBoolProperty("ro.build.system_root_image", false)) { + return "/system_root"; + } else { + return "/system"; + } +} + +bool do_fsck_unshare_blocks() { + // List of partitions we will try to e2fsck -E unshare_blocks. + std::vector partitions = { "/odm", "/oem", "/product", "/vendor" }; + + // Temporarily mount system so we can copy e2fsck_static. + bool mounted = false; + if (android::base::GetBoolProperty("ro.build.system_root_image", false)) { + mounted = ensure_path_mounted_at("/", "/system_root") != -1; + partitions.push_back("/"); + } else { + mounted = ensure_path_mounted("/system") != -1; + partitions.push_back("/system"); + } + if (!mounted) { + LOG(ERROR) << "Failed to mount system image."; + return false; + } + if (!copy_file(SYSTEM_E2FSCK_BIN, TMP_E2FSCK_BIN)) { + LOG(ERROR) << "Could not copy e2fsck to /tmp."; + return false; + } + if (umount(get_system_root()) < 0) { + PLOG(ERROR) << "umount failed"; + return false; + } + + bool ok = true; + for (const auto& partition : partitions) { + ok &= run_e2fsck(partition); + } + + if (ok) { + LOG(INFO) << "Finished running e2fsck."; + } else { + LOG(ERROR) << "Finished running e2fsck, but not all partitions succceeded."; + } + return ok; +} diff --git a/fsck_unshare_blocks.h b/fsck_unshare_blocks.h new file mode 100644 index 00000000..9de8ef9a --- /dev/null +++ b/fsck_unshare_blocks.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _FILESYSTEM_CMDS_H +#define _FILESYSTEM_CMDS_H + +bool do_fsck_unshare_blocks(); + +#endif // _FILESYSTEM_CMDS_H diff --git a/recovery.cpp b/recovery.cpp index 56b2567d..98cbfed2 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -55,6 +55,7 @@ #include "adb_install.h" #include "common.h" #include "device.h" +#include "fsck_unshare_blocks.h" #include "fuse_sdcard_provider.h" #include "fuse_sideload.h" #include "install.h" @@ -969,6 +970,7 @@ Device::BuiltinAction start_recovery(Device* device, const std::vector(arg.c_str()); }); static constexpr struct option OPTIONS[] = { + { "fsck_unshare_blocks", no_argument, nullptr, 0 }, { "just_exit", no_argument, nullptr, 'x' }, { "locale", required_argument, nullptr, 0 }, { "prompt_and_wipe_data", no_argument, nullptr, 0 }, @@ -997,6 +999,7 @@ Device::BuiltinAction start_recovery(Device* device, const std::vectorPrint("Rebooting automatically.\n"); } + } else if (fsck_unshare_blocks) { + if (!do_fsck_unshare_blocks()) { + status = INSTALL_ERROR; + } } else if (!just_exit) { // If this is an eng or userdebug build, automatically turn on the text display if no command // is specified. Note that this should be called before setting the background to avoid