Merge "roots.cpp: Clean up format_volume()."

am: 8eef26cf3d

Change-Id: I0cf7d7cf82f4e14567b585b2603ba387a8f94231
This commit is contained in:
Tao Bao
2017-07-27 17:38:45 +00:00
committed by android-build-merger
+63 -71
View File
@@ -25,6 +25,10 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <algorithm>
#include <string>
#include <vector>
#include <android-base/logging.h> #include <android-base/logging.h>
#include <android-base/properties.h> #include <android-base/properties.h>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
@@ -152,16 +156,24 @@ int ensure_path_unmounted(const char* path) {
return unmount_mounted_volume(mv); return unmount_mounted_volume(mv);
} }
static int exec_cmd(const char* path, char* const argv[]) { static int exec_cmd(const std::vector<std::string>& args) {
int status; CHECK_NE(static_cast<size_t>(0), args.size());
std::vector<char*> argv(args.size());
std::transform(args.cbegin(), args.cend(), argv.begin(),
[](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
argv.push_back(nullptr);
pid_t child; pid_t child;
if ((child = vfork()) == 0) { if ((child = vfork()) == 0) {
execv(path, argv); execv(argv[0], argv.data());
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
int status;
waitpid(child, &status, 0); waitpid(child, &status, 0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status); LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
} }
return WEXITSTATUS(status); return WEXITSTATUS(status);
} }
@@ -184,13 +196,12 @@ static ssize_t get_file_size(int fd, uint64_t reserve_len) {
} }
int format_volume(const char* volume, const char* directory) { int format_volume(const char* volume, const char* directory) {
Volume* v = volume_for_path(volume); const Volume* v = volume_for_path(volume);
if (v == NULL) { if (v == nullptr) {
LOG(ERROR) << "unknown volume \"" << volume << "\""; LOG(ERROR) << "unknown volume \"" << volume << "\"";
return -1; return -1;
} }
if (strcmp(v->fs_type, "ramdisk") == 0) { if (strcmp(v->fs_type, "ramdisk") == 0) {
// you can't format the ramdisk.
LOG(ERROR) << "can't format_volume \"" << volume << "\""; LOG(ERROR) << "can't format_volume \"" << volume << "\"";
return -1; return -1;
} }
@@ -198,20 +209,22 @@ int format_volume(const char* volume, const char* directory) {
LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume"; LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
return -1; return -1;
} }
if (ensure_path_unmounted(volume) != 0) { if (ensure_path_unmounted(volume) != 0) {
LOG(ERROR) << "format_volume failed to unmount \"" << v->mount_point << "\""; LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
return -1;
}
if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
return -1; return -1;
} }
if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) { // If there's a key_loc that looks like a path, it should be a block device for storing encryption
// if there's a key_loc that looks like a path, it should be a // metadata. Wipe it too.
// block device for storing encryption metadata. wipe it too. if (v->key_loc != nullptr && v->key_loc[0] == '/') {
if (v->key_loc != NULL && v->key_loc[0] == '/') { LOG(INFO) << "Wiping " << v->key_loc;
LOG(INFO) << "wiping " << v->key_loc;
int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644); int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
if (fd < 0) { if (fd == -1) {
LOG(ERROR) << "format_volume: failed to open " << v->key_loc; PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
return -1; return -1;
} }
wipe_block_device(fd, get_file_size(fd)); wipe_block_device(fd, get_file_size(fd));
@@ -221,9 +234,9 @@ int format_volume(const char* volume, const char* directory) {
ssize_t length = 0; ssize_t length = 0;
if (v->length != 0) { if (v->length != 0) {
length = v->length; length = v->length;
} else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) { } else if (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0) {
android::base::unique_fd fd(open(v->blk_device, O_RDONLY)); android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
if (fd < 0) { if (fd == -1) {
PLOG(ERROR) << "get_file_size: failed to open " << v->blk_device; PLOG(ERROR) << "get_file_size: failed to open " << v->blk_device;
return -1; return -1;
} }
@@ -233,84 +246,63 @@ int format_volume(const char* volume, const char* directory) {
return -1; return -1;
} }
} }
int result;
if (strcmp(v->fs_type, "ext4") == 0) { if (strcmp(v->fs_type, "ext4") == 0) {
static constexpr int block_size = 4096; static constexpr int kBlockSize = 4096;
int raid_stride = v->logical_blk_size / block_size; std::vector<std::string> mke2fs_args = {
int raid_stripe_width = v->erase_blk_size / block_size; "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
};
// stride should be the max of 8kb and logical block size int raid_stride = v->logical_blk_size / kBlockSize;
int raid_stripe_width = v->erase_blk_size / kBlockSize;
// stride should be the max of 8KB and logical block size
if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) { if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
raid_stride = 8192 / block_size; raid_stride = 8192 / kBlockSize;
} }
const char* mke2fs_argv[] = { "/sbin/mke2fs_static",
"-F",
"-t",
"ext4",
"-b",
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr };
int i = 5;
std::string block_size_str = std::to_string(block_size);
mke2fs_argv[i++] = block_size_str.c_str();
std::string ext_args;
if (v->erase_blk_size != 0 && v->logical_blk_size != 0) { if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
ext_args = android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, mke2fs_args.push_back("-E");
raid_stripe_width); mke2fs_args.push_back(
mke2fs_argv[i++] = "-E"; android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
mke2fs_argv[i++] = ext_args.c_str();
} }
mke2fs_args.push_back(v->blk_device);
mke2fs_argv[i++] = v->blk_device;
std::string size_str = std::to_string(length / block_size);
if (length != 0) { if (length != 0) {
mke2fs_argv[i++] = size_str.c_str(); mke2fs_args.push_back(std::to_string(length / kBlockSize));
} }
result = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv)); int result = exec_cmd(mke2fs_args);
if (result == 0 && directory != nullptr) { if (result == 0 && directory != nullptr) {
const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static", std::vector<std::string> e2fsdroid_args = {
"/sbin/e2fsdroid_static",
"-e", "-e",
"-f", "-f",
directory, directory,
"-a", "-a",
volume, volume,
v->blk_device, v->blk_device,
nullptr }; };
result = exec_cmd(e2fsdroid_args);
}
result = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
}
} else { /* Has to be f2fs because we checked earlier. */
char *num_sectors = nullptr;
if (length >= 512 && asprintf(&num_sectors, "%zd", length / 512) <= 0) {
LOG(ERROR) << "format_volume: failed to create " << v->fs_type
<< " command for " << v->blk_device;
return -1;
}
const char *f2fs_path = "/sbin/mkfs.f2fs";
const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, nullptr};
result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
free(num_sectors);
}
if (result != 0) { if (result != 0) {
PLOG(ERROR) << "format_volume: make " << v->fs_type << " failed on " << v->blk_device; PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
return -1; return -1;
} }
return 0; return 0;
} }
LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported"; // Has to be f2fs because we checked earlier.
std::vector<std::string> f2fs_args = { "/sbin/mkfs.f2fs", "-t", "-d1", v->blk_device };
if (length >= 512) {
f2fs_args.push_back(std::to_string(length / 512));
}
int result = exec_cmd(f2fs_args);
if (result != 0) {
PLOG(ERROR) << "format_volume: Failed to make f2fs on " << v->blk_device;
return -1; return -1;
} }
return 0;
}
int format_volume(const char* volume) { int format_volume(const char* volume) {
return format_volume(volume, nullptr); return format_volume(volume, nullptr);