]> git.proxmox.com Git - mirror_zfs.git/commitdiff
Introduce a destroy_dataset helper
authorGiuseppe Di Natale <dinatale2@users.noreply.github.com>
Tue, 6 Mar 2018 22:54:57 +0000 (14:54 -0800)
committerTony Hutter <hutter2@llnl.gov>
Tue, 8 May 2018 00:19:56 +0000 (17:19 -0700)
Datasets can be busy when calling zfs destroy. Introduce
a helper function to destroy datasets and use it to destroy
datasets in zfs_allow_004_pos, zfs_promote_008_pos, and
zfs_destroy_002_pos.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Giuseppe Di Natale <dinatale2@llnl.gov>
Closes #7224
Closes #7246
Closes #7249
Closes #7267

tests/zfs-tests/include/libtest.shlib
tests/zfs-tests/tests/functional/cli_root/zfs_destroy/zfs_destroy_002_pos.ksh
tests/zfs-tests/tests/functional/cli_root/zfs_promote/zfs_promote_008_pos.ksh
tests/zfs-tests/tests/functional/delegate/delegate_common.kshlib

index d51d73e619f94736fe82e52687660da5f9eb0221..c1862c983cfa7ca47c8ff071339b13cc402cc0ee 100644 (file)
@@ -503,8 +503,7 @@ function default_cleanup_noexit
                typeset fs=""
                for fs in $(zfs list -H -o name \
                    | grep "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
-                       datasetexists $fs && \
-                               log_must zfs destroy -Rf $fs
+                       destroy_dataset "$fs" "-Rf"
                done
 
                # Need cleanup here to avoid garbage dir left.
@@ -565,11 +564,8 @@ function default_container_cleanup
        [[ $? -eq 0 ]] && \
            log_must zfs unmount $TESTPOOL/$TESTCTR/$TESTFS1
 
-       datasetexists $TESTPOOL/$TESTCTR/$TESTFS1 && \
-           log_must zfs destroy -R $TESTPOOL/$TESTCTR/$TESTFS1
-
-       datasetexists $TESTPOOL/$TESTCTR && \
-           log_must zfs destroy -Rf $TESTPOOL/$TESTCTR
+       destroy_dataset "$TESTPOOL/$TESTCTR/$TESTFS1" "-R"
+       destroy_dataset "$TESTPOOL/$TESTCTR" "-Rf"
 
        [[ -e $TESTDIR1 ]] && \
            log_must rm -rf $TESTDIR1 > /dev/null 2>&1
@@ -603,7 +599,7 @@ function destroy_snapshot
                        log_fail "get_prop mountpoint $snap failed."
        fi
 
-       log_must zfs destroy $snap
+       destroy_dataset "$snap"
        [[ $mtpt != "" && -d $mtpt ]] && \
                log_must rm -rf $mtpt
 }
@@ -629,7 +625,7 @@ function destroy_clone
                        log_fail "get_prop mountpoint $clone failed."
        fi
 
-       log_must zfs destroy $clone
+       destroy_dataset "$clone"
        [[ $mtpt != "" && -d $mtpt ]] && \
                log_must rm -rf $mtpt
 }
@@ -648,7 +644,7 @@ function destroy_bookmark
                log_fail "'$bkmarkp' does not existed."
        fi
 
-       log_must zfs destroy $bkmark
+       destroy_dataset "$bkmark"
 }
 
 # Return 0 if a snapshot exists; $? otherwise
@@ -1555,6 +1551,40 @@ function destroy_pool #pool
        return 0
 }
 
+# Return 0 if destroy successfully or the dataset exists; $? otherwise
+# Note: In local zones, this function should return 0 silently.
+#
+# $1 - dataset name
+# $2 - custom arguments for zfs destroy
+# Destroy dataset with the given parameters.
+
+function destroy_dataset #dataset #args
+{
+       typeset dataset=$1
+       typeset mtpt
+       typeset args=${2:-""}
+
+       if [[ -z $dataset ]]; then
+               log_note "No dataset name given."
+               return 1
+       fi
+
+       if is_global_zone ; then
+               if datasetexists "$dataset" ; then
+                       mtpt=$(get_prop mountpoint "$dataset")
+                       log_must_busy zfs destroy $args $dataset
+
+                       [[ -d $mtpt ]] && \
+                               log_must rm -rf $mtpt
+               else
+                       log_note "Dataset does not exist. ($dataset)"
+                       return 1
+               fi
+       fi
+
+       return 0
+}
+
 #
 # Firstly, create a pool with 5 datasets. Then, create a single zone and
 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
@@ -2541,9 +2571,7 @@ function verify_opt_p_ops
        esac
 
        # make sure the upper level filesystem does not exist
-       if datasetexists ${newdataset%/*} ; then
-               log_must zfs destroy -rRf ${newdataset%/*}
-       fi
+       destroy_dataset "${newdataset%/*}" "-rRf"
 
        # without -p option, operation will fail
        log_mustnot zfs $ops $dataset $newdataset
index 594e3ad952835e58d213173eb5079a1464b2d0ab..128921226a733b63a3c1d07950577843de72324f 100755 (executable)
@@ -49,8 +49,7 @@ function cleanup
 {
        typeset -i i=0
        while (( $i < ${#data_objs[*]} )); do
-               datasetexists "${data_objs[i]}" && \
-                       zfs destroy -rf ${data_objs[i]}
+               destroy_dataset "${data_objs[i]}" "-rf"
                ((i = i + 1))
        done
 }
@@ -87,7 +86,7 @@ done
 
 i=0
 while (( $i < ${#data_objs[*]} )); do
-       log_must zfs destroy ${data_objs[i]}
+       destroy_dataset "${data_objs[i]}"
        datasetexists ${data_objs[i]} && \
                log_fail "'zfs destroy <filesystem>|<volume>|<snapshot>' fail."
        ((i = i + 1))
index 05d482c405387a003036cb883c77ab2167d17919..c37c9a6269e7df3a04f3ff302a5f03bd5b973fc7 100755 (executable)
@@ -49,7 +49,7 @@ function cleanup
                log_must zfs promote $vol
        fi
 
-       log_must zfs destroy -rR $snap
+       destroy_dataset "$snap" "-rR"
 }
 
 log_assert "'zfs promote' can promote a volume clone."
index 8dd601850884146fc5b250aaed41a03ea091190f..ebe2f4ba2494c9f53b68f0f358153445bdf9708f 100644 (file)
@@ -53,15 +53,11 @@ function cleanup_user_group
 #
 function restore_root_datasets
 {
-       if datasetexists $ROOT_TESTFS ; then
-               log_must zfs destroy -Rf $ROOT_TESTFS
-       fi
+       destroy_dataset "$ROOT_TESTFS" "-Rf"
        log_must zfs create $ROOT_TESTFS
 
        if is_global_zone ; then
-               if datasetexists $ROOT_TESTVOL ; then
-                       log_must zfs destroy -Rf $ROOT_TESTVOL
-               fi
+               destroy_dataset "$ROOT_TESTVOL" "-Rf"
                log_must zfs create -V $VOLSIZE $ROOT_TESTVOL
                block_device_wait
        fi