Add ext4 img helper commands

This commit is contained in:
topjohnwu
2017-06-03 03:58:26 +08:00
parent 20903784a4
commit b9eab39541
4 changed files with 91 additions and 32 deletions

View File

@@ -86,38 +86,8 @@ static void umount_image(const char *target, const char *device) {
close(fd);
}
static int get_img_size(const char *img, int *used, int *total) {
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "e2fsck -n %s 2>/dev/null | tail -n 1", img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, fd;
pid = run_command(&fd, "/system/bin/sh", command);
fdgets(buffer, sizeof(buffer), fd);
close(fd);
if (pid == -1)
return 1;
waitpid(pid, NULL, 0);
char *tok;
tok = strtok(buffer, ",");
while(tok != NULL) {
if (strstr(tok, "blocks"))
break;
tok = strtok(NULL, ",");
}
sscanf(tok, "%d/%d", used, total);
*used = *used / 256 + 1;
*total /= 256;
return 0;
}
#define round_size(a) ((((a) / 32) + 2) * 32)
static int resize_img(const char *img, int size) {
LOGI("resize %s to %dM\n", img, size);
snprintf(buf, PATH_MAX, "e2fsck -yf %s; resize2fs %s %dM;", img, img, size);
return system(buf);
}
static int merge_img(const char *source, const char *target) {
if (access(source, F_OK) == -1)
return 0;
@@ -544,6 +514,9 @@ void post_fs_data(int client) {
if (merge_img("/data/magisk_merge.img", MAINIMG))
goto unblock;
if (access(MAINIMG, F_OK) == -1 && create_img(MAINIMG, 64))
goto unblock;
LOGI("* Mounting " MAINIMG "\n");
// Mounting magisk image
char *magiskloop = mount_image(MAINIMG, MOUNTPOINT);