Add -p switch to "zpool get"
[zfs.git] / cmd / zpool / zpool_main.c
index 3200698..2632e8c 100644 (file)
@@ -63,6 +63,7 @@ static int zpool_do_destroy(int, char **);
 
 static int zpool_do_add(int, char **);
 static int zpool_do_remove(int, char **);
+static int zpool_do_labelclear(int, char **);
 
 static int zpool_do_list(int, char **);
 static int zpool_do_iostat(int, char **);
@@ -123,6 +124,7 @@ typedef enum {
        HELP_HISTORY,
        HELP_IMPORT,
        HELP_IOSTAT,
+       HELP_LABELCLEAR,
        HELP_LIST,
        HELP_OFFLINE,
        HELP_ONLINE,
@@ -162,6 +164,8 @@ static zpool_command_t command_table[] = {
        { "add",        zpool_do_add,           HELP_ADD                },
        { "remove",     zpool_do_remove,        HELP_REMOVE             },
        { NULL },
+       { "labelclear", zpool_do_labelclear,    HELP_LABELCLEAR         },
+       { NULL },
        { "list",       zpool_do_list,          HELP_LIST               },
        { "iostat",     zpool_do_iostat,        HELP_IOSTAT             },
        { "status",     zpool_do_status,        HELP_STATUS             },
@@ -233,8 +237,10 @@ get_usage(zpool_help_t idx) {
        case HELP_IOSTAT:
                return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
                    "[count]]\n"));
+       case HELP_LABELCLEAR:
+               return (gettext("\tlabelclear [-f] <vdev>\n"));
        case HELP_LIST:
-               return (gettext("\tlist [-H] [-o property[,...]] "
+               return (gettext("\tlist [-Hv] [-o property[,...]] "
                    "[-T d|u] [pool] ... [interval [count]]\n"));
        case HELP_OFFLINE:
                return (gettext("\toffline [-t] <pool> <device> ...\n"));
@@ -246,7 +252,7 @@ get_usage(zpool_help_t idx) {
        case HELP_REMOVE:
                return (gettext("\tremove <pool> <device> ...\n"));
        case HELP_REOPEN:
-               return (""); /* Undocumented command */
+               return (gettext("\treopen <pool>\n"));
        case HELP_SCRUB:
                return (gettext("\tscrub [-s] <pool> ...\n"));
        case HELP_STATUS:
@@ -259,7 +265,7 @@ get_usage(zpool_help_t idx) {
        case HELP_EVENTS:
                return (gettext("\tevents [-vHfc]\n"));
        case HELP_GET:
-               return (gettext("\tget <\"all\" | property[,...]> "
+               return (gettext("\tget [-p] <\"all\" | property[,...]> "
                    "<pool> ...\n"));
        case HELP_SET:
                return (gettext("\tset <property=value> <pool> \n"));
@@ -642,6 +648,127 @@ zpool_do_remove(int argc, char **argv)
 }
 
 /*
+ * zpool labelclear <vdev>
+ *
+ * Verifies that the vdev is not active and zeros out the label information
+ * on the device.
+ */
+int
+zpool_do_labelclear(int argc, char **argv)
+{
+       char *vdev, *name;
+       int c, fd = -1, ret = 0;
+       pool_state_t state;
+       boolean_t inuse = B_FALSE;
+       boolean_t force = B_FALSE;
+
+       /* check options */
+       while ((c = getopt(argc, argv, "f")) != -1) {
+               switch (c) {
+               case 'f':
+                       force = B_TRUE;
+                       break;
+               default:
+                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
+                           optopt);
+                       usage(B_FALSE);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       /* get vdev name */
+       if (argc < 1) {
+               (void) fprintf(stderr, gettext("missing vdev device name\n"));
+               usage(B_FALSE);
+       }
+
+       vdev = argv[0];
+       if ((fd = open(vdev, O_RDWR)) < 0) {
+               (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
+               return (B_FALSE);
+       }
+
+       name = NULL;
+       if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
+               if (force)
+                       goto wipe_label;
+
+               (void) fprintf(stderr,
+                   gettext("Unable to determine pool state for %s\n"
+                   "Use -f to force the clearing any label data\n"), vdev);
+
+               return (1);
+       }
+
+       if (inuse) {
+               switch (state) {
+               default:
+               case POOL_STATE_ACTIVE:
+               case POOL_STATE_SPARE:
+               case POOL_STATE_L2CACHE:
+                       (void) fprintf(stderr,
+                           gettext("labelclear operation failed.\n"
+                           "\tVdev %s is a member (%s), of pool \"%s\".\n"
+                           "\tTo remove label information from this device, "
+                           "export or destroy\n\tthe pool, or remove %s from "
+                           "the configuration of this pool\n\tand retry the "
+                           "labelclear operation.\n"),
+                           vdev, zpool_pool_state_to_name(state), name, vdev);
+                       ret = 1;
+                       goto errout;
+
+               case POOL_STATE_EXPORTED:
+                       if (force)
+                               break;
+
+                       (void) fprintf(stderr,
+                           gettext("labelclear operation failed.\n\tVdev "
+                           "%s is a member of the exported pool \"%s\".\n"
+                           "\tUse \"zpool labelclear -f %s\" to force the "
+                           "removal of label\n\tinformation.\n"),
+                           vdev, name, vdev);
+                       ret = 1;
+                       goto errout;
+
+               case POOL_STATE_POTENTIALLY_ACTIVE:
+                       if (force)
+                               break;
+
+                       (void) fprintf(stderr,
+                           gettext("labelclear operation failed.\n"
+                           "\tVdev %s is a member of the pool \"%s\".\n"
+                           "\tThis pool is unknown to this system, but may "
+                           "be active on\n\tanother system. Use "
+                           "\'zpool labelclear -f %s\' to force the\n"
+                           "\tremoval of label information.\n"),
+                           vdev, name, vdev);
+                       ret = 1;
+                       goto errout;
+
+               case POOL_STATE_DESTROYED:
+                       /* inuse should never be set for a destroyed pool... */
+                       break;
+               }
+       }
+
+wipe_label:
+       if (zpool_clear_label(fd) != 0) {
+               (void) fprintf(stderr,
+                   gettext("Label clear failed on vdev %s\n"), vdev);
+               ret = 1;
+       }
+
+errout:
+       close(fd);
+       if (name != NULL)
+               free(name);
+
+       return (ret);
+}
+
+/*
  * zpool create [-fnd] [-o property=value] ...
  *             [-O file-system-property=value] ...
  *             [-R root] [-m mountpoint] <pool> <dev> ...
@@ -2932,7 +3059,7 @@ int
 zpool_do_list(int argc, char **argv)
 {
        int c;
-       int ret;
+       int ret = 0;
        list_cbdata_t cb = { 0 };
        static char default_props[] =
            "name,size,allocated,free,capacity,dedupratio,"
@@ -2971,6 +3098,9 @@ zpool_do_list(int argc, char **argv)
 
        argc -= optind;
        argv += optind;
+       fprintf(stderr, "argc = %d\n", argc);
+       for (c=0; c<argc; c++)
+               fprintf(stderr, "argv[%d] = %s\n", argc, argv[c]);
 
        get_interval_count(&argc, argv, &interval, &count);
 
@@ -3612,22 +3742,37 @@ zpool_do_reguid(int argc, char **argv)
  * zpool reopen <pool>
  *
  * Reopen the pool so that the kernel can update the sizes of all vdevs.
- *
- * NOTE: This command is currently undocumented.  If the command is ever
- * exposed then the appropriate usage() messages will need to be made.
  */
 int
 zpool_do_reopen(int argc, char **argv)
 {
+       int c;
        int ret = 0;
        zpool_handle_t *zhp;
        char *pool;
 
+       /* check options */
+       while ((c = getopt(argc, argv, "")) != -1) {
+               switch (c) {
+               case '?':
+                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
+                           optopt);
+                       usage(B_FALSE);
+               }
+       }
+
        argc--;
        argv++;
 
-       if (argc != 1)
-               return (2);
+       if (argc < 1) {
+               (void) fprintf(stderr, gettext("missing pool name\n"));
+               usage(B_FALSE);
+       }
+
+       if (argc > 1) {
+               (void) fprintf(stderr, gettext("too many arguments\n"));
+               usage(B_FALSE);
+       }
 
        pool = argv[0];
        if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
@@ -5228,6 +5373,7 @@ zpool_do_events_next(ev_opts_t *opts)
                        zpool_do_events_nvprint(nvl, 8);
                        printf(gettext("\n"));
                }
+               (void) fflush(stdout);
 
                nvlist_free(nvl);
        }
@@ -5323,8 +5469,8 @@ get_callback(zpool_handle_t *zhp, void *data)
                                    NULL, NULL);
                        }
                } else {
-                       if (zpool_get_prop(zhp, pl->pl_prop, value,
-                           sizeof (value), &srctype) != 0)
+                       if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
+                           sizeof (value), &srctype, cbp->cb_literal) != 0)
                                continue;
 
                        zprop_print_one_property(zpool_get_name(zhp), cbp,
@@ -5340,9 +5486,26 @@ zpool_do_get(int argc, char **argv)
 {
        zprop_get_cbdata_t cb = { 0 };
        zprop_list_t fake_name = { 0 };
-       int ret;
+       int c, ret;
 
-       if (argc < 2) {
+       /* check options */
+       while ((c = getopt(argc, argv, "p")) != -1) {
+               switch (c) {
+               case 'p':
+                       cb.cb_literal = B_TRUE;
+                       break;
+
+               case '?':
+                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
+                           optopt);
+                       usage(B_FALSE);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       if (argc < 1) {
                (void) fprintf(stderr, gettext("missing property "
                    "argument\n"));
                usage(B_FALSE);
@@ -5356,7 +5519,7 @@ zpool_do_get(int argc, char **argv)
        cb.cb_columns[3] = GET_COL_SOURCE;
        cb.cb_type = ZFS_TYPE_POOL;
 
-       if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
+       if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
            ZFS_TYPE_POOL) != 0)
                usage(B_FALSE);
 
@@ -5367,7 +5530,7 @@ zpool_do_get(int argc, char **argv)
                cb.cb_proplist = &fake_name;
        }
 
-       ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
+       ret = for_each_pool(argc - 1, argv + 1, B_TRUE, &cb.cb_proplist,
            get_callback, &cb);
 
        if (cb.cb_proplist == &fake_name)