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