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.
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.
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]
23 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
25 # Copyright (c) 2012, 2017 by Delphix. All rights reserved.
26 # Copyright 2016 Nexenta Systems, Inc.
27 # Copyright (c) 2017 Lawrence Livermore National Security, LLC.
28 # Copyright (c) 2017 Datto Inc.
29 # Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 . ${STF_TOOLS}/include/logapi.shlib
33 . ${STF_SUITE}/include/math.shlib
34 . ${STF_SUITE}/include/blkdev.shlib
37 # Apply constrained path when available. This is required since the
38 # PATH may have been modified by sudo's secure_path behavior.
40 if [ -n "$STF_PATH" ]; then
45 # Generic dot version comparison function
47 # Returns success when version $1 is greater than or equal to $2.
49 function compare_version_gte
51 if [[ "$(printf "$1\n$2" | sort -V | tail -n1)" == "$1" ]]; then
58 # Linux kernel version comparison function
60 # $1 Linux version ("4.10", "2.6.32") or blank for installed Linux version
62 # Used for comparison: if [ $(linux_version) -ge $(linux_version "2.6.32") ]
64 function linux_version
68 [[ -z "$ver" ]] && ver=$(uname -r | grep -Eo "^[0-9]+\.[0-9]+\.[0-9]+")
70 typeset version=$(echo $ver | cut -d '.' -f 1)
71 typeset major=$(echo $ver | cut -d '.' -f 2)
72 typeset minor=$(echo $ver | cut -d '.' -f 3)
74 [[ -z "$version" ]] && version=0
75 [[ -z "$major" ]] && major=0
76 [[ -z "$minor" ]] && minor=0
78 echo $((version * 10000 + major * 100 + minor))
81 # Determine if this is a Linux test system
83 # Return 0 if platform Linux, 1 if otherwise
87 if [[ $(uname -o) == "GNU/Linux" ]]; then
94 # Determine if this is a 32-bit system
96 # Return 0 if platform is 32-bit, 1 if otherwise
100 if [[ $(getconf LONG_BIT) == "32" ]]; then
107 # Determine if kmemleak is enabled
109 # Return 0 if kmemleak is enabled, 1 if otherwise
113 if is_linux && [[ -e /sys/kernel/debug/kmemleak ]]; then
120 # Determine whether a dataset is mounted
123 # $2 filesystem type; optional - defaulted to zfs
125 # Return 0 if dataset is mounted; 1 if unmounted; 2 on error
130 [[ -z $fstype ]] && fstype=zfs
131 typeset out dir name ret
135 if [[ "$1" == "/"* ]] ; then
136 for out in $(zfs mount | awk '{print $2}'); do
137 [[ $1 == $out ]] && return 0
140 for out in $(zfs mount | awk '{print $1}'); do
141 [[ $1 == $out ]] && return 0
146 out=$(df -F $fstype $1 2>/dev/null)
148 (($ret != 0)) && return $ret
156 [[ "$1" == "$dir" || "$1" == "$name" ]] && return 0
159 out=$(df -t $fstype $1 2>/dev/null)
163 if [[ -L "$ZVOL_DEVDIR/$1" ]]; then
164 link=$(readlink -f $ZVOL_DEVDIR/$1)
165 [[ -n "$link" ]] && \
166 mount | grep -q "^$link" && \
175 # Return 0 if a dataset is mounted; 1 otherwise
178 # $2 filesystem type; optional - defaulted to zfs
183 (($? == 0)) && return 0
187 # Return 0 if a dataset is unmounted; 1 otherwise
190 # $2 filesystem type; optional - defaulted to zfs
195 (($? == 1)) && return 0
205 echo $1 | sed "s/,/ /g"
208 function default_setup
210 default_setup_noexit "$@"
216 # Given a list of disks, setup storage pools and datasets.
218 function default_setup_noexit
223 log_note begin default_setup_noexit
225 if is_global_zone; then
226 if poolexists $TESTPOOL ; then
227 destroy_pool $TESTPOOL
229 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
230 log_must zpool create -f $TESTPOOL $disklist
235 rm -rf $TESTDIR || log_unresolved Could not remove $TESTDIR
236 mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR
238 log_must zfs create $TESTPOOL/$TESTFS
239 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
241 if [[ -n $container ]]; then
242 rm -rf $TESTDIR1 || \
243 log_unresolved Could not remove $TESTDIR1
244 mkdir -p $TESTDIR1 || \
245 log_unresolved Could not create $TESTDIR1
247 log_must zfs create $TESTPOOL/$TESTCTR
248 log_must zfs set canmount=off $TESTPOOL/$TESTCTR
249 log_must zfs create $TESTPOOL/$TESTCTR/$TESTFS1
250 log_must zfs set mountpoint=$TESTDIR1 \
251 $TESTPOOL/$TESTCTR/$TESTFS1
254 if [[ -n $volume ]]; then
255 if is_global_zone ; then
256 log_must zfs create -V $VOLSIZE $TESTPOOL/$TESTVOL
259 log_must zfs create $TESTPOOL/$TESTVOL
265 # Given a list of disks, setup a storage pool, file system and
268 function default_container_setup
272 default_setup "$disklist" "true"
276 # Given a list of disks, setup a storage pool,file system
279 function default_volume_setup
283 default_setup "$disklist" "" "true"
287 # Given a list of disks, setup a storage pool,file system,
288 # a container and a volume.
290 function default_container_volume_setup
294 default_setup "$disklist" "true" "true"
298 # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
301 # $1 Existing filesystem or volume name. Default, $TESTPOOL/$TESTFS
302 # $2 snapshot name. Default, $TESTSNAP
304 function create_snapshot
306 typeset fs_vol=${1:-$TESTPOOL/$TESTFS}
307 typeset snap=${2:-$TESTSNAP}
309 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
310 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
312 if snapexists $fs_vol@$snap; then
313 log_fail "$fs_vol@$snap already exists."
315 datasetexists $fs_vol || \
316 log_fail "$fs_vol must exist."
318 log_must zfs snapshot $fs_vol@$snap
322 # Create a clone from a snapshot, default clone name is $TESTCLONE.
324 # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
325 # $2 Clone name, $TESTPOOL/$TESTCLONE is default.
327 function create_clone # snapshot clone
329 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
330 typeset clone=${2:-$TESTPOOL/$TESTCLONE}
333 log_fail "Snapshot name is undefined."
335 log_fail "Clone name is undefined."
337 log_must zfs clone $snap $clone
341 # Create a bookmark of the given snapshot. Defaultly create a bookmark on
344 # $1 Existing filesystem or volume name. Default, $TESTFS
345 # $2 Existing snapshot name. Default, $TESTSNAP
346 # $3 bookmark name. Default, $TESTBKMARK
348 function create_bookmark
350 typeset fs_vol=${1:-$TESTFS}
351 typeset snap=${2:-$TESTSNAP}
352 typeset bkmark=${3:-$TESTBKMARK}
354 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
355 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
356 [[ -z $bkmark ]] && log_fail "Bookmark's name is undefined."
358 if bkmarkexists $fs_vol#$bkmark; then
359 log_fail "$fs_vol#$bkmark already exists."
361 datasetexists $fs_vol || \
362 log_fail "$fs_vol must exist."
363 snapexists $fs_vol@$snap || \
364 log_fail "$fs_vol@$snap must exist."
366 log_must zfs bookmark $fs_vol@$snap $fs_vol#$bkmark
370 # Create a temporary clone result of an interrupted resumable 'zfs receive'
371 # $1 Destination filesystem name. Must not exist, will be created as the result
372 # of this function along with its %recv temporary clone
373 # $2 Source filesystem name. Must not exist, will be created and destroyed
375 function create_recv_clone
378 typeset sendfs="${2:-$TESTPOOL/create_recv_clone}"
379 typeset snap="$sendfs@snap1"
380 typeset incr="$sendfs@snap2"
381 typeset mountpoint="$TESTDIR/create_recv_clone"
382 typeset sendfile="$TESTDIR/create_recv_clone.zsnap"
384 [[ -z $recvfs ]] && log_fail "Recv filesystem's name is undefined."
386 datasetexists $recvfs && log_fail "Recv filesystem must not exist."
387 datasetexists $sendfs && log_fail "Send filesystem must not exist."
389 log_must zfs create -o mountpoint="$mountpoint" $sendfs
390 log_must zfs snapshot $snap
391 log_must eval "zfs send $snap | zfs recv -u $recvfs"
392 log_must mkfile 1m "$mountpoint/data"
393 log_must zfs snapshot $incr
394 log_must eval "zfs send -i $snap $incr | dd bs=10K count=1 > $sendfile"
395 log_mustnot eval "zfs recv -su $recvfs < $sendfile"
396 destroy_dataset "$sendfs" "-r"
397 log_must rm -f "$sendfile"
399 if [[ $(get_prop 'inconsistent' "$recvfs/%recv") -ne 1 ]]; then
400 log_fail "Error creating temporary $recvfs/%recv clone"
404 function default_mirror_setup
406 default_mirror_setup_noexit $1 $2 $3
412 # Given a pair of disks, set up a storage pool and dataset for the mirror
413 # @parameters: $1 the primary side of the mirror
414 # $2 the secondary side of the mirror
415 # @uses: ZPOOL ZFS TESTPOOL TESTFS
416 function default_mirror_setup_noexit
418 readonly func="default_mirror_setup_noexit"
422 [[ -z $primary ]] && \
423 log_fail "$func: No parameters passed"
424 [[ -z $secondary ]] && \
425 log_fail "$func: No secondary partition passed"
426 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
427 log_must zpool create -f $TESTPOOL mirror $@
428 log_must zfs create $TESTPOOL/$TESTFS
429 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
433 # create a number of mirrors.
434 # We create a number($1) of 2 way mirrors using the pairs of disks named
435 # on the command line. These mirrors are *not* mounted
436 # @parameters: $1 the number of mirrors to create
437 # $... the devices to use to create the mirrors on
438 # @uses: ZPOOL ZFS TESTPOOL
439 function setup_mirrors
441 typeset -i nmirrors=$1
444 while ((nmirrors > 0)); do
445 log_must test -n "$1" -a -n "$2"
446 [[ -d /$TESTPOOL$nmirrors ]] && rm -rf /$TESTPOOL$nmirrors
447 log_must zpool create -f $TESTPOOL$nmirrors mirror $1 $2
449 ((nmirrors = nmirrors - 1))
454 # create a number of raidz pools.
455 # We create a number($1) of 2 raidz pools using the pairs of disks named
456 # on the command line. These pools are *not* mounted
457 # @parameters: $1 the number of pools to create
458 # $... the devices to use to create the pools on
459 # @uses: ZPOOL ZFS TESTPOOL
460 function setup_raidzs
462 typeset -i nraidzs=$1
465 while ((nraidzs > 0)); do
466 log_must test -n "$1" -a -n "$2"
467 [[ -d /$TESTPOOL$nraidzs ]] && rm -rf /$TESTPOOL$nraidzs
468 log_must zpool create -f $TESTPOOL$nraidzs raidz $1 $2
470 ((nraidzs = nraidzs - 1))
475 # Destroy the configured testpool mirrors.
476 # the mirrors are of the form ${TESTPOOL}{number}
477 # @uses: ZPOOL ZFS TESTPOOL
478 function destroy_mirrors
480 default_cleanup_noexit
486 # Given a minimum of two disks, set up a storage pool and dataset for the raid-z
487 # $1 the list of disks
489 function default_raidz_setup
491 typeset disklist="$*"
492 disks=(${disklist[*]})
494 if [[ ${#disks[*]} -lt 2 ]]; then
495 log_fail "A raid-z requires a minimum of two disks."
498 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
499 log_must zpool create -f $TESTPOOL raidz $disklist
500 log_must zfs create $TESTPOOL/$TESTFS
501 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
507 # Common function used to cleanup storage pools and datasets.
509 # Invoked at the start of the test suite to ensure the system
510 # is in a known state, and also at the end of each set of
511 # sub-tests to ensure errors from one set of tests doesn't
512 # impact the execution of the next set.
514 function default_cleanup
516 default_cleanup_noexit
522 # Utility function used to list all available pool names.
524 # NOTE: $KEEP is a variable containing pool names, separated by a newline
525 # character, that must be excluded from the returned list.
527 function get_all_pools
529 zpool list -H -o name | grep -Fvx "$KEEP" | grep -v "$NO_POOLS"
532 function default_cleanup_noexit
536 # Destroying the pool will also destroy any
537 # filesystems it contains.
539 if is_global_zone; then
540 zfs unmount -a > /dev/null 2>&1
541 ALL_POOLS=$(get_all_pools)
542 # Here, we loop through the pools we're allowed to
543 # destroy, only destroying them if it's safe to do
545 while [ ! -z ${ALL_POOLS} ]
547 for pool in ${ALL_POOLS}
549 if safe_to_destroy_pool $pool ;
553 ALL_POOLS=$(get_all_pools)
560 for fs in $(zfs list -H -o name \
561 | grep "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
562 destroy_dataset "$fs" "-Rf"
565 # Need cleanup here to avoid garbage dir left.
566 for fs in $(zfs list -H -o name); do
567 [[ $fs == /$ZONE_POOL ]] && continue
568 [[ -d $fs ]] && log_must rm -rf $fs/*
572 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
575 for fs in $(zfs list -H -o name); do
576 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
577 log_must zfs set reservation=none $fs
578 log_must zfs set recordsize=128K $fs
579 log_must zfs set mountpoint=/$fs $fs
581 enc=$(get_prop encryption $fs)
582 if [[ $? -ne 0 ]] || [[ -z "$enc" ]] || \
583 [[ "$enc" == "off" ]]; then
584 log_must zfs set checksum=on $fs
586 log_must zfs set compression=off $fs
587 log_must zfs set atime=on $fs
588 log_must zfs set devices=off $fs
589 log_must zfs set exec=on $fs
590 log_must zfs set setuid=on $fs
591 log_must zfs set readonly=off $fs
592 log_must zfs set snapdir=hidden $fs
593 log_must zfs set aclmode=groupmask $fs
594 log_must zfs set aclinherit=secure $fs
599 [[ -d $TESTDIR ]] && \
600 log_must rm -rf $TESTDIR
603 if is_mpath_device $disk1; then
607 rm -f $TEST_BASE_DIR/{err,out}
612 # Common function used to cleanup storage pools, file systems
615 function default_container_cleanup
617 if ! is_global_zone; then
621 ismounted $TESTPOOL/$TESTCTR/$TESTFS1
623 log_must zfs unmount $TESTPOOL/$TESTCTR/$TESTFS1
625 destroy_dataset "$TESTPOOL/$TESTCTR/$TESTFS1" "-R"
626 destroy_dataset "$TESTPOOL/$TESTCTR" "-Rf"
628 [[ -e $TESTDIR1 ]] && \
629 log_must rm -rf $TESTDIR1 > /dev/null 2>&1
635 # Common function used to cleanup snapshot of file system or volume. Default to
636 # delete the file system's snapshot
640 function destroy_snapshot
642 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
644 if ! snapexists $snap; then
645 log_fail "'$snap' does not exist."
649 # For the sake of the value which come from 'get_prop' is not equal
650 # to the really mountpoint when the snapshot is unmounted. So, firstly
651 # check and make sure this snapshot's been mounted in current system.
654 if ismounted $snap; then
655 mtpt=$(get_prop mountpoint $snap)
657 log_fail "get_prop mountpoint $snap failed."
660 destroy_dataset "$snap"
661 [[ $mtpt != "" && -d $mtpt ]] && \
662 log_must rm -rf $mtpt
666 # Common function used to cleanup clone.
670 function destroy_clone
672 typeset clone=${1:-$TESTPOOL/$TESTCLONE}
674 if ! datasetexists $clone; then
675 log_fail "'$clone' does not existed."
678 # With the same reason in destroy_snapshot
680 if ismounted $clone; then
681 mtpt=$(get_prop mountpoint $clone)
683 log_fail "get_prop mountpoint $clone failed."
686 destroy_dataset "$clone"
687 [[ $mtpt != "" && -d $mtpt ]] && \
688 log_must rm -rf $mtpt
692 # Common function used to cleanup bookmark of file system or volume. Default
693 # to delete the file system's bookmark.
697 function destroy_bookmark
699 typeset bkmark=${1:-$TESTPOOL/$TESTFS#$TESTBKMARK}
701 if ! bkmarkexists $bkmark; then
702 log_fail "'$bkmarkp' does not existed."
705 destroy_dataset "$bkmark"
708 # Return 0 if a snapshot exists; $? otherwise
714 zfs list -H -t snapshot "$1" > /dev/null 2>&1
719 # Return 0 if a bookmark exists; $? otherwise
723 function bkmarkexists
725 zfs list -H -t bookmark "$1" > /dev/null 2>&1
730 # Set a property to a certain value on a dataset.
731 # Sets a property of the dataset to the value as passed in.
733 # $1 dataset who's property is being set
735 # $3 value to set property to
737 # 0 if the property could be set.
738 # non-zero otherwise.
741 function dataset_setprop
743 typeset fn=dataset_setprop
746 log_note "$fn: Insufficient parameters (need 3, had $#)"
750 output=$(zfs set $2=$3 $1 2>&1)
753 log_note "Setting property on $1 failed."
754 log_note "property $2=$3"
755 log_note "Return Code: $rv"
756 log_note "Output: $output"
763 # Assign suite defined dataset properties.
764 # This function is used to apply the suite's defined default set of
765 # properties to a dataset.
766 # @parameters: $1 dataset to use
767 # @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
769 # 0 if the dataset has been altered.
770 # 1 if no pool name was passed in.
771 # 2 if the dataset could not be found.
772 # 3 if the dataset could not have it's properties set.
774 function dataset_set_defaultproperties
778 [[ -z $dataset ]] && return 1
782 for confset in $(zfs list); do
783 if [[ $dataset = $confset ]]; then
788 [[ $found -eq 0 ]] && return 2
789 if [[ -n $COMPRESSION_PROP ]]; then
790 dataset_setprop $dataset compression $COMPRESSION_PROP || \
792 log_note "Compression set to '$COMPRESSION_PROP' on $dataset"
794 if [[ -n $CHECKSUM_PROP ]]; then
795 dataset_setprop $dataset checksum $CHECKSUM_PROP || \
797 log_note "Checksum set to '$CHECKSUM_PROP' on $dataset"
803 # Check a numeric assertion
804 # @parameter: $@ the assertion to check
805 # @output: big loud notice if assertion failed
810 (($@)) || log_fail "$@"
814 # Function to format partition size of a disk
815 # Given a disk cxtxdx reduces all partitions
818 function zero_partitions #<whole_disk_name>
824 DSK=$DEV_DSKDIR/$diskname
825 DSK=$(echo $DSK | sed -e "s|//|/|g")
826 log_must parted $DSK -s -- mklabel gpt
827 blockdev --rereadpt $DSK 2>/dev/null
830 for i in 0 1 3 4 5 6 7
832 log_must set_partition $i "" 0mb $diskname
840 # Given a slice, size and disk, this function
841 # formats the slice to the specified size.
842 # Size should be specified with units as per
843 # the `format` command requirements eg. 100mb 3gb
845 # NOTE: This entire interface is problematic for the Linux parted utilty
846 # which requires the end of the partition to be specified. It would be
847 # best to retire this interface and replace it with something more flexible.
848 # At the moment a best effort is made.
850 function set_partition #<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
852 typeset -i slicenum=$1
858 if [[ -z $size || -z $disk ]]; then
859 log_fail "The size or disk name is unspecified."
861 typeset size_mb=${size%%[mMgG]}
863 size_mb=${size_mb%%[mMgG][bB]}
864 if [[ ${size:1:1} == 'g' ]]; then
865 ((size_mb = size_mb * 1024))
868 # Create GPT partition table when setting slice 0 or
869 # when the device doesn't already contain a GPT label.
870 parted $DEV_DSKDIR/$disk -s -- print 1 >/dev/null
872 if [[ $slicenum -eq 0 || $ret_val -ne 0 ]]; then
873 parted $DEV_DSKDIR/$disk -s -- mklabel gpt
874 if [[ $? -ne 0 ]]; then
875 log_note "Failed to create GPT partition table on $disk"
880 # When no start is given align on the first cylinder.
881 if [[ -z "$start" ]]; then
885 # Determine the cylinder size for the device and using
886 # that calculate the end offset in cylinders.
887 typeset -i cly_size_kb=0
888 cly_size_kb=$(parted -m $DEV_DSKDIR/$disk -s -- \
889 unit cyl print | head -3 | tail -1 | \
890 awk -F '[:k.]' '{print $4}')
891 ((end = (size_mb * 1024 / cly_size_kb) + start))
893 parted $DEV_DSKDIR/$disk -s -- \
894 mkpart part$slicenum ${start}cyl ${end}cyl
895 if [[ $? -ne 0 ]]; then
896 log_note "Failed to create partition $slicenum on $disk"
900 blockdev --rereadpt $DEV_DSKDIR/$disk 2>/dev/null
903 if [[ -z $slicenum || -z $size || -z $disk ]]; then
904 log_fail "The slice, size or disk name is unspecified."
907 typeset format_file=/var/tmp/format_in.$$
909 echo "partition" >$format_file
910 echo "$slicenum" >> $format_file
911 echo "" >> $format_file
912 echo "" >> $format_file
913 echo "$start" >> $format_file
914 echo "$size" >> $format_file
915 echo "label" >> $format_file
916 echo "" >> $format_file
917 echo "q" >> $format_file
918 echo "q" >> $format_file
920 format -e -s -d $disk -f $format_file
925 if [[ $ret_val -ne 0 ]]; then
926 log_note "Unable to format $disk slice $slicenum to $size"
933 # Delete all partitions on all disks - this is specifically for the use of multipath
934 # devices which currently can only be used in the test suite as raw/un-partitioned
935 # devices (ie a zpool cannot be created on a whole mpath device that has partitions)
937 function delete_partitions
941 if [[ -z $DISK_ARRAY_NUM ]]; then
942 DISK_ARRAY_NUM=$(echo ${DISKS} | nawk '{print NF}')
944 if [[ -z $DISKSARRAY ]]; then
949 if (( $DISK_ARRAY_NUM == 1 )); then
950 while ((j < MAX_PARTITIONS)); do
951 parted $DEV_DSKDIR/$DISK -s rm $j \
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"
958 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
962 lsblk | egrep ${DISK}${SLICE_PREFIX}${j} > /dev/null
963 if (( $? == 0 )); then
964 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
970 for disk in `echo $DISKSARRAY`; do
971 while ((j < MAX_PARTITIONS)); do
972 parted $DEV_DSKDIR/$disk -s rm $j > /dev/null 2>&1
973 if (( $? == 1 )); then
974 lsblk | egrep ${disk}${SLICE_PREFIX}${j} > /dev/null
975 if (( $? == 1 )); then
976 log_note "Partitions for $disk should be deleted"
978 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
982 lsblk | egrep ${disk}${SLICE_PREFIX}${j} > /dev/null
983 if (( $? == 0 )); then
984 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
997 # Get the end cyl of the given slice
999 function get_endslice #<disk> <slice>
1003 if [[ -z $disk || -z $slice ]] ; then
1004 log_fail "The disk name or slice number is unspecified."
1008 endcyl=$(parted -s $DEV_DSKDIR/$disk -- unit cyl print | \
1009 grep "part${slice}" | \
1010 awk '{print $3}' | \
1012 ((endcyl = (endcyl + 1)))
1014 disk=${disk#/dev/dsk/}
1015 disk=${disk#/dev/rdsk/}
1019 ratio=$(prtvtoc /dev/rdsk/${disk}s2 | \
1020 grep "sectors\/cylinder" | \
1023 if ((ratio == 0)); then
1027 typeset -i endcyl=$(prtvtoc -h /dev/rdsk/${disk}s2 |
1028 nawk -v token="$slice" '{if ($1==token) print $6}')
1030 ((endcyl = (endcyl + 1) / ratio))
1038 # Given a size,disk and total slice number, this function formats the
1039 # disk slices from 0 to the total slice number with the same specified
1042 function partition_disk #<slice_size> <whole_disk_name> <total_slices>
1045 typeset slice_size=$1
1046 typeset disk_name=$2
1047 typeset total_slices=$3
1050 zero_partitions $disk_name
1051 while ((i < $total_slices)); do
1058 log_must set_partition $i "$cyl" $slice_size $disk_name
1059 cyl=$(get_endslice $disk_name $i)
1065 # This function continues to write to a filenum number of files into dirnum
1066 # number of directories until either file_write returns an error or the
1067 # maximum number of files per directory have been written.
1070 # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
1072 # Return value: 0 on success
1076 # destdir: is the directory where everything is to be created under
1077 # dirnum: the maximum number of subdirectories to use, -1 no limit
1078 # filenum: the maximum number of files per subdirectory
1079 # bytes: number of bytes to write
1080 # num_writes: numer of types to write out bytes
1081 # data: the data that will be written
1084 # file_fs /testdir 20 25 1024 256 0
1086 # Note: bytes * num_writes equals the size of the testfile
1088 function fill_fs # destdir dirnum filenum bytes num_writes data
1090 typeset destdir=${1:-$TESTDIR}
1091 typeset -i dirnum=${2:-50}
1092 typeset -i filenum=${3:-50}
1093 typeset -i bytes=${4:-8192}
1094 typeset -i num_writes=${5:-10240}
1095 typeset -i data=${6:-0}
1097 typeset -i odirnum=1
1098 typeset -i idirnum=0
1102 log_must mkdir -p $destdir/$idirnum
1103 while (($odirnum > 0)); do
1104 if ((dirnum >= 0 && idirnum >= dirnum)); then
1108 file_write -o create -f $destdir/$idirnum/$TESTFILE.$fn \
1109 -b $bytes -c $num_writes -d $data
1111 if (($retval != 0)); then
1115 if (($fn >= $filenum)); then
1117 ((idirnum = idirnum + 1))
1118 log_must mkdir -p $destdir/$idirnum
1127 # Simple function to get the specified property. If unable to
1128 # get the property then exits.
1130 # Note property is in 'parsable' format (-p)
1132 function get_prop # property dataset
1138 prop_val=$(zfs get -pH -o value $prop $dataset 2>/dev/null)
1139 if [[ $? -ne 0 ]]; then
1140 log_note "Unable to get $prop property for dataset " \
1150 # Simple function to get the specified property of pool. If unable to
1151 # get the property then exits.
1153 # Note property is in 'parsable' format (-p)
1155 function get_pool_prop # property pool
1161 if poolexists $pool ; then
1162 prop_val=$(zpool get -pH $prop $pool 2>/dev/null | tail -1 | \
1164 if [[ $? -ne 0 ]]; then
1165 log_note "Unable to get $prop property for pool " \
1170 log_note "Pool $pool not exists."
1178 # Return 0 if a pool exists; $? otherwise
1186 if [[ -z $pool ]]; then
1187 log_note "No pool name given."
1191 zpool get name "$pool" > /dev/null 2>&1
1195 # Return 0 if all the specified datasets exist; $? otherwise
1198 function datasetexists
1200 if (($# == 0)); then
1201 log_note "No dataset name given."
1205 while (($# > 0)); do
1206 zfs get name $1 > /dev/null 2>&1 || \
1214 # return 0 if none of the specified datasets exists, otherwise return 1.
1217 function datasetnonexists
1219 if (($# == 0)); then
1220 log_note "No dataset name given."
1224 while (($# > 0)); do
1225 zfs list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \
1233 function is_shared_impl
1239 for mtpt in `share | awk '{print $1}'` ; do
1240 if [[ $mtpt == $fs ]] ; then
1247 for mtpt in `share | awk '{print $2}'` ; do
1248 if [[ $mtpt == $fs ]] ; then
1253 typeset stat=$(svcs -H -o STA nfs/server:default)
1254 if [[ $stat != "ON" ]]; then
1255 log_note "Current nfs/server status: $stat"
1262 # Given a mountpoint, or a dataset name, determine if it is shared via NFS.
1264 # Returns 0 if shared, 1 otherwise.
1271 if [[ $fs != "/"* ]] ; then
1272 if datasetnonexists "$fs" ; then
1275 mtpt=$(get_prop mountpoint "$fs")
1277 none|legacy|-) return 1
1285 is_shared_impl "$fs"
1289 # Given a dataset name determine if it is shared via SMB.
1291 # Returns 0 if shared, 1 otherwise.
1293 function is_shared_smb
1298 if datasetnonexists "$fs" ; then
1301 fs=$(echo $fs | sed 's@/@_@g')
1305 for mtpt in `net usershare list | awk '{print $1}'` ; do
1306 if [[ $mtpt == $fs ]] ; then
1312 log_unsupported "Currently unsupported by the test framework"
1318 # Given a mountpoint, determine if it is not shared via NFS.
1320 # Returns 0 if not shared, 1 otherwise.
1327 if (($? == 0)); then
1335 # Given a dataset determine if it is not shared via SMB.
1337 # Returns 0 if not shared, 1 otherwise.
1339 function not_shared_smb
1344 if (($? == 0)); then
1352 # Helper function to unshare a mountpoint.
1354 function unshare_fs #fs
1358 is_shared $fs || is_shared_smb $fs
1359 if (($? == 0)); then
1360 log_must zfs unshare $fs
1367 # Helper function to share a NFS mountpoint.
1369 function share_nfs #fs
1375 if (($? != 0)); then
1376 log_must share "*:$fs"
1380 if (($? != 0)); then
1381 log_must share -F nfs $fs
1389 # Helper function to unshare a NFS mountpoint.
1391 function unshare_nfs #fs
1397 if (($? == 0)); then
1398 log_must unshare -u "*:$fs"
1402 if (($? == 0)); then
1403 log_must unshare -F nfs $fs
1411 # Helper function to show NFS shares.
1413 function showshares_nfs
1425 # Helper function to show SMB shares.
1427 function showshares_smb
1439 # Check NFS server status and trigger it online.
1441 function setup_nfs_server
1443 # Cannot share directory in non-global zone.
1445 if ! is_global_zone; then
1446 log_note "Cannot trigger NFS server by sharing in LZ."
1452 # Re-synchronize /var/lib/nfs/etab with /etc/exports and
1453 # /etc/exports.d./* to provide a clean test environment.
1457 log_note "NFS server must be started prior to running ZTS."
1461 typeset nfs_fmri="svc:/network/nfs/server:default"
1462 if [[ $(svcs -Ho STA $nfs_fmri) != "ON" ]]; then
1464 # Only really sharing operation can enable NFS server
1465 # to online permanently.
1467 typeset dummy=/tmp/dummy
1469 if [[ -d $dummy ]]; then
1470 log_must rm -rf $dummy
1473 log_must mkdir $dummy
1474 log_must share $dummy
1477 # Waiting for fmri's status to be the final status.
1478 # Otherwise, in transition, an asterisk (*) is appended for
1479 # instances, unshare will reverse status to 'DIS' again.
1481 # Waiting for 1's at least.
1485 while [[ timeout -ne 0 && $(svcs -Ho STA $nfs_fmri) == *'*' ]]
1492 log_must unshare $dummy
1493 log_must rm -rf $dummy
1496 log_note "Current NFS status: '$(svcs -Ho STA,FMRI $nfs_fmri)'"
1500 # To verify whether calling process is in global zone
1502 # Return 0 if in global zone, 1 in non-global zone
1504 function is_global_zone
1509 typeset cur_zone=$(zonename 2>/dev/null)
1510 if [[ $cur_zone != "global" ]]; then
1518 # Verify whether test is permitted to run from
1519 # global zone, local zone, or both
1521 # $1 zone limit, could be "global", "local", or "both"(no limit)
1523 # Return 0 if permitted, otherwise exit with log_unsupported
1525 function verify_runnable # zone limit
1529 [[ -z $limit ]] && return 0
1531 if is_global_zone ; then
1535 local) log_unsupported "Test is unable to run from "\
1538 *) log_note "Warning: unknown limit $limit - " \
1546 global) log_unsupported "Test is unable to run from "\
1549 *) log_note "Warning: unknown limit $limit - " \
1560 # Return 0 if create successfully or the pool exists; $? otherwise
1561 # Note: In local zones, this function should return 0 silently.
1564 # $2-n - [keyword] devs_list
1566 function create_pool #pool devs_list
1568 typeset pool=${1%%/*}
1572 if [[ -z $pool ]]; then
1573 log_note "Missing pool name."
1577 if poolexists $pool ; then
1581 if is_global_zone ; then
1582 [[ -d /$pool ]] && rm -rf /$pool
1583 log_must zpool create -f $pool $@
1589 # Return 0 if destroy successfully or the pool exists; $? otherwise
1590 # Note: In local zones, this function should return 0 silently.
1593 # Destroy pool with the given parameters.
1595 function destroy_pool #pool
1597 typeset pool=${1%%/*}
1600 if [[ -z $pool ]]; then
1601 log_note "No pool name given."
1605 if is_global_zone ; then
1606 if poolexists "$pool" ; then
1607 mtpt=$(get_prop mountpoint "$pool")
1609 # At times, syseventd/udev activity can cause attempts
1610 # to destroy a pool to fail with EBUSY. We retry a few
1611 # times allowing failures before requiring the destroy
1613 log_must_busy zpool destroy -f $pool
1616 log_must rm -rf $mtpt
1618 log_note "Pool does not exist. ($pool)"
1626 # Return 0 if created successfully; $? otherwise
1629 # $2-n - dataset options
1631 function create_dataset #dataset dataset_options
1637 if [[ -z $dataset ]]; then
1638 log_note "Missing dataset name."
1642 if datasetexists $dataset ; then
1643 destroy_dataset $dataset
1646 log_must zfs create $@ $dataset
1651 # Return 0 if destroy successfully or the dataset exists; $? otherwise
1652 # Note: In local zones, this function should return 0 silently.
1655 # $2 - custom arguments for zfs destroy
1656 # Destroy dataset with the given parameters.
1658 function destroy_dataset #dataset #args
1662 typeset args=${2:-""}
1664 if [[ -z $dataset ]]; then
1665 log_note "No dataset name given."
1669 if is_global_zone ; then
1670 if datasetexists "$dataset" ; then
1671 mtpt=$(get_prop mountpoint "$dataset")
1672 log_must_busy zfs destroy $args $dataset
1675 log_must rm -rf $mtpt
1677 log_note "Dataset does not exist. ($dataset)"
1686 # Firstly, create a pool with 5 datasets. Then, create a single zone and
1687 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
1688 # and a zvol device to the zone.
1691 # $2 zone root directory prefix
1694 function zfs_zones_setup #zone_name zone_root zone_ip
1696 typeset zone_name=${1:-$(hostname)-z}
1697 typeset zone_root=${2:-"/zone_root"}
1698 typeset zone_ip=${3:-"10.1.1.10"}
1699 typeset prefix_ctr=$ZONE_CTR
1700 typeset pool_name=$ZONE_POOL
1704 # Create pool and 5 container within it
1706 [[ -d /$pool_name ]] && rm -rf /$pool_name
1707 log_must zpool create -f $pool_name $DISKS
1708 while ((i < cntctr)); do
1709 log_must zfs create $pool_name/$prefix_ctr$i
1714 log_must zfs create -V 1g $pool_name/zone_zvol
1718 # If current system support slog, add slog device for pool
1720 if verify_slog_support ; then
1721 typeset sdevs="$TEST_BASE_DIR/sdev1 $TEST_BASE_DIR/sdev2"
1722 log_must mkfile $MINVDEVSIZE $sdevs
1723 log_must zpool add $pool_name log mirror $sdevs
1726 # this isn't supported just yet.
1727 # Create a filesystem. In order to add this to
1728 # the zone, it must have it's mountpoint set to 'legacy'
1729 # log_must zfs create $pool_name/zfs_filesystem
1730 # log_must zfs set mountpoint=legacy $pool_name/zfs_filesystem
1732 [[ -d $zone_root ]] && \
1733 log_must rm -rf $zone_root/$zone_name
1734 [[ ! -d $zone_root ]] && \
1735 log_must mkdir -p -m 0700 $zone_root/$zone_name
1737 # Create zone configure file and configure the zone
1739 typeset zone_conf=/tmp/zone_conf.$$
1740 echo "create" > $zone_conf
1741 echo "set zonepath=$zone_root/$zone_name" >> $zone_conf
1742 echo "set autoboot=true" >> $zone_conf
1744 while ((i < cntctr)); do
1745 echo "add dataset" >> $zone_conf
1746 echo "set name=$pool_name/$prefix_ctr$i" >> \
1748 echo "end" >> $zone_conf
1752 # add our zvol to the zone
1753 echo "add device" >> $zone_conf
1754 echo "set match=/dev/zvol/dsk/$pool_name/zone_zvol" >> $zone_conf
1755 echo "end" >> $zone_conf
1757 # add a corresponding zvol rdsk to the zone
1758 echo "add device" >> $zone_conf
1759 echo "set match=$ZVOL_RDEVDIR/$pool_name/zone_zvol" >> $zone_conf
1760 echo "end" >> $zone_conf
1762 # once it's supported, we'll add our filesystem to the zone
1763 # echo "add fs" >> $zone_conf
1764 # echo "set type=zfs" >> $zone_conf
1765 # echo "set special=$pool_name/zfs_filesystem" >> $zone_conf
1766 # echo "set dir=/export/zfs_filesystem" >> $zone_conf
1767 # echo "end" >> $zone_conf
1769 echo "verify" >> $zone_conf
1770 echo "commit" >> $zone_conf
1771 log_must zonecfg -z $zone_name -f $zone_conf
1772 log_must rm -f $zone_conf
1775 zoneadm -z $zone_name install
1776 if (($? == 0)); then
1777 log_note "SUCCESS: zoneadm -z $zone_name install"
1779 log_fail "FAIL: zoneadm -z $zone_name install"
1782 # Install sysidcfg file
1784 typeset sysidcfg=$zone_root/$zone_name/root/etc/sysidcfg
1785 echo "system_locale=C" > $sysidcfg
1786 echo "terminal=dtterm" >> $sysidcfg
1787 echo "network_interface=primary {" >> $sysidcfg
1788 echo "hostname=$zone_name" >> $sysidcfg
1789 echo "}" >> $sysidcfg
1790 echo "name_service=NONE" >> $sysidcfg
1791 echo "root_password=mo791xfZ/SFiw" >> $sysidcfg
1792 echo "security_policy=NONE" >> $sysidcfg
1793 echo "timezone=US/Eastern" >> $sysidcfg
1796 log_must zoneadm -z $zone_name boot
1800 # Reexport TESTPOOL & TESTPOOL(1-4)
1802 function reexport_pool
1807 while ((i < cntctr)); do
1809 TESTPOOL=$ZONE_POOL/$ZONE_CTR$i
1810 if ! ismounted $TESTPOOL; then
1811 log_must zfs mount $TESTPOOL
1814 eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i
1815 if eval ! ismounted \$TESTPOOL$i; then
1816 log_must eval zfs mount \$TESTPOOL$i
1824 # Verify a given disk or pool state
1826 # Return 0 is pool/disk matches expected state, 1 otherwise
1828 function check_state # pool disk state{online,offline,degraded}
1831 typeset disk=${2#$DEV_DSKDIR/}
1834 [[ -z $pool ]] || [[ -z $state ]] \
1835 && log_fail "Arguments invalid or missing"
1837 if [[ -z $disk ]]; then
1838 #check pool state only
1839 zpool get -H -o value health $pool \
1840 | grep -i "$state" > /dev/null 2>&1
1842 zpool status -v $pool | grep "$disk" \
1843 | grep -i "$state" > /dev/null 2>&1
1850 # Get the mountpoint of snapshot
1851 # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1854 function snapshot_mountpoint
1856 typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1858 if [[ $dataset != *@* ]]; then
1859 log_fail "Error name of snapshot '$dataset'."
1862 typeset fs=${dataset%@*}
1863 typeset snap=${dataset#*@}
1865 if [[ -z $fs || -z $snap ]]; then
1866 log_fail "Error name of snapshot '$dataset'."
1869 echo $(get_prop mountpoint $fs)/.zfs/snapshot/$snap
1873 # Given a device and 'ashift' value verify it's correctly set on every label
1875 function verify_ashift # device ashift
1880 zdb -e -lll $device | awk -v ashift=$ashift '/ashift: / {
1896 # Given a pool and file system, this function will verify the file system
1897 # using the zdb internal tool. Note that the pool is exported and imported
1898 # to ensure it has consistent state.
1900 function verify_filesys # pool filesystem dir
1903 typeset filesys="$2"
1904 typeset zdbout="/tmp/zdbout.$$"
1909 typeset search_path=""
1911 log_note "Calling zdb to verify filesystem '$filesys'"
1912 zfs unmount -a > /dev/null 2>&1
1913 log_must zpool export $pool
1915 if [[ -n $dirs ]] ; then
1916 for dir in $dirs ; do
1917 search_path="$search_path -d $dir"
1921 log_must zpool import $search_path $pool
1923 zdb -cudi $filesys > $zdbout 2>&1
1924 if [[ $? != 0 ]]; then
1925 log_note "Output: zdb -cudi $filesys"
1927 log_fail "zdb detected errors with: '$filesys'"
1930 log_must zfs mount -a
1931 log_must rm -rf $zdbout
1935 # Given a pool issue a scrub and verify that no checksum errors are reported.
1937 function verify_pool
1939 typeset pool=${1:-$TESTPOOL}
1941 log_must zpool scrub $pool
1942 log_must wait_scrubbed $pool
1944 cksum=$(zpool status $pool | awk 'L{print $NF;L=0} /CKSUM$/{L=1}')
1945 if [[ $cksum != 0 ]]; then
1946 log_must zpool status -v
1947 log_fail "Unexpected CKSUM errors found on $pool ($cksum)"
1952 # Given a pool, and this function list all disks in the pool
1954 function get_disklist # pool
1958 disklist=$(zpool iostat -v $1 | nawk '(NR >4) {print $1}' | \
1959 grep -v "\-\-\-\-\-" | \
1960 egrep -v -e "^(mirror|raidz1|raidz2|spare|log|cache)$")
1966 # Given a pool, and this function list all disks in the pool with their full
1967 # path (like "/dev/sda" instead of "sda").
1969 function get_disklist_fullpath # pool
1978 # This function kills a given list of processes after a time period. We use
1979 # this in the stress tests instead of STF_TIMEOUT so that we can have processes
1980 # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1981 # would be listed as FAIL, which we don't want : we're happy with stress tests
1982 # running for a certain amount of time, then finishing.
1984 # @param $1 the time in seconds after which we should terminate these processes
1985 # @param $2..$n the processes we wish to terminate.
1987 function stress_timeout
1989 typeset -i TIMEOUT=$1
1993 log_note "Waiting for child processes($cpids). " \
1994 "It could last dozens of minutes, please be patient ..."
1995 log_must sleep $TIMEOUT
1997 log_note "Killing child processes after ${TIMEOUT} stress timeout."
1999 for pid in $cpids; do
2000 ps -p $pid > /dev/null 2>&1
2001 if (($? == 0)); then
2002 log_must kill -USR1 $pid
2008 # Verify a given hotspare disk is inuse or avail
2010 # Return 0 is pool/disk matches expected state, 1 otherwise
2012 function check_hotspare_state # pool disk state{inuse,avail}
2015 typeset disk=${2#$DEV_DSKDIR/}
2018 cur_state=$(get_device_state $pool $disk "spares")
2020 if [[ $state != ${cur_state} ]]; then
2027 # Wait until a hotspare transitions to a given state or times out.
2029 # Return 0 when pool/disk matches expected state, 1 on timeout.
2031 function wait_hotspare_state # pool disk state timeout
2034 typeset disk=${2#*$DEV_DSKDIR/}
2036 typeset timeout=${4:-60}
2039 while [[ $i -lt $timeout ]]; do
2040 if check_hotspare_state $pool $disk $state; then
2052 # Verify a given slog disk is inuse or avail
2054 # Return 0 is pool/disk matches expected state, 1 otherwise
2056 function check_slog_state # pool disk state{online,offline,unavail}
2059 typeset disk=${2#$DEV_DSKDIR/}
2062 cur_state=$(get_device_state $pool $disk "logs")
2064 if [[ $state != ${cur_state} ]]; then
2071 # Verify a given vdev disk is inuse or avail
2073 # Return 0 is pool/disk matches expected state, 1 otherwise
2075 function check_vdev_state # pool disk state{online,offline,unavail}
2078 typeset disk=${2#*$DEV_DSKDIR/}
2081 cur_state=$(get_device_state $pool $disk)
2083 if [[ $state != ${cur_state} ]]; then
2090 # Wait until a vdev transitions to a given state or times out.
2092 # Return 0 when pool/disk matches expected state, 1 on timeout.
2094 function wait_vdev_state # pool disk state timeout
2097 typeset disk=${2#*$DEV_DSKDIR/}
2099 typeset timeout=${4:-60}
2102 while [[ $i -lt $timeout ]]; do
2103 if check_vdev_state $pool $disk $state; then
2115 # Check the output of 'zpool status -v <pool>',
2116 # and to see if the content of <token> contain the <keyword> specified.
2118 # Return 0 is contain, 1 otherwise
2120 function check_pool_status # pool token keyword <verbose>
2125 typeset verbose=${4:-false}
2127 scan=$(zpool status -v "$pool" 2>/dev/null | nawk -v token="$token:" '
2128 ($1==token) {print $0}')
2129 if [[ $verbose == true ]]; then
2132 echo $scan | grep -i "$keyword" > /dev/null 2>&1
2138 # These 6 following functions are instance of check_pool_status()
2139 # is_pool_resilvering - to check if the pool is resilver in progress
2140 # is_pool_resilvered - to check if the pool is resilver completed
2141 # is_pool_scrubbing - to check if the pool is scrub in progress
2142 # is_pool_scrubbed - to check if the pool is scrub completed
2143 # is_pool_scrub_stopped - to check if the pool is scrub stopped
2144 # is_pool_scrub_paused - to check if the pool has scrub paused
2145 # is_pool_removing - to check if the pool is removing a vdev
2146 # is_pool_removed - to check if the pool is remove completed
2148 function is_pool_resilvering #pool <verbose>
2150 check_pool_status "$1" "scan" "resilver in progress since " $2
2154 function is_pool_resilvered #pool <verbose>
2156 check_pool_status "$1" "scan" "resilvered " $2
2160 function is_pool_scrubbing #pool <verbose>
2162 check_pool_status "$1" "scan" "scrub in progress since " $2
2166 function is_pool_scrubbed #pool <verbose>
2168 check_pool_status "$1" "scan" "scrub repaired" $2
2172 function is_pool_scrub_stopped #pool <verbose>
2174 check_pool_status "$1" "scan" "scrub canceled" $2
2178 function is_pool_scrub_paused #pool <verbose>
2180 check_pool_status "$1" "scan" "scrub paused since " $2
2184 function is_pool_removing #pool
2186 check_pool_status "$1" "remove" "in progress since "
2190 function is_pool_removed #pool
2192 check_pool_status "$1" "remove" "completed on"
2196 function wait_for_degraded
2199 typeset timeout=${2:-30}
2203 [[ $(get_pool_prop health $pool) == "DEGRADED" ]] && break
2204 log_note "$pool is not yet degraded."
2206 if ((SECONDS - t0 > $timeout)); then
2207 log_note "$pool not degraded after $timeout seconds."
2216 # Use create_pool()/destroy_pool() to clean up the information in
2217 # in the given disk to avoid slice overlapping.
2219 function cleanup_devices #vdevs
2221 typeset pool="foopool$$"
2223 if poolexists $pool ; then
2227 create_pool $pool $@
2234 # A function to find and locate free disks on a system or from given
2235 # disks as the parameter. It works by locating disks that are in use
2236 # as swap devices and dump devices, and also disks listed in /etc/vfstab
2238 # $@ given disks to find which are free, default is all disks in
2241 # @return a string containing the list of available disks
2245 # Trust provided list, no attempt is made to locate unused devices.
2252 sfi=/tmp/swaplist.$$
2253 dmpi=/tmp/dumpdev.$$
2254 max_finddisksnum=${MAX_FINDDISKSNUM:-6}
2257 dumpadm > $dmpi 2>/dev/null
2259 # write an awk script that can process the output of format
2260 # to produce a list of disks we know about. Note that we have
2261 # to escape "$2" so that the shell doesn't interpret it while
2262 # we're creating the awk script.
2263 # -------------------
2264 cat > /tmp/find_disks.awk <<EOF
2273 if (searchdisks && \$2 !~ "^$"){
2279 /^AVAILABLE DISK SELECTIONS:/{
2283 #---------------------
2285 chmod 755 /tmp/find_disks.awk
2286 disks=${@:-$(echo "" | format -e 2>/dev/null | /tmp/find_disks.awk)}
2287 rm /tmp/find_disks.awk
2290 for disk in $disks; do
2292 grep "${disk}[sp]" /etc/mnttab >/dev/null
2293 (($? == 0)) && continue
2295 grep "${disk}[sp]" $sfi >/dev/null
2296 (($? == 0)) && continue
2297 # check for dump device
2298 grep "${disk}[sp]" $dmpi >/dev/null
2299 (($? == 0)) && continue
2300 # check to see if this disk hasn't been explicitly excluded
2301 # by a user-set environment variable
2302 echo "${ZFS_HOST_DEVICES_IGNORE}" | grep "${disk}" > /dev/null
2303 (($? == 0)) && continue
2304 unused_candidates="$unused_candidates $disk"
2309 # now just check to see if those disks do actually exist
2310 # by looking for a device pointing to the first slice in
2311 # each case. limit the number to max_finddisksnum
2313 for disk in $unused_candidates; do
2314 if [ -b $DEV_DSKDIR/${disk}s0 ]; then
2315 if [ $count -lt $max_finddisksnum ]; then
2316 unused="$unused $disk"
2317 # do not impose limit if $@ is provided
2318 [[ -z $@ ]] && ((count = count + 1))
2323 # finally, return our disk list
2328 # Add specified user to specified group
2332 # $3 base of the homedir (optional)
2334 function add_user #<group_name> <user_name> <basedir>
2338 typeset basedir=${3:-"/var/tmp"}
2340 if ((${#gname} == 0 || ${#uname} == 0)); then
2341 log_fail "group name or user name are not defined."
2344 log_must useradd -g $gname -d $basedir/$uname -m $uname
2345 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.profile
2346 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.bash_profile
2347 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.login
2349 # Add new users to the same group and the command line utils.
2350 # This allows them to be run out of the original users home
2351 # directory as long as it permissioned to be group readable.
2353 cmd_group=$(stat --format="%G" $(which zfs))
2354 log_must usermod -a -G $cmd_group $uname
2361 # Delete the specified user.
2364 # $2 base of the homedir (optional)
2366 function del_user #<logname> <basedir>
2369 typeset basedir=${2:-"/var/tmp"}
2371 if ((${#user} == 0)); then
2372 log_fail "login name is necessary."
2375 if id $user > /dev/null 2>&1; then
2376 log_must_retry "currently used" 5 userdel $user
2379 [[ -d $basedir/$user ]] && rm -fr $basedir/$user
2385 # Select valid gid and create specified group.
2389 function add_group #<group_name>
2393 if ((${#group} == 0)); then
2394 log_fail "group name is necessary."
2397 # Assign 100 as the base gid, a larger value is selected for
2398 # Linux because for many distributions 1000 and under are reserved.
2401 groupadd $group > /dev/null 2>&1
2411 groupadd -g $gid $group > /dev/null 2>&1
2415 # The gid is not unique
2424 # Delete the specified group.
2428 function del_group #<group_name>
2431 if ((${#grp} == 0)); then
2432 log_fail "group name is necessary."
2436 getent group $grp > /dev/null 2>&1
2439 # Group does not exist.
2441 # Name already exists as a group name
2442 0) log_must groupdel $grp ;;
2446 groupmod -n $grp $grp > /dev/null 2>&1
2449 # Group does not exist.
2451 # Name already exists as a group name
2452 9) log_must groupdel $grp ;;
2461 # This function will return true if it's safe to destroy the pool passed
2462 # as argument 1. It checks for pools based on zvols and files, and also
2463 # files contained in a pool that may have a different mountpoint.
2465 function safe_to_destroy_pool { # $1 the pool name
2468 typeset DONT_DESTROY=""
2470 # We check that by deleting the $1 pool, we're not
2471 # going to pull the rug out from other pools. Do this
2472 # by looking at all other pools, ensuring that they
2473 # aren't built from files or zvols contained in this pool.
2475 for pool in $(zpool list -H -o name)
2479 # this is a list of the top-level directories in each of the
2480 # files that make up the path to the files the pool is based on
2481 FILEPOOL=$(zpool status -v $pool | grep /$1/ | \
2484 # this is a list of the zvols that make up the pool
2485 ZVOLPOOL=$(zpool status -v $pool | grep "$ZVOL_DEVDIR/$1$" \
2488 # also want to determine if it's a file-based pool using an
2489 # alternate mountpoint...
2490 POOL_FILE_DIRS=$(zpool status -v $pool | \
2491 grep / | awk '{print $1}' | \
2492 awk -F/ '{print $2}' | grep -v "dev")
2494 for pooldir in $POOL_FILE_DIRS
2496 OUTPUT=$(zfs list -H -r -o mountpoint $1 | \
2497 grep "${pooldir}$" | awk '{print $1}')
2499 ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
2503 if [ ! -z "$ZVOLPOOL" ]
2506 log_note "Pool $pool is built from $ZVOLPOOL on $1"
2509 if [ ! -z "$FILEPOOL" ]
2512 log_note "Pool $pool is built from $FILEPOOL on $1"
2515 if [ ! -z "$ALTMOUNTPOOL" ]
2518 log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
2522 if [ -z "${DONT_DESTROY}" ]
2526 log_note "Warning: it is not safe to destroy $1!"
2532 # Get the available ZFS compression options
2533 # $1 option type zfs_set|zfs_compress
2535 function get_compress_opts
2537 typeset COMPRESS_OPTS
2538 typeset GZIP_OPTS="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2539 gzip-6 gzip-7 gzip-8 gzip-9"
2541 if [[ $1 == "zfs_compress" ]] ; then
2542 COMPRESS_OPTS="on lzjb"
2543 elif [[ $1 == "zfs_set" ]] ; then
2544 COMPRESS_OPTS="on off lzjb"
2546 typeset valid_opts="$COMPRESS_OPTS"
2547 zfs get 2>&1 | grep gzip >/dev/null 2>&1
2548 if [[ $? -eq 0 ]]; then
2549 valid_opts="$valid_opts $GZIP_OPTS"
2555 # Verify zfs operation with -p option work as expected
2556 # $1 operation, value could be create, clone or rename
2557 # $2 dataset type, value could be fs or vol
2559 # $4 new dataset name
2561 function verify_opt_p_ops
2566 typeset newdataset=$4
2568 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2569 log_fail "$datatype is not supported."
2572 # check parameters accordingly
2577 if [[ $datatype == "vol" ]]; then
2578 ops="create -V $VOLSIZE"
2582 if [[ -z $newdataset ]]; then
2583 log_fail "newdataset should not be empty" \
2586 log_must datasetexists $dataset
2587 log_must snapexists $dataset
2590 if [[ -z $newdataset ]]; then
2591 log_fail "newdataset should not be empty" \
2594 log_must datasetexists $dataset
2595 log_mustnot snapexists $dataset
2598 log_fail "$ops is not supported."
2602 # make sure the upper level filesystem does not exist
2603 destroy_dataset "${newdataset%/*}" "-rRf"
2605 # without -p option, operation will fail
2606 log_mustnot zfs $ops $dataset $newdataset
2607 log_mustnot datasetexists $newdataset ${newdataset%/*}
2609 # with -p option, operation should succeed
2610 log_must zfs $ops -p $dataset $newdataset
2613 if ! datasetexists $newdataset ; then
2614 log_fail "-p option does not work for $ops"
2617 # when $ops is create or clone, redo the operation still return zero
2618 if [[ $ops != "rename" ]]; then
2619 log_must zfs $ops -p $dataset $newdataset
2626 # Get configuration of pool
2636 if ! poolexists "$pool" ; then
2639 alt_root=$(zpool list -H $pool | awk '{print $NF}')
2640 if [[ $alt_root == "-" ]]; then
2641 value=$(zdb -C $pool | grep "$config:" | awk -F: \
2644 value=$(zdb -e $pool | grep "$config:" | awk -F: \
2647 if [[ -n $value ]] ; then
2657 # Privated function. Random select one of items from arguments.
2662 function _random_get
2669 ((ind = RANDOM % cnt + 1))
2671 typeset ret=$(echo "$str" | cut -f $ind -d ' ')
2676 # Random select one of item from arguments which include NONE string
2678 function random_get_with_non
2683 _random_get "$cnt" "$@"
2687 # Random select one of item from arguments which doesn't include NONE string
2691 _random_get "$#" "$@"
2695 # Detect if the current system support slog
2697 function verify_slog_support
2699 typeset dir=$TEST_BASE_DIR/disk.$$
2705 mkfile $MINVDEVSIZE $vdev $sdev
2708 if ! zpool create -n $pool $vdev log $sdev > /dev/null 2>&1; then
2717 # The function will generate a dataset name with specific length
2718 # $1, the length of the name
2719 # $2, the base string to construct the name
2721 function gen_dataset_name
2724 typeset basestr="$2"
2725 typeset -i baselen=${#basestr}
2729 if ((len % baselen == 0)); then
2730 ((iter = len / baselen))
2732 ((iter = len / baselen + 1))
2734 while ((iter > 0)); do
2735 l_name="${l_name}$basestr"
2744 # Get cksum tuple of dataset
2747 # sample zdb output:
2748 # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2749 # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2750 # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2751 # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2752 function datasetcksum
2756 cksum=$(zdb -vvv $1 | grep "^Dataset $1 \[" | grep "cksum" \
2757 | awk -F= '{print $7}')
2768 cksum=$(cksum $1 | awk '{print $1}')
2773 # Get the given disk/slice state from the specific field of the pool
2775 function get_device_state #pool disk field("", "spares","logs")
2778 typeset disk=${2#$DEV_DSKDIR/}
2779 typeset field=${3:-$pool}
2781 state=$(zpool status -v "$pool" 2>/dev/null | \
2782 nawk -v device=$disk -v pool=$pool -v field=$field \
2783 'BEGIN {startconfig=0; startfield=0; }
2784 /config:/ {startconfig=1}
2785 (startconfig==1) && ($1==field) {startfield=1; next;}
2786 (startfield==1) && ($1==device) {print $2; exit;}
2788 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2794 # print the given directory filesystem type
2802 if [[ -z $dir ]]; then
2803 log_fail "Usage: get_fstype <directory>"
2810 df -n $dir | awk '{print $3}'
2814 # Given a disk, label it to VTOC regardless what label was on the disk
2820 if [[ -z $disk ]]; then
2821 log_fail "The disk name is unspecified."
2823 typeset label_file=/var/tmp/labelvtoc.$$
2824 typeset arch=$(uname -p)
2827 log_note "Currently unsupported by the test framework"
2831 if [[ $arch == "i386" ]]; then
2832 echo "label" > $label_file
2833 echo "0" >> $label_file
2834 echo "" >> $label_file
2835 echo "q" >> $label_file
2836 echo "q" >> $label_file
2838 fdisk -B $disk >/dev/null 2>&1
2839 # wait a while for fdisk finishes
2841 elif [[ $arch == "sparc" ]]; then
2842 echo "label" > $label_file
2843 echo "0" >> $label_file
2844 echo "" >> $label_file
2845 echo "" >> $label_file
2846 echo "" >> $label_file
2847 echo "q" >> $label_file
2849 log_fail "unknown arch type"
2852 format -e -s -d $disk -f $label_file
2853 typeset -i ret_val=$?
2856 # wait the format to finish
2859 if ((ret_val != 0)); then
2860 log_fail "unable to label $disk as VTOC."
2867 # check if the system was installed as zfsroot or not
2868 # return: 0 ture, otherwise false
2872 df -n / | grep zfs > /dev/null 2>&1
2877 # get the root filesystem name if it's zfsroot system.
2879 # return: root filesystem name
2885 rootfs=$(awk '{if ($2 == "/" && $3 == "zfs") print $1}' \
2888 if [[ -z "$rootfs" ]]; then
2889 log_fail "Can not get rootfs"
2891 zfs list $rootfs > /dev/null 2>&1
2892 if (($? == 0)); then
2895 log_fail "This is not a zfsroot system."
2900 # get the rootfs's pool name
2904 function get_rootpool
2910 rootfs=$(awk '{if ($2 == "/" && $3 =="zfs") print $1}' \
2913 if [[ -z "$rootfs" ]]; then
2914 log_fail "Can not get rootpool"
2916 zfs list $rootfs > /dev/null 2>&1
2917 if (($? == 0)); then
2918 rootpool=`echo $rootfs | awk -F\/ '{print $1}'`
2921 log_fail "This is not a zfsroot system."
2926 # Get the package name
2928 function get_package_name
2930 typeset dirpath=${1:-$STC_NAME}
2932 echo "SUNWstc-${dirpath}" | /usr/bin/sed -e "s/\//-/g"
2936 # Get the word numbers from a string separated by white space
2938 function get_word_count
2944 # To verify if the require numbers of disks is given
2946 function verify_disk_count
2948 typeset -i min=${2:-1}
2950 typeset -i count=$(get_word_count "$1")
2952 if ((count < min)); then
2953 log_untested "A minimum of $min disks is required to run." \
2954 " You specified $count disk(s)"
2958 function ds_is_volume
2960 typeset type=$(get_prop type $1)
2961 [[ $type = "volume" ]] && return 0
2965 function ds_is_filesystem
2967 typeset type=$(get_prop type $1)
2968 [[ $type = "filesystem" ]] && return 0
2972 function ds_is_snapshot
2974 typeset type=$(get_prop type $1)
2975 [[ $type = "snapshot" ]] && return 0
2980 # Check if Trusted Extensions are installed and enabled
2982 function is_te_enabled
2984 svcs -H -o state labeld 2>/dev/null | grep "enabled"
2985 if (($? != 0)); then
2992 # Utility function to determine if a system has multiple cpus.
2998 (($(psrinfo | wc -l) > 1))
3004 function get_cpu_freq
3007 lscpu | awk '/CPU MHz/ { print $3 }'
3009 psrinfo -v 0 | awk '/processor operates at/ {print $6}'
3013 # Run the given command as the user provided.
3019 log_note "user:$user $@"
3020 eval su - \$user -c \"$@\" > $TEST_BASE_DIR/out 2>$TEST_BASE_DIR/err
3025 # Check if the pool contains the specified vdevs
3030 # Return 0 if the vdevs are contained in the pool, 1 if any of the specified
3031 # vdevs is not in the pool, and 2 if pool name is missing.
3033 function vdevs_in_pool
3038 if [[ -z $pool ]]; then
3039 log_note "Missing pool name."
3045 # We could use 'zpool list' to only get the vdevs of the pool but we
3046 # can't reference a mirror/raidz vdev using its ID (i.e mirror-0),
3047 # therefore we use the 'zpool status' output.
3048 typeset tmpfile=$(mktemp)
3049 zpool status -v "$pool" | grep -A 1000 "config:" >$tmpfile
3051 grep -w ${vdev##*/} $tmpfile >/dev/null 2>&1
3052 [[ $? -ne 0 ]] && return 1
3066 max=$(echo $((max > i ? max : i)))
3078 min=$(echo $((min < i ? min : i)))
3085 # Generate a random number between 1 and the argument.
3090 echo $(( ($RANDOM % $max) + 1 ))
3093 # Write data that can be compressed into a directory
3094 function write_compressible
3098 typeset nfiles=${3:-1}
3099 typeset bs=${4:-1024k}
3100 typeset fname=${5:-file}
3102 [[ -d $dir ]] || log_fail "No directory: $dir"
3104 # Under Linux fio is not currently used since its behavior can
3105 # differ significantly across versions. This includes missing
3106 # command line options and cases where the --buffer_compress_*
3107 # options fail to behave as expected.
3109 typeset file_bytes=$(to_bytes $megs)
3110 typeset bs_bytes=4096
3111 typeset blocks=$(($file_bytes / $bs_bytes))
3113 for (( i = 0; i < $nfiles; i++ )); do
3114 truncate -s $file_bytes $dir/$fname.$i
3116 # Write every third block to get 66% compression.
3117 for (( j = 0; j < $blocks; j += 3 )); do
3118 dd if=/dev/urandom of=$dir/$fname.$i \
3119 seek=$j bs=$bs_bytes count=1 \
3120 conv=notrunc >/dev/null 2>&1
3124 log_must eval "fio \
3129 --buffer_compress_percentage=66 \
3130 --buffer_compress_chunk=4096 \
3137 --filename_format='$fname.\$jobnum' >/dev/null"
3146 [[ -e $pathname ]] || log_fail "No such file or directory: $pathname"
3147 objnum=$(stat -c %i $pathname)
3152 # Sync data to the pool
3155 # $2 boolean to force uberblock (and config including zpool cache file) update
3157 function sync_pool #pool <force>
3159 typeset pool=${1:-$TESTPOOL}
3160 typeset force=${2:-false}
3162 if [[ $force == true ]]; then
3163 log_must zpool sync -f $pool
3165 log_must zpool sync $pool
3172 # Wait for zpool 'freeing' property drops to zero.
3176 function wait_freeing #pool
3178 typeset pool=${1:-$TESTPOOL}
3180 [[ "0" == "$(zpool list -Ho freeing $pool)" ]] && break
3186 # Wait for every device replace operation to complete
3190 function wait_replacing #pool
3192 typeset pool=${1:-$TESTPOOL}
3194 [[ "" == "$(zpool status $pool |
3195 awk '/replacing-[0-9]+/ {print $1}')" ]] && break
3201 # Wait for a pool to be scrubbed
3204 # $2 number of seconds to wait (optional)
3206 # Returns true when pool has been scrubbed, or false if there's a timeout or if
3207 # no scrub was done.
3209 function wait_scrubbed
3211 typeset pool=${1:-$TESTPOOL}
3212 typeset iter=${2:-10}
3214 for i in {1..$iter} ; do
3215 if is_pool_scrubbed $pool ; then
3223 # Backup the zed.rc in our test directory so that we can edit it for our test.
3225 # Returns: Backup file name. You will need to pass this to zed_rc_restore().
3226 function zed_rc_backup
3228 zedrc_backup="$(mktemp)"
3229 cp $ZEDLET_DIR/zed.rc $zedrc_backup
3233 function zed_rc_restore
3235 mv $1 $ZEDLET_DIR/zed.rc
3239 # Setup custom environment for the ZED.
3241 # $@ Optional list of zedlets to run under zed.
3248 if [[ ! -d $ZEDLET_DIR ]]; then
3249 log_must mkdir $ZEDLET_DIR
3252 if [[ ! -e $VDEVID_CONF ]]; then
3253 log_must touch $VDEVID_CONF
3256 if [[ -e $VDEVID_CONF_ETC ]]; then
3257 log_fail "Must not have $VDEVID_CONF_ETC file present on system"
3261 # Create a symlink for /etc/zfs/vdev_id.conf file.
3262 log_must ln -s $VDEVID_CONF $VDEVID_CONF_ETC
3264 # Setup minimal ZED configuration. Individual test cases should
3265 # add additional ZEDLETs as needed for their specific test.
3266 log_must cp ${ZEDLET_ETC_DIR}/zed.rc $ZEDLET_DIR
3267 log_must cp ${ZEDLET_ETC_DIR}/zed-functions.sh $ZEDLET_DIR
3269 # Scripts must only be user writable.
3270 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3271 saved_umask=$(umask)
3273 for i in $EXTRA_ZEDLETS ; do
3274 log_must cp ${ZEDLET_LIBEXEC_DIR}/$i $ZEDLET_DIR
3276 log_must umask $saved_umask
3279 # Customize the zed.rc file to enable the full debug log.
3280 log_must sed -i '/\#ZED_DEBUG_LOG=.*/d' $ZEDLET_DIR/zed.rc
3281 echo "ZED_DEBUG_LOG=$ZED_DEBUG_LOG" >>$ZEDLET_DIR/zed.rc
3286 # Cleanup custom ZED environment.
3288 # $@ Optional list of zedlets to remove from our test zed.d directory.
3289 function zed_cleanup
3296 log_must rm -f ${ZEDLET_DIR}/zed.rc
3297 log_must rm -f ${ZEDLET_DIR}/zed-functions.sh
3298 log_must rm -f ${ZEDLET_DIR}/all-syslog.sh
3299 log_must rm -f ${ZEDLET_DIR}/all-debug.sh
3300 log_must rm -f ${ZEDLET_DIR}/state
3302 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3303 for i in $EXTRA_ZEDLETS ; do
3304 log_must rm -f ${ZEDLET_DIR}/$i
3307 log_must rm -f $ZED_LOG
3308 log_must rm -f $ZED_DEBUG_LOG
3309 log_must rm -f $VDEVID_CONF_ETC
3310 log_must rm -f $VDEVID_CONF
3315 # Check if ZED is currently running, if not start ZED.
3323 # ZEDLET_DIR=/var/tmp/zed
3324 if [[ ! -d $ZEDLET_DIR ]]; then
3325 log_must mkdir $ZEDLET_DIR
3328 # Verify the ZED is not already running.
3329 pgrep -x zed > /dev/null
3330 if (($? == 0)); then
3331 log_fail "ZED already running"
3334 log_note "Starting ZED"
3335 # run ZED in the background and redirect foreground logging
3336 # output to $ZED_LOG.
3337 log_must truncate -s 0 $ZED_DEBUG_LOG
3338 log_must eval "zed -vF -d $ZEDLET_DIR -p $ZEDLET_DIR/zed.pid -P $PATH" \
3339 "-s $ZEDLET_DIR/state 2>$ZED_LOG &"
3353 log_note "Stopping ZED"
3354 if [[ -f ${ZEDLET_DIR}/zed.pid ]]; then
3355 zedpid=$(<${ZEDLET_DIR}/zed.pid)
3357 while ps -p $zedpid > /dev/null; do
3360 rm -f ${ZEDLET_DIR}/zed.pid
3368 function zed_events_drain
3370 while [ $(zpool events -H | wc -l) -ne 0 ]; do
3372 zpool events -c >/dev/null
3376 # Set a variable in zed.rc to something, un-commenting it in the process.
3386 eval sed -i $cmd $ZEDLET_DIR/zed.rc
3389 echo "$var=$val" >> $ZEDLET_DIR/zed.rc
3394 # Check is provided device is being active used as a swap device.
3396 function is_swap_inuse
3400 if [[ -z $device ]] ; then
3401 log_note "No device specified."
3406 swapon -s | grep -w $(readlink -f $device) > /dev/null 2>&1
3408 swap -l | grep -w $device > /dev/null 2>&1
3415 # Setup a swap device using the provided device.
3422 log_must eval "mkswap $swapdev > /dev/null 2>&1"
3423 log_must swapon $swapdev
3425 log_must swap -a $swapdev
3432 # Cleanup a swap device on the provided device.
3434 function swap_cleanup
3438 if is_swap_inuse $swapdev; then
3440 log_must swapoff $swapdev
3442 log_must swap -d $swapdev
3450 # Set a global system tunable (64-bit value)
3455 function set_tunable64
3457 set_tunable_impl "$1" "$2" Z
3461 # Set a global system tunable (32-bit value)
3466 function set_tunable32
3468 set_tunable_impl "$1" "$2" W
3471 function set_tunable_impl
3473 typeset tunable="$1"
3475 typeset mdb_cmd="$3"
3476 typeset module="${4:-zfs}"
3478 [[ -z "$tunable" ]] && return 1
3479 [[ -z "$value" ]] && return 1
3480 [[ -z "$mdb_cmd" ]] && return 1
3484 typeset zfs_tunables="/sys/module/$module/parameters"
3485 [[ -w "$zfs_tunables/$tunable" ]] || return 1
3486 echo -n "$value" > "$zfs_tunables/$tunable"
3490 [[ "$module" -eq "zfs" ]] || return 1
3491 echo "${tunable}/${mdb_cmd}0t${value}" | mdb -kw
3498 # Get a global system tunable
3502 function get_tunable
3504 get_tunable_impl "$1"
3507 function get_tunable_impl
3509 typeset tunable="$1"
3510 typeset module="${2:-zfs}"
3512 [[ -z "$tunable" ]] && return 1
3516 typeset zfs_tunables="/sys/module/$module/parameters"
3517 [[ -f "$zfs_tunables/$tunable" ]] || return 1
3518 cat $zfs_tunables/$tunable
3522 [[ "$module" -eq "zfs" ]] || return 1
3530 # Prints the current time in seconds since UNIX Epoch.
3532 function current_epoch
3538 # Get decimal value of global uint32_t variable using mdb.
3540 function mdb_get_uint32
3545 value=$(mdb -k -e "$variable/X | ::eval .=U")
3546 if [[ $? -ne 0 ]]; then
3547 log_fail "Failed to get value of '$variable' from mdb."
3556 # Set global uint32_t variable to a decimal value using mdb.
3558 function mdb_set_uint32
3563 mdb -kw -e "$variable/W 0t$value" > /dev/null
3564 if [[ $? -ne 0 ]]; then
3565 echo "Failed to set '$variable' to '$value' in mdb."
3573 # Set global scalar integer variable to a hex value using mdb.
3574 # Note: Target should have CTF data loaded.
3576 function mdb_ctf_set_int
3581 mdb -kw -e "$variable/z $value" > /dev/null
3582 if [[ $? -ne 0 ]]; then
3583 echo "Failed to set '$variable' to '$value' in mdb."