Skip to content

Commit

Permalink
Add zfs create dryrun
Browse files Browse the repository at this point in the history
Adds the ability to sanity check zfs create arguments and to see the
value of any additional properties that will local to the dataset.  For
example, automation that may need to adjust quota on a parent filesystem
before creating a volume may call `zfs create -nP -V <size> <volume>` to
obtain the value of refreservation.  This adds the following options to
zfs create:

- -n dry-run (no-op)
- -v verbose
- -P parseable (implies verbose)

Reviewed-by: Ryan Moeller <ryan@ixsystems.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Matt Ahrens <matt@delphix.com>
Reviewed-by: Jerry Jelinek <jerry.jelinek@joyent.com>
Signed-off-by: Mike Gerdts <mike.gerdts@joyent.com>
Closes openzfs#8974
  • Loading branch information
Mike Gerdts authored and TulsiJain committed Jul 20, 2019
1 parent 1861af4 commit 3666fb1
Show file tree
Hide file tree
Showing 6 changed files with 523 additions and 29 deletions.
114 changes: 92 additions & 22 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ get_usage(zfs_help_t idx)
return (gettext("\tclone [-p] [-o property=value] ... "
"<snapshot> <filesystem|volume>\n"));
case HELP_CREATE:
return (gettext("\tcreate [-p] [-o property=value] ... "
return (gettext("\tcreate [-Pnpv] [-o property=value] ... "
"<filesystem>\n"
"\tcreate [-ps] [-b blocksize] [-o property=value] ... "
"\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
"-V <size> <volume>\n"));
case HELP_DESTROY:
return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
Expand Down Expand Up @@ -867,8 +867,8 @@ zfs_do_clone(int argc, char **argv)
}

/*
* zfs create [-p] [-o prop=value] ... fs
* zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
* zfs create [-Pnpv] [-o prop=value] ... fs
* zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
*
* Create a new dataset. This command can be used to create filesystems
* and volumes. Snapshot creation is handled by 'zfs snapshot'.
Expand All @@ -880,16 +880,29 @@ zfs_do_clone(int argc, char **argv)
* SPA_VERSION_REFRESERVATION, we set a refreservation instead.
*
* The '-p' flag creates all the non-existing ancestors of the target first.
*
* The '-n' flag is no-op (dry run) mode. This will perform a user-space sanity
* check of arguments and properties, but does not check for permissions,
* available space, etc.
*
* The '-v' flag is for verbose output.
*
* The '-P' flag is used for parseable output. It implies '-v'.
*/
static int
zfs_do_create(int argc, char **argv)
{
zfs_type_t type = ZFS_TYPE_FILESYSTEM;
zpool_handle_t *zpool_handle = NULL;
nvlist_t *real_props = NULL;
uint64_t volsize = 0;
int c;
boolean_t noreserve = B_FALSE;
boolean_t bflag = B_FALSE;
boolean_t parents = B_FALSE;
boolean_t dryrun = B_FALSE;
boolean_t verbose = B_FALSE;
boolean_t parseable = B_FALSE;
int ret = 1;
nvlist_t *props;
uint64_t intval;
Expand All @@ -898,7 +911,7 @@ zfs_do_create(int argc, char **argv)
nomem();

/* check options */
while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
while ((c = getopt(argc, argv, ":PV:b:nso:pv")) != -1) {
switch (c) {
case 'V':
type = ZFS_TYPE_VOLUME;
Expand All @@ -914,6 +927,10 @@ zfs_do_create(int argc, char **argv)
nomem();
volsize = intval;
break;
case 'P':
verbose = B_TRUE;
parseable = B_TRUE;
break;
case 'p':
parents = B_TRUE;
break;
Expand All @@ -931,13 +948,19 @@ zfs_do_create(int argc, char **argv)
intval) != 0)
nomem();
break;
case 'n':
dryrun = B_TRUE;
break;
case 'o':
if (!parseprop(props, optarg))
goto error;
break;
case 's':
noreserve = B_TRUE;
break;
case 'v':
verbose = B_TRUE;
break;
case ':':
(void) fprintf(stderr, gettext("missing size "
"argument\n"));
Expand Down Expand Up @@ -969,14 +992,9 @@ zfs_do_create(int argc, char **argv)
goto badusage;
}

if (type == ZFS_TYPE_VOLUME && !noreserve) {
zpool_handle_t *zpool_handle;
nvlist_t *real_props = NULL;
uint64_t spa_version;
if (dryrun || (type == ZFS_TYPE_VOLUME && !noreserve)) {
char msg[ZFS_MAX_DATASET_NAME_LEN * 2];
char *p;
zfs_prop_t resv_prop;
char *strval;
char msg[1024];

if ((p = strchr(argv[0], '/')) != NULL)
*p = '\0';
Expand All @@ -985,25 +1003,31 @@ zfs_do_create(int argc, char **argv)
*p = '/';
if (zpool_handle == NULL)
goto error;
spa_version = zpool_get_prop_int(zpool_handle,
ZPOOL_PROP_VERSION, NULL);
if (spa_version >= SPA_VERSION_REFRESERVATION)
resv_prop = ZFS_PROP_REFRESERVATION;
else
resv_prop = ZFS_PROP_RESERVATION;

(void) snprintf(msg, sizeof (msg),
dryrun ? gettext("cannot verify '%s'") :
gettext("cannot create '%s'"), argv[0]);
if (props && (real_props = zfs_valid_proplist(g_zfs, type,
props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
zpool_close(zpool_handle);
goto error;
}
}

if (type == ZFS_TYPE_VOLUME && !noreserve) {
uint64_t spa_version;
zfs_prop_t resv_prop;
char *strval;

spa_version = zpool_get_prop_int(zpool_handle,
ZPOOL_PROP_VERSION, NULL);
if (spa_version >= SPA_VERSION_REFRESERVATION)
resv_prop = ZFS_PROP_REFRESERVATION;
else
resv_prop = ZFS_PROP_RESERVATION;

volsize = zvol_volsize_to_reservation(zpool_handle, volsize,
real_props);
nvlist_free(real_props);
zpool_close(zpool_handle);

if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
&strval) != 0) {
Expand All @@ -1014,6 +1038,10 @@ zfs_do_create(int argc, char **argv)
}
}
}
if (zpool_handle != NULL) {
zpool_close(zpool_handle);
nvlist_free(real_props);
}

if (parents && zfs_name_valid(argv[0], type)) {
/*
Expand All @@ -1025,8 +1053,50 @@ zfs_do_create(int argc, char **argv)
ret = 0;
goto error;
}
if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
goto error;
if (verbose) {
(void) printf(parseable ? "create_ancestors\t%s\n" :
dryrun ? "would create ancestors of %s\n" :
"create ancestors of %s\n", argv[0]);
}
if (!dryrun) {
if (zfs_create_ancestors(g_zfs, argv[0]) != 0) {
goto error;
}
}
}

if (verbose) {
nvpair_t *nvp = NULL;
(void) printf(parseable ? "create\t%s\n" :
dryrun ? "would create %s\n" : "create %s\n", argv[0]);
while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
uint64_t uval;
char *sval;

switch (nvpair_type(nvp)) {
case DATA_TYPE_UINT64:
VERIFY0(nvpair_value_uint64(nvp, &uval));
(void) printf(parseable ?
"property\t%s\t%llu\n" : "\t%s=%llu\n",
nvpair_name(nvp), (u_longlong_t)uval);
break;
case DATA_TYPE_STRING:
VERIFY0(nvpair_value_string(nvp, &sval));
(void) printf(parseable ?
"property\t%s\t%s\n" : "\t%s=%s\n",
nvpair_name(nvp), sval);
break;
default:
(void) fprintf(stderr, "property '%s' "
"has illegal type %d\n",
nvpair_name(nvp), nvpair_type(nvp));
abort();
}
}
}
if (dryrun) {
ret = 0;
goto error;
}

/* pass to libzfs */
Expand Down
98 changes: 93 additions & 5 deletions man/man8/zfs.8
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
.\" Copyright (c) 2014 Integros [integros.com]
.\" Copyright 2019 Richard Laager. All rights reserved.
.\" Copyright 2018 Nexenta Systems, Inc.
.\" Copyright 2018 Joyent, Inc.
.\" Copyright 2019 Joyent, Inc.
.\"
.Dd April 30, 2019
.Dd June 30, 2019
.Dt ZFS 8 SMM
.Os Linux
.Sh NAME
Expand All @@ -41,12 +41,12 @@
.Fl ?V
.Nm
.Cm create
.Op Fl p
.Op Fl Pnpv
.Oo Fl o Ar property Ns = Ns Ar value Oc Ns ...
.Ar filesystem
.Nm
.Cm create
.Op Fl ps
.Op Fl Pnpsv
.Op Fl b Ar blocksize
.Oo Fl o Ar property Ns = Ns Ar value Oc Ns ...
.Fl V Ar size Ar volume
Expand Down Expand Up @@ -2556,7 +2556,7 @@ subcommand.
.It Xo
.Nm
.Cm create
.Op Fl p
.Op Fl Pnpv
.Oo Fl o Ar property Ns = Ns Ar value Oc Ns ...
.Ar filesystem
.Xc
Expand Down Expand Up @@ -2585,6 +2585,48 @@ Any property specified on the command line using the
.Fl o
option is ignored.
If the target filesystem already exists, the operation completes successfully.
.It Fl n
Do a dry-run
.Pq Qq No-op
creation.
No datasets will be created.
This is useful in conjunction with the
.Fl v
or
.Fl P
flags to validate properties that are passed via
.Fl o
options and those implied by other options.
The actual dataset creation can still fail due to insufficient privileges or
available capacity.
.It Fl P
Print machine-parsable verbose information about the created dataset.
Each line of output contains a key and one or two values, all separated by tabs.
The
.Sy create_ancestors
and
.Sy create
keys have
.Em filesystem
as their only value.
The
.Sy create_ancestors
key only appears if the
.Fl p
option is used.
The
.Sy property
key has two values, a property name that property's value.
The
.Sy property
key may appear zero or more times, once for each property that will be set local
to
.Em filesystem
due to the use of the
.Fl o
option.
.It Fl v
Print verbose information about the created dataset.
.El
.It Xo
.Nm
Expand Down Expand Up @@ -2641,6 +2683,52 @@ See
in the
.Sx Native Properties
section for more information about sparse volumes.
.It Fl n
Do a dry-run
.Pq Qq No-op
creation.
No datasets will be created.
This is useful in conjunction with the
.Fl v
or
.Fl P
flags to validate properties that are passed via
.Fl o
options and those implied by other options.
The actual dataset creation can still fail due to insufficient privileges or
available capacity.
.It Fl P
Print machine-parsable verbose information about the created dataset.
Each line of output contains a key and one or two values, all separated by tabs.
The
.Sy create_ancestors
and
.Sy create
keys have
.Em volume
as their only value.
The
.Sy create_ancestors
key only appears if the
.Fl p
option is used.
The
.Sy property
key has two values, a property name that property's value.
The
.Sy property
key may appear zero or more times, once for each property that will be set local
to
.Em volume
due to the use of the
.Fl b
or
.Fl o
options, as well as
.Sy refreservation
if the volume is not sparse.
.It Fl v
Print verbose information about the created dataset.
.El
.It Xo
.Nm
Expand Down
2 changes: 1 addition & 1 deletion tests/runfiles/linux.run
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ tests = ['zfs_create_001_pos', 'zfs_create_002_pos', 'zfs_create_003_pos',
'zfs_create_007_pos', 'zfs_create_008_neg', 'zfs_create_009_neg',
'zfs_create_010_neg', 'zfs_create_011_pos', 'zfs_create_012_pos',
'zfs_create_013_pos', 'zfs_create_014_pos', 'zfs_create_encrypted',
'zfs_create_crypt_combos']
'zfs_create_crypt_combos', 'zfs_create_dryrun', 'zfs_create_verbose']
tags = ['functional', 'cli_root', 'zfs_create']

[tests/functional/cli_root/zfs_destroy]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ dist_pkgdata_SCRIPTS = \
zfs_create_013_pos.ksh \
zfs_create_014_pos.ksh \
zfs_create_encrypted.ksh \
zfs_create_crypt_combos.ksh
zfs_create_crypt_combos.ksh \
zfs_create_dryrun.ksh \
zfs_create_verbose.ksh

dist_pkgdata_DATA = \
properties.kshlib \
Expand Down
Loading

0 comments on commit 3666fb1

Please sign in to comment.