From d7f039fccde837365f14e43912b1c59b54c79245 Mon Sep 17 00:00:00 2001 From: xlfeng Date: Fri, 7 Jan 2022 17:54:21 +0800 Subject: [PATCH] resize and check userdata.img before mount Signed-off-by: xlfeng --- interfaces/innerkits/fs_manager/fstab_mount.c | 115 +++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) mode change 100755 => 100644 interfaces/innerkits/fs_manager/fstab_mount.c diff --git a/interfaces/innerkits/fs_manager/fstab_mount.c b/interfaces/innerkits/fs_manager/fstab_mount.c old mode 100755 new mode 100644 index 6448e8c2..d2054c27 --- a/interfaces/innerkits/fs_manager/fstab_mount.c +++ b/interfaces/innerkits/fs_manager/fstab_mount.c @@ -36,6 +36,7 @@ extern "C" { #define FS_MANAGER_BUFFER_SIZE 512 #define BLOCK_SIZE_BUFFER (64) +#define RESIZE_BUFFER_SIZE 1024 bool IsSupportedFilesystem(const char *fsType) { @@ -153,6 +154,96 @@ MountStatus GetMountStatusForMountPoint(const char *mp) return status; } +static int DoResizeF2fs(const char* device, const unsigned long long size) +{ + char *file = "/system/bin/resize.f2fs"; + if (access(file, F_OK) != 0) { + INIT_LOGE("resize.f2fs is not exists."); + return -1; + } + + int ret = 0; + if (size <= 0) { + char *cmd[] = { + file, "-s", (char *)device, NULL + }; + int argc = ARRAY_LENGTH(cmd); + char **argv = (char **)cmd; + ret = ExecCommand(argc, argv); + } else { + char sizeStr[RESIZE_BUFFER_SIZE] = {0}; + sprintf_s(sizeStr, RESIZE_BUFFER_SIZE, "%llu", size * 1024 * 1024 / 512); + char *cmd[] = { + file, "-s", "-t", sizeStr, (char *)device, NULL + }; + int argc = ARRAY_LENGTH(cmd); + char **argv = (char **)cmd; + ret = ExecCommand(argc, argv); + } + return ret; +} + +static int DoFsckF2fs(const char* device) +{ + char *file = "/system/bin/fsck.f2fs"; + if (access(file, F_OK) != 0) { + INIT_LOGE("fsck.f2fs is not exists."); + return -1; + } + + char *cmd[] = { + file, "-a", (char *)device, NULL + }; + int argc = ARRAY_LENGTH(cmd); + char **argv = (char **)cmd; + return(ExecCommand(argc, argv)); +} + +static int DoResizeExt(const char* device, const unsigned long long size) +{ + char *file = "/system/bin/resize2fs"; + if (access(file, F_OK) != 0) { + INIT_LOGE("resize2fs is not exists."); + return -1; + } + + int ret = 0; + if (size <= 0) { + char *cmd[] = { + file, "-f", (char *)device, NULL + }; + int argc = ARRAY_LENGTH(cmd); + char **argv = (char **)cmd; + ret = ExecCommand(argc, argv); + } else { + char sizeStr[RESIZE_BUFFER_SIZE] = {0}; + sprintf_s(sizeStr, RESIZE_BUFFER_SIZE, "%lluM", size); + char *cmd[] = { + file, "-f", (char *)device, sizeStr, NULL + }; + int argc = ARRAY_LENGTH(cmd); + char **argv = (char **)cmd; + ret = ExecCommand(argc, argv); + } + return ret; +} + +static int DoFsckExt(const char* device) +{ + char *file = "/system/bin/e2fsck"; + if (access(file, F_OK) != 0) { + INIT_LOGE("e2fsck is not exists."); + return -1; + } + + char *cmd[] = { + file, "-y", (char *)device, NULL + }; + int argc = ARRAY_LENGTH(cmd); + char **argv = (char **)cmd; + return ExecCommand(argc, argv); +} + static int Mount(const char *source, const char *target, const char *fsType, unsigned long flags, const char *data) { @@ -204,6 +295,28 @@ int MountOneItem(FstabItem *item) if (FM_MANAGER_WAIT_ENABLED(item->fsManagerFlags)) { WaitForFile(item->deviceName, WAIT_MAX_COUNT); } + + if (strcmp(item->fsType, "f2fs") == 0 && strcmp(item->mountPoint, "/data") == 0) { + int ret = DoResizeF2fs(item->deviceName, 0); + if (ret != 0) { + INIT_LOGE("Failed to resize.f2fs dir %s , ret = %d", item->deviceName, ret); + } + + ret = DoFsckF2fs(item->deviceName); + if (ret != 0) { + INIT_LOGE("Failed to fsck.f2fs dir %s , ret = %d", item->deviceName, ret); + } + } else if (strcmp(item->fsType, "ext4") == 0 && strcmp(item->mountPoint, "/data") == 0) { + int ret = DoResizeExt(item->deviceName, 0); + if (ret != 0) { + INIT_LOGE("Failed to resize2fs dir %s , ret = %d", item->deviceName, ret); + } + ret = DoFsckExt(item->deviceName); + if (ret != 0) { + INIT_LOGE("Failed to e2fsck dir %s , ret = %d", item->deviceName, ret); + } + } + int rc = Mount(item->deviceName, item->mountPoint, item->fsType, mountFlags, fsSpecificData); if (rc != 0) { FSMGR_LOGE("Mount %s to %s failed %d", item->deviceName, item->mountPoint, errno); @@ -306,4 +419,4 @@ int UmountAllWithFstabFile(const char *fstabFile) #if __cplusplus } #endif -#endif +#endif \ No newline at end of file -- GitLab