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