Skip to content

Commit

Permalink
Use /data as tmpfs mount point in 2SI setup
Browse files Browse the repository at this point in the history
Design credit to @yujincheng08
Close #5146. Fix #5491, fix #3752

Previously, Magisk changes the mount point from /system to /system_root
by patching fstab to prevent the original init from changing root.
The reason why we want to prevent the original init from switching the
root directory is because it will then be read-only, making patching
and injecting magiskinit into the boot chain difficult.

This commit (ab)uses the fact that the /data folder will never be part
of early mount (because it is handled very late in the boot by vold),
so that we can use it as the mount point of tmpfs to store files.

Some advantages of this method:

- No need to switch root manually
- No need to modify fstab, which significantly improves compatibility
  e.g. avoid hacks for weird devices like those using oplus.fstab,
  and avoid hacking init to bypass fstab in device trees
- Supports skip_mount.cfg
- Support DSU
  • Loading branch information
topjohnwu committed Mar 13, 2022
1 parent 9b60c00 commit 810d27a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 329 deletions.
4 changes: 1 addition & 3 deletions native/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ LOCAL_SRC_FILES := \
init/rootdir.cpp \
init/getinfo.cpp \
init/twostage.cpp \
core/socket.cpp \
magiskpolicy/sepolicy.cpp \
magiskpolicy/magiskpolicy.cpp \
magiskpolicy/rules.cpp \
magiskpolicy/policydb.cpp \
magiskpolicy/statement.cpp \
magiskboot/pattern.cpp
magiskpolicy/statement.cpp

LOCAL_LDFLAGS := -static
include $(BUILD_EXECUTABLE)
Expand Down
18 changes: 7 additions & 11 deletions native/jni/init/init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct fstab_entry {
fstab_entry(fstab_entry &&) = default;
fstab_entry &operator=(const fstab_entry&) = delete;
fstab_entry &operator=(fstab_entry&&) = default;
void to_file(FILE *fp);
};

#define INIT_SOCKET "MAGISKINIT"
Expand All @@ -44,7 +43,6 @@ extern std::vector<std::string> mount_list;

