From 4b6e2a5a33957af806b708723f32b83dbafce326 Mon Sep 17 00:00:00 2001 From: Ryan Moeller Date: Fri, 4 Dec 2020 17:01:42 -0500 Subject: [PATCH] Add -u option to 'zfs create' Add -u option to 'zfs create' that prevents file system from being automatically mounted. This is similar to the 'zfs receive -u'. Authored by: pjd FreeBSD-commit: freebsd/freebsd@35c58230e292775a694d189ff2b0bea2dcf6947d Reviewed-by: Brian Behlendorf Reviewed-by: Allan Jude Ported-by: Ryan Moeller Signed-off-by: Ryan Moeller Closes #11254 --- cmd/zfs/zfs_main.c | 20 +++++++- man/man8/zfs-create.8 | 12 +++-- tests/runfiles/common.run | 3 +- .../cli_root/zfs_create/Makefile.am | 1 + .../zfs_create/zfs_create_nomount.ksh | 51 +++++++++++++++++++ 5 files changed, 80 insertions(+), 7 deletions(-) create mode 100755 tests/zfs-tests/tests/functional/cli_root/zfs_create/zfs_create_nomount.ksh diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c index 340a7db964c9..8064f6363933 100644 --- a/cmd/zfs/zfs_main.c +++ b/cmd/zfs/zfs_main.c @@ -270,7 +270,7 @@ get_usage(zfs_help_t idx) return (gettext("\tclone [-p] [-o property=value] ... " " \n")); case HELP_CREATE: - return (gettext("\tcreate [-Pnpv] [-o property=value] ... " + return (gettext("\tcreate [-Pnpuv] [-o property=value] ... " "\n" "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... " "-V \n")); @@ -1012,6 +1012,8 @@ default_volblocksize(zpool_handle_t *zhp, nvlist_t *props) * check of arguments and properties, but does not check for permissions, * available space, etc. * + * The '-u' flag prevents the newly created file system from being mounted. + * * The '-v' flag is for verbose output. * * The '-P' flag is used for parseable output. It implies '-v'. @@ -1028,6 +1030,7 @@ zfs_do_create(int argc, char **argv) boolean_t bflag = B_FALSE; boolean_t parents = B_FALSE; boolean_t dryrun = B_FALSE; + boolean_t nomount = B_FALSE; boolean_t verbose = B_FALSE; boolean_t parseable = B_FALSE; int ret = 1; @@ -1039,7 +1042,7 @@ zfs_do_create(int argc, char **argv) nomem(); /* check options */ - while ((c = getopt(argc, argv, ":PV:b:nso:pv")) != -1) { + while ((c = getopt(argc, argv, ":PV:b:nso:puv")) != -1) { switch (c) { case 'V': type = ZFS_TYPE_VOLUME; @@ -1086,6 +1089,9 @@ zfs_do_create(int argc, char **argv) case 's': noreserve = B_TRUE; break; + case 'u': + nomount = B_TRUE; + break; case 'v': verbose = B_TRUE; break; @@ -1105,6 +1111,11 @@ zfs_do_create(int argc, char **argv) "used when creating a volume\n")); goto badusage; } + if (nomount && type != ZFS_TYPE_FILESYSTEM) { + (void) fprintf(stderr, gettext("'-u' can only be " + "used when creating a filesystem\n")); + goto badusage; + } argc -= optind; argv += optind; @@ -1265,6 +1276,11 @@ zfs_do_create(int argc, char **argv) log_history = B_FALSE; } + if (nomount) { + ret = 0; + goto error; + } + ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET); error: nvlist_free(props); diff --git a/man/man8/zfs-create.8 b/man/man8/zfs-create.8 index 9c5c959b34a2..5a4f9a32afa5 100644 --- a/man/man8/zfs-create.8 +++ b/man/man8/zfs-create.8 @@ -30,7 +30,7 @@ .\" Copyright 2018 Nexenta Systems, Inc. .\" Copyright 2019 Joyent, Inc. .\" -.Dd June 30, 2019 +.Dd December 1, 2020 .Dt ZFS-CREATE 8 .Os .Sh NAME @@ -39,7 +39,7 @@ .Sh SYNOPSIS .Nm zfs .Cm create -.Op Fl Pnpv +.Op Fl Pnpuv .Oo Fl o Ar property Ns = Ns Ar value Oc Ns ... .Ar filesystem .Nm zfs @@ -53,14 +53,16 @@ .It Xo .Nm zfs .Cm create -.Op Fl Pnpv +.Op Fl Pnpuv .Oo Fl o Ar property Ns = Ns Ar value Oc Ns ... .Ar filesystem .Xc Creates a new ZFS file system. The file system is automatically mounted according to the .Sy mountpoint -property inherited from the parent. +property inherited from the parent, unless the +.Fl u +option is used. .Bl -tag -width "-o" .It Fl o Ar property Ns = Ns Ar value Sets the specified property as if the command @@ -122,6 +124,8 @@ to due to the use of the .Fl o option. +.It Fl u +Do not mount the newly created file system. .It Fl v Print verbose information about the created dataset. .El diff --git a/tests/runfiles/common.run b/tests/runfiles/common.run index 7ca4194e130b..d6077e2922ce 100644 --- a/tests/runfiles/common.run +++ b/tests/runfiles/common.run @@ -156,7 +156,8 @@ 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_dryrun', 'zfs_create_verbose'] + 'zfs_create_crypt_combos', 'zfs_create_dryrun', 'zfs_create_nomount', + 'zfs_create_verbose'] tags = ['functional', 'cli_root', 'zfs_create'] [tests/functional/cli_root/zfs_destroy] diff --git a/tests/zfs-tests/tests/functional/cli_root/zfs_create/Makefile.am b/tests/zfs-tests/tests/functional/cli_root/zfs_create/Makefile.am index cb65507ae711..7515753c1bc2 100644 --- a/tests/zfs-tests/tests/functional/cli_root/zfs_create/Makefile.am +++ b/tests/zfs-tests/tests/functional/cli_root/zfs_create/Makefile.am @@ -19,6 +19,7 @@ dist_pkgdata_SCRIPTS = \ zfs_create_encrypted.ksh \ zfs_create_crypt_combos.ksh \ zfs_create_dryrun.ksh \ + zfs_create_nomount.ksh \ zfs_create_verbose.ksh dist_pkgdata_DATA = \ diff --git a/tests/zfs-tests/tests/functional/cli_root/zfs_create/zfs_create_nomount.ksh b/tests/zfs-tests/tests/functional/cli_root/zfs_create/zfs_create_nomount.ksh new file mode 100755 index 000000000000..e1fbbe63ad31 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zfs_create/zfs_create_nomount.ksh @@ -0,0 +1,51 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2020 iXsystems, Inc. +# + +. $STF_SUITE/include/libtest.shlib + +# +# DESCRIPTION: +# zfs create -u should leave the new file system unmounted. +# It should not work for a volume. +# +# STRATEGY: +# 1. Create a file system using -u and make sure the file system is not mounted. +# 3. Do it for a volume to verify it fails. +# + +verify_runnable "both" + +function cleanup +{ + local ds + + for ds in "$fs" "$vol"; do + datasetexists "$ds" && destroy_dataset "$ds" + done +} +log_onexit cleanup + +log_assert "zfs create -u leaves the new file system unmounted" + +typeset fs="$TESTPOOL/$TESTFS1" +typeset vol="$TESTPOOL/$TESTVOL1" + +log_must create_dataset "$fs" "-u" +log_mustnot ismounted "$fs" + +log_mustnot zfs create -V $VOLSIZE -u "$vol" + +log_pass "zfs create -u leaves the new file system unmounted"