diff --git a/fs_mgr/Android.bp b/fs_mgr/Android.bp index 0af615905..4441ad0c1 100644 --- a/fs_mgr/Android.bp +++ b/fs_mgr/Android.bp @@ -22,7 +22,6 @@ cc_defaults { local_include_dirs: ["include/"], cppflags: ["-Werror"], static_libs: [ - "liblogwrap", "libfec", "libfec_rs", "libbase", @@ -33,6 +32,9 @@ cc_defaults { "libselinux", "libavb", ], + whole_static_libs: [ + "liblogwrap", + ], } cc_library_static { diff --git a/fs_mgr/Android.mk b/fs_mgr/Android.mk index f3ca7246f..924934308 100644 --- a/fs_mgr/Android.mk +++ b/fs_mgr/Android.mk @@ -21,6 +21,7 @@ LOCAL_SRC_FILES:= fs_mgr_main.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE:= fs_mgr LOCAL_MODULE_TAGS := optional +LOCAL_REQUIRED_MODULES := mke2fs mke2fs.conf e2fsdroid LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)/sbin LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED) diff --git a/fs_mgr/fs_mgr_format.cpp b/fs_mgr/fs_mgr_format.cpp index a03d92c6b..75feee798 100644 --- a/fs_mgr/fs_mgr_format.cpp +++ b/fs_mgr/fs_mgr_format.cpp @@ -24,25 +24,21 @@ #include #include -#include #include -#include -#include -#include +#include +#include #include +#include +#include #include "fs_mgr_priv.h" #include "cryptfs.h" -extern "C" { -extern struct fs_info info; /* magic global from ext4_utils */ -extern void reset_ext4fs_info(); -} - static int format_ext4(char *fs_blkdev, char *fs_mnt_point, bool crypt_footer) { uint64_t dev_sz; int fd, rc = 0; + int status; if ((fd = open(fs_blkdev, O_WRONLY)) < 0) { PERROR << "Cannot open block device"; @@ -55,30 +51,36 @@ static int format_ext4(char *fs_blkdev, char *fs_mnt_point, bool crypt_footer) return -1; } - struct selabel_handle *sehandle = selinux_android_file_context_handle(); - if (!sehandle) { - /* libselinux logs specific error */ - LERROR << "Cannot initialize android file_contexts"; - close(fd); - return -1; - } - - /* Format the partition using the calculated length */ - reset_ext4fs_info(); - info.len = (off64_t)dev_sz; - if (crypt_footer) { - info.len -= CRYPT_FOOTER_OFFSET; - } - - /* Use make_ext4fs_internal to avoid wiping an already-wiped partition. */ - rc = make_ext4fs_internal(fd, NULL, NULL, fs_mnt_point, 0, 0, 0, 0, 0, 0, sehandle, 0, 0, NULL, NULL, NULL); - if (rc) { - LERROR << "make_ext4fs returned " << rc; - } close(fd); - if (sehandle) { - selabel_close(sehandle); + /* Format the partition using the calculated length */ + if (crypt_footer) { + dev_sz -= CRYPT_FOOTER_OFFSET; + } + + std::string size_str = std::to_string(dev_sz / 4096); + const char* const mke2fs_args[] = { + "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", fs_blkdev, size_str.c_str(), nullptr}; + + rc = android_fork_execvp_ext(arraysize(mke2fs_args), const_cast(mke2fs_args), &status, + true, LOG_KLOG, true, nullptr, nullptr, 0); + if (rc) { + LERROR << "mke2fs returned " << rc; + return rc; + } + + const char* const e2fsdroid_args[] = { + "/system/bin/e2fsdroid", + "-e", + "-a", + fs_mnt_point, + fs_blkdev, + nullptr}; + + rc = android_fork_execvp_ext(arraysize(e2fsdroid_args), const_cast(e2fsdroid_args), + &status, true, LOG_KLOG, true, nullptr, nullptr, 0); + if (rc) { + LERROR << "e2fsdroid returned " << rc; } return rc; @@ -86,44 +88,11 @@ static int format_ext4(char *fs_blkdev, char *fs_mnt_point, bool crypt_footer) static int format_f2fs(char *fs_blkdev) { - char * args[5]; - int pid; - int rc = 0; + int status; + const char* const args[] = {"/system/bin/make_f2fs", "-f", "-O encrypt", fs_blkdev, nullptr}; - args[0] = (char *)"/system/bin/make_f2fs"; - args[1] = (char *)"-f"; - args[2] = (char *)"-O encrypt"; - args[3] = fs_blkdev; - args[4] = (char *)0; - - pid = fork(); - if (pid < 0) { - return pid; - } - if (!pid) { - /* This doesn't return */ - execv(args[0], args); - exit(1); - } - for(;;) { - pid_t p = waitpid(pid, &rc, 0); - if (p != pid) { - LERROR << "Error waiting for child process - " << p; - rc = -1; - break; - } - if (WIFEXITED(rc)) { - rc = WEXITSTATUS(rc); - LINFO << args[0] << " done, status " << rc; - if (rc) { - rc = -1; - } - break; - } - LERROR << "Still waiting for " << args[0] << "..."; - } - - return rc; + return android_fork_execvp_ext(arraysize(args), const_cast(args), &status, true, + LOG_KLOG, true, nullptr, nullptr, 0); } int fs_mgr_do_format(struct fstab_rec *fstab, bool crypt_footer)