Add more image commands

This commit is contained in:
topjohnwu 2017-06-24 23:37:45 +09:00
parent 9430ed66cd
commit 04538372c6
6 changed files with 155 additions and 113 deletions

View File

@ -20,6 +20,7 @@ LOCAL_SRC_FILES := \
utils/vector.c \ utils/vector.c \
utils/xwrap.c \ utils/xwrap.c \
utils/list.c \ utils/list.c \
utils/img.c \
daemon/daemon.c \ daemon/daemon.c \
daemon/socket_trans.c \ daemon/socket_trans.c \
daemon/log_monitor.c \ daemon/log_monitor.c \

View File

@ -54,44 +54,6 @@ struct node_entry {
* Image handling * * Image handling *
******************/ ******************/
static char *loopsetup(const char *img) {
char device[20];
struct loop_info64 info;
int i, lfd, ffd;
memset(&info, 0, sizeof(info));
// First get an empty loop device
for (i = 0; i <= 7; ++i) {
sprintf(device, "/dev/block/loop%d", i);
lfd = xopen(device, O_RDWR);
if (ioctl(lfd, LOOP_GET_STATUS64, &info) == -1)
break;
close(lfd);
}
if (i == 8) return NULL;
ffd = xopen(img, O_RDWR);
if (ioctl(lfd, LOOP_SET_FD, ffd) == -1)
return NULL;
strcpy((char *) info.lo_file_name, img);
ioctl(lfd, LOOP_SET_STATUS64, &info);
close(lfd);
close(ffd);
return strdup(device);
}
static char *mount_image(const char *img, const char *target) {
char *device = loopsetup(img);
if (device)
xmount(device, target, "ext4", 0, NULL);
return device;
}
static void umount_image(const char *target, const char *device) {
xumount(target);
int fd = xopen(device, O_RDWR);
ioctl(fd, LOOP_CLR_FD);
close(fd);
}
#define round_size(a) ((((a) / 32) + 2) * 32) #define round_size(a) ((((a) / 32) + 2) * 32)
#define SOURCE_TMP "/dev/source" #define SOURCE_TMP "/dev/source"
#define TARGET_TMP "/dev/target" #define TARGET_TMP "/dev/target"

View File

@ -32,6 +32,9 @@ static void usage() {
" or: %s --imgsize <PATH>\n" " or: %s --imgsize <PATH>\n"
" or: %s --resizeimg <PATH> <SIZE>\n" " or: %s --resizeimg <PATH> <SIZE>\n"
" SIZE is interpreted in MB\n" " SIZE is interpreted in MB\n"
" or: %s --mountimg <IMG> <PATH>\n"
" Prints out the loop device\n"
" or: %s --umountimg <PATH> <LOOP>\n"
" or: %s --[boot stage]\n" " or: %s --[boot stage]\n"
" start boot stage service\n" " start boot stage service\n"
" or: %s [options]\n" " or: %s [options]\n"
@ -46,7 +49,7 @@ static void usage() {
" -V print daemon version code\n" " -V print daemon version code\n"
"\n" "\n"
"Supported applets:\n" "Supported applets:\n"
, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0); , argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0);
for (int i = 0; applet[i]; ++i) { for (int i = 0; applet[i]; ++i) {
fprintf(stderr, i ? ", %s" : " %s", applet[i]); fprintf(stderr, i ? ", %s" : " %s", applet[i]);
@ -99,7 +102,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Cannot check %s size\n", argv[2]); fprintf(stderr, "Cannot check %s size\n", argv[2]);
return 1; return 1;
} }
printf("Used: %dM\tTotal: %dM\n", used, total); printf("%d %d\n", used, total);
return 0; return 0;
} else if (strcmp(argv[1], "--resizeimg") == 0) { } else if (strcmp(argv[1], "--resizeimg") == 0) {
if (argc < 4) usage(); if (argc < 4) usage();
@ -114,6 +117,21 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
return resize_img(argv[2], size); return resize_img(argv[2], size);
} else if (strcmp(argv[1], "--mountimg") == 0) {
if (argc < 4) usage();
char *loop = mount_image(argv[2], argv[3]);
if (loop == NULL) {
fprintf(stderr, "Cannot mount image!\n");
return 1;
} else {
printf("%s\n", loop);
free(loop);
return 0;
}
} else if (strcmp(argv[1], "--umountimg") == 0) {
if (argc < 4) usage();
umount_image(argv[2], argv[3]);
return 0;
} else if (strcmp(argv[1], "--post-fs") == 0) { } else if (strcmp(argv[1], "--post-fs") == 0) {
int fd = connect_daemon(); int fd = connect_daemon();
write_int(fd, POST_FS); write_int(fd, POST_FS);

128
jni/utils/img.c Normal file
View File

@ -0,0 +1,128 @@
/* img.c - All image related functions
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <linux/loop.h>
#include "magisk.h"
#include "utils.h"
int create_img(const char *img, int size) {
unlink(img);
LOGI("Create %s with size %dM\n", img, size);
// Create a temp file with the file contexts
char file_contexts[] = "/magisk(/.*)? u:object_r:system_file:s0\n";
// If not root, attempt to create in current diretory
char *filename = getuid() == UID_ROOT ? "/dev/file_contexts_image" : "file_contexts_image";
int fd = xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
xwrite(fd, file_contexts, sizeof(file_contexts));
close(fd);
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer),
"make_ext4fs -l %dM -a /magisk -S %s %s; e2fsck -yf %s;", size, filename, img, img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, status;
pid = run_command(0, NULL, "/system/bin/sh", command);
if (pid == -1)
return 1;
waitpid(pid, &status, 0);
unlink(filename);
return WEXITSTATUS(status);
}
int get_img_size(const char *img, int *used, int *total) {
if (access(img, R_OK) == -1)
return 1;
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "e2fsck -n %s", img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, fd = 0, status = 1;
pid = run_command(1, &fd, "/system/bin/sh", command);
if (pid == -1)
return 1;
while (fdgets(buffer, sizeof(buffer), fd)) {
LOGD("magisk_img: %s", buffer);
if (strstr(buffer, img)) {
char *tok = strtok(buffer, ",");
while(tok != NULL) {
if (strstr(tok, "blocks")) {
status = 0;
break;
}
tok = strtok(NULL, ",");
}
if (status) continue;
sscanf(tok, "%d/%d", used, total);
*used = *used / 256 + 1;
*total /= 256;
break;
}
}
close(fd);
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
int resize_img(const char *img, int size) {
LOGI("Resize %s to %dM\n", img, size);
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "e2fsck -yf %s; resize2fs %s %dM;", img, img, size);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, status, fd = 0;
pid = run_command(1, &fd, "/system/bin/sh", command);
if (pid == -1)
return 1;
while (fdgets(buffer, sizeof(buffer), fd))
LOGD("magisk_img: %s", buffer);
close(fd);
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
char *loopsetup(const char *img) {
char device[20];
struct loop_info64 info;
int i, lfd, ffd;
memset(&info, 0, sizeof(info));
// First get an empty loop device
for (i = 0; i <= 7; ++i) {
sprintf(device, "/dev/block/loop%d", i);
lfd = xopen(device, O_RDWR);
if (ioctl(lfd, LOOP_GET_STATUS64, &info) == -1)
break;
close(lfd);
}
if (i == 8) return NULL;
ffd = xopen(img, O_RDWR);
if (ioctl(lfd, LOOP_SET_FD, ffd) == -1)
return NULL;
strcpy((char *) info.lo_file_name, img);
ioctl(lfd, LOOP_SET_STATUS64, &info);
close(lfd);
close(ffd);
return strdup(device);
}
char *mount_image(const char *img, const char *target) {
if (access(target, F_OK) == -1) {
if (mkdir(target, 0755) == -1) {
xmount(NULL, "/", NULL, MS_REMOUNT, NULL);
xmkdir(target, 0755);
}
}
char *device = loopsetup(img);
if (device)
xmount(device, target, "ext4", 0, NULL);
return device;
}
void umount_image(const char *target, const char *device) {
xumount(target);
int fd = xopen(device, O_RDWR);
ioctl(fd, LOOP_CLR_FD);
close(fd);
}

View File

@ -411,78 +411,6 @@ void get_client_cred(int fd, struct ucred *cred) {
PLOGE("getsockopt"); PLOGE("getsockopt");
} }
int create_img(const char *img, int size) {
unlink(img);
LOGI("Create %s with size %dM\n", img, size);
// Create a temp file with the file contexts
char file_contexts[] = "/magisk(/.*)? u:object_r:system_file:s0\n";
int fd = xopen("/dev/file_contexts_image", O_WRONLY | O_CREAT | O_TRUNC, 0644);
xwrite(fd, file_contexts, sizeof(file_contexts));
close(fd);
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer),
"make_ext4fs -l %dM -a /magisk -S /dev/file_contexts_image %s; e2fsck -yf %s;", size, img, img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, status;
pid = run_command(0, NULL, "/system/bin/sh", command);
if (pid == -1)
return 1;
waitpid(pid, &status, 0);
unlink("/dev/file_contexts_image");
return WEXITSTATUS(status);
}
int get_img_size(const char *img, int *used, int *total) {
if (access(img, R_OK) == -1)
return 1;
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "e2fsck -yf %s", img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, fd = 0, status = 1;
pid = run_command(1, &fd, "/system/bin/sh", command);
if (pid == -1)
return 1;
while (fdgets(buffer, sizeof(buffer), fd)) {
LOGD("magisk_img: %s", buffer);
if (strstr(buffer, img)) {
char *tok;
tok = strtok(buffer, ",");
while(tok != NULL) {
if (strstr(tok, "blocks")) {
status = 0;
break;
}
tok = strtok(NULL, ",");
}
if (status) continue;
sscanf(tok, "%d/%d", used, total);
*used = *used / 256 + 1;
*total /= 256;
break;
}
}
close(fd);
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
int resize_img(const char *img, int size) {
LOGI("Resize %s to %dM\n", img, size);
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "resize2fs %s %dM; e2fsck -yf %s;", img, size, img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, status, fd = 0;
pid = run_command(1, &fd, "/system/bin/sh", command);
if (pid == -1)
return 1;
while (fdgets(buffer, sizeof(buffer), fd))
LOGD("magisk_img: %s", buffer);
close(fd);
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
int switch_mnt_ns(int pid) { int switch_mnt_ns(int pid) {
char mnt[32]; char mnt[32];
snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid); snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);

View File

@ -91,9 +91,14 @@ int clone_dir(const char *source, const char *target);
int rm_rf(const char *target); int rm_rf(const char *target);
void clone_attr(const char *source, const char *target); void clone_attr(const char *source, const char *target);
void get_client_cred(int fd, struct ucred *cred); void get_client_cred(int fd, struct ucred *cred);
int switch_mnt_ns(int pid);
// img.c
int create_img(const char *img, int size); int create_img(const char *img, int size);
int get_img_size(const char *img, int *used, int *total); int get_img_size(const char *img, int *used, int *total);
int resize_img(const char *img, int size); int resize_img(const char *img, int size);
int switch_mnt_ns(int pid); char *loopsetup(const char *img);
char *mount_image(const char *img, const char *target);
void umount_image(const char *target, const char *device);
#endif #endif