]> git.proxmox.com Git - mirror_zfs.git/blame - tests/zfs-tests/include/libtest.shlib
tests: add zfs_unshare_008_pos checking whitespace escaping
[mirror_zfs.git] / tests / zfs-tests / include / libtest.shlib
CommitLineData
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
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
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#
44if [ -n "$STF_PATH" ]; then
e0b53a5d 45 export PATH="$STF_PATH"
c1d9abf9
JWK
46fi
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#
53function 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#
64function 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
84function 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
92function 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
101function 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
110function 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
119function 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
131function 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
184function 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
194function unmounted
195{
23914a3b 196 ! ismounted $1 $2
6bb24f4d
BB
197}
198
6bb24f4d
BB
199function default_setup
200{
201 default_setup_noexit "$@"
202
203 log_pass
204}
205
9c53e516
TK
206function 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#
216function 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#
271function 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#
282function 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#
293function 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#
307function 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#
330function 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#
351function 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#
378function 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
408function 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
420function 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
440function destroy_mirrors
441{
442 default_cleanup_noexit
443
444 log_pass
445}
446
1b609d4b
BA
447function 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 458function 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
481function 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#
494function get_all_pools
495{
496 zpool list -H -o name | grep -Fvx "$KEEP" | grep -v "$NO_POOLS"
497}
498
6bb24f4d
BB
499function 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#
580function 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#
604function 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#
632function 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#
657function 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
672function 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#
682function 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#
693function 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#
710function 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#
737function 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#
747function 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>
782function 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#
900function 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#
935function 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#
985function 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#
1031function 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
1048function 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
1057function 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
1069function 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
1084function 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
1097function 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
1120function shares_can_have_whitespace
1121{
1122 is_linux
1123}
1124
b7dbbf6a 1125function 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
1132function 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
1151function 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#
1162function 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
1188function 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
1200function 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
1212function 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#
1230function 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 1261function 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
5b0e75ca 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#
1280function 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#
1290function not_shared_smb
6bb24f4d 1291{
bf228f3d 1292 ! is_shared_smb $1
2f71caf2 1293}
1294
1295#
1296# Helper function to unshare a mountpoint.
1297#
1298function 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#
1310function 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#
1337function 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#
1365function 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
1380function 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#
1398function 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#
1466function 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#
1484function 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
1525function 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
1554function 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
1590function 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 1617function 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#
1646function 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 1672function 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#
1694function 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#
1715function 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#
1737function 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#
1774function 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#
1796function 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#
1806function 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# */
1823function 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#
1846function 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#
1862function 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#
1887function check_vdev_state # pool disk state{online,offline,unavail}
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#
1903function 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 1929function 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
6bb24f4d 1954#
0ea05c64
AP
1955function is_pool_resilvering #pool <verbose>
1956{
9a49d3f3 1957 check_pool_status "$1" "scan" \
b2255edc 1958 "resilver[ ()0-9A-Za-z:_-]* in progress since" $2
0ea05c64
AP
1959}
1960
1961function is_pool_resilvered #pool <verbose>
6bb24f4d 1962{
0ea05c64 1963 check_pool_status "$1" "scan" "resilvered " $2
6bb24f4d
BB
1964}
1965
0ea05c64 1966function is_pool_scrubbing #pool <verbose>
6bb24f4d 1967{
0ea05c64 1968 check_pool_status "$1" "scan" "scrub in progress since " $2
6bb24f4d
BB
1969}
1970
0ea05c64 1971function is_pool_scrubbed #pool <verbose>
6bb24f4d 1972{
0ea05c64 1973 check_pool_status "$1" "scan" "scrub repaired" $2
6bb24f4d
BB
1974}
1975
0ea05c64 1976function is_pool_scrub_stopped #pool <verbose>
6bb24f4d 1977{
0ea05c64 1978 check_pool_status "$1" "scan" "scrub canceled" $2
6bb24f4d
BB
1979}
1980
0ea05c64 1981function is_pool_scrub_paused #pool <verbose>
6bb24f4d 1982{
0ea05c64 1983 check_pool_status "$1" "scan" "scrub paused since " $2
6bb24f4d
BB
1984}
1985
a1d477c2
MA
1986function is_pool_removing #pool
1987{
1988 check_pool_status "$1" "remove" "in progress since "
a1d477c2
MA
1989}
1990
1991function is_pool_removed #pool
1992{
1993 check_pool_status "$1" "remove" "completed on"
a1d477c2
MA
1994}
1995
e60e158e
JG
1996function is_pool_discarding #pool
1997{
1998 check_pool_status "$1" "checkpoint" "discarding"
e60e158e
JG
1999}
2000
ab44e511
JWK
2001function wait_for_degraded
2002{
2003 typeset pool=$1
2004 typeset timeout=${2:-30}
2005 typeset t0=$SECONDS
2006
2007 while :; do
2008 [[ $(get_pool_prop health $pool) == "DEGRADED" ]] && break
2009 log_note "$pool is not yet degraded."
2010 sleep 1
2011 if ((SECONDS - t0 > $timeout)); then
2012 log_note "$pool not degraded after $timeout seconds."
2013 return 1
2014 fi
2015 done
2016
2017 return 0
2018}
2019
6bb24f4d 2020#
4e33ba4c 2021# Use create_pool()/destroy_pool() to clean up the information in
6bb24f4d
BB
2022# in the given disk to avoid slice overlapping.
2023#
2024function cleanup_devices #vdevs
2025{
2026 typeset pool="foopool$$"
2027
581ca281
BB
2028 for vdev in $@; do
2029 zero_partitions $vdev
2030 done
6bb24f4d 2031
581ca281 2032 poolexists $pool && destroy_pool $pool
6bb24f4d
BB
2033 create_pool $pool $@
2034 destroy_pool $pool
2035
2036 return 0
2037}
2038
6bb24f4d
BB
2039#/**
2040# A function to find and locate free disks on a system or from given
2041# disks as the parameter. It works by locating disks that are in use
2042# as swap devices and dump devices, and also disks listed in /etc/vfstab
2043#
2044# $@ given disks to find which are free, default is all disks in
2045# the test system
2046#
2047# @return a string containing the list of available disks
2048#*/
2049function find_disks
2050{
2051 # Trust provided list, no attempt is made to locate unused devices.
7839c4b5 2052 if is_linux || is_freebsd; then
c1d9abf9 2053 echo "$@"
6bb24f4d
BB
2054 return
2055 fi
2056
2057
2058 sfi=/tmp/swaplist.$$
2059 dmpi=/tmp/dumpdev.$$
2060 max_finddisksnum=${MAX_FINDDISKSNUM:-6}
2061
c1d9abf9
JWK
2062 swap -l > $sfi
2063 dumpadm > $dmpi 2>/dev/null
6bb24f4d 2064
75746e9a
AZ
2065 disks=${@:-$(echo "" | format -e 2>/dev/null | awk '
2066BEGIN { FS="."; }
6bb24f4d 2067
75746e9a
AZ
2068/^Specify disk/{
2069 searchdisks=0;
2070}
6bb24f4d 2071
75746e9a
AZ
2072{
2073 if (searchdisks && $2 !~ "^$"){
2074 split($2,arr," ");
2075 print arr[1];
6bb24f4d 2076 }
75746e9a 2077}
6bb24f4d 2078
75746e9a
AZ
2079/^AVAILABLE DISK SELECTIONS:/{
2080 searchdisks=1;
2081}
2082')}
6bb24f4d
BB
2083
2084 unused=""
2085 for disk in $disks; do
2086 # Check for mounted
23914a3b 2087 grep -q "${disk}[sp]" /etc/mnttab && continue
6bb24f4d 2088 # Check for swap
23914a3b 2089 grep -q "${disk}[sp]" $sfi && continue
6bb24f4d 2090 # check for dump device
23914a3b 2091 grep -q "${disk}[sp]" $dmpi && continue
6bb24f4d
BB
2092 # check to see if this disk hasn't been explicitly excluded
2093 # by a user-set environment variable
23914a3b 2094 echo "${ZFS_HOST_DEVICES_IGNORE}" | grep -q "${disk}" && continue
6bb24f4d
BB
2095 unused_candidates="$unused_candidates $disk"
2096 done
23914a3b 2097 rm $sfi $dmpi
6bb24f4d
BB
2098
2099# now just check to see if those disks do actually exist
2100# by looking for a device pointing to the first slice in
2101# each case. limit the number to max_finddisksnum
2102 count=0
2103 for disk in $unused_candidates; do
665684d7
RM
2104 if is_disk_device $DEV_DSKDIR/${disk}s0 && \
2105 [ $count -lt $max_finddisksnum ]; then
6bb24f4d
BB
2106 unused="$unused $disk"
2107 # do not impose limit if $@ is provided
2108 [[ -z $@ ]] && ((count = count + 1))
2109 fi
6bb24f4d
BB
2110 done
2111
2112# finally, return our disk list
c1d9abf9 2113 echo $unused
6bb24f4d
BB
2114}
2115
7839c4b5
MM
2116function add_user_freebsd #<group_name> <user_name> <basedir>
2117{
2118 typeset group=$1
2119 typeset user=$2
2120 typeset basedir=$3
2121
2122 # Check to see if the user exists.
2123 if id $user > /dev/null 2>&1; then
2124 return 0
2125 fi
2126
2127 # Assign 1000 as the base uid
2128 typeset -i uid=1000
2129 while true; do
7839c4b5 2130 pw useradd -u $uid -g $group -d $basedir/$user -m -n $user
23914a3b 2131 case $? in
7839c4b5
MM
2132 0) break ;;
2133 # The uid is not unique
2134 65) ((uid += 1)) ;;
2135 *) return 1 ;;
2136 esac
2137 if [[ $uid == 65000 ]]; then
2138 log_fail "No user id available under 65000 for $user"
2139 fi
2140 done
2141
2142 # Silence MOTD
2143 touch $basedir/$user/.hushlogin
2144
2145 return 0
2146}
2147
2148#
2149# Delete the specified user.
2150#
2151# $1 login name
2152#
2153function del_user_freebsd #<logname>
2154{
2155 typeset user=$1
2156
2157 if id $user > /dev/null 2>&1; then
2158 log_must pw userdel $user
2159 fi
2160
2161 return 0
2162}
2163
2164#
2165# Select valid gid and create specified group.
2166#
2167# $1 group name
2168#
2169function add_group_freebsd #<group_name>
2170{
2171 typeset group=$1
2172
2173 # See if the group already exists.
2174 if pw groupshow $group >/dev/null 2>&1; then
2175 return 0
2176 fi
2177
2178 # Assign 1000 as the base gid
2179 typeset -i gid=1000
2180 while true; do
2181 pw groupadd -g $gid -n $group > /dev/null 2>&1
23914a3b 2182 case $? in
7839c4b5
MM
2183 0) return 0 ;;
2184 # The gid is not unique
2185 65) ((gid += 1)) ;;
2186 *) return 1 ;;
2187 esac
2188 if [[ $gid == 65000 ]]; then
2189 log_fail "No user id available under 65000 for $group"
2190 fi
2191 done
2192}
2193
2194#
2195# Delete the specified group.
2196#
2197# $1 group name
2198#
2199function del_group_freebsd #<group_name>
2200{
2201 typeset group=$1
2202
2203 pw groupdel -n $group > /dev/null 2>&1
23914a3b 2204 case $? in
7839c4b5
MM
2205 # Group does not exist, or was deleted successfully.
2206 0|6|65) return 0 ;;
2207 # Name already exists as a group name
2208 9) log_must pw groupdel $group ;;
2209 *) return 1 ;;
2210 esac
2211
2212 return 0
2213}
2214
2215function add_user_illumos #<group_name> <user_name> <basedir>
2216{
2217 typeset group=$1
2218 typeset user=$2
2219 typeset basedir=$3
2220
2221 log_must useradd -g $group -d $basedir/$user -m $user
2222
2223 return 0
2224}
2225
2226function del_user_illumos #<user_name>
2227{
2228 typeset user=$1
2229
2230 if id $user > /dev/null 2>&1; then
2231 log_must_retry "currently used" 6 userdel $user
2232 fi
2233
2234 return 0
2235}
2236
2237function add_group_illumos #<group_name>
2238{
2239 typeset group=$1
2240
2241 typeset -i gid=100
2242 while true; do
2243 groupadd -g $gid $group > /dev/null 2>&1
23914a3b 2244 case $? in
7839c4b5
MM
2245 0) return 0 ;;
2246 # The gid is not unique
2247 4) ((gid += 1)) ;;
2248 *) return 1 ;;
2249 esac
2250 done
2251}
2252
2253function del_group_illumos #<group_name>
2254{
2255 typeset group=$1
2256
2257 groupmod -n $grp $grp > /dev/null 2>&1
23914a3b 2258 case $? in
7839c4b5
MM
2259 # Group does not exist.
2260 6) return 0 ;;
2261 # Name already exists as a group name
2262 9) log_must groupdel $grp ;;
2263 *) return 1 ;;
2264 esac
2265}
2266
2267function add_user_linux #<group_name> <user_name> <basedir>
2268{
2269 typeset group=$1
2270 typeset user=$2
2271 typeset basedir=$3
2272
2273 log_must useradd -g $group -d $basedir/$user -m $user
2274
2275 # Add new users to the same group and the command line utils.
2276 # This allows them to be run out of the original users home
2277 # directory as long as it permissioned to be group readable.
592cf7f1 2278 cmd_group=$(stat --format="%G" $(command -v zfs))
7839c4b5
MM
2279 log_must usermod -a -G $cmd_group $user
2280
2281 return 0
2282}
2283
2284function del_user_linux #<user_name>
2285{
2286 typeset user=$1
2287
2288 if id $user > /dev/null 2>&1; then
2289 log_must_retry "currently used" 6 userdel $user
2290 fi
7839c4b5
MM
2291}
2292
2293function add_group_linux #<group_name>
2294{
2295 typeset group=$1
2296
2297 # Assign 100 as the base gid, a larger value is selected for
2298 # Linux because for many distributions 1000 and under are reserved.
2299 while true; do
2300 groupadd $group > /dev/null 2>&1
23914a3b 2301 case $? in
7839c4b5
MM
2302 0) return 0 ;;
2303 *) return 1 ;;
2304 esac
2305 done
2306}
2307
2308function del_group_linux #<group_name>
2309{
2310 typeset group=$1
2311
2312 getent group $group > /dev/null 2>&1
23914a3b 2313 case $? in
7839c4b5
MM
2314 # Group does not exist.
2315 2) return 0 ;;
2316 # Name already exists as a group name
2317 0) log_must groupdel $group ;;
2318 *) return 1 ;;
2319 esac
2320
2321 return 0
2322}
2323
6bb24f4d
BB
2324#
2325# Add specified user to specified group
2326#
2327# $1 group name
2328# $2 user name
2329# $3 base of the homedir (optional)
2330#
2331function add_user #<group_name> <user_name> <basedir>
2332{
7839c4b5
MM
2333 typeset group=$1
2334 typeset user=$2
6bb24f4d
BB
2335 typeset basedir=${3:-"/var/tmp"}
2336
7839c4b5 2337 if ((${#group} == 0 || ${#user} == 0)); then
6bb24f4d
BB
2338 log_fail "group name or user name are not defined."
2339 fi
2340
5c9f744b 2341 case "$UNAME" in
7839c4b5
MM
2342 FreeBSD)
2343 add_user_freebsd "$group" "$user" "$basedir"
2344 ;;
2345 Linux)
2346 add_user_linux "$group" "$user" "$basedir"
2347 ;;
2348 *)
2349 add_user_illumos "$group" "$user" "$basedir"
2350 ;;
2351 esac
6bb24f4d
BB
2352
2353 return 0
2354}
2355
2356#
2357# Delete the specified user.
2358#
2359# $1 login name
2360# $2 base of the homedir (optional)
2361#
2362function del_user #<logname> <basedir>
2363{
2364 typeset user=$1
2365 typeset basedir=${2:-"/var/tmp"}
2366
2367 if ((${#user} == 0)); then
2368 log_fail "login name is necessary."
2369 fi
2370
5c9f744b 2371 case "$UNAME" in
7839c4b5
MM
2372 FreeBSD)
2373 del_user_freebsd "$user"
2374 ;;
2375 Linux)
2376 del_user_linux "$user"
2377 ;;
2378 *)
2379 del_user_illumos "$user"
2380 ;;
2381 esac
6bb24f4d 2382
c1d9abf9 2383 [[ -d $basedir/$user ]] && rm -fr $basedir/$user
6bb24f4d
BB
2384
2385 return 0
2386}
2387
2388#
2389# Select valid gid and create specified group.
2390#
2391# $1 group name
2392#
2393function add_group #<group_name>
2394{
2395 typeset group=$1
2396
2397 if ((${#group} == 0)); then
2398 log_fail "group name is necessary."
2399 fi
2400
5c9f744b 2401 case "$UNAME" in
7839c4b5
MM
2402 FreeBSD)
2403 add_group_freebsd "$group"
2404 ;;
2405 Linux)
2406 add_group_linux "$group"
2407 ;;
2408 *)
2409 add_group_illumos "$group"
2410 ;;
2411 esac
2412
2413 return 0
6bb24f4d
BB
2414}
2415
2416#
2417# Delete the specified group.
2418#
2419# $1 group name
2420#
2421function del_group #<group_name>
2422{
7839c4b5
MM
2423 typeset group=$1
2424
2425 if ((${#group} == 0)); then
6bb24f4d
BB
2426 log_fail "group name is necessary."
2427 fi
2428
5c9f744b 2429 case "$UNAME" in
7839c4b5
MM
2430 FreeBSD)
2431 del_group_freebsd "$group"
2432 ;;
2433 Linux)
2434 del_group_linux "$group"
2435 ;;
2436 *)
2437 del_group_illumos "$group"
2438 ;;
2439 esac
6bb24f4d
BB
2440
2441 return 0
2442}
2443
2444#
2445# This function will return true if it's safe to destroy the pool passed
2446# as argument 1. It checks for pools based on zvols and files, and also
2447# files contained in a pool that may have a different mountpoint.
2448#
2449function safe_to_destroy_pool { # $1 the pool name
2450
2451 typeset pool=""
2452 typeset DONT_DESTROY=""
2453
2454 # We check that by deleting the $1 pool, we're not
2455 # going to pull the rug out from other pools. Do this
2456 # by looking at all other pools, ensuring that they
2457 # aren't built from files or zvols contained in this pool.
2458
c1d9abf9 2459 for pool in $(zpool list -H -o name)
6bb24f4d
BB
2460 do
2461 ALTMOUNTPOOL=""
2462
2463 # this is a list of the top-level directories in each of the
2464 # files that make up the path to the files the pool is based on
75746e9a 2465 FILEPOOL=$(zpool status -v $pool | awk -v pool="/$1/" '$0 ~ pool {print $1}')
6bb24f4d
BB
2466
2467 # this is a list of the zvols that make up the pool
75746e9a 2468 ZVOLPOOL=$(zpool status -v $pool | awk -v zvols="$ZVOL_DEVDIR/$1$" '$0 ~ zvols {print $1}')
6bb24f4d
BB
2469
2470 # also want to determine if it's a file-based pool using an
2471 # alternate mountpoint...
c1d9abf9 2472 POOL_FILE_DIRS=$(zpool status -v $pool | \
75746e9a
AZ
2473 awk '/\// {print $1}' | \
2474 awk -F/ '!/dev/ {print $2}')
6bb24f4d
BB
2475
2476 for pooldir in $POOL_FILE_DIRS
2477 do
c1d9abf9 2478 OUTPUT=$(zfs list -H -r -o mountpoint $1 | \
75746e9a 2479 awk -v pd="${pooldir}$" '$0 ~ pd {print $1}')
6bb24f4d
BB
2480
2481 ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
2482 done
2483
2484
2485 if [ ! -z "$ZVOLPOOL" ]
2486 then
2487 DONT_DESTROY="true"
2488 log_note "Pool $pool is built from $ZVOLPOOL on $1"
2489 fi
2490
2491 if [ ! -z "$FILEPOOL" ]
2492 then
2493 DONT_DESTROY="true"
2494 log_note "Pool $pool is built from $FILEPOOL on $1"
2495 fi
2496
2497 if [ ! -z "$ALTMOUNTPOOL" ]
2498 then
2499 DONT_DESTROY="true"
2500 log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
2501 fi
2502 done
2503
2504 if [ -z "${DONT_DESTROY}" ]
2505 then
2506 return 0
2507 else
2508 log_note "Warning: it is not safe to destroy $1!"
2509 return 1
2510 fi
2511}
2512
6bb24f4d
BB
2513#
2514# Verify zfs operation with -p option work as expected
2515# $1 operation, value could be create, clone or rename
2516# $2 dataset type, value could be fs or vol
2517# $3 dataset name
2518# $4 new dataset name
2519#
2520function verify_opt_p_ops
2521{
2522 typeset ops=$1
2523 typeset datatype=$2
2524 typeset dataset=$3
2525 typeset newdataset=$4
2526
2527 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2528 log_fail "$datatype is not supported."
2529 fi
2530
2531 # check parameters accordingly
2532 case $ops in
2533 create)
2534 newdataset=$dataset
2535 dataset=""
2536 if [[ $datatype == "vol" ]]; then
2537 ops="create -V $VOLSIZE"
2538 fi
2539 ;;
2540 clone)
2541 if [[ -z $newdataset ]]; then
2542 log_fail "newdataset should not be empty" \
2543 "when ops is $ops."
2544 fi
2545 log_must datasetexists $dataset
2546 log_must snapexists $dataset
2547 ;;
2548 rename)
2549 if [[ -z $newdataset ]]; then
2550 log_fail "newdataset should not be empty" \
2551 "when ops is $ops."
2552 fi
2553 log_must datasetexists $dataset
6bb24f4d
BB
2554 ;;
2555 *)
2556 log_fail "$ops is not supported."
2557 ;;
2558 esac
2559
2560 # make sure the upper level filesystem does not exist
c7b55e71 2561 destroy_dataset "${newdataset%/*}" "-rRf"
6bb24f4d
BB
2562
2563 # without -p option, operation will fail
c1d9abf9 2564 log_mustnot zfs $ops $dataset $newdataset
6bb24f4d
BB
2565 log_mustnot datasetexists $newdataset ${newdataset%/*}
2566
2567 # with -p option, operation should succeed
c1d9abf9 2568 log_must zfs $ops -p $dataset $newdataset
6bb24f4d
BB
2569 block_device_wait
2570
2571 if ! datasetexists $newdataset ; then
2572 log_fail "-p option does not work for $ops"
2573 fi
2574
2575 # when $ops is create or clone, redo the operation still return zero
2576 if [[ $ops != "rename" ]]; then
c1d9abf9 2577 log_must zfs $ops -p $dataset $newdataset
6bb24f4d
BB
2578 fi
2579
2580 return 0
2581}
2582
2583#
2584# Get configuration of pool
2585# $1 pool name
2586# $2 config name
2587#
2588function get_config
2589{
2590 typeset pool=$1
2591 typeset config=$2
6bb24f4d
BB
2592
2593 if ! poolexists "$pool" ; then
2594 return 1
2595 fi
75746e9a
AZ
2596 if [ "$(get_pool_prop cachefile "$pool")" = "none" ]; then
2597 zdb -e $pool
6bb24f4d 2598 else
75746e9a
AZ
2599 zdb -C $pool
2600 fi | awk -F: -v cfg="$config:" '$0 ~ cfg {sub(/^'\''/, $2); sub(/'\''$/, $2); print $2}'
6bb24f4d
BB
2601}
2602
2603#
2604# Privated function. Random select one of items from arguments.
2605#
2606# $1 count
2607# $2-n string
2608#
2609function _random_get
2610{
2611 typeset cnt=$1
2612 shift
2613
2614 typeset str="$@"
2615 typeset -i ind
2616 ((ind = RANDOM % cnt + 1))
2617
75746e9a 2618 echo "$str" | cut -f $ind -d ' '
6bb24f4d
BB
2619}
2620
2621#
2622# Random select one of item from arguments which include NONE string
2623#
2624function random_get_with_non
2625{
2626 typeset -i cnt=$#
2627 ((cnt =+ 1))
2628
2629 _random_get "$cnt" "$@"
2630}
2631
2632#
2633# Random select one of item from arguments which doesn't include NONE string
2634#
2635function random_get
2636{
2637 _random_get "$#" "$@"
2638}
2639
6bb24f4d
BB
2640#
2641# The function will generate a dataset name with specific length
2642# $1, the length of the name
2643# $2, the base string to construct the name
2644#
2645function gen_dataset_name
2646{
2647 typeset -i len=$1
2648 typeset basestr="$2"
2649 typeset -i baselen=${#basestr}
2650 typeset -i iter=0
2651 typeset l_name=""
2652
2653 if ((len % baselen == 0)); then
2654 ((iter = len / baselen))
2655 else
2656 ((iter = len / baselen + 1))
2657 fi
2658 while ((iter > 0)); do
2659 l_name="${l_name}$basestr"
2660
2661 ((iter -= 1))
2662 done
2663
c1d9abf9 2664 echo $l_name
6bb24f4d
BB
2665}
2666
2667#
2668# Get cksum tuple of dataset
2669# $1 dataset name
2670#
2671# sample zdb output:
2672# Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2673# DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2674# lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2675# fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2676function datasetcksum
2677{
2678 typeset cksum
c1d9abf9 2679 sync
7454275a 2680 sync_all_pools
75746e9a 2681 zdb -vvv $1 | awk -F= -v ds="^Dataset $1 "'\\[' '$0 ~ ds && /cksum/ {print $7}'
6bb24f4d
BB
2682}
2683
2684#
2685# Get the given disk/slice state from the specific field of the pool
2686#
2687function get_device_state #pool disk field("", "spares","logs")
2688{
2689 typeset pool=$1
2690 typeset disk=${2#$DEV_DSKDIR/}
2691 typeset field=${3:-$pool}
2692
270d0a5a
AZ
2693 zpool status -v "$pool" 2>/dev/null | \
2694 awk -v device=$disk -v pool=$pool -v field=$field \
6bb24f4d
BB
2695 'BEGIN {startconfig=0; startfield=0; }
2696 /config:/ {startconfig=1}
2697 (startconfig==1) && ($1==field) {startfield=1; next;}
2698 (startfield==1) && ($1==device) {print $2; exit;}
2699 (startfield==1) &&
270d0a5a 2700 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}'
6bb24f4d
BB
2701}
2702
6bb24f4d
BB
2703#
2704# get the root filesystem name if it's zfsroot system.
2705#
2706# return: root filesystem name
2707function get_rootfs
2708{
2709 typeset rootfs=""
8aab1218 2710
7839c4b5
MM
2711 if is_freebsd; then
2712 rootfs=$(mount -p | awk '$2 == "/" && $3 == "zfs" {print $1}')
2713 elif ! is_linux; then
75746e9a 2714 rootfs=$(awk '$2 == "/" && $3 == "zfs" {print $1}' \
8aab1218
TS
2715 /etc/mnttab)
2716 fi
6bb24f4d
BB
2717 if [[ -z "$rootfs" ]]; then
2718 log_fail "Can not get rootfs"
2719 fi
23914a3b 2720 if datasetexists $rootfs; then
c1d9abf9 2721 echo $rootfs
6bb24f4d
BB
2722 else
2723 log_fail "This is not a zfsroot system."
2724 fi
2725}
2726
2727#
2728# get the rootfs's pool name
2729# return:
2730# rootpool name
2731#
2732function get_rootpool
2733{
bd328a58
AZ
2734 typeset rootfs=$(get_rootfs)
2735 echo ${rootfs%%/*}
6bb24f4d
BB
2736}
2737
2738#
2739# To verify if the require numbers of disks is given
2740#
2741function verify_disk_count
2742{
2743 typeset -i min=${2:-1}
2744
bd328a58 2745 typeset -i count=$(echo "$1" | wc -w)
6bb24f4d
BB
2746
2747 if ((count < min)); then
2748 log_untested "A minimum of $min disks is required to run." \
2749 " You specified $count disk(s)"
2750 fi
2751}
2752
2753function ds_is_volume
2754{
2755 typeset type=$(get_prop type $1)
caccfc87 2756 [ $type = "volume" ]
6bb24f4d
BB
2757}
2758
2759function ds_is_filesystem
2760{
2761 typeset type=$(get_prop type $1)
caccfc87 2762 [ $type = "filesystem" ]
6bb24f4d
BB
2763}
2764
6bb24f4d
BB
2765#
2766# Check if Trusted Extensions are installed and enabled
2767#
2768function is_te_enabled
2769{
bd328a58 2770 svcs -H -o state labeld 2>/dev/null | grep -q "enabled"
6bb24f4d
BB
2771}
2772
2773# Utility function to determine if a system has multiple cpus.
2774function is_mp
2775{
5c9f744b 2776 case "$UNAME" in
bd328a58 2777 Linux)
caccfc87 2778 (($(grep -c '^processor' /proc/cpuinfo) > 1))
bd328a58
AZ
2779 ;;
2780 FreeBSD)
2781 sysctl -n kern.smp.cpus
2782 ;;
2783 *)
2784 (($(psrinfo | wc -l) > 1))
2785 ;;
2786 esac
6bb24f4d
BB
2787}
2788
2789function get_cpu_freq
2790{
2791 if is_linux; then
c1d9abf9 2792 lscpu | awk '/CPU MHz/ { print $3 }'
7839c4b5 2793 elif is_freebsd; then
9be70c37 2794 sysctl -n hw.clockrate
6bb24f4d 2795 else
c1d9abf9 2796 psrinfo -v 0 | awk '/processor operates at/ {print $6}'
6bb24f4d
BB
2797 fi
2798}
2799
2800# Run the given command as the user provided.
2801function user_run
2802{
2803 typeset user=$1
2804 shift
2805
e0b53a5d
RM
2806 log_note "user: $user"
2807 log_note "cmd: $*"
2808
2809 typeset out=$TEST_BASE_DIR/out
2810 typeset err=$TEST_BASE_DIR/err
2811
2812 sudo -Eu $user env PATH="$PATH" ksh <<<"$*" >$out 2>$err
2813 typeset res=$?
2814 log_note "out: $(<$out)"
2815 log_note "err: $(<$err)"
2816 return $res
6bb24f4d
BB
2817}
2818
2819#
2820# Check if the pool contains the specified vdevs
2821#
2822# $1 pool
2823# $2..n <vdev> ...
2824#
2825# Return 0 if the vdevs are contained in the pool, 1 if any of the specified
2826# vdevs is not in the pool, and 2 if pool name is missing.
2827#
2828function vdevs_in_pool
2829{
2830 typeset pool=$1
2831 typeset vdev
2832
2833 if [[ -z $pool ]]; then
2834 log_note "Missing pool name."
2835 return 2
2836 fi
2837
2838 shift
2839
7c9a4292
BB
2840 # We could use 'zpool list' to only get the vdevs of the pool but we
2841 # can't reference a mirror/raidz vdev using its ID (i.e mirror-0),
2842 # therefore we use the 'zpool status' output.
c1d9abf9 2843 typeset tmpfile=$(mktemp)
7c9a4292 2844 zpool status -v "$pool" | grep -A 1000 "config:" >$tmpfile
23914a3b 2845 for vdev in "$@"; do
964d4180 2846 grep -wq ${vdev##*/} $tmpfile || return 1
6bb24f4d
BB
2847 done
2848
c1d9abf9 2849 rm -f $tmpfile
23914a3b 2850 return 0
6bb24f4d
BB
2851}
2852
679d73e9
JWK
2853function get_max
2854{
2855 typeset -l i max=$1
2856 shift
2857
2858 for i in "$@"; do
9be70c37 2859 max=$((max > i ? max : i))
679d73e9
JWK
2860 done
2861
2862 echo $max
2863}
2864
a7004725
DK
2865# Write data that can be compressed into a directory
2866function write_compressible
2867{
2868 typeset dir=$1
2869 typeset megs=$2
2870 typeset nfiles=${3:-1}
2871 typeset bs=${4:-1024k}
2872 typeset fname=${5:-file}
2873
2874 [[ -d $dir ]] || log_fail "No directory: $dir"
2875
2876 # Under Linux fio is not currently used since its behavior can
2877 # differ significantly across versions. This includes missing
2878 # command line options and cases where the --buffer_compress_*
2879 # options fail to behave as expected.
2880 if is_linux; then
2881 typeset file_bytes=$(to_bytes $megs)
2882 typeset bs_bytes=4096
2883 typeset blocks=$(($file_bytes / $bs_bytes))
2884
2885 for (( i = 0; i < $nfiles; i++ )); do
2886 truncate -s $file_bytes $dir/$fname.$i
2887
2888 # Write every third block to get 66% compression.
2889 for (( j = 0; j < $blocks; j += 3 )); do
2890 dd if=/dev/urandom of=$dir/$fname.$i \
2891 seek=$j bs=$bs_bytes count=1 \
2892 conv=notrunc >/dev/null 2>&1
2893 done
2894 done
2895 else
2d9da5e1 2896 command -v fio > /dev/null || log_unsupported "fio missing"
bd328a58 2897 log_must eval fio \
a7004725
DK
2898 --name=job \
2899 --fallocate=0 \
2900 --minimal \
2901 --randrepeat=0 \
2902 --buffer_compress_percentage=66 \
2903 --buffer_compress_chunk=4096 \
bd328a58
AZ
2904 --directory="$dir" \
2905 --numjobs="$nfiles" \
2906 --nrfiles="$nfiles" \
a7004725 2907 --rw=write \
bd328a58
AZ
2908 --bs="$bs" \
2909 --filesize="$megs" \
2910 "--filename_format='$fname.\$jobnum' >/dev/null"
a7004725
DK
2911 fi
2912}
2913
2914function get_objnum
2915{
2916 typeset pathname=$1
2917 typeset objnum
2918
2919 [[ -e $pathname ]] || log_fail "No such file or directory: $pathname"
7839c4b5
MM
2920 if is_freebsd; then
2921 objnum=$(stat -f "%i" $pathname)
2922 else
2923 objnum=$(stat -c %i $pathname)
2924 fi
a7004725
DK
2925 echo $objnum
2926}
2927
1de321e6 2928#
bec1067d 2929# Sync data to the pool
1de321e6
JX
2930#
2931# $1 pool name
bec1067d 2932# $2 boolean to force uberblock (and config including zpool cache file) update
1de321e6 2933#
bec1067d 2934function sync_pool #pool <force>
1de321e6
JX
2935{
2936 typeset pool=${1:-$TESTPOOL}
bec1067d 2937 typeset force=${2:-false}
1de321e6 2938
bec1067d
AP
2939 if [[ $force == true ]]; then
2940 log_must zpool sync -f $pool
2941 else
2942 log_must zpool sync $pool
2943 fi
2944
2945 return 0
1de321e6 2946}
d834b9ce 2947
7454275a
AJ
2948#
2949# Sync all pools
2950#
2951# $1 boolean to force uberblock (and config including zpool cache file) update
2952#
2953function sync_all_pools #<force>
2954{
2955 typeset force=${1:-false}
2956
2957 if [[ $force == true ]]; then
2958 log_must zpool sync -f
2959 else
2960 log_must zpool sync
2961 fi
2962
2963 return 0
2964}
2965
d834b9ce
GM
2966#
2967# Wait for zpool 'freeing' property drops to zero.
2968#
2969# $1 pool name
2970#
2971function wait_freeing #pool
2972{
2973 typeset pool=${1:-$TESTPOOL}
2974 while true; do
c1d9abf9
JWK
2975 [[ "0" == "$(zpool list -Ho freeing $pool)" ]] && break
2976 log_must sleep 1
d834b9ce
GM
2977 done
2978}
7a4500a1 2979
dddef7d6 2980#
2981# Wait for every device replace operation to complete
2982#
2983# $1 pool name
2984#
2985function wait_replacing #pool
2986{
2987 typeset pool=${1:-$TESTPOOL}
75746e9a 2988 while zpool status $pool | grep -qE 'replacing-[0-9]+'; do
dddef7d6 2989 log_must sleep 1
2990 done
2991}
2992
bf95a000
TH
2993# Wait for a pool to be scrubbed
2994#
2995# $1 pool name
56fa4aa9 2996# $2 timeout
bf95a000 2997#
56fa4aa9 2998function wait_scrubbed #pool timeout
bf95a000 2999{
56fa4aa9
RE
3000 typeset timeout=${2:-300}
3001 typeset pool=${1:-$TESTPOOL}
3002 for (( timer = 0; timer < $timeout; timer++ )); do
3003 is_pool_scrubbed $pool && break;
3004 sleep 1;
3005 done
bf95a000
TH
3006}
3007
639b1894
TH
3008# Backup the zed.rc in our test directory so that we can edit it for our test.
3009#
3010# Returns: Backup file name. You will need to pass this to zed_rc_restore().
3011function zed_rc_backup
3012{
3013 zedrc_backup="$(mktemp)"
3014 cp $ZEDLET_DIR/zed.rc $zedrc_backup
3015 echo $zedrc_backup
3016}
3017
3018function zed_rc_restore
3019{
3020 mv $1 $ZEDLET_DIR/zed.rc
3021}
3022
95401cb6
BB
3023#
3024# Setup custom environment for the ZED.
3025#
bf95a000 3026# $@ Optional list of zedlets to run under zed.
95401cb6
BB
3027function zed_setup
3028{
3029 if ! is_linux; then
5c9f744b 3030 log_unsupported "No zed on $UNAME"
95401cb6
BB
3031 fi
3032
3033 if [[ ! -d $ZEDLET_DIR ]]; then
3034 log_must mkdir $ZEDLET_DIR
3035 fi
3036
3037 if [[ ! -e $VDEVID_CONF ]]; then
3038 log_must touch $VDEVID_CONF
3039 fi
3040
3041 if [[ -e $VDEVID_CONF_ETC ]]; then
3042 log_fail "Must not have $VDEVID_CONF_ETC file present on system"
3043 fi
bf95a000 3044 EXTRA_ZEDLETS=$@
95401cb6
BB
3045
3046 # Create a symlink for /etc/zfs/vdev_id.conf file.
3047 log_must ln -s $VDEVID_CONF $VDEVID_CONF_ETC
3048
3049 # Setup minimal ZED configuration. Individual test cases should
3050 # add additional ZEDLETs as needed for their specific test.
3f03fc8d
BB
3051 log_must cp ${ZEDLET_ETC_DIR}/zed.rc $ZEDLET_DIR
3052 log_must cp ${ZEDLET_ETC_DIR}/zed-functions.sh $ZEDLET_DIR
95401cb6 3053
bf95a000
TH
3054 # Scripts must only be user writable.
3055 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3056 saved_umask=$(umask)
3057 log_must umask 0022
3058 for i in $EXTRA_ZEDLETS ; do
3059 log_must cp ${ZEDLET_LIBEXEC_DIR}/$i $ZEDLET_DIR
3060 done
3061 log_must umask $saved_umask
3062 fi
3063
3f03fc8d
BB
3064 # Customize the zed.rc file to enable the full debug log.
3065 log_must sed -i '/\#ZED_DEBUG_LOG=.*/d' $ZEDLET_DIR/zed.rc
d5e024cb 3066 echo "ZED_DEBUG_LOG=$ZED_DEBUG_LOG" >>$ZEDLET_DIR/zed.rc
3f03fc8d 3067
95401cb6
BB
3068}
3069
3070#
3071# Cleanup custom ZED environment.
3072#
bf95a000 3073# $@ Optional list of zedlets to remove from our test zed.d directory.
95401cb6
BB
3074function zed_cleanup
3075{
3076 if ! is_linux; then
3077 return
3078 fi
3079
bd328a58
AZ
3080 for extra_zedlet; do
3081 log_must rm -f ${ZEDLET_DIR}/$extra_zedlet
3082 done
3083 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 \
3084 $ZED_LOG $ZED_DEBUG_LOG $VDEVID_CONF_ETC $VDEVID_CONF \
3085 $ZEDLET_DIR
95401cb6
BB
3086}
3087
56fa4aa9
RE
3088#
3089# Check if ZED is currently running; if so, returns PIDs
3090#
3091function zed_check
3092{
3093 if ! is_linux; then
3094 return
3095 fi
3096 zedpids="$(pgrep -x zed)"
56fa4aa9 3097 zedpids2="$(pgrep -x lt-zed)"
56fa4aa9
RE
3098 echo ${zedpids} ${zedpids2}
3099}
3100
7a4500a1
SV
3101#
3102# Check if ZED is currently running, if not start ZED.
3103#
3104function zed_start
3105{
95401cb6
BB
3106 if ! is_linux; then
3107 return
3108 fi
7a4500a1 3109
95401cb6
BB
3110 # ZEDLET_DIR=/var/tmp/zed
3111 if [[ ! -d $ZEDLET_DIR ]]; then
3112 log_must mkdir $ZEDLET_DIR
3113 fi
7a4500a1 3114
95401cb6 3115 # Verify the ZED is not already running.
56fa4aa9
RE
3116 zedpids=$(zed_check)
3117 if [ -n "$zedpids" ]; then
3118 # We never, ever, really want it to just keep going if zed
3119 # is already running - usually this implies our test cases
3120 # will break very strangely because whatever we wanted to
3121 # configure zed for won't be listening to our changes in the
3122 # tmpdir
3123 log_fail "ZED already running - ${zedpids}"
03431390
OF
3124 else
3125 log_note "Starting ZED"
3126 # run ZED in the background and redirect foreground logging
3127 # output to $ZED_LOG.
3128 log_must truncate -s 0 $ZED_DEBUG_LOG
73218f41
AZ
3129 log_must eval "zed -vF -d $ZEDLET_DIR -P $PATH" \
3130 "-s $ZEDLET_DIR/state -j 1 2>$ZED_LOG &"
7a4500a1 3131 fi
95401cb6 3132
3f03fc8d 3133 return 0
7a4500a1
SV
3134}
3135
3136#
3137# Kill ZED process
3138#
3139function zed_stop
3140{
95401cb6 3141 if ! is_linux; then
56fa4aa9 3142 return ""
95401cb6
BB
3143 fi
3144
3f03fc8d 3145 log_note "Stopping ZED"
73218f41 3146 while true; do
56fa4aa9
RE
3147 zedpids=$(zed_check)
3148 [ ! -n "$zedpids" ] && break
73218f41
AZ
3149
3150 log_must kill $zedpids
3151 sleep 1
3152 done
3f03fc8d 3153 return 0
7a4500a1 3154}
8c54ddd3 3155
4e9b1569 3156#
3157# Drain all zevents
3158#
3159function zed_events_drain
3160{
3161 while [ $(zpool events -H | wc -l) -ne 0 ]; do
3162 sleep 1
3163 zpool events -c >/dev/null
3164 done
3165}
3166
639b1894
TH
3167# Set a variable in zed.rc to something, un-commenting it in the process.
3168#
3169# $1 variable
3170# $2 value
3171function zed_rc_set
3172{
3173 var="$1"
3174 val="$2"
3175 # Remove the line
3176 cmd="'/$var/d'"
3177 eval sed -i $cmd $ZEDLET_DIR/zed.rc
3178
3179 # Add it at the end
3180 echo "$var=$val" >> $ZEDLET_DIR/zed.rc
3181}
3182
3183
8c54ddd3
BB
3184#
3185# Check is provided device is being active used as a swap device.
3186#
3187function is_swap_inuse
3188{
3189 typeset device=$1
3190
3191 if [[ -z $device ]] ; then
3192 log_note "No device specified."
3193 return 1
3194 fi
3195
5c9f744b 3196 case "$UNAME" in
62c5ccdf
AZ
3197 Linux)
3198 swapon -s | grep -wq $(readlink -f $device)
3199 ;;
3200 FreeBSD)
3201 swapctl -l | grep -wq $device
3202 ;;
3203 *)
3204 swap -l | grep -wq $device
3205 ;;
3206 esac
8c54ddd3
BB
3207}
3208
3209#
3210# Setup a swap device using the provided device.
3211#
3212function swap_setup
3213{
3214 typeset swapdev=$1
3215
5c9f744b 3216 case "$UNAME" in
62c5ccdf 3217 Linux)
c7a7601c 3218 log_must eval "mkswap $swapdev > /dev/null 2>&1"
8c54ddd3 3219 log_must swapon $swapdev
62c5ccdf
AZ
3220 ;;
3221 FreeBSD)
7839c4b5 3222 log_must swapctl -a $swapdev
62c5ccdf
AZ
3223 ;;
3224 *)
3225 log_must swap -a $swapdev
3226 ;;
3227 esac
8c54ddd3
BB
3228
3229 return 0
3230}
3231
3232#
3233# Cleanup a swap device on the provided device.
3234#
3235function swap_cleanup
3236{
3237 typeset swapdev=$1
3238
3239 if is_swap_inuse $swapdev; then
3240 if is_linux; then
3241 log_must swapoff $swapdev
7839c4b5
MM
3242 elif is_freebsd; then
3243 log_must swapoff $swapdev
8c54ddd3
BB
3244 else
3245 log_must swap -d $swapdev
3246 fi
3247 fi
3248
3249 return 0
3250}
379ca9cf
OF
3251
3252#
3253# Set a global system tunable (64-bit value)
3254#
2476f103 3255# $1 tunable name (use a NAME defined in tunables.cfg)
379ca9cf
OF
3256# $2 tunable values
3257#
3258function set_tunable64
3259{
3260 set_tunable_impl "$1" "$2" Z
3261}
3262
3263#
3264# Set a global system tunable (32-bit value)
3265#
2476f103 3266# $1 tunable name (use a NAME defined in tunables.cfg)
379ca9cf
OF
3267# $2 tunable values
3268#
3269function set_tunable32
3270{
3271 set_tunable_impl "$1" "$2" W
3272}
3273
3274function set_tunable_impl
3275{
2476f103 3276 typeset name="$1"
379ca9cf
OF
3277 typeset value="$2"
3278 typeset mdb_cmd="$3"
379ca9cf 3279
2476f103
RM
3280 eval "typeset tunable=\$$name"
3281 case "$tunable" in
3282 UNSUPPORTED)
5c9f744b 3283 log_unsupported "Tunable '$name' is unsupported on $UNAME"
2476f103
RM
3284 ;;
3285 "")
3286 log_fail "Tunable '$name' must be added to tunables.cfg"
3287 ;;
3288 *)
3289 ;;
3290 esac
3291
379ca9cf
OF
3292 [[ -z "$value" ]] && return 1
3293 [[ -z "$mdb_cmd" ]] && return 1
3294
5c9f744b 3295 case "$UNAME" in
379ca9cf 3296 Linux)
ad9e7676 3297 typeset zfs_tunables="/sys/module/zfs/parameters"
23914a3b 3298 echo "$value" >"$zfs_tunables/$tunable"
379ca9cf 3299 ;;
7839c4b5
MM
3300 FreeBSD)
3301 sysctl vfs.zfs.$tunable=$value
7839c4b5 3302 ;;
379ca9cf 3303 SunOS)
379ca9cf 3304 echo "${tunable}/${mdb_cmd}0t${value}" | mdb -kw
379ca9cf
OF
3305 ;;
3306 esac
3307}
3308
3309#
3310# Get a global system tunable
3311#
2476f103 3312# $1 tunable name (use a NAME defined in tunables.cfg)
379ca9cf
OF
3313#
3314function get_tunable
3315{
3316 get_tunable_impl "$1"
3317}
3318
3319function get_tunable_impl
3320{
2476f103 3321 typeset name="$1"
379ca9cf
OF
3322 typeset module="${2:-zfs}"
3323
2476f103
RM
3324 eval "typeset tunable=\$$name"
3325 case "$tunable" in
3326 UNSUPPORTED)
5c9f744b 3327 log_unsupported "Tunable '$name' is unsupported on $UNAME"
2476f103
RM
3328 ;;
3329 "")
3330 log_fail "Tunable '$name' must be added to tunables.cfg"
3331 ;;
3332 *)
3333 ;;
3334 esac
379ca9cf 3335
5c9f744b 3336 case "$UNAME" in
379ca9cf
OF
3337 Linux)
3338 typeset zfs_tunables="/sys/module/$module/parameters"
379ca9cf 3339 cat $zfs_tunables/$tunable
379ca9cf 3340 ;;
7839c4b5
MM
3341 FreeBSD)
3342 sysctl -n vfs.zfs.$tunable
3343 ;;
379ca9cf
OF
3344 SunOS)
3345 [[ "$module" -eq "zfs" ]] || return 1
3346 ;;
3347 esac
d2734cce 3348}
240c015a
RM
3349
3350#
3351# Compute MD5 digest for given file or stdin if no file given.
3352# Note: file path must not contain spaces
3353#
3354function md5digest
3355{
3356 typeset file=$1
3357
5c9f744b 3358 case "$UNAME" in
7839c4b5
MM
3359 FreeBSD)
3360 md5 -q $file
3361 ;;
3362 *)
75746e9a
AZ
3363 typeset sum _
3364 read -r sum _ < <(md5sum -b $file)
3365 echo $sum
7839c4b5
MM
3366 ;;
3367 esac
240c015a
RM
3368}
3369
3370#
3371# Compute SHA256 digest for given file or stdin if no file given.
3372# Note: file path must not contain spaces
3373#
3374function sha256digest
3375{
3376 typeset file=$1
3377
5c9f744b 3378 case "$UNAME" in
7839c4b5
MM
3379 FreeBSD)
3380 sha256 -q $file
3381 ;;
3382 *)
75746e9a
AZ
3383 typeset sum _
3384 read -r sum _ < <(sha256sum -b $file)
3385 echo $sum
7839c4b5
MM
3386 ;;
3387 esac
3388}
3389
3390function new_fs #<args>
3391{
5c9f744b 3392 case "$UNAME" in
7839c4b5
MM
3393 FreeBSD)
3394 newfs "$@"
3395 ;;
3396 *)
3397 echo y | newfs -v "$@"
3398 ;;
3399 esac
3400}
3401
3402function stat_size #<path>
3403{
3404 typeset path=$1
3405
5c9f744b 3406 case "$UNAME" in
7839c4b5
MM
3407 FreeBSD)
3408 stat -f %z "$path"
3409 ;;
3410 *)
3411 stat -c %s "$path"
3412 ;;
3413 esac
240c015a 3414}
9fb2771a 3415
8ae86e2e
RM
3416function stat_ctime #<path>
3417{
3418 typeset path=$1
3419
5c9f744b 3420 case "$UNAME" in
8ae86e2e
RM
3421 FreeBSD)
3422 stat -f %c "$path"
3423 ;;
3424 *)
3425 stat -c %Z "$path"
3426 ;;
3427 esac
3428}
3429
3430function stat_crtime #<path>
3431{
3432 typeset path=$1
3433
5c9f744b 3434 case "$UNAME" in
8ae86e2e
RM
3435 FreeBSD)
3436 stat -f %B "$path"
3437 ;;
3438 *)
3439 stat -c %W "$path"
3440 ;;
3441 esac
3442}
3443
3fa5266d
RM
3444function stat_generation #<path>
3445{
3446 typeset path=$1
3447
5c9f744b 3448 case "$UNAME" in
3fa5266d
RM
3449 Linux)
3450 getversion "${path}"
3451 ;;
3452 *)
3453 stat -f %v "${path}"
3454 ;;
3455 esac
3456}
3457
9fb2771a
TH
3458# Run a command as if it was being run in a TTY.
3459#
3460# Usage:
3461#
3462# faketty command
3463#
3464function faketty
3465{
3466 if is_freebsd; then
834f274f 3467 script -q /dev/null env "$@"
9fb2771a
TH
3468 else
3469 script --return --quiet -c "$*" /dev/null
3470 fi
3471}
90ae4873
RM
3472
3473#
3474# Produce a random permutation of the integers in a given range (inclusive).
3475#
3476function range_shuffle # begin end
3477{
3478 typeset -i begin=$1
3479 typeset -i end=$2
3480
7a298ae9 3481 seq ${begin} ${end} | sort -R
90ae4873 3482}
6e1c594d
RM
3483
3484#
3485# Cross-platform xattr helpers
3486#
3487
3488function get_xattr # name path
3489{
3490 typeset name=$1
3491 typeset path=$2
3492
5c9f744b 3493 case "$UNAME" in
6e1c594d
RM
3494 FreeBSD)
3495 getextattr -qq user "${name}" "${path}"
3496 ;;
3497 *)
3498 attr -qg "${name}" "${path}"
3499 ;;
3500 esac
3501}
3502
3503function set_xattr # name value path
3504{
3505 typeset name=$1
3506 typeset value=$2
3507 typeset path=$3
3508
5c9f744b 3509 case "$UNAME" in
6e1c594d
RM
3510 FreeBSD)
3511 setextattr user "${name}" "${value}" "${path}"
3512 ;;
3513 *)
3514 attr -qs "${name}" -V "${value}" "${path}"
3515 ;;
3516 esac
3517}
3518
3519function set_xattr_stdin # name value
3520{
3521 typeset name=$1
3522 typeset path=$2
3523
5c9f744b 3524 case "$UNAME" in
6e1c594d
RM
3525 FreeBSD)
3526 setextattr -i user "${name}" "${path}"
3527 ;;
3528 *)
3529 attr -qs "${name}" "${path}"
3530 ;;
3531 esac
3532}
3533
3534function rm_xattr # name path
3535{
3536 typeset name=$1
3537 typeset path=$2
3538
5c9f744b 3539 case "$UNAME" in
6e1c594d
RM
3540 FreeBSD)
3541 rmextattr -q user "${name}" "${path}"
3542 ;;
3543 *)
3544 attr -qr "${name}" "${path}"
3545 ;;
3546 esac
3547}
3548
3549function ls_xattr # path
3550{
3551 typeset path=$1
3552
5c9f744b 3553 case "$UNAME" in
6e1c594d
RM
3554 FreeBSD)
3555 lsextattr -qq user "${path}"
3556 ;;
3557 *)
3558 attr -ql "${path}"
3559 ;;
3560 esac
3561}
37c22948 3562
73989f4b
RM
3563function kstat # stat flags?
3564{
3565 typeset stat=$1
3566 typeset flags=${2-"-n"}
3567
5c9f744b 3568 case "$UNAME" in
73989f4b
RM
3569 FreeBSD)
3570 sysctl $flags kstat.zfs.misc.$stat
3571 ;;
3572 Linux)
75746e9a 3573 cat "/proc/spl/kstat/zfs/$stat" 2>/dev/null
73989f4b
RM
3574 ;;
3575 *)
3576 false
3577 ;;
3578 esac
3579}
3580
37c22948
GA
3581function get_arcstat # stat
3582{
9f0a21e6
MM
3583 typeset stat=$1
3584
5c9f744b 3585 case "$UNAME" in
9f0a21e6 3586 FreeBSD)
73989f4b 3587 kstat arcstats.$stat
9f0a21e6
MM
3588 ;;
3589 Linux)
75746e9a 3590 kstat arcstats | awk "/$stat/"' { print $3 }'
9f0a21e6
MM
3591 ;;
3592 *)
3593 false
3594 ;;
3595 esac
37c22948 3596}
c15d36c6 3597
c3cb57ae
KHN
3598function punch_hole # offset length file
3599{
3600 typeset offset=$1
3601 typeset length=$2
3602 typeset file=$3
3603
5c9f744b 3604 case "$UNAME" in
c3cb57ae
KHN
3605 FreeBSD)
3606 truncate -d -o $offset -l $length "$file"
3607 ;;
3608 Linux)
3609 fallocate --punch-hole --offset $offset --length $length "$file"
3610 ;;
3611 *)
3612 false
3613 ;;
3614 esac
3615}
3616
b657f2c5
RE
3617function zero_range # offset length file
3618{
3619 typeset offset=$1
3620 typeset length=$2
3621 typeset file=$3
3622
3623 case "$UNAME" in
3624 Linux)
3625 fallocate --zero-range --offset $offset --length $length "$file"
3626 ;;
3627 *)
3628 false
3629 ;;
3630 esac
3631}
3632
a76e4e67
GA
3633#
3634# Wait for the specified arcstat to reach non-zero quiescence.
3635# If echo is 1 echo the value after reaching quiescence, otherwise
3636# if echo is 0 print the arcstat we are waiting on.
3637#
3638function arcstat_quiescence # stat echo
3639{
3640 typeset stat=$1
3641 typeset echo=$2
3642 typeset do_once=true
3643
3644 if [[ $echo -eq 0 ]]; then
3645 echo "Waiting for arcstat $1 quiescence."
3646 fi
3647
3648 while $do_once || [ $stat1 -ne $stat2 ] || [ $stat2 -eq 0 ]; do
3649 typeset stat1=$(get_arcstat $stat)
3650 sleep 2
3651 typeset stat2=$(get_arcstat $stat)
3652 do_once=false
3653 done
3654
3655 if [[ $echo -eq 1 ]]; then
3656 echo $stat2
3657 fi
3658}
3659
3660function arcstat_quiescence_noecho # stat
3661{
3662 typeset stat=$1
3663 arcstat_quiescence $stat 0
3664}
3665
3666function arcstat_quiescence_echo # stat
3667{
3668 typeset stat=$1
3669 arcstat_quiescence $stat 1
3670}
3671
c15d36c6
GW
3672#
3673# Given an array of pids, wait until all processes
3674# have completed and check their return status.
3675#
3676function wait_for_children #children
3677{
3678 rv=0
3679 children=("$@")
3680 for child in "${children[@]}"
3681 do
3682 child_exit=0
3683 wait ${child} || child_exit=$?
3684 if [ $child_exit -ne 0 ]; then
3685 echo "child ${child} failed with ${child_exit}"
3686 rv=1
3687 fi
3688 done
3689 return $rv
3690}
669683c4
AS
3691
3692#
3693# Compare two directory trees recursively in a manner similar to diff(1), but
3694# using rsync. If there are any discrepancies, a summary of the differences are
3695# output and a non-zero error is returned.
3696#
3697# If you're comparing a directory after a ZIL replay, you should set
3698# LIBTEST_DIFF_ZIL_REPLAY=1 or use replay_directory_diff which will cause
3699# directory_diff to ignore mtime changes (the ZIL replay won't fix up mtime
3700# information).
3701#
3702function directory_diff # dir_a dir_b
3703{
3704 dir_a="$1"
3705 dir_b="$2"
3706 zil_replay="${LIBTEST_DIFF_ZIL_REPLAY:-0}"
3707
3708 # If one of the directories doesn't exist, return 2. This is to match the
3709 # semantics of diff.
3710 if ! [ -d "$dir_a" -a -d "$dir_b" ]; then
3711 return 2
3712 fi
3713
3714 # Run rsync with --dry-run --itemize-changes to get something akin to diff
3715 # output, but rsync is far more thorough in detecting differences (diff
3716 # doesn't compare file metadata, and cannot handle special files).
3717 #
3718 # Also make sure to filter out non-user.* xattrs when comparing. On
3719 # SELinux-enabled systems the copied tree will probably have different
3720 # SELinux labels.
3721 args=("-nicaAHX" '--filter=-x! user.*' "--delete")
3722
3723 # NOTE: Quite a few rsync builds do not support --crtimes which would be
3724 # necessary to verify that creation times are being maintained properly.
3725 # Unfortunately because of this we cannot use it unconditionally but we can
3726 # check if this rsync build supports it and use it then. This check is
3727 # based on the same check in the rsync test suite (testsuite/crtimes.test).
3728 #
3729 # We check ctimes even with zil_replay=1 because the ZIL does store
3730 # creation times and we should make sure they match (if the creation times
3731 # do not match there is a "c" entry in one of the columns).
964d4180 3732 if rsync --version | grep -q "[, ] crtimes"; then
669683c4
AS
3733 args+=("--crtimes")
3734 else
964d4180 3735 log_note "This rsync package does not support --crtimes (-N)."
669683c4
AS
3736 fi
3737
3738 # If we are testing a ZIL replay, we need to ignore timestamp changes.
3739 # Unfortunately --no-times doesn't do what we want -- it will still tell
3740 # you if the timestamps don't match but rsync will set the timestamps to
3741 # the current time (leading to an itemised change entry). It's simpler to
3742 # just filter out those lines.
3743 if [ "$zil_replay" -eq 0 ]; then
3744 filter=("cat")
3745 else
3746 # Different rsync versions have different numbers of columns. So just
3747 # require that aside from the first two, all other columns must be
3748 # blank (literal ".") or a timestamp field ("[tT]").
3749 filter=("grep" "-v" '^\..[.Tt]\+ ')
3750 fi
3751
3752 diff="$(rsync "${args[@]}" "$dir_a/" "$dir_b/" | "${filter[@]}")"
3753 rv=0
3754 if [ -n "$diff" ]; then
3755 echo "$diff"
3756 rv=1
3757 fi
3758 return $rv
3759}
3760
3761#
3762# Compare two directory trees recursively, without checking whether the mtimes
3763# match (creation times will be checked if the available rsync binary supports
3764# it). This is necessary for ZIL replay checks (because the ZIL does not
3765# contain mtimes and thus after a ZIL replay, mtimes won't match).
3766#
3767# This is shorthand for LIBTEST_DIFF_ZIL_REPLAY=1 directory_diff <...>.
3768#
3769function replay_directory_diff # dir_a dir_b
3770{
3771 LIBTEST_DIFF_ZIL_REPLAY=1 directory_diff "$@"
669683c4 3772}
598fed7e
AZ
3773
3774#
3775# Put coredumps into $1/core.{basename}
3776#
3777# Output must be saved and passed to pop_coredump_pattern on cleanup
3778#
3779function push_coredump_pattern # dir
3780{
3781 ulimit -c unlimited
5c9f744b 3782 case "$UNAME" in
598fed7e
AZ
3783 Linux)
3784 cat /proc/sys/kernel/core_pattern /proc/sys/kernel/core_uses_pid
3785 echo "$1/core.%e" >/proc/sys/kernel/core_pattern &&
3786 echo 0 >/proc/sys/kernel/core_uses_pid
3787 ;;
3788 FreeBSD)
3789 sysctl -n kern.corefile
3790 sysctl kern.corefile="$1/core.%N" >/dev/null
3791 ;;
3792 *)
3793 # Nothing to output – set only for this shell
3794 coreadm -p "$1/core.%f"
3795 ;;
3796 esac
3797}
3798
3799#
3800# Put coredumps back into the default location
3801#
3802function pop_coredump_pattern
3803{
3804 [ -s "$1" ] || return 0
5c9f744b 3805 case "$UNAME" in
598fed7e
AZ
3806 Linux)
3807 typeset pat pid
3808 { read -r pat; read -r pid; } < "$1"
3809 echo "$pat" >/proc/sys/kernel/core_pattern &&
3810 echo "$pid" >/proc/sys/kernel/core_uses_pid
3811 ;;
3812 FreeBSD)
3813 sysctl kern.corefile="$(<"$1")" >/dev/null
3814 ;;
3815 esac
3816}