bool unxz(int fd, const uint8_t *buf, size_t size);
void load_kernel_info(BootConfig *config);
bool is_dsu();
bool check_two_stage();
void setup_klog();
const char *backup_init();
Expand Down Expand Up @@ -107,7 +105,6 @@ class SARBase : virtual public MagiskInit {
class FirstStageInit : public BaseInit {
private:
void prepare();
void get_default_fstab(char *buf, size_t len);
public:
FirstStageInit(char *argv[], BootConfig *cmd) : BaseInit(argv, cmd) {
LOGD("%s\n", __FUNCTION__);
Expand All @@ -124,17 +121,14 @@ class FirstStageInit : public BaseInit {

class SARInit : public SARBase {
private:
bool is_two_stage;

void early_mount();
bool early_mount();
void first_stage_prep();
public:
SARInit(char *argv[], BootConfig *cmd) : MagiskInit(argv, cmd), is_two_stage(false) {
SARInit(char *argv[], BootConfig *cmd) : MagiskInit(argv, cmd) {
LOGD("%s\n", __FUNCTION__);
};
void start() override {
early_mount();
if (is_two_stage)
if (early_mount())
first_stage_prep();
else
patch_rootdir();
Expand Down Expand Up @@ -178,8 +172,10 @@ class SecondStageInit : public RootFSBase, public SARBase {
};

void start() override {
if (prepare()) patch_rootfs();
else patch_rootdir();
if (prepare())
patch_rootfs();
else
patch_rootdir();
exec_init();
}
};
Expand Down
49 changes: 7 additions & 42 deletions native/jni/init/mount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,6 @@ static void switch_root(const string &path) {
frm_rf(root);
}

bool is_dsu() {
strcpy(blk_info.partname, "metadata");
xmkdir("/metadata", 0755);
if (setup_block(true) < 0 ||
xmount(blk_info.block_dev, "/metadata", "ext4", MS_RDONLY, nullptr)) {
PLOGE("Failed to mount /metadata");
return false;
} else {
run_finally f([]{ xumount2("/metadata", MNT_DETACH); });
constexpr auto dsu_status = "/metadata/gsi/dsu/install_status";
if (xaccess(dsu_status, F_OK) == 0) {
char status[PATH_MAX] = {0};
auto fp = xopen_file(dsu_status, "r");
fgets(status, sizeof(status), fp.get());
if (status == "ok"sv || status == "0"sv)
return true;
}
}
return false;
}

void MagiskInit::mount_rules_dir(const char *dev_base, const char *mnt_base) {
char path[128];
xrealpath(dev_base, blk_info.block_dev);
Expand Down Expand Up @@ -327,10 +306,14 @@ void RootFSInit::early_mount() {
void SARBase::backup_files() {
if (access("/overlay.d", F_OK) == 0)
backup_folder("/overlay.d", overlays);
else if (access("/data/overlay.d", F_OK) == 0)
backup_folder("/data/overlay.d", overlays);

self = mmap_data("/proc/self/exe");
if (access("/.backup/.magisk", R_OK) == 0)
magisk_config = mmap_data("/.backup/.magisk");
else if (access("/data/.backup/.magisk", R_OK) == 0)
magisk_config = mmap_data("/data/.backup/.magisk");
}

void SARBase::mount_system_root() {
Expand Down Expand Up @@ -367,13 +350,13 @@ void SARBase::mount_system_root() {
xmount("/dev/root", "/system_root", "erofs", MS_RDONLY, nullptr);
}

void SARInit::early_mount() {
bool SARInit::early_mount() {
backup_files();
mount_system_root();
switch_root("/system_root");

// Use the apex folder to determine whether 2SI (Android 10+)
is_two_stage = access("/apex", F_OK) == 0;
bool is_two_stage = access("/apex", F_OK) == 0;
LOGD("is_two_stage: [%d]\n", is_two_stage);

if (!is_two_stage) {
Expand All @@ -386,26 +369,8 @@ void SARInit::early_mount() {
#endif
mount_with_dt();
}
}

bool SecondStageInit::prepare() {
backup_files();

umount2("/init", MNT_DETACH);
umount2("/proc/self/exe", MNT_DETACH);

// some weird devices, like meizu, embrace two stage init but still have legacy rootfs behaviour
bool legacy = false;
if (access("/system_root", F_OK) == 0) {
if (access("/system_root/proc", F_OK) == 0) {
switch_root("/system_root");
} else {
xmount("/system_root", "/system", nullptr, MS_MOVE, nullptr);
rmdir("/system_root");
legacy = true;
}
}
return legacy;
return is_two_stage;
}

void BaseInit::exec_init() {
Expand Down
20 changes: 3 additions & 17 deletions native/jni/init/rootdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void SARBase::patch_rootdir() {
make_pair(SPLIT_PLAT_CIL, "xxx"), /* Force loading monolithic sepolicy */
make_pair(MONOPOLICY, sepol) /* Redirect /sepolicy to custom path */
});
if constexpr (avd_hack) {
if (avd_hack) {
// Force disable early mount on original init
init.patch({ make_pair("android,fstab", "xxx") });
}
Expand Down Expand Up @@ -276,23 +276,9 @@ void SARBase::patch_rootdir() {
// sepolicy
patch_sepolicy(sepol);

// Restore backup files
struct sockaddr_un sun;
int sockfd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (connect(sockfd, (struct sockaddr*) &sun, setup_sockaddr(&sun, INIT_SOCKET)) == 0) {
LOGD("ACK init daemon to write backup files\n");
// Let daemon know where tmp_dir is
write_string(sockfd, tmp_dir);
// Wait for daemon to finish restoring files
read_int(sockfd);
} else {
LOGD("Restore backup files locally\n");
restore_folder(ROOTOVL, overlays);
overlays.clear();
}
close(sockfd);

// Handle overlay.d
restore_folder(ROOTOVL, overlays);
overlays.clear();
load_overlay_rc(ROOTOVL);
if (access(ROOTOVL "/sbin", F_OK) == 0) {
// Move files in overlay.d/sbin into tmp_dir
Expand Down
Loading

0 comments on commit 810d27a

Please sign in to comment.