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 http://www.opensolaris.org/os/licensing.
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 2008 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/inuse/inuse.cfg
34
35#
36# DESCRIPTION:
37# ZFS will not interfere with devices that are in use by ufsdump or
38# ufsrestore.
39#
40# STRATEGY:
41# 1. newfs a disk
42# 2. mount the disk
43# 3. create files and dirs on disk
44# 4. umount the disk
45# 5. ufsdump this disk to a backup disk
46# 6. Try to create a ZFS pool with same disk (also as a spare device)
47# 7. ufsrestore the disk from backup
48# 8. try to create a zpool during the ufsrestore
49#
50
51verify_runnable "global"
52
53function cleanup
54{
55	poolexists $TESTPOOL1 && destroy_pool $TESTPOOL1
56
57	poolexists $TESTPOOL2 && destroy_pool $TESTPOOL2
58
59	log_note "Kill off ufsdump process if still running"
60	kill -0 $PIDUFSDUMP > /dev/null 2>&1 && \
61	    log_must kill -9 $PIDUFSDUMP  > /dev/null 2>&1
62	#
63	# Note: It would appear that ufsdump spawns a number of processes
64	# which are not killed when the $PIDUFSDUMP is whacked.  So best bet
65	# is to find the rest of the them and deal with them individually.
66	#
67	for all in `pgrep ufsdump`
68	do
69		kill -9 $all > /dev/null 2>&1
70	done
71
72	log_note "Kill off ufsrestore process if still running"
73	kill -0 $PIDUFSRESTORE > /dev/null 2>&1 && \
74	    log_must kill -9 $PIDUFSRESTORE  > /dev/null 2>&1
75
76	ismounted $UFSMP ufs && log_must umount $UFSMP
77
78	rm -rf $UFSMP
79	rm -rf $TESTDIR
80
81	#
82	# Tidy up the disks we used.
83	#
84	log_must cleanup_devices $vdisks $sdisks
85}
86
87log_assert "Ensure ZFS does not interfere with devices that are in use by " \
88    "ufsdump or ufsrestore"
89
90log_onexit cleanup
91
92typeset bigdir="${UFSMP}/bigdirectory"
93typeset restored_files="${UFSMP}/restored_files"
94typeset -i dirnum=0
95typeset -i filenum=0
96typeset cwd=""
97
98log_note "Make a ufs filesystem on source $rawdisk1"
99new_fs $rawdisk1 > /dev/null 2>&1
100(($? != 0)) && log_untested "Unable to create ufs filesystem on $rawdisk1"
101
102log_must mkdir -p $UFSMP
103
104log_note "mount source $disk1 on $UFSMP"
105log_must mount $disk1 $UFSMP
106
107log_note "Now create some directories and files to be ufsdump'ed"
108while (($dirnum <= 2)); do
109	log_must mkdir $bigdir${dirnum}
110	while (( $filenum <= 2 )); do
111		file_write -o create -f $bigdir${dirnum}/file${filenum} \
112		    -b $BLOCK_SIZE -c $BLOCK_COUNT
113		if [[ $? -ne 0 ]]; then
114			if [[ $dirnum -lt 3 ]]; then
115				log_fail "file_write only wrote" \
116				    "<(( $dirnum * 3 + $filenum ))>" \
117				    "files, this is not enough"
118			fi
119		fi
120		((filenum = filenum + 1))
121	done
122	filenum=0
123	((dirnum = dirnum + 1))
124done
125
126log_must umount $UFSMP
127
128log_note "Start ufsdump in the background"
129log_note "ufsdump 0bf 512 $rawdisk0 $disk1"
130ufsdump 0bf 512 $rawdisk0 $disk1 &
131PIDUFSDUMP=$!
132
133unset NOINUSE_CHECK
134log_note "Attempt to zpool the source device in use by ufsdump"
135log_mustnot zpool create $TESTPOOL1 "$disk1"
136log_mustnot poolexists $TESTPOOL1
137
138log_note "Attempt to take the source device in use by ufsdump as spare device"
139log_mustnot zpool create $TESTPOOL1 "$FS_DISK2" spare "$disk1"
140log_mustnot poolexists $TESTPOOL1
141
142wait $PIDUFSDUMP
143typeset -i retval=$?
144(($retval != 0)) && log_fail "ufsdump failed with error code $ret_val"
145
146log_must mount $disk1 $UFSMP
147
148log_must rm -rf $UFSMP/*
149log_must mkdir $restored_files
150
151cwd=$PWD
152log_must cd $restored_files
153log_note "Start ufsrestore in the background from the target device"
154log_note "ufsrestore rbf 512 $rawdisk0"
155ufsrestore rbf 512 $rawdisk0 &
156PIDUFSRESTORE=$!
157log_must cd $cwd
158
159log_note "Attempt to zpool the restored device in use by ufsrestore"
160log_mustnot zpool create -f $TESTPOOL2 "$disk1"
161log_mustnot poolexists $TESTPOOL2
162
163log_note "Attempt to take the restored device in use by ufsrestore as spare" \
164    "device"
165log_mustnot zpool create -f $TESTPOOL2 "$FS_DISK2" spare "$disk1"
166log_mustnot poolexists $TESTPOOL2
167
168log_pass "Unable to zpool over a device in use by ufsdump or ufsrestore"
169