Improve image merging

This commit is contained in:
topjohnwu 2017-06-11 16:51:44 +08:00
parent 4e801788d7
commit aced0632ec
2 changed files with 49 additions and 30 deletions

View File

@ -102,14 +102,14 @@ static int merge_img(const char *source, const char *target) {
// resize target to worst case // resize target to worst case
int s_used, s_total, t_used, t_total, n_total; int s_used, s_total, t_used, t_total, n_total;
get_img_size(source, &s_used, &s_total); if (get_img_size(source, &s_used, &s_total)) return 1;
get_img_size(target, &t_used, &t_total); if (get_img_size(target, &t_used, &t_total)) return 1;
n_total = round_size(s_used + t_used); n_total = round_size(s_used + t_used);
if (n_total != t_total && resize_img(target, n_total)) if (n_total != t_total && resize_img(target, n_total))
return 1; return 1;
xmkdir("/cache/source", 0755); mkdir("/cache/source", 0755);
xmkdir("/cache/target", 0755); mkdir("/cache/target", 0755);
char *s_loop, *t_loop; char *s_loop, *t_loop;
s_loop = mount_image(source, "/cache/source"); s_loop = mount_image(source, "/cache/source");
if (s_loop == NULL) return 1; if (s_loop == NULL) return 1;
@ -130,8 +130,10 @@ static int merge_img(const char *source, const char *target) {
// Cleanup old module // Cleanup old module
snprintf(buf, PATH_MAX, "/cache/target/%s", entry->d_name); snprintf(buf, PATH_MAX, "/cache/target/%s", entry->d_name);
if (access(buf, F_OK) == 0) { if (access(buf, F_OK) == 0) {
LOGI("merge module %s\n", entry->d_name); LOGI("Upgrade module: %s\n", entry->d_name);
rm_rf(buf); rm_rf(buf);
} else {
LOGI("New module: %s\n", entry->d_name);
} }
} }
} }
@ -523,19 +525,23 @@ void post_fs_data(int client) {
} }
// Magisk Manual Injector support // Magisk Manual Injector support
if (access("/data/local/tmp/magisk", F_OK) == 0) { if (access("/data/local/tmp/magisk_inject", F_OK) == 0) {
rm_rf(DATABIN); rm_rf(DATABIN);
rename("/data/local/tmp/magisk", DATABIN); rename("/data/local/tmp/magisk_inject", DATABIN);
} }
// Lazy.... use shell blob // Lazy.... use shell blob
system("mv /data/magisk/stock_boot* /data;"); system("mv /data/magisk/stock_boot* /data;");
// Merge images // Merge images
if (merge_img("/cache/magisk.img", MAINIMG)) if (merge_img("/cache/magisk.img", MAINIMG)) {
LOGE("Image merge %s -> %s failed!\n", "/cache/magisk.img", MAINIMG);
goto unblock; goto unblock;
if (merge_img("/data/magisk_merge.img", MAINIMG)) }
if (merge_img("/data/magisk_merge.img", MAINIMG)) {
LOGE("Image merge %s -> %s failed!\n", "/data/magisk_merge.img", MAINIMG);
goto unblock; goto unblock;
}
int new_img = 0; int new_img = 0;
@ -735,12 +741,19 @@ void late_start(int client) {
// Run scripts after full patch, most reliable way to run scripts // Run scripts after full patch, most reliable way to run scripts
LOGI("* Running service.d scripts\n"); LOGI("* Running service.d scripts\n");
exec_common_script("service"); exec_common_script("service");
// Core only mode
if (access(DISABLEFILE, F_OK) == 0) {
setprop("ro.magisk.disable", "1");
return;
}
LOGI("* Running module service scripts\n"); LOGI("* Running module service scripts\n");
exec_module_script("service"); exec_module_script("service");
// Install Magisk Manager if exists // Install Magisk Manager if exists
if (access(MANAGERAPK, F_OK) == 0) { if (access(MANAGERAPK, F_OK) == 0) {
while(1) { while (1) {
sleep(5); sleep(5);
char *const command[] = { "sh", "-c", char *const command[] = { "sh", "-c",
"CLASSPATH=/system/framework/pm.jar " "CLASSPATH=/system/framework/pm.jar "
@ -765,7 +778,6 @@ void late_start(int client) {
#ifdef DEBUG #ifdef DEBUG
// Stop recording the boot logcat after every boot task is done // Stop recording the boot logcat after every boot task is done
extern int debug_log_pid, debug_log_fd;
kill(debug_log_pid, SIGTERM); kill(debug_log_pid, SIGTERM);
waitpid(debug_log_pid, NULL, 0); waitpid(debug_log_pid, NULL, 0);
close(debug_log_fd); close(debug_log_fd);

View File

@ -277,11 +277,11 @@ int mkdir_p(const char *pathname, mode_t mode) {
int bind_mount(const char *from, const char *to) { int bind_mount(const char *from, const char *to) {
int ret = xmount(from, to, NULL, MS_BIND, NULL); int ret = xmount(from, to, NULL, MS_BIND, NULL);
#ifdef DEBUG #ifdef DEBUG
LOGD("bind_mount: %s -> %s\n", from, to); LOGD("bind_mount: %s -> %s\n", from, to);
#else #else
LOGI("bind_mount: %s\n", to); LOGI("bind_mount: %s\n", to);
#endif #endif
return ret; return ret;
} }
@ -431,32 +431,39 @@ int get_img_size(const char *img, int *used, int *total) {
if (access(img, R_OK) == -1) if (access(img, R_OK) == -1)
return 1; return 1;
char buffer[PATH_MAX]; char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "e2fsck -n %s 2>/dev/null | tail -n 1", img); snprintf(buffer, sizeof(buffer), "e2fsck -yf %s", img);
char *const command[] = { "sh", "-c", buffer, NULL }; char *const command[] = { "sh", "-c", buffer, NULL };
int pid, fd = 0; int pid, fd = 0, ret = 1;
pid = run_command(0, &fd, "/system/bin/sh", command); pid = run_command(1, &fd, "/system/bin/sh", command);
fdgets(buffer, sizeof(buffer), fd);
close(fd);
if (pid == -1) if (pid == -1)
return 1; return 1;
waitpid(pid, NULL, 0); while (fdgets(buffer, sizeof(buffer), fd)) {
if (strstr(buffer, img)) {
char *tok; char *tok;
tok = strtok(buffer, ","); tok = strtok(buffer, ",");
while(tok != NULL) { while(tok != NULL) {
if (strstr(tok, "blocks")) if (strstr(tok, "blocks")) {
ret = 0;
break; break;
}
tok = strtok(NULL, ","); tok = strtok(NULL, ",");
} }
if (ret) continue;
sscanf(tok, "%d/%d", used, total); sscanf(tok, "%d/%d", used, total);
*used = *used / 256 + 1; *used = *used / 256 + 1;
*total /= 256; *total /= 256;
return 0; break;
}
}
close(fd);
waitpid(pid, NULL, 0);
return ret;
} }
int resize_img(const char *img, int size) { int resize_img(const char *img, int size) {
LOGI("Resize %s to %dM\n", img, size); LOGI("Resize %s to %dM\n", img, size);
char buffer[PATH_MAX]; char buffer[PATH_MAX];
snprintf(buffer, PATH_MAX, "e2fsck -yf %s; resize2fs %s %dM;", img, img, size); snprintf(buffer, PATH_MAX, "resize2fs %s %dM;", img, size);
return system(buffer); return system(buffer);
} }