aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libzfs
diff options
context:
space:
mode:
authorTerraTech <[email protected]>2019-04-10 00:43:28 -0700
committerBrian Behlendorf <[email protected]>2019-04-16 12:24:06 -0700
commit50478c6dadc2accfab5bfe72d278ede17c2e13cf (patch)
treedb8bacb6f2193e7c20b311780f7516549b24f798 /lib/libzfs
parent8750edf1f75e4f02c353be490309940f11115f23 (diff)
Add option [-V|--version] to emit version string
Add the 'zfs version' and 'zpool version' subcommands to display the version of the user space utilities and loaded zfs kernel module. For example: $ zfs version zfs-0.8.0-rc3_169_g67e0366b88 zfs-kmod-0.8.0-rc3_169_g67e0366b88 The '-V' and '--version' aliases were added to support the common convention of using 'zfs --version` to obtain the version information. Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Matthew Ahrens <[email protected]> Reviewed-by: Richard Laager <[email protected]> Signed-off-by: TerraTech <[email protected]> Closes #2501 Closes #8567
Diffstat (limited to 'lib/libzfs')
-rw-r--r--lib/libzfs/libzfs_util.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/libzfs/libzfs_util.c b/lib/libzfs/libzfs_util.c
index 23dcb11bd..b988d8f31 100644
--- a/lib/libzfs/libzfs_util.c
+++ b/lib/libzfs/libzfs_util.c
@@ -54,6 +54,7 @@
#include "zfeature_common.h"
#include <zfs_fletcher.h>
#include <libzutil.h>
+#include <sys/zfs_sysfs.h>
int
libzfs_errno(libzfs_handle_t *hdl)
@@ -1940,3 +1941,66 @@ zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
{
return (zprop_iter_common(func, cb, show_all, ordered, type));
}
+
+/*
+ * Fill given version buffer with zfs userland version
+ */
+void
+zfs_version_userland(char *version, int len)
+{
+ (void) strlcpy(version, ZFS_META_ALIAS, len);
+}
+
+/*
+ * Fill given version buffer with zfs kernel version read from ZFS_SYSFS_DIR
+ * Returns 0 on success, and -1 on error (with errno set)
+ */
+int
+zfs_version_kernel(char *version, int len)
+{
+ int _errno;
+ int fd;
+ int rlen;
+
+ if ((fd = open(ZFS_SYSFS_DIR "/version", O_RDONLY)) == -1)
+ return (-1);
+
+ if ((rlen = read(fd, version, len)) == -1) {
+ version[0] = '\0';
+ _errno = errno;
+ (void) close(fd);
+ errno = _errno;
+ return (-1);
+ }
+
+ version[rlen-1] = '\0'; /* discard '\n' */
+
+ if (close(fd) == -1)
+ return (-1);
+
+ return (0);
+}
+
+/*
+ * Prints both zfs userland and kernel versions
+ * Returns 0 on success, and -1 on error (with errno set)
+ */
+int
+zfs_version_print(void)
+{
+ char zver_userland[128];
+ char zver_kernel[128];
+
+ if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) {
+ fprintf(stderr, "zfs_version_kernel() failed: %s\n",
+ strerror(errno));
+ return (-1);
+ }
+
+ zfs_version_userland(zver_userland, sizeof (zver_userland));
+
+ (void) printf("%s\n", zver_userland);
+ (void) printf("zfs-kmod-%s\n", zver_kernel);
+
+ return (0);
+}