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