]> git.proxmox.com Git - mirror_zfs.git/commitdiff
Add option [-V|--version] to emit version string
authorTerraTech <1118433+TerraTech@users.noreply.github.com>
Wed, 10 Apr 2019 07:43:28 +0000 (00:43 -0700)
committerBrian Behlendorf <behlendorf1@llnl.gov>
Tue, 16 Apr 2019 19:24:06 +0000 (12:24 -0700)
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 <behlendorf1@llnl.gov>
Reviewed-by: Matthew Ahrens <mahrens@delphix.com>
Reviewed-by: Richard Laager <rlaager@wiktel.com>
Signed-off-by: TerraTech <1118433+TerraTech@users.noreply.github.com>
Closes #2501
Closes #8567

cmd/zfs/zfs_main.c
cmd/zpool/zpool_main.c
include/libzfs.h
lib/libzfs/libzfs_util.c
man/man8/zfs.8
man/man8/zpool.8

index 5df20629333b7351adc061862d761608df7efe76..90893a8571242252aae6919a4f6c7d99c47f3a87 100644 (file)
@@ -117,6 +117,7 @@ static int zfs_do_load_key(int argc, char **argv);
 static int zfs_do_unload_key(int argc, char **argv);
 static int zfs_do_change_key(int argc, char **argv);
 static int zfs_do_project(int argc, char **argv);
+static int zfs_do_version(int argc, char **argv);
 
 /*
  * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
@@ -171,6 +172,7 @@ typedef enum {
        HELP_LOAD_KEY,
        HELP_UNLOAD_KEY,
        HELP_CHANGE_KEY,
+       HELP_VERSION
 } zfs_help_t;
 
 typedef struct zfs_command {
@@ -189,6 +191,8 @@ typedef struct zfs_command {
  * the generic usage message.
  */
 static zfs_command_t command_table[] = {
+       { "version",    zfs_do_version,         HELP_VERSION            },
+       { NULL },
        { "create",     zfs_do_create,          HELP_CREATE             },
        { "destroy",    zfs_do_destroy,         HELP_DESTROY            },
        { NULL },
@@ -379,6 +383,8 @@ get_usage(zfs_help_t idx)
                    "\t    [-o keylocation=<value>] [-o pbkfd2iters=<value>]\n"
                    "\t    <filesystem|volume>\n"
                    "\tchange-key -i [-l] <filesystem|volume>\n"));
+       case HELP_VERSION:
+               return (gettext("\tversion\n"));
        }
 
        abort();
@@ -8059,6 +8065,18 @@ zfs_do_project(int argc, char **argv)
        return (ret);
 }
 
+/*
+ * Display version message
+ */
+static int
+zfs_do_version(int argc, char **argv)
+{
+       if (zfs_version_print() == -1)
+               return (1);
+
+       return (0);
+}
+
 int
 main(int argc, char **argv)
 {
@@ -8114,6 +8132,12 @@ main(int argc, char **argv)
            (strcmp(cmdname, "--help") == 0))
                usage(B_TRUE);
 
+       /*
+        * Special case '-V|--version'
+        */
+       if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
+               return (zfs_do_version(argc, argv));
+
        if ((g_zfs = libzfs_init()) == NULL) {
                (void) fprintf(stderr, "%s", libzfs_error_init(errno));
                return (1);
index f4670fd6235be8e7ca81486578617876f8a40344..c167324ce820ba057706557f26c68f7fbbf7fa4a 100644 (file)
@@ -115,6 +115,8 @@ static int zpool_do_set(int, char **);
 
 static int zpool_do_sync(int, char **);
 
+static int zpool_do_version(int, char **);
+
 /*
  * These libumem hooks provide a reasonable set of defaults for the allocator's
  * debugging facilities.
@@ -164,7 +166,8 @@ typedef enum {
        HELP_SPLIT,
        HELP_SYNC,
        HELP_REGUID,
-       HELP_REOPEN
+       HELP_REOPEN,
+       HELP_VERSION
 } zpool_help_t;
 
 
@@ -263,6 +266,8 @@ typedef struct zpool_command {
  * the generic usage message.
  */
 static zpool_command_t command_table[] = {
+       { "version",    zpool_do_version,       HELP_VERSION            },
+       { NULL },
        { "create",     zpool_do_create,        HELP_CREATE             },
        { "destroy",    zpool_do_destroy,       HELP_DESTROY            },
        { NULL },
@@ -404,6 +409,8 @@ get_usage(zpool_help_t idx)
                return (gettext("\treguid <pool>\n"));
        case HELP_SYNC:
                return (gettext("\tsync [pool] ...\n"));
+       case HELP_VERSION:
+               return (gettext("\tversion\n"));
        }
 
        abort();
@@ -9222,6 +9229,18 @@ find_command_idx(char *command, int *idx)
        return (1);
 }
 
