]>
Commit | Line | Data |
---|---|---|
6bb24f4d BB |
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 | |
1d3ba0bf | 9 | # or https://opensource.org/licenses/CDDL-1.0. |
6bb24f4d BB |
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 | # | |
618b6adf | 23 | # Copyright (c) 2009, Sun Microsystems Inc. All rights reserved. |
c15d36c6 | 24 | # Copyright (c) 2012, 2020, Delphix. All rights reserved. |
618b6adf KS |
25 | # Copyright (c) 2017, Tim Chase. All rights reserved. |
26 | # Copyright (c) 2017, Nexenta Systems Inc. All rights reserved. | |
27 | # Copyright (c) 2017, Lawrence Livermore National Security LLC. | |
28 | # Copyright (c) 2017, Datto Inc. All rights reserved. | |
29 | # Copyright (c) 2017, Open-E Inc. All rights reserved. | |
c3cb57ae | 30 | # Copyright (c) 2021, The FreeBSD Foundation. |
1b939560 | 31 | # Use is subject to license terms. |
6bb24f4d BB |
32 | # |
33 | ||
5c9f744b AZ |
34 | . ${STF_SUITE}/include/tunables.cfg |
35 | ||
6bb24f4d | 36 | . ${STF_TOOLS}/include/logapi.shlib |
a7004725 | 37 | . ${STF_SUITE}/include/math.shlib |
d3f2cd7e | 38 | . ${STF_SUITE}/include/blkdev.shlib |
6bb24f4d | 39 | |
c1d9abf9 JWK |
40 | # |
41 | # Apply constrained path when available. This is required since the | |
42 | # PATH may have been modified by sudo's secure_path behavior. | |
43 | # | |
44 | if [ -n "$STF_PATH" ]; then | |
e0b53a5d | 45 | export PATH="$STF_PATH" |
c1d9abf9 JWK |
46 | fi |
47 | ||
a584ef26 BB |
48 | # |
49 | # Generic dot version comparison function | |
50 | # | |
51 | # Returns success when version $1 is greater than or equal to $2. | |
52 | # | |
53 | function compare_version_gte | |
54 | { | |
5b0e75ca | 55 | [ "$(printf "$1\n$2" | sort -V | tail -n1)" = "$1" ] |
a584ef26 BB |
56 | } |
57 | ||
0c656a96 GDN |
58 | # Linux kernel version comparison function |
59 | # | |
60 | # $1 Linux version ("4.10", "2.6.32") or blank for installed Linux version | |
61 | # | |
62 | # Used for comparison: if [ $(linux_version) -ge $(linux_version "2.6.32") ] | |
63 | # | |
64 | function linux_version | |
65 | { | |
66 | typeset ver="$1" | |
67 | ||
23914a3b | 68 | [ -z "$ver" ] && ver=$(uname -r | grep -Eo "^[0-9]+\.[0-9]+\.[0-9]+") |
0c656a96 | 69 | |
23914a3b AZ |
70 | typeset version major minor _ |
71 | IFS='.' read -r version major minor _ <<<"$ver" | |
0c656a96 | 72 | |
23914a3b AZ |
73 | [ -z "$version" ] && version=0 |
74 | [ -z "$major" ] && major=0 | |
75 | [ -z "$minor" ] && minor=0 | |
0c656a96 | 76 | |
23914a3b | 77 | echo $((version * 100000 + major * 1000 + minor)) |
0c656a96 GDN |
78 | } |
79 | ||
6bb24f4d BB |
80 | # Determine if this is a Linux test system |
81 | # | |
82 | # Return 0 if platform Linux, 1 if otherwise | |
83 | ||
84 | function is_linux | |
85 | { | |
5c9f744b | 86 | [ "$UNAME" = "Linux" ] |
6bb24f4d BB |
87 | } |
88 | ||
d7164b27 RM |
89 | # Determine if this is an illumos test system |
90 | # | |
91 | # Return 0 if platform illumos, 1 if otherwise | |
92 | function is_illumos | |
93 | { | |
5c9f744b | 94 | [ "$UNAME" = "illumos" ] |
d7164b27 RM |
95 | } |
96 | ||
7839c4b5 MM |
97 | # Determine if this is a FreeBSD test system |
98 | # | |
99 | # Return 0 if platform FreeBSD, 1 if otherwise | |
100 | ||
101 | function is_freebsd | |
102 | { | |
5c9f744b | 103 | [ "$UNAME" = "FreeBSD" ] |
7839c4b5 MM |
104 | } |
105 | ||
e676a196 BB |
106 | # Determine if this is a 32-bit system |
107 | # | |
108 | # Return 0 if platform is 32-bit, 1 if otherwise | |
109 | ||
110 | function is_32bit | |
111 | { | |
bf228f3d | 112 | [ $(getconf LONG_BIT) = "32" ] |
e676a196 BB |
113 | } |
114 | ||
c6ced726 BB |
115 | # Determine if kmemleak is enabled |
116 | # | |
117 | # Return 0 if kmemleak is enabled, 1 if otherwise | |
118 | ||
119 | function is_kmemleak | |
120 | { | |
bf228f3d | 121 | is_linux && [ -e /sys/kernel/debug/kmemleak ] |
c6ced726 BB |
122 | } |
123 | ||
6bb24f4d BB |
124 | # Determine whether a dataset is mounted |
125 | # | |
126 | # $1 dataset name | |
127 | # $2 filesystem type; optional - defaulted to zfs | |
128 | # | |
129 | # Return 0 if dataset is mounted; 1 if unmounted; 2 on error | |
130 | ||
131 | function ismounted | |
132 | { | |
133 | typeset fstype=$2 | |
134 | [[ -z $fstype ]] && fstype=zfs | |
23914a3b | 135 | typeset out dir name |
6bb24f4d BB |
136 | |
137 | case $fstype in | |
138 | zfs) | |
139 | if [[ "$1" == "/"* ]] ; then | |
75746e9a | 140 | ! zfs mount | awk -v fs="$1" '$2 == fs {exit 1}' |
6bb24f4d | 141 | else |
75746e9a | 142 | ! zfs mount | awk -v ds="$1" '$1 == ds {exit 1}' |
6bb24f4d BB |
143 | fi |
144 | ;; | |
145 | ufs|nfs) | |
7839c4b5 MM |
146 | if is_freebsd; then |
147 | mount -pt $fstype | while read dev dir _t _flags; do | |
148 | [[ "$1" == "$dev" || "$1" == "$dir" ]] && return 0 | |
149 | done | |
150 | else | |
23914a3b | 151 | out=$(df -F $fstype $1 2>/dev/null) || return |
6bb24f4d | 152 | |
7839c4b5 MM |
153 | dir=${out%%\(*} |
154 | dir=${dir%% *} | |
155 | name=${out##*\(} | |
156 | name=${name%%\)*} | |
157 | name=${name%% *} | |
6bb24f4d | 158 | |
7839c4b5 MM |
159 | [[ "$1" == "$dir" || "$1" == "$name" ]] && return 0 |
160 | fi | |
6bb24f4d | 161 | ;; |
5c214ae3 | 162 | ext*) |
75746e9a | 163 | df -t $fstype $1 > /dev/null 2>&1 |
6bb24f4d BB |
164 | ;; |
165 | zvol) | |
166 | if [[ -L "$ZVOL_DEVDIR/$1" ]]; then | |
167 | link=$(readlink -f $ZVOL_DEVDIR/$1) | |
168 | [[ -n "$link" ]] && \ | |
c1d9abf9 | 169 | mount | grep -q "^$link" && \ |
6bb24f4d BB |
170 | return 0 |
171 | fi | |
172 | ;; | |
23914a3b AZ |
173 | *) |
174 | false | |
175 | ;; | |
6bb24f4d | 176 | esac |
6bb24f4d BB |
177 | } |
178 | ||
179 | # Return 0 if a dataset is mounted; 1 otherwise | |
180 | # | |
181 | # $1 dataset name | |
182 | # $2 filesystem type; optional - defaulted to zfs | |
183 | ||
184 | function mounted | |
185 | { | |
186 | ismounted $1 $2 | |
6bb24f4d BB |
187 | } |
188 | ||
189 | # Return 0 if a dataset is unmounted; 1 otherwise | |
190 | # | |
191 | # $1 dataset name | |
192 | # $2 filesystem type; optional - defaulted to zfs | |
193 | ||
194 | function unmounted | |
195 | { | |
23914a3b | 196 | ! ismounted $1 $2 |
6bb24f4d BB |
197 | } |
198 | ||
6bb24f4d BB |
199 | function default_setup |
200 | { | |
201 | default_setup_noexit "$@" | |
202 | ||
203 | log_pass | |
204 | } | |
205 | ||
9c53e516 TK |
206 | function default_setup_no_mountpoint |
207 | { | |
208 | default_setup_noexit "$1" "$2" "$3" "yes" | |
209 | ||
210 | log_pass | |
211 | } | |
212 | ||
6bb24f4d BB |
213 | # |
214 | # Given a list of disks, setup storage pools and datasets. | |
215 | # | |
216 | function default_setup_noexit | |
217 | { | |
218 | typeset disklist=$1 | |
219 | typeset container=$2 | |
220 | typeset volume=$3 | |
9c53e516 | 221 | typeset no_mountpoint=$4 |
3c67d83a | 222 | log_note begin default_setup_noexit |
6bb24f4d BB |
223 | |
224 | if is_global_zone; then | |
225 | if poolexists $TESTPOOL ; then | |
226 | destroy_pool $TESTPOOL | |
227 | fi | |
c1d9abf9 JWK |
228 | [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL |
229 | log_must zpool create -f $TESTPOOL $disklist | |
6bb24f4d BB |
230 | else |
231 | reexport_pool | |
232 | fi | |
233 | ||
c1d9abf9 JWK |
234 | rm -rf $TESTDIR || log_unresolved Could not remove $TESTDIR |
235 | mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR | |
6bb24f4d | 236 | |
c1d9abf9 | 237 | log_must zfs create $TESTPOOL/$TESTFS |
9c53e516 TK |
238 | if [[ -z $no_mountpoint ]]; then |
239 | log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS | |
240 | fi | |
6bb24f4d BB |
241 | |
242 | if [[ -n $container ]]; then | |
c1d9abf9 | 243 | rm -rf $TESTDIR1 || \ |
6bb24f4d | 244 | log_unresolved Could not remove $TESTDIR1 |
c1d9abf9 | 245 | mkdir -p $TESTDIR1 || \ |
6bb24f4d BB |
246 | log_unresolved Could not create $TESTDIR1 |
247 | ||
c1d9abf9 JWK |
248 | log_must zfs create $TESTPOOL/$TESTCTR |
249 | log_must zfs set canmount=off $TESTPOOL/$TESTCTR | |
250 | log_must zfs create $TESTPOOL/$TESTCTR/$TESTFS1 | |
9c53e516 TK |
251 | if [[ -z $no_mountpoint ]]; then |
252 | log_must zfs set mountpoint=$TESTDIR1 \ | |
253 | $TESTPOOL/$TESTCTR/$TESTFS1 | |
254 | fi | |
6bb24f4d BB |
255 | fi |
256 | ||
257 | if [[ -n $volume ]]; then | |
258 | if is_global_zone ; then | |
c1d9abf9 | 259 | log_must zfs create -V $VOLSIZE $TESTPOOL/$TESTVOL |
6bb24f4d BB |
260 | block_device_wait |
261 | else | |
c1d9abf9 | 262 | log_must zfs create $TESTPOOL/$TESTVOL |
6bb24f4d BB |
263 | fi |
264 | fi | |
265 | } | |
266 | ||
267 | # | |
268 | # Given a list of disks, setup a storage pool, file system and | |
269 | # a container. | |
270 | # | |
271 | function default_container_setup | |
272 | { | |
273 | typeset disklist=$1 | |
274 | ||
275 | default_setup "$disklist" "true" | |
276 | } | |
277 | ||
278 | # | |
279 | # Given a list of disks, setup a storage pool,file system | |
280 | # and a volume. | |
281 | # | |
282 | function default_volume_setup | |
283 | { | |
284 | typeset disklist=$1 | |
285 | ||
286 | default_setup "$disklist" "" "true" | |
287 | } | |
288 | ||
289 | # | |
290 | # Given a list of disks, setup a storage pool,file system, | |
291 | # a container and a volume. | |
292 | # | |
293 | function default_container_volume_setup | |
294 | { | |
295 | typeset disklist=$1 | |
296 | ||
297 | default_setup "$disklist" "true" "true" | |
298 | } | |
299 | ||
300 | # | |
301 | # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on | |
302 | # filesystem | |
303 | # | |
d99a0153 | 304 | # $1 Existing filesystem or volume name. Default, $TESTPOOL/$TESTFS |
6bb24f4d BB |
305 | # $2 snapshot name. Default, $TESTSNAP |
306 | # | |
307 | function create_snapshot | |
308 | { | |
d99a0153 | 309 | typeset fs_vol=${1:-$TESTPOOL/$TESTFS} |
6bb24f4d BB |
310 | typeset snap=${2:-$TESTSNAP} |
311 | ||
312 | [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined." | |
313 | [[ -z $snap ]] && log_fail "Snapshot's name is undefined." | |
314 | ||
315 | if snapexists $fs_vol@$snap; then | |
316 | log_fail "$fs_vol@$snap already exists." | |
317 | fi | |
318 | datasetexists $fs_vol || \ | |
319 | log_fail "$fs_vol must exist." | |
320 | ||
c1d9abf9 | 321 | log_must zfs snapshot $fs_vol@$snap |
6bb24f4d BB |
322 | } |
323 | ||
324 | # | |
325 | # Create a clone from a snapshot, default clone name is $TESTCLONE. | |
326 | # | |
327 | # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default. | |
328 | # $2 Clone name, $TESTPOOL/$TESTCLONE is default. | |
329 | # | |
330 | function create_clone # snapshot clone | |
331 | { | |
332 | typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP} | |
333 | typeset clone=${2:-$TESTPOOL/$TESTCLONE} | |
334 | ||
335 | [[ -z $snap ]] && \ | |
336 | log_fail "Snapshot name is undefined." | |
337 | [[ -z $clone ]] && \ | |
338 | log_fail "Clone name is undefined." | |
339 | ||
c1d9abf9 | 340 | log_must zfs clone $snap $clone |
6bb24f4d BB |
341 | } |
342 | ||
aeacdefe GM |
343 | # |
344 | # Create a bookmark of the given snapshot. Defaultly create a bookmark on | |
345 | # filesystem. | |
346 | # | |
347 | # $1 Existing filesystem or volume name. Default, $TESTFS | |
348 | # $2 Existing snapshot name. Default, $TESTSNAP | |
349 | # $3 bookmark name. Default, $TESTBKMARK | |
350 | # | |
351 | function create_bookmark | |
352 | { | |
353 | typeset fs_vol=${1:-$TESTFS} | |
354 | typeset snap=${2:-$TESTSNAP} | |
355 | typeset bkmark=${3:-$TESTBKMARK} | |
356 | ||
357 | [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined." | |
358 | [[ -z $snap ]] && log_fail "Snapshot's name is undefined." | |
359 | [[ -z $bkmark ]] && log_fail "Bookmark's name is undefined." | |
360 | ||
361 | if bkmarkexists $fs_vol#$bkmark; then | |
362 | log_fail "$fs_vol#$bkmark already exists." | |
363 | fi | |
364 | datasetexists $fs_vol || \ | |
365 | log_fail "$fs_vol must exist." | |
366 | snapexists $fs_vol@$snap || \ | |
367 | log_fail "$fs_vol@$snap must exist." | |
368 | ||
c1d9abf9 | 369 | log_must zfs bookmark $fs_vol@$snap $fs_vol#$bkmark |
aeacdefe GM |
370 | } |
371 | ||
650258d7 | 372 | # |
373 | # Create a temporary clone result of an interrupted resumable 'zfs receive' | |
374 | # $1 Destination filesystem name. Must not exist, will be created as the result | |
375 | # of this function along with its %recv temporary clone | |
376 | # $2 Source filesystem name. Must not exist, will be created and destroyed | |
377 | # | |
378 | function create_recv_clone | |
379 | { | |
380 | typeset recvfs="$1" | |
381 | typeset sendfs="${2:-$TESTPOOL/create_recv_clone}" | |
382 | typeset snap="$sendfs@snap1" | |
383 | typeset incr="$sendfs@snap2" | |
384 | typeset mountpoint="$TESTDIR/create_recv_clone" | |
385 | typeset sendfile="$TESTDIR/create_recv_clone.zsnap" | |
386 | ||
387 | [[ -z $recvfs ]] && log_fail "Recv filesystem's name is undefined." | |
388 | ||
389 | datasetexists $recvfs && log_fail "Recv filesystem must not exist." | |
390 | datasetexists $sendfs && log_fail "Send filesystem must not exist." | |
391 | ||
56fa4aa9 | 392 | log_must zfs create -o compression=off -o mountpoint="$mountpoint" $sendfs |
650258d7 | 393 | log_must zfs snapshot $snap |
394 | log_must eval "zfs send $snap | zfs recv -u $recvfs" | |
395 | log_must mkfile 1m "$mountpoint/data" | |
396 | log_must zfs snapshot $incr | |
30af21b0 PD |
397 | log_must eval "zfs send -i $snap $incr | dd bs=10K count=1 \ |
398 | iflag=fullblock > $sendfile" | |
650258d7 | 399 | log_mustnot eval "zfs recv -su $recvfs < $sendfile" |
c7b55e71 | 400 | destroy_dataset "$sendfs" "-r" |
650258d7 | 401 | log_must rm -f "$sendfile" |
402 | ||
403 | if [[ $(get_prop 'inconsistent' "$recvfs/%recv") -ne 1 ]]; then | |
404 | log_fail "Error creating temporary $recvfs/%recv clone" | |
405 | fi | |
406 | } | |
407 | ||
6bb24f4d BB |
408 | function default_mirror_setup |
409 | { | |
410 | default_mirror_setup_noexit $1 $2 $3 | |
411 | ||
412 | log_pass | |
413 | } | |
414 | ||
415 | # | |
416 | # Given a pair of disks, set up a storage pool and dataset for the mirror | |
417 | # @parameters: $1 the primary side of the mirror | |
418 | # $2 the secondary side of the mirror | |
419 | # @uses: ZPOOL ZFS TESTPOOL TESTFS | |
420 | function default_mirror_setup_noexit | |
421 | { | |
422 | readonly func="default_mirror_setup_noexit" | |
423 | typeset primary=$1 | |
424 | typeset secondary=$2 | |
425 | ||
426 | [[ -z $primary ]] && \ | |
427 | log_fail "$func: No parameters passed" | |
428 | [[ -z $secondary ]] && \ | |
429 | log_fail "$func: No secondary partition passed" | |
c1d9abf9 JWK |
430 | [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL |
431 | log_must zpool create -f $TESTPOOL mirror $@ | |
432 | log_must zfs create $TESTPOOL/$TESTFS | |
433 | log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS | |
6bb24f4d BB |
434 | } |
435 | ||
6bb24f4d BB |
436 | # |
437 | # Destroy the configured testpool mirrors. | |
438 | # the mirrors are of the form ${TESTPOOL}{number} | |
439 | # @uses: ZPOOL ZFS TESTPOOL | |
440 | function destroy_mirrors | |
441 | { | |
442 | default_cleanup_noexit | |
443 | ||
444 | log_pass | |
445 | } | |
446 | ||
1b609d4b BA |
447 | function default_raidz_setup |
448 | { | |
449 | default_raidz_setup_noexit "$*" | |
450 | ||
451 | log_pass | |
452 | } | |
453 | ||
6bb24f4d BB |
454 | # |
455 | # Given a minimum of two disks, set up a storage pool and dataset for the raid-z | |
456 | # $1 the list of disks | |
457 | # | |
1b609d4b | 458 | function default_raidz_setup_noexit |
6bb24f4d BB |
459 | { |
460 | typeset disklist="$*" | |
461 | disks=(${disklist[*]}) | |
462 | ||
463 | if [[ ${#disks[*]} -lt 2 ]]; then | |
464 | log_fail "A raid-z requires a minimum of two disks." | |
465 | fi | |
466 | ||
c1d9abf9 | 467 | [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL |
3fd3e56c | 468 | log_must zpool create -f $TESTPOOL raidz $disklist |
c1d9abf9 JWK |
469 | log_must zfs create $TESTPOOL/$TESTFS |
470 | log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS | |
6bb24f4d BB |
471 | } |
472 | ||
473 | # | |
474 | # Common function used to cleanup storage pools and datasets. | |
475 | # | |
476 | # Invoked at the start of the test suite to ensure the system | |
477 | # is in a known state, and also at the end of each set of | |
478 | # sub-tests to ensure errors from one set of tests doesn't | |
479 | # impact the execution of the next set. | |
480 | ||
481 | function default_cleanup | |
482 | { | |
483 | default_cleanup_noexit | |
484 | ||
485 | log_pass | |
486 | } | |
487 | ||
3fd3e56c | 488 | # |
489 | # Utility function used to list all available pool names. | |
490 | # | |
491 | # NOTE: $KEEP is a variable containing pool names, separated by a newline | |
492 | # character, that must be excluded from the returned list. | |
493 | # | |
494 | function get_all_pools | |
495 | { | |
496 | zpool list -H -o name | grep -Fvx "$KEEP" | grep -v "$NO_POOLS" | |
497 | } | |
498 | ||
6bb24f4d BB |
499 | function default_cleanup_noexit |
500 | { | |
6bb24f4d BB |
501 | typeset pool="" |
502 | # | |
503 | # Destroying the pool will also destroy any | |
504 | # filesystems it contains. | |
505 | # | |
506 | if is_global_zone; then | |
c1d9abf9 | 507 | zfs unmount -a > /dev/null 2>&1 |
3fd3e56c | 508 | ALL_POOLS=$(get_all_pools) |
6bb24f4d BB |
509 | # Here, we loop through the pools we're allowed to |
510 | # destroy, only destroying them if it's safe to do | |
511 | # so. | |
512 | while [ ! -z ${ALL_POOLS} ] | |
513 | do | |
514 | for pool in ${ALL_POOLS} | |
515 | do | |
516 | if safe_to_destroy_pool $pool ; | |
517 | then | |
518 | destroy_pool $pool | |
519 | fi | |
6bb24f4d | 520 | done |
2b95e911 | 521 | ALL_POOLS=$(get_all_pools) |
6bb24f4d BB |
522 | done |
523 | ||
c1d9abf9 | 524 | zfs mount -a |
6bb24f4d BB |
525 | else |
526 | typeset fs="" | |
c1d9abf9 JWK |
527 | for fs in $(zfs list -H -o name \ |
528 | | grep "^$ZONE_POOL/$ZONE_CTR[01234]/"); do | |
c7b55e71 | 529 | destroy_dataset "$fs" "-Rf" |
6bb24f4d BB |
530 | done |
531 | ||
532 | # Need cleanup here to avoid garbage dir left. | |
c1d9abf9 | 533 | for fs in $(zfs list -H -o name); do |
6bb24f4d | 534 | [[ $fs == /$ZONE_POOL ]] && continue |
c1d9abf9 | 535 | [[ -d $fs ]] && log_must rm -rf $fs/* |
6bb24f4d BB |
536 | done |
537 | ||
538 | # | |
539 | # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to | |
540 | # the default value | |
541 | # | |
c1d9abf9 | 542 | for fs in $(zfs list -H -o name); do |
6bb24f4d | 543 | if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then |
c1d9abf9 JWK |
544 | log_must zfs set reservation=none $fs |
545 | log_must zfs set recordsize=128K $fs | |
546 | log_must zfs set mountpoint=/$fs $fs | |
75746e9a AZ |
547 | typeset enc=$(get_prop encryption $fs) |
548 | if [ -z "$enc" ] || [ "$enc" = "off" ]; then | |
c1d9abf9 | 549 | log_must zfs set checksum=on $fs |
6bb24f4d | 550 | fi |
c1d9abf9 JWK |
551 | log_must zfs set compression=off $fs |
552 | log_must zfs set atime=on $fs | |
553 | log_must zfs set devices=off $fs | |
554 | log_must zfs set exec=on $fs | |
555 | log_must zfs set setuid=on $fs | |
556 | log_must zfs set readonly=off $fs | |
557 | log_must zfs set snapdir=hidden $fs | |
558 | log_must zfs set aclmode=groupmask $fs | |
559 | log_must zfs set aclinherit=secure $fs | |
6bb24f4d BB |
560 | fi |
561 | done | |
562 | fi | |
563 | ||
564 | [[ -d $TESTDIR ]] && \ | |
c1d9abf9 | 565 | log_must rm -rf $TESTDIR |
7050a65d SV |
566 | |
567 | disk1=${DISKS%% *} | |
568 | if is_mpath_device $disk1; then | |
569 | delete_partitions | |
570 | fi | |
52775712 | 571 | |
572 | rm -f $TEST_BASE_DIR/{err,out} | |
6bb24f4d BB |
573 | } |
574 | ||
575 | ||
576 | # | |
577 | # Common function used to cleanup storage pools, file systems | |
578 | # and containers. | |
579 | # | |
580 | function default_container_cleanup | |
581 | { | |
582 | if ! is_global_zone; then | |
583 | reexport_pool | |
584 | fi | |
585 | ||
23914a3b | 586 | ismounted $TESTPOOL/$TESTCTR/$TESTFS1 && |
c1d9abf9 | 587 | log_must zfs unmount $TESTPOOL/$TESTCTR/$TESTFS1 |
6bb24f4d | 588 | |
c7b55e71 GDN |
589 | destroy_dataset "$TESTPOOL/$TESTCTR/$TESTFS1" "-R" |
590 | destroy_dataset "$TESTPOOL/$TESTCTR" "-Rf" | |
6bb24f4d BB |
591 | |
592 | [[ -e $TESTDIR1 ]] && \ | |
62c5ccdf | 593 | log_must rm -rf $TESTDIR1 |
6bb24f4d BB |
594 | |
595 | default_cleanup | |
596 | } | |
597 | ||
598 | # | |
599 | # Common function used to cleanup snapshot of file system or volume. Default to | |
600 | # delete the file system's snapshot | |
601 | # | |
602 | # $1 snapshot name | |
603 | # | |
604 | function destroy_snapshot | |
605 | { | |
606 | typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP} | |
607 | ||
608 | if ! snapexists $snap; then | |
838bd5ff | 609 | log_fail "'$snap' does not exist." |
6bb24f4d BB |
610 | fi |
611 | ||
612 | # | |
613 | # For the sake of the value which come from 'get_prop' is not equal | |
614 | # to the really mountpoint when the snapshot is unmounted. So, firstly | |
615 | # check and make sure this snapshot's been mounted in current system. | |
616 | # | |
617 | typeset mtpt="" | |
618 | if ismounted $snap; then | |
619 | mtpt=$(get_prop mountpoint $snap) | |
6bb24f4d BB |
620 | fi |
621 | ||
c7b55e71 | 622 | destroy_dataset "$snap" |
6bb24f4d | 623 | [[ $mtpt != "" && -d $mtpt ]] && \ |
c1d9abf9 | 624 | log_must rm -rf $mtpt |
6bb24f4d BB |
625 | } |
626 | ||
627 | # | |
628 | # Common function used to cleanup clone. | |
629 | # | |
630 | # $1 clone name | |
631 | # | |
632 | function destroy_clone | |
633 | { | |
634 | typeset clone=${1:-$TESTPOOL/$TESTCLONE} | |
635 | ||
636 | if ! datasetexists $clone; then | |
637 | log_fail "'$clone' does not existed." | |
638 | fi | |
639 | ||
640 | # With the same reason in destroy_snapshot | |
641 | typeset mtpt="" | |
642 | if ismounted $clone; then | |
643 | mtpt=$(get_prop mountpoint $clone) | |
6bb24f4d BB |
644 | fi |
645 | ||
c7b55e71 | 646 | destroy_dataset "$clone" |
6bb24f4d | 647 | [[ $mtpt != "" && -d $mtpt ]] && \ |
c1d9abf9 | 648 | log_must rm -rf $mtpt |
6bb24f4d BB |
649 | } |
650 | ||
aeacdefe GM |
651 | # |
652 | # Common function used to cleanup bookmark of file system or volume. Default | |
653 | # to delete the file system's bookmark. | |
654 | # | |
655 | # $1 bookmark name | |
656 | # | |
657 | function destroy_bookmark | |
658 | { | |
659 | typeset bkmark=${1:-$TESTPOOL/$TESTFS#$TESTBKMARK} | |
660 | ||
661 | if ! bkmarkexists $bkmark; then | |
662 | log_fail "'$bkmarkp' does not existed." | |
663 | fi | |
664 | ||
c7b55e71 | 665 | destroy_dataset "$bkmark" |
aeacdefe GM |
666 | } |
667 | ||
6bb24f4d BB |
668 | # Return 0 if a snapshot exists; $? otherwise |
669 | # | |
670 | # $1 - snapshot name | |
671 | ||
672 | function snapexists | |
673 | { | |
c1d9abf9 | 674 | zfs list -H -t snapshot "$1" > /dev/null 2>&1 |
6bb24f4d BB |
675 | } |
676 | ||
aeacdefe GM |
677 | # |
678 | # Return 0 if a bookmark exists; $? otherwise | |
679 | # | |
680 | # $1 - bookmark name | |
681 | # | |
682 | function bkmarkexists | |
683 | { | |
c1d9abf9 | 684 | zfs list -H -t bookmark "$1" > /dev/null 2>&1 |
aeacdefe GM |
685 | } |
686 | ||
3b9edd7b SD |
687 | # |
688 | # Return 0 if a hold exists; $? otherwise | |
689 | # | |
690 | # $1 - hold tag | |
691 | # $2 - snapshot name | |
692 | # | |
693 | function holdexists | |
694 | { | |
75746e9a | 695 | ! zfs holds "$2" | awk -v t="$1" '$2 ~ t { exit 1 }' |
3b9edd7b SD |
696 | } |
697 | ||
6bb24f4d BB |
698 | # |
699 | # Set a property to a certain value on a dataset. | |
700 | # Sets a property of the dataset to the value as passed in. | |
701 | # @param: | |
702 | # $1 dataset who's property is being set | |
703 | # $2 property to set | |
704 | # $3 value to set property to | |
705 | # @return: | |
706 | # 0 if the property could be set. | |
707 | # non-zero otherwise. | |
708 | # @use: ZFS | |
709 | # | |
710 | function dataset_setprop | |
711 | { | |
712 | typeset fn=dataset_setprop | |
713 | ||
714 | if (($# < 3)); then | |
715 | log_note "$fn: Insufficient parameters (need 3, had $#)" | |
716 | return 1 | |
717 | fi | |
718 | typeset output= | |
c1d9abf9 | 719 | output=$(zfs set $2=$3 $1 2>&1) |
6bb24f4d BB |
720 | typeset rv=$? |
721 | if ((rv != 0)); then | |
722 | log_note "Setting property on $1 failed." | |
723 | log_note "property $2=$3" | |
724 | log_note "Return Code: $rv" | |
725 | log_note "Output: $output" | |
726 | return $rv | |
727 | fi | |
728 | return 0 | |
729 | } | |
730 | ||
6bb24f4d BB |
731 | # |
732 | # Check a numeric assertion | |
733 | # @parameter: $@ the assertion to check | |
734 | # @output: big loud notice if assertion failed | |
735 | # @use: log_fail | |
736 | # | |
737 | function assert | |
738 | { | |
739 | (($@)) || log_fail "$@" | |
740 | } | |
741 | ||
742 | # | |
743 | # Function to format partition size of a disk | |
744 | # Given a disk cxtxdx reduces all partitions | |
745 | # to 0 size | |
746 | # | |
747 | function zero_partitions #<whole_disk_name> | |
748 | { | |
749 | typeset diskname=$1 | |
750 | typeset i | |
751 | ||
395a6b19 RM |
752 | if is_freebsd; then |
753 | gpart destroy -F $diskname | |
754 | elif is_linux; then | |
8e5d1484 PZ |
755 | DSK=$DEV_DSKDIR/$diskname |
756 | DSK=$(echo $DSK | sed -e "s|//|/|g") | |
757 | log_must parted $DSK -s -- mklabel gpt | |
758 | blockdev --rereadpt $DSK 2>/dev/null | |
759 | block_device_wait | |
6bb24f4d BB |
760 | else |
761 | for i in 0 1 3 4 5 6 7 | |
762 | do | |
cf8738d8 | 763 | log_must set_partition $i "" 0mb $diskname |
6bb24f4d BB |
764 | done |
765 | fi | |
95401cb6 BB |
766 | |
767 | return 0 | |
6bb24f4d BB |
768 | } |
769 | ||
770 | # | |
771 | # Given a slice, size and disk, this function | |
772 | # formats the slice to the specified size. | |
773 | # Size should be specified with units as per | |
774 | # the `format` command requirements eg. 100mb 3gb | |
775 | # | |
c6e457df | 776 | # NOTE: This entire interface is problematic for the Linux parted utility |
6bb24f4d BB |
777 | # which requires the end of the partition to be specified. It would be |
778 | # best to retire this interface and replace it with something more flexible. | |
779 | # At the moment a best effort is made. | |
780 | # | |
2ff615b2 RE |
781 | # arguments: <slice_num> <slice_start> <size_plus_units> <whole_disk_name> |
782 | function set_partition | |
6bb24f4d BB |
783 | { |
784 | typeset -i slicenum=$1 | |
785 | typeset start=$2 | |
786 | typeset size=$3 | |
a3bddd49 RM |
787 | typeset disk=${4#$DEV_DSKDIR/} |
788 | disk=${disk#$DEV_RDSKDIR/} | |
6bb24f4d | 789 | |
5c9f744b | 790 | case "$UNAME" in |
7839c4b5 | 791 | Linux) |
8e5d1484 PZ |
792 | if [[ -z $size || -z $disk ]]; then |
793 | log_fail "The size or disk name is unspecified." | |
794 | fi | |
a3bddd49 | 795 | disk=$DEV_DSKDIR/$disk |
6bb24f4d BB |
796 | typeset size_mb=${size%%[mMgG]} |
797 | ||
798 | size_mb=${size_mb%%[mMgG][bB]} | |
799 | if [[ ${size:1:1} == 'g' ]]; then | |
800 | ((size_mb = size_mb * 1024)) | |
801 | fi | |
802 | ||
803 | # Create GPT partition table when setting slice 0 or | |
804 | # when the device doesn't already contain a GPT label. | |
2ff615b2 | 805 | parted $disk -s -- print 1 >/dev/null |
6bb24f4d BB |
806 | typeset ret_val=$? |
807 | if [[ $slicenum -eq 0 || $ret_val -ne 0 ]]; then | |
23914a3b | 808 | if ! parted $disk -s -- mklabel gpt; then |
cf8738d8 | 809 | log_note "Failed to create GPT partition table on $disk" |
810 | return 1 | |
811 | fi | |
6bb24f4d BB |
812 | fi |
813 | ||
814 | # When no start is given align on the first cylinder. | |
815 | if [[ -z "$start" ]]; then | |
816 | start=1 | |
817 | fi | |
818 | ||
819 | # Determine the cylinder size for the device and using | |
820 | # that calculate the end offset in cylinders. | |
821 | typeset -i cly_size_kb=0 | |
75746e9a AZ |
822 | cly_size_kb=$(parted -m $disk -s -- unit cyl print | |
823 | awk -F '[:k.]' 'NR == 3 {print $4}') | |
6bb24f4d BB |
824 | ((end = (size_mb * 1024 / cly_size_kb) + start)) |
825 | ||
2ff615b2 | 826 | parted $disk -s -- \ |
6bb24f4d | 827 | mkpart part$slicenum ${start}cyl ${end}cyl |
2ff615b2 RE |
828 | typeset ret_val=$? |
829 | if [[ $ret_val -ne 0 ]]; then | |
cf8738d8 | 830 | log_note "Failed to create partition $slicenum on $disk" |
831 | return 1 | |
832 | fi | |
6bb24f4d | 833 | |
2ff615b2 RE |
834 | blockdev --rereadpt $disk 2>/dev/null |
835 | block_device_wait $disk | |
7839c4b5 MM |
836 | ;; |
837 | FreeBSD) | |
838 | if [[ -z $size || -z $disk ]]; then | |
839 | log_fail "The size or disk name is unspecified." | |
840 | fi | |
a3bddd49 | 841 | disk=$DEV_DSKDIR/$disk |
7839c4b5 MM |
842 | |
843 | if [[ $slicenum -eq 0 ]] || ! gpart show $disk >/dev/null 2>&1; then | |
844 | gpart destroy -F $disk >/dev/null 2>&1 | |
23914a3b | 845 | if ! gpart create -s GPT $disk; then |
7839c4b5 MM |
846 | log_note "Failed to create GPT partition table on $disk" |
847 | return 1 | |
848 | fi | |
849 | fi | |
850 | ||
851 | typeset index=$((slicenum + 1)) | |
852 | ||
853 | if [[ -n $start ]]; then | |
854 | start="-b $start" | |
855 | fi | |
856 | gpart add -t freebsd-zfs $start -s $size -i $index $disk | |
857 | if [[ $ret_val -ne 0 ]]; then | |
858 | log_note "Failed to create partition $slicenum on $disk" | |
859 | return 1 | |
860 | fi | |
861 | ||
862 | block_device_wait $disk | |
863 | ;; | |
864 | *) | |
8e5d1484 PZ |
865 | if [[ -z $slicenum || -z $size || -z $disk ]]; then |
866 | log_fail "The slice, size or disk name is unspecified." | |
867 | fi | |
868 | ||
6bb24f4d BB |
869 | typeset format_file=/var/tmp/format_in.$$ |
870 | ||
c1d9abf9 JWK |
871 | echo "partition" >$format_file |
872 | echo "$slicenum" >> $format_file | |
873 | echo "" >> $format_file | |
874 | echo "" >> $format_file | |
875 | echo "$start" >> $format_file | |
876 | echo "$size" >> $format_file | |
877 | echo "label" >> $format_file | |
878 | echo "" >> $format_file | |
879 | echo "q" >> $format_file | |
880 | echo "q" >> $format_file | |
6bb24f4d | 881 | |
c1d9abf9 | 882 | format -e -s -d $disk -f $format_file |
2ff615b2 RE |
883 | typeset ret_val=$? |
884 | rm -f $format_file | |
7839c4b5 MM |
885 | ;; |
886 | esac | |
c1d9abf9 | 887 | |
cf8738d8 | 888 | if [[ $ret_val -ne 0 ]]; then |
889 | log_note "Unable to format $disk slice $slicenum to $size" | |
890 | return 1 | |
891 | fi | |
6bb24f4d BB |
892 | return 0 |
893 | } | |
894 | ||
7050a65d SV |
895 | # |
896 | # Delete all partitions on all disks - this is specifically for the use of multipath | |
897 | # devices which currently can only be used in the test suite as raw/un-partitioned | |
898 | # devices (ie a zpool cannot be created on a whole mpath device that has partitions) | |
899 | # | |
900 | function delete_partitions | |
901 | { | |
815a6456 | 902 | typeset disk |
7050a65d | 903 | |
7050a65d SV |
904 | if [[ -z $DISKSARRAY ]]; then |
905 | DISKSARRAY=$DISKS | |
906 | fi | |
907 | ||
908 | if is_linux; then | |
815a6456 RM |
909 | typeset -i part |
910 | for disk in $DISKSARRAY; do | |
911 | for (( part = 1; part < MAX_PARTITIONS; part++ )); do | |
912 | typeset partition=${disk}${SLICE_PREFIX}${part} | |
913 | parted $DEV_DSKDIR/$disk -s rm $part > /dev/null 2>&1 | |
914 | if lsblk | grep -qF ${partition}; then | |
915 | log_fail "Partition ${partition} not deleted" | |
7050a65d | 916 | else |
815a6456 | 917 | log_note "Partition ${partition} deleted" |
7050a65d | 918 | fi |
7050a65d | 919 | done |
815a6456 | 920 | done |
7839c4b5 MM |
921 | elif is_freebsd; then |
922 | for disk in $DISKSARRAY; do | |
923 | if gpart destroy -F $disk; then | |
924 | log_note "Partitions for ${disk} deleted" | |
925 | else | |
926 | log_fail "Partitions for ${disk} not deleted" | |
927 | fi | |
928 | done | |
7050a65d | 929 | fi |
7050a65d SV |
930 | } |
931 | ||
6bb24f4d BB |
932 | # |
933 | # Get the end cyl of the given slice | |
934 | # | |
935 | function get_endslice #<disk> <slice> | |
936 | { | |
937 | typeset disk=$1 | |
938 | typeset slice=$2 | |
939 | if [[ -z $disk || -z $slice ]] ; then | |
940 | log_fail "The disk name or slice number is unspecified." | |
941 | fi | |
942 | ||
5c9f744b | 943 | case "$UNAME" in |
7839c4b5 | 944 | Linux) |
c1d9abf9 | 945 | endcyl=$(parted -s $DEV_DSKDIR/$disk -- unit cyl print | \ |
420b4448 | 946 | awk "/part${slice}/"' {sub(/cyl/, "", $3); print $3}') |
6bb24f4d | 947 | ((endcyl = (endcyl + 1))) |
7839c4b5 MM |
948 | ;; |
949 | FreeBSD) | |
950 | disk=${disk#/dev/zvol/} | |
951 | disk=${disk%p*} | |
952 | slice=$((slice + 1)) | |
953 | endcyl=$(gpart show $disk | \ | |
954 | awk -v slice=$slice '$3 == slice { print $1 + $2 }') | |
955 | ;; | |
956 | *) | |
6bb24f4d BB |
957 | disk=${disk#/dev/dsk/} |
958 | disk=${disk#/dev/rdsk/} | |
959 | disk=${disk%s*} | |
960 | ||
961 | typeset -i ratio=0 | |
c1d9abf9 | 962 | ratio=$(prtvtoc /dev/rdsk/${disk}s2 | \ |
75746e9a | 963 | awk '/sectors\/cylinder/ {print $2}') |
6bb24f4d BB |
964 | |
965 | if ((ratio == 0)); then | |
966 | return | |
967 | fi | |
968 | ||
c1d9abf9 | 969 | typeset -i endcyl=$(prtvtoc -h /dev/rdsk/${disk}s2 | |
75746e9a | 970 | awk -v token="$slice" '$1 == token {print $6}') |
6bb24f4d BB |
971 | |
972 | ((endcyl = (endcyl + 1) / ratio)) | |
7839c4b5 MM |
973 | ;; |
974 | esac | |
7c468940 | 975 | |
6bb24f4d BB |
976 | echo $endcyl |
977 | } | |
978 | ||
979 | ||
980 | # | |
981 | # Given a size,disk and total slice number, this function formats the | |
982 | # disk slices from 0 to the total slice number with the same specified | |
983 | # size. | |
984 | # | |
985 | function partition_disk #<slice_size> <whole_disk_name> <total_slices> | |
986 | { | |
987 | typeset -i i=0 | |
988 | typeset slice_size=$1 | |
989 | typeset disk_name=$2 | |
990 | typeset total_slices=$3 | |
991 | typeset cyl | |
992 | ||
993 | zero_partitions $disk_name | |
994 | while ((i < $total_slices)); do | |
995 | if ! is_linux; then | |
996 | if ((i == 2)); then | |
997 | ((i = i + 1)) | |
998 | continue | |
999 | fi | |
1000 | fi | |
cf8738d8 | 1001 | log_must set_partition $i "$cyl" $slice_size $disk_name |
6bb24f4d BB |
1002 | cyl=$(get_endslice $disk_name $i) |
1003 | ((i = i+1)) | |
1004 | done | |
1005 | } | |
1006 | ||
1007 | # | |
1008 | # This function continues to write to a filenum number of files into dirnum | |
c1d9abf9 | 1009 | # number of directories until either file_write returns an error or the |
6bb24f4d BB |
1010 | # maximum number of files per directory have been written. |
1011 | # | |
1012 | # Usage: | |
1013 | # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data] | |
1014 | # | |
1015 | # Return value: 0 on success | |
1016 | # non 0 on error | |
1017 | # | |
1018 | # Where : | |
1019 | # destdir: is the directory where everything is to be created under | |
1020 | # dirnum: the maximum number of subdirectories to use, -1 no limit | |
1021 | # filenum: the maximum number of files per subdirectory | |
1022 | # bytes: number of bytes to write | |
c6e457df | 1023 | # num_writes: number of types to write out bytes |
4e33ba4c | 1024 | # data: the data that will be written |
6bb24f4d BB |
1025 | # |
1026 | # E.g. | |
9be70c37 | 1027 | # fill_fs /testdir 20 25 1024 256 0 |
6bb24f4d BB |
1028 | # |
1029 | # Note: bytes * num_writes equals the size of the testfile | |
1030 | # | |
1031 | function fill_fs # destdir dirnum filenum bytes num_writes data | |
1032 | { | |
1033 | typeset destdir=${1:-$TESTDIR} | |
1034 | typeset -i dirnum=${2:-50} | |
1035 | typeset -i filenum=${3:-50} | |
1036 | typeset -i bytes=${4:-8192} | |
1037 | typeset -i num_writes=${5:-10240} | |
1b939560 | 1038 | typeset data=${6:-0} |
6bb24f4d | 1039 | |
9be70c37 RM |
1040 | mkdir -p $destdir/{1..$dirnum} |
1041 | for f in $destdir/{1..$dirnum}/$TESTFILE{1..$filenum}; do | |
1042 | file_write -o create -f $f -b $bytes -c $num_writes -d $data \ | |
23914a3b | 1043 | || return |
6bb24f4d | 1044 | done |
6bb24f4d BB |
1045 | } |
1046 | ||
75746e9a | 1047 | # Get the specified dataset property in parsable format or fail |
6bb24f4d BB |
1048 | function get_prop # property dataset |
1049 | { | |
6bb24f4d BB |
1050 | typeset prop=$1 |
1051 | typeset dataset=$2 | |
1052 | ||
75746e9a | 1053 | zfs get -Hpo value "$prop" "$dataset" || log_fail "zfs get $prop $dataset" |
6bb24f4d BB |
1054 | } |
1055 | ||
75746e9a | 1056 | # Get the specified pool property in parsable format or fail |
6bb24f4d BB |
1057 | function get_pool_prop # property pool |
1058 | { | |
6bb24f4d BB |
1059 | typeset prop=$1 |
1060 | typeset pool=$2 | |
1061 | ||
75746e9a | 1062 | zpool get -Hpo value "$prop" "$pool" || log_fail "zpool get $prop $pool" |
6bb24f4d BB |
1063 | } |
1064 | ||
1065 | # Return 0 if a pool exists; $? otherwise | |
1066 | # | |
1067 | # $1 - pool name | |
1068 | ||
1069 | function poolexists | |
1070 | { | |
1071 | typeset pool=$1 | |
1072 | ||
1073 | if [[ -z $pool ]]; then | |
1074 | log_note "No pool name given." | |
1075 | return 1 | |
1076 | fi | |
1077 | ||
c1d9abf9 | 1078 | zpool get name "$pool" > /dev/null 2>&1 |
6bb24f4d BB |
1079 | } |
1080 | ||
1081 | # Return 0 if all the specified datasets exist; $? otherwise | |
1082 | # | |
1083 | # $1-n dataset name | |
1084 | function datasetexists | |
1085 | { | |
1086 | if (($# == 0)); then | |
1087 | log_note "No dataset name given." | |
1088 | return 1 | |
1089 | fi | |
1090 | ||
41ebf403 | 1091 | zfs get name "$@" > /dev/null 2>&1 |
6bb24f4d BB |
1092 | } |
1093 | ||
1094 | # return 0 if none of the specified datasets exists, otherwise return 1. | |
1095 | # | |
1096 | # $1-n dataset name | |
1097 | function datasetnonexists | |
1098 | { | |
1099 | if (($# == 0)); then | |
1100 | log_note "No dataset name given." | |
1101 | return 1 | |
1102 | fi | |
1103 | ||
1104 | while (($# > 0)); do | |
c1d9abf9 | 1105 | zfs list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \ |
6bb24f4d BB |
1106 | && return 1 |
1107 | shift | |
1108 | done | |
1109 | ||
1110 | return 0 | |
1111 | } | |
1112 | ||
88d5580e AZ |
1113 | # FreeBSD breaks exports(5) at whitespace and doesn't process escapes |
1114 | # Solaris just breaks | |
1115 | # | |
1116 | # cf. https://github.com/openzfs/zfs/pull/13165#issuecomment-1059845807 | |
1117 | # | |
1118 | # Linux can have spaces (which are \OOO-escaped), | |
1119 | # but can't have backslashes because they're parsed recursively | |
1120 | function shares_can_have_whitespace | |
1121 | { | |
1122 | is_linux | |
1123 | } | |
1124 | ||
b7dbbf6a | 1125 | function is_shared_freebsd |
6bb24f4d BB |
1126 | { |
1127 | typeset fs=$1 | |
6bb24f4d | 1128 | |
5b0e75ca | 1129 | pgrep -q mountd && showmount -E | grep -qx "$fs" |
b7dbbf6a RM |
1130 | } |
1131 | ||
1132 | function is_shared_illumos | |
1133 | { | |
1134 | typeset fs=$1 | |
1135 | typeset mtpt | |
2f71caf2 | 1136 | |
c1d9abf9 | 1137 | for mtpt in `share | awk '{print $2}'` ; do |
6bb24f4d BB |
1138 | if [[ $mtpt == $fs ]] ; then |
1139 | return 0 | |
1140 | fi | |
1141 | done | |
1142 | ||
c1d9abf9 | 1143 | typeset stat=$(svcs -H -o STA nfs/server:default) |
6bb24f4d BB |
1144 | if [[ $stat != "ON" ]]; then |
1145 | log_note "Current nfs/server status: $stat" | |
1146 | fi | |
1147 | ||
1148 | return 1 | |
1149 | } | |
1150 | ||
b7dbbf6a RM |
1151 | function is_shared_linux |
1152 | { | |
1153 | typeset fs=$1 | |
5b0e75ca | 1154 | ! exportfs -s | awk -v fs="${fs//\\/\\\\}" '/^\// && $1 == fs {exit 1}' |
b7dbbf6a RM |
1155 | } |
1156 | ||
54eb2c41 PS |
1157 | # |
1158 | # Given a mountpoint, or a dataset name, determine if it is shared via NFS. | |
1159 | # | |
1160 | # Returns 0 if shared, 1 otherwise. | |
1161 | # | |
1162 | function is_shared | |
1163 | { | |
1164 | typeset fs=$1 | |
1165 | typeset mtpt | |
1166 | ||
1167 | if [[ $fs != "/"* ]] ; then | |
1168 | if datasetnonexists "$fs" ; then | |
1169 | return 1 | |
1170 | else | |
1171 | mtpt=$(get_prop mountpoint "$fs") | |
5b0e75ca | 1172 | case "$mtpt" in |
54eb2c41 PS |
1173 | none|legacy|-) return 1 |
1174 | ;; | |
1175 | *) fs=$mtpt | |
1176 | ;; | |
1177 | esac | |
1178 | fi | |
1179 | fi | |
1180 | ||
5c9f744b | 1181 | case "$UNAME" in |
b7dbbf6a RM |
1182 | FreeBSD) is_shared_freebsd "$fs" ;; |
1183 | Linux) is_shared_linux "$fs" ;; | |
1184 | *) is_shared_illumos "$fs" ;; | |
1185 | esac | |
54eb2c41 PS |
1186 | } |
1187 | ||
c15d36c6 GW |
1188 | function is_exported_illumos |
1189 | { | |
1190 | typeset fs=$1 | |
5b0e75ca | 1191 | typeset mtpt _ |
c15d36c6 | 1192 | |
5b0e75ca AZ |
1193 | while read -r mtpt _; do |
1194 | [ "$mtpt" = "$fs" ] && return | |
1195 | done < /etc/dfs/sharetab | |
c15d36c6 GW |
1196 | |
1197 | return 1 | |
1198 | } | |
1199 | ||
1200 | function is_exported_freebsd | |
1201 | { | |
1202 | typeset fs=$1 | |
5b0e75ca | 1203 | typeset mtpt _ |
c15d36c6 | 1204 | |
5b0e75ca AZ |
1205 | while read -r mtpt _; do |
1206 | [ "$mtpt" = "$fs" ] && return | |
1207 | done < /etc/zfs/exports | |
c15d36c6 GW |
1208 | |
1209 | return 1 | |
1210 | } | |
1211 | ||
1212 | function is_exported_linux | |
1213 | { | |
1214 | typeset fs=$1 | |
5b0e75ca | 1215 | typeset mtpt _ |
c15d36c6 | 1216 | |
5b0e75ca AZ |
1217 | while read -r mtpt _; do |
1218 | [ "$(printf "$mtpt")" = "$fs" ] && return | |
1219 | done < /etc/exports.d/zfs.exports | |
c15d36c6 GW |
1220 | |
1221 | return 1 | |
1222 | } | |
1223 | ||
1224 | # | |
1225 | # Given a mountpoint, or a dataset name, determine if it is exported via | |
1226 | # the os-specific NFS exports file. | |
1227 | # | |
1228 | # Returns 0 if exported, 1 otherwise. | |
1229 | # | |
1230 | function is_exported | |
1231 | { | |
1232 | typeset fs=$1 | |
1233 | typeset mtpt | |
1234 | ||
1235 | if [[ $fs != "/"* ]] ; then | |
1236 | if datasetnonexists "$fs" ; then | |
1237 | return 1 | |
1238 | else | |
1239 | mtpt=$(get_prop mountpoint "$fs") | |
1240 | case $mtpt in | |
1241 | none|legacy|-) return 1 | |
1242 | ;; | |
1243 | *) fs=$mtpt | |
1244 | ;; | |
1245 | esac | |
1246 | fi | |
1247 | fi | |
1248 | ||
5c9f744b | 1249 | case "$UNAME" in |
c15d36c6 GW |
1250 | FreeBSD) is_exported_freebsd "$fs" ;; |
1251 | Linux) is_exported_linux "$fs" ;; | |
1252 | *) is_exported_illumos "$fs" ;; | |
1253 | esac | |
1254 | } | |
1255 | ||
6bb24f4d | 1256 | # |
2f71caf2 | 1257 | # Given a dataset name determine if it is shared via SMB. |
6bb24f4d | 1258 | # |
2f71caf2 | 1259 | # Returns 0 if shared, 1 otherwise. |
6bb24f4d | 1260 | # |
2f71caf2 | 1261 | function is_shared_smb |
6bb24f4d BB |
1262 | { |
1263 | typeset fs=$1 | |
2f71caf2 | 1264 | |
5b0e75ca | 1265 | datasetexists "$fs" || return |
6bb24f4d BB |
1266 | |
1267 | if is_linux; then | |
dc2fe24c | 1268 | net usershare list | grep -xFq "${fs//[-\/]/_}" |
2f71caf2 | 1269 | else |
5c9f744b | 1270 | log_note "SMB on $UNAME currently unsupported by the test framework" |
6bb24f4d BB |
1271 | return 1 |
1272 | fi | |
2f71caf2 | 1273 | } |
1274 | ||
1275 | # | |
1276 | # Given a mountpoint, determine if it is not shared via NFS. | |
1277 | # | |
1278 | # Returns 0 if not shared, 1 otherwise. | |
1279 | # | |
1280 | function not_shared | |
1281 | { | |
bf228f3d | 1282 | ! is_shared $1 |
6bb24f4d BB |
1283 | } |
1284 | ||
1285 | # | |
2f71caf2 | 1286 | # Given a dataset determine if it is not shared via SMB. |
6bb24f4d | 1287 | # |
2f71caf2 | 1288 | # Returns 0 if not shared, 1 otherwise. |
1289 | # | |
1290 | function not_shared_smb | |
6bb24f4d | 1291 | { |
bf228f3d | 1292 | ! is_shared_smb $1 |
2f71caf2 | 1293 | } |
1294 | ||
1295 | # | |
1296 | # Helper function to unshare a mountpoint. | |
1297 | # | |
1298 | function unshare_fs #fs | |
1299 | { | |
1300 | typeset fs=$1 | |
1301 | ||
bf228f3d | 1302 | if is_shared $fs || is_shared_smb $fs; then |
bd328a58 | 1303 | log_must zfs unshare $fs |
6bb24f4d | 1304 | fi |
6bb24f4d BB |
1305 | } |
1306 | ||
2f71caf2 | 1307 | # |
1308 | # Helper function to share a NFS mountpoint. | |
1309 | # | |
1310 | function share_nfs #fs | |
1311 | { | |
1312 | typeset fs=$1 | |
1313 | ||
5b0e75ca AZ |
1314 | is_shared "$fs" && return |
1315 | ||
5c9f744b | 1316 | case "$UNAME" in |
5b0e75ca AZ |
1317 | Linux) |
1318 | log_must exportfs "*:$fs" | |
1319 | ;; | |
1320 | FreeBSD) | |
1321 | typeset mountd | |
1322 | read -r mountd < /var/run/mountd.pid | |
1323 | log_must eval "printf '%s\t\n' \"$fs\" >> /etc/zfs/exports" | |
1324 | log_must kill -s HUP "$mountd" | |
1325 | ;; | |
1326 | *) | |
1327 | log_must share -F nfs "$fs" | |
1328 | ;; | |
1329 | esac | |
2f71caf2 | 1330 | |
1331 | return 0 | |
1332 | } | |
1333 | ||
1334 | # | |
1335 | # Helper function to unshare a NFS mountpoint. | |
1336 | # | |
1337 | function unshare_nfs #fs | |
1338 | { | |
1339 | typeset fs=$1 | |
1340 | ||
5b0e75ca AZ |
1341 | ! is_shared "$fs" && return |
1342 | ||
5c9f744b | 1343 | case "$UNAME" in |
5b0e75ca AZ |
1344 | Linux) |
1345 | log_must exportfs -u "*:$fs" | |
1346 | ;; | |
1347 | FreeBSD) | |
1348 | typeset mountd | |
1349 | read -r mountd < /var/run/mountd.pid | |
1350 | awk -v fs="${fs//\\/\\\\}" '$1 != fs' /etc/zfs/exports > /etc/zfs/exports.$$ | |
1351 | log_must mv /etc/zfs/exports.$$ /etc/zfs/exports | |
1352 | log_must kill -s HUP "$mountd" | |
1353 | ;; | |
1354 | *) | |
1355 | log_must unshare -F nfs $fs | |
1356 | ;; | |
1357 | esac | |
2f71caf2 | 1358 | |
1359 | return 0 | |
1360 | } | |
1361 | ||
1362 | # | |
1363 | # Helper function to show NFS shares. | |
1364 | # | |
1365 | function showshares_nfs | |
1366 | { | |
5c9f744b | 1367 | case "$UNAME" in |
5b0e75ca AZ |
1368 | Linux) |
1369 | exportfs -v | |
1370 | ;; | |
1371 | FreeBSD) | |
1372 | showmount | |
1373 | ;; | |
1374 | *) | |
c1d9abf9 | 1375 | share -F nfs |
5b0e75ca AZ |
1376 | ;; |
1377 | esac | |
2f71caf2 | 1378 | } |
1379 | ||
c15d36c6 GW |
1380 | function check_nfs |
1381 | { | |
5c9f744b | 1382 | case "$UNAME" in |
5b0e75ca AZ |
1383 | Linux) |
1384 | exportfs -s | |
1385 | ;; | |
1386 | FreeBSD) | |
c15d36c6 | 1387 | showmount -e |
5b0e75ca AZ |
1388 | ;; |
1389 | *) | |
c15d36c6 | 1390 | log_unsupported "Unknown platform" |
5b0e75ca AZ |
1391 | ;; |
1392 | esac || log_unsupported "The NFS utilities are not installed" | |
c15d36c6 GW |
1393 | } |
1394 | ||
6bb24f4d BB |
1395 | # |
1396 | # Check NFS server status and trigger it online. | |
1397 | # | |
1398 | function setup_nfs_server | |
1399 | { | |
1400 | # Cannot share directory in non-global zone. | |
1401 | # | |
1402 | if ! is_global_zone; then | |
1403 | log_note "Cannot trigger NFS server by sharing in LZ." | |
1404 | return | |
1405 | fi | |
1406 | ||
c15d36c6 | 1407 | if is_linux; then |
2a0428f1 BB |
1408 | # |
1409 | # Re-synchronize /var/lib/nfs/etab with /etc/exports and | |
1410 | # /etc/exports.d./* to provide a clean test environment. | |
1411 | # | |
5b0e75ca | 1412 | log_must exportfs -r |
2a0428f1 | 1413 | |
c15d36c6 GW |
1414 | log_note "NFS server must be started prior to running ZTS." |
1415 | return | |
1416 | elif is_freebsd; then | |
5b0e75ca | 1417 | log_must kill -s HUP $(</var/run/mountd.pid) |
c15d36c6 | 1418 | |
2a0428f1 | 1419 | log_note "NFS server must be started prior to running ZTS." |
6bb24f4d BB |
1420 | return |
1421 | fi | |
1422 | ||
1423 | typeset nfs_fmri="svc:/network/nfs/server:default" | |
c1d9abf9 | 1424 | if [[ $(svcs -Ho STA $nfs_fmri) != "ON" ]]; then |
6bb24f4d BB |
1425 | # |
1426 | # Only really sharing operation can enable NFS server | |
1427 | # to online permanently. | |
1428 | # | |
1429 | typeset dummy=/tmp/dummy | |
1430 | ||
1431 | if [[ -d $dummy ]]; then | |
c1d9abf9 | 1432 | log_must rm -rf $dummy |
6bb24f4d BB |
1433 | fi |
1434 | ||
c1d9abf9 JWK |
1435 | log_must mkdir $dummy |
1436 | log_must share $dummy | |
6bb24f4d BB |
1437 | |
1438 | # | |
1439 | # Waiting for fmri's status to be the final status. | |
1440 | # Otherwise, in transition, an asterisk (*) is appended for | |
1441 | # instances, unshare will reverse status to 'DIS' again. | |
1442 | # | |
1443 | # Waiting for 1's at least. | |
1444 | # | |
c1d9abf9 | 1445 | log_must sleep 1 |
6bb24f4d | 1446 | timeout=10 |
c1d9abf9 | 1447 | while [[ timeout -ne 0 && $(svcs -Ho STA $nfs_fmri) == *'*' ]] |
6bb24f4d | 1448 | do |
c1d9abf9 | 1449 | log_must sleep 1 |
6bb24f4d BB |
1450 | |
1451 | ((timeout -= 1)) | |
1452 | done | |
1453 | ||
c1d9abf9 JWK |
1454 | log_must unshare $dummy |
1455 | log_must rm -rf $dummy | |
6bb24f4d BB |
1456 | fi |
1457 | ||
c1d9abf9 | 1458 | log_note "Current NFS status: '$(svcs -Ho STA,FMRI $nfs_fmri)'" |
6bb24f4d BB |
1459 | } |
1460 | ||
1461 | # | |
1462 | # To verify whether calling process is in global zone | |
1463 | # | |
1464 | # Return 0 if in global zone, 1 in non-global zone | |
1465 | # | |
1466 | function is_global_zone | |
1467 | { | |
7839c4b5 | 1468 | if is_linux || is_freebsd; then |
c1d9abf9 JWK |
1469 | return 0 |
1470 | else | |
1471 | typeset cur_zone=$(zonename 2>/dev/null) | |
bf228f3d | 1472 | [ $cur_zone = "global" ] |
6bb24f4d | 1473 | fi |
6bb24f4d BB |
1474 | } |
1475 | ||
1476 | # | |
1477 | # Verify whether test is permitted to run from | |
1478 | # global zone, local zone, or both | |
1479 | # | |
1480 | # $1 zone limit, could be "global", "local", or "both"(no limit) | |
1481 | # | |
1482 | # Return 0 if permitted, otherwise exit with log_unsupported | |
1483 | # | |
1484 | function verify_runnable # zone limit | |
1485 | { | |
1486 | typeset limit=$1 | |
1487 | ||
1488 | [[ -z $limit ]] && return 0 | |
1489 | ||
1490 | if is_global_zone ; then | |
1491 | case $limit in | |
1492 | global|both) | |
1493 | ;; | |
1494 | local) log_unsupported "Test is unable to run from "\ | |
1495 | "global zone." | |
1496 | ;; | |
1497 | *) log_note "Warning: unknown limit $limit - " \ | |
1498 | "use both." | |
1499 | ;; | |
1500 | esac | |
1501 | else | |
1502 | case $limit in | |
1503 | local|both) | |
1504 | ;; | |
1505 | global) log_unsupported "Test is unable to run from "\ | |
1506 | "local zone." | |
1507 | ;; | |
1508 | *) log_note "Warning: unknown limit $limit - " \ | |
1509 | "use both." | |
1510 | ;; | |
1511 | esac | |
1512 | ||
1513 | reexport_pool | |
1514 | fi | |
1515 | ||
1516 | return 0 | |
1517 | } | |
1518 | ||
1519 | # Return 0 if create successfully or the pool exists; $? otherwise | |
1520 | # Note: In local zones, this function should return 0 silently. | |
1521 | # | |
1522 | # $1 - pool name | |
1523 | # $2-n - [keyword] devs_list | |
1524 | ||
1525 | function create_pool #pool devs_list | |
1526 | { | |
1527 | typeset pool=${1%%/*} | |
1528 | ||
1529 | shift | |
1530 | ||
1531 | if [[ -z $pool ]]; then | |
1532 | log_note "Missing pool name." | |
1533 | return 1 | |
1534 | fi | |
1535 | ||
1536 | if poolexists $pool ; then | |
1537 | destroy_pool $pool | |
1538 | fi | |
1539 | ||
1540 | if is_global_zone ; then | |
c1d9abf9 JWK |
1541 | [[ -d /$pool ]] && rm -rf /$pool |
1542 | log_must zpool create -f $pool $@ | |
6bb24f4d BB |
1543 | fi |
1544 | ||
1545 | return 0 | |
1546 | } | |
1547 | ||
1548 | # Return 0 if destroy successfully or the pool exists; $? otherwise | |
1549 | # Note: In local zones, this function should return 0 silently. | |
1550 | # | |
1551 | # $1 - pool name | |
1552 | # Destroy pool with the given parameters. | |
1553 | ||
1554 | function destroy_pool #pool | |
1555 | { | |
1556 | typeset pool=${1%%/*} | |
1557 | typeset mtpt | |
1558 | ||
1559 | if [[ -z $pool ]]; then | |
1560 | log_note "No pool name given." | |
1561 | return 1 | |
1562 | fi | |
1563 | ||
1564 | if is_global_zone ; then | |
1565 | if poolexists "$pool" ; then | |
1566 | mtpt=$(get_prop mountpoint "$pool") | |
1567 | ||
851aa99c BB |
1568 | # At times, syseventd/udev activity can cause attempts |
1569 | # to destroy a pool to fail with EBUSY. We retry a few | |
6bb24f4d BB |
1570 | # times allowing failures before requiring the destroy |
1571 | # to succeed. | |
851aa99c | 1572 | log_must_busy zpool destroy -f $pool |
6bb24f4d BB |
1573 | |
1574 | [[ -d $mtpt ]] && \ | |
c1d9abf9 | 1575 | log_must rm -rf $mtpt |
6bb24f4d BB |
1576 | else |
1577 | log_note "Pool does not exist. ($pool)" | |
1578 | return 1 | |
1579 | fi | |
1580 | fi | |
1581 | ||
1582 | return 0 | |
1583 | } | |
1584 | ||
93491c4b JWK |
1585 | # Return 0 if created successfully; $? otherwise |
1586 | # | |
1587 | # $1 - dataset name | |
1588 | # $2-n - dataset options | |
1589 | ||
1590 | function create_dataset #dataset dataset_options | |
1591 | { | |
1592 | typeset dataset=$1 | |
1593 | ||
1594 | shift | |
1595 | ||
1596 | if [[ -z $dataset ]]; then | |
1597 | log_note "Missing dataset name." | |
1598 | return 1 | |
1599 | fi | |
1600 | ||
1601 | if datasetexists $dataset ; then | |
1602 | destroy_dataset $dataset | |
1603 | fi | |
1604 | ||
1605 | log_must zfs create $@ $dataset | |
1606 | ||
1607 | return 0 | |
1608 | } | |
1609 | ||
c7b55e71 GDN |
1610 | # Return 0 if destroy successfully or the dataset exists; $? otherwise |
1611 | # Note: In local zones, this function should return 0 silently. | |
1612 | # | |
1613 | # $1 - dataset name | |
1614 | # $2 - custom arguments for zfs destroy | |
1615 | # Destroy dataset with the given parameters. | |
1616 | ||
41ebf403 | 1617 | function destroy_dataset # dataset [args] |
c7b55e71 GDN |
1618 | { |
1619 | typeset dataset=$1 | |
1620 | typeset mtpt | |
1621 | typeset args=${2:-""} | |
1622 | ||
1623 | if [[ -z $dataset ]]; then | |
1624 | log_note "No dataset name given." | |
1625 | return 1 | |
1626 | fi | |
1627 | ||
1628 | if is_global_zone ; then | |
1629 | if datasetexists "$dataset" ; then | |
1630 | mtpt=$(get_prop mountpoint "$dataset") | |
1631 | log_must_busy zfs destroy $args $dataset | |
1632 | ||
41ebf403 | 1633 | [ -d $mtpt ] && log_must rm -rf $mtpt |
c7b55e71 GDN |
1634 | else |
1635 | log_note "Dataset does not exist. ($dataset)" | |
1636 | return 1 | |
1637 | fi | |
1638 | fi | |
1639 | ||
1640 | return 0 | |
1641 | } | |
1642 | ||
6bb24f4d BB |
1643 | # |
1644 | # Reexport TESTPOOL & TESTPOOL(1-4) | |
1645 | # | |
1646 | function reexport_pool | |
1647 | { | |
1648 | typeset -i cntctr=5 | |
1649 | typeset -i i=0 | |
1650 | ||
1651 | while ((i < cntctr)); do | |
1652 | if ((i == 0)); then | |
1653 | TESTPOOL=$ZONE_POOL/$ZONE_CTR$i | |
1654 | if ! ismounted $TESTPOOL; then | |
c1d9abf9 | 1655 | log_must zfs mount $TESTPOOL |
6bb24f4d BB |
1656 | fi |
1657 | else | |
1658 | eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i | |
1659 | if eval ! ismounted \$TESTPOOL$i; then | |
c1d9abf9 | 1660 | log_must eval zfs mount \$TESTPOOL$i |
6bb24f4d BB |
1661 | fi |
1662 | fi | |
1663 | ((i += 1)) | |
1664 | done | |
1665 | } | |
1666 | ||
1667 | # | |
ec0e24c2 | 1668 | # Verify a given disk or pool state |
6bb24f4d BB |
1669 | # |
1670 | # Return 0 is pool/disk matches expected state, 1 otherwise | |
1671 | # | |
ec0e24c2 | 1672 | function check_state # pool disk state{online,offline,degraded} |
6bb24f4d BB |
1673 | { |
1674 | typeset pool=$1 | |
1675 | typeset disk=${2#$DEV_DSKDIR/} | |
1676 | typeset state=$3 | |
1677 | ||
ec0e24c2 SV |
1678 | [[ -z $pool ]] || [[ -z $state ]] \ |
1679 | && log_fail "Arguments invalid or missing" | |
1680 | ||
1681 | if [[ -z $disk ]]; then | |
1682 | #check pool state only | |
23914a3b | 1683 | zpool get -H -o value health $pool | grep -qi "$state" |
ec0e24c2 | 1684 | else |
23914a3b | 1685 | zpool status -v $pool | grep "$disk" | grep -qi "$state" |
ec0e24c2 | 1686 | fi |
6bb24f4d BB |
1687 | } |
1688 | ||
1689 | # | |
1690 | # Get the mountpoint of snapshot | |
1691 | # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap> | |
1692 | # as its mountpoint | |
1693 | # | |
1694 | function snapshot_mountpoint | |
1695 | { | |
1696 | typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP} | |
1697 | ||
1698 | if [[ $dataset != *@* ]]; then | |
1699 | log_fail "Error name of snapshot '$dataset'." | |
1700 | fi | |
1701 | ||
1702 | typeset fs=${dataset%@*} | |
1703 | typeset snap=${dataset#*@} | |
1704 | ||
1705 | if [[ -z $fs || -z $snap ]]; then | |
1706 | log_fail "Error name of snapshot '$dataset'." | |
1707 | fi | |
1708 | ||
c1d9abf9 | 1709 | echo $(get_prop mountpoint $fs)/.zfs/snapshot/$snap |
6bb24f4d BB |
1710 | } |
1711 | ||
dddef7d6 | 1712 | # |
1713 | # Given a device and 'ashift' value verify it's correctly set on every label | |
1714 | # | |
1715 | function verify_ashift # device ashift | |
1716 | { | |
1717 | typeset device="$1" | |
1718 | typeset ashift="$2" | |
1719 | ||
75746e9a AZ |
1720 | zdb -e -lll $device | awk -v ashift=$ashift ' |
1721 | /ashift: / { | |
1722 | if (ashift != $2) | |
1723 | exit 1; | |
1724 | else | |
1725 | count++; | |
1726 | } | |
1727 | END { | |
1728 | exit (count != 4); | |
dddef7d6 | 1729 | }' |
dddef7d6 | 1730 | } |
1731 | ||
6bb24f4d BB |
1732 | # |
1733 | # Given a pool and file system, this function will verify the file system | |
1734 | # using the zdb internal tool. Note that the pool is exported and imported | |
1735 | # to ensure it has consistent state. | |
1736 | # | |
1737 | function verify_filesys # pool filesystem dir | |
1738 | { | |
1739 | typeset pool="$1" | |
1740 | typeset filesys="$2" | |
1741 | typeset zdbout="/tmp/zdbout.$$" | |
1742 | ||
1743 | shift | |
1744 | shift | |
1745 | typeset dirs=$@ | |
1746 | typeset search_path="" | |
1747 | ||
c1d9abf9 JWK |
1748 | log_note "Calling zdb to verify filesystem '$filesys'" |
1749 | zfs unmount -a > /dev/null 2>&1 | |
1750 | log_must zpool export $pool | |
6bb24f4d BB |
1751 | |
1752 | if [[ -n $dirs ]] ; then | |
1753 | for dir in $dirs ; do | |
1754 | search_path="$search_path -d $dir" | |
1755 | done | |
1756 | fi | |
1757 | ||
c1d9abf9 | 1758 | log_must zpool import $search_path $pool |
6bb24f4d | 1759 | |
23914a3b | 1760 | if ! zdb -cudi $filesys > $zdbout 2>&1; then |
c1d9abf9 JWK |
1761 | log_note "Output: zdb -cudi $filesys" |
1762 | cat $zdbout | |
23914a3b | 1763 | rm -f $zdbout |
c1d9abf9 | 1764 | log_fail "zdb detected errors with: '$filesys'" |
6bb24f4d BB |
1765 | fi |
1766 | ||
c1d9abf9 JWK |
1767 | log_must zfs mount -a |
1768 | log_must rm -rf $zdbout | |
6bb24f4d BB |
1769 | } |
1770 | ||
7c9a4292 BB |
1771 | # |
1772 | # Given a pool issue a scrub and verify that no checksum errors are reported. | |
1773 | # | |
1774 | function verify_pool | |
1775 | { | |
1776 | typeset pool=${1:-$TESTPOOL} | |
1777 | ||
1778 | log_must zpool scrub $pool | |
1779 | log_must wait_scrubbed $pool | |
1780 | ||
6ed4391d RM |
1781 | typeset -i cksum=$(zpool status $pool | awk ' |
1782 | !NF { isvdev = 0 } | |
1783 | isvdev { errors += $NF } | |
1784 | /CKSUM$/ { isvdev = 1 } | |
1785 | END { print errors } | |
1786 | ') | |
7c9a4292 BB |
1787 | if [[ $cksum != 0 ]]; then |
1788 | log_must zpool status -v | |
1789 | log_fail "Unexpected CKSUM errors found on $pool ($cksum)" | |
1790 | fi | |
1791 | } | |
1792 | ||
6bb24f4d BB |
1793 | # |
1794 | # Given a pool, and this function list all disks in the pool | |
1795 | # | |
1796 | function get_disklist # pool | |
1797 | { | |
f63c9dc7 AZ |
1798 | echo $(zpool iostat -v $1 | awk '(NR > 4) {print $1}' | \ |
1799 | grep -vEe '^-----' -e "^(mirror|raidz[1-3]|draid[1-3]|spare|log|cache|special|dedup)|\-[0-9]$") | |
6bb24f4d BB |
1800 | } |
1801 | ||
3c67d83a TH |
1802 | # |
1803 | # Given a pool, and this function list all disks in the pool with their full | |
1804 | # path (like "/dev/sda" instead of "sda"). | |
1805 | # | |
1806 | function get_disklist_fullpath # pool | |
1807 | { | |
270d0a5a | 1808 | get_disklist "-P $1" |
3c67d83a TH |
1809 | } |
1810 | ||
1811 | ||
1812 | ||
6bb24f4d BB |
1813 | # /** |
1814 | # This function kills a given list of processes after a time period. We use | |
1815 | # this in the stress tests instead of STF_TIMEOUT so that we can have processes | |
1816 | # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT | |
1817 | # would be listed as FAIL, which we don't want : we're happy with stress tests | |
1818 | # running for a certain amount of time, then finishing. | |
1819 | # | |
1820 | # @param $1 the time in seconds after which we should terminate these processes | |
1821 | # @param $2..$n the processes we wish to terminate. | |
1822 | # */ | |
1823 | function stress_timeout | |
1824 | { | |
1825 | typeset -i TIMEOUT=$1 | |
1826 | shift | |
1827 | typeset cpids="$@" | |
1828 | ||
1829 | log_note "Waiting for child processes($cpids). " \ | |
1830 | "It could last dozens of minutes, please be patient ..." | |
c1d9abf9 | 1831 | log_must sleep $TIMEOUT |
6bb24f4d BB |
1832 | |
1833 | log_note "Killing child processes after ${TIMEOUT} stress timeout." | |
1834 | typeset pid | |
1835 | for pid in $cpids; do | |
23914a3b | 1836 | ps -p $pid > /dev/null 2>&1 && |
c1d9abf9 | 1837 | log_must kill -USR1 $pid |
6bb24f4d BB |
1838 | done |
1839 | } | |
1840 | ||
1841 | # | |
1842 | # Verify a given hotspare disk is inuse or avail | |
1843 | # | |
1844 | # Return 0 is pool/disk matches expected state, 1 otherwise | |
1845 | # | |
1846 | function check_hotspare_state # pool disk state{inuse,avail} | |
1847 | { | |
1848 | typeset pool=$1 | |
1849 | typeset disk=${2#$DEV_DSKDIR/} | |
1850 | typeset state=$3 | |
1851 | ||
1852 | cur_state=$(get_device_state $pool $disk "spares") | |
1853 | ||
bd328a58 | 1854 | [ $state = $cur_state ] |
6bb24f4d BB |
1855 | } |
1856 | ||
d9daa7ab DQ |
1857 | # |
1858 | # Wait until a hotspare transitions to a given state or times out. | |
1859 | # | |
1860 | # Return 0 when pool/disk matches expected state, 1 on timeout. | |
1861 | # | |
1862 | function wait_hotspare_state # pool disk state timeout | |
1863 | { | |
1864 | typeset pool=$1 | |
6fa1e1e7 | 1865 | typeset disk=${2#*$DEV_DSKDIR/} |
d9daa7ab DQ |
1866 | typeset state=$3 |
1867 | typeset timeout=${4:-60} | |
1868 | typeset -i i=0 | |
1869 | ||
1870 | while [[ $i -lt $timeout ]]; do | |
1871 | if check_hotspare_state $pool $disk $state; then | |
1872 | return 0 | |
1873 | fi | |
1874 | ||
1875 | i=$((i+1)) | |
1876 | sleep 1 | |
1877 | done | |
1878 | ||
1879 | return 1 | |
1880 | } | |
1881 | ||
6bb24f4d BB |
1882 | # |
1883 | # Verify a given vdev disk is inuse or avail | |
1884 | # | |
1885 | # Return 0 is pool/disk matches expected state, 1 otherwise | |
1886 | # | |
55c12724 | 1887 | function check_vdev_state # pool disk state{online,offline,unavail,removed} |
6bb24f4d BB |
1888 | { |
1889 | typeset pool=$1 | |
6fa1e1e7 | 1890 | typeset disk=${2#*$DEV_DSKDIR/} |
6bb24f4d BB |
1891 | typeset state=$3 |
1892 | ||
1893 | cur_state=$(get_device_state $pool $disk) | |
1894 | ||
bd328a58 | 1895 | [ $state = $cur_state ] |
6bb24f4d BB |
1896 | } |
1897 | ||
d9daa7ab DQ |
1898 | # |
1899 | # Wait until a vdev transitions to a given state or times out. | |
1900 | # | |
1901 | # Return 0 when pool/disk matches expected state, 1 on timeout. | |
1902 | # | |
1903 | function wait_vdev_state # pool disk state timeout | |
1904 | { | |
1905 | typeset pool=$1 | |
6fa1e1e7 | 1906 | typeset disk=${2#*$DEV_DSKDIR/} |
d9daa7ab DQ |
1907 | typeset state=$3 |
1908 | typeset timeout=${4:-60} | |
1909 | typeset -i i=0 | |
1910 | ||
1911 | while [[ $i -lt $timeout ]]; do | |
1912 | if check_vdev_state $pool $disk $state; then | |
1913 | return 0 | |
1914 | fi | |
1915 | ||
1916 | i=$((i+1)) | |
1917 | sleep 1 | |
1918 | done | |
1919 | ||
1920 | return 1 | |
1921 | } | |
1922 | ||
6bb24f4d BB |
1923 | # |
1924 | # Check the output of 'zpool status -v <pool>', | |
1925 | # and to see if the content of <token> contain the <keyword> specified. | |
1926 | # | |
1927 | # Return 0 is contain, 1 otherwise | |
1928 | # | |
0ea05c64 | 1929 | function check_pool_status # pool token keyword <verbose> |
6bb24f4d BB |
1930 | { |
1931 | typeset pool=$1 | |
1932 | typeset token=$2 | |
1933 | typeset keyword=$3 | |
0ea05c64 | 1934 | typeset verbose=${4:-false} |
6bb24f4d | 1935 | |
964d4180 | 1936 | scan=$(zpool status -v "$pool" 2>/dev/null | awk -v token="$token:" '$1==token') |
0ea05c64 AP |
1937 | if [[ $verbose == true ]]; then |
1938 | log_note $scan | |
1939 | fi | |
f63c9dc7 | 1940 | echo $scan | grep -qi "$keyword" |
6bb24f4d BB |
1941 | } |
1942 | ||
1943 | # | |
e60e158e | 1944 | # The following functions are instance of check_pool_status() |
9a49d3f3 BB |
1945 | # is_pool_resilvering - to check if the pool resilver is in progress |
1946 | # is_pool_resilvered - to check if the pool resilver is completed | |
1947 | # is_pool_scrubbing - to check if the pool scrub is in progress | |
1948 | # is_pool_scrubbed - to check if the pool scrub is completed | |
1949 | # is_pool_scrub_stopped - to check if the pool scrub is stopped | |
1950 | # is_pool_scrub_paused - to check if the pool scrub has paused | |
1951 | # is_pool_removing - to check if the pool removing is a vdev | |
1952 | # is_pool_removed - to check if the pool remove is completed | |
1953 | # is_pool_discarding - to check if the pool checkpoint is being discarded | |
c8b3dda1 | 1954 | # is_pool_replacing - to check if the pool is performing a replacement |
6bb24f4d | 1955 | # |
0ea05c64 AP |
1956 | function is_pool_resilvering #pool <verbose> |
1957 | { | |
9a49d3f3 | 1958 | check_pool_status "$1" "scan" \ |
b2255edc | 1959 | "resilver[ ()0-9A-Za-z:_-]* in progress since" $2 |
0ea05c64 AP |
1960 | } |
1961 | ||
1962 | function is_pool_resilvered #pool <verbose> | |
6bb24f4d | 1963 | { |
0ea05c64 | 1964 | check_pool_status "$1" "scan" "resilvered " $2 |
6bb24f4d BB |
1965 | } |
1966 | ||
0ea05c64 | 1967 | function is_pool_scrubbing #pool <verbose> |
6bb24f4d | 1968 | { |
0ea05c64 | 1969 | check_pool_status "$1" "scan" "scrub in progress since " $2 |
6bb24f4d BB |
1970 | } |
1971 | ||
482eeef8 GA |
1972 | function is_pool_error_scrubbing #pool <verbose> |
1973 | { | |
1974 | check_pool_status "$1" "scrub" "error scrub in progress since " $2 | |
1975 | return $? | |
1976 | } | |
1977 | ||
0ea05c64 | 1978 | function is_pool_scrubbed #pool <verbose> |
6bb24f4d | 1979 | { |
0ea05c64 | 1980 | check_pool_status "$1" "scan" "scrub repaired" $2 |
6bb24f4d BB |
1981 | } |
1982 | ||
0ea05c64 | 1983 | function is_pool_scrub_stopped #pool <verbose> |
6bb24f4d | 1984 | { |
0ea05c64 | 1985 | check_pool_status "$1" "scan" "scrub canceled" $2 |
6bb24f4d BB |
1986 | } |
1987 | ||
482eeef8 GA |
1988 | function is_pool_error_scrub_stopped #pool <verbose> |
1989 | { | |
1990 | check_pool_status "$1" "scrub" "error scrub canceled on " $2 | |
1991 | return $? | |
1992 | } | |
1993 | ||
0ea05c64 | 1994 | function is_pool_scrub_paused #pool <verbose> |
6bb24f4d | 1995 | { |
0ea05c64 | 1996 | check_pool_status "$1" "scan" "scrub paused since " $2 |
6bb24f4d BB |
1997 | } |
1998 | ||
482eeef8 GA |
1999 | function is_pool_error_scrub_paused #pool <verbose> |
2000 | { | |
2001 | check_pool_status "$1" "scrub" "error scrub paused since " $2 | |
2002 | return $? | |
2003 | } | |
2004 | ||
a1d477c2 MA |
2005 | function is_pool_removing #pool |
2006 | { | |
2007 | check_pool_status "$1" "remove" "in progress since " | |
a1d477c2 MA |
2008 | } |
2009 | ||
2010 | function is_pool_removed #pool | |
2011 | { | |
2012 | check_pool_status "$1" "remove" "completed on" | |
a1d477c2 MA |
2013 | } |
2014 | ||
e60e158e JG |
2015 | function is_pool_discarding #pool |
2016 | { | |
2017 | check_pool_status "$1" "checkpoint" "discarding" | |
e60e158e | 2018 | } |
c8b3dda1 BB |
2019 | function is_pool_replacing #pool |
2020 | { | |
2021 | zpool status "$1" | grep -qE 'replacing-[0-9]+' | |
2022 | } | |
e60e158e | 2023 | |
ab44e511 JWK |
2024 | function wait_for_degraded |
2025 | { | |
2026 | typeset pool=$1 | |
2027 | typeset timeout=${2:-30} | |
2028 | typeset t0=$SECONDS | |
2029 | ||
2030 | while :; do | |
2031 | [[ $(get_pool_prop health $pool) == "DEGRADED" ]] && break | |
2032 | log_note "$pool is not yet degraded." | |
2033 | sleep 1 | |
2034 | if ((SECONDS - t0 > $timeout)); then | |
2035 | log_note "$pool not degraded after $timeout seconds." | |
2036 | return 1 | |
2037 | fi | |
2038 | done | |
2039 | ||
2040 | return 0 | |
2041 | } | |
2042 | ||
6bb24f4d | 2043 | # |
4e33ba4c | 2044 | # Use create_pool()/destroy_pool() to clean up the information in |
6bb24f4d BB |
2045 | # in the given disk to avoid slice overlapping. |
2046 | # | |
2047 | function cleanup_devices #vdevs | |
2048 | { | |
2049 | typeset pool="foopool$$" | |
2050 | ||
581ca281 BB |
2051 | for vdev in $@; do |
2052 | zero_partitions $vdev | |
2053 | done | |
6bb24f4d | 2054 | |
581ca281 | 2055 | poolexists $pool && destroy_pool $pool |
6bb24f4d BB |
2056 | create_pool $pool $@ |
2057 | destroy_pool $pool | |
2058 | ||
2059 | return 0 | |
2060 | } | |
2061 | ||
6bb24f4d BB |
2062 | #/** |
2063 | # A function to find and locate free disks on a system or from given | |
2064 | # disks as the parameter. It works by locating disks that are in use | |
2065 | # as swap devices and dump devices, and also disks listed in /etc/vfstab | |
2066 | # | |
2067 | # $@ given disks to find which are free, default is all disks in | |
2068 | # the test system | |
2069 | # | |
2070 | # @return a string containing the list of available disks | |
2071 | #*/ | |
2072 | function find_disks | |
2073 | { | |
2074 | # Trust provided list, no attempt is made to locate unused devices. | |
7839c4b5 | 2075 | if is_linux || is_freebsd; then |
c1d9abf9 | 2076 | echo "$@" |
6bb24f4d BB |
2077 | return |
2078 | fi | |
2079 | ||
2080 | ||
2081 | sfi=/tmp/swaplist.$$ | |
2082 | dmpi=/tmp/dumpdev.$$ | |
2083 | max_finddisksnum=${MAX_FINDDISKSNUM:-6} | |
2084 | ||
c1d9abf9 JWK |
2085 | swap -l > $sfi |
2086 | dumpadm > $dmpi 2>/dev/null | |
6bb24f4d | 2087 | |
75746e9a AZ |
2088 | disks=${@:-$(echo "" | format -e 2>/dev/null | awk ' |
2089 | BEGIN { FS="."; } | |
6bb24f4d | 2090 | |
75746e9a AZ |
2091 | /^Specify disk/{ |
2092 | searchdisks=0; | |
2093 | } | |
6bb24f4d | 2094 | |
75746e9a AZ |
2095 | { |
2096 | if (searchdisks && $2 !~ "^$"){ | |
2097 | split($2,arr," "); | |
2098 | print arr[1]; | |
6bb24f4d | 2099 | } |
75746e9a | 2100 | } |
6bb24f4d | 2101 | |
75746e9a AZ |
2102 | /^AVAILABLE DISK SELECTIONS:/{ |
2103 | searchdisks=1; | |
2104 | } | |
2105 | ')} | |
6bb24f4d BB |
2106 | |
2107 | unused="" | |
2108 | for disk in $disks; do | |
2109 | # Check for mounted | |
23914a3b | 2110 | grep -q "${disk}[sp]" /etc/mnttab && continue |
6bb24f4d | 2111 | # Check for swap |
23914a3b | 2112 | grep -q "${disk}[sp]" $sfi && continue |
6bb24f4d | 2113 | # check for dump device |
23914a3b | 2114 | grep -q "${disk}[sp]" $dmpi && continue |
6bb24f4d BB |
2115 | # check to see if this disk hasn't been explicitly excluded |
2116 | # by a user-set environment variable | |
23914a3b | 2117 | echo "${ZFS_HOST_DEVICES_IGNORE}" | grep -q "${disk}" && continue |
6bb24f4d BB |
2118 | unused_candidates="$unused_candidates $disk" |
2119 | done | |
23914a3b | 2120 | rm $sfi $dmpi |
6bb24f4d BB |
2121 | |
2122 | # now just check to see if those disks do actually exist | |
2123 | # by looking for a device pointing to the first slice in | |
2124 | # each case. limit the number to max_finddisksnum | |
2125 | count=0 | |
2126 | for disk in $unused_candidates; do | |
665684d7 RM |
2127 | if is_disk_device $DEV_DSKDIR/${disk}s0 && \ |
2128 | [ $count -lt $max_finddisksnum ]; then | |
6bb24f4d BB |
2129 | unused="$unused $disk" |
2130 | # do not impose limit if $@ is provided | |
2131 | [[ -z $@ ]] && ((count = count + 1)) | |
2132 | fi | |
6bb24f4d BB |
2133 | done |
2134 | ||
2135 | # finally, return our disk list | |
c1d9abf9 | 2136 | echo $unused |
6bb24f4d BB |
2137 | } |
2138 | ||
7839c4b5 MM |
2139 | function add_user_freebsd #<group_name> <user_name> <basedir> |
2140 | { | |
2141 | typeset group=$1 | |
2142 | typeset user=$2 | |
2143 | typeset basedir=$3 | |
2144 | ||
2145 | # Check to see if the user exists. | |
2146 | if id $user > /dev/null 2>&1; then | |
2147 | return 0 | |
2148 | fi | |
2149 | ||
2150 | # Assign 1000 as the base uid | |
2151 | typeset -i uid=1000 | |
2152 | while true; do | |
7839c4b5 | 2153 | pw useradd -u $uid -g $group -d $basedir/$user -m -n $user |
23914a3b | 2154 | case $? in |
7839c4b5 MM |
2155 | 0) break ;; |
2156 | # The uid is not unique | |
2157 | 65) ((uid += 1)) ;; | |
2158 | *) return 1 ;; | |
2159 | esac | |
2160 | if [[ $uid == 65000 ]]; then | |
2161 | log_fail "No user id available under 65000 for $user" | |
2162 | fi | |
2163 | done | |
2164 | ||
2165 | # Silence MOTD | |
2166 | touch $basedir/$user/.hushlogin | |
2167 | ||
2168 | return 0 | |
2169 | } | |
2170 | ||
2171 | # | |
2172 | # Delete the specified user. | |
2173 | # | |
2174 | # $1 login name | |
2175 | # | |
2176 | function del_user_freebsd #<logname> | |
2177 | { | |
2178 | typeset user=$1 | |
2179 | ||
2180 | if id $user > /dev/null 2>&1; then | |
2181 | log_must pw userdel $user | |
2182 | fi | |
2183 | ||
2184 | return 0 | |
2185 | } | |
2186 | ||
2187 | # | |
2188 | # Select valid gid and create specified group. | |
2189 | # | |
2190 | # $1 group name | |
2191 | # | |
2192 | function add_group_freebsd #<group_name> | |
2193 | { | |
2194 | typeset group=$1 | |
2195 | ||
2196 | # See if the group already exists. | |
2197 | if pw groupshow $group >/dev/null 2>&1; then | |
2198 | return 0 | |
2199 | fi | |
2200 | ||
2201 | # Assign 1000 as the base gid | |
2202 | typeset -i gid=1000 | |
2203 | while true; do | |
2204 | pw groupadd -g $gid -n $group > /dev/null 2>&1 | |
23914a3b | 2205 | case $? in |
7839c4b5 MM |
2206 | 0) return 0 ;; |
2207 | # The gid is not unique | |
2208 | 65) ((gid += 1)) ;; | |
2209 | *) return 1 ;; | |
2210 | esac | |
2211 | if [[ $gid == 65000 ]]; then | |
2212 | log_fail "No user id available under 65000 for $group" | |
2213 | fi | |
2214 | done | |
2215 | } | |
2216 | ||
2217 | # | |
2218 | # Delete the specified group. | |
2219 | # | |
2220 | # $1 group name | |
2221 | # | |
2222 | function del_group_freebsd #<group_name> | |
2223 | { | |
2224 | typeset group=$1 | |
2225 | ||
2226 | pw groupdel -n $group > /dev/null 2>&1 | |
23914a3b | 2227 | case $? in |
7839c4b5 MM |
2228 | # Group does not exist, or was deleted successfully. |
2229 | 0|6|65) return 0 ;; | |
2230 | # Name already exists as a group name | |
2231 | 9) log_must pw groupdel $group ;; | |
2232 | *) return 1 ;; | |
2233 | esac | |
2234 | ||
2235 | return 0 | |
2236 | } | |
2237 | ||
2238 | function add_user_illumos #<group_name> <user_name> <basedir> | |
2239 | { | |
2240 | typeset group=$1 | |
2241 | typeset user=$2 | |
2242 | typeset basedir=$3 | |
2243 | ||
2244 | log_must useradd -g $group -d $basedir/$user -m $user | |
2245 | ||
2246 | return 0 | |
2247 | } | |
2248 | ||
2249 | function del_user_illumos #<user_name> | |
2250 | { | |
2251 | typeset user=$1 | |
2252 | ||
2253 | if id $user > /dev/null 2>&1; then | |
2254 | log_must_retry "currently used" 6 userdel $user | |
2255 | fi | |
2256 | ||
2257 | return 0 | |
2258 | } | |
2259 | ||
2260 | function add_group_illumos #<group_name> | |
2261 | { | |
2262 | typeset group=$1 | |
2263 | ||
2264 | typeset -i gid=100 | |
2265 | while true; do | |
2266 | groupadd -g $gid $group > /dev/null 2>&1 | |
23914a3b | 2267 | case $? in |
7839c4b5 MM |
2268 | 0) return 0 ;; |
2269 | # The gid is not unique | |
2270 | 4) ((gid += 1)) ;; | |
2271 | *) return 1 ;; | |
2272 | esac | |
2273 | done | |
2274 | } | |
2275 | ||
2276 | function del_group_illumos #<group_name> | |
2277 | { | |
2278 | typeset group=$1 | |
2279 | ||
2280 | groupmod -n $grp $grp > /dev/null 2>&1 | |
23914a3b | 2281 | case $? in |
7839c4b5 MM |
2282 | # Group does not exist. |
2283 | 6) return 0 ;; | |
2284 | # Name already exists as a group name | |
2285 | 9) log_must groupdel $grp ;; | |
2286 | *) return 1 ;; | |
2287 | esac | |
2288 | } | |
2289 | ||
2290 | function add_user_linux #<group_name> <user_name> <basedir> | |
2291 | { | |
2292 | typeset group=$1 | |
2293 | typeset user=$2 | |
2294 | typeset basedir=$3 | |
2295 | ||
2296 | log_must useradd -g $group -d $basedir/$user -m $user | |
2297 | ||
2298 | # Add new users to the same group and the command line utils. | |
2299 | # This allows them to be run out of the original users home | |
2300 | # directory as long as it permissioned to be group readable. | |
592cf7f1 | 2301 | cmd_group=$(stat --format="%G" $(command -v zfs)) |
7839c4b5 MM |
2302 | log_must usermod -a -G $cmd_group $user |
2303 | ||
2304 | return 0 | |
2305 | } | |
2306 | ||
2307 | function del_user_linux #<user_name> | |
2308 | { | |
2309 | typeset user=$1 | |
2310 | ||
2311 | if id $user > /dev/null 2>&1; then | |
2312 | log_must_retry "currently used" 6 userdel $user | |
2313 | fi | |
7839c4b5 MM |
2314 | } |
2315 | ||
2316 | function add_group_linux #<group_name> | |
2317 | { | |
2318 | typeset group=$1 | |
2319 | ||
2320 | # Assign 100 as the base gid, a larger value is selected for | |
2321 | # Linux because for many distributions 1000 and under are reserved. | |
2322 | while true; do | |
2323 | groupadd $group > /dev/null 2>&1 | |
23914a3b | 2324 | case $? in |
7839c4b5 MM |
2325 | 0) return 0 ;; |
2326 | *) return 1 ;; | |
2327 | esac | |
2328 | done | |
2329 | } | |
2330 | ||
2331 | function del_group_linux #<group_name> | |
2332 | { | |
2333 | typeset group=$1 | |
2334 | ||
2335 | getent group $group > /dev/null 2>&1 | |
23914a3b | 2336 | case $? in |
7839c4b5 MM |
2337 | # Group does not exist. |
2338 | 2) return 0 ;; | |
2339 | # Name already exists as a group name | |
2340 | 0) log_must groupdel $group ;; | |
2341 | *) return 1 ;; | |
2342 | esac | |
2343 | ||
2344 | return 0 | |
2345 | } | |
2346 | ||
6bb24f4d BB |
2347 | # |
2348 | # Add specified user to specified group | |
2349 | # | |
2350 | # $1 group name | |
2351 | # $2 user name | |
2352 | # $3 base of the homedir (optional) | |
2353 | # | |
2354 | function add_user #<group_name> <user_name> <basedir> | |
2355 | { | |
7839c4b5 MM |
2356 | typeset group=$1 |
2357 | typeset user=$2 | |
6bb24f4d BB |
2358 | typeset basedir=${3:-"/var/tmp"} |
2359 | ||
7839c4b5 | 2360 | if ((${#group} == 0 || ${#user} == 0)); then |
6bb24f4d BB |
2361 | log_fail "group name or user name are not defined." |
2362 | fi | |
2363 | ||
5c9f744b | 2364 | case "$UNAME" in |
7839c4b5 MM |
2365 | FreeBSD) |
2366 | add_user_freebsd "$group" "$user" "$basedir" | |
2367 | ;; | |
2368 | Linux) | |
2369 | add_user_linux "$group" "$user" "$basedir" | |
2370 | ;; | |
2371 | *) | |
2372 | add_user_illumos "$group" "$user" "$basedir" | |
2373 | ;; | |
2374 | esac | |
6bb24f4d BB |
2375 | |
2376 | return 0 | |
2377 | } | |
2378 | ||
2379 | # | |
2380 | # Delete the specified user. | |
2381 | # | |
2382 | # $1 login name | |
2383 | # $2 base of the homedir (optional) | |
2384 | # | |
2385 | function del_user #<logname> <basedir> | |
2386 | { | |
2387 | typeset user=$1 | |
2388 | typeset basedir=${2:-"/var/tmp"} | |
2389 | ||
2390 | if ((${#user} == 0)); then | |
2391 | log_fail "login name is necessary." | |
2392 | fi | |
2393 | ||
5c9f744b | 2394 | case "$UNAME" in |
7839c4b5 MM |
2395 | FreeBSD) |
2396 | del_user_freebsd "$user" | |
2397 | ;; | |
2398 | Linux) | |
2399 | del_user_linux "$user" | |
2400 | ;; | |
2401 | *) | |
2402 | del_user_illumos "$user" | |
2403 | ;; | |
2404 | esac | |
6bb24f4d | 2405 | |
c1d9abf9 | 2406 | [[ -d $basedir/$user ]] && rm -fr $basedir/$user |
6bb24f4d BB |
2407 | |
2408 | return 0 | |
2409 | } | |
2410 | ||
2411 | # | |
2412 | # Select valid gid and create specified group. | |
2413 | # | |
2414 | # $1 group name | |
2415 | # | |
2416 | function add_group #<group_name> | |
2417 | { | |
2418 | typeset group=$1 | |
2419 | ||
2420 | if ((${#group} == 0)); then | |
2421 | log_fail "group name is necessary." | |
2422 | fi | |
2423 | ||
5c9f744b | 2424 | case "$UNAME" in |
7839c4b5 MM |
2425 | FreeBSD) |
2426 | add_group_freebsd "$group" | |
2427 | ;; | |
2428 | Linux) | |
2429 | add_group_linux "$group" | |
2430 | ;; | |
2431 | *) | |
2432 | add_group_illumos "$group" | |
2433 | ;; | |
2434 | esac | |
2435 | ||
2436 | return 0 | |
6bb24f4d BB |
2437 | } |
2438 | ||
2439 | # | |
2440 | # Delete the specified group. | |
2441 | # | |
2442 | # $1 group name | |
2443 | # | |
2444 | function del_group #<group_name> | |
2445 | { | |
7839c4b5 MM |
2446 | typeset group=$1 |
2447 | ||
2448 | if ((${#group} == 0)); then | |
6bb24f4d BB |
2449 | log_fail "group name is necessary." |
2450 | fi | |
2451 | ||
5c9f744b | 2452 | case "$UNAME" in |
7839c4b5 MM |
2453 | FreeBSD) |
2454 | del_group_freebsd "$group" | |
2455 | ;; | |
2456 | Linux) | |
2457 | del_group_linux "$group" | |
2458 | ;; | |
2459 | *) | |
2460 | del_group_illumos "$group" | |
2461 | ;; | |
2462 | esac | |
6bb24f4d BB |
2463 | |
2464 | return 0 | |
2465 | } | |
2466 | ||
2467 | # | |
2468 | # This function will return true if it's safe to destroy the pool passed | |
2469 | # as argument 1. It checks for pools based on zvols and files, and also | |
2470 | # files contained in a pool that may have a different mountpoint. | |
2471 | # | |
2472 | function safe_to_destroy_pool { # $1 the pool name | |
2473 | ||
2474 | typeset pool="" | |
2475 | typeset DONT_DESTROY="" | |
2476 | ||
2477 | # We check that by deleting the $1 pool, we're not | |
2478 | # going to pull the rug out from other pools. Do this | |
2479 | # by looking at all other pools, ensuring that they | |
2480 | # aren't built from files or zvols contained in this pool. | |
2481 | ||
c1d9abf9 | 2482 | for pool in $(zpool list -H -o name) |
6bb24f4d BB |
2483 | do |
2484 | ALTMOUNTPOOL="" | |
2485 | ||
2486 | # this is a list of the top-level directories in each of the | |
2487 | # files that make up the path to the files the pool is based on | |
75746e9a | 2488 | FILEPOOL=$(zpool status -v $pool | awk -v pool="/$1/" '$0 ~ pool {print $1}') |
6bb24f4d BB |
2489 | |
2490 | # this is a list of the zvols that make up the pool | |
75746e9a | 2491 | ZVOLPOOL=$(zpool status -v $pool | awk -v zvols="$ZVOL_DEVDIR/$1$" '$0 ~ zvols {print $1}') |
6bb24f4d BB |
2492 | |
2493 | # also want to determine if it's a file-based pool using an | |
2494 | # alternate mountpoint... | |
c1d9abf9 | 2495 | POOL_FILE_DIRS=$(zpool status -v $pool | \ |
75746e9a AZ |
2496 | awk '/\// {print $1}' | \ |
2497 | awk -F/ '!/dev/ {print $2}') | |
6bb24f4d BB |
2498 | |
2499 | for pooldir in $POOL_FILE_DIRS | |
2500 | do | |
c1d9abf9 | 2501 | OUTPUT=$(zfs list -H -r -o mountpoint $1 | \ |
75746e9a | 2502 | awk -v pd="${pooldir}$" '$0 ~ pd {print $1}') |
6bb24f4d BB |
2503 | |
2504 | ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}" | |
2505 | done | |
2506 | ||
2507 | ||
2508 | if [ ! -z "$ZVOLPOOL" ] | |
2509 | then | |
2510 | DONT_DESTROY="true" | |
2511 | log_note "Pool $pool is built from $ZVOLPOOL on $1" | |
2512 | fi | |
2513 | ||
2514 | if [ ! -z "$FILEPOOL" ] | |
2515 | then | |
2516 | DONT_DESTROY="true" | |
2517 | log_note "Pool $pool is built from $FILEPOOL on $1" | |
2518 | fi | |
2519 | ||
2520 | if [ ! -z "$ALTMOUNTPOOL" ] | |
2521 | then | |
2522 | DONT_DESTROY="true" | |
2523 | log_note "Pool $pool is built from $ALTMOUNTPOOL on $1" | |
2524 | fi | |
2525 | done | |
2526 | ||
2527 | if [ -z "${DONT_DESTROY}" ] | |
2528 | then | |
2529 | return 0 | |
2530 | else | |
2531 | log_note "Warning: it is not safe to destroy $1!" | |
2532 | return 1 | |
2533 | fi | |
2534 | } | |
2535 | ||
6bb24f4d BB |
2536 | # |
2537 | # Verify zfs operation with -p option work as expected | |
2538 | # $1 operation, value could be create, clone or rename | |
2539 | # $2 dataset type, value could be fs or vol | |
2540 | # $3 dataset name | |
2541 | # $4 new dataset name | |
2542 | # | |
2543 | function verify_opt_p_ops | |
2544 | { | |
2545 | typeset ops=$1 | |
2546 | typeset datatype=$2 | |
2547 | typeset dataset=$3 | |
2548 | typeset newdataset=$4 | |
2549 | ||
2550 | if [[ $datatype != "fs" && $datatype != "vol" ]]; then | |
2551 | log_fail "$datatype is not supported." | |
2552 | fi | |
2553 | ||
2554 | # check parameters accordingly | |
2555 | case $ops in | |
2556 | create) | |
2557 | newdataset=$dataset | |
2558 | dataset="" | |
2559 | if [[ $datatype == "vol" ]]; then | |
2560 | ops="create -V $VOLSIZE" | |
2561 | fi | |
2562 | ;; | |
2563 | clone) | |
2564 | if [[ -z $newdataset ]]; then | |
2565 | log_fail "newdataset should not be empty" \ | |
2566 | "when ops is $ops." | |
2567 | fi | |
2568 | log_must datasetexists $dataset | |
2569 | log_must snapexists $dataset | |
2570 | ;; | |
2571 | rename) | |
2572 | if [[ -z $newdataset ]]; then | |
2573 | log_fail "newdataset should not be empty" \ | |
2574 | "when ops is $ops." | |
2575 | fi | |
2576 | log_must datasetexists $dataset | |
6bb24f4d BB |
2577 | ;; |
2578 | *) | |
2579 | log_fail "$ops is not supported." | |
2580 | ;; | |
2581 | esac | |
2582 | ||
2583 | # make sure the upper level filesystem does not exist | |
c7b55e71 | 2584 | destroy_dataset "${newdataset%/*}" "-rRf" |
6bb24f4d BB |
2585 | |
2586 | # without -p option, operation will fail | |
c1d9abf9 | 2587 | log_mustnot zfs $ops $dataset $newdataset |
6bb24f4d BB |
2588 | log_mustnot datasetexists $newdataset ${newdataset%/*} |
2589 | ||
2590 | # with -p option, operation should succeed | |
c1d9abf9 | 2591 | log_must zfs $ops -p $dataset $newdataset |
6bb24f4d BB |
2592 | block_device_wait |
2593 | ||
2594 | if ! datasetexists $newdataset ; then | |
2595 | log_fail "-p option does not work for $ops" | |
2596 | fi | |
2597 | ||
2598 | # when $ops is create or clone, redo the operation still return zero | |
2599 | if [[ $ops != "rename" ]]; then | |
c1d9abf9 | 2600 | log_must zfs $ops -p $dataset $newdataset |
6bb24f4d BB |
2601 | fi |
2602 | ||
2603 | return 0 | |
2604 | } | |
2605 | ||
2606 | # | |
2607 | # Get configuration of pool | |
2608 | # $1 pool name | |
2609 | # $2 config name | |
2610 | # | |
2611 | function get_config | |
2612 | { | |
2613 | typeset pool=$1 | |
2614 | typeset config=$2 | |
6bb24f4d BB |
2615 | |
2616 | if ! poolexists "$pool" ; then | |
2617 | return 1 | |
2618 | fi | |
75746e9a AZ |
2619 | if [ "$(get_pool_prop cachefile "$pool")" = "none" ]; then |
2620 | zdb -e $pool | |
6bb24f4d | 2621 | else |
75746e9a AZ |
2622 | zdb -C $pool |
2623 | fi | awk -F: -v cfg="$config:" '$0 ~ cfg {sub(/^'\''/, $2); sub(/'\''$/, $2); print $2}' | |
6bb24f4d BB |
2624 | } |
2625 | ||
2626 | # | |
2627 | # Privated function. Random select one of items from arguments. | |
2628 | # | |
2629 | # $1 count | |
2630 | # $2-n string | |
2631 | # | |
2632 | function _random_get | |
2633 | { | |
2634 | typeset cnt=$1 | |
2635 | shift | |
2636 | ||
2637 | typeset str="$@" | |
2638 | typeset -i ind | |
2639 | ((ind = RANDOM % cnt + 1)) | |
2640 | ||
75746e9a | 2641 | echo "$str" | cut -f $ind -d ' ' |
6bb24f4d BB |
2642 | } |
2643 | ||
2644 | # | |
2645 | # Random select one of item from arguments which include NONE string | |
2646 | # | |
2647 | function random_get_with_non | |
2648 | { | |
2649 | typeset -i cnt=$# | |
2650 | ((cnt =+ 1)) | |
2651 | ||
2652 | _random_get "$cnt" "$@" | |
2653 | } | |
2654 | ||
2655 | # | |
2656 | # Random select one of item from arguments which doesn't include NONE string | |
2657 | # | |
2658 | function random_get | |
2659 | { | |
2660 | _random_get "$#" "$@" | |
2661 | } | |
2662 | ||
6bb24f4d BB |
2663 | # |
2664 | # The function will generate a dataset name with specific length | |
2665 | # $1, the length of the name | |
2666 | # $2, the base string to construct the name | |
2667 | # | |
2668 | function gen_dataset_name | |
2669 | { | |
2670 | typeset -i len=$1 | |
2671 | typeset basestr="$2" | |
2672 | typeset -i baselen=${#basestr} | |
2673 | typeset -i iter=0 | |
2674 | typeset l_name="" | |
2675 | ||
2676 | if ((len % baselen == 0)); then | |
2677 | ((iter = len / baselen)) | |
2678 | else | |
2679 | ((iter = len / baselen + 1)) | |
2680 | fi | |
2681 | while ((iter > 0)); do | |
2682 | l_name="${l_name}$basestr" | |
2683 | ||
2684 | ((iter -= 1)) | |
2685 | done | |
2686 | ||
c1d9abf9 | 2687 | echo $l_name |
6bb24f4d BB |
2688 | } |
2689 | ||
2690 | # | |
2691 | # Get cksum tuple of dataset | |
2692 | # $1 dataset name | |
2693 | # | |
2694 | # sample zdb output: | |
2695 | # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp | |
2696 | # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4 | |
2697 | # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P | |
2698 | # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744 | |
2699 | function datasetcksum | |
2700 | { | |
2701 | typeset cksum | |
c1d9abf9 | 2702 | sync |
7454275a | 2703 | sync_all_pools |
75746e9a | 2704 | zdb -vvv $1 | awk -F= -v ds="^Dataset $1 "'\\[' '$0 ~ ds && /cksum/ {print $7}' |
6bb24f4d BB |
2705 | } |
2706 | ||
2707 | # | |
2708 | # Get the given disk/slice state from the specific field of the pool | |
2709 | # | |
2710 | function get_device_state #pool disk field("", "spares","logs") | |
2711 | { | |
2712 | typeset pool=$1 | |
2713 | typeset disk=${2#$DEV_DSKDIR/} | |
2714 | typeset field=${3:-$pool} | |
2715 | ||
270d0a5a AZ |
2716 | zpool status -v "$pool" 2>/dev/null | \ |
2717 | awk -v device=$disk -v pool=$pool -v field=$field \ | |
6bb24f4d BB |
2718 | 'BEGIN {startconfig=0; startfield=0; } |
2719 | /config:/ {startconfig=1} | |
2720 | (startconfig==1) && ($1==field) {startfield=1; next;} | |
2721 | (startfield==1) && ($1==device) {print $2; exit;} | |
2722 | (startfield==1) && | |
270d0a5a | 2723 | ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}' |
6bb24f4d BB |
2724 | } |
2725 | ||
6bb24f4d BB |
2726 | # |
2727 | # get the root filesystem name if it's zfsroot system. | |
2728 | # | |
2729 | # return: root filesystem name | |
2730 | function get_rootfs | |
2731 | { | |
2732 | typeset rootfs="" | |
8aab1218 | 2733 | |
7839c4b5 MM |
2734 | if is_freebsd; then |
2735 | rootfs=$(mount -p | awk '$2 == "/" && $3 == "zfs" {print $1}') | |
2736 | elif ! is_linux; then | |
75746e9a | 2737 | rootfs=$(awk '$2 == "/" && $3 == "zfs" {print $1}' \ |
8aab1218 TS |
2738 | /etc/mnttab) |
2739 | fi | |
6bb24f4d BB |
2740 | if [[ -z "$rootfs" ]]; then |
2741 | log_fail "Can not get rootfs" | |
2742 | fi | |
23914a3b | 2743 | if datasetexists $rootfs; then |
c1d9abf9 | 2744 | echo $rootfs |
6bb24f4d BB |
2745 | else |
2746 | log_fail "This is not a zfsroot system." | |
2747 | fi | |
2748 | } | |
2749 | ||
2750 | # | |
2751 | # get the rootfs's pool name | |
2752 | # return: | |
2753 | # rootpool name | |
2754 | # | |
2755 | function get_rootpool | |
2756 | { | |
bd328a58 AZ |
2757 | typeset rootfs=$(get_rootfs) |
2758 | echo ${rootfs%%/*} | |
6bb24f4d BB |
2759 | } |
2760 | ||
2761 | # | |
2762 | # To verify if the require numbers of disks is given | |
2763 | # | |
2764 | function verify_disk_count | |
2765 | { | |
2766 | typeset -i min=${2:-1} | |
2767 | ||
bd328a58 | 2768 | typeset -i count=$(echo "$1" | wc -w) |
6bb24f4d BB |
2769 | |
2770 | if ((count < min)); then | |
2771 | log_untested "A minimum of $min disks is required to run." \ | |
2772 | " You specified $count disk(s)" | |
2773 | fi | |
2774 | } | |
2775 | ||
2776 | function ds_is_volume | |
2777 | { | |
2778 | typeset type=$(get_prop type $1) | |
caccfc87 | 2779 | [ $type = "volume" ] |
6bb24f4d BB |
2780 | } |
2781 | ||
2782 | function ds_is_filesystem | |
2783 | { | |
2784 | typeset type=$(get_prop type $1) | |
caccfc87 | 2785 | [ $type = "filesystem" ] |
6bb24f4d BB |
2786 | } |
2787 | ||
6bb24f4d BB |
2788 | # |
2789 | # Check if Trusted Extensions are installed and enabled | |
2790 | # | |
2791 | function is_te_enabled | |
2792 | { | |
bd328a58 | 2793 | svcs -H -o state labeld 2>/dev/null | grep -q "enabled" |
6bb24f4d BB |
2794 | } |
2795 | ||
6f73d021 TH |
2796 | # Return the number of CPUs (cross-platform) |
2797 | function get_num_cpus | |
2798 | { | |
2799 | if is_linux ; then | |
2800 | grep -c '^processor' /proc/cpuinfo | |
2801 | elif is_freebsd; then | |
2802 | sysctl -n kern.smp.cpus | |
2803 | else | |
2804 | psrinfo | wc -l | |
2805 | fi | |
2806 | } | |
2807 | ||
6bb24f4d BB |
2808 | # Utility function to determine if a system has multiple cpus. |
2809 | function is_mp | |
2810 | { | |
6f73d021 | 2811 | [[ $(get_num_cpus) -gt 1 ]] |
6bb24f4d BB |
2812 | } |
2813 | ||
2814 | function get_cpu_freq | |
2815 | { | |
2816 | if is_linux; then | |
c1d9abf9 | 2817 | lscpu | awk '/CPU MHz/ { print $3 }' |
7839c4b5 | 2818 | elif is_freebsd; then |
9be70c37 | 2819 | sysctl -n hw.clockrate |
6bb24f4d | 2820 | else |
c1d9abf9 | 2821 | psrinfo -v 0 | awk '/processor operates at/ {print $6}' |
6bb24f4d BB |
2822 | fi |
2823 | } | |
2824 | ||
2825 | # Run the given command as the user provided. | |
2826 | function user_run | |
2827 | { | |
2828 | typeset user=$1 | |
2829 | shift | |
2830 | ||
e0b53a5d RM |
2831 | log_note "user: $user" |
2832 | log_note "cmd: $*" | |
2833 | ||
2834 | typeset out=$TEST_BASE_DIR/out | |
2835 | typeset err=$TEST_BASE_DIR/err | |
2836 | ||
2837 | sudo -Eu $user env PATH="$PATH" ksh <<<"$*" >$out 2>$err | |
2838 | typeset res=$? | |
2839 | log_note "out: $(<$out)" | |
2840 | log_note "err: $(<$err)" | |
2841 | return $res | |
6bb24f4d BB |
2842 | } |
2843 | ||
2844 | # | |
2845 | # Check if the pool contains the specified vdevs | |
2846 | # | |
2847 | # $1 pool | |
2848 | # $2..n <vdev> ... | |
2849 | # | |
2850 | # Return 0 if the vdevs are contained in the pool, 1 if any of the specified | |
2851 | # vdevs is not in the pool, and 2 if pool name is missing. | |
2852 | # | |
2853 | function vdevs_in_pool | |
2854 | { | |
2855 | typeset pool=$1 | |
2856 | typeset vdev | |
2857 | ||
2858 | if [[ -z $pool ]]; then | |
2859 | log_note "Missing pool name." | |
2860 | return 2 | |
2861 | fi | |
2862 | ||
2863 | shift | |
2864 | ||
7c9a4292 BB |
2865 | # We could use 'zpool list' to only get the vdevs of the pool but we |
2866 | # can't reference a mirror/raidz vdev using its ID (i.e mirror-0), | |
2867 | # therefore we use the 'zpool status' output. | |
c1d9abf9 | 2868 | typeset tmpfile=$(mktemp) |
7c9a4292 | 2869 | zpool status -v "$pool" | grep -A 1000 "config:" >$tmpfile |
23914a3b | 2870 | for vdev in "$@"; do |
964d4180 | 2871 | grep -wq ${vdev##*/} $tmpfile || return 1 |
6bb24f4d BB |
2872 | done |
2873 | ||
c1d9abf9 | 2874 | rm -f $tmpfile |
23914a3b | 2875 | return 0 |
6bb24f4d BB |
2876 | } |
2877 | ||
679d73e9 JWK |
2878 | function get_max |
2879 | { | |
2880 | typeset -l i max=$1 | |
2881 | shift | |
2882 | ||
2883 | for i in "$@"; do | |
9be70c37 | 2884 | max=$((max > i ? max : i)) |
679d73e9 JWK |
2885 | done |
2886 | ||
2887 | echo $max | |
2888 | } | |
2889 | ||
a7004725 DK |
2890 | # Write data that can be compressed into a directory |
2891 | function write_compressible | |
2892 | { | |
2893 | typeset dir=$1 | |
2894 | typeset megs=$2 | |
2895 | typeset nfiles=${3:-1} | |
2896 | typeset bs=${4:-1024k} | |
2897 | typeset fname=${5:-file} | |
2898 | ||
2899 | [[ -d $dir ]] || log_fail "No directory: $dir" | |
2900 | ||
2901 | # Under Linux fio is not currently used since its behavior can | |
2902 | # differ significantly across versions. This includes missing | |
2903 | # command line options and cases where the --buffer_compress_* | |
2904 | # options fail to behave as expected. | |
2905 | if is_linux; then | |
2906 | typeset file_bytes=$(to_bytes $megs) | |
2907 | typeset bs_bytes=4096 | |
2908 | typeset blocks=$(($file_bytes / $bs_bytes)) | |
2909 | ||
2910 | for (( i = 0; i < $nfiles; i++ )); do | |
2911 | truncate -s $file_bytes $dir/$fname.$i | |
2912 | ||
2913 | # Write every third block to get 66% compression. | |
2914 | for (( j = 0; j < $blocks; j += 3 )); do | |
2915 | dd if=/dev/urandom of=$dir/$fname.$i \ | |
2916 | seek=$j bs=$bs_bytes count=1 \ | |
2917 | conv=notrunc >/dev/null 2>&1 | |
2918 | done | |
2919 | done | |
2920 | else | |
2d9da5e1 | 2921 | command -v fio > /dev/null || log_unsupported "fio missing" |
bd328a58 | 2922 | log_must eval fio \ |
a7004725 DK |
2923 | --name=job \ |
2924 | --fallocate=0 \ | |
2925 | --minimal \ | |
2926 | --randrepeat=0 \ | |
2927 | --buffer_compress_percentage=66 \ | |
2928 | --buffer_compress_chunk=4096 \ | |
bd328a58 AZ |
2929 | --directory="$dir" \ |
2930 | --numjobs="$nfiles" \ | |
2931 | --nrfiles="$nfiles" \ | |
a7004725 | 2932 | --rw=write \ |
bd328a58 AZ |
2933 | --bs="$bs" \ |
2934 | --filesize="$megs" \ | |
2935 | "--filename_format='$fname.\$jobnum' >/dev/null" | |
a7004725 DK |
2936 | fi |
2937 | } | |
2938 | ||
2939 | function get_objnum | |
2940 | { | |
2941 | typeset pathname=$1 | |
2942 | typeset objnum | |
2943 | ||
2944 | [[ -e $pathname ]] || log_fail "No such file or directory: $pathname" | |
7839c4b5 MM |
2945 | if is_freebsd; then |
2946 | objnum=$(stat -f "%i" $pathname) | |
2947 | else | |
2948 | objnum=$(stat -c %i $pathname) | |
2949 | fi | |
a7004725 DK |
2950 | echo $objnum |
2951 | } | |
2952 | ||
1de321e6 | 2953 | # |
bec1067d | 2954 | # Sync data to the pool |
1de321e6 JX |
2955 | # |
2956 | # $1 pool name | |
bec1067d | 2957 | # $2 boolean to force uberblock (and config including zpool cache file) update |
1de321e6 | 2958 | # |
bec1067d | 2959 | function sync_pool #pool <force> |
1de321e6 JX |
2960 | { |
2961 | typeset pool=${1:-$TESTPOOL} | |
bec1067d | 2962 | typeset force=${2:-false} |
1de321e6 | 2963 | |
bec1067d AP |
2964 | if [[ $force == true ]]; then |
2965 | log_must zpool sync -f $pool | |
2966 | else | |
2967 | log_must zpool sync $pool | |
2968 | fi | |
2969 | ||
2970 | return 0 | |
1de321e6 | 2971 | } |
d834b9ce | 2972 | |
7454275a AJ |
2973 | # |
2974 | # Sync all pools | |
2975 | # | |
2976 | # $1 boolean to force uberblock (and config including zpool cache file) update | |
2977 | # | |
2978 | function sync_all_pools #<force> | |
2979 | { | |
2980 | typeset force=${1:-false} | |
2981 | ||
2982 | if [[ $force == true ]]; then | |
2983 | log_must zpool sync -f | |
2984 | else | |
2985 | log_must zpool sync | |
2986 | fi | |
2987 | ||
2988 | return 0 | |
2989 | } | |
2990 | ||
d834b9ce GM |
2991 | # |
2992 | # Wait for zpool 'freeing' property drops to zero. | |
2993 | # | |
2994 | # $1 pool name | |
2995 | # | |
2996 | function wait_freeing #pool | |
2997 | { | |
2998 | typeset pool=${1:-$TESTPOOL} | |
2999 | while true; do | |
c1d9abf9 JWK |
3000 | [[ "0" == "$(zpool list -Ho freeing $pool)" ]] && break |
3001 | log_must sleep 1 | |
d834b9ce GM |
3002 | done |
3003 | } | |
7a4500a1 | 3004 | |
dddef7d6 | 3005 | # |
3006 | # Wait for every device replace operation to complete | |
3007 | # | |
3008 | # $1 pool name | |
c8b3dda1 | 3009 | # $2 timeout |
dddef7d6 | 3010 | # |
c8b3dda1 | 3011 | function wait_replacing #pool timeout |
dddef7d6 | 3012 | { |
c8b3dda1 | 3013 | typeset timeout=${2:-300} |
dddef7d6 | 3014 | typeset pool=${1:-$TESTPOOL} |
c8b3dda1 BB |
3015 | for (( timer = 0; timer < $timeout; timer++ )); do |
3016 | is_pool_replacing $pool || break; | |
3017 | sleep 1; | |
dddef7d6 | 3018 | done |
3019 | } | |
3020 | ||
bf95a000 TH |
3021 | # Wait for a pool to be scrubbed |
3022 | # | |
3023 | # $1 pool name | |
56fa4aa9 | 3024 | # $2 timeout |
bf95a000 | 3025 | # |
56fa4aa9 | 3026 | function wait_scrubbed #pool timeout |
bf95a000 | 3027 | { |
56fa4aa9 RE |
3028 | typeset timeout=${2:-300} |
3029 | typeset pool=${1:-$TESTPOOL} | |
3030 | for (( timer = 0; timer < $timeout; timer++ )); do | |
3031 | is_pool_scrubbed $pool && break; | |
3032 | sleep 1; | |
3033 | done | |
bf95a000 TH |
3034 | } |
3035 | ||
639b1894 TH |
3036 | # Backup the zed.rc in our test directory so that we can edit it for our test. |
3037 | # | |
3038 | # Returns: Backup file name. You will need to pass this to zed_rc_restore(). | |
3039 | function zed_rc_backup | |
3040 | { | |
3041 | zedrc_backup="$(mktemp)" | |
3042 | cp $ZEDLET_DIR/zed.rc $zedrc_backup | |
3043 | echo $zedrc_backup | |
3044 | } | |
3045 | ||
3046 | function zed_rc_restore | |
3047 | { | |
3048 | mv $1 $ZEDLET_DIR/zed.rc | |
3049 | } | |
3050 | ||
95401cb6 BB |
3051 | # |
3052 | # Setup custom environment for the ZED. | |
3053 | # | |
bf95a000 | 3054 | # $@ Optional list of zedlets to run under zed. |
95401cb6 BB |
3055 | function zed_setup |
3056 | { | |
3057 | if ! is_linux; then | |
5c9f744b | 3058 | log_unsupported "No zed on $UNAME" |
95401cb6 BB |
3059 | fi |
3060 | ||
3061 | if [[ ! -d $ZEDLET_DIR ]]; then | |
3062 | log_must mkdir $ZEDLET_DIR | |
3063 | fi | |
3064 | ||
3065 | if [[ ! -e $VDEVID_CONF ]]; then | |
3066 | log_must touch $VDEVID_CONF | |
3067 | fi | |
3068 | ||
3069 | if [[ -e $VDEVID_CONF_ETC ]]; then | |
3070 | log_fail "Must not have $VDEVID_CONF_ETC file present on system" | |
3071 | fi | |
bf95a000 | 3072 | EXTRA_ZEDLETS=$@ |
95401cb6 BB |
3073 | |
3074 | # Create a symlink for /etc/zfs/vdev_id.conf file. | |
3075 | log_must ln -s $VDEVID_CONF $VDEVID_CONF_ETC | |
3076 | ||
3077 | # Setup minimal ZED configuration. Individual test cases should | |
3078 | # add additional ZEDLETs as needed for their specific test. | |
3f03fc8d BB |
3079 | log_must cp ${ZEDLET_ETC_DIR}/zed.rc $ZEDLET_DIR |
3080 | log_must cp ${ZEDLET_ETC_DIR}/zed-functions.sh $ZEDLET_DIR | |
95401cb6 | 3081 | |
bf95a000 TH |
3082 | # Scripts must only be user writable. |
3083 | if [[ -n "$EXTRA_ZEDLETS" ]] ; then | |
3084 | saved_umask=$(umask) | |
3085 | log_must umask 0022 | |
3086 | for i in $EXTRA_ZEDLETS ; do | |
3087 | log_must cp ${ZEDLET_LIBEXEC_DIR}/$i $ZEDLET_DIR | |
3088 | done | |
3089 | log_must umask $saved_umask | |
3090 | fi | |
3091 | ||
3f03fc8d BB |
3092 | # Customize the zed.rc file to enable the full debug log. |
3093 | log_must sed -i '/\#ZED_DEBUG_LOG=.*/d' $ZEDLET_DIR/zed.rc | |
d5e024cb | 3094 | echo "ZED_DEBUG_LOG=$ZED_DEBUG_LOG" >>$ZEDLET_DIR/zed.rc |
3f03fc8d | 3095 | |
95401cb6 BB |
3096 | } |
3097 | ||
3098 | # | |
3099 | # Cleanup custom ZED environment. | |
3100 | # | |
bf95a000 | 3101 | # $@ Optional list of zedlets to remove from our test zed.d directory. |
95401cb6 BB |
3102 | function zed_cleanup |
3103 | { | |
3104 | if ! is_linux; then | |
3105 | return | |
3106 | fi | |
3107 | ||
bd328a58 AZ |
3108 | for extra_zedlet; do |
3109 | log_must rm -f ${ZEDLET_DIR}/$extra_zedlet | |
3110 | done | |
3111 | log_must rm -fd ${ZEDLET_DIR}/zed.rc ${ZEDLET_DIR}/zed-functions.sh ${ZEDLET_DIR}/all-syslog.sh ${ZEDLET_DIR}/all-debug.sh ${ZEDLET_DIR}/state \ | |
3112 | $ZED_LOG $ZED_DEBUG_LOG $VDEVID_CONF_ETC $VDEVID_CONF \ | |
3113 | $ZEDLET_DIR | |
95401cb6 BB |
3114 | } |
3115 | ||
56fa4aa9 RE |
3116 | # |
3117 | # Check if ZED is currently running; if so, returns PIDs | |
3118 | # | |
3119 | function zed_check | |
3120 | { | |
3121 | if ! is_linux; then | |
3122 | return | |
3123 | fi | |
3124 | zedpids="$(pgrep -x zed)" | |
56fa4aa9 | 3125 | zedpids2="$(pgrep -x lt-zed)" |
56fa4aa9 RE |
3126 | echo ${zedpids} ${zedpids2} |
3127 | } | |
3128 | ||
7a4500a1 SV |
3129 | # |
3130 | # Check if ZED is currently running, if not start ZED. | |
3131 | # | |
3132 | function zed_start | |
3133 | { | |
95401cb6 BB |
3134 | if ! is_linux; then |
3135 | return | |
3136 | fi | |
7a4500a1 | 3137 | |
95401cb6 BB |
3138 | # ZEDLET_DIR=/var/tmp/zed |
3139 | if [[ ! -d $ZEDLET_DIR ]]; then | |
3140 | log_must mkdir $ZEDLET_DIR | |
3141 | fi | |
7a4500a1 | 3142 | |
95401cb6 | 3143 | # Verify the ZED is not already running. |
56fa4aa9 RE |
3144 | zedpids=$(zed_check) |
3145 | if [ -n "$zedpids" ]; then | |
3146 | # We never, ever, really want it to just keep going if zed | |
3147 | # is already running - usually this implies our test cases | |
3148 | # will break very strangely because whatever we wanted to | |
3149 | # configure zed for won't be listening to our changes in the | |
3150 | # tmpdir | |
3151 | log_fail "ZED already running - ${zedpids}" | |
03431390 OF |
3152 | else |
3153 | log_note "Starting ZED" | |
3154 | # run ZED in the background and redirect foreground logging | |
3155 | # output to $ZED_LOG. | |
3156 | log_must truncate -s 0 $ZED_DEBUG_LOG | |
73218f41 AZ |
3157 | log_must eval "zed -vF -d $ZEDLET_DIR -P $PATH" \ |
3158 | "-s $ZEDLET_DIR/state -j 1 2>$ZED_LOG &" | |
7a4500a1 | 3159 | fi |
95401cb6 | 3160 | |
3f03fc8d | 3161 | return 0 |
7a4500a1 SV |
3162 | } |
3163 | ||
3164 | # | |
3165 | # Kill ZED process | |
3166 | # | |
3167 | function zed_stop | |
3168 | { | |
95401cb6 | 3169 | if ! is_linux; then |
56fa4aa9 | 3170 | return "" |
95401cb6 BB |
3171 | fi |
3172 | ||
3f03fc8d | 3173 | log_note "Stopping ZED" |
73218f41 | 3174 | while true; do |
56fa4aa9 RE |
3175 | zedpids=$(zed_check) |
3176 | [ ! -n "$zedpids" ] && break | |
73218f41 AZ |
3177 | |
3178 | log_must kill $zedpids | |
3179 | sleep 1 | |
3180 | done | |
3f03fc8d | 3181 | return 0 |
7a4500a1 | 3182 | } |
8c54ddd3 | 3183 | |
4e9b1569 | 3184 | # |
3185 | # Drain all zevents | |
3186 | # | |
3187 | function zed_events_drain | |
3188 | { | |
3189 | while [ $(zpool events -H | wc -l) -ne 0 ]; do | |
3190 | sleep 1 | |
3191 | zpool events -c >/dev/null | |
3192 | done | |
3193 | } | |
3194 | ||
639b1894 TH |
3195 | # Set a variable in zed.rc to something, un-commenting it in the process. |
3196 | # | |
3197 | # $1 variable | |
3198 | # $2 value | |
3199 | function zed_rc_set | |
3200 | { | |
3201 | var="$1" | |
3202 | val="$2" | |
3203 | # Remove the line | |
3204 | cmd="'/$var/d'" | |
3205 | eval sed -i $cmd $ZEDLET_DIR/zed.rc | |
3206 | ||
3207 | # Add it at the end | |
3208 | echo "$var=$val" >> $ZEDLET_DIR/zed.rc | |
3209 | } | |
3210 | ||
3211 | ||
8c54ddd3 BB |
3212 | # |
3213 | # Check is provided device is being active used as a swap device. | |
3214 | # | |
3215 | function is_swap_inuse | |
3216 | { | |
3217 | typeset device=$1 | |
3218 | ||
3219 | if [[ -z $device ]] ; then | |
3220 | log_note "No device specified." | |
3221 | return 1 | |
3222 | fi | |
3223 | ||
5c9f744b | 3224 | case "$UNAME" in |
62c5ccdf AZ |
3225 | Linux) |
3226 | swapon -s | grep -wq $(readlink -f $device) | |
3227 | ;; | |
3228 | FreeBSD) | |
3229 | swapctl -l | grep -wq $device | |
3230 | ;; | |
3231 | *) | |
3232 | swap -l | grep -wq $device | |
3233 | ;; | |
3234 | esac | |
8c54ddd3 BB |
3235 | } |
3236 | ||
3237 | # | |
3238 | # Setup a swap device using the provided device. | |
3239 | # | |
3240 | function swap_setup | |
3241 | { | |
3242 | typeset swapdev=$1 | |
3243 | ||
5c9f744b | 3244 | case "$UNAME" in |
62c5ccdf | 3245 | Linux) |
c7a7601c | 3246 | log_must eval "mkswap $swapdev > /dev/null 2>&1" |
8c54ddd3 | 3247 | log_must swapon $swapdev |
62c5ccdf AZ |
3248 | ;; |
3249 | FreeBSD) | |
7839c4b5 | 3250 | log_must swapctl -a $swapdev |
62c5ccdf AZ |
3251 | ;; |
3252 | *) | |
3253 | log_must swap -a $swapdev | |
3254 | ;; | |
3255 | esac | |
8c54ddd3 BB |
3256 | |
3257 | return 0 | |
3258 | } | |
3259 | ||
3260 | # | |
3261 | # Cleanup a swap device on the provided device. | |
3262 | # | |
3263 | function swap_cleanup | |
3264 | { | |
3265 | typeset swapdev=$1 | |
3266 | ||
3267 | if is_swap_inuse $swapdev; then | |
3268 | if is_linux; then | |
3269 | log_must swapoff $swapdev | |
7839c4b5 MM |
3270 | elif is_freebsd; then |
3271 | log_must swapoff $swapdev | |
8c54ddd3 BB |
3272 | else |
3273 | log_must swap -d $swapdev | |
3274 | fi | |
3275 | fi | |
3276 | ||
3277 | return 0 | |
3278 | } | |
379ca9cf OF |
3279 | |
3280 | # | |
3281 | # Set a global system tunable (64-bit value) | |
3282 | # | |
2476f103 | 3283 | # $1 tunable name (use a NAME defined in tunables.cfg) |
379ca9cf OF |
3284 | # $2 tunable values |
3285 | # | |
3286 | function set_tunable64 | |
3287 | { | |
3288 | set_tunable_impl "$1" "$2" Z | |
3289 | } | |
3290 | ||
3291 | # | |
3292 | # Set a global system tunable (32-bit value) | |
3293 | # | |
2476f103 | 3294 | # $1 tunable name (use a NAME defined in tunables.cfg) |
379ca9cf OF |
3295 | # $2 tunable values |
3296 | # | |
3297 | function set_tunable32 | |
3298 | { | |
3299 | set_tunable_impl "$1" "$2" W | |
3300 | } | |
3301 | ||
3302 | function set_tunable_impl | |
3303 | { | |
2476f103 | 3304 | typeset name="$1" |
379ca9cf OF |
3305 | typeset value="$2" |
3306 | typeset mdb_cmd="$3" | |
379ca9cf | 3307 | |
2476f103 RM |
3308 | eval "typeset tunable=\$$name" |
3309 | case "$tunable" in | |
3310 | UNSUPPORTED) | |
5c9f744b | 3311 | log_unsupported "Tunable '$name' is unsupported on $UNAME" |
2476f103 RM |
3312 | ;; |
3313 | "") | |
3314 | log_fail "Tunable '$name' must be added to tunables.cfg" | |
3315 | ;; | |
3316 | *) | |
3317 | ;; | |
3318 | esac | |
3319 | ||
379ca9cf OF |
3320 | [[ -z "$value" ]] && return 1 |
3321 | [[ -z "$mdb_cmd" ]] && return 1 | |
3322 | ||
5c9f744b | 3323 | case "$UNAME" in |
379ca9cf | 3324 | Linux) |
ad9e7676 | 3325 | typeset zfs_tunables="/sys/module/zfs/parameters" |
23914a3b | 3326 | echo "$value" >"$zfs_tunables/$tunable" |
379ca9cf | 3327 | ;; |
7839c4b5 MM |
3328 | FreeBSD) |
3329 | sysctl vfs.zfs.$tunable=$value | |
7839c4b5 | 3330 | ;; |
379ca9cf | 3331 | SunOS) |
379ca9cf | 3332 | echo "${tunable}/${mdb_cmd}0t${value}" | mdb -kw |
379ca9cf OF |
3333 | ;; |
3334 | esac | |
3335 | } | |
3336 | ||
03e9caae RE |
3337 | function save_tunable |
3338 | { | |
3339 | [[ ! -d $TEST_BASE_DIR ]] && return 1 | |
3340 | [[ -e $TEST_BASE_DIR/tunable-$1 ]] && return 2 | |
3341 | echo "$(get_tunable """$1""")" > "$TEST_BASE_DIR"/tunable-"$1" | |
3342 | } | |
3343 | ||
3344 | function restore_tunable | |
3345 | { | |
3346 | [[ ! -e $TEST_BASE_DIR/tunable-$1 ]] && return 1 | |
3347 | val="$(cat $TEST_BASE_DIR/tunable-"""$1""")" | |
3348 | set_tunable64 "$1" "$val" | |
3349 | rm $TEST_BASE_DIR/tunable-$1 | |
3350 | } | |
3351 | ||
379ca9cf OF |
3352 | # |
3353 | # Get a global system tunable | |
3354 | # | |
2476f103 | 3355 | # $1 tunable name (use a NAME defined in tunables.cfg) |
379ca9cf OF |
3356 | # |
3357 | function get_tunable | |
3358 | { | |
3359 | get_tunable_impl "$1" | |
3360 | } | |
3361 | ||
3362 | function get_tunable_impl | |
3363 | { | |
2476f103 | 3364 | typeset name="$1" |
379ca9cf | 3365 | typeset module="${2:-zfs}" |
6f73d021 | 3366 | typeset check_only="$3" |
379ca9cf | 3367 | |
2476f103 RM |
3368 | eval "typeset tunable=\$$name" |
3369 | case "$tunable" in | |
3370 | UNSUPPORTED) | |
6f73d021 TH |
3371 | if [ -z "$check_only" ] ; then |
3372 | log_unsupported "Tunable '$name' is unsupported on $UNAME" | |
3373 | else | |
3374 | return 1 | |
3375 | fi | |
2476f103 RM |
3376 | ;; |
3377 | "") | |
6f73d021 TH |
3378 | if [ -z "$check_only" ] ; then |
3379 | log_fail "Tunable '$name' must be added to tunables.cfg" | |
3380 | else | |
3381 | return 1 | |
3382 | fi | |
2476f103 RM |
3383 | ;; |
3384 | *) | |
3385 | ;; | |
3386 | esac | |
379ca9cf | 3387 | |
5c9f744b | 3388 | case "$UNAME" in |
379ca9cf OF |
3389 | Linux) |
3390 | typeset zfs_tunables="/sys/module/$module/parameters" | |
379ca9cf | 3391 | cat $zfs_tunables/$tunable |
379ca9cf | 3392 | ;; |
7839c4b5 MM |
3393 | FreeBSD) |
3394 | sysctl -n vfs.zfs.$tunable | |
3395 | ;; | |
379ca9cf OF |
3396 | SunOS) |
3397 | [[ "$module" -eq "zfs" ]] || return 1 | |
3398 | ;; | |
3399 | esac | |
d2734cce | 3400 | } |
240c015a | 3401 | |
6f73d021 TH |
3402 | # Does a tunable exist? |
3403 | # | |
3404 | # $1: Tunable name | |
3405 | function tunable_exists | |
3406 | { | |
3407 | get_tunable_impl $1 "zfs" 1 | |
3408 | } | |
3409 | ||
240c015a RM |
3410 | # |
3411 | # Compute MD5 digest for given file or stdin if no file given. | |
3412 | # Note: file path must not contain spaces | |
3413 | # | |
3414 | function md5digest | |
3415 | { | |
3416 | typeset file=$1 | |
3417 | ||
5c9f744b | 3418 | case "$UNAME" in |
7839c4b5 MM |
3419 | FreeBSD) |
3420 | md5 -q $file | |
3421 | ;; | |
3422 | *) | |
75746e9a AZ |
3423 | typeset sum _ |
3424 | read -r sum _ < <(md5sum -b $file) | |
3425 | echo $sum | |
7839c4b5 MM |
3426 | ;; |
3427 | esac | |
240c015a RM |
3428 | } |
3429 | ||
3430 | # | |
3431 | # Compute SHA256 digest for given file or stdin if no file given. | |
3432 | # Note: file path must not contain spaces | |
3433 | # | |
3434 | function sha256digest | |
3435 | { | |
3436 | typeset file=$1 | |
3437 | ||
5c9f744b | 3438 | case "$UNAME" in |
7839c4b5 MM |
3439 | FreeBSD) |
3440 | sha256 -q $file | |
3441 | ;; | |
3442 | *) | |
75746e9a AZ |
3443 | typeset sum _ |
3444 | read -r sum _ < <(sha256sum -b $file) | |
3445 | echo $sum | |
7839c4b5 MM |
3446 | ;; |
3447 | esac | |
3448 | } | |
3449 | ||
3450 | function new_fs #<args> | |
3451 | { | |
5c9f744b | 3452 | case "$UNAME" in |
7839c4b5 MM |
3453 | FreeBSD) |
3454 | newfs "$@" | |
3455 | ;; | |
3456 | *) | |
3457 | echo y | newfs -v "$@" | |
3458 | ;; | |
3459 | esac | |
3460 | } | |
3461 | ||
3462 | function stat_size #<path> | |
3463 | { | |
3464 | typeset path=$1 | |
3465 | ||
5c9f744b | 3466 | case "$UNAME" in |
7839c4b5 MM |
3467 | FreeBSD) |
3468 | stat -f %z "$path" | |
3469 | ;; | |
3470 | *) | |
3471 | stat -c %s "$path" | |
3472 | ;; | |
3473 | esac | |
240c015a | 3474 | } |
9fb2771a | 3475 | |
9681de46 US |
3476 | function stat_mtime #<path> |
3477 | { | |
3478 | typeset path=$1 | |
3479 | ||
3480 | case "$UNAME" in | |
3481 | FreeBSD) | |
3482 | stat -f %m "$path" | |
3483 | ;; | |
3484 | *) | |
3485 | stat -c %Y "$path" | |
3486 | ;; | |
3487 | esac | |
3488 | } | |
3489 | ||
8ae86e2e RM |
3490 | function stat_ctime #<path> |
3491 | { | |
3492 | typeset path=$1 | |
3493 | ||
5c9f744b | 3494 | case "$UNAME" in |
8ae86e2e RM |
3495 | FreeBSD) |
3496 | stat -f %c "$path" | |
3497 | ;; | |
3498 | *) | |
3499 | stat -c %Z "$path" | |
3500 | ;; | |
3501 | esac | |
3502 | } | |
3503 | ||
3504 | function stat_crtime #<path> | |
3505 | { | |
3506 | typeset path=$1 | |
3507 | ||
5c9f744b | 3508 | case "$UNAME" in |
8ae86e2e RM |
3509 | FreeBSD) |
3510 | stat -f %B "$path" | |
3511 | ;; | |
3512 | *) | |
3513 | stat -c %W "$path" | |
3514 | ;; | |
3515 | esac | |
3516 | } | |
3517 | ||
3fa5266d RM |
3518 | function stat_generation #<path> |
3519 | { | |
3520 | typeset path=$1 | |
3521 | ||
5c9f744b | 3522 | case "$UNAME" in |
3fa5266d RM |
3523 | Linux) |
3524 | getversion "${path}" | |
3525 | ;; | |
3526 | *) | |
3527 | stat -f %v "${path}" | |
3528 | ;; | |
3529 | esac | |
3530 | } | |
3531 | ||
9fb2771a TH |
3532 | # Run a command as if it was being run in a TTY. |
3533 | # | |
3534 | # Usage: | |
3535 | # | |
3536 | # faketty command | |
3537 | # | |
3538 | function faketty | |
3539 | { | |
3540 | if is_freebsd; then | |
834f274f | 3541 | script -q /dev/null env "$@" |
9fb2771a TH |
3542 | else |
3543 | script --return --quiet -c "$*" /dev/null | |
3544 | fi | |
3545 | } | |
90ae4873 RM |
3546 | |
3547 | # | |
3548 | # Produce a random permutation of the integers in a given range (inclusive). | |
3549 | # | |
3550 | function range_shuffle # begin end | |
3551 | { | |
3552 | typeset -i begin=$1 | |
3553 | typeset -i end=$2 | |
3554 | ||
7a298ae9 | 3555 | seq ${begin} ${end} | sort -R |
90ae4873 | 3556 | } |
6e1c594d RM |
3557 | |
3558 | # | |
3559 | # Cross-platform xattr helpers | |
3560 | # | |
3561 | ||
3562 | function get_xattr # name path | |
3563 | { | |
3564 | typeset name=$1 | |
3565 | typeset path=$2 | |
3566 | ||
5c9f744b | 3567 | case "$UNAME" in |
6e1c594d RM |
3568 | FreeBSD) |
3569 | getextattr -qq user "${name}" "${path}" | |
3570 | ;; | |
3571 | *) | |
3572 | attr -qg "${name}" "${path}" | |
3573 | ;; | |
3574 | esac | |
3575 | } | |
3576 | ||
3577 | function set_xattr # name value path | |
3578 | { | |
3579 | typeset name=$1 | |
3580 | typeset value=$2 | |
3581 | typeset path=$3 | |
3582 | ||
5c9f744b | 3583 | case "$UNAME" in |
6e1c594d RM |
3584 | FreeBSD) |
3585 | setextattr user "${name}" "${value}" "${path}" | |
3586 | ;; | |
3587 | *) | |
3588 | attr -qs "${name}" -V "${value}" "${path}" | |
3589 | ;; | |
3590 | esac | |
3591 | } | |
3592 | ||
3593 | function set_xattr_stdin # name value | |
3594 | { | |
3595 | typeset name=$1 | |
3596 | typeset path=$2 | |
3597 | ||
5c9f744b | 3598 | case "$UNAME" in |
6e1c594d RM |
3599 | FreeBSD) |
3600 | setextattr -i user "${name}" "${path}" | |
3601 | ;; | |
3602 | *) | |
3603 | attr -qs "${name}" "${path}" | |
3604 | ;; | |
3605 | esac | |
3606 | } | |
3607 | ||
3608 | function rm_xattr # name path | |
3609 | { | |
3610 | typeset name=$1 | |
3611 | typeset path=$2 | |
3612 | ||
5c9f744b | 3613 | case "$UNAME" in |
6e1c594d RM |
3614 | FreeBSD) |
3615 | rmextattr -q user "${name}" "${path}" | |
3616 | ;; | |
3617 | *) | |
3618 | attr -qr "${name}" "${path}" | |
3619 | ;; | |
3620 | esac | |
3621 | } | |
3622 | ||
3623 | function ls_xattr # path | |
3624 | { | |
3625 | typeset path=$1 | |
3626 | ||
5c9f744b | 3627 | case "$UNAME" in |
6e1c594d RM |
3628 | FreeBSD) |
3629 | lsextattr -qq user "${path}" | |
3630 | ;; | |
3631 | *) | |
3632 | attr -ql "${path}" | |
3633 | ;; | |
3634 | esac | |
3635 | } | |
37c22948 | 3636 | |
73989f4b RM |
3637 | function kstat # stat flags? |
3638 | { | |
3639 | typeset stat=$1 | |
3640 | typeset flags=${2-"-n"} | |
3641 | ||
5c9f744b | 3642 | case "$UNAME" in |
73989f4b RM |
3643 | FreeBSD) |
3644 | sysctl $flags kstat.zfs.misc.$stat | |
3645 | ;; | |
3646 | Linux) | |
75746e9a | 3647 | cat "/proc/spl/kstat/zfs/$stat" 2>/dev/null |
73989f4b RM |
3648 | ;; |
3649 | *) | |
3650 | false | |
3651 | ;; | |
3652 | esac | |
3653 | } | |
3654 | ||
37c22948 GA |
3655 | function get_arcstat # stat |
3656 | { | |
9f0a21e6 MM |
3657 | typeset stat=$1 |
3658 | ||
5c9f744b | 3659 | case "$UNAME" in |
9f0a21e6 | 3660 | FreeBSD) |
73989f4b | 3661 | kstat arcstats.$stat |
9f0a21e6 MM |
3662 | ;; |
3663 | Linux) | |
75746e9a | 3664 | kstat arcstats | awk "/$stat/"' { print $3 }' |
9f0a21e6 MM |
3665 | ;; |
3666 | *) | |
3667 | false | |
3668 | ;; | |
3669 | esac | |
37c22948 | 3670 | } |
c15d36c6 | 3671 | |
c3cb57ae KHN |
3672 | function punch_hole # offset length file |
3673 | { | |
3674 | typeset offset=$1 | |
3675 | typeset length=$2 | |
3676 | typeset file=$3 | |
3677 | ||
5c9f744b | 3678 | case "$UNAME" in |
c3cb57ae KHN |
3679 | FreeBSD) |
3680 | truncate -d -o $offset -l $length "$file" | |
3681 | ;; | |
3682 | Linux) | |
3683 | fallocate --punch-hole --offset $offset --length $length "$file" | |
3684 | ;; | |
3685 | *) | |
3686 | false | |
3687 | ;; | |
3688 | esac | |
3689 | } | |
3690 | ||
b657f2c5 RE |
3691 | function zero_range # offset length file |
3692 | { | |
3693 | typeset offset=$1 | |
3694 | typeset length=$2 | |
3695 | typeset file=$3 | |
3696 | ||
3697 | case "$UNAME" in | |
3698 | Linux) | |
3699 | fallocate --zero-range --offset $offset --length $length "$file" | |
3700 | ;; | |
3701 | *) | |
3702 | false | |
3703 | ;; | |
3704 | esac | |
3705 | } | |
3706 | ||
a76e4e67 GA |
3707 | # |
3708 | # Wait for the specified arcstat to reach non-zero quiescence. | |
3709 | # If echo is 1 echo the value after reaching quiescence, otherwise | |
3710 | # if echo is 0 print the arcstat we are waiting on. | |
3711 | # | |
3712 | function arcstat_quiescence # stat echo | |
3713 | { | |
3714 | typeset stat=$1 | |
3715 | typeset echo=$2 | |
3716 | typeset do_once=true | |
3717 | ||
3718 | if [[ $echo -eq 0 ]]; then | |
3719 | echo "Waiting for arcstat $1 quiescence." | |
3720 | fi | |
3721 | ||
3722 | while $do_once || [ $stat1 -ne $stat2 ] || [ $stat2 -eq 0 ]; do | |
3723 | typeset stat1=$(get_arcstat $stat) | |
10e36e17 | 3724 | sleep 0.5 |
a76e4e67 GA |
3725 | typeset stat2=$(get_arcstat $stat) |
3726 | do_once=false | |
3727 | done | |
3728 | ||
3729 | if [[ $echo -eq 1 ]]; then | |
3730 | echo $stat2 | |
3731 | fi | |
3732 | } | |
3733 | ||
3734 | function arcstat_quiescence_noecho # stat | |
3735 | { | |
3736 | typeset stat=$1 | |
3737 | arcstat_quiescence $stat 0 | |
3738 | } | |
3739 | ||
3740 | function arcstat_quiescence_echo # stat | |
3741 | { | |
3742 | typeset stat=$1 | |
3743 | arcstat_quiescence $stat 1 | |
3744 | } | |
3745 | ||
c15d36c6 GW |
3746 | # |
3747 | # Given an array of pids, wait until all processes | |
3748 | # have completed and check their return status. | |
3749 | # | |
3750 | function wait_for_children #children | |
3751 | { | |
3752 | rv=0 | |
3753 | children=("$@") | |
3754 | for child in "${children[@]}" | |
3755 | do | |
3756 | child_exit=0 | |
3757 | wait ${child} || child_exit=$? | |
3758 | if [ $child_exit -ne 0 ]; then | |
3759 | echo "child ${child} failed with ${child_exit}" | |
3760 | rv=1 | |
3761 | fi | |
3762 | done | |
3763 | return $rv | |
3764 | } | |
669683c4 AS |
3765 | |
3766 | # | |
3767 | # Compare two directory trees recursively in a manner similar to diff(1), but | |
3768 | # using rsync. If there are any discrepancies, a summary of the differences are | |
3769 | # output and a non-zero error is returned. | |
3770 | # | |
3771 | # If you're comparing a directory after a ZIL replay, you should set | |
3772 | # LIBTEST_DIFF_ZIL_REPLAY=1 or use replay_directory_diff which will cause | |
3773 | # directory_diff to ignore mtime changes (the ZIL replay won't fix up mtime | |
3774 | # information). | |
3775 | # | |
3776 | function directory_diff # dir_a dir_b | |
3777 | { | |
3778 | dir_a="$1" | |
3779 | dir_b="$2" | |
3780 | zil_replay="${LIBTEST_DIFF_ZIL_REPLAY:-0}" | |
3781 | ||
3782 | # If one of the directories doesn't exist, return 2. This is to match the | |
3783 | # semantics of diff. | |
3784 | if ! [ -d "$dir_a" -a -d "$dir_b" ]; then | |
3785 | return 2 | |
3786 | fi | |
3787 | ||
3788 | # Run rsync with --dry-run --itemize-changes to get something akin to diff | |
3789 | # output, but rsync is far more thorough in detecting differences (diff | |
3790 | # doesn't compare file metadata, and cannot handle special files). | |
3791 | # | |
3792 | # Also make sure to filter out non-user.* xattrs when comparing. On | |
3793 | # SELinux-enabled systems the copied tree will probably have different | |
3794 | # SELinux labels. | |
3795 | args=("-nicaAHX" '--filter=-x! user.*' "--delete") | |
3796 | ||
3797 | # NOTE: Quite a few rsync builds do not support --crtimes which would be | |
3798 | # necessary to verify that creation times are being maintained properly. | |
3799 | # Unfortunately because of this we cannot use it unconditionally but we can | |
3800 | # check if this rsync build supports it and use it then. This check is | |
3801 | # based on the same check in the rsync test suite (testsuite/crtimes.test). | |
3802 | # | |
3803 | # We check ctimes even with zil_replay=1 because the ZIL does store | |
3804 | # creation times and we should make sure they match (if the creation times | |
3805 | # do not match there is a "c" entry in one of the columns). | |
964d4180 | 3806 | if rsync --version | grep -q "[, ] crtimes"; then |
669683c4 AS |
3807 | args+=("--crtimes") |
3808 | else | |
964d4180 | 3809 | log_note "This rsync package does not support --crtimes (-N)." |
669683c4 AS |
3810 | fi |
3811 | ||
3812 | # If we are testing a ZIL replay, we need to ignore timestamp changes. | |
3813 | # Unfortunately --no-times doesn't do what we want -- it will still tell | |
3814 | # you if the timestamps don't match but rsync will set the timestamps to | |
3815 | # the current time (leading to an itemised change entry). It's simpler to | |
3816 | # just filter out those lines. | |
3817 | if [ "$zil_replay" -eq 0 ]; then | |
3818 | filter=("cat") | |
3819 | else | |
3820 | # Different rsync versions have different numbers of columns. So just | |
3821 | # require that aside from the first two, all other columns must be | |
3822 | # blank (literal ".") or a timestamp field ("[tT]"). | |
3823 | filter=("grep" "-v" '^\..[.Tt]\+ ') | |
3824 | fi | |
3825 | ||
3826 | diff="$(rsync "${args[@]}" "$dir_a/" "$dir_b/" | "${filter[@]}")" | |
3827 | rv=0 | |
3828 | if [ -n "$diff" ]; then | |
3829 | echo "$diff" | |
3830 | rv=1 | |
3831 | fi | |
3832 | return $rv | |
3833 | } | |
3834 | ||
3835 | # | |
3836 | # Compare two directory trees recursively, without checking whether the mtimes | |
3837 | # match (creation times will be checked if the available rsync binary supports | |
3838 | # it). This is necessary for ZIL replay checks (because the ZIL does not | |
3839 | # contain mtimes and thus after a ZIL replay, mtimes won't match). | |
3840 | # | |
3841 | # This is shorthand for LIBTEST_DIFF_ZIL_REPLAY=1 directory_diff <...>. | |
3842 | # | |
3843 | function replay_directory_diff # dir_a dir_b | |
3844 | { | |
3845 | LIBTEST_DIFF_ZIL_REPLAY=1 directory_diff "$@" | |
669683c4 | 3846 | } |
598fed7e AZ |
3847 | |
3848 | # | |
3849 | # Put coredumps into $1/core.{basename} | |
3850 | # | |
3851 | # Output must be saved and passed to pop_coredump_pattern on cleanup | |
3852 | # | |
3853 | function push_coredump_pattern # dir | |
3854 | { | |
3855 | ulimit -c unlimited | |
5c9f744b | 3856 | case "$UNAME" in |
598fed7e AZ |
3857 | Linux) |
3858 | cat /proc/sys/kernel/core_pattern /proc/sys/kernel/core_uses_pid | |
3859 | echo "$1/core.%e" >/proc/sys/kernel/core_pattern && | |
3860 | echo 0 >/proc/sys/kernel/core_uses_pid | |
3861 | ;; | |
3862 | FreeBSD) | |
3863 | sysctl -n kern.corefile | |
3864 | sysctl kern.corefile="$1/core.%N" >/dev/null | |
3865 | ;; | |
3866 | *) | |
3867 | # Nothing to output – set only for this shell | |
3868 | coreadm -p "$1/core.%f" | |
3869 | ;; | |
3870 | esac | |
3871 | } | |
3872 | ||
3873 | # | |
3874 | # Put coredumps back into the default location | |
3875 | # | |
3876 | function pop_coredump_pattern | |
3877 | { | |
3878 | [ -s "$1" ] || return 0 | |
5c9f744b | 3879 | case "$UNAME" in |
598fed7e AZ |
3880 | Linux) |
3881 | typeset pat pid | |
3882 | { read -r pat; read -r pid; } < "$1" | |
3883 | echo "$pat" >/proc/sys/kernel/core_pattern && | |
3884 | echo "$pid" >/proc/sys/kernel/core_uses_pid | |
3885 | ;; | |
3886 | FreeBSD) | |
3887 | sysctl kern.corefile="$(<"$1")" >/dev/null | |
3888 | ;; | |
3889 | esac | |
3890 | } |