1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or https://opensource.org/licenses/CDDL-1.0.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zfs_destroy/zfs_destroy.cfg
34
35#
36# DESCRIPTION:
37#	Verify 'zfs destroy -f' succeeds as root.
38#
39# STRATEGY:
40#	1. Create filesystem in the storage pool
41#	2. Set mountpoint for the filesystem and make it busy
42#	3. Verify that 'zfs destroy' fails to destroy the filesystem
43#	4. Verify 'zfs destroy -f' succeeds to destroy the filesystem.
44#
45
46verify_runnable "both"
47
48function cleanup
49{
50	cd $olddir
51
52	datasetexists $clone && destroy_dataset $clone -f
53	snapexists $snap && destroy_dataset $snap -f
54
55	for fs in $fs1 $fs2; do
56		datasetexists $fs && destroy_dataset $fs -f
57	done
58
59	for dir in $TESTDIR1 $TESTDIR2; do
60		[[ -d $dir ]] && \
61			log_must rm -rf $dir
62	done
63}
64
65log_assert "Verify that 'zfs destroy -f' succeeds as root. "
66
67log_onexit cleanup
68
69#
70# Preparations for testing
71#
72olddir=$PWD
73
74for dir in $TESTDIR1 $TESTDIR2; do
75	[[ ! -d $dir ]] && \
76		log_must mkdir -p $dir
77done
78
79fs1=$TESTPOOL/$TESTFS1
80mntp1=$TESTDIR1
81fs2=$TESTPOOL/$TESTFS2
82snap=$TESTPOOL/$TESTFS2@snap
83clone=$TESTPOOL/$TESTCLONE
84mntp2=$TESTDIR2
85
86#
87# Create filesystem and clone in the storage pool,  mount them and
88# make the mountpoint busy
89#
90for fs in $fs1 $fs2; do
91	log_must zfs create $fs
92done
93
94log_must zfs snapshot $snap
95log_must zfs clone $snap $clone
96
97log_must zfs set mountpoint=$mntp1 $fs1
98log_must zfs set mountpoint=$mntp2 $clone
99
100for arg in "$fs1 $mntp1" "$clone $mntp2"; do
101	read -r fs mntp <<<"$arg"
102
103	log_note "Verify that 'zfs destroy' fails to" \
104			"destroy filesystem when it is busy."
105	cd $mntp
106	log_mustnot zfs destroy $fs
107
108	if is_linux; then
109		log_mustnot zfs destroy -f $fs
110		datasetnonexists $fs && \
111		    log_fail "'zfs destroy -f' destroyed busy filesystem."
112	else
113		log_must zfs destroy -f $fs
114		datasetexists $fs && \
115		    log_fail "'zfs destroy -f' fail to destroy busy filesystem."
116	fi
117
118	cd $olddir
119done
120
121log_pass "'zfs destroy -f' succeeds as root."
122