Print this page
5882 Temporary pool names
Reviewed by: Matt Ahrens <matt@delphix.com>
Reviewed by: Igor Kozhukhov <igor@dilos.org>
Reviewed by: John Kennedy <john.kennedy@delphix.com>
Approved by: Dan McDonald <danmcd@joyent.com>


 203 
 204 static zpool_command_t *current_command;
 205 static char history_str[HIS_MAX_RECORD_LEN];
 206 static boolean_t log_history = B_TRUE;
 207 static uint_t timestamp_fmt = NODATE;
 208 
 209 static const char *
 210 get_usage(zpool_help_t idx)
 211 {
 212         switch (idx) {
 213         case HELP_ADD:
 214                 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
 215         case HELP_ATTACH:
 216                 return (gettext("\tattach [-f] <pool> <device> "
 217                     "<new-device>\n"));
 218         case HELP_CLEAR:
 219                 return (gettext("\tclear [-nF] <pool> [device]\n"));
 220         case HELP_CREATE:
 221                 return (gettext("\tcreate [-fnd] [-B] "
 222                     "[-o property=value] ... \n"
 223                     "\t    [-O file-system-property=value] ... \n"
 224                     "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));

 225         case HELP_CHECKPOINT:
 226                 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
 227         case HELP_DESTROY:
 228                 return (gettext("\tdestroy [-f] <pool>\n"));
 229         case HELP_DETACH:
 230                 return (gettext("\tdetach <pool> <device>\n"));
 231         case HELP_EXPORT:
 232                 return (gettext("\texport [-f] <pool> ...\n"));
 233         case HELP_HISTORY:
 234                 return (gettext("\thistory [-il] [<pool>] ...\n"));
 235         case HELP_IMPORT:
 236                 return (gettext("\timport [-d dir] [-D]\n"
 237                     "\timport [-o mntopts] [-o property=value] ... \n"
 238                     "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
 239                     "[-R root] [-F [-n]] -a\n"
 240                     "\timport [-o mntopts] [-o property=value] ... \n"
 241                     "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
 242                     "[-R root] [-F [-n]]\n"
 243                     "\t    [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
 244         case HELP_IOSTAT:
 245                 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
 246                     "[count]]\n"));
 247         case HELP_LABELCLEAR:
 248                 return (gettext("\tlabelclear [-f] <vdev>\n"));
 249         case HELP_LIST:
 250                 return (gettext("\tlist [-Hp] [-o property[,...]] "
 251                     "[-T d|u] [pool] ... [interval [count]]\n"));
 252         case HELP_OFFLINE:
 253                 return (gettext("\toffline [-t] <pool> <device> ...\n"));
 254         case HELP_ONLINE:
 255                 return (gettext("\tonline <pool> <device> ...\n"));
 256         case HELP_REPLACE:
 257                 return (gettext("\treplace [-f] <pool> <device> "
 258                     "[new-device]\n"));
 259         case HELP_REMOVE:
 260                 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
 261         case HELP_REOPEN:
 262                 return (gettext("\treopen <pool>\n"));


 475                 }
 476         }
 477 
 478         if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
 479             prop != ZPOOL_PROP_CACHEFILE) {
 480                 (void) fprintf(stderr, gettext("property '%s' "
 481                     "specified multiple times\n"), propname);
 482                 return (2);
 483         }
 484 
 485         if (nvlist_add_string(proplist, normnm, propval) != 0) {
 486                 (void) fprintf(stderr, gettext("internal "
 487                     "error: out of memory\n"));
 488                 return (1);
 489         }
 490 
 491         return (0);
 492 }
 493 
 494 /*















 495  * zpool add [-fn] <pool> <vdev> ...
 496  *
 497  *      -f      Force addition of devices, even if they appear in use
 498  *      -n      Do not add the devices, but display the resulting layout if
 499  *              they were to be added.
 500  *
 501  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
 502  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
 503  * libzfs.
 504  */
 505 int
 506 zpool_do_add(int argc, char **argv)
 507 {
 508         boolean_t force = B_FALSE;
 509         boolean_t dryrun = B_FALSE;
 510         int c;
 511         nvlist_t *nvroot;
 512         char *poolname;
 513         zpool_boot_label_t boot_type;
 514         uint64_t boot_size;


 832                 break;
 833         }
 834 
 835 wipe_label:
 836         ret = zpool_clear_label(fd);
 837         if (ret != 0) {
 838                 (void) fprintf(stderr,
 839                     gettext("failed to clear label for %s\n"), vdev);
 840         }
 841 
 842 errout:
 843         free(name);
 844         (void) close(fd);
 845 
 846         return (ret);
 847 }
 848 
 849 /*
 850  * zpool create [-fnd] [-B] [-o property=value] ...
 851  *              [-O file-system-property=value] ...
 852  *              [-R root] [-m mountpoint] <pool> <dev> ...
 853  *
 854  *      -B      Create boot partition.
 855  *      -f      Force creation, even if devices appear in use
 856  *      -n      Do not create the pool, but display the resulting layout if it
 857  *              were to be created.
 858  *      -R      Create a pool under an alternate root
 859  *      -m      Set default mountpoint for the root dataset.  By default it's
 860  *              '/<pool>'

 861  *      -o      Set property=value.
 862  *      -d      Don't automatically enable all supported pool features
 863  *              (individual features can be enabled with -o).
 864  *      -O      Set fsproperty=value in the pool's root file system
 865  *
 866  * Creates the named pool according to the given vdev specification.  The
 867  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
 868  * we get the nvlist back from get_vdev_spec(), we either print out the contents
 869  * (if '-n' was specified), or pass it to libzfs to do the creation.
 870  */
 871 
 872 #define SYSTEM256       (256 * 1024 * 1024)
 873 int
 874 zpool_do_create(int argc, char **argv)
 875 {
 876         boolean_t force = B_FALSE;
 877         boolean_t dryrun = B_FALSE;
 878         boolean_t enable_all_pool_feat = B_TRUE;
 879         zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
 880         uint64_t boot_size = 0;
 881         int c;
 882         nvlist_t *nvroot = NULL;
 883         char *poolname;

 884         int ret = 1;
 885         char *altroot = NULL;
 886         char *mountpoint = NULL;
 887         nvlist_t *fsprops = NULL;
 888         nvlist_t *props = NULL;
 889         char *propval;
 890 
 891         /* check options */
 892         while ((c = getopt(argc, argv, ":fndBR:m:o:O:")) != -1) {
 893                 switch (c) {
 894                 case 'f':
 895                         force = B_TRUE;
 896                         break;
 897                 case 'n':
 898                         dryrun = B_TRUE;
 899                         break;
 900                 case 'd':
 901                         enable_all_pool_feat = B_FALSE;
 902                         break;
 903                 case 'B':
 904                         /*
 905                          * We should create the system partition.
 906                          * Also make sure the size is set.
 907                          */
 908                         boot_type = ZPOOL_CREATE_BOOT_LABEL;
 909                         if (boot_size == 0)
 910                                 boot_size = SYSTEM256;
 911                         break;
 912                 case 'R':
 913                         altroot = optarg;
 914                         if (add_prop_list(zpool_prop_to_name(
 915                             ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
 916                                 goto errout;
 917                         if (nvlist_lookup_string(props,
 918                             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
 919                             &propval) == 0)
 920                                 break;
 921                         if (add_prop_list(zpool_prop_to_name(
 922                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
 923                                 goto errout;
 924                         break;
 925                 case 'm':
 926                         /* Equivalent to -O mountpoint=optarg */
 927                         mountpoint = optarg;
 928                         break;
 929                 case 'o':
 930                         if ((propval = strchr(optarg, '=')) == NULL) {
 931                                 (void) fprintf(stderr, gettext("missing "
 932                                     "'=' for -o option\n"));
 933                                 goto errout;
 934                         }
 935                         *propval = '\0';
 936                         propval++;
 937 
 938                         if (add_prop_list(optarg, propval, &props, B_TRUE))
 939                                 goto errout;
 940 
 941                         /*


 974                                 (void) fprintf(stderr, gettext("missing "
 975                                     "'=' for -O option\n"));
 976                                 goto errout;
 977                         }
 978                         *propval = '\0';
 979                         propval++;
 980 
 981                         /*
 982                          * Mountpoints are checked and then added later.
 983                          * Uniquely among properties, they can be specified
 984                          * more than once, to avoid conflict with -m.
 985                          */
 986                         if (0 == strcmp(optarg,
 987                             zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
 988                                 mountpoint = propval;
 989                         } else if (add_prop_list(optarg, propval, &fsprops,
 990                             B_FALSE)) {
 991                                 goto errout;
 992                         }
 993                         break;





















 994                 case ':':
 995                         (void) fprintf(stderr, gettext("missing argument for "
 996                             "'%c' option\n"), optopt);
 997                         goto badusage;
 998                 case '?':
 999                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1000                             optopt);
1001                         goto badusage;
1002                 }
1003         }
1004 
1005         argc -= optind;
1006         argv += optind;
1007 
1008         /* get pool name and check number of arguments */
1009         if (argc < 1) {
1010                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1011                 goto badusage;
1012         }
1013         if (argc < 2) {


1179                                 (void) snprintf(propname, sizeof (propname),
1180                                     "feature@%s", feat->fi_uname);
1181 
1182                                 /*
1183                                  * Skip feature if user specified it manually
1184                                  * on the command line.
1185                                  */
1186                                 if (nvlist_exists(props, propname))
1187                                         continue;
1188 
1189                                 ret = add_prop_list(propname,
1190                                     ZFS_FEATURE_ENABLED, &props, B_TRUE);
1191                                 if (ret != 0)
1192                                         goto errout;
1193                         }
1194                 }
1195 
1196                 ret = 1;
1197                 if (zpool_create(g_zfs, poolname,
1198                     nvroot, props, fsprops) == 0) {
1199                         zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1200                             ZFS_TYPE_FILESYSTEM);
1201                         if (pool != NULL) {
1202                                 if (zfs_mount(pool, NULL, 0) == 0)
1203                                         ret = zfs_shareall(pool);
1204                                 zfs_close(pool);
1205                         }
1206                 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1207                         (void) fprintf(stderr, gettext("pool name may have "
1208                             "been omitted\n"));
1209                 }
1210         }
1211 
1212 errout:
1213         nvlist_free(nvroot);
1214         nvlist_free(fsprops);
1215         nvlist_free(props);
1216         return (ret);
1217 badusage:
1218         nvlist_free(fsprops);
1219         nvlist_free(props);
1220         usage(B_FALSE);


2155                 return (1);
2156         }
2157 
2158         if (discard)
2159                 err = (zpool_discard_checkpoint(zhp) != 0);
2160         else
2161                 err = (zpool_checkpoint(zhp) != 0);
2162 
2163         zpool_close(zhp);
2164 
2165         return (err);
2166 }
2167 
2168 #define CHECKPOINT_OPT  1024
2169 
2170 /*
2171  * zpool import [-d dir] [-D]
2172  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2173  *              [-d dir | -c cachefile] [-f] -a
2174  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2175  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]

2176  *
2177  *       -c     Read pool information from a cachefile instead of searching
2178  *              devices.
2179  *
2180  *       -d     Scan in a specific directory, other than /dev/dsk.  More than
2181  *              one directory can be specified using multiple '-d' options.
2182  *
2183  *       -D     Scan for previously destroyed pools or import all or only
2184  *              specified destroyed pools.
2185  *
2186  *       -R     Temporarily import the pool, with all mountpoints relative to
2187  *              the given root.  The pool will remain exported when the machine
2188  *              is rebooted.
2189  *
2190  *       -V     Import even in the presence of faulted vdevs.  This is an
2191  *              intentionally undocumented option for testing purposes, and
2192  *              treats the pool configuration as complete, leaving any bad
2193  *              vdevs in the FAULTED state. In other words, it does verbatim
2194  *              import.
2195  *
2196  *       -f     Force import, even if it appears that the pool is active.
2197  *
2198  *       -F     Attempt rewind if necessary.
2199  *
2200  *       -n     See if rewind would work, but don't actually rewind.
2201  *
2202  *       -N     Import the pool but don't mount datasets.
2203  *



2204  *       -T     Specify a starting txg to use for import. This option is
2205  *              intentionally undocumented option for testing purposes.
2206  *
2207  *       -a     Import all pools found.
2208  *
2209  *       -o     Set property=value and/or temporary mount options (without '=').
2210  *
2211  *       --rewind-to-checkpoint
2212  *              Import the pool and revert back to the checkpoint.
2213  *
2214  * The import command scans for pools to import, and import pools based on pool
2215  * name and GUID.  The pool can also be renamed as part of the import process.
2216  */
2217 int
2218 zpool_do_import(int argc, char **argv)
2219 {
2220         char **searchdirs = NULL;
2221         int nsearch = 0;
2222         int c;
2223         int err = 0;


2234         nvlist_t *policy = NULL;
2235         nvlist_t *props = NULL;
2236         boolean_t first;
2237         int flags = ZFS_IMPORT_NORMAL;
2238         uint32_t rewind_policy = ZPOOL_NO_REWIND;
2239         boolean_t dryrun = B_FALSE;
2240         boolean_t do_rewind = B_FALSE;
2241         boolean_t xtreme_rewind = B_FALSE;
2242         uint64_t pool_state, txg = -1ULL;
2243         char *cachefile = NULL;
2244         importargs_t idata = { 0 };
2245         char *endptr;
2246 
2247 
2248         struct option long_options[] = {
2249                 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
2250                 {0, 0, 0, 0}
2251         };
2252 
2253         /* check options */
2254         while ((c = getopt_long(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX",
2255             long_options, NULL)) != -1) {
2256                 switch (c) {
2257                 case 'a':
2258                         do_all = B_TRUE;
2259                         break;
2260                 case 'c':
2261                         cachefile = optarg;
2262                         break;
2263                 case 'd':
2264                         if (searchdirs == NULL) {
2265                                 searchdirs = safe_malloc(sizeof (char *));
2266                         } else {
2267                                 char **tmp = safe_malloc((nsearch + 1) *
2268                                     sizeof (char *));
2269                                 bcopy(searchdirs, tmp, nsearch *
2270                                     sizeof (char *));
2271                                 free(searchdirs);
2272                                 searchdirs = tmp;
2273                         }
2274                         searchdirs[nsearch++] = optarg;


2289                         dryrun = B_TRUE;
2290                         break;
2291                 case 'N':
2292                         flags |= ZFS_IMPORT_ONLY;
2293                         break;
2294                 case 'o':
2295                         if ((propval = strchr(optarg, '=')) != NULL) {
2296                                 *propval = '\0';
2297                                 propval++;
2298                                 if (add_prop_list(optarg, propval,
2299                                     &props, B_TRUE))
2300                                         goto error;
2301                         } else {
2302                                 mntopts = optarg;
2303                         }
2304                         break;
2305                 case 'R':
2306                         if (add_prop_list(zpool_prop_to_name(
2307                             ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2308                                 goto error;
2309                         if (nvlist_lookup_string(props,
2310                             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2311                             &propval) == 0)
2312                                 break;
2313                         if (add_prop_list(zpool_prop_to_name(


2314                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2315                                 goto error;
2316                         break;
2317                 case 'T':
2318                         errno = 0;
2319                         txg = strtoull(optarg, &endptr, 0);
2320                         if (errno != 0 || *endptr != '\0') {
2321                                 (void) fprintf(stderr,
2322                                     gettext("invalid txg value\n"));
2323                                 usage(B_FALSE);
2324                         }
2325                         rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2326                         break;
2327                 case 'V':
2328                         flags |= ZFS_IMPORT_VERBATIM;
2329                         break;
2330                 case 'X':
2331                         xtreme_rewind = B_TRUE;
2332                         break;
2333                 case CHECKPOINT_OPT:


2432                  * User specified a name or guid.  Ensure it's unique.
2433                  */
2434                 idata.unique = B_TRUE;
2435         }
2436 
2437 
2438         idata.path = searchdirs;
2439         idata.paths = nsearch;
2440         idata.poolname = searchname;
2441         idata.guid = searchguid;
2442         idata.cachefile = cachefile;
2443         idata.policy = policy;
2444 
2445         pools = zpool_search_import(g_zfs, &idata);
2446 
2447         if (pools != NULL && idata.exists &&
2448             (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2449                 (void) fprintf(stderr, gettext("cannot import '%s': "
2450                     "a pool with that name already exists\n"),
2451                     argv[0]);
2452                 (void) fprintf(stderr, gettext("use the form '%s "
2453                     "<pool | id> <newpool>' to give it a new name\n"),
2454                     "zpool import");
2455                 err = 1;
2456         } else if (pools == NULL && idata.exists) {
2457                 (void) fprintf(stderr, gettext("cannot import '%s': "
2458                     "a pool with that name is already created/imported,\n"),
2459                     argv[0]);
2460                 (void) fprintf(stderr, gettext("and no additional pools "
2461                     "with that name were found\n"));
2462                 err = 1;
2463         } else if (pools == NULL) {
2464                 if (argc != 0) {
2465                         (void) fprintf(stderr, gettext("cannot import '%s': "
2466                             "no such pool available\n"), argv[0]);
2467                 }
2468                 err = 1;
2469         }
2470 
2471         if (err == 1) {
2472                 free(searchdirs);
2473                 nvlist_free(policy);
2474                 return (1);




 203 
 204 static zpool_command_t *current_command;
 205 static char history_str[HIS_MAX_RECORD_LEN];
 206 static boolean_t log_history = B_TRUE;
 207 static uint_t timestamp_fmt = NODATE;
 208 
 209 static const char *
 210 get_usage(zpool_help_t idx)
 211 {
 212         switch (idx) {
 213         case HELP_ADD:
 214                 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
 215         case HELP_ATTACH:
 216                 return (gettext("\tattach [-f] <pool> <device> "
 217                     "<new-device>\n"));
 218         case HELP_CLEAR:
 219                 return (gettext("\tclear [-nF] <pool> [device]\n"));
 220         case HELP_CREATE:
 221                 return (gettext("\tcreate [-fnd] [-B] "
 222                     "[-o property=value] ... \n"
 223                     "\t    [-O file-system-property=value] ...\n"
 224                     "\t    [-m mountpoint] [-R root] [-t tempname] "
 225                     "<pool> <vdev> ...\n"));
 226         case HELP_CHECKPOINT:
 227                 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
 228         case HELP_DESTROY:
 229                 return (gettext("\tdestroy [-f] <pool>\n"));
 230         case HELP_DETACH:
 231                 return (gettext("\tdetach <pool> <device>\n"));
 232         case HELP_EXPORT:
 233                 return (gettext("\texport [-f] <pool> ...\n"));
 234         case HELP_HISTORY:
 235                 return (gettext("\thistory [-il] [<pool>] ...\n"));
 236         case HELP_IMPORT:
 237                 return (gettext("\timport [-d dir] [-D]\n"
 238                     "\timport [-o mntopts] [-o property=value] ... \n"
 239                     "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
 240                     "[-R root] [-F [-n]] -a\n"
 241                     "\timport [-o mntopts] [-o property=value] ... \n"
 242                     "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
 243                     "[-R root] [-F [-n]] [-t]\n"
 244                     "\t    [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
 245         case HELP_IOSTAT:
 246                 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
 247                     "[count]]\n"));
 248         case HELP_LABELCLEAR:
 249                 return (gettext("\tlabelclear [-f] <vdev>\n"));
 250         case HELP_LIST:
 251                 return (gettext("\tlist [-Hp] [-o property[,...]] "
 252                     "[-T d|u] [pool] ... [interval [count]]\n"));
 253         case HELP_OFFLINE:
 254                 return (gettext("\toffline [-t] <pool> <device> ...\n"));
 255         case HELP_ONLINE:
 256                 return (gettext("\tonline <pool> <device> ...\n"));
 257         case HELP_REPLACE:
 258                 return (gettext("\treplace [-f] <pool> <device> "
 259                     "[new-device]\n"));
 260         case HELP_REMOVE:
 261                 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
 262         case HELP_REOPEN:
 263                 return (gettext("\treopen <pool>\n"));


 476                 }
 477         }
 478 
 479         if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
 480             prop != ZPOOL_PROP_CACHEFILE) {
 481                 (void) fprintf(stderr, gettext("property '%s' "
 482                     "specified multiple times\n"), propname);
 483                 return (2);
 484         }
 485 
 486         if (nvlist_add_string(proplist, normnm, propval) != 0) {
 487                 (void) fprintf(stderr, gettext("internal "
 488                     "error: out of memory\n"));
 489                 return (1);
 490         }
 491 
 492         return (0);
 493 }
 494 
 495 /*
 496  * Set a default property pair (name, string-value) in a property nvlist
 497  */
 498 static int
 499 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
 500     boolean_t poolprop)
 501 {
 502         char *pval;
 503 
 504         if (nvlist_lookup_string(*props, propname, &pval) == 0)
 505                 return (0);
 506 
 507         return (add_prop_list(propname, propval, props, poolprop));
 508 }
 509 
 510 /*
 511  * zpool add [-fn] <pool> <vdev> ...
 512  *
 513  *      -f      Force addition of devices, even if they appear in use
 514  *      -n      Do not add the devices, but display the resulting layout if
 515  *              they were to be added.
 516  *
 517  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
 518  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
 519  * libzfs.
 520  */
 521 int
 522 zpool_do_add(int argc, char **argv)
 523 {
 524         boolean_t force = B_FALSE;
 525         boolean_t dryrun = B_FALSE;
 526         int c;
 527         nvlist_t *nvroot;
 528         char *poolname;
 529         zpool_boot_label_t boot_type;
 530         uint64_t boot_size;


 848                 break;
 849         }
 850 
 851 wipe_label:
 852         ret = zpool_clear_label(fd);
 853         if (ret != 0) {
 854                 (void) fprintf(stderr,
 855                     gettext("failed to clear label for %s\n"), vdev);
 856         }
 857 
 858 errout:
 859         free(name);
 860         (void) close(fd);
 861 
 862         return (ret);
 863 }
 864 
 865 /*
 866  * zpool create [-fnd] [-B] [-o property=value] ...
 867  *              [-O file-system-property=value] ...
 868  *              [-R root] [-m mountpoint] [-t tempname] <pool> <dev> ...
 869  *
 870  *      -B      Create boot partition.
 871  *      -f      Force creation, even if devices appear in use
 872  *      -n      Do not create the pool, but display the resulting layout if it
 873  *              were to be created.
 874  *      -R      Create a pool under an alternate root
 875  *      -m      Set default mountpoint for the root dataset.  By default it's
 876  *              '/<pool>'
 877  *      -t      Use the temporary name until the pool is exported.
 878  *      -o      Set property=value.
 879  *      -d      Don't automatically enable all supported pool features
 880  *              (individual features can be enabled with -o).
 881  *      -O      Set fsproperty=value in the pool's root file system
 882  *
 883  * Creates the named pool according to the given vdev specification.  The
 884  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
 885  * we get the nvlist back from get_vdev_spec(), we either print out the contents
 886  * (if '-n' was specified), or pass it to libzfs to do the creation.
 887  */
 888 
 889 #define SYSTEM256       (256 * 1024 * 1024)
 890 int
 891 zpool_do_create(int argc, char **argv)
 892 {
 893         boolean_t force = B_FALSE;
 894         boolean_t dryrun = B_FALSE;
 895         boolean_t enable_all_pool_feat = B_TRUE;
 896         zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
 897         uint64_t boot_size = 0;
 898         int c;
 899         nvlist_t *nvroot = NULL;
 900         char *poolname;
 901         char *tname = NULL;
 902         int ret = 1;
 903         char *altroot = NULL;
 904         char *mountpoint = NULL;
 905         nvlist_t *fsprops = NULL;
 906         nvlist_t *props = NULL;
 907         char *propval;
 908 
 909         /* check options */
 910         while ((c = getopt(argc, argv, ":fndBR:m:o:O:t:")) != -1) {
 911                 switch (c) {
 912                 case 'f':
 913                         force = B_TRUE;
 914                         break;
 915                 case 'n':
 916                         dryrun = B_TRUE;
 917                         break;
 918                 case 'd':
 919                         enable_all_pool_feat = B_FALSE;
 920                         break;
 921                 case 'B':
 922                         /*
 923                          * We should create the system partition.
 924                          * Also make sure the size is set.
 925                          */
 926                         boot_type = ZPOOL_CREATE_BOOT_LABEL;
 927                         if (boot_size == 0)
 928                                 boot_size = SYSTEM256;
 929                         break;
 930                 case 'R':
 931                         altroot = optarg;
 932                         if (add_prop_list(zpool_prop_to_name(
 933                             ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
 934                                 goto errout;
 935                         if (add_prop_list_default(zpool_prop_to_name(




 936                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
 937                                 goto errout;
 938                         break;
 939                 case 'm':
 940                         /* Equivalent to -O mountpoint=optarg */
 941                         mountpoint = optarg;
 942                         break;
 943                 case 'o':
 944                         if ((propval = strchr(optarg, '=')) == NULL) {
 945                                 (void) fprintf(stderr, gettext("missing "
 946                                     "'=' for -o option\n"));
 947                                 goto errout;
 948                         }
 949                         *propval = '\0';
 950                         propval++;
 951 
 952                         if (add_prop_list(optarg, propval, &props, B_TRUE))
 953                                 goto errout;
 954 
 955                         /*


 988                                 (void) fprintf(stderr, gettext("missing "
 989                                     "'=' for -O option\n"));
 990                                 goto errout;
 991                         }
 992                         *propval = '\0';
 993                         propval++;
 994 
 995                         /*
 996                          * Mountpoints are checked and then added later.
 997                          * Uniquely among properties, they can be specified
 998                          * more than once, to avoid conflict with -m.
 999                          */
1000                         if (0 == strcmp(optarg,
1001                             zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1002                                 mountpoint = propval;
1003                         } else if (add_prop_list(optarg, propval, &fsprops,
1004                             B_FALSE)) {
1005                                 goto errout;
1006                         }
1007                         break;
1008                 case 't':
1009                         /*
1010                          * Sanity check temporary pool name.
1011                          */
1012                         if (strchr(optarg, '/') != NULL) {
1013                                 (void) fprintf(stderr, gettext("cannot create "
1014                                     "'%s': invalid character '/' in temporary "
1015                                     "name\n"), optarg);
1016                                 (void) fprintf(stderr, gettext("use 'zfs "
1017                                     "create' to create a dataset\n"));
1018                                 goto errout;
1019                         }
1020 
1021                         if (add_prop_list(zpool_prop_to_name(
1022                             ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1023                                 goto errout;
1024                         if (add_prop_list_default(zpool_prop_to_name(
1025                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1026                                 goto errout;
1027                         tname = optarg;
1028                         break;
1029                 case ':':
1030                         (void) fprintf(stderr, gettext("missing argument for "
1031                             "'%c' option\n"), optopt);
1032                         goto badusage;
1033                 case '?':
1034                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1035                             optopt);
1036                         goto badusage;
1037                 }
1038         }
1039 
1040         argc -= optind;
1041         argv += optind;
1042 
1043         /* get pool name and check number of arguments */
1044         if (argc < 1) {
1045                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1046                 goto badusage;
1047         }
1048         if (argc < 2) {


1214                                 (void) snprintf(propname, sizeof (propname),
1215                                     "feature@%s", feat->fi_uname);
1216 
1217                                 /*
1218                                  * Skip feature if user specified it manually
1219                                  * on the command line.
1220                                  */
1221                                 if (nvlist_exists(props, propname))
1222                                         continue;
1223 
1224                                 ret = add_prop_list(propname,
1225                                     ZFS_FEATURE_ENABLED, &props, B_TRUE);
1226                                 if (ret != 0)
1227                                         goto errout;
1228                         }
1229                 }
1230 
1231                 ret = 1;
1232                 if (zpool_create(g_zfs, poolname,
1233                     nvroot, props, fsprops) == 0) {
1234                         zfs_handle_t *pool = zfs_open(g_zfs,
1235                             tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1236                         if (pool != NULL) {
1237                                 if (zfs_mount(pool, NULL, 0) == 0)
1238                                         ret = zfs_shareall(pool);
1239                                 zfs_close(pool);
1240                         }
1241                 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1242                         (void) fprintf(stderr, gettext("pool name may have "
1243                             "been omitted\n"));
1244                 }
1245         }
1246 
1247 errout:
1248         nvlist_free(nvroot);
1249         nvlist_free(fsprops);
1250         nvlist_free(props);
1251         return (ret);
1252 badusage:
1253         nvlist_free(fsprops);
1254         nvlist_free(props);
1255         usage(B_FALSE);


2190                 return (1);
2191         }
2192 
2193         if (discard)
2194                 err = (zpool_discard_checkpoint(zhp) != 0);
2195         else
2196                 err = (zpool_checkpoint(zhp) != 0);
2197 
2198         zpool_close(zhp);
2199 
2200         return (err);
2201 }
2202 
2203 #define CHECKPOINT_OPT  1024
2204 
2205 /*
2206  * zpool import [-d dir] [-D]
2207  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2208  *              [-d dir | -c cachefile] [-f] -a
2209  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2210  *              [-d dir | -c cachefile] [-f] [-n] [-F] [-t]
2211  *              <pool | id> [newpool]
2212  *
2213  *      -c      Read pool information from a cachefile instead of searching
2214  *              devices.
2215  *
2216  *      -d      Scan in a specific directory, other than /dev/dsk.  More than
2217  *              one directory can be specified using multiple '-d' options.
2218  *
2219  *      -D      Scan for previously destroyed pools or import all or only
2220  *              specified destroyed pools.
2221  *
2222  *      -R      Temporarily import the pool, with all mountpoints relative to
2223  *              the given root.  The pool will remain exported when the machine
2224  *              is rebooted.
2225  *
2226  *      -V      Import even in the presence of faulted vdevs.  This is an
2227  *              intentionally undocumented option for testing purposes, and
2228  *              treats the pool configuration as complete, leaving any bad
2229  *              vdevs in the FAULTED state. In other words, it does verbatim
2230  *              import.
2231  *
2232  *      -f      Force import, even if it appears that the pool is active.
2233  *
2234  *      -F      Attempt rewind if necessary.
2235  *
2236  *      -n      See if rewind would work, but don't actually rewind.
2237  *
2238  *      -N      Import the pool but don't mount datasets.
2239  *
2240  *      -t      Use newpool as a temporary pool name instead of renaming
2241  *              the pool.
2242  *
2243  *      -T      Specify a starting txg to use for import. This option is
2244  *              intentionally undocumented option for testing purposes.
2245  *
2246  *      -a      Import all pools found.
2247  *
2248  *      -o      Set property=value and/or temporary mount options (without '=').
2249  *
2250  *      --rewind-to-checkpoint
2251  *              Import the pool and revert back to the checkpoint.
2252  *
2253  * The import command scans for pools to import, and import pools based on pool
2254  * name and GUID.  The pool can also be renamed as part of the import process.
2255  */
2256 int
2257 zpool_do_import(int argc, char **argv)
2258 {
2259         char **searchdirs = NULL;
2260         int nsearch = 0;
2261         int c;
2262         int err = 0;


2273         nvlist_t *policy = NULL;
2274         nvlist_t *props = NULL;
2275         boolean_t first;
2276         int flags = ZFS_IMPORT_NORMAL;
2277         uint32_t rewind_policy = ZPOOL_NO_REWIND;
2278         boolean_t dryrun = B_FALSE;
2279         boolean_t do_rewind = B_FALSE;
2280         boolean_t xtreme_rewind = B_FALSE;
2281         uint64_t pool_state, txg = -1ULL;
2282         char *cachefile = NULL;
2283         importargs_t idata = { 0 };
2284         char *endptr;
2285 
2286 
2287         struct option long_options[] = {
2288                 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
2289                 {0, 0, 0, 0}
2290         };
2291 
2292         /* check options */
2293         while ((c = getopt_long(argc, argv, ":aCc:d:DEfFmnNo:rR:tT:VX",
2294             long_options, NULL)) != -1) {
2295                 switch (c) {
2296                 case 'a':
2297                         do_all = B_TRUE;
2298                         break;
2299                 case 'c':
2300                         cachefile = optarg;
2301                         break;
2302                 case 'd':
2303                         if (searchdirs == NULL) {
2304                                 searchdirs = safe_malloc(sizeof (char *));
2305                         } else {
2306                                 char **tmp = safe_malloc((nsearch + 1) *
2307                                     sizeof (char *));
2308                                 bcopy(searchdirs, tmp, nsearch *
2309                                     sizeof (char *));
2310                                 free(searchdirs);
2311                                 searchdirs = tmp;
2312                         }
2313                         searchdirs[nsearch++] = optarg;


2328                         dryrun = B_TRUE;
2329                         break;
2330                 case 'N':
2331                         flags |= ZFS_IMPORT_ONLY;
2332                         break;
2333                 case 'o':
2334                         if ((propval = strchr(optarg, '=')) != NULL) {
2335                                 *propval = '\0';
2336                                 propval++;
2337                                 if (add_prop_list(optarg, propval,
2338                                     &props, B_TRUE))
2339                                         goto error;
2340                         } else {
2341                                 mntopts = optarg;
2342                         }
2343                         break;
2344                 case 'R':
2345                         if (add_prop_list(zpool_prop_to_name(
2346                             ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2347                                 goto error;
2348                         if (add_prop_list_default(zpool_prop_to_name(
2349                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2350                                 goto error;
2351                         break;
2352                 case 't':
2353                         flags |= ZFS_IMPORT_TEMP_NAME;
2354                         if (add_prop_list_default(zpool_prop_to_name(
2355                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2356                                 goto error;
2357                         break;
2358                 case 'T':
2359                         errno = 0;
2360                         txg = strtoull(optarg, &endptr, 0);
2361                         if (errno != 0 || *endptr != '\0') {
2362                                 (void) fprintf(stderr,
2363                                     gettext("invalid txg value\n"));
2364                                 usage(B_FALSE);
2365                         }
2366                         rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2367                         break;
2368                 case 'V':
2369                         flags |= ZFS_IMPORT_VERBATIM;
2370                         break;
2371                 case 'X':
2372                         xtreme_rewind = B_TRUE;
2373                         break;
2374                 case CHECKPOINT_OPT:


2473                  * User specified a name or guid.  Ensure it's unique.
2474                  */
2475                 idata.unique = B_TRUE;
2476         }
2477 
2478 
2479         idata.path = searchdirs;
2480         idata.paths = nsearch;
2481         idata.poolname = searchname;
2482         idata.guid = searchguid;
2483         idata.cachefile = cachefile;
2484         idata.policy = policy;
2485 
2486         pools = zpool_search_import(g_zfs, &idata);
2487 
2488         if (pools != NULL && idata.exists &&
2489             (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2490                 (void) fprintf(stderr, gettext("cannot import '%s': "
2491                     "a pool with that name already exists\n"),
2492                     argv[0]);
2493                 (void) fprintf(stderr, gettext("use the form 'zpool import "
2494                     "[-t] <pool | id> <newpool>' to give it a new temporary "
2495                     "or permanent name\n"));
2496                 err = 1;
2497         } else if (pools == NULL && idata.exists) {
2498                 (void) fprintf(stderr, gettext("cannot import '%s': "
2499                     "a pool with that name is already created/imported,\n"),
2500                     argv[0]);
2501                 (void) fprintf(stderr, gettext("and no additional pools "
2502                     "with that name were found\n"));
2503                 err = 1;
2504         } else if (pools == NULL) {
2505                 if (argc != 0) {
2506                         (void) fprintf(stderr, gettext("cannot import '%s': "
2507                             "no such pool available\n"), argv[0]);
2508                 }
2509                 err = 1;
2510         }
2511 
2512         if (err == 1) {
2513                 free(searchdirs);
2514                 nvlist_free(policy);
2515                 return (1);