Skip to contentSkip to conversation

qga: add mountpoint usage to GuestFilesystemInfo

PATCH Superseded Chen Hanxiao 2 participants 5 messages May 30, 2018

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qga: add mountpoint usage to GuestFilesystemInfo

From: Chen Hanxiao <[email protected]>

This patch adds support for getting the usage of mounted filesystem.
It's very useful when we try to monitor guest's filesystem. Use df of coreutils for reference.

---
 qga/commands-posix.c | 17 +++++++++++++++++
 qga/qapi-schema.json |  3 ++-
 2 files changed, 19 insertions(+), 1 deletion(-)
qga/commands-posix.c +17 -0
index 0dc219dbcf..0d93c47a5d 100644
@@ -46,6 +46,7 @@ extern char **environ;
 #include <arpa/inet.h>
 #include <sys/socket.h>
 #include <net/if.h>
+#include <sys/statvfs.h>
 
 #ifdef FIFREEZE
 #define CONFIG_FSFREEZE
@@ -1072,6 +1073,9 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
                                                Error **errp)
 {
     GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
+    struct statvfs buf;
+    unsigned long u100, used, nonroot_total;
+    int usage;
     char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
                                     mount->devmajor, mount->devminor);
 
@@ -1079,7 +1083,20 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
     fs->type = g_strdup(mount->devtype);
     build_guest_fsinfo_for_device(devpath, fs, errp);
 
+    if (statvfs(fs->mountpoint, &buf)) {
+        error_setg_errno(errp, errno, "Failed to get statvfs");
+        return NULL;
+    }
+
+    used = buf.f_blocks - buf.f_bfree;
+    u100 = 100 * used;
+    nonroot_total = used + buf.f_bavail;
+    usage = u100 / nonroot_total + (u100 % nonroot_total != 0);
+
+    fs->usage = usage;
+
     g_free(devpath);
+
     return fs;
 }
 
qga/qapi-schema.json +2 -1
index 17884c7c70..22c22d8a62 100644
@@ -846,13 +846,14 @@
 # @name: disk name
 # @mountpoint: mount point path
 # @type: file system type string
+# @usage: file system usage
 # @disk: an array of disk hardware information that the volume lies on,
 #        which may be empty if the disk type is not supported
 #
 # Since: 2.2
 ##
 { 'struct': 'GuestFilesystemInfo',
-  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str',
+  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str', 'usage': 'int',
            'disk': ['GuestDiskAddress']} }
 
 ##
Signature · 1 line
2.17.0
#1 lore raw
Quoted 24 lines · Chen Hanxiao

From: Chen Hanxiao <[email protected]>

This patch adds support for getting the usage of mounted filesystem.
It's very useful when we try to monitor guest's filesystem. Use df of coreutils for reference.

---

qga/commands-posix.c +3 -0
@@ -1072,6 +1073,9 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
                                                 Error **errp)
  {
      GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
+    struct statvfs buf;
+    unsigned long u100, used, nonroot_total;
+    int usage;
      char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
                                      mount->devmajor, mount->devminor);
 
@@ -1079,7 +1083,20 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
      fs->type = g_strdup(mount->devtype);
      build_guest_fsinfo_for_device(devpath, fs, errp);

qga/commands-posix.c static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount, · line 1094

Quoted 10 lines
qga/commands-posix.c +9 -0
 
+    if (statvfs(fs->mountpoint, &buf)) {
+        error_setg_errno(errp, errno, "Failed to get statvfs");
+        return NULL;
+    }
+
+    used = buf.f_blocks - buf.f_bfree;
+    u100 = 100 * used;
+    nonroot_total = used + buf.f_bavail;
+    usage = u100 / nonroot_total + (u100 % nonroot_total != 0);

Why integral instead of floating point?

qga/qapi-schema.json line 849

Quoted 6 lines
qga/qapi-schema.json +2 -0
+++ b/qga/qapi-schema.json
@@ -846,13 +846,14 @@
  # @name: disk name
  # @mountpoint: mount point path
  # @type: file system type string
+# @usage: file system usage

Needs more details. As written, it is an integer between 0 and 100; but if you use floating point, a better description would be a fraction between 0 and 1.

Quoted 12 lines
qga/qapi-schema.json +1 -1
  # @disk: an array of disk hardware information that the volume lies on,
  #        which may be empty if the disk type is not supported
  #
  # Since: 2.2
  ##
  { 'struct': 'GuestFilesystemInfo',
-  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str',
+  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str', 'usage': 'int',
             'disk': ['GuestDiskAddress']} }
 
  ##
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 7 lines · Eric Blake

qga/qapi-schema.json line 849

Quoted 5 lines
qga/qapi-schema.json +1 -0
@@ -846,13 +846,14 @@
   # @name: disk name
   # @mountpoint: mount point path
   # @type: file system type string
+# @usage: file system usage

Needs more details.  As written, it is an integer between 0 and 100; but if you use floating point, a better description would be a fraction between 0 and 1.

Also, missing a '(since 3.0)' designation.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#3 lore raw
In reply to Eric Blake
Quoted 26 lines · Eric Blake
Quoted 25 lines · Chen Hanxiao

From: Chen Hanxiao <[email protected]>

This patch adds support for getting the usage of mounted filesystem.
It's very useful when we try to monitor guest's filesystem. Use df of coreutils for reference.

---

qga/commands-posix.c +3 -0
@@ -1072,6 +1073,9 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
                                                 Error **errp)
  {
      GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
+    struct statvfs buf;
+    unsigned long u100, used, nonroot_total;
+    int usage;
      char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
                                      mount->devmajor, mount->devminor);
 
@@ -1079,7 +1083,20 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
      fs->type = g_strdup(mount->devtype);
      build_guest_fsinfo_for_device(devpath, fs, errp);
 
Quoted 11 lines

qga/commands-posix.c static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount, · line 1094

Quoted 9 lines
qga/commands-posix.c +9 -0
+    if (statvfs(fs->mountpoint, &buf)) {
+        error_setg_errno(errp, errno, "Failed to get statvfs");
+        return NULL;
+    }
+
+    used = buf.f_blocks - buf.f_bfree;
+    u100 = 100 * used;
+    nonroot_total = used + buf.f_bavail;
+    usage = u100 / nonroot_total + (u100 % nonroot_total != 0);

Why integral instead of floating point?

I followed the style of df from coreutils.
As the percentage already multiplied by 100,
I think it has enough precision.

Quoted 11 lines

qga/qapi-schema.json line 849

Quoted 7 lines
qga/qapi-schema.json +2 -0
+++ b/qga/qapi-schema.json
@@ -846,13 +846,14 @@
  # @name: disk name
  # @mountpoint: mount point path
  # @type: file system type string
+# @usage: file system usage

Needs more details. As written, it is an integer between 0 and 100; but if you use floating point, a better description would be a fraction between 0 and 1.

Will be updated in the following patch.

Regards,
- Chen

Quoted 18 lines
Quoted 13 lines
qga/qapi-schema.json +1 -1
  # @disk: an array of disk hardware information that the volume lies on,
  #        which may be empty if the disk type is not supported
  #
  # Since: 2.2
  ##
  { 'struct': 'GuestFilesystemInfo',
-  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str',
+  'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str', 'usage': 'int',
             'disk': ['GuestDiskAddress']} }
 
  ##
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#4 lore raw
Quoted 7 lines · Chen Hanxiao
Quoted 3 lines
Quoted 1 line

+ usage = u100 / nonroot_total + (u100 % nonroot_total != 0);

Why integral instead of floating point?

I followed the style of df from coreutils.
As the percentage already multiplied by 100,
I think it has enough precision.

You're making an arbitrary policy decision. Give the client a floating point, and they can still truncate to print out a 2-digit percentage. But give them a percentage, and for a 2T disk image, they are forced to guess whether they are 20G high or low on usage, which starts to take a non-trivial amount of time to copy that much data. In other words, I think an int is wrong for being too imprecise.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org