lxcfs/0009-lxcfs-add-proc-partitions.patch
vegbir 9cae669874 upgrade to 5.0.4
Signed-off-by: vegbir <yangjiaqi16@huawei.com>
2023-07-29 09:15:09 +00:00

205 lines
5.6 KiB
Diff

From f230a8816877cc803d52661a444cb6d1b9d55f96 Mon Sep 17 00:00:00 2001
From: vegbir <yangjiaqi16@huawei.com>
Date: Thu, 27 Jul 2023 08:28:00 +0000
Subject: [PATCH 09/15] lxcfs add proc partitions
Signed-off-by: vegbir <yangjiaqi16@huawei.com>
---
src/bindings.h | 3 ++
src/proc_fuse.c | 137 ++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 131 insertions(+), 9 deletions(-)
diff --git a/src/bindings.h b/src/bindings.h
index 8d9d6eb..fcf408a 100644
--- a/src/bindings.h
+++ b/src/bindings.h
@@ -60,6 +60,9 @@ enum lxcfs_virt_t {
LXC_TYPE_PROC_SLABINFO,
#define LXC_TYPE_PROC_SLABINFO_PATH "/proc/slabinfo"
+ LXC_TYPE_PROC_PARTITIONS,
+#define LXC_TYPE_PROC_PARTITIONS_PATH "/proc/partitions"
+
LXC_TYPE_SYS,
LXC_TYPE_SYS_DEVICES,
LXC_TYPE_SYS_DEVICES_SYSTEM,
diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index 1f732ff..0dbbefe 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -119,14 +119,15 @@ __lxcfs_fuse_ops int proc_getattr(const char *path, struct stat *sb)
return 0;
}
- if (strcmp(path, "/proc/meminfo") == 0 ||
- strcmp(path, "/proc/cpuinfo") == 0 ||
- strcmp(path, "/proc/uptime") == 0 ||
- strcmp(path, "/proc/stat") == 0 ||
- strcmp(path, "/proc/diskstats") == 0 ||
- strcmp(path, "/proc/swaps") == 0 ||
- strcmp(path, "/proc/loadavg") == 0 ||
- strcmp(path, "/proc/slabinfo") == 0) {
+ if (strcmp(path, "/proc/meminfo") == 0 ||
+ strcmp(path, "/proc/cpuinfo") == 0 ||
+ strcmp(path, "/proc/uptime") == 0 ||
+ strcmp(path, "/proc/stat") == 0 ||
+ strcmp(path, "/proc/diskstats") == 0 ||
+ strcmp(path, "/proc/swaps") == 0 ||
+ strcmp(path, "/proc/loadavg") == 0 ||
+ strcmp(path, "/proc/slabinfo") == 0 ||
+ strcmp(path, "/proc/partitions") == 0) {
sb->st_size = get_procfile_size(path);
sb->st_mode = S_IFREG | 00444;
sb->st_nlink = 1;
@@ -149,7 +150,8 @@ __lxcfs_fuse_ops int proc_readdir(const char *path, void *buf,
dir_filler(filler, buf, "diskstats", 0) != 0 ||
dir_filler(filler, buf, "swaps", 0) != 0 ||
dir_filler(filler, buf, "loadavg", 0) != 0 ||
- dir_filler(filler, buf, "slabinfo", 0) != 0)
+ dir_filler(filler, buf, "slabinfo", 0) != 0 ||
+ dir_filler(filler, buf, "partitions", 0) != 0)
return -EINVAL;
return 0;
@@ -176,6 +178,8 @@ __lxcfs_fuse_ops int proc_open(const char *path, struct fuse_file_info *fi)
type = LXC_TYPE_PROC_LOADAVG;
else if (strcmp(path, "/proc/slabinfo") == 0)
type = LXC_TYPE_PROC_SLABINFO;
+ else if (strcmp(path, "/proc/partitions") == 0)
+ type = LXC_TYPE_PROC_PARTITIONS;
if (type == -1)
return -ENOENT;
@@ -975,6 +979,115 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset,
return total_len;
}
+static int proc_partitions_read(char *buf, size_t size, off_t offset,
+ struct fuse_file_info *fi)
+{
+ char dev_name[72] = {0};
+ struct fuse_context *fc = fuse_get_context();
+ struct file_info *d = (struct file_info *)fi->fh;
+ struct devinfo *container_devinfo = NULL, *ptr = NULL;
+ char *cg = NULL;
+ char *cache = d->buf;
+ size_t cache_size = d->buflen;
+ char *line = NULL;
+ size_t linelen = 0, total_len = 0;
+ int rv = 0;
+ unsigned int major = 0, minor = 0;
+ unsigned long long blocks = 0;
+ int i = 0, lines = 0;
+ bool found = false;
+ FILE *f = NULL;
+
+ if (offset > 0){
+ size_t left;
+
+ if (offset > d->size)
+ return -EINVAL;
+
+ if (!d->cached)
+ return 0;
+
+ left = d->size - offset;
+ total_len = left > size ? size: left;
+ memcpy(buf, cache + offset, total_len);
+
+ return total_len;
+ }
+
+ pid_t initpid = lookup_initpid_in_store(fc->pid);
+ if (initpid <= 0)
+ initpid = fc->pid;
+ cg = get_pid_cgroup(initpid, "blkio");
+ if (cg == NULL)
+ return read_file_fuse("/proc/partitions", buf, size, d);
+ prune_init_slice(cg);
+
+ f = fopen("/proc/partitions", "r");
+ if (f == NULL)
+ goto err;
+
+ lock_mutex(&container_dev_mutex);
+ container_devinfo = container_dev_read(initpid);
+ unlock_mutex(&container_dev_mutex);
+
+ while (getline(&line, &linelen, f) != -1) {
+ ssize_t l;
+ char lbuf[256];
+
+ if (lines < 2) {
+ strncpy(lbuf, line, sizeof(lbuf)-1);
+ lines++;
+ } else {
+ memset(dev_name, 0, sizeof(dev_name));
+
+ i = sscanf(line, "%u %u %llu %71s", &major, &minor, &blocks, dev_name);
+ if (i != 4)
+ continue;
+ found = false;
+ for (ptr = container_devinfo; ptr != NULL; ptr = ptr->next) {
+ if (major == ptr->major && minor == ptr->minor) {
+ snprintf(lbuf, 256, "%4u %7u %10llu %s\n", major, minor, blocks, ptr->name);
+ found = true;
+ }
+ }
+ if (!found)
+ continue;
+ }
+
+ l = snprintf(cache, cache_size, "%s", lbuf);
+ if (l < 0) {
+ perror("Error writing to fuse buf");
+ rv = 0;
+ goto err;
+ }
+ if ((size_t)l >= cache_size) {
+ lxcfs_error("%s\n", "Internal error: truncated write to cache.");
+ rv = 0;
+ goto err;
+ }
+ cache += l;
+ cache_size -= l;
+ total_len += l;
+ }
+ d->cached = 1;
+ d->size = total_len;
+ if (total_len > size )
+ total_len = size;
+
+ memcpy(buf, d->buf, total_len);
+
+ rv = total_len;
+err:
+ free(cg);
+ cg = NULL;
+ if (f != NULL)
+ fclose(f);
+ free(line);
+ line = NULL;
+ free_devinfo_list(container_devinfo);
+ return rv;
+}
+
#ifdef RELOADTEST
static inline void iwashere(void)
{
@@ -1912,6 +2025,12 @@ __lxcfs_fuse_ops int proc_read(const char *path, char *buf, size_t size,
return read_file_fuse_with_offset(LXC_TYPE_PROC_SLABINFO_PATH,
buf, size, offset, f);
+ case LXC_TYPE_PROC_PARTITIONS:
+ if(liblxcfs_functional())
+ return proc_partitions_read(buf, size, offset, fi);
+
+ return read_file_fuse_with_offset(LXC_TYPE_PROC_PARTITIONS_PATH,
+ buf, size, offset, f);
}
return -EINVAL;
--
2.41.0