Mount persist partition mirror pre-init

This commit is contained in:
topjohnwu 2019-12-09 02:44:49 -05:00
parent d22b9c26b6
commit 08177c3dd8
3 changed files with 38 additions and 14 deletions

View File

@ -162,3 +162,4 @@ void load_kernel_info(cmdline *cmd);
int dump_magisk(const char *path, mode_t mode); int dump_magisk(const char *path, mode_t mode);
int magisk_proxy_main(int argc, char *argv[]); int magisk_proxy_main(int argc, char *argv[]);
void setup_klog(); void setup_klog();
void mount_sbin();

View File

@ -6,6 +6,7 @@
#include <utils.h> #include <utils.h>
#include <logging.h> #include <logging.h>
#include <selinux.h> #include <selinux.h>
#include <magisk.h>
#include "init.h" #include "init.h"
@ -42,12 +43,11 @@ static void parse_device(devinfo *dev, const char *uevent) {
static void collect_devices() { static void collect_devices() {
char path[128]; char path[128];
struct dirent *entry;
devinfo dev{}; devinfo dev{};
DIR *dir = xopendir("/sys/dev/block"); DIR *dir = xopendir("/sys/dev/block");
if (dir == nullptr) if (dir == nullptr)
return; return;
while ((entry = readdir(dir))) { for (dirent *entry; (entry = readdir(dir));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv) if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue; continue;
sprintf(path, "/sys/dev/block/%s/uevent", entry->d_name); sprintf(path, "/sys/dev/block/%s/uevent", entry->d_name);
@ -57,21 +57,21 @@ static void collect_devices() {
closedir(dir); closedir(dir);
} }
static int64_t setup_block(char *block_dev = nullptr) { static int64_t setup_block(bool write_block = true) {
if (dev_list.empty()) if (dev_list.empty())
collect_devices(); collect_devices();
xmkdir("/dev", 0755); xmkdir("/dev", 0755);
xmkdir("/dev/block", 0755);
for (int tries = 0; tries < 3; ++tries) { for (int tries = 0; tries < 3; ++tries) {
for (auto &dev : dev_list) { for (auto &dev : dev_list) {
if (strcasecmp(dev.partname, partname) == 0) { if (strcasecmp(dev.partname, partname) == 0) {
if (block_dev) { if (write_block) {
sprintf(block_dev, "/dev/block/%s", dev.devname); sprintf(block_dev, "/dev/block/%s", dev.devname);
xmkdir("/dev/block", 0755);
} }
LOGD("Found %s: [%s] (%d, %d)\n", dev.partname, dev.devname, dev.major, dev.minor); LOGD("Found %s: [%s] (%d, %d)\n", dev.partname, dev.devname, dev.major, dev.minor);
dev_t rdev = makedev(dev.major, dev.minor); dev_t rdev = makedev(dev.major, dev.minor);
mknod(block_dev ? block_dev : "/dev/root", S_IFBLK | 0600, rdev); mknod(block_dev, S_IFBLK | 0600, rdev);
return rdev; return rdev;
} }
} }
@ -119,7 +119,7 @@ if (is_lnk("/system_root" name)) \
#define mount_root(name) \ #define mount_root(name) \
if (!is_lnk("/" #name) && read_dt_fstab(cmd, #name)) { \ if (!is_lnk("/" #name) && read_dt_fstab(cmd, #name)) { \
LOGD("Early mount " #name "\n"); \ LOGD("Early mount " #name "\n"); \
setup_block(block_dev); \ setup_block(); \
xmkdir("/" #name, 0755); \ xmkdir("/" #name, 0755); \
xmount(block_dev, "/" #name, fstype, MS_RDONLY, nullptr); \ xmount(block_dev, "/" #name, fstype, MS_RDONLY, nullptr); \
mount_list.emplace_back("/" #name); \ mount_list.emplace_back("/" #name); \
@ -147,7 +147,7 @@ void SARCompatInit::early_mount() {
LOGD("Early mount system_root\n"); LOGD("Early mount system_root\n");
sprintf(partname, "system%s", cmd->slot); sprintf(partname, "system%s", cmd->slot);
setup_block(block_dev); setup_block();
xmkdir("/system_root", 0755); xmkdir("/system_root", 0755);
if (xmount(block_dev, "/system_root", "ext4", MS_RDONLY, nullptr)) if (xmount(block_dev, "/system_root", "ext4", MS_RDONLY, nullptr))
xmount(block_dev, "/system_root", "erofs", MS_RDONLY, nullptr); xmount(block_dev, "/system_root", "erofs", MS_RDONLY, nullptr);
@ -209,7 +209,8 @@ void SARInit::early_mount() {
LOGD("Early mount system_root\n"); LOGD("Early mount system_root\n");
sprintf(partname, "system%s", cmd->slot); sprintf(partname, "system%s", cmd->slot);
auto dev = setup_block(); strcpy(block_dev, "/dev/root");
auto dev = setup_block(false);
if (dev < 0) { if (dev < 0) {
// Try NVIDIA naming scheme // Try NVIDIA naming scheme
strcpy(partname, "APP"); strcpy(partname, "APP");
@ -260,3 +261,28 @@ void BaseInit::cleanup() {
umount(p.data()); umount(p.data());
} }
} }
void mount_sbin() {
LOGD("Mount /sbin tmpfs overlay\n");
xmount("tmpfs", "/sbin", "tmpfs", 0, "mode=755");
xmkdir(MAGISKTMP, 0755);
xmkdir(MIRRDIR, 0);
xmkdir(BLOCKDIR, 0);
// Mount persist partition
strcpy(partname, "persist");
strcpy(block_dev, BLOCKDIR "/persist");
const char *mnt_point = MIRRDIR "/persist";
if (setup_block(false) < 0) {
// Fallback to cache
strcpy(partname, "cache");
strcpy(block_dev, BLOCKDIR "/cache");
if (setup_block(false) < 0)
return;
mnt_point = MIRRDIR "/cache";
xsymlink("./cache", MIRRDIR "/persist");
}
xmkdir(mnt_point, 0755);
xmount(block_dev, mnt_point, "ext4", 0, nullptr);
}

View File

@ -192,8 +192,7 @@ constexpr const char wrapper[] =
; ;
static void sbin_overlay(const raw_data &self, const raw_data &config) { static void sbin_overlay(const raw_data &self, const raw_data &config) {
LOGD("Mount /sbin tmpfs overlay\n"); mount_sbin();
xmount("tmpfs", "/sbin", "tmpfs", 0, "mode=755");
// Dump binaries // Dump binaries
xmkdir(MAGISKTMP, 0755); xmkdir(MAGISKTMP, 0755);
@ -302,9 +301,7 @@ void SARBase::patch_rootdir() {
sbin_overlay(self, config); sbin_overlay(self, config);
// Mount system_root mirror // Mount system_root mirror
xmkdir(MIRRDIR, 0); xmkdir(ROOTMIR, 0755);
xmkdir(ROOTMIR, 0);
xmkdir(BLOCKDIR, 0);
mknod(ROOTBLK, S_IFBLK | 0600, system_dev); mknod(ROOTBLK, S_IFBLK | 0600, system_dev);
if (xmount(ROOTBLK, ROOTMIR, "ext4", MS_RDONLY, nullptr)) if (xmount(ROOTBLK, ROOTMIR, "ext4", MS_RDONLY, nullptr))
xmount(ROOTBLK, ROOTMIR, "erofs", MS_RDONLY, nullptr); xmount(ROOTBLK, ROOTMIR, "erofs", MS_RDONLY, nullptr);