1 #
2 # CDDL HEADER START
3 #
4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
7 #
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
12 #
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
18 #
19 # CDDL HEADER END
20 #
21
22 #
23 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
25 # Copyright (c) 2012, 2016 by Delphix. All rights reserved.
26 # Copyright 2016 Nexenta Systems, Inc.
27 # Copyright (c) 2017 Datto Inc.
28 #
29
30 . ${STF_TOOLS}/contrib/include/logapi.shlib
31
32 # Determine whether a dataset is mounted
33 #
34 # $1 dataset name
35 # $2 filesystem type; optional - defaulted to zfs
36 #
37 # Return 0 if dataset is mounted; 1 if unmounted; 2 on error
38
39 function ismounted
40 {
41 typeset fstype=$2
42 [[ -z $fstype ]] && fstype=zfs
43 typeset out dir name ret
44
45 case $fstype in
46 zfs)
47 if [[ "$1" == "/"* ]] ; then
48 for out in $(zfs mount | awk '{print $2}'); do
49 [[ $1 == $out ]] && return 0
50 done
51 else
52 for out in $(zfs mount | awk '{print $1}'); do
53 [[ $1 == $out ]] && return 0
54 done
55 fi
56 ;;
57 ufs|nfs)
58 out=$(df -F $fstype $1 2>/dev/null)
59 ret=$?
60 (($ret != 0)) && return $ret
61
62 dir=${out%%\(*}
63 dir=${dir%% *}
64 name=${out##*\(}
65 name=${name%%\)*}
66 name=${name%% *}
67
68 [[ "$1" == "$dir" || "$1" == "$name" ]] && return 0
69 ;;
70 esac
71
72 return 1
73 }
74
75 # Return 0 if a dataset is mounted; 1 otherwise
76 #
77 # $1 dataset name
78 # $2 filesystem type; optional - defaulted to zfs
79
80 function mounted
81 {
82 ismounted $1 $2
83 (($? == 0)) && return 0
84 return 1
85 }
86
87 # Return 0 if a dataset is unmounted; 1 otherwise
88 #
89 # $1 dataset name
90 # $2 filesystem type; optional - defaulted to zfs
91
92 function unmounted
93 {
94 ismounted $1 $2
95 (($? == 1)) && return 0
96 return 1
97 }
98
99 # split line on ","
100 #
101 # $1 - line to split
102
103 function splitline
104 {
105 echo $1 | sed "s/,/ /g"
106 }
107
108 function default_setup
109 {
110 default_setup_noexit "$@"
111
112 log_pass
113 }
114
115 #
116 # Given a list of disks, setup storage pools and datasets.
117 #
118 function default_setup_noexit
119 {
120 typeset disklist=$1
121 typeset container=$2
122 typeset volume=$3
123
124 if is_global_zone; then
125 if poolexists $TESTPOOL ; then
126 destroy_pool $TESTPOOL
127 fi
128 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
129 log_must zpool create -f $TESTPOOL $disklist
130 else
131 reexport_pool
132 fi
133
134 rm -rf $TESTDIR || log_unresolved Could not remove $TESTDIR
135 mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR
136
137 log_must zfs create $TESTPOOL/$TESTFS
138 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
139
140 if [[ -n $container ]]; then
141 rm -rf $TESTDIR1 || \
142 log_unresolved Could not remove $TESTDIR1
143 mkdir -p $TESTDIR1 || \
144 log_unresolved Could not create $TESTDIR1
145
146 log_must zfs create $TESTPOOL/$TESTCTR
147 log_must zfs set canmount=off $TESTPOOL/$TESTCTR
148 log_must zfs create $TESTPOOL/$TESTCTR/$TESTFS1
149 log_must zfs set mountpoint=$TESTDIR1 \
150 $TESTPOOL/$TESTCTR/$TESTFS1
151 fi
152
153 if [[ -n $volume ]]; then
154 if is_global_zone ; then
155 log_must zfs create -V $VOLSIZE $TESTPOOL/$TESTVOL
156 else
157 log_must zfs create $TESTPOOL/$TESTVOL
158 fi
159 fi
160 }
161
162 #
163 # Given a list of disks, setup a storage pool, file system and
164 # a container.
165 #
166 function default_container_setup
167 {
168 typeset disklist=$1
169
170 default_setup "$disklist" "true"
171 }
172
173 #
174 # Given a list of disks, setup a storage pool,file system
175 # and a volume.
176 #
177 function default_volume_setup
178 {
179 typeset disklist=$1
180
181 default_setup "$disklist" "" "true"
182 }
183
184 #
185 # Given a list of disks, setup a storage pool,file system,
186 # a container and a volume.
187 #
188 function default_container_volume_setup
189 {
190 typeset disklist=$1
191
192 default_setup "$disklist" "true" "true"
193 }
194
195 #
196 # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
197 # filesystem
198 #
199 # $1 Existing filesystem or volume name. Default, $TESTPOOL/$TESTFS
200 # $2 snapshot name. Default, $TESTSNAP
201 #
202 function create_snapshot
203 {
204 typeset fs_vol=${1:-$TESTPOOL/$TESTFS}
205 typeset snap=${2:-$TESTSNAP}
206
207 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
208 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
209
210 if snapexists $fs_vol@$snap; then
211 log_fail "$fs_vol@$snap already exists."
212 fi
213 datasetexists $fs_vol || \
214 log_fail "$fs_vol must exist."
215
216 log_must zfs snapshot $fs_vol@$snap
217 }
218
219 #
220 # Create a clone from a snapshot, default clone name is $TESTCLONE.
221 #
222 # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
223 # $2 Clone name, $TESTPOOL/$TESTCLONE is default.
224 #
225 function create_clone # snapshot clone
226 {
227 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
228 typeset clone=${2:-$TESTPOOL/$TESTCLONE}
229
230 [[ -z $snap ]] && \
231 log_fail "Snapshot name is undefined."
232 [[ -z $clone ]] && \
233 log_fail "Clone name is undefined."
234
235 log_must zfs clone $snap $clone
236 }
237
238 #
239 # Create a bookmark of the given snapshot. Defaultly create a bookmark on
240 # filesystem.
241 #
242 # $1 Existing filesystem or volume name. Default, $TESTFS
243 # $2 Existing snapshot name. Default, $TESTSNAP
244 # $3 bookmark name. Default, $TESTBKMARK
245 #
246 function create_bookmark
247 {
248 typeset fs_vol=${1:-$TESTFS}
249 typeset snap=${2:-$TESTSNAP}
250 typeset bkmark=${3:-$TESTBKMARK}
251
252 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
253 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
254 [[ -z $bkmark ]] && log_fail "Bookmark's name is undefined."
255
256 if bkmarkexists $fs_vol#$bkmark; then
257 log_fail "$fs_vol#$bkmark already exists."
258 fi
259 datasetexists $fs_vol || \
260 log_fail "$fs_vol must exist."
261 snapexists $fs_vol@$snap || \
262 log_fail "$fs_vol@$snap must exist."
263
264 log_must zfs bookmark $fs_vol@$snap $fs_vol#$bkmark
265 }
266
267 function default_mirror_setup
268 {
269 default_mirror_setup_noexit $1 $2 $3
270
271 log_pass
272 }
273
274 #
275 # Given a pair of disks, set up a storage pool and dataset for the mirror
276 # @parameters: $1 the primary side of the mirror
277 # $2 the secondary side of the mirror
278 # @uses: ZPOOL ZFS TESTPOOL TESTFS
279 function default_mirror_setup_noexit
280 {
281 readonly func="default_mirror_setup_noexit"
282 typeset primary=$1
283 typeset secondary=$2
284
285 [[ -z $primary ]] && \
286 log_fail "$func: No parameters passed"
287 [[ -z $secondary ]] && \
288 log_fail "$func: No secondary partition passed"
289 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
290 log_must zpool create -f $TESTPOOL mirror $@
291 log_must zfs create $TESTPOOL/$TESTFS
292 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
293 }
294
295 #
296 # create a number of mirrors.
297 # We create a number($1) of 2 way mirrors using the pairs of disks named
298 # on the command line. These mirrors are *not* mounted
299 # @parameters: $1 the number of mirrors to create
300 # $... the devices to use to create the mirrors on
301 # @uses: ZPOOL ZFS TESTPOOL
302 function setup_mirrors
303 {
304 typeset -i nmirrors=$1
305
306 shift
307 while ((nmirrors > 0)); do
308 log_must test -n "$1" -a -n "$2"
309 [[ -d /$TESTPOOL$nmirrors ]] && rm -rf /$TESTPOOL$nmirrors
310 log_must zpool create -f $TESTPOOL$nmirrors mirror $1 $2
311 shift 2
312 ((nmirrors = nmirrors - 1))
313 done
314 }
315
316 #
317 # create a number of raidz pools.
318 # We create a number($1) of 2 raidz pools using the pairs of disks named
319 # on the command line. These pools are *not* mounted
320 # @parameters: $1 the number of pools to create
321 # $... the devices to use to create the pools on
322 # @uses: ZPOOL ZFS TESTPOOL
323 function setup_raidzs
324 {
325 typeset -i nraidzs=$1
326
327 shift
328 while ((nraidzs > 0)); do
329 log_must test -n "$1" -a -n "$2"
330 [[ -d /$TESTPOOL$nraidzs ]] && rm -rf /$TESTPOOL$nraidzs
331 log_must zpool create -f $TESTPOOL$nraidzs raidz $1 $2
332 shift 2
333 ((nraidzs = nraidzs - 1))
334 done
335 }
336
337 #
338 # Destroy the configured testpool mirrors.
339 # the mirrors are of the form ${TESTPOOL}{number}
340 # @uses: ZPOOL ZFS TESTPOOL
341 function destroy_mirrors
342 {
343 default_cleanup_noexit
344
345 log_pass
346 }
347
348 #
349 # Given a minimum of two disks, set up a storage pool and dataset for the raid-z
350 # $1 the list of disks
351 #
352 function default_raidz_setup
353 {
354 typeset disklist="$*"
355 disks=(${disklist[*]})
356
357 if [[ ${#disks[*]} -lt 2 ]]; then
358 log_fail "A raid-z requires a minimum of two disks."
359 fi
360
361 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
362 log_must zpool create -f $TESTPOOL raidz $1 $2 $3
363 log_must zfs create $TESTPOOL/$TESTFS
364 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
365
366 log_pass
367 }
368
369 #
370 # Common function used to cleanup storage pools and datasets.
371 #
372 # Invoked at the start of the test suite to ensure the system
373 # is in a known state, and also at the end of each set of
374 # sub-tests to ensure errors from one set of tests doesn't
375 # impact the execution of the next set.
376
377 function default_cleanup
378 {
379 default_cleanup_noexit
380
381 log_pass
382 }
383
384 function default_cleanup_noexit
385 {
386 typeset exclude=""
387 typeset pool=""
388 #
389 # Destroying the pool will also destroy any
390 # filesystems it contains.
391 #
392 if is_global_zone; then
393 zfs unmount -a > /dev/null 2>&1
394 exclude=`eval echo \"'(${KEEP})'\"`
395 ALL_POOLS=$(zpool list -H -o name \
396 | grep -v "$NO_POOLS" | egrep -v "$exclude")
397 # Here, we loop through the pools we're allowed to
398 # destroy, only destroying them if it's safe to do
399 # so.
400 while [ ! -z ${ALL_POOLS} ]
401 do
402 for pool in ${ALL_POOLS}
403 do
404 if safe_to_destroy_pool $pool ;
405 then
406 destroy_pool $pool
407 fi
408 ALL_POOLS=$(zpool list -H -o name \
409 | grep -v "$NO_POOLS" \
410 | egrep -v "$exclude")
411 done
412 done
413
414 zfs mount -a
415 else
416 typeset fs=""
417 for fs in $(zfs list -H -o name \
418 | grep "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
419 datasetexists $fs && \
420 log_must zfs destroy -Rf $fs
421 done
422
423 # Need cleanup here to avoid garbage dir left.
424 for fs in $(zfs list -H -o name); do
425 [[ $fs == /$ZONE_POOL ]] && continue
426 [[ -d $fs ]] && log_must rm -rf $fs/*
427 done
428
429 #
430 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
431 # the default value
432 #
433 for fs in $(zfs list -H -o name); do
434 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
435 log_must zfs set reservation=none $fs
436 log_must zfs set recordsize=128K $fs
437 log_must zfs set mountpoint=/$fs $fs
438 typeset enc=""
439 enc=$(get_prop encryption $fs)
440 if [[ $? -ne 0 ]] || [[ -z "$enc" ]] || \
441 [[ "$enc" == "off" ]]; then
442 log_must zfs set checksum=on $fs
443 fi
444 log_must zfs set compression=off $fs
445 log_must zfs set atime=on $fs
446 log_must zfs set devices=off $fs
447 log_must zfs set exec=on $fs
448 log_must zfs set setuid=on $fs
449 log_must zfs set readonly=off $fs
450 log_must zfs set snapdir=hidden $fs
451 log_must zfs set aclmode=groupmask $fs
452 log_must zfs set aclinherit=secure $fs
453 fi
454 done
455 fi
456
457 [[ -d $TESTDIR ]] && \
458 log_must rm -rf $TESTDIR
459 [[ -d $TESTDIR1 ]] && \
460 log_must rm -rf $TESTDIR1
461 }
462
463
464 #
465 # Common function used to cleanup storage pools, file systems
466 # and containers.
467 #
468 function default_container_cleanup
469 {
470 if ! is_global_zone; then
471 reexport_pool
472 fi
473
474 ismounted $TESTPOOL/$TESTCTR/$TESTFS1
475 [[ $? -eq 0 ]] && \
476 log_must zfs unmount $TESTPOOL/$TESTCTR/$TESTFS1
477
478 datasetexists $TESTPOOL/$TESTCTR/$TESTFS1 && \
479 log_must zfs destroy -R $TESTPOOL/$TESTCTR/$TESTFS1
480
481 datasetexists $TESTPOOL/$TESTCTR && \
482 log_must zfs destroy -Rf $TESTPOOL/$TESTCTR
483
484 [[ -e $TESTDIR1 ]] && \
485 log_must rm -rf $TESTDIR1 > /dev/null 2>&1
486
487 default_cleanup
488 }
489
490 #
491 # Common function used to cleanup snapshot of file system or volume. Default to
492 # delete the file system's snapshot
493 #
494 # $1 snapshot name
495 #
496 function destroy_snapshot
497 {
498 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
499
500 if ! snapexists $snap; then
501 log_fail "'$snap' does not existed."
502 fi
503
504 #
505 # For the sake of the value which come from 'get_prop' is not equal
506 # to the really mountpoint when the snapshot is unmounted. So, firstly
507 # check and make sure this snapshot's been mounted in current system.
508 #
509 typeset mtpt=""
510 if ismounted $snap; then
511 mtpt=$(get_prop mountpoint $snap)
512 (($? != 0)) && \
513 log_fail "get_prop mountpoint $snap failed."
514 fi
515
516 log_must zfs destroy $snap
517 [[ $mtpt != "" && -d $mtpt ]] && \
518 log_must rm -rf $mtpt
519 }
520
521 #
522 # Common function used to cleanup clone.
523 #
524 # $1 clone name
525 #
526 function destroy_clone
527 {
528 typeset clone=${1:-$TESTPOOL/$TESTCLONE}
529
530 if ! datasetexists $clone; then
531 log_fail "'$clone' does not existed."
532 fi
533
534 # With the same reason in destroy_snapshot
535 typeset mtpt=""
536 if ismounted $clone; then
537 mtpt=$(get_prop mountpoint $clone)
538 (($? != 0)) && \
539 log_fail "get_prop mountpoint $clone failed."
540 fi
541
542 log_must zfs destroy $clone
543 [[ $mtpt != "" && -d $mtpt ]] && \
544 log_must rm -rf $mtpt
545 }
546
547 #
548 # Common function used to cleanup bookmark of file system or volume. Default
549 # to delete the file system's bookmark.
550 #
551 # $1 bookmark name
552 #
553 function destroy_bookmark
554 {
555 typeset bkmark=${1:-$TESTPOOL/$TESTFS#$TESTBKMARK}
556
557 if ! bkmarkexists $bkmark; then
558 log_fail "'$bkmarkp' does not existed."
559 fi
560
561 log_must zfs destroy $bkmark
562 }
563
564 # Return 0 if a snapshot exists; $? otherwise
565 #
566 # $1 - snapshot name
567
568 function snapexists
569 {
570 zfs list -H -t snapshot "$1" > /dev/null 2>&1
571 return $?
572 }
573
574 #
575 # Return 0 if a bookmark exists; $? otherwise
576 #
577 # $1 - bookmark name
578 #
579 function bkmarkexists
580 {
581 zfs list -H -t bookmark "$1" > /dev/null 2>&1
582 return $?
583 }
584
585 #
586 # Set a property to a certain value on a dataset.
587 # Sets a property of the dataset to the value as passed in.
588 # @param:
589 # $1 dataset who's property is being set
590 # $2 property to set
591 # $3 value to set property to
592 # @return:
593 # 0 if the property could be set.
594 # non-zero otherwise.
595 # @use: ZFS
596 #
597 function dataset_setprop
598 {
599 typeset fn=dataset_setprop
600
601 if (($# < 3)); then
602 log_note "$fn: Insufficient parameters (need 3, had $#)"
603 return 1
604 fi
605 typeset output=
606 output=$(zfs set $2=$3 $1 2>&1)
607 typeset rv=$?
608 if ((rv != 0)); then
609 log_note "Setting property on $1 failed."
610 log_note "property $2=$3"
611 log_note "Return Code: $rv"
612 log_note "Output: $output"
613 return $rv
614 fi
615 return 0
616 }
617
618 #
619 # Assign suite defined dataset properties.
620 # This function is used to apply the suite's defined default set of
621 # properties to a dataset.
622 # @parameters: $1 dataset to use
623 # @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
624 # @returns:
625 # 0 if the dataset has been altered.
626 # 1 if no pool name was passed in.
627 # 2 if the dataset could not be found.
628 # 3 if the dataset could not have it's properties set.
629 #
630 function dataset_set_defaultproperties
631 {
632 typeset dataset="$1"
633
634 [[ -z $dataset ]] && return 1
635
636 typeset confset=
637 typeset -i found=0
638 for confset in $(zfs list); do
639 if [[ $dataset = $confset ]]; then
640 found=1
641 break
642 fi
643 done
644 [[ $found -eq 0 ]] && return 2
645 if [[ -n $COMPRESSION_PROP ]]; then
646 dataset_setprop $dataset compression $COMPRESSION_PROP || \
647 return 3
648 log_note "Compression set to '$COMPRESSION_PROP' on $dataset"
649 fi
650 if [[ -n $CHECKSUM_PROP ]]; then
651 dataset_setprop $dataset checksum $CHECKSUM_PROP || \
652 return 3
653 log_note "Checksum set to '$CHECKSUM_PROP' on $dataset"
654 fi
655 return 0
656 }
657
658 #
659 # Check a numeric assertion
660 # @parameter: $@ the assertion to check
661 # @output: big loud notice if assertion failed
662 # @use: log_fail
663 #
664 function assert
665 {
666 (($@)) || log_fail "$@"
667 }
668
669 #
670 # Function to format partition size of a disk
671 # Given a disk cxtxdx reduces all partitions
672 # to 0 size
673 #
674 function zero_partitions #<whole_disk_name>
675 {
676 typeset diskname=$1
677 typeset i
678
679 for i in 0 1 3 4 5 6 7
680 do
681 set_partition $i "" 0mb $diskname
682 done
683 }
684
685 #
686 # Given a slice, size and disk, this function
687 # formats the slice to the specified size.
688 # Size should be specified with units as per
689 # the `format` command requirements eg. 100mb 3gb
690 #
691 function set_partition #<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
692 {
693 typeset -i slicenum=$1
694 typeset start=$2
695 typeset size=$3
696 typeset disk=$4
697 [[ -z $slicenum || -z $size || -z $disk ]] && \
698 log_fail "The slice, size or disk name is unspecified."
699 typeset format_file=/var/tmp/format_in.$$
700
701 echo "partition" >$format_file
702 echo "$slicenum" >> $format_file
703 echo "" >> $format_file
704 echo "" >> $format_file
705 echo "$start" >> $format_file
706 echo "$size" >> $format_file
707 echo "label" >> $format_file
708 echo "" >> $format_file
709 echo "q" >> $format_file
710 echo "q" >> $format_file
711
712 format -e -s -d $disk -f $format_file
713 typeset ret_val=$?
714 rm -f $format_file
715 [[ $ret_val -ne 0 ]] && \
716 log_fail "Unable to format $disk slice $slicenum to $size"
717 return 0
718 }
719
720 #
721 # Get the end cyl of the given slice
722 #
723 function get_endslice #<disk> <slice>
724 {
725 typeset disk=$1
726 typeset slice=$2
727 if [[ -z $disk || -z $slice ]] ; then
728 log_fail "The disk name or slice number is unspecified."
729 fi
730
731 disk=${disk#/dev/dsk/}
732 disk=${disk#/dev/rdsk/}
733 disk=${disk%s*}
734
735 typeset -i ratio=0
736 ratio=$(prtvtoc /dev/rdsk/${disk}s2 | \
737 grep "sectors\/cylinder" | \
738 awk '{print $2}')
739
740 if ((ratio == 0)); then
741 return
742 fi
743
744 typeset -i endcyl=$(prtvtoc -h /dev/rdsk/${disk}s2 |
745 nawk -v token="$slice" '{if ($1==token) print $6}')
746
747 ((endcyl = (endcyl + 1) / ratio))
748 echo $endcyl
749 }
750
751
752 #
753 # Given a size,disk and total slice number, this function formats the
754 # disk slices from 0 to the total slice number with the same specified
755 # size.
756 #
757 function partition_disk #<slice_size> <whole_disk_name> <total_slices>
758 {
759 typeset -i i=0
760 typeset slice_size=$1
761 typeset disk_name=$2
762 typeset total_slices=$3
763 typeset cyl
764
765 zero_partitions $disk_name
766 while ((i < $total_slices)); do
767 if ((i == 2)); then
768 ((i = i + 1))
769 continue
770 fi
771 set_partition $i "$cyl" $slice_size $disk_name
772 cyl=$(get_endslice $disk_name $i)
773 ((i = i+1))
774 done
775 }
776
777 #
778 # This function continues to write to a filenum number of files into dirnum
779 # number of directories until either file_write returns an error or the
780 # maximum number of files per directory have been written.
781 #
782 # Usage:
783 # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
784 #
785 # Return value: 0 on success
786 # non 0 on error
787 #
788 # Where :
789 # destdir: is the directory where everything is to be created under
790 # dirnum: the maximum number of subdirectories to use, -1 no limit
791 # filenum: the maximum number of files per subdirectory
792 # bytes: number of bytes to write
793 # num_writes: numer of types to write out bytes
794 # data: the data that will be writen
795 #
796 # E.g.
797 # file_fs /testdir 20 25 1024 256 0
798 #
799 # Note: bytes * num_writes equals the size of the testfile
800 #
801 function fill_fs # destdir dirnum filenum bytes num_writes data
802 {
803 typeset destdir=${1:-$TESTDIR}
804 typeset -i dirnum=${2:-50}
805 typeset -i filenum=${3:-50}
806 typeset -i bytes=${4:-8192}
807 typeset -i num_writes=${5:-10240}
808 typeset -i data=${6:-0}
809
810 typeset -i odirnum=1
811 typeset -i idirnum=0
812 typeset -i fn=0
813 typeset -i retval=0
814
815 log_must mkdir -p $destdir/$idirnum
816 while (($odirnum > 0)); do
817 if ((dirnum >= 0 && idirnum >= dirnum)); then
818 odirnum=0
819 break
820 fi
821 file_write -o create -f $destdir/$idirnum/$TESTFILE.$fn \
822 -b $bytes -c $num_writes -d $data
823 retval=$?
824 if (($retval != 0)); then
825 odirnum=0
826 break
827 fi
828 if (($fn >= $filenum)); then
829 fn=0
830 ((idirnum = idirnum + 1))
831 log_must mkdir -p $destdir/$idirnum
832 else
833 ((fn = fn + 1))
834 fi
835 done
836 return $retval
837 }
838
839 #
840 # Simple function to get the specified property. If unable to
841 # get the property then exits.
842 #
843 # Note property is in 'parsable' format (-p)
844 #
845 function get_prop # property dataset
846 {
847 typeset prop_val
848 typeset prop=$1
849 typeset dataset=$2
850
851 prop_val=$(zfs get -pH -o value $prop $dataset 2>/dev/null)
852 if [[ $? -ne 0 ]]; then
853 log_note "Unable to get $prop property for dataset " \
854 "$dataset"
855 return 1
856 fi
857
858 echo "$prop_val"
859 return 0
860 }
861
862 #
863 # Simple function to get the specified property of pool. If unable to
864 # get the property then exits.
865 #
866 function get_pool_prop # property pool
867 {
868 typeset prop_val
869 typeset prop=$1
870 typeset pool=$2
871
872 if poolexists $pool ; then
873 prop_val=$(zpool get $prop $pool 2>/dev/null | tail -1 | \
874 awk '{print $3}')
875 if [[ $? -ne 0 ]]; then
876 log_note "Unable to get $prop property for pool " \
877 "$pool"
878 return 1
879 fi
880 else
881 log_note "Pool $pool not exists."
882 return 1
883 fi
884
885 echo $prop_val
886 return 0
887 }
888
889 # Return 0 if a pool exists; $? otherwise
890 #
891 # $1 - pool name
892
893 function poolexists
894 {
895 typeset pool=$1
896
897 if [[ -z $pool ]]; then
898 log_note "No pool name given."
899 return 1
900 fi
901
902 zpool get name "$pool" > /dev/null 2>&1
903 return $?
904 }
905
906 # Return 0 if all the specified datasets exist; $? otherwise
907 #
908 # $1-n dataset name
909 function datasetexists
910 {
911 if (($# == 0)); then
912 log_note "No dataset name given."
913 return 1
914 fi
915
916 while (($# > 0)); do
917 zfs get name $1 > /dev/null 2>&1 || \
918 return $?
919 shift
920 done
921
922 return 0
923 }
924
925 # return 0 if none of the specified datasets exists, otherwise return 1.
926 #
927 # $1-n dataset name
928 function datasetnonexists
929 {
930 if (($# == 0)); then
931 log_note "No dataset name given."
932 return 1
933 fi
934
935 while (($# > 0)); do
936 zfs list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \
937 && return 1
938 shift
939 done
940
941 return 0
942 }
943
944 #
945 # Given a mountpoint, or a dataset name, determine if it is shared.
946 #
947 # Returns 0 if shared, 1 otherwise.
948 #
949 function is_shared
950 {
951 typeset fs=$1
952 typeset mtpt
953
954 if [[ $fs != "/"* ]] ; then
955 if datasetnonexists "$fs" ; then
956 return 1
957 else
958 mtpt=$(get_prop mountpoint "$fs")
959 case $mtpt in
960 none|legacy|-) return 1
961 ;;
962 *) fs=$mtpt
963 ;;
964 esac
965 fi
966 fi
967
968 for mtpt in `share | awk '{print $2}'` ; do
969 if [[ $mtpt == $fs ]] ; then
970 return 0
971 fi
972 done
973
974 typeset stat=$(svcs -H -o STA nfs/server:default)
975 if [[ $stat != "ON" ]]; then
976 log_note "Current nfs/server status: $stat"
977 fi
978
979 return 1
980 }
981
982 #
983 # Given a mountpoint, determine if it is not shared.
984 #
985 # Returns 0 if not shared, 1 otherwise.
986 #
987 function not_shared
988 {
989 typeset fs=$1
990
991 is_shared $fs
992 if (($? == 0)); then
993 return 1
994 fi
995
996 return 0
997 }
998
999 #
1000 # Helper function to unshare a mountpoint.
1001 #
1002 function unshare_fs #fs
1003 {
1004 typeset fs=$1
1005
1006 is_shared $fs
1007 if (($? == 0)); then
1008 log_must zfs unshare $fs
1009 fi
1010
1011 return 0
1012 }
1013
1014 #
1015 # Check NFS server status and trigger it online.
1016 #
1017 function setup_nfs_server
1018 {
1019 # Cannot share directory in non-global zone.
1020 #
1021 if ! is_global_zone; then
1022 log_note "Cannot trigger NFS server by sharing in LZ."
1023 return
1024 fi
1025
1026 typeset nfs_fmri="svc:/network/nfs/server:default"
1027 if [[ $(svcs -Ho STA $nfs_fmri) != "ON" ]]; then
1028 #
1029 # Only really sharing operation can enable NFS server
1030 # to online permanently.
1031 #
1032 typeset dummy=/tmp/dummy
1033
1034 if [[ -d $dummy ]]; then
1035 log_must rm -rf $dummy
1036 fi
1037
1038 log_must mkdir $dummy
1039 log_must share $dummy
1040
1041 #
1042 # Waiting for fmri's status to be the final status.
1043 # Otherwise, in transition, an asterisk (*) is appended for
1044 # instances, unshare will reverse status to 'DIS' again.
1045 #
1046 # Waiting for 1's at least.
1047 #
1048 log_must sleep 1
1049 timeout=10
1050 while [[ timeout -ne 0 && $(svcs -Ho STA $nfs_fmri) == *'*' ]]
1051 do
1052 log_must sleep 1
1053
1054 ((timeout -= 1))
1055 done
1056
1057 log_must unshare $dummy
1058 log_must rm -rf $dummy
1059 fi
1060
1061 log_note "Current NFS status: '$(svcs -Ho STA,FMRI $nfs_fmri)'"
1062 }
1063
1064 #
1065 # To verify whether calling process is in global zone
1066 #
1067 # Return 0 if in global zone, 1 in non-global zone
1068 #
1069 function is_global_zone
1070 {
1071 typeset cur_zone=$(zonename 2>/dev/null)
1072 if [[ $cur_zone != "global" ]]; then
1073 return 1
1074 fi
1075 return 0
1076 }
1077
1078 #
1079 # Verify whether test is permitted to run from
1080 # global zone, local zone, or both
1081 #
1082 # $1 zone limit, could be "global", "local", or "both"(no limit)
1083 #
1084 # Return 0 if permitted, otherwise exit with log_unsupported
1085 #
1086 function verify_runnable # zone limit
1087 {
1088 typeset limit=$1
1089
1090 [[ -z $limit ]] && return 0
1091
1092 if is_global_zone ; then
1093 case $limit in
1094 global|both)
1095 ;;
1096 local) log_unsupported "Test is unable to run from "\
1097 "global zone."
1098 ;;
1099 *) log_note "Warning: unknown limit $limit - " \
1100 "use both."
1101 ;;
1102 esac
1103 else
1104 case $limit in
1105 local|both)
1106 ;;
1107 global) log_unsupported "Test is unable to run from "\
1108 "local zone."
1109 ;;
1110 *) log_note "Warning: unknown limit $limit - " \
1111 "use both."
1112 ;;
1113 esac
1114
1115 reexport_pool
1116 fi
1117
1118 return 0
1119 }
1120
1121 # Return 0 if create successfully or the pool exists; $? otherwise
1122 # Note: In local zones, this function should return 0 silently.
1123 #
1124 # $1 - pool name
1125 # $2-n - [keyword] devs_list
1126
1127 function create_pool #pool devs_list
1128 {
1129 typeset pool=${1%%/*}
1130
1131 shift
1132
1133 if [[ -z $pool ]]; then
1134 log_note "Missing pool name."
1135 return 1
1136 fi
1137
1138 if poolexists $pool ; then
1139 destroy_pool $pool
1140 fi
1141
1142 if is_global_zone ; then
1143 [[ -d /$pool ]] && rm -rf /$pool
1144 log_must zpool create -f $pool $@
1145 fi
1146
1147 return 0
1148 }
1149
1150 # Return 0 if destroy successfully or the pool exists; $? otherwise
1151 # Note: In local zones, this function should return 0 silently.
1152 #
1153 # $1 - pool name
1154 # $2 - optional force flag
1155 # Destroy pool with the given parameters.
1156
1157 function do_destroy_pool #pool <-f>
1158 {
1159 typeset pool=${1%%/*}
1160 typeset force=$2
1161 typeset mtpt
1162
1163 if [[ -z $pool ]]; then
1164 log_note "No pool name given."
1165 return 1
1166 fi
1167
1168 if is_global_zone ; then
1169 if poolexists "$pool" ; then
1170 mtpt=$(get_prop mountpoint "$pool")
1171
1172 # At times, syseventd activity can cause attempts to
1173 # destroy a pool to fail with EBUSY. We retry a few
1174 # times allowing failures before requiring the destroy
1175 # to succeed.
1176 typeset -i wait_time=$DESTROY_SLEEP_TIME ret=1 count=0
1177 must=""
1178 while [[ $ret -ne 0 ]]; do
1179 $must zpool destroy $force $pool
1180 ret=$?
1181 [[ $ret -eq 0 ]] && break
1182 log_note "zpool destroy failed with $ret"
1183 [[ count++ -ge $NUM_RETRIES ]] && must=log_must
1184 sleep $wait_time
1185 done
1186
1187 [[ -d $mtpt ]] && \
1188 log_must rm -rf $mtpt
1189 else
1190 log_note "Pool does not exist. ($pool)"
1191 return 1
1192 fi
1193 fi
1194
1195 return 0
1196 }
1197
1198 # Return 0 if destroy successfully or the pool exists; $? otherwise
1199 # Note: In local zones, this function should return 0 silently.
1200 #
1201 # $1 - pool name
1202 # Destroy pool with the given parameters.
1203
1204 function destroy_pool_no_force #pool
1205 {
1206 typeset pool=${1%%/*}
1207
1208 do_destroy_pool $pool
1209 if (( $? != 0 )); then
1210 return 1
1211 else
1212 return 0
1213 fi
1214 }
1215
1216 # Return 0 if destroy successfully or the pool exists; $? otherwise
1217 # Note: In local zones, this function should return 0 silently.
1218 #
1219 # $1 - pool name
1220 # Force a destroy pool with the given parameters.
1221
1222 function destroy_pool #pool
1223 {
1224 typeset pool=${1%%/*}
1225
1226 do_destroy_pool $pool -f
1227 if (( $? != 0 )); then
1228 return 1
1229 else
1230 return 0
1231 fi
1232 }
1233
1234 #
1235 # Firstly, create a pool with 5 datasets. Then, create a single zone and
1236 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
1237 # and a zvol device to the zone.
1238 #
1239 # $1 zone name
1240 # $2 zone root directory prefix
1241 # $3 zone ip
1242 #
1243 function zfs_zones_setup #zone_name zone_root zone_ip
1244 {
1245 typeset zone_name=${1:-$(hostname)-z}
1246 typeset zone_root=${2:-"/zone_root"}
1247 typeset zone_ip=${3:-"10.1.1.10"}
1248 typeset prefix_ctr=$ZONE_CTR
1249 typeset pool_name=$ZONE_POOL
1250 typeset -i cntctr=5
1251 typeset -i i=0
1252
1253 # Create pool and 5 container within it
1254 #
1255 [[ -d /$pool_name ]] && rm -rf /$pool_name
1256 log_must zpool create -f $pool_name $DISKS
1257 while ((i < cntctr)); do
1258 log_must zfs create $pool_name/$prefix_ctr$i
1259 ((i += 1))
1260 done
1261
1262 # create a zvol
1263 log_must zfs create -V 1g $pool_name/zone_zvol
1264
1265 #
1266 # If current system support slog, add slog device for pool
1267 #
1268 if verify_slog_support ; then
1269 typeset sdevs="/var/tmp/sdev1 /var/tmp/sdev2"
1270 log_must mkfile $MINVDEVSIZE $sdevs
1271 log_must zpool add $pool_name log mirror $sdevs
1272 fi
1273
1274 # this isn't supported just yet.
1275 # Create a filesystem. In order to add this to
1276 # the zone, it must have it's mountpoint set to 'legacy'
1277 # log_must zfs create $pool_name/zfs_filesystem
1278 # log_must zfs set mountpoint=legacy $pool_name/zfs_filesystem
1279
1280 [[ -d $zone_root ]] && \
1281 log_must rm -rf $zone_root/$zone_name
1282 [[ ! -d $zone_root ]] && \
1283 log_must mkdir -p -m 0700 $zone_root/$zone_name
1284
1285 # Create zone configure file and configure the zone
1286 #
1287 typeset zone_conf=/tmp/zone_conf.$$
1288 echo "create" > $zone_conf
1289 echo "set zonepath=$zone_root/$zone_name" >> $zone_conf
1290 echo "set autoboot=true" >> $zone_conf
1291 i=0
1292 while ((i < cntctr)); do
1293 echo "add dataset" >> $zone_conf
1294 echo "set name=$pool_name/$prefix_ctr$i" >> \
1295 $zone_conf
1296 echo "end" >> $zone_conf
1297 ((i += 1))
1298 done
1299
1300 # add our zvol to the zone
1301 echo "add device" >> $zone_conf
1302 echo "set match=/dev/zvol/dsk/$pool_name/zone_zvol" >> $zone_conf
1303 echo "end" >> $zone_conf
1304
1305 # add a corresponding zvol rdsk to the zone
1306 echo "add device" >> $zone_conf
1307 echo "set match=/dev/zvol/rdsk/$pool_name/zone_zvol" >> $zone_conf
1308 echo "end" >> $zone_conf
1309
1310 # once it's supported, we'll add our filesystem to the zone
1311 # echo "add fs" >> $zone_conf
1312 # echo "set type=zfs" >> $zone_conf
1313 # echo "set special=$pool_name/zfs_filesystem" >> $zone_conf
1314 # echo "set dir=/export/zfs_filesystem" >> $zone_conf
1315 # echo "end" >> $zone_conf
1316
1317 echo "verify" >> $zone_conf
1318 echo "commit" >> $zone_conf
1319 log_must zonecfg -z $zone_name -f $zone_conf
1320 log_must rm -f $zone_conf
1321
1322 # Install the zone
1323 zoneadm -z $zone_name install
1324 if (($? == 0)); then
1325 log_note "SUCCESS: zoneadm -z $zone_name install"
1326 else
1327 log_fail "FAIL: zoneadm -z $zone_name install"
1328 fi
1329
1330 # Install sysidcfg file
1331 #
1332 typeset sysidcfg=$zone_root/$zone_name/root/etc/sysidcfg
1333 echo "system_locale=C" > $sysidcfg
1334 echo "terminal=dtterm" >> $sysidcfg
1335 echo "network_interface=primary {" >> $sysidcfg
1336 echo "hostname=$zone_name" >> $sysidcfg
1337 echo "}" >> $sysidcfg
1338 echo "name_service=NONE" >> $sysidcfg
1339 echo "root_password=mo791xfZ/SFiw" >> $sysidcfg
1340 echo "security_policy=NONE" >> $sysidcfg
1341 echo "timezone=US/Eastern" >> $sysidcfg
1342
1343 # Boot this zone
1344 log_must zoneadm -z $zone_name boot
1345 }
1346
1347 #
1348 # Reexport TESTPOOL & TESTPOOL(1-4)
1349 #
1350 function reexport_pool
1351 {
1352 typeset -i cntctr=5
1353 typeset -i i=0
1354
1355 while ((i < cntctr)); do
1356 if ((i == 0)); then
1357 TESTPOOL=$ZONE_POOL/$ZONE_CTR$i
1358 if ! ismounted $TESTPOOL; then
1359 log_must zfs mount $TESTPOOL
1360 fi
1361 else
1362 eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i
1363 if eval ! ismounted \$TESTPOOL$i; then
1364 log_must eval zfs mount \$TESTPOOL$i
1365 fi
1366 fi
1367 ((i += 1))
1368 done
1369 }
1370
1371 #
1372 # Verify a given disk is online or offline
1373 #
1374 # Return 0 is pool/disk matches expected state, 1 otherwise
1375 #
1376 function check_state # pool disk state{online,offline}
1377 {
1378 typeset pool=$1
1379 typeset disk=${2#/dev/dsk/}
1380 typeset state=$3
1381
1382 zpool status -v $pool | grep "$disk" \
1383 | grep -i "$state" > /dev/null 2>&1
1384
1385 return $?
1386 }
1387
1388 #
1389 # Get the mountpoint of snapshot
1390 # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1391 # as its mountpoint
1392 #
1393 function snapshot_mountpoint
1394 {
1395 typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1396
1397 if [[ $dataset != *@* ]]; then
1398 log_fail "Error name of snapshot '$dataset'."
1399 fi
1400
1401 typeset fs=${dataset%@*}
1402 typeset snap=${dataset#*@}
1403
1404 if [[ -z $fs || -z $snap ]]; then
1405 log_fail "Error name of snapshot '$dataset'."
1406 fi
1407
1408 echo $(get_prop mountpoint $fs)/.zfs/snapshot/$snap
1409 }
1410
1411 #
1412 # Given a pool and file system, this function will verify the file system
1413 # using the zdb internal tool. Note that the pool is exported and imported
1414 # to ensure it has consistent state.
1415 #
1416 function verify_filesys # pool filesystem dir
1417 {
1418 typeset pool="$1"
1419 typeset filesys="$2"
1420 typeset zdbout="/tmp/zdbout.$$"
1421
1422 shift
1423 shift
1424 typeset dirs=$@
1425 typeset search_path=""
1426
1427 log_note "Calling zdb to verify filesystem '$filesys'"
1428 zfs unmount -a > /dev/null 2>&1
1429 log_must zpool export $pool
1430
1431 if [[ -n $dirs ]] ; then
1432 for dir in $dirs ; do
1433 search_path="$search_path -d $dir"
1434 done
1435 fi
1436
1437 log_must zpool import $search_path $pool
1438
1439 zdb -cudi $filesys > $zdbout 2>&1
1440 if [[ $? != 0 ]]; then
1441 log_note "Output: zdb -cudi $filesys"
1442 cat $zdbout
1443 log_fail "zdb detected errors with: '$filesys'"
1444 fi
1445
1446 log_must zfs mount -a
1447 log_must rm -rf $zdbout
1448 }
1449
1450 #
1451 # Given a pool, and this function list all disks in the pool
1452 #
1453 function get_disklist # pool
1454 {
1455 typeset disklist=""
1456
1457 disklist=$(zpool iostat -v $1 | nawk '(NR >4) {print $1}' | \
1458 grep -v "\-\-\-\-\-" | \
1459 egrep -v -e "^(mirror|raidz1|raidz2|spare|log|cache)$")
1460
1461 echo $disklist
1462 }
1463
1464 # /**
1465 # This function kills a given list of processes after a time period. We use
1466 # this in the stress tests instead of STF_TIMEOUT so that we can have processes
1467 # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1468 # would be listed as FAIL, which we don't want : we're happy with stress tests
1469 # running for a certain amount of time, then finishing.
1470 #
1471 # @param $1 the time in seconds after which we should terminate these processes
1472 # @param $2..$n the processes we wish to terminate.
1473 # */
1474 function stress_timeout
1475 {
1476 typeset -i TIMEOUT=$1
1477 shift
1478 typeset cpids="$@"
1479
1480 log_note "Waiting for child processes($cpids). " \
1481 "It could last dozens of minutes, please be patient ..."
1482 log_must sleep $TIMEOUT
1483
1484 log_note "Killing child processes after ${TIMEOUT} stress timeout."
1485 typeset pid
1486 for pid in $cpids; do
1487 ps -p $pid > /dev/null 2>&1
1488 if (($? == 0)); then
1489 log_must kill -USR1 $pid
1490 fi
1491 done
1492 }
1493
1494 #
1495 # Verify a given hotspare disk is inuse or avail
1496 #
1497 # Return 0 is pool/disk matches expected state, 1 otherwise
1498 #
1499 function check_hotspare_state # pool disk state{inuse,avail}
1500 {
1501 typeset pool=$1
1502 typeset disk=${2#/dev/dsk/}
1503 typeset state=$3
1504
1505 cur_state=$(get_device_state $pool $disk "spares")
1506
1507 if [[ $state != ${cur_state} ]]; then
1508 return 1
1509 fi
1510 return 0
1511 }
1512
1513 #
1514 # Verify a given slog disk is inuse or avail
1515 #
1516 # Return 0 is pool/disk matches expected state, 1 otherwise
1517 #
1518 function check_slog_state # pool disk state{online,offline,unavail}
1519 {
1520 typeset pool=$1
1521 typeset disk=${2#/dev/dsk/}
1522 typeset state=$3
1523
1524 cur_state=$(get_device_state $pool $disk "logs")
1525
1526 if [[ $state != ${cur_state} ]]; then
1527 return 1
1528 fi
1529 return 0
1530 }
1531
1532 #
1533 # Verify a given vdev disk is inuse or avail
1534 #
1535 # Return 0 is pool/disk matches expected state, 1 otherwise
1536 #
1537 function check_vdev_state # pool disk state{online,offline,unavail}
1538 {
1539 typeset pool=$1
1540 typeset disk=${2#/dev/dsk/}
1541 typeset state=$3
1542
1543 cur_state=$(get_device_state $pool $disk)
1544
1545 if [[ $state != ${cur_state} ]]; then
1546 return 1
1547 fi
1548 return 0
1549 }
1550
1551 #
1552 # Check the output of 'zpool status -v <pool>',
1553 # and to see if the content of <token> contain the <keyword> specified.
1554 #
1555 # Return 0 is contain, 1 otherwise
1556 #
1557 function check_pool_status # pool token keyword <verbose>
1558 {
1559 typeset pool=$1
1560 typeset token=$2
1561 typeset keyword=$3
1562 typeset verbose=${4:-false}
1563
1564 scan=$(zpool status -v "$pool" 2>/dev/null | nawk -v token="$token:" '
1565 ($1==token) {print $0}')
1566 if [[ $verbose == true ]]; then
1567 log_note $scan
1568 fi
1569 echo $scan | grep -i "$keyword" > /dev/null 2>&1
1570
1571 return $?
1572 }
1573
1574 #
1575 # These 6 following functions are instance of check_pool_status()
1576 # is_pool_resilvering - to check if the pool is resilver in progress
1577 # is_pool_resilvered - to check if the pool is resilver completed
1578 # is_pool_scrubbing - to check if the pool is scrub in progress
1579 # is_pool_scrubbed - to check if the pool is scrub completed
1580 # is_pool_scrub_stopped - to check if the pool is scrub stopped
1581 # is_pool_scrub_paused - to check if the pool has scrub paused
1582 #
1583 function is_pool_resilvering #pool <verbose>
1584 {
1585 check_pool_status "$1" "scan" "resilver in progress since " $2
1586 return $?
1587 }
1588
1589 function is_pool_resilvered #pool <verbose>
1590 {
1591 check_pool_status "$1" "scan" "resilvered " $2
1592 return $?
1593 }
1594
1595 function is_pool_scrubbing #pool <verbose>
1596 {
1597 check_pool_status "$1" "scan" "scrub in progress since " $2
1598 return $?
1599 }
1600
1601 function is_pool_scrubbed #pool <verbose>
1602 {
1603 check_pool_status "$1" "scan" "scrub repaired" $2
1604 return $?
1605 }
1606
1607 function is_pool_scrub_stopped #pool <verbose>
1608 {
1609 check_pool_status "$1" "scan" "scrub canceled" $2
1610 return $?
1611 }
1612
1613 function is_pool_scrub_paused #pool <verbose>
1614 {
1615 check_pool_status "$1" "scan" "scrub paused since " $2
1616 return $?
1617 }
1618
1619 #
1620 # Use create_pool()/destroy_pool() to clean up the infomation in
1621 # in the given disk to avoid slice overlapping.
1622 #
1623 function cleanup_devices #vdevs
1624 {
1625 typeset pool="foopool$$"
1626
1627 if poolexists $pool ; then
1628 destroy_pool $pool
1629 fi
1630
1631 create_pool $pool $@
1632 destroy_pool $pool
1633
1634 return 0
1635 }
1636
1637 #/**
1638 # A function to find and locate free disks on a system or from given
1639 # disks as the parameter. It works by locating disks that are in use
1640 # as swap devices and dump devices, and also disks listed in /etc/vfstab
1641 #
1642 # $@ given disks to find which are free, default is all disks in
1643 # the test system
1644 #
1645 # @return a string containing the list of available disks
1646 #*/
1647 function find_disks
1648 {
1649 sfi=/tmp/swaplist.$$
1650 dmpi=/tmp/dumpdev.$$
1651 max_finddisksnum=${MAX_FINDDISKSNUM:-6}
1652
1653 swap -l > $sfi
1654 dumpadm > $dmpi 2>/dev/null
1655
1656 # write an awk script that can process the output of format
1657 # to produce a list of disks we know about. Note that we have
1658 # to escape "$2" so that the shell doesn't interpret it while
1659 # we're creating the awk script.
1660 # -------------------
1661 cat > /tmp/find_disks.awk <<EOF
1662 #!/bin/nawk -f
1663 BEGIN { FS="."; }
1664
1665 /^Specify disk/{
1666 searchdisks=0;
1667 }
1668
1669 {
1670 if (searchdisks && \$2 !~ "^$"){
1671 split(\$2,arr," ");
1672 print arr[1];
1673 }
1674 }
1675
1676 /^AVAILABLE DISK SELECTIONS:/{
1677 searchdisks=1;
1678 }
1679 EOF
1680 #---------------------
1681
1682 chmod 755 /tmp/find_disks.awk
1683 disks=${@:-$(echo "" | format -e 2>/dev/null | /tmp/find_disks.awk)}
1684 rm /tmp/find_disks.awk
1685
1686 unused=""
1687 for disk in $disks; do
1688 # Check for mounted
1689 grep "${disk}[sp]" /etc/mnttab >/dev/null
1690 (($? == 0)) && continue
1691 # Check for swap
1692 grep "${disk}[sp]" $sfi >/dev/null
1693 (($? == 0)) && continue
1694 # check for dump device
1695 grep "${disk}[sp]" $dmpi >/dev/null
1696 (($? == 0)) && continue
1697 # check to see if this disk hasn't been explicitly excluded
1698 # by a user-set environment variable
1699 echo "${ZFS_HOST_DEVICES_IGNORE}" | grep "${disk}" > /dev/null
1700 (($? == 0)) && continue
1701 unused_candidates="$unused_candidates $disk"
1702 done
1703 rm $sfi
1704 rm $dmpi
1705
1706 # now just check to see if those disks do actually exist
1707 # by looking for a device pointing to the first slice in
1708 # each case. limit the number to max_finddisksnum
1709 count=0
1710 for disk in $unused_candidates; do
1711 if [ -b /dev/dsk/${disk}s0 ]; then
1712 if [ $count -lt $max_finddisksnum ]; then
1713 unused="$unused $disk"
1714 # do not impose limit if $@ is provided
1715 [[ -z $@ ]] && ((count = count + 1))
1716 fi
1717 fi
1718 done
1719
1720 # finally, return our disk list
1721 echo $unused
1722 }
1723
1724 #
1725 # Add specified user to specified group
1726 #
1727 # $1 group name
1728 # $2 user name
1729 # $3 base of the homedir (optional)
1730 #
1731 function add_user #<group_name> <user_name> <basedir>
1732 {
1733 typeset gname=$1
1734 typeset uname=$2
1735 typeset basedir=${3:-"/var/tmp"}
1736
1737 if ((${#gname} == 0 || ${#uname} == 0)); then
1738 log_fail "group name or user name are not defined."
1739 fi
1740
1741 log_must useradd -g $gname -d $basedir/$uname -m $uname
1742
1743 return 0
1744 }
1745
1746 #
1747 # Delete the specified user.
1748 #
1749 # $1 login name
1750 # $2 base of the homedir (optional)
1751 #
1752 function del_user #<logname> <basedir>
1753 {
1754 typeset user=$1
1755 typeset basedir=${2:-"/var/tmp"}
1756
1757 if ((${#user} == 0)); then
1758 log_fail "login name is necessary."
1759 fi
1760
1761 if id $user > /dev/null 2>&1; then
1762 log_must userdel $user
1763 fi
1764
1765 [[ -d $basedir/$user ]] && rm -fr $basedir/$user
1766
1767 return 0
1768 }
1769
1770 #
1771 # Select valid gid and create specified group.
1772 #
1773 # $1 group name
1774 #
1775 function add_group #<group_name>
1776 {
1777 typeset group=$1
1778
1779 if ((${#group} == 0)); then
1780 log_fail "group name is necessary."
1781 fi
1782
1783 # Assign 100 as the base gid
1784 typeset -i gid=100
1785 while true; do
1786 groupadd -g $gid $group > /dev/null 2>&1
1787 typeset -i ret=$?
1788 case $ret in
1789 0) return 0 ;;
1790 # The gid is not unique
1791 4) ((gid += 1)) ;;
1792 *) return 1 ;;
1793 esac
1794 done
1795 }
1796
1797 #
1798 # Delete the specified group.
1799 #
1800 # $1 group name
1801 #
1802 function del_group #<group_name>
1803 {
1804 typeset grp=$1
1805 if ((${#grp} == 0)); then
1806 log_fail "group name is necessary."
1807 fi
1808
1809 groupmod -n $grp $grp > /dev/null 2>&1
1810 typeset -i ret=$?
1811 case $ret in
1812 # Group does not exist.
1813 6) return 0 ;;
1814 # Name already exists as a group name
1815 9) log_must groupdel $grp ;;
1816 *) return 1 ;;
1817 esac
1818
1819 return 0
1820 }
1821
1822 #
1823 # This function will return true if it's safe to destroy the pool passed
1824 # as argument 1. It checks for pools based on zvols and files, and also
1825 # files contained in a pool that may have a different mountpoint.
1826 #
1827 function safe_to_destroy_pool { # $1 the pool name
1828
1829 typeset pool=""
1830 typeset DONT_DESTROY=""
1831
1832 # We check that by deleting the $1 pool, we're not
1833 # going to pull the rug out from other pools. Do this
1834 # by looking at all other pools, ensuring that they
1835 # aren't built from files or zvols contained in this pool.
1836
1837 for pool in $(zpool list -H -o name)
1838 do
1839 ALTMOUNTPOOL=""
1840
1841 # this is a list of the top-level directories in each of the
1842 # files that make up the path to the files the pool is based on
1843 FILEPOOL=$(zpool status -v $pool | grep /$1/ | \
1844 awk '{print $1}')
1845
1846 # this is a list of the zvols that make up the pool
1847 ZVOLPOOL=$(zpool status -v $pool | grep "/dev/zvol/dsk/$1$" \
1848 | awk '{print $1}')
1849
1850 # also want to determine if it's a file-based pool using an
1851 # alternate mountpoint...
1852 POOL_FILE_DIRS=$(zpool status -v $pool | \
1853 grep / | awk '{print $1}' | \
1854 awk -F/ '{print $2}' | grep -v "dev")
1855
1856 for pooldir in $POOL_FILE_DIRS
1857 do
1858 OUTPUT=$(zfs list -H -r -o mountpoint $1 | \
1859 grep "${pooldir}$" | awk '{print $1}')
1860
1861 ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
1862 done
1863
1864
1865 if [ ! -z "$ZVOLPOOL" ]
1866 then
1867 DONT_DESTROY="true"
1868 log_note "Pool $pool is built from $ZVOLPOOL on $1"
1869 fi
1870
1871 if [ ! -z "$FILEPOOL" ]
1872 then
1873 DONT_DESTROY="true"
1874 log_note "Pool $pool is built from $FILEPOOL on $1"
1875 fi
1876
1877 if [ ! -z "$ALTMOUNTPOOL" ]
1878 then
1879 DONT_DESTROY="true"
1880 log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
1881 fi
1882 done
1883
1884 if [ -z "${DONT_DESTROY}" ]
1885 then
1886 return 0
1887 else
1888 log_note "Warning: it is not safe to destroy $1!"
1889 return 1
1890 fi
1891 }
1892
1893 #
1894 # Get the available ZFS compression options
1895 # $1 option type zfs_set|zfs_compress
1896 #
1897 function get_compress_opts
1898 {
1899 typeset COMPRESS_OPTS
1900 typeset GZIP_OPTS="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
1901 gzip-6 gzip-7 gzip-8 gzip-9"
1902
1903 if [[ $1 == "zfs_compress" ]] ; then
1904 COMPRESS_OPTS="on lzjb"
1905 elif [[ $1 == "zfs_set" ]] ; then
1906 COMPRESS_OPTS="on off lzjb"
1907 fi
1908 typeset valid_opts="$COMPRESS_OPTS"
1909 zfs get 2>&1 | grep gzip >/dev/null 2>&1
1910 if [[ $? -eq 0 ]]; then
1911 valid_opts="$valid_opts $GZIP_OPTS"
1912 fi
1913 echo "$valid_opts"
1914 }
1915
1916 #
1917 # Verify zfs operation with -p option work as expected
1918 # $1 operation, value could be create, clone or rename
1919 # $2 dataset type, value could be fs or vol
1920 # $3 dataset name
1921 # $4 new dataset name
1922 #
1923 function verify_opt_p_ops
1924 {
1925 typeset ops=$1
1926 typeset datatype=$2
1927 typeset dataset=$3
1928 typeset newdataset=$4
1929
1930 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
1931 log_fail "$datatype is not supported."
1932 fi
1933
1934 # check parameters accordingly
1935 case $ops in
1936 create)
1937 newdataset=$dataset
1938 dataset=""
1939 if [[ $datatype == "vol" ]]; then
1940 ops="create -V $VOLSIZE"
1941 fi
1942 ;;
1943 clone)
1944 if [[ -z $newdataset ]]; then
1945 log_fail "newdataset should not be empty" \
1946 "when ops is $ops."
1947 fi
1948 log_must datasetexists $dataset
1949 log_must snapexists $dataset
1950 ;;
1951 rename)
1952 if [[ -z $newdataset ]]; then
1953 log_fail "newdataset should not be empty" \
1954 "when ops is $ops."
1955 fi
1956 log_must datasetexists $dataset
1957 log_mustnot snapexists $dataset
1958 ;;
1959 *)
1960 log_fail "$ops is not supported."
1961 ;;
1962 esac
1963
1964 # make sure the upper level filesystem does not exist
1965 if datasetexists ${newdataset%/*} ; then
1966 log_must zfs destroy -rRf ${newdataset%/*}
1967 fi
1968
1969 # without -p option, operation will fail
1970 log_mustnot zfs $ops $dataset $newdataset
1971 log_mustnot datasetexists $newdataset ${newdataset%/*}
1972
1973 # with -p option, operation should succeed
1974 log_must zfs $ops -p $dataset $newdataset
1975 if ! datasetexists $newdataset ; then
1976 log_fail "-p option does not work for $ops"
1977 fi
1978
1979 # when $ops is create or clone, redo the operation still return zero
1980 if [[ $ops != "rename" ]]; then
1981 log_must zfs $ops -p $dataset $newdataset
1982 fi
1983
1984 return 0
1985 }
1986
1987 #
1988 # Get configuration of pool
1989 # $1 pool name
1990 # $2 config name
1991 #
1992 function get_config
1993 {
1994 typeset pool=$1
1995 typeset config=$2
1996 typeset alt_root
1997
1998 if ! poolexists "$pool" ; then
1999 return 1
2000 fi
2001 alt_root=$(zpool list -H $pool | awk '{print $NF}')
2002 if [[ $alt_root == "-" ]]; then
2003 value=$(zdb -C $pool | grep "$config:" | awk -F: \
2004 '{print $2}')
2005 else
2006 value=$(zdb -e $pool | grep "$config:" | awk -F: \
2007 '{print $2}')
2008 fi
2009 if [[ -n $value ]] ; then
2010 value=${value#'}
2011 value=${value%'}
2012 fi
2013 echo $value
2014
2015 return 0
2016 }
2017
2018 #
2019 # Privated function. Random select one of items from arguments.
2020 #
2021 # $1 count
2022 # $2-n string
2023 #
2024 function _random_get
2025 {
2026 typeset cnt=$1
2027 shift
2028
2029 typeset str="$@"
2030 typeset -i ind
2031 ((ind = RANDOM % cnt + 1))
2032
2033 typeset ret=$(echo "$str" | cut -f $ind -d ' ')
2034 echo $ret
2035 }
2036
2037 #
2038 # Random select one of item from arguments which include NONE string
2039 #
2040 function random_get_with_non
2041 {
2042 typeset -i cnt=$#
2043 ((cnt =+ 1))
2044
2045 _random_get "$cnt" "$@"
2046 }
2047
2048 #
2049 # Random select one of item from arguments which doesn't include NONE string
2050 #
2051 function random_get
2052 {
2053 _random_get "$#" "$@"
2054 }
2055
2056 #
2057 # Detect if the current system support slog
2058 #
2059 function verify_slog_support
2060 {
2061 typeset dir=/tmp/disk.$$
2062 typeset pool=foo.$$
2063 typeset vdev=$dir/a
2064 typeset sdev=$dir/b
2065
2066 mkdir -p $dir
2067 mkfile $MINVDEVSIZE $vdev $sdev
2068
2069 typeset -i ret=0
2070 if ! zpool create -n $pool $vdev log $sdev > /dev/null 2>&1; then
2071 ret=1
2072 fi
2073 rm -r $dir
2074
2075 return $ret
2076 }
2077
2078 #
2079 # The function will generate a dataset name with specific length
2080 # $1, the length of the name
2081 # $2, the base string to construct the name
2082 #
2083 function gen_dataset_name
2084 {
2085 typeset -i len=$1
2086 typeset basestr="$2"
2087 typeset -i baselen=${#basestr}
2088 typeset -i iter=0
2089 typeset l_name=""
2090
2091 if ((len % baselen == 0)); then
2092 ((iter = len / baselen))
2093 else
2094 ((iter = len / baselen + 1))
2095 fi
2096 while ((iter > 0)); do
2097 l_name="${l_name}$basestr"
2098
2099 ((iter -= 1))
2100 done
2101
2102 echo $l_name
2103 }
2104
2105 #
2106 # Get cksum tuple of dataset
2107 # $1 dataset name
2108 #
2109 # sample zdb output:
2110 # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2111 # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2112 # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2113 # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2114 function datasetcksum
2115 {
2116 typeset cksum
2117 sync
2118 cksum=$(zdb -vvv $1 | grep "^Dataset $1 \[" | grep "cksum" \
2119 | awk -F= '{print $7}')
2120 echo $cksum
2121 }
2122
2123 #
2124 # Get cksum of file
2125 # #1 file path
2126 #
2127 function checksum
2128 {
2129 typeset cksum
2130 cksum=$(cksum $1 | awk '{print $1}')
2131 echo $cksum
2132 }
2133
2134 #
2135 # Get the given disk/slice state from the specific field of the pool
2136 #
2137 function get_device_state #pool disk field("", "spares","logs")
2138 {
2139 typeset pool=$1
2140 typeset disk=${2#/dev/dsk/}
2141 typeset field=${3:-$pool}
2142
2143 state=$(zpool status -v "$pool" 2>/dev/null | \
2144 nawk -v device=$disk -v pool=$pool -v field=$field \
2145 'BEGIN {startconfig=0; startfield=0; }
2146 /config:/ {startconfig=1}
2147 (startconfig==1) && ($1==field) {startfield=1; next;}
2148 (startfield==1) && ($1==device) {print $2; exit;}
2149 (startfield==1) &&
2150 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2151 echo $state
2152 }
2153
2154
2155 #
2156 # print the given directory filesystem type
2157 #
2158 # $1 directory name
2159 #
2160 function get_fstype
2161 {
2162 typeset dir=$1
2163
2164 if [[ -z $dir ]]; then
2165 log_fail "Usage: get_fstype <directory>"
2166 fi
2167
2168 #
2169 # $ df -n /
2170 # / : ufs
2171 #
2172 df -n $dir | awk '{print $3}'
2173 }
2174
2175 #
2176 # Given a disk, label it to VTOC regardless what label was on the disk
2177 # $1 disk
2178 #
2179 function labelvtoc
2180 {
2181 typeset disk=$1
2182 if [[ -z $disk ]]; then
2183 log_fail "The disk name is unspecified."
2184 fi
2185 typeset label_file=/var/tmp/labelvtoc.$$
2186 typeset arch=$(uname -p)
2187
2188 if [[ $arch == "i386" ]]; then
2189 echo "label" > $label_file
2190 echo "0" >> $label_file
2191 echo "" >> $label_file
2192 echo "q" >> $label_file
2193 echo "q" >> $label_file
2194
2195 fdisk -B $disk >/dev/null 2>&1
2196 # wait a while for fdisk finishes
2197 sleep 60
2198 elif [[ $arch == "sparc" ]]; then
2199 echo "label" > $label_file
2200 echo "0" >> $label_file
2201 echo "" >> $label_file
2202 echo "" >> $label_file
2203 echo "" >> $label_file
2204 echo "q" >> $label_file
2205 else
2206 log_fail "unknown arch type"
2207 fi
2208
2209 format -e -s -d $disk -f $label_file
2210 typeset -i ret_val=$?
2211 rm -f $label_file
2212 #
2213 # wait the format to finish
2214 #
2215 sleep 60
2216 if ((ret_val != 0)); then
2217 log_fail "unable to label $disk as VTOC."
2218 fi
2219
2220 return 0
2221 }
2222
2223 #
2224 # check if the system was installed as zfsroot or not
2225 # return: 0 ture, otherwise false
2226 #
2227 function is_zfsroot
2228 {
2229 df -n / | grep zfs > /dev/null 2>&1
2230 return $?
2231 }
2232
2233 #
2234 # get the root filesystem name if it's zfsroot system.
2235 #
2236 # return: root filesystem name
2237 function get_rootfs
2238 {
2239 typeset rootfs=""
2240 rootfs=$(awk '{if ($2 == "/" && $3 == "zfs") print $1}' \
2241 /etc/mnttab)
2242 if [[ -z "$rootfs" ]]; then
2243 log_fail "Can not get rootfs"
2244 fi
2245 zfs list $rootfs > /dev/null 2>&1
2246 if (($? == 0)); then
2247 echo $rootfs
2248 else
2249 log_fail "This is not a zfsroot system."
2250 fi
2251 }
2252
2253 #
2254 # get the rootfs's pool name
2255 # return:
2256 # rootpool name
2257 #
2258 function get_rootpool
2259 {
2260 typeset rootfs=""
2261 typeset rootpool=""
2262 rootfs=$(awk '{if ($2 == "/" && $3 =="zfs") print $1}' \
2263 /etc/mnttab)
2264 if [[ -z "$rootfs" ]]; then
2265 log_fail "Can not get rootpool"
2266 fi
2267 zfs list $rootfs > /dev/null 2>&1
2268 if (($? == 0)); then
2269 rootpool=`echo $rootfs | awk -F\/ '{print $1}'`
2270 echo $rootpool
2271 else
2272 log_fail "This is not a zfsroot system."
2273 fi
2274 }
2275
2276 #
2277 # Check if the given device is physical device
2278 #
2279 function is_physical_device #device
2280 {
2281 typeset device=${1#/dev/dsk/}
2282 device=${device#/dev/rdsk/}
2283
2284 echo $device | egrep "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
2285 return $?
2286 }
2287
2288 #
2289 # Get the directory path of given device
2290 #
2291 function get_device_dir #device
2292 {
2293 typeset device=$1
2294
2295 if ! $(is_physical_device $device) ; then
2296 if [[ $device != "/" ]]; then
2297 device=${device%/*}
2298 fi
2299 echo $device
2300 else
2301 echo "/dev/dsk"
2302 fi
2303 }
2304
2305 #
2306 # Get the package name
2307 #
2308 function get_package_name
2309 {
2310 typeset dirpath=${1:-$STC_NAME}
2311
2312 echo "SUNWstc-${dirpath}" | /usr/bin/sed -e "s/\//-/g"
2313 }
2314
2315 #
2316 # Get the word numbers from a string separated by white space
2317 #
2318 function get_word_count
2319 {
2320 echo $1 | wc -w
2321 }
2322
2323 #
2324 # To verify if the require numbers of disks is given
2325 #
2326 function verify_disk_count
2327 {
2328 typeset -i min=${2:-1}
2329
2330 typeset -i count=$(get_word_count "$1")
2331
2332 if ((count < min)); then
2333 log_untested "A minimum of $min disks is required to run." \
2334 " You specified $count disk(s)"
2335 fi
2336 }
2337
2338 function ds_is_volume
2339 {
2340 typeset type=$(get_prop type $1)
2341 [[ $type = "volume" ]] && return 0
2342 return 1
2343 }
2344
2345 function ds_is_filesystem
2346 {
2347 typeset type=$(get_prop type $1)
2348 [[ $type = "filesystem" ]] && return 0
2349 return 1
2350 }
2351
2352 function ds_is_snapshot
2353 {
2354 typeset type=$(get_prop type $1)
2355 [[ $type = "snapshot" ]] && return 0
2356 return 1
2357 }
2358
2359 #
2360 # Check if Trusted Extensions are installed and enabled
2361 #
2362 function is_te_enabled
2363 {
2364 svcs -H -o state labeld 2>/dev/null | grep "enabled"
2365 if (($? != 0)); then
2366 return 1
2367 else
2368 return 0
2369 fi
2370 }
2371
2372 # Utility function to determine if a system has multiple cpus.
2373 function is_mp
2374 {
2375 (($(psrinfo | wc -l) > 1))
2376 }
2377
2378 function get_cpu_freq
2379 {
2380 psrinfo -v 0 | awk '/processor operates at/ {print $6}'
2381 }
2382
2383 # Run the given command as the user provided.
2384 function user_run
2385 {
2386 typeset user=$1
2387 shift
2388
2389 eval su \$user -c \"$@\" > /tmp/out 2>/tmp/err
2390 return $?
2391 }
2392
2393 # Return 0 if the pool is successfully epxorted; $? otherwise
2394 #
2395 # $1 - pool name
2396 # Export pool.
2397 function export_pool #pool
2398 {
2399 typeset pool=${1%%/*}
2400 # Checking to see if the device is busy. If so, we'll
2401 # retry the export a few times with a sleep between tries.
2402 errmsg='device is busy'
2403 retry_num=$NUM_RETRIES
2404 TMPFILE=`mktemp`
2405 if [ -z "$TMPFILE" ] ; then
2406 log_note "Unable to create temporary file $TMPFILE"
2407 return 1
2408 fi
2409 until [ $retry_num == 0 ] ; do
2410 log_note "zpool export $pool"
2411 zpool export $pool 2>$TMPFILE
2412 # If the export failed, see if it's due to a
2413 # device is busy issue and retry if it is.
2414 if (( $? != 0 )); then
2415 # if this is busy then we want to retry
2416 if [ "`grep "$errmsg" $TMPFILE`" != "" ]; then
2417 retry_num-=1
2418 log_note "Device is busy, retry zpool export"
2419 sleep $EXPORT_SLEEP_TIME
2420 else # return here if error is something else
2421 rn $TMPFILE
2422 return 1
2423 fi
2424 else
2425 # export succeeded.
2426 rm $TMPFILE
2427 return 0
2428 fi
2429 done
2430 # We've reached our max retries, try to export one more time
2431 # and require it to succeed.
2432 log_must zpool export $pool
2433 }
2434
2435 #
2436 # Check if the pool contains the specified vdevs
2437 #
2438 # $1 pool
2439 # $2..n <vdev> ...
2440 #
2441 # Return 0 if the vdevs are contained in the pool, 1 if any of the specified
2442 # vdevs is not in the pool, and 2 if pool name is missing.
2443 #
2444 function vdevs_in_pool
2445 {
2446 typeset pool=$1
2447 typeset vdev
2448
2449 if [[ -z $pool ]]; then
2450 log_note "Missing pool name."
2451 return 2
2452 fi
2453
2454 shift
2455
2456 typeset tmpfile=$(mktemp)
2457 zpool list -Hv "$pool" >$tmpfile
2458 for vdev in $@; do
2459 grep -w ${vdev##*/} $tmpfile >/dev/null 2>&1
2460 [[ $? -ne 0 ]] && return 1
2461 done
2462
2463 rm -f $tmpfile
2464
2465 return 0;
2466 }
2467
2468 function get_max
2469 {
2470 typeset -l i max=$1
2471 shift
2472
2473 for i in "$@"; do
2474 max=$(echo $((max > i ? max : i)))
2475 done
2476
2477 echo $max
2478 }
2479
2480 function get_min
2481 {
2482 typeset -l i min=$1
2483 shift
2484
2485 for i in "$@"; do
2486 min=$(echo $((min < i ? min : i)))
2487 done
2488
2489 echo $min
2490 }
2491
2492 #
2493 # Generate a random number between 1 and the argument.
2494 #
2495 function random
2496 {
2497 typeset max=$1
2498 echo $(( ($RANDOM % $max) + 1 ))
2499 }
2500
2501 # Write data that can be compressed into a directory
2502 function write_compressible
2503 {
2504 typeset dir=$1
2505 typeset megs=$2
2506 typeset nfiles=${3:-1}
2507 typeset bs=${4:-1024k}
2508 typeset fname=${5:-file}
2509
2510 [[ -d $dir ]] || log_fail "No directory: $dir"
2511
2512 log_must eval "fio \
2513 --name=job \
2514 --fallocate=0 \
2515 --minimal \
2516 --randrepeat=0 \
2517 --buffer_compress_percentage=66 \
2518 --buffer_compress_chunk=4096 \
2519 --directory=$dir \
2520 --numjobs=$nfiles \
2521 --rw=write \
2522 --bs=$bs \
2523 --filesize=$megs \
2524 --filename_format='$fname.\$jobnum' >/dev/null"
2525 }
2526
2527 function get_objnum
2528 {
2529 typeset pathname=$1
2530 typeset objnum
2531
2532 [[ -e $pathname ]] || log_fail "No such file or directory: $pathname"
2533 objnum=$(stat -c %i $pathname)
2534 echo $objnum
2535 }