]> git.proxmox.com Git - mirror_zfs-debian.git/blob - tests/zfs-tests/tests/functional/mmp/mmp.kshlib
4c46ae7a2add991720b5d2841d8d24cf38b05c7f
[mirror_zfs-debian.git] / tests / zfs-tests / tests / functional / mmp / mmp.kshlib
1 #
2 # CDDL HEADER START
3 #
4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
7 #
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
12 #
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
18 #
19 # CDDL HEADER END
20 #
21
22 #
23 # Copyright (c) 2017 by Lawrence Livermore National Security, LLC.
24 # Use is subject to license terms.
25 #
26
27 . $STF_SUITE/include/libtest.shlib
28 . $STF_SUITE/tests/functional/mmp/mmp.cfg
29
30
31 function check_pool_import # pool opts token keyword
32 {
33 typeset pool=${1:-$MMP_POOL}
34 typeset opts=$2
35 typeset token=$3
36 typeset keyword=$4
37
38 zpool import $opts 2>&1 | \
39 nawk -v token="$token:" '($1==token) {print $0}' | \
40 grep -i "$keyword" > /dev/null 2>&1
41
42 return $?
43 }
44
45 function is_pool_imported # pool opts
46 {
47 typeset pool=${1:-$MMP_POOL}
48 typeset opts=$2
49
50 check_pool_import "$pool" "$opts" "status" \
51 "The pool is currently imported"
52 return $?
53 }
54
55 function wait_pool_imported # pool opts
56 {
57 typeset pool=${1:-$MMP_POOL}
58 typeset opts=$2
59
60 while is_pool_imported "$pool" "$opts"; do
61 log_must sleep 5
62 done
63
64 return 0
65 }
66
67 function try_pool_import # pool opts message
68 {
69 typeset pool=${1:-$MMP_POOL}
70 typeset opts=$2
71 typeset msg=$3
72
73 zpool import $opts $pool 2>&1 | grep -i "$msg"
74
75 return $?
76 }
77
78 function mmp_set_hostid
79 {
80 typeset hostid=$1
81
82 zgenhostid $1
83
84 if [ $(hostid) != "$hostid" ]; then
85 return 1
86 fi
87
88 return 0
89 }
90
91 function mmp_clear_hostid
92 {
93 rm -f $HOSTID_FILE
94 }
95
96 function mmp_pool_create # pool dir
97 {
98 typeset pool=${1:-$MMP_POOL}
99 typeset dir=${2:-$MMP_DIR}
100 typeset opts="-VVVVV -T120 -M -k0 -f $dir -E -p $pool"
101
102 log_must mkdir -p $dir
103 log_must rm -f $dir/*
104 log_must truncate -s $MINVDEVSIZE $dir/vdev1 $dir/vdev2
105
106 log_must mmp_clear_hostid
107 log_must mmp_set_hostid $HOSTID1
108 log_must zpool create -f -o cachefile=$MMP_CACHE $pool \
109 mirror $dir/vdev1 $dir/vdev2
110 log_must zpool set multihost=on $pool
111 log_must mv $MMP_CACHE ${MMP_CACHE}.stale
112 log_must zpool export $pool
113 log_must mmp_clear_hostid
114 log_must mmp_set_hostid $HOSTID2
115
116 log_note "Starting ztest in the background as hostid $HOSTID1"
117 log_must eval "ZFS_HOSTID=$HOSTID1 ztest $opts >$MMP_ZTEST_LOG 2>&1 &"
118
119 while ! is_pool_imported "$pool" "-d $dir"; do
120 log_must pgrep ztest
121 log_must sleep 5
122 done
123 }
124
125 function mmp_pool_destroy # pool dir
126 {
127 typeset pool=${1:-$MMP_POOL}
128 typeset dir=${2:-$MMP_DIR}
129
130 ZTESTPID=$(pgrep ztest)
131 if [ -n "$ZTESTPID" ]; then
132 log_must kill $ZTESTPID
133 wait $ZTESTPID
134 fi
135
136 if poolexists $pool; then
137 destroy_pool $pool
138 fi
139
140 log_must rm -f $dir/*
141 mmp_clear_hostid
142 }
143
144 function mmp_pool_set_hostid # pool hostid
145 {
146 typeset pool=$1
147 typeset hostid=$2
148
149 log_must mmp_clear_hostid
150 log_must mmp_set_hostid $hostid
151 log_must zpool export $pool
152 log_must zpool import $pool
153
154 return 0
155 }
156
157 function import_no_activity_check # pool opts
158 {
159 typeset pool=$1
160 typeset opts=$2
161
162 SECONDS=0
163 zpool import $opts $pool
164 typeset rc=$?
165
166 if [[ $SECONDS -gt $ZPOOL_IMPORT_DURATION ]]; then
167 log_fail "unexpected activity check (${SECONDS}s)"
168 fi
169
170 return $rc
171 }
172
173 function import_activity_check # pool opts
174 {
175 typeset pool=$1
176 typeset opts=$2
177
178 SECONDS=0
179 zpool import $opts $pool
180 typeset rc=$?
181
182 if [[ $SECONDS -le $ZPOOL_IMPORT_DURATION ]]; then
183 log_fail "expected activity check (${SECONDS}s)"
184 fi
185
186 return $rc
187 }
188
189 function clear_mmp_history
190 {
191 log_must set_tunable64 zfs_multihost_history $MMP_HISTORY_OFF
192 log_must set_tunable64 zfs_multihost_history $MMP_HISTORY
193 }
194
195 function count_uberblocks # pool duration
196 {
197 typeset pool=$1
198 typeset -i duration=$2
199 typeset hist_path="/proc/spl/kstat/zfs/$pool/multihost"
200
201 log_must sleep $duration
202 echo $(cat "$hist_path" | sed '1,2d' | wc -l)
203 }