+/*
+ * Display version message
+ */
+static int
+zpool_do_version(int argc, char **argv)
+{
+       if (zfs_version_print() == -1)
+               return (1);
+
+       return (0);
+}
+
 int
 main(int argc, char **argv)
 {
@@ -9252,6 +9271,12 @@ main(int argc, char **argv)
        if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
                usage(B_TRUE);
 
+       /*
+        * Special case '-V|--version'
+        */
+       if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
+               return (zpool_do_version(argc, argv));
+
        if ((g_zfs = libzfs_init()) == NULL) {
                (void) fprintf(stderr, "%s", libzfs_error_init(errno));
                return (1);
index b604f1194dc68940b6a2ffe39b80cef2b7b38b14..8d79fe69ed7b2618fcd1e74f619b3855b7f1e355 100644 (file)
@@ -815,6 +815,13 @@ void libzfs_free_str_array(char **strs, int count);
 
 int libzfs_envvar_is_set(char *envvar);
 
+/*
+ * Utility functions for zfs version
+ */
+extern void zfs_version_userland(char *, int);
+extern int zfs_version_kernel(char *, int);
+extern int zfs_version_print(void);
+
 /*
  * Given a device or file, determine if it is part of a pool.
  */
index 23dcb11bdab48ef655b48ee86d7d8759a7eb8a3a..b988d8f313d194afcc45a8aefafbc72c59acf653 100644 (file)
@@ -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);
+}
index 36ab9353fa74b7df0e4df50338c9e14c4d8a2e4d..69036d04caf2a6e3a3b201d2b665fdbc908d0237 100644 (file)
@@ -38,7 +38,7 @@
 .Nd configures ZFS file systems
 .Sh SYNOPSIS
 .Nm
-.Fl ?
+.Fl ?V
 .Nm
 .Cm create
 .Op Fl p
 .Fl i
 .Op Fl l
 .Ar filesystem
+.Nm
+.Cm version
 .Sh DESCRIPTION
 The
 .Nm
@@ -2453,6 +2455,13 @@ original form.
 Displays a help message.
 .It Xo
 .Nm
+.Fl V, -version
+.Xc
+An alias for the
+.Nm zfs Cm version
+subcommand.
+.It Xo
+.Nm
 .Cm create
 .Op Fl p
 .Oo Fl o Ar property Ns = Ns Ar value Oc Ns ...
@@ -4624,6 +4633,13 @@ Indicates that zfs should make
 inherit the key of its parent. Note that this command can only be run on an
 encryption root that has an encrypted parent.
 .El
+.It Xo
+.Nm
+.Cm version
+.Xc
+Displays the software version of the
+.Nm
+userland utility and the zfs kernel module.
 .El
 .Sh EXIT STATUS
 The
index ebdb7225113b77ff1d18936292a005e7e3608238..e644f7e6a20f2b872be1d1759781c98b25a24411 100644 (file)
@@ -35,7 +35,7 @@
 .Nd configure ZFS storage pools
 .Sh SYNOPSIS
 .Nm
-.Fl ?
+.Fl ?V
 .Nm
 .Cm add
 .Op Fl fgLnP
 .Cm upgrade
 .Op Fl V Ar version
 .Fl a Ns | Ns Ar pool Ns ...
+.Nm
+.Cm version
 .Sh DESCRIPTION
 The
 .Nm
@@ -933,6 +935,13 @@ The following subcommands are supported:
 Displays a help message.
 .It Xo
 .Nm
+.Fl V, -version
+.Xc
+An alias for the
+.Nm zpool Cm version
+subcommand.
+.It Xo
+.Nm
 .Cm add
 .Op Fl fgLnP
 .Oo Fl o Ar property Ns = Ns Ar value Oc
@@ -2465,6 +2474,13 @@ flag is specified, no features will be enabled on the pool.
 This option can only be used to increase the version number up to the last
 supported legacy version number.
 .El
+.It Xo
+.Nm
+.Cm version
+.Xc
+Displays the software version of the
+.Nm
+userland utility and the zfs kernel module.
 .El
 .Sh EXIT STATUS
 The following exit values are returned: