[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 2/8] qga: Add ZFS TRIM support for FreeBSD
From: |
Konstantin Kostiuk |
Subject: |
[PULL 2/8] qga: Add ZFS TRIM support for FreeBSD |
Date: |
Fri, 16 Dec 2022 15:54:33 +0200 |
From: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Use zpool tool for ZFS pools trimming in FreeBSD.
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Konstantin Kostiuk <kkostiuk@redhat.com>
Signed-off-by: Konstantin Kostiuk <kkostiuk@redhat.com>
---
qga/commands-bsd.c | 109 ++++++++++++++++++++++++++++++++++++++++++
qga/commands-common.h | 1 +
2 files changed, 110 insertions(+)
diff --git a/qga/commands-bsd.c b/qga/commands-bsd.c
index 15cade2d4c..960c4209e5 100644
--- a/qga/commands-bsd.c
+++ b/qga/commands-bsd.c
@@ -170,6 +170,115 @@ GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp)
}
#endif /* CONFIG_FSFREEZE */
+#if defined(CONFIG_FSTRIM)
+typedef struct FsPool {
+ char *name;
+ QTAILQ_ENTRY(FsPool) next;
+} FsPool;
+
+typedef QTAILQ_HEAD(FsPoolList, FsPool) FsPoolList;
+
+static void build_fs_pool_list(FsPoolList *pools, Error **errp)
+{
+ FILE *fp;
+ char *line = NULL, *p;
+ size_t linecap = 0;
+ ssize_t linelen;
+ FsPool *pool;
+
+ fp = popen("/sbin/zpool list -H", "r");
+ if (fp == NULL) {
+ error_setg_errno(errp, errno, "failed to run zpool");
+ return;
+ }
+
+ while ((linelen = getline(&line, &linecap, fp)) > 0) {
+ p = strchr(line, '\t');
+ if (!p) {
+ continue;
+ }
+
+ *p = '\0';
+
+ pool = g_new0(FsPool, 1);
+ pool->name = g_strdup(line);
+ QTAILQ_INSERT_TAIL(pools, pool, next);
+ }
+
+ free(line);
+ pclose(fp);
+}
+
+static void free_fs_pool_list(FsPoolList *pools)
+{
+ FsPool *pool, *temp;
+
+ if (!pools) {
+ return;
+ }
+
+ QTAILQ_FOREACH_SAFE(pool, pools, next, temp) {
+ QTAILQ_REMOVE(pools, pool, next);
+ g_free(pool->name);
+ g_free(pool);
+ }
+}
+
+/*
+ * Walk the list of ZFS pools in the guest, and trim them.
+ */
+GuestFilesystemTrimResponse *
+qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
+{
+ GuestFilesystemTrimResponse *response;
+ GuestFilesystemTrimResultList *list;
+ GuestFilesystemTrimResult *result;
+ int ret;
+ FsPoolList pools;
+ FsPool *pool;
+ char cmd[256];
+ Error *local_err = NULL;
+
+ slog("guest-fstrim called");
+
+ QTAILQ_INIT(&pools);
+ build_fs_pool_list(&pools, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return NULL;
+ }
+
+ response = g_malloc0(sizeof(*response));
+
+ QTAILQ_FOREACH(pool, &pools, next) {
+ result = g_malloc0(sizeof(*result));
+ result->path = g_strdup(pool->name);
+
+ list = g_malloc0(sizeof(*list));
+ list->value = result;
+ list->next = response->paths;
+ response->paths = list;
+
+ snprintf(cmd, sizeof(cmd), "/sbin/zpool trim %s", pool->name);
+ ret = system(cmd);
+ if (ret != 0) {
+ result->error = g_strdup_printf("failed to trim %s: %s",
+ pool->name, strerror(errno));
+ result->has_error = true;
+ continue;
+ }
+
+ result->has_minimum = true;
+ result->minimum = 0;
+ result->has_trimmed = true;
+ result->trimmed = 0;
+ }
+
+ free_fs_pool_list(&pools);
+ return response;
+}
+#endif /* CONFIG_FSTRIM */
+
#ifdef HAVE_GETIFADDRS
/*
* Fill "buf" with MAC address by ifaddrs. Pointer buf must point to a
diff --git a/qga/commands-common.h b/qga/commands-common.h
index 8c1c56aac9..922f9c479b 100644
--- a/qga/commands-common.h
+++ b/qga/commands-common.h
@@ -28,6 +28,7 @@
#ifdef UFSSUSPEND
#define CONFIG_FSFREEZE
#endif
+#define CONFIG_FSTRIM
#endif /* __FreeBSD__ */
#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
--
2.25.1
- [PULL 0/8] QEMU Guest Agent misc patches, Konstantin Kostiuk, 2022/12/16
- [PULL 1/8] qga: Move FS TRIM code to commands-linux.c, Konstantin Kostiuk, 2022/12/16
- [PULL 2/8] qga: Add ZFS TRIM support for FreeBSD,
Konstantin Kostiuk <=
- [PULL 3/8] qga: Add initial OpenBSD and NetBSD support, Konstantin Kostiuk, 2022/12/16
- [PULL 4/8] qga:/qga-win: adding a empty PCI address creation function, Konstantin Kostiuk, 2022/12/16
- [PULL 5/8] qga:/qga-win: skip getting pci info for USB disks, Konstantin Kostiuk, 2022/12/16
- [PULL 8/8] qga: map GLib log levels to system levels, Konstantin Kostiuk, 2022/12/16
- [PULL 6/8] qga-win: choose the right libpcre version to include in MSI package, Konstantin Kostiuk, 2022/12/16
- [PULL 7/8] qga-win: add logging to Windows event log, Konstantin Kostiuk, 2022/12/16
- Re: [PULL 0/8] QEMU Guest Agent misc patches, Peter Maydell, 2022/12/16