diff --git a/interfaces/innerkits/fs_manager/fstab_mount.c b/interfaces/innerkits/fs_manager/fstab_mount.c index d33197af1..a9a7fce6a 100755 --- a/interfaces/innerkits/fs_manager/fstab_mount.c +++ b/interfaces/innerkits/fs_manager/fstab_mount.c @@ -28,6 +28,7 @@ #include "param/init_param.h" #include "securec.h" #ifdef SUPPORT_HVB +#include "fs_dm.h" #include "dm_verity.h" #endif #include "init_filesystems.h" @@ -563,6 +564,55 @@ int UmountAllWithFstabFile(const char *fstabFile) fstab = NULL; return rc; } + +int FsManagerDmRemoveDevice(const char *devName) +{ +#ifdef SUPPORT_HVB + return FsDmRemoveDevice(devName); +#endif + return 0; +} + +int MountOneWithFstabFile(const char *fstabFile, const char *devName, bool required) +{ + bool isFile = fstabFile == NULL || *fstabFile == '\0'; + BEGET_CHECK(!isFile, return -1); + + Fstab *fstab = NULL; + fstab = ReadFstabFromFile(fstabFile, false); + BEGET_ERROR_CHECK(fstab != NULL, return -1, "Read fstab file \" %s \" failed.", fstabFile); + + FstabItem *item = NULL; + int rc = -1; + +#ifdef SUPPORT_HVB + if (required) { + rc = HvbDmVerityinit(fstab); + if (rc != 0) { + BEGET_LOGE("set dm_verity init, ret = 0x%x", rc); + return rc; + } + } +#endif + + for (item = fstab->head; item != NULL; item = item->next) { + if (strcmp(item->mountPoint, devName) == 0) { + rc = CheckRequiredAndMount(item, required); + break; + } + } + +#ifdef SUPPORT_HVB + if (required) { + HvbDmVerityFinal(); + } +#endif + + ReleaseFstab(fstab); + fstab = NULL; + return rc; +} + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c index e7d90bf49..166e9714b 100755 --- a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c +++ b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c @@ -160,7 +160,7 @@ static int LoadDmDeviceTable(int fd, const char *devName, break; } } while (0); - + free(parasBuf); return rc; @@ -300,6 +300,38 @@ int FsDmInitDmDev(char *devPath, bool useSocket) return 0; } +int FsDmRemoveDevice(const char *devName) +{ + int rc; + int fd = -1; + struct dm_ioctl io; + + fd = open(DEVICE_MAPPER_PATH, O_RDWR | O_CLOEXEC); + if (fd < 0) { + BEGET_LOGE("error 0x%x, open %s", errno, DEVICE_MAPPER_PATH); + return -1; + } + + rc = InitDmIo(&io, devName); + if (rc != 0) { + close(fd); + BEGET_LOGE("error 0x%x, init dm io", rc); + return -1; + } + + io.flags |= DM_READONLY_FLAG; + + rc = ioctl(fd, DM_DEV_REMOVE, &io); + if (rc != 0) { + close(fd); + BEGET_LOGE("error, DM_DEV_REMOVE failed for %s, ret=%d", devName, rc); + return -1; + } + + close(fd); + return 0; +} + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/innerkits/fs_manager/libfs_dm/include/fs_dm.h b/interfaces/innerkits/fs_manager/libfs_dm/include/fs_dm.h index 4442c428d..42b29fe02 100755 --- a/interfaces/innerkits/fs_manager/libfs_dm/include/fs_dm.h +++ b/interfaces/innerkits/fs_manager/libfs_dm/include/fs_dm.h @@ -36,6 +36,7 @@ typedef struct { int FsDmInitDmDev(char *devPath, bool useSocket); int FsDmCreateDevice(char **dmDevPath, const char *devName, DmVerityTarget *target); +int FsDmRemoveDevice(const char *devName); #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/innerkits/include/fs_manager/fs_manager.h b/interfaces/innerkits/include/fs_manager/fs_manager.h index 8bb1e33d8..5aab45f09 100644 --- a/interfaces/innerkits/include/fs_manager/fs_manager.h +++ b/interfaces/innerkits/include/fs_manager/fs_manager.h @@ -97,6 +97,8 @@ MountStatus GetMountStatusForMountPoint(const char *mp); int MountAllWithFstabFile(const char *fstabFile, bool required); int MountAllWithFstab(const Fstab *fstab, bool required); int UmountAllWithFstabFile(const char *file); +int MountOneWithFstabFile(const char *fstabFile, const char *devName, bool required); +int FsManagerDmRemoveDevice(const char *devName); unsigned long GetMountFlags(char *mountFlag, char *fsSpecificFlags, size_t fsSpecificFlagSize, const char *mountPoint); diff --git a/services/init/standard/init_cmds.c b/services/init/standard/init_cmds.c index 7800b5ee0..460d261e5 100755 --- a/services/init/standard/init_cmds.c +++ b/services/init/standard/init_cmds.c @@ -389,6 +389,28 @@ static void DoUmount(const struct CmdArgs *ctx) } } +static void DoRemoveDmDevice(const struct CmdArgs *ctx) +{ + INIT_LOGI("DoRemoveDmDevice %s", ctx->argv[0]); + int ret = FsManagerDmRemoveDevice(ctx->argv[0]); + if (ret < 0) { + INIT_LOGE("Failed to remove dm device %s", ctx->argv[0]); + } else { + INIT_LOGI("Successed to remove dm device %s", ctx->argv[0]); + } +} + +static void DoMountOneFstabFile(const struct CmdArgs *ctx) +{ + INIT_LOGI("Mount %s from fstab file \" %s \"", ctx->argv[1], ctx->argv[0]); + int ret = MountOneWithFstabFile(ctx->argv[0], ctx->argv[1], 1); + if (ret < 0) { + INIT_LOGE("Failed to mount dm device %d", ret); + } else { + INIT_LOGI("Successed to mount dm device %d", ret); + } +} + static void DoSync(const struct CmdArgs *ctx) { sync(); @@ -558,6 +580,8 @@ static const struct CmdTable g_cmdTable[] = { { "ifup ", 1, 1, 1, DoIfup }, { "mount_fstab ", 1, 1, 0, DoMountFstabFile }, { "umount_fstab ", 1, 1, 0, DoUmountFstabFile }, + { "remove_dm_device", 1, 1, 0, DoRemoveDmDevice }, + { "mount_one_fstab", 2, 2, 0, DoMountOneFstabFile }, { "restorecon ", 1, 2, 1, DoRestorecon }, { "stopAllServices ", 0, 10, 0, DoStopAllServices }, { "umount ", 1, 1, 0, DoUmount },