f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#! /bin/ksh -p
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# CDDL HEADER START
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# The contents of this file are subject to the terms of the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Common Development and Distribution License (the "License").
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# You may not use this file except in compliance with the License.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# or http://www.opensolaris.org/os/licensing.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# See the License for the specific language governing permissions
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# and limitations under the License.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# CDDL HEADER END
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Use is subject to license terms.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy. $STF_SUITE/include/libtest.shlib
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy. $STF_SUITE/tests/functional/snapshot/snapshot.cfg
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# DESCRIPTION:
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Create a snapshot from regular filesystem, volume,
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# or filesystem upon volume, Build a clone file system
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# from the snapshot and verify new files can be written.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# STRATEGY:
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# 1. Create snapshot use 3 combination:
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# - Regular filesystem
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# - Regular volume
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# - Filesystem upon volume
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# 2. Clone a new file system from the snapshot
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# 3. Verify the cloned file system is writable
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyverify_runnable "both"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Setup array, 4 elements as a group, refer to:
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# i+0: name of a snapshot
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# i+1: mountpoint of the snapshot
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# i+2: clone created from the snapshot
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# i+3: mountpoint of the clone
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyset -A args "$SNAPFS" "$SNAPDIR" "$TESTPOOL/$TESTCLONE" "$TESTDIR.0" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy "$SNAPFS1" "$SNAPDIR3" "$TESTPOOL/$TESTCLONE1" "" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy "$SNAPFS2" "$SNAPDIR2" "$TESTPOOL1/$TESTCLONE2" "$TESTDIR.2"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction setup_all
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy create_pool $TESTPOOL1 /dev/zvol/dsk/$TESTPOOL/$TESTVOL
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must zfs create $TESTPOOL1/$TESTFS
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL1/$TESTFS
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction cleanup_all
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset -i i=0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy i=0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy while (( i < ${#args[*]} )); do
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy snapexists ${args[i]} && \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must zfs destroy -Rf ${args[i]}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -d ${args[i+3]} ]] && \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must rm -rf ${args[i+3]}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -d ${args[i+1]} ]] && \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must rm -rf ${args[i+1]}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy (( i = i + 4 ))
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy done
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy datasetexists $TESTPOOL1/$TESTFS && \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must zfs destroy -f $TESTPOOL1/$TESTFS
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy destroy_pool $TESTPOOL1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -d $TESTDIR2 ]] && \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must rm -rf $TESTDIR2
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedylog_assert "Verify a cloned file system is writable."
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedylog_onexit cleanup_all
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedysetup_all
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy[[ -n $TESTDIR ]] && \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must rm -rf $TESTDIR/* > /dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedytypeset -i COUNT=10
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedytypeset -i i=0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfor mtpt in $TESTDIR $TESTDIR2 ; do
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Populate the $mtpt directory (prior to snapshot)"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset -i j=1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy while [[ $j -le $COUNT ]]; do
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must file_write -o create -f $mtpt/before_file$j \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy -b $BLOCKSZ -c $NUM_WRITES -d $j
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy (( j = j + 1 ))
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy done
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedydone
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedywhile (( i < ${#args[*]} )); do
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # Take a snapshot of the test file system.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must zfs snapshot ${args[i]}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # Clone a new file system from the snapshot
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must zfs clone ${args[i]} ${args[i+2]}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -n ${args[i+3]} ]] ; then
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must zfs set mountpoint=${args[i+3]} ${args[i+2]}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy FILE_COUNT=`ls -Al ${args[i+3]} | grep -v "total" \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy | grep -v "\.zfs" | wc -l`
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $FILE_COUNT -ne $COUNT ]]; then
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy ls -Al ${args[i+3]}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_fail "AFTER: ${args[i+3]} contains $FILE_COUNT files(s)."
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Verify the ${args[i+3]} directory is writable"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy j=1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy while [[ $j -le $COUNT ]]; do
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must file_write -o create -f ${args[i+3]}/after_file$j \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy -b $BLOCKSZ -c $NUM_WRITES -d $j
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy (( j = j + 1 ))
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy done
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy FILE_COUNT=`ls -Al ${args[i+3]}/after* | grep -v "total" | wc -l`
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $FILE_COUNT -ne $COUNT ]]; then
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy ls -Al ${args[i+3]}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_fail "${args[i+3]} contains $FILE_COUNT after* files(s)."
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy (( i = i + 4 ))
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedydone
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedylog_pass "The clone file system is writable."