提交 07190f90 编写于 作者: L lifeng68

rootfs: add get container rootfs usage info interface

Signed-off-by: Nlifeng68 <lifeng68@huawei.com>
上级 0f31d041
......@@ -367,10 +367,12 @@ int im_get_container_filesystem_usage(const char *image_type, const char *id, im
}
*fs_usage = filesystemusage;
filesystemusage = NULL;
EVENT("Event: {Object: container \'%s\' filesystem info, Type: inspected}", id != NULL ? id : "");
out:
free_im_container_fs_usage_request(request);
free_imagetool_fs_info(filesystemusage);
return ret;
}
......
......@@ -296,30 +296,32 @@ out:
int oci_container_filesystem_usage(const im_container_fs_usage_request *request, imagetool_fs_info **fs_usage)
{
int ret = 0;
char *output = NULL;
parser_error err = NULL;
imagetool_fs_info *layer_fs_tmp = NULL;
if (request == NULL || fs_usage == NULL) {
ERROR("Invalid input arguments");
return -1;
}
// TODO call storage container fs interface
// ret = isula_container_fs_usage(request->name_id, &output);
if (ret != 0) {
ERROR("Failed to inspect container filesystem info");
layer_fs_tmp = util_common_calloc_s(sizeof(imagetool_fs_info));
if (layer_fs_tmp == NULL) {
ERROR("Memory out");
ret = -1;
goto out;
}
*fs_usage = imagetool_fs_info_parse_data(output, NULL, &err);
if (*fs_usage == NULL) {
ERROR("Failed to parse output json: %s", err);
isulad_set_error_message("Failed to parse output json:%s", err);
ret = storage_rootfs_fs_usgae(request->name_id, layer_fs_tmp);
if (ret != 0) {
ERROR("Failed to inspect container filesystem info");
ret = -1;
goto out;
}
*fs_usage = layer_fs_tmp;
layer_fs_tmp = NULL;
out:
free(output);
free_imagetool_fs_info(layer_fs_tmp);
return ret;
}
......
......@@ -47,6 +47,7 @@ static const struct graphdriver_ops g_overlay2_ops = {
.get_driver_status = overlay2_get_driver_status,
.clean_up = overlay2_clean_up,
.try_repair_lowers = overlay2_repair_lowers,
.get_layer_fs_info = overlay2_get_layer_fs_info,
};
/* devicemapper */
......@@ -369,3 +370,18 @@ int graphdriver_try_repair_lowers(const char *id, const char *parent)
return g_graphdriver->ops->try_repair_lowers(id, parent, g_graphdriver);
}
int graphdriver_get_layer_fs_info(const char *id, imagetool_fs_info *fs_info)
{
if (g_graphdriver == NULL) {
ERROR("Driver not inited yet");
return -1;
}
if (id == NULL || fs_info == NULL) {
ERROR("Invalid input arguments for driver get layer info");
return -1;
}
return g_graphdriver->ops->get_layer_fs_info(id, g_graphdriver, fs_info);
}
\ No newline at end of file
......@@ -71,6 +71,8 @@ struct graphdriver_ops {
int (*clean_up)(const struct graphdriver *driver);
int (*try_repair_lowers)(const char *id, const char *parent, const struct graphdriver *driver);
int (*get_layer_fs_info)(const char *id, const struct graphdriver *driver, imagetool_fs_info *fs_info);
};
struct graphdriver {
......@@ -116,6 +118,8 @@ int graphdriver_try_repair_lowers(const char *id, const char *parent);
container_inspect_graph_driver *graphdriver_get_metadata(const char *id);
int graphdriver_get_layer_fs_info(const char *id, imagetool_fs_info *fs_info);
#ifdef __cplusplus
}
#endif
......
......@@ -1845,3 +1845,100 @@ out:
free(lowers_repaired);
return ret;
}
static int do_cal_layer_fs_info(const char *layer_diff, imagetool_fs_info *fs_info)
{
int ret = 0;
imagetool_fs_info_image_filesystems_element *fs_usage_tmp = NULL;
int64_t total_size = 0;
int64_t total_inodes = 0;
fs_usage_tmp = util_common_calloc_s(sizeof(imagetool_fs_info_image_filesystems_element));
if (fs_usage_tmp == NULL) {
ERROR("Memory out");
ret = -1;
goto out;
}
fs_usage_tmp->timestamp = get_now_time_nanos();
fs_usage_tmp->fs_id = util_common_calloc_s(sizeof(imagetool_fs_info_image_filesystems_fs_id));
if (fs_usage_tmp->fs_id == NULL) {
ERROR("Memory out");
ret = -1;
goto out;
}
fs_usage_tmp->fs_id->mountpoint = util_strdup_s(layer_diff);
util_calculate_dir_size(layer_diff, 0, &total_size, &total_inodes);
fs_usage_tmp->inodes_used = util_common_calloc_s(sizeof(imagetool_fs_info_image_filesystems_inodes_used));
if (fs_usage_tmp->inodes_used == NULL) {
ERROR("Memory out");
ret = -1;
goto out;
}
fs_usage_tmp->inodes_used->value = total_inodes;
fs_usage_tmp->used_bytes = util_common_calloc_s(sizeof(imagetool_fs_info_image_filesystems_used_bytes));
if (fs_usage_tmp->used_bytes == NULL) {
ERROR("Memory out");
ret = -1;
goto out;
}
fs_usage_tmp->used_bytes->value = total_size;
fs_info->image_filesystems = util_common_calloc_s(sizeof(imagetool_fs_info_image_filesystems_element *));
if (fs_info->image_filesystems == NULL) {
ERROR("Memory out");
ret = -1;
goto out;
}
fs_info->image_filesystems[0] = fs_usage_tmp;
fs_usage_tmp = NULL;
fs_info->image_filesystems_len = 1;
out:
free_imagetool_fs_info_image_filesystems_element(fs_usage_tmp);
return ret;
}
int overlay2_get_layer_fs_info(const char *id, const struct graphdriver *driver, imagetool_fs_info *fs_info)
{
int ret = 0;
char *layer_dir = NULL;
char *layer_diff = NULL;
if (id == NULL || fs_info == NULL) {
return -1;
}
layer_dir = util_path_join(driver->home, id);
if (layer_dir == NULL) {
ERROR("Failed to join layer dir:%s", id);
goto out;
}
if (!util_dir_exists(layer_dir)) {
SYSERROR("layer dir %s not exist", layer_dir);
goto out;
}
layer_diff = util_path_join(layer_dir, OVERLAY_LAYER_DIFF);
if (layer_diff == NULL) {
ERROR("Failed to join layer diff dir:%s", id);
ret = -1;
goto out;
}
if (do_cal_layer_fs_info(layer_diff, fs_info) != 0) {
ERROR("Failed to cal layer diff :%s fs info", layer_diff);
ret = -1;
goto out;
}
out:
free(layer_dir);
free(layer_diff);
return ret;
}
\ No newline at end of file
......@@ -55,6 +55,8 @@ void free_driver_mount_opts(struct driver_mount_opts *opts);
int overlay2_repair_lowers(const char *id, const char *parent, const struct graphdriver *driver);
int overlay2_get_layer_fs_info(const char *id, const struct graphdriver *driver, imagetool_fs_info *fs_info);
#ifdef __cplusplus
}
#endif
......
......@@ -1634,3 +1634,8 @@ void free_layer_store_mount_opts(struct layer_store_mount_opts *ptr)
ptr->mount_opts = NULL;
free(ptr);
}
int layer_store_get_layer_fs_info(const char *layer_id, imagetool_fs_info *fs_info)
{
return graphdriver_get_layer_fs_info(layer_id, fs_info);
}
......@@ -64,6 +64,8 @@ int layer_store_try_repair_lowers(const char *id);
void free_layer_store_mount_opts(struct layer_store_mount_opts *ptr);
void free_layer_opts(struct layer_opts *opts);
int layer_store_get_layer_fs_info(const char *layer_id, imagetool_fs_info *fs_info);
#ifdef __cplusplus
}
#endif
......
......@@ -1025,3 +1025,32 @@ out:
free_storage_rootfs(rootfs_info);
return ret;
}
int storage_rootfs_fs_usgae(const char *container_id, imagetool_fs_info *fs_info)
{
int ret = 0;
storage_rootfs *rootfs_info = NULL;
if (container_id == NULL || fs_info == NULL) {
ERROR("Invalid input arguments");
ret = -1;
goto out;
}
rootfs_info = rootfs_store_get_rootfs(container_id);
if (rootfs_info == NULL) {
ERROR("Failed to get rootfs %s info", container_id);
ret = -1;
goto out;
}
if (layer_store_get_layer_fs_info(rootfs_info->layer, fs_info) != 0) {
ERROR("Failed to get layer %s fs usgae info", rootfs_info->layer);
ret = -1;
goto out;
}
out:
free_storage_rootfs(rootfs_info);
return ret;
}
......@@ -154,6 +154,8 @@ int storage_rootfs_create(const char *container_id, const char *image, json_map_
int storage_rootfs_delete(const char *container_id);
int storage_rootfs_fs_usgae(const char *container_id, imagetool_fs_info *fs_info);
#ifdef __cplusplus
}
#endif
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册