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