]> git.proxmox.com Git - mirror_zfs.git/blame - tests/zfs-tests/include/libtest.shlib
Send stream should only list included snaps
[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
7c468940 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
7c9a4292
BB
1934#
1935# Given a pool issue a scrub and verify that no checksum errors are reported.
1936#
1937function verify_pool
1938{
1939 typeset pool=${1:-$TESTPOOL}
1940
1941 log_must zpool scrub $pool
1942 log_must wait_scrubbed $pool
1943
1944 cksum=$(zpool status $pool | awk 'L{print $NF;L=0} /CKSUM$/{L=1}')
1945 if [[ $cksum != 0 ]]; then
1946 log_must zpool status -v
1947 log_fail "Unexpected CKSUM errors found on $pool ($cksum)"
1948 fi
1949}
1950
6bb24f4d
BB
1951#
1952# Given a pool, and this function list all disks in the pool
1953#
1954function get_disklist # pool
1955{
1956 typeset disklist=""
1957
c1d9abf9
JWK
1958 disklist=$(zpool iostat -v $1 | nawk '(NR >4) {print $1}' | \
1959 grep -v "\-\-\-\-\-" | \
85a150ce 1960 egrep -v -e "^(mirror|raidz[1-3]|spare|log|cache|special|dedup)$")
6bb24f4d 1961
c1d9abf9 1962 echo $disklist
6bb24f4d
BB
1963}
1964
3c67d83a
TH
1965#
1966# Given a pool, and this function list all disks in the pool with their full
1967# path (like "/dev/sda" instead of "sda").
1968#
1969function get_disklist_fullpath # pool
1970{
1971 args="-P $1"
1972 get_disklist $args
1973}
1974
1975
1976
6bb24f4d
BB
1977# /**
1978# This function kills a given list of processes after a time period. We use
1979# this in the stress tests instead of STF_TIMEOUT so that we can have processes
1980# run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1981# would be listed as FAIL, which we don't want : we're happy with stress tests
1982# running for a certain amount of time, then finishing.
1983#
1984# @param $1 the time in seconds after which we should terminate these processes
1985# @param $2..$n the processes we wish to terminate.
1986# */
1987function stress_timeout
1988{
1989 typeset -i TIMEOUT=$1
1990 shift
1991 typeset cpids="$@"
1992
1993 log_note "Waiting for child processes($cpids). " \
1994 "It could last dozens of minutes, please be patient ..."
c1d9abf9 1995 log_must sleep $TIMEOUT
6bb24f4d
BB
1996
1997 log_note "Killing child processes after ${TIMEOUT} stress timeout."
1998 typeset pid
1999 for pid in $cpids; do
c1d9abf9 2000 ps -p $pid > /dev/null 2>&1
6bb24f4d 2001 if (($? == 0)); then
c1d9abf9 2002 log_must kill -USR1 $pid
6bb24f4d
BB
2003 fi
2004 done
2005}
2006
2007#
2008# Verify a given hotspare disk is inuse or avail
2009#
2010# Return 0 is pool/disk matches expected state, 1 otherwise
2011#
2012function check_hotspare_state # pool disk state{inuse,avail}
2013{
2014 typeset pool=$1
2015 typeset disk=${2#$DEV_DSKDIR/}
2016 typeset state=$3
2017
2018 cur_state=$(get_device_state $pool $disk "spares")
2019
2020 if [[ $state != ${cur_state} ]]; then
2021 return 1
2022 fi
2023 return 0
2024}
2025
d9daa7ab
DQ
2026#
2027# Wait until a hotspare transitions to a given state or times out.
2028#
2029# Return 0 when pool/disk matches expected state, 1 on timeout.
2030#
2031function wait_hotspare_state # pool disk state timeout
2032{
2033 typeset pool=$1
6fa1e1e7 2034 typeset disk=${2#*$DEV_DSKDIR/}
d9daa7ab
DQ
2035 typeset state=$3
2036 typeset timeout=${4:-60}
2037 typeset -i i=0
2038
2039 while [[ $i -lt $timeout ]]; do
2040 if check_hotspare_state $pool $disk $state; then
2041 return 0
2042 fi
2043
2044 i=$((i+1))
2045 sleep 1
2046 done
2047
2048 return 1
2049}
2050
6bb24f4d
BB
2051#
2052# Verify a given slog disk is inuse or avail
2053#
2054# Return 0 is pool/disk matches expected state, 1 otherwise
2055#
2056function check_slog_state # pool disk state{online,offline,unavail}
2057{
2058 typeset pool=$1
2059 typeset disk=${2#$DEV_DSKDIR/}
2060 typeset state=$3
2061
2062 cur_state=$(get_device_state $pool $disk "logs")
2063
2064 if [[ $state != ${cur_state} ]]; then
2065 return 1
2066 fi
2067 return 0
2068}
2069
2070#
2071# Verify a given vdev disk is inuse or avail
2072#
2073# Return 0 is pool/disk matches expected state, 1 otherwise
2074#
2075function check_vdev_state # pool disk state{online,offline,unavail}
2076{
2077 typeset pool=$1
6fa1e1e7 2078 typeset disk=${2#*$DEV_DSKDIR/}
6bb24f4d
BB
2079 typeset state=$3
2080
2081 cur_state=$(get_device_state $pool $disk)
2082
2083 if [[ $state != ${cur_state} ]]; then
2084 return 1
2085 fi
2086 return 0
2087}
2088
d9daa7ab
DQ
2089#
2090# Wait until a vdev transitions to a given state or times out.
2091#
2092# Return 0 when pool/disk matches expected state, 1 on timeout.
2093#
2094function wait_vdev_state # pool disk state timeout
2095{
2096 typeset pool=$1
6fa1e1e7 2097 typeset disk=${2#*$DEV_DSKDIR/}
d9daa7ab
DQ
2098 typeset state=$3
2099 typeset timeout=${4:-60}
2100 typeset -i i=0
2101
2102 while [[ $i -lt $timeout ]]; do
2103 if check_vdev_state $pool $disk $state; then
2104 return 0
2105 fi
2106
2107 i=$((i+1))
2108 sleep 1
2109 done
2110
2111 return 1
2112}
2113
6bb24f4d
BB
2114#
2115# Check the output of 'zpool status -v <pool>',
2116# and to see if the content of <token> contain the <keyword> specified.
2117#
2118# Return 0 is contain, 1 otherwise
2119#
0ea05c64 2120function check_pool_status # pool token keyword <verbose>
6bb24f4d
BB
2121{
2122 typeset pool=$1
2123 typeset token=$2
2124 typeset keyword=$3
0ea05c64 2125 typeset verbose=${4:-false}
6bb24f4d 2126
0ea05c64
AP
2127 scan=$(zpool status -v "$pool" 2>/dev/null | nawk -v token="$token:" '
2128 ($1==token) {print $0}')
2129 if [[ $verbose == true ]]; then
2130 log_note $scan
2131 fi
2132 echo $scan | grep -i "$keyword" > /dev/null 2>&1
6bb24f4d
BB
2133
2134 return $?
2135}
2136
2137#
0ea05c64 2138# These 6 following functions are instance of check_pool_status()
6bb24f4d
BB
2139# is_pool_resilvering - to check if the pool is resilver in progress
2140# is_pool_resilvered - to check if the pool is resilver completed
2141# is_pool_scrubbing - to check if the pool is scrub in progress
2142# is_pool_scrubbed - to check if the pool is scrub completed
2143# is_pool_scrub_stopped - to check if the pool is scrub stopped
0ea05c64 2144# is_pool_scrub_paused - to check if the pool has scrub paused
a1d477c2
MA
2145# is_pool_removing - to check if the pool is removing a vdev
2146# is_pool_removed - to check if the pool is remove completed
6bb24f4d 2147#
0ea05c64
AP
2148function is_pool_resilvering #pool <verbose>
2149{
2150 check_pool_status "$1" "scan" "resilver in progress since " $2
2151 return $?
2152}
2153
2154function is_pool_resilvered #pool <verbose>
6bb24f4d 2155{
0ea05c64 2156 check_pool_status "$1" "scan" "resilvered " $2
6bb24f4d
BB
2157 return $?
2158}
2159
0ea05c64 2160function is_pool_scrubbing #pool <verbose>
6bb24f4d 2161{
0ea05c64 2162 check_pool_status "$1" "scan" "scrub in progress since " $2
6bb24f4d
BB
2163 return $?
2164}
2165
0ea05c64 2166function is_pool_scrubbed #pool <verbose>
6bb24f4d 2167{
0ea05c64 2168 check_pool_status "$1" "scan" "scrub repaired" $2
6bb24f4d
BB
2169 return $?
2170}
2171
0ea05c64 2172function is_pool_scrub_stopped #pool <verbose>
6bb24f4d 2173{
0ea05c64 2174 check_pool_status "$1" "scan" "scrub canceled" $2
6bb24f4d
BB
2175 return $?
2176}
2177
0ea05c64 2178function is_pool_scrub_paused #pool <verbose>
6bb24f4d 2179{
0ea05c64 2180 check_pool_status "$1" "scan" "scrub paused since " $2
6bb24f4d
BB
2181 return $?
2182}
2183
a1d477c2
MA
2184function is_pool_removing #pool
2185{
2186 check_pool_status "$1" "remove" "in progress since "
2187 return $?
2188}
2189
2190function is_pool_removed #pool
2191{
2192 check_pool_status "$1" "remove" "completed on"
2193 return $?
2194}
2195
ab44e511
JWK
2196function wait_for_degraded
2197{
2198 typeset pool=$1
2199 typeset timeout=${2:-30}
2200 typeset t0=$SECONDS
2201
2202 while :; do
2203 [[ $(get_pool_prop health $pool) == "DEGRADED" ]] && break
2204 log_note "$pool is not yet degraded."
2205 sleep 1
2206 if ((SECONDS - t0 > $timeout)); then
2207 log_note "$pool not degraded after $timeout seconds."
2208 return 1
2209 fi
2210 done
2211
2212 return 0
2213}
2214
6bb24f4d 2215#
4e33ba4c 2216# Use create_pool()/destroy_pool() to clean up the information in
6bb24f4d
BB
2217# in the given disk to avoid slice overlapping.
2218#
2219function cleanup_devices #vdevs
2220{
2221 typeset pool="foopool$$"
2222
2223 if poolexists $pool ; then
2224 destroy_pool $pool
2225 fi
2226
2227 create_pool $pool $@
2228 destroy_pool $pool
2229
2230 return 0
2231}
2232
6bb24f4d
BB
2233#/**
2234# A function to find and locate free disks on a system or from given
2235# disks as the parameter. It works by locating disks that are in use
2236# as swap devices and dump devices, and also disks listed in /etc/vfstab
2237#
2238# $@ given disks to find which are free, default is all disks in
2239# the test system
2240#
2241# @return a string containing the list of available disks
2242#*/
2243function find_disks
2244{
2245 # Trust provided list, no attempt is made to locate unused devices.
2246 if is_linux; then
c1d9abf9 2247 echo "$@"
6bb24f4d
BB
2248 return
2249 fi
2250
2251
2252 sfi=/tmp/swaplist.$$
2253 dmpi=/tmp/dumpdev.$$
2254 max_finddisksnum=${MAX_FINDDISKSNUM:-6}
2255
c1d9abf9
JWK
2256 swap -l > $sfi
2257 dumpadm > $dmpi 2>/dev/null
6bb24f4d
BB
2258
2259# write an awk script that can process the output of format
2260# to produce a list of disks we know about. Note that we have
2261# to escape "$2" so that the shell doesn't interpret it while
2262# we're creating the awk script.
2263# -------------------
c1d9abf9 2264 cat > /tmp/find_disks.awk <<EOF
6bb24f4d
BB
2265#!/bin/nawk -f
2266 BEGIN { FS="."; }
2267
2268 /^Specify disk/{
2269 searchdisks=0;
2270 }
2271
2272 {
2273 if (searchdisks && \$2 !~ "^$"){
2274 split(\$2,arr," ");
2275 print arr[1];
2276 }
2277 }
2278
2279 /^AVAILABLE DISK SELECTIONS:/{
2280 searchdisks=1;
2281 }
2282EOF
2283#---------------------
2284
c1d9abf9
JWK
2285 chmod 755 /tmp/find_disks.awk
2286 disks=${@:-$(echo "" | format -e 2>/dev/null | /tmp/find_disks.awk)}
2287 rm /tmp/find_disks.awk
6bb24f4d
BB
2288
2289 unused=""
2290 for disk in $disks; do
2291 # Check for mounted
c1d9abf9 2292 grep "${disk}[sp]" /etc/mnttab >/dev/null
6bb24f4d
BB
2293 (($? == 0)) && continue
2294 # Check for swap
c1d9abf9 2295 grep "${disk}[sp]" $sfi >/dev/null
6bb24f4d
BB
2296 (($? == 0)) && continue
2297 # check for dump device
c1d9abf9 2298 grep "${disk}[sp]" $dmpi >/dev/null
6bb24f4d
BB
2299 (($? == 0)) && continue
2300 # check to see if this disk hasn't been explicitly excluded
2301 # by a user-set environment variable
c1d9abf9 2302 echo "${ZFS_HOST_DEVICES_IGNORE}" | grep "${disk}" > /dev/null
6bb24f4d
BB
2303 (($? == 0)) && continue
2304 unused_candidates="$unused_candidates $disk"
2305 done
c1d9abf9
JWK
2306 rm $sfi
2307 rm $dmpi
6bb24f4d
BB
2308
2309# now just check to see if those disks do actually exist
2310# by looking for a device pointing to the first slice in
2311# each case. limit the number to max_finddisksnum
2312 count=0
2313 for disk in $unused_candidates; do
2314 if [ -b $DEV_DSKDIR/${disk}s0 ]; then
2315 if [ $count -lt $max_finddisksnum ]; then
2316 unused="$unused $disk"
2317 # do not impose limit if $@ is provided
2318 [[ -z $@ ]] && ((count = count + 1))
2319 fi
2320 fi
2321 done
2322
2323# finally, return our disk list
c1d9abf9 2324 echo $unused
6bb24f4d
BB
2325}
2326
2327#
2328# Add specified user to specified group
2329#
2330# $1 group name
2331# $2 user name
2332# $3 base of the homedir (optional)
2333#
2334function add_user #<group_name> <user_name> <basedir>
2335{
2336 typeset gname=$1
2337 typeset uname=$2
2338 typeset basedir=${3:-"/var/tmp"}
2339
2340 if ((${#gname} == 0 || ${#uname} == 0)); then
2341 log_fail "group name or user name are not defined."
2342 fi
2343
c1d9abf9
JWK
2344 log_must useradd -g $gname -d $basedir/$uname -m $uname
2345 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.profile
2346 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.bash_profile
2347 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.login
6bb24f4d 2348
f74b821a
BB
2349 # Add new users to the same group and the command line utils.
2350 # This allows them to be run out of the original users home
2351 # directory as long as it permissioned to be group readable.
2352 if is_linux; then
c1d9abf9
JWK
2353 cmd_group=$(stat --format="%G" $(which zfs))
2354 log_must usermod -a -G $cmd_group $uname
f74b821a
BB
2355 fi
2356
6bb24f4d
BB
2357 return 0
2358}
2359
2360#
2361# Delete the specified user.
2362#
2363# $1 login name
2364# $2 base of the homedir (optional)
2365#
2366function del_user #<logname> <basedir>
2367{
2368 typeset user=$1
2369 typeset basedir=${2:-"/var/tmp"}
2370
2371 if ((${#user} == 0)); then
2372 log_fail "login name is necessary."
2373 fi
2374
c1d9abf9 2375 if id $user > /dev/null 2>&1; then
29e07af5 2376 log_must_retry "currently used" 5 userdel $user
6bb24f4d
BB
2377 fi
2378
c1d9abf9 2379 [[ -d $basedir/$user ]] && rm -fr $basedir/$user
6bb24f4d
BB
2380
2381 return 0
2382}
2383
2384#
2385# Select valid gid and create specified group.
2386#
2387# $1 group name
2388#
2389function add_group #<group_name>
2390{
2391 typeset group=$1
2392
2393 if ((${#group} == 0)); then
2394 log_fail "group name is necessary."
2395 fi
2396
2397 # Assign 100 as the base gid, a larger value is selected for
2398 # Linux because for many distributions 1000 and under are reserved.
2399 if is_linux; then
6bb24f4d 2400 while true; do
c1d9abf9 2401 groupadd $group > /dev/null 2>&1
6bb24f4d
BB
2402 typeset -i ret=$?
2403 case $ret in
2404 0) return 0 ;;
6bb24f4d
BB
2405 *) return 1 ;;
2406 esac
2407 done
2408 else
2409 typeset -i gid=100
6bb24f4d 2410 while true; do
c1d9abf9 2411 groupadd -g $gid $group > /dev/null 2>&1
6bb24f4d
BB
2412 typeset -i ret=$?
2413 case $ret in
2414 0) return 0 ;;
2415 # The gid is not unique
2416 4) ((gid += 1)) ;;
2417 *) return 1 ;;
2418 esac
2419 done
2420 fi
2421}
2422
2423#
2424# Delete the specified group.
2425#
2426# $1 group name
2427#
2428function del_group #<group_name>
2429{
2430 typeset grp=$1
2431 if ((${#grp} == 0)); then
2432 log_fail "group name is necessary."
2433 fi
2434
2435 if is_linux; then
c1d9abf9 2436 getent group $grp > /dev/null 2>&1
6bb24f4d
BB
2437 typeset -i ret=$?
2438 case $ret in
2439 # Group does not exist.
2440 2) return 0 ;;
2441 # Name already exists as a group name
c1d9abf9 2442 0) log_must groupdel $grp ;;
6bb24f4d
BB
2443 *) return 1 ;;
2444 esac
2445 else
c1d9abf9 2446 groupmod -n $grp $grp > /dev/null 2>&1
6bb24f4d
BB
2447 typeset -i ret=$?
2448 case $ret in
2449 # Group does not exist.
2450 6) return 0 ;;
2451 # Name already exists as a group name
c1d9abf9 2452 9) log_must groupdel $grp ;;
6bb24f4d
BB
2453 *) return 1 ;;
2454 esac
2455 fi
2456
2457 return 0
2458}
2459
2460#
2461# This function will return true if it's safe to destroy the pool passed
2462# as argument 1. It checks for pools based on zvols and files, and also
2463# files contained in a pool that may have a different mountpoint.
2464#
2465function safe_to_destroy_pool { # $1 the pool name
2466
2467 typeset pool=""
2468 typeset DONT_DESTROY=""
2469
2470 # We check that by deleting the $1 pool, we're not
2471 # going to pull the rug out from other pools. Do this
2472 # by looking at all other pools, ensuring that they
2473 # aren't built from files or zvols contained in this pool.
2474
c1d9abf9 2475 for pool in $(zpool list -H -o name)
6bb24f4d
BB
2476 do
2477 ALTMOUNTPOOL=""
2478
2479 # this is a list of the top-level directories in each of the
2480 # files that make up the path to the files the pool is based on
c1d9abf9
JWK
2481 FILEPOOL=$(zpool status -v $pool | grep /$1/ | \
2482 awk '{print $1}')
6bb24f4d
BB
2483
2484 # this is a list of the zvols that make up the pool
c1d9abf9
JWK
2485 ZVOLPOOL=$(zpool status -v $pool | grep "$ZVOL_DEVDIR/$1$" \
2486 | awk '{print $1}')
6bb24f4d
BB
2487
2488 # also want to determine if it's a file-based pool using an
2489 # alternate mountpoint...
c1d9abf9
JWK
2490 POOL_FILE_DIRS=$(zpool status -v $pool | \
2491 grep / | awk '{print $1}' | \
2492 awk -F/ '{print $2}' | grep -v "dev")
6bb24f4d
BB
2493
2494 for pooldir in $POOL_FILE_DIRS
2495 do
c1d9abf9
JWK
2496 OUTPUT=$(zfs list -H -r -o mountpoint $1 | \
2497 grep "${pooldir}$" | awk '{print $1}')
6bb24f4d
BB
2498
2499 ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
2500 done
2501
2502
2503 if [ ! -z "$ZVOLPOOL" ]
2504 then
2505 DONT_DESTROY="true"
2506 log_note "Pool $pool is built from $ZVOLPOOL on $1"
2507 fi
2508
2509 if [ ! -z "$FILEPOOL" ]
2510 then
2511 DONT_DESTROY="true"
2512 log_note "Pool $pool is built from $FILEPOOL on $1"
2513 fi
2514
2515 if [ ! -z "$ALTMOUNTPOOL" ]
2516 then
2517 DONT_DESTROY="true"
2518 log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
2519 fi
2520 done
2521
2522 if [ -z "${DONT_DESTROY}" ]
2523 then
2524 return 0
2525 else
2526 log_note "Warning: it is not safe to destroy $1!"
2527 return 1
2528 fi
2529}
2530
2531#
2532# Get the available ZFS compression options
2533# $1 option type zfs_set|zfs_compress
2534#
2535function get_compress_opts
2536{
2537 typeset COMPRESS_OPTS
2538 typeset GZIP_OPTS="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2539 gzip-6 gzip-7 gzip-8 gzip-9"
2540
2541 if [[ $1 == "zfs_compress" ]] ; then
2542 COMPRESS_OPTS="on lzjb"
2543 elif [[ $1 == "zfs_set" ]] ; then
2544 COMPRESS_OPTS="on off lzjb"
2545 fi
2546 typeset valid_opts="$COMPRESS_OPTS"
c1d9abf9 2547 zfs get 2>&1 | grep gzip >/dev/null 2>&1
6bb24f4d
BB
2548 if [[ $? -eq 0 ]]; then
2549 valid_opts="$valid_opts $GZIP_OPTS"
2550 fi
c1d9abf9 2551 echo "$valid_opts"
6bb24f4d
BB
2552}
2553
2554#
2555# Verify zfs operation with -p option work as expected
2556# $1 operation, value could be create, clone or rename
2557# $2 dataset type, value could be fs or vol
2558# $3 dataset name
2559# $4 new dataset name
2560#
2561function verify_opt_p_ops
2562{
2563 typeset ops=$1
2564 typeset datatype=$2
2565 typeset dataset=$3
2566 typeset newdataset=$4
2567
2568 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2569 log_fail "$datatype is not supported."
2570 fi
2571
2572 # check parameters accordingly
2573 case $ops in
2574 create)
2575 newdataset=$dataset
2576 dataset=""
2577 if [[ $datatype == "vol" ]]; then
2578 ops="create -V $VOLSIZE"
2579 fi
2580 ;;
2581 clone)
2582 if [[ -z $newdataset ]]; then
2583 log_fail "newdataset should not be empty" \
2584 "when ops is $ops."
2585 fi
2586 log_must datasetexists $dataset
2587 log_must snapexists $dataset
2588 ;;
2589 rename)
2590 if [[ -z $newdataset ]]; then
2591 log_fail "newdataset should not be empty" \
2592 "when ops is $ops."
2593 fi
2594 log_must datasetexists $dataset
2595 log_mustnot snapexists $dataset
2596 ;;
2597 *)
2598 log_fail "$ops is not supported."
2599 ;;
2600 esac
2601
2602 # make sure the upper level filesystem does not exist
c7b55e71 2603 destroy_dataset "${newdataset%/*}" "-rRf"
6bb24f4d
BB
2604
2605 # without -p option, operation will fail
c1d9abf9 2606 log_mustnot zfs $ops $dataset $newdataset
6bb24f4d
BB
2607 log_mustnot datasetexists $newdataset ${newdataset%/*}
2608
2609 # with -p option, operation should succeed
c1d9abf9 2610 log_must zfs $ops -p $dataset $newdataset
6bb24f4d
BB
2611 block_device_wait
2612
2613 if ! datasetexists $newdataset ; then
2614 log_fail "-p option does not work for $ops"
2615 fi
2616
2617 # when $ops is create or clone, redo the operation still return zero
2618 if [[ $ops != "rename" ]]; then
c1d9abf9 2619 log_must zfs $ops -p $dataset $newdataset
6bb24f4d
BB
2620 fi
2621
2622 return 0
2623}
2624
2625#
2626# Get configuration of pool
2627# $1 pool name
2628# $2 config name
2629#
2630function get_config
2631{
2632 typeset pool=$1
2633 typeset config=$2
2634 typeset alt_root
2635
2636 if ! poolexists "$pool" ; then
2637 return 1
2638 fi
c1d9abf9 2639 alt_root=$(zpool list -H $pool | awk '{print $NF}')
6bb24f4d 2640 if [[ $alt_root == "-" ]]; then
c1d9abf9 2641 value=$(zdb -C $pool | grep "$config:" | awk -F: \
6bb24f4d
BB
2642 '{print $2}')
2643 else
c1d9abf9 2644 value=$(zdb -e $pool | grep "$config:" | awk -F: \
6bb24f4d
BB
2645 '{print $2}')
2646 fi
2647 if [[ -n $value ]] ; then
2648 value=${value#'}
2649 value=${value%'}
2650 fi
2651 echo $value
2652
2653 return 0
2654}
2655
2656#
2657# Privated function. Random select one of items from arguments.
2658#
2659# $1 count
2660# $2-n string
2661#
2662function _random_get
2663{
2664 typeset cnt=$1
2665 shift
2666
2667 typeset str="$@"
2668 typeset -i ind
2669 ((ind = RANDOM % cnt + 1))
2670
c1d9abf9
JWK
2671 typeset ret=$(echo "$str" | cut -f $ind -d ' ')
2672 echo $ret
6bb24f4d
BB
2673}
2674
2675#
2676# Random select one of item from arguments which include NONE string
2677#
2678function random_get_with_non
2679{
2680 typeset -i cnt=$#
2681 ((cnt =+ 1))
2682
2683 _random_get "$cnt" "$@"
2684}
2685
2686#
2687# Random select one of item from arguments which doesn't include NONE string
2688#
2689function random_get
2690{
2691 _random_get "$#" "$@"
2692}
2693
2694#
2695# Detect if the current system support slog
2696#
2697function verify_slog_support
2698{
3fd3e56c 2699 typeset dir=$TEST_BASE_DIR/disk.$$
6bb24f4d
BB
2700 typeset pool=foo.$$
2701 typeset vdev=$dir/a
2702 typeset sdev=$dir/b
2703
c1d9abf9
JWK
2704 mkdir -p $dir
2705 mkfile $MINVDEVSIZE $vdev $sdev
6bb24f4d
BB
2706
2707 typeset -i ret=0
c1d9abf9 2708 if ! zpool create -n $pool $vdev log $sdev > /dev/null 2>&1; then
6bb24f4d
BB
2709 ret=1
2710 fi
c1d9abf9 2711 rm -r $dir
6bb24f4d
BB
2712
2713 return $ret
2714}
2715
2716#
2717# The function will generate a dataset name with specific length
2718# $1, the length of the name
2719# $2, the base string to construct the name
2720#
2721function gen_dataset_name
2722{
2723 typeset -i len=$1
2724 typeset basestr="$2"
2725 typeset -i baselen=${#basestr}
2726 typeset -i iter=0
2727 typeset l_name=""
2728
2729 if ((len % baselen == 0)); then
2730 ((iter = len / baselen))
2731 else
2732 ((iter = len / baselen + 1))
2733 fi
2734 while ((iter > 0)); do
2735 l_name="${l_name}$basestr"
2736
2737 ((iter -= 1))
2738 done
2739
c1d9abf9 2740 echo $l_name
6bb24f4d
BB
2741}
2742
2743#
2744# Get cksum tuple of dataset
2745# $1 dataset name
2746#
2747# sample zdb output:
2748# Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2749# DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2750# lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2751# fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2752function datasetcksum
2753{
2754 typeset cksum
c1d9abf9
JWK
2755 sync
2756 cksum=$(zdb -vvv $1 | grep "^Dataset $1 \[" | grep "cksum" \
2757 | awk -F= '{print $7}')
2758 echo $cksum
6bb24f4d
BB
2759}
2760
2761#
2762# Get cksum of file
2763# #1 file path
2764#
2765function checksum
2766{
2767 typeset cksum
c1d9abf9
JWK
2768 cksum=$(cksum $1 | awk '{print $1}')
2769 echo $cksum
6bb24f4d
BB
2770}
2771
2772#
2773# Get the given disk/slice state from the specific field of the pool
2774#
2775function get_device_state #pool disk field("", "spares","logs")
2776{
2777 typeset pool=$1
2778 typeset disk=${2#$DEV_DSKDIR/}
2779 typeset field=${3:-$pool}
2780
c1d9abf9
JWK
2781 state=$(zpool status -v "$pool" 2>/dev/null | \
2782 nawk -v device=$disk -v pool=$pool -v field=$field \
6bb24f4d
BB
2783 'BEGIN {startconfig=0; startfield=0; }
2784 /config:/ {startconfig=1}
2785 (startconfig==1) && ($1==field) {startfield=1; next;}
2786 (startfield==1) && ($1==device) {print $2; exit;}
2787 (startfield==1) &&
2788 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2789 echo $state
2790}
2791
2792
2793#
2794# print the given directory filesystem type
2795#
2796# $1 directory name
2797#
2798function get_fstype
2799{
2800 typeset dir=$1
2801
2802 if [[ -z $dir ]]; then
2803 log_fail "Usage: get_fstype <directory>"
2804 fi
2805
2806 #
2807 # $ df -n /
2808 # / : ufs
2809 #
c1d9abf9 2810 df -n $dir | awk '{print $3}'
6bb24f4d
BB
2811}
2812
2813#
2814# Given a disk, label it to VTOC regardless what label was on the disk
2815# $1 disk
2816#
2817function labelvtoc
2818{
2819 typeset disk=$1
2820 if [[ -z $disk ]]; then
2821 log_fail "The disk name is unspecified."
2822 fi
2823 typeset label_file=/var/tmp/labelvtoc.$$
c1d9abf9 2824 typeset arch=$(uname -p)
6bb24f4d
BB
2825
2826 if is_linux; then
2827 log_note "Currently unsupported by the test framework"
2828 return 1
2829 fi
2830
2831 if [[ $arch == "i386" ]]; then
c1d9abf9
JWK
2832 echo "label" > $label_file
2833 echo "0" >> $label_file
2834 echo "" >> $label_file
2835 echo "q" >> $label_file
2836 echo "q" >> $label_file
6bb24f4d 2837
c1d9abf9 2838 fdisk -B $disk >/dev/null 2>&1
6bb24f4d 2839 # wait a while for fdisk finishes
c1d9abf9 2840 sleep 60
6bb24f4d 2841 elif [[ $arch == "sparc" ]]; then
c1d9abf9
JWK
2842 echo "label" > $label_file
2843 echo "0" >> $label_file
2844 echo "" >> $label_file
2845 echo "" >> $label_file
2846 echo "" >> $label_file
2847 echo "q" >> $label_file
6bb24f4d
BB
2848 else
2849 log_fail "unknown arch type"
2850 fi
2851
c1d9abf9 2852 format -e -s -d $disk -f $label_file
6bb24f4d 2853 typeset -i ret_val=$?
c1d9abf9 2854 rm -f $label_file
6bb24f4d
BB
2855 #
2856 # wait the format to finish
2857 #
c1d9abf9 2858 sleep 60
6bb24f4d
BB
2859 if ((ret_val != 0)); then
2860 log_fail "unable to label $disk as VTOC."
2861 fi
2862
2863 return 0
2864}
2865
2866#
2867# check if the system was installed as zfsroot or not
2868# return: 0 ture, otherwise false
2869#
2870function is_zfsroot
2871{
c1d9abf9 2872 df -n / | grep zfs > /dev/null 2>&1
6bb24f4d
BB
2873 return $?
2874}
2875
2876#
2877# get the root filesystem name if it's zfsroot system.
2878#
2879# return: root filesystem name
2880function get_rootfs
2881{
2882 typeset rootfs=""
8aab1218
TS
2883
2884 if ! is_linux; then
2885 rootfs=$(awk '{if ($2 == "/" && $3 == "zfs") print $1}' \
2886 /etc/mnttab)
2887 fi
6bb24f4d
BB
2888 if [[ -z "$rootfs" ]]; then
2889 log_fail "Can not get rootfs"
2890 fi
c1d9abf9 2891 zfs list $rootfs > /dev/null 2>&1
6bb24f4d 2892 if (($? == 0)); then
c1d9abf9 2893 echo $rootfs
6bb24f4d
BB
2894 else
2895 log_fail "This is not a zfsroot system."
2896 fi
2897}
2898
2899#
2900# get the rootfs's pool name
2901# return:
2902# rootpool name
2903#
2904function get_rootpool
2905{
2906 typeset rootfs=""
2907 typeset rootpool=""
8aab1218
TS
2908
2909 if ! is_linux; then
2910 rootfs=$(awk '{if ($2 == "/" && $3 =="zfs") print $1}' \
2911 /etc/mnttab)
2912 fi
6bb24f4d
BB
2913 if [[ -z "$rootfs" ]]; then
2914 log_fail "Can not get rootpool"
2915 fi
c1d9abf9 2916 zfs list $rootfs > /dev/null 2>&1
6bb24f4d 2917 if (($? == 0)); then
c1d9abf9
JWK
2918 rootpool=`echo $rootfs | awk -F\/ '{print $1}'`
2919 echo $rootpool
6bb24f4d
BB
2920 else
2921 log_fail "This is not a zfsroot system."
2922 fi
2923}
2924
6bb24f4d
BB
2925#
2926# Get the package name
2927#
2928function get_package_name
2929{
2930 typeset dirpath=${1:-$STC_NAME}
2931
2932 echo "SUNWstc-${dirpath}" | /usr/bin/sed -e "s/\//-/g"
2933}
2934
2935#
2936# Get the word numbers from a string separated by white space
2937#
2938function get_word_count
2939{
c1d9abf9 2940 echo $1 | wc -w
6bb24f4d
BB
2941}
2942
2943#
2944# To verify if the require numbers of disks is given
2945#
2946function verify_disk_count
2947{
2948 typeset -i min=${2:-1}
2949
2950 typeset -i count=$(get_word_count "$1")
2951
2952 if ((count < min)); then
2953 log_untested "A minimum of $min disks is required to run." \
2954 " You specified $count disk(s)"
2955 fi
2956}
2957
2958function ds_is_volume
2959{
2960 typeset type=$(get_prop type $1)
2961 [[ $type = "volume" ]] && return 0
2962 return 1
2963}
2964
2965function ds_is_filesystem
2966{
2967 typeset type=$(get_prop type $1)
2968 [[ $type = "filesystem" ]] && return 0
2969 return 1
2970}
2971
2972function ds_is_snapshot
2973{
2974 typeset type=$(get_prop type $1)
2975 [[ $type = "snapshot" ]] && return 0
2976 return 1
2977}
2978
2979#
2980# Check if Trusted Extensions are installed and enabled
2981#
2982function is_te_enabled
2983{
c1d9abf9 2984 svcs -H -o state labeld 2>/dev/null | grep "enabled"
6bb24f4d
BB
2985 if (($? != 0)); then
2986 return 1
2987 else
2988 return 0
2989 fi
2990}
2991
2992# Utility function to determine if a system has multiple cpus.
2993function is_mp
2994{
2995 if is_linux; then
c1d9abf9 2996 (($(nproc) > 1))
6bb24f4d 2997 else
c1d9abf9 2998 (($(psrinfo | wc -l) > 1))
6bb24f4d
BB
2999 fi
3000
3001 return $?
3002}
3003
3004function get_cpu_freq
3005{
3006 if is_linux; then
c1d9abf9 3007 lscpu | awk '/CPU MHz/ { print $3 }'
6bb24f4d 3008 else
c1d9abf9 3009 psrinfo -v 0 | awk '/processor operates at/ {print $6}'
6bb24f4d
BB
3010 fi
3011}
3012
3013# Run the given command as the user provided.
3014function user_run
3015{
3016 typeset user=$1
3017 shift
3018
f74b821a 3019 log_note "user:$user $@"
52775712 3020 eval su - \$user -c \"$@\" > $TEST_BASE_DIR/out 2>$TEST_BASE_DIR/err
6bb24f4d
BB
3021 return $?
3022}
3023
3024#
3025# Check if the pool contains the specified vdevs
3026#
3027# $1 pool
3028# $2..n <vdev> ...
3029#
3030# Return 0 if the vdevs are contained in the pool, 1 if any of the specified
3031# vdevs is not in the pool, and 2 if pool name is missing.
3032#
3033function vdevs_in_pool
3034{
3035 typeset pool=$1
3036 typeset vdev
3037
3038 if [[ -z $pool ]]; then
3039 log_note "Missing pool name."
3040 return 2
3041 fi
3042
3043 shift
3044
7c9a4292
BB
3045 # We could use 'zpool list' to only get the vdevs of the pool but we
3046 # can't reference a mirror/raidz vdev using its ID (i.e mirror-0),
3047 # therefore we use the 'zpool status' output.
c1d9abf9 3048 typeset tmpfile=$(mktemp)
7c9a4292 3049 zpool status -v "$pool" | grep -A 1000 "config:" >$tmpfile
6bb24f4d 3050 for vdev in $@; do
c1d9abf9 3051 grep -w ${vdev##*/} $tmpfile >/dev/null 2>&1
6bb24f4d
BB
3052 [[ $? -ne 0 ]] && return 1
3053 done
3054
c1d9abf9 3055 rm -f $tmpfile
6bb24f4d
BB
3056
3057 return 0;
3058}
3059
679d73e9
JWK
3060function get_max
3061{
3062 typeset -l i max=$1
3063 shift
3064
3065 for i in "$@"; do
3066 max=$(echo $((max > i ? max : i)))
3067 done
3068
3069 echo $max
3070}
3071
3072function get_min
3073{
3074 typeset -l i min=$1
3075 shift
3076
3077 for i in "$@"; do
3078 min=$(echo $((min < i ? min : i)))
3079 done
3080
3081 echo $min
3082}
3083
a7004725
DK
3084#
3085# Generate a random number between 1 and the argument.
3086#
3087function random
3088{
3089 typeset max=$1
3090 echo $(( ($RANDOM % $max) + 1 ))
3091}
3092
3093# Write data that can be compressed into a directory
3094function write_compressible
3095{
3096 typeset dir=$1
3097 typeset megs=$2
3098 typeset nfiles=${3:-1}
3099 typeset bs=${4:-1024k}
3100 typeset fname=${5:-file}
3101
3102 [[ -d $dir ]] || log_fail "No directory: $dir"
3103
3104 # Under Linux fio is not currently used since its behavior can
3105 # differ significantly across versions. This includes missing
3106 # command line options and cases where the --buffer_compress_*
3107 # options fail to behave as expected.
3108 if is_linux; then
3109 typeset file_bytes=$(to_bytes $megs)
3110 typeset bs_bytes=4096
3111 typeset blocks=$(($file_bytes / $bs_bytes))
3112
3113 for (( i = 0; i < $nfiles; i++ )); do
3114 truncate -s $file_bytes $dir/$fname.$i
3115
3116 # Write every third block to get 66% compression.
3117 for (( j = 0; j < $blocks; j += 3 )); do
3118 dd if=/dev/urandom of=$dir/$fname.$i \
3119 seek=$j bs=$bs_bytes count=1 \
3120 conv=notrunc >/dev/null 2>&1
3121 done
3122 done
3123 else
3124 log_must eval "fio \
3125 --name=job \
3126 --fallocate=0 \
3127 --minimal \
3128 --randrepeat=0 \
3129 --buffer_compress_percentage=66 \
3130 --buffer_compress_chunk=4096 \
3131 --directory=$dir \
3132 --numjobs=$nfiles \
3133 --nrfiles=$nfiles \
3134 --rw=write \
3135 --bs=$bs \
3136 --filesize=$megs \
3137 --filename_format='$fname.\$jobnum' >/dev/null"
3138 fi
3139}
3140
3141function get_objnum
3142{
3143 typeset pathname=$1
3144 typeset objnum
3145
3146 [[ -e $pathname ]] || log_fail "No such file or directory: $pathname"
3147 objnum=$(stat -c %i $pathname)
3148 echo $objnum
3149}
3150
1de321e6 3151#
bec1067d 3152# Sync data to the pool
1de321e6
JX
3153#
3154# $1 pool name
bec1067d 3155# $2 boolean to force uberblock (and config including zpool cache file) update
1de321e6 3156#
bec1067d 3157function sync_pool #pool <force>
1de321e6
JX
3158{
3159 typeset pool=${1:-$TESTPOOL}
bec1067d 3160 typeset force=${2:-false}
1de321e6 3161
bec1067d
AP
3162 if [[ $force == true ]]; then
3163 log_must zpool sync -f $pool
3164 else
3165 log_must zpool sync $pool
3166 fi
3167
3168 return 0
1de321e6 3169}
d834b9ce
GM
3170
3171#
3172# Wait for zpool 'freeing' property drops to zero.
3173#
3174# $1 pool name
3175#
3176function wait_freeing #pool
3177{
3178 typeset pool=${1:-$TESTPOOL}
3179 while true; do
c1d9abf9
JWK
3180 [[ "0" == "$(zpool list -Ho freeing $pool)" ]] && break
3181 log_must sleep 1
d834b9ce
GM
3182 done
3183}
7a4500a1 3184
dddef7d6 3185#
3186# Wait for every device replace operation to complete
3187#
3188# $1 pool name
3189#
3190function wait_replacing #pool
3191{
3192 typeset pool=${1:-$TESTPOOL}
3193 while true; do
3194 [[ "" == "$(zpool status $pool |
3195 awk '/replacing-[0-9]+/ {print $1}')" ]] && break
3196 log_must sleep 1
3197 done
3198}
3199
bf95a000
TH
3200#
3201# Wait for a pool to be scrubbed
3202#
3203# $1 pool name
3204# $2 number of seconds to wait (optional)
3205#
3206# Returns true when pool has been scrubbed, or false if there's a timeout or if
3207# no scrub was done.
3208#
3209function wait_scrubbed
3210{
3211 typeset pool=${1:-$TESTPOOL}
7c468940
TC
3212 while true ; do
3213 is_pool_scrubbed $pool && break
3214 log_must sleep 1
bf95a000 3215 done
bf95a000
TH
3216}
3217
639b1894
TH
3218# Backup the zed.rc in our test directory so that we can edit it for our test.
3219#
3220# Returns: Backup file name. You will need to pass this to zed_rc_restore().
3221function zed_rc_backup
3222{
3223 zedrc_backup="$(mktemp)"
3224 cp $ZEDLET_DIR/zed.rc $zedrc_backup
3225 echo $zedrc_backup
3226}
3227
3228function zed_rc_restore
3229{
3230 mv $1 $ZEDLET_DIR/zed.rc
3231}
3232
95401cb6
BB
3233#
3234# Setup custom environment for the ZED.
3235#
bf95a000 3236# $@ Optional list of zedlets to run under zed.
95401cb6
BB
3237function zed_setup
3238{
3239 if ! is_linux; then
3240 return
3241 fi
3242
3243 if [[ ! -d $ZEDLET_DIR ]]; then
3244 log_must mkdir $ZEDLET_DIR
3245 fi
3246
3247 if [[ ! -e $VDEVID_CONF ]]; then
3248 log_must touch $VDEVID_CONF
3249 fi
3250
3251 if [[ -e $VDEVID_CONF_ETC ]]; then
3252 log_fail "Must not have $VDEVID_CONF_ETC file present on system"
3253 fi
bf95a000 3254 EXTRA_ZEDLETS=$@
95401cb6
BB
3255
3256 # Create a symlink for /etc/zfs/vdev_id.conf file.
3257 log_must ln -s $VDEVID_CONF $VDEVID_CONF_ETC
3258
3259 # Setup minimal ZED configuration. Individual test cases should
3260 # add additional ZEDLETs as needed for their specific test.
3f03fc8d
BB
3261 log_must cp ${ZEDLET_ETC_DIR}/zed.rc $ZEDLET_DIR
3262 log_must cp ${ZEDLET_ETC_DIR}/zed-functions.sh $ZEDLET_DIR
95401cb6 3263
bf95a000
TH
3264 # Scripts must only be user writable.
3265 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3266 saved_umask=$(umask)
3267 log_must umask 0022
3268 for i in $EXTRA_ZEDLETS ; do
3269 log_must cp ${ZEDLET_LIBEXEC_DIR}/$i $ZEDLET_DIR
3270 done
3271 log_must umask $saved_umask
3272 fi
3273
3f03fc8d
BB
3274 # Customize the zed.rc file to enable the full debug log.
3275 log_must sed -i '/\#ZED_DEBUG_LOG=.*/d' $ZEDLET_DIR/zed.rc
d5e024cb 3276 echo "ZED_DEBUG_LOG=$ZED_DEBUG_LOG" >>$ZEDLET_DIR/zed.rc
3f03fc8d 3277
95401cb6
BB
3278}
3279
3280#
3281# Cleanup custom ZED environment.
3282#
bf95a000 3283# $@ Optional list of zedlets to remove from our test zed.d directory.
95401cb6
BB
3284function zed_cleanup
3285{
3286 if ! is_linux; then
3287 return
3288 fi
bf95a000 3289 EXTRA_ZEDLETS=$@
95401cb6
BB
3290
3291 log_must rm -f ${ZEDLET_DIR}/zed.rc
3292 log_must rm -f ${ZEDLET_DIR}/zed-functions.sh
3293 log_must rm -f ${ZEDLET_DIR}/all-syslog.sh
3f03fc8d 3294 log_must rm -f ${ZEDLET_DIR}/all-debug.sh
95401cb6 3295 log_must rm -f ${ZEDLET_DIR}/state
bf95a000
TH
3296
3297 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3298 for i in $EXTRA_ZEDLETS ; do
3299 log_must rm -f ${ZEDLET_DIR}/$i
3300 done
3301 fi
d5e024cb
BB
3302 log_must rm -f $ZED_LOG
3303 log_must rm -f $ZED_DEBUG_LOG
95401cb6
BB
3304 log_must rm -f $VDEVID_CONF_ETC
3305 log_must rm -f $VDEVID_CONF
3306 rmdir $ZEDLET_DIR
3307}
3308
7a4500a1
SV
3309#
3310# Check if ZED is currently running, if not start ZED.
3311#
3312function zed_start
3313{
95401cb6
BB
3314 if ! is_linux; then
3315 return
3316 fi
7a4500a1 3317
95401cb6
BB
3318 # ZEDLET_DIR=/var/tmp/zed
3319 if [[ ! -d $ZEDLET_DIR ]]; then
3320 log_must mkdir $ZEDLET_DIR
3321 fi
7a4500a1 3322
95401cb6
BB
3323 # Verify the ZED is not already running.
3324 pgrep -x zed > /dev/null
3325 if (($? == 0)); then
3326 log_fail "ZED already running"
7a4500a1 3327 fi
95401cb6
BB
3328
3329 log_note "Starting ZED"
3330 # run ZED in the background and redirect foreground logging
d5e024cb
BB
3331 # output to $ZED_LOG.
3332 log_must truncate -s 0 $ZED_DEBUG_LOG
bf95a000 3333 log_must eval "zed -vF -d $ZEDLET_DIR -p $ZEDLET_DIR/zed.pid -P $PATH" \
d5e024cb 3334 "-s $ZEDLET_DIR/state 2>$ZED_LOG &"
3f03fc8d
BB
3335
3336 return 0
7a4500a1
SV
3337}
3338
3339#
3340# Kill ZED process
3341#
3342function zed_stop
3343{
95401cb6
BB
3344 if ! is_linux; then
3345 return
3346 fi
3347
3f03fc8d 3348 log_note "Stopping ZED"
95401cb6 3349 if [[ -f ${ZEDLET_DIR}/zed.pid ]]; then
58aeb87a 3350 zedpid=$(<${ZEDLET_DIR}/zed.pid)
d5e024cb 3351 kill $zedpid
4e9b1569 3352 while ps -p $zedpid > /dev/null; do
3353 sleep 1
3354 done
d5e024cb 3355 rm -f ${ZEDLET_DIR}/zed.pid
7a4500a1 3356 fi
3f03fc8d 3357 return 0
7a4500a1 3358}
8c54ddd3 3359
4e9b1569 3360#
3361# Drain all zevents
3362#
3363function zed_events_drain
3364{
3365 while [ $(zpool events -H | wc -l) -ne 0 ]; do
3366 sleep 1
3367 zpool events -c >/dev/null
3368 done
3369}
3370
639b1894
TH
3371# Set a variable in zed.rc to something, un-commenting it in the process.
3372#
3373# $1 variable
3374# $2 value
3375function zed_rc_set
3376{
3377 var="$1"
3378 val="$2"
3379 # Remove the line
3380 cmd="'/$var/d'"
3381 eval sed -i $cmd $ZEDLET_DIR/zed.rc
3382
3383 # Add it at the end
3384 echo "$var=$val" >> $ZEDLET_DIR/zed.rc
3385}
3386
3387
8c54ddd3
BB
3388#
3389# Check is provided device is being active used as a swap device.
3390#
3391function is_swap_inuse
3392{
3393 typeset device=$1
3394
3395 if [[ -z $device ]] ; then
3396 log_note "No device specified."
3397 return 1
3398 fi
3399
3400 if is_linux; then
3401 swapon -s | grep -w $(readlink -f $device) > /dev/null 2>&1
3402 else
3403 swap -l | grep -w $device > /dev/null 2>&1
3404 fi
3405
3406 return $?
3407}
3408
3409#
3410# Setup a swap device using the provided device.
3411#
3412function swap_setup
3413{
3414 typeset swapdev=$1
3415
3416 if is_linux; then
c7a7601c 3417 log_must eval "mkswap $swapdev > /dev/null 2>&1"
8c54ddd3
BB
3418 log_must swapon $swapdev
3419 else
3420 log_must swap -a $swapdev
3421 fi
3422
3423 return 0
3424}
3425
3426#
3427# Cleanup a swap device on the provided device.
3428#
3429function swap_cleanup
3430{
3431 typeset swapdev=$1
3432
3433 if is_swap_inuse $swapdev; then
3434 if is_linux; then
3435 log_must swapoff $swapdev
3436 else
3437 log_must swap -d $swapdev
3438 fi
3439 fi
3440
3441 return 0
3442}
379ca9cf
OF
3443
3444#
3445# Set a global system tunable (64-bit value)
3446#
3447# $1 tunable name
3448# $2 tunable values
3449#
3450function set_tunable64
3451{
3452 set_tunable_impl "$1" "$2" Z
3453}
3454
3455#
3456# Set a global system tunable (32-bit value)
3457#
3458# $1 tunable name
3459# $2 tunable values
3460#
3461function set_tunable32
3462{
3463 set_tunable_impl "$1" "$2" W
3464}
3465
3466function set_tunable_impl
3467{
3468 typeset tunable="$1"
3469 typeset value="$2"
3470 typeset mdb_cmd="$3"
3471 typeset module="${4:-zfs}"
3472
3473 [[ -z "$tunable" ]] && return 1
3474 [[ -z "$value" ]] && return 1
3475 [[ -z "$mdb_cmd" ]] && return 1
3476
3477 case "$(uname)" in
3478 Linux)
3479 typeset zfs_tunables="/sys/module/$module/parameters"
3480 [[ -w "$zfs_tunables/$tunable" ]] || return 1
3481 echo -n "$value" > "$zfs_tunables/$tunable"
3482 return "$?"
3483 ;;
3484 SunOS)
3485 [[ "$module" -eq "zfs" ]] || return 1
3486 echo "${tunable}/${mdb_cmd}0t${value}" | mdb -kw
3487 return "$?"
3488 ;;
3489 esac
3490}
3491
3492#
3493# Get a global system tunable
3494#
3495# $1 tunable name
3496#
3497function get_tunable
3498{
3499 get_tunable_impl "$1"
3500}
3501
3502function get_tunable_impl
3503{
3504 typeset tunable="$1"
3505 typeset module="${2:-zfs}"
3506
3507 [[ -z "$tunable" ]] && return 1
3508
3509 case "$(uname)" in
3510 Linux)
3511 typeset zfs_tunables="/sys/module/$module/parameters"
3512 [[ -f "$zfs_tunables/$tunable" ]] || return 1
3513 cat $zfs_tunables/$tunable
3514 return "$?"
3515 ;;
3516 SunOS)
3517 [[ "$module" -eq "zfs" ]] || return 1
3518 ;;
3519 esac
3520
3521 return 1
3522}
a1d477c2
MA
3523
3524#
3525# Prints the current time in seconds since UNIX Epoch.
3526#
3527function current_epoch
3528{
3529 printf '%(%s)T'
3530}
3531
3532#
3533# Get decimal value of global uint32_t variable using mdb.
3534#
3535function mdb_get_uint32
3536{
3537 typeset variable=$1
3538 typeset value
3539
3540 value=$(mdb -k -e "$variable/X | ::eval .=U")
3541 if [[ $? -ne 0 ]]; then
3542 log_fail "Failed to get value of '$variable' from mdb."
3543 return 1
3544 fi
3545
3546 echo $value
3547 return 0
3548}
3549
3550#
3551# Set global uint32_t variable to a decimal value using mdb.
3552#
3553function mdb_set_uint32
3554{
3555 typeset variable=$1
3556 typeset value=$2
3557
3558 mdb -kw -e "$variable/W 0t$value" > /dev/null
3559 if [[ $? -ne 0 ]]; then
3560 echo "Failed to set '$variable' to '$value' in mdb."
3561 return 1
3562 fi
3563
3564 return 0
3565}
d2734cce
SD
3566
3567#
3568# Set global scalar integer variable to a hex value using mdb.
3569# Note: Target should have CTF data loaded.
3570#
3571function mdb_ctf_set_int
3572{
3573 typeset variable=$1
3574 typeset value=$2
3575
3576 mdb -kw -e "$variable/z $value" > /dev/null
3577 if [[ $? -ne 0 ]]; then
3578 echo "Failed to set '$variable' to '$value' in mdb."
3579 return 1
3580 fi
3581
3582 return 0
3583}