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 (c) 2017 by Tim Chase. All rights reserved.
27 # Copyright (c) 2017 by Nexenta Systems, Inc. All rights reserved.
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 # Use is subject to license terms.
34 . ${STF_TOOLS}/include/logapi.shlib
35 . ${STF_SUITE}/include/math.shlib
36 . ${STF_SUITE}/include/blkdev.shlib
39 # Apply constrained path when available. This is required since the
40 # PATH may have been modified by sudo's secure_path behavior.
42 if [ -n "$STF_PATH" ]; then
47 # Generic dot version comparison function
49 # Returns success when version $1 is greater than or equal to $2.
51 function compare_version_gte
53 if [[ "$(printf "$1\n$2" | sort -V | tail -n1)" == "$1" ]]; then
60 # Linux kernel version comparison function
62 # $1 Linux version ("4.10", "2.6.32") or blank for installed Linux version
64 # Used for comparison: if [ $(linux_version) -ge $(linux_version "2.6.32") ]
66 function linux_version
70 [[ -z "$ver" ]] && ver=$(uname -r | grep -Eo "^[0-9]+\.[0-9]+\.[0-9]+")
72 typeset version=$(echo $ver | cut -d '.' -f 1)
73 typeset major=$(echo $ver | cut -d '.' -f 2)
74 typeset minor=$(echo $ver | cut -d '.' -f 3)
76 [[ -z "$version" ]] && version=0
77 [[ -z "$major" ]] && major=0
78 [[ -z "$minor" ]] && minor=0
80 echo $((version * 10000 + major * 100 + minor))
83 # Determine if this is a Linux test system
85 # Return 0 if platform Linux, 1 if otherwise
89 if [[ $(uname -o) == "GNU/Linux" ]]; then
96 # Determine if this is a 32-bit system
98 # Return 0 if platform is 32-bit, 1 if otherwise
102 if [[ $(getconf LONG_BIT) == "32" ]]; then
109 # Determine if kmemleak is enabled
111 # Return 0 if kmemleak is enabled, 1 if otherwise
115 if is_linux && [[ -e /sys/kernel/debug/kmemleak ]]; then
122 # Determine whether a dataset is mounted
125 # $2 filesystem type; optional - defaulted to zfs
127 # Return 0 if dataset is mounted; 1 if unmounted; 2 on error
132 [[ -z $fstype ]] && fstype=zfs
133 typeset out dir name ret
137 if [[ "$1" == "/"* ]] ; then
138 for out in $(zfs mount | awk '{print $2}'); do
139 [[ $1 == $out ]] && return 0
142 for out in $(zfs mount | awk '{print $1}'); do
143 [[ $1 == $out ]] && return 0
148 out=$(df -F $fstype $1 2>/dev/null)
150 (($ret != 0)) && return $ret
158 [[ "$1" == "$dir" || "$1" == "$name" ]] && return 0
161 out=$(df -t $fstype $1 2>/dev/null)
165 if [[ -L "$ZVOL_DEVDIR/$1" ]]; then
166 link=$(readlink -f $ZVOL_DEVDIR/$1)
167 [[ -n "$link" ]] && \
168 mount | grep -q "^$link" && \
177 # Return 0 if a dataset is mounted; 1 otherwise
180 # $2 filesystem type; optional - defaulted to zfs
185 (($? == 0)) && return 0
189 # Return 0 if a dataset is unmounted; 1 otherwise
192 # $2 filesystem type; optional - defaulted to zfs
197 (($? == 1)) && return 0
207 echo $1 | sed "s/,/ /g"
210 function default_setup
212 default_setup_noexit "$@"
217 function default_setup_no_mountpoint
219 default_setup_noexit "$1" "$2" "$3" "yes"
225 # Given a list of disks, setup storage pools and datasets.
227 function default_setup_noexit
232 typeset no_mountpoint=$4
233 log_note begin default_setup_noexit
235 if is_global_zone; then
236 if poolexists $TESTPOOL ; then
237 destroy_pool $TESTPOOL
239 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
240 log_must zpool create -f $TESTPOOL $disklist
245 rm -rf $TESTDIR || log_unresolved Could not remove $TESTDIR
246 mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR
248 log_must zfs create $TESTPOOL/$TESTFS
249 if [[ -z $no_mountpoint ]]; then
250 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
253 if [[ -n $container ]]; then
254 rm -rf $TESTDIR1 || \
255 log_unresolved Could not remove $TESTDIR1
256 mkdir -p $TESTDIR1 || \
257 log_unresolved Could not create $TESTDIR1
259 log_must zfs create $TESTPOOL/$TESTCTR
260 log_must zfs set canmount=off $TESTPOOL/$TESTCTR
261 log_must zfs create $TESTPOOL/$TESTCTR/$TESTFS1
262 if [[ -z $no_mountpoint ]]; then
263 log_must zfs set mountpoint=$TESTDIR1 \
264 $TESTPOOL/$TESTCTR/$TESTFS1
268 if [[ -n $volume ]]; then
269 if is_global_zone ; then
270 log_must zfs create -V $VOLSIZE $TESTPOOL/$TESTVOL
273 log_must zfs create $TESTPOOL/$TESTVOL
279 # Given a list of disks, setup a storage pool, file system and
282 function default_container_setup
286 default_setup "$disklist" "true"
290 # Given a list of disks, setup a storage pool,file system
293 function default_volume_setup
297 default_setup "$disklist" "" "true"
301 # Given a list of disks, setup a storage pool,file system,
302 # a container and a volume.
304 function default_container_volume_setup
308 default_setup "$disklist" "true" "true"
312 # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
315 # $1 Existing filesystem or volume name. Default, $TESTPOOL/$TESTFS
316 # $2 snapshot name. Default, $TESTSNAP
318 function create_snapshot
320 typeset fs_vol=${1:-$TESTPOOL/$TESTFS}
321 typeset snap=${2:-$TESTSNAP}
323 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
324 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
326 if snapexists $fs_vol@$snap; then
327 log_fail "$fs_vol@$snap already exists."
329 datasetexists $fs_vol || \
330 log_fail "$fs_vol must exist."
332 log_must zfs snapshot $fs_vol@$snap
336 # Create a clone from a snapshot, default clone name is $TESTCLONE.
338 # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
339 # $2 Clone name, $TESTPOOL/$TESTCLONE is default.
341 function create_clone # snapshot clone
343 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
344 typeset clone=${2:-$TESTPOOL/$TESTCLONE}
347 log_fail "Snapshot name is undefined."
349 log_fail "Clone name is undefined."
351 log_must zfs clone $snap $clone
355 # Create a bookmark of the given snapshot. Defaultly create a bookmark on
358 # $1 Existing filesystem or volume name. Default, $TESTFS
359 # $2 Existing snapshot name. Default, $TESTSNAP
360 # $3 bookmark name. Default, $TESTBKMARK
362 function create_bookmark
364 typeset fs_vol=${1:-$TESTFS}
365 typeset snap=${2:-$TESTSNAP}
366 typeset bkmark=${3:-$TESTBKMARK}
368 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
369 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
370 [[ -z $bkmark ]] && log_fail "Bookmark's name is undefined."
372 if bkmarkexists $fs_vol#$bkmark; then
373 log_fail "$fs_vol#$bkmark already exists."
375 datasetexists $fs_vol || \
376 log_fail "$fs_vol must exist."
377 snapexists $fs_vol@$snap || \
378 log_fail "$fs_vol@$snap must exist."
380 log_must zfs bookmark $fs_vol@$snap $fs_vol#$bkmark
384 # Create a temporary clone result of an interrupted resumable 'zfs receive'
385 # $1 Destination filesystem name. Must not exist, will be created as the result
386 # of this function along with its %recv temporary clone
387 # $2 Source filesystem name. Must not exist, will be created and destroyed
389 function create_recv_clone
392 typeset sendfs="${2:-$TESTPOOL/create_recv_clone}"
393 typeset snap="$sendfs@snap1"
394 typeset incr="$sendfs@snap2"
395 typeset mountpoint="$TESTDIR/create_recv_clone"
396 typeset sendfile="$TESTDIR/create_recv_clone.zsnap"
398 [[ -z $recvfs ]] && log_fail "Recv filesystem's name is undefined."
400 datasetexists $recvfs && log_fail "Recv filesystem must not exist."
401 datasetexists $sendfs && log_fail "Send filesystem must not exist."
403 log_must zfs create -o mountpoint="$mountpoint" $sendfs
404 log_must zfs snapshot $snap
405 log_must eval "zfs send $snap | zfs recv -u $recvfs"
406 log_must mkfile 1m "$mountpoint/data"
407 log_must zfs snapshot $incr
408 log_must eval "zfs send -i $snap $incr | dd bs=10K count=1 > $sendfile"
409 log_mustnot eval "zfs recv -su $recvfs < $sendfile"
410 destroy_dataset "$sendfs" "-r"
411 log_must rm -f "$sendfile"
413 if [[ $(get_prop 'inconsistent' "$recvfs/%recv") -ne 1 ]]; then
414 log_fail "Error creating temporary $recvfs/%recv clone"
418 function default_mirror_setup
420 default_mirror_setup_noexit $1 $2 $3
426 # Given a pair of disks, set up a storage pool and dataset for the mirror
427 # @parameters: $1 the primary side of the mirror
428 # $2 the secondary side of the mirror
429 # @uses: ZPOOL ZFS TESTPOOL TESTFS
430 function default_mirror_setup_noexit
432 readonly func="default_mirror_setup_noexit"
436 [[ -z $primary ]] && \
437 log_fail "$func: No parameters passed"
438 [[ -z $secondary ]] && \
439 log_fail "$func: No secondary partition passed"
440 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
441 log_must zpool create -f $TESTPOOL mirror $@
442 log_must zfs create $TESTPOOL/$TESTFS
443 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
447 # create a number of mirrors.
448 # We create a number($1) of 2 way mirrors using the pairs of disks named
449 # on the command line. These mirrors are *not* mounted
450 # @parameters: $1 the number of mirrors to create
451 # $... the devices to use to create the mirrors on
452 # @uses: ZPOOL ZFS TESTPOOL
453 function setup_mirrors
455 typeset -i nmirrors=$1
458 while ((nmirrors > 0)); do
459 log_must test -n "$1" -a -n "$2"
460 [[ -d /$TESTPOOL$nmirrors ]] && rm -rf /$TESTPOOL$nmirrors
461 log_must zpool create -f $TESTPOOL$nmirrors mirror $1 $2
463 ((nmirrors = nmirrors - 1))
468 # create a number of raidz pools.
469 # We create a number($1) of 2 raidz pools using the pairs of disks named
470 # on the command line. These pools are *not* mounted
471 # @parameters: $1 the number of pools to create
472 # $... the devices to use to create the pools on
473 # @uses: ZPOOL ZFS TESTPOOL
474 function setup_raidzs
476 typeset -i nraidzs=$1
479 while ((nraidzs > 0)); do
480 log_must test -n "$1" -a -n "$2"
481 [[ -d /$TESTPOOL$nraidzs ]] && rm -rf /$TESTPOOL$nraidzs
482 log_must zpool create -f $TESTPOOL$nraidzs raidz $1 $2
484 ((nraidzs = nraidzs - 1))
489 # Destroy the configured testpool mirrors.
490 # the mirrors are of the form ${TESTPOOL}{number}
491 # @uses: ZPOOL ZFS TESTPOOL
492 function destroy_mirrors
494 default_cleanup_noexit
500 # Given a minimum of two disks, set up a storage pool and dataset for the raid-z
501 # $1 the list of disks
503 function default_raidz_setup
505 typeset disklist="$*"
506 disks=(${disklist[*]})
508 if [[ ${#disks[*]} -lt 2 ]]; then
509 log_fail "A raid-z requires a minimum of two disks."
512 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
513 log_must zpool create -f $TESTPOOL raidz $disklist
514 log_must zfs create $TESTPOOL/$TESTFS
515 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
521 # Common function used to cleanup storage pools and datasets.
523 # Invoked at the start of the test suite to ensure the system
524 # is in a known state, and also at the end of each set of
525 # sub-tests to ensure errors from one set of tests doesn't
526 # impact the execution of the next set.
528 function default_cleanup
530 default_cleanup_noexit
536 # Utility function used to list all available pool names.
538 # NOTE: $KEEP is a variable containing pool names, separated by a newline
539 # character, that must be excluded from the returned list.
541 function get_all_pools
543 zpool list -H -o name | grep -Fvx "$KEEP" | grep -v "$NO_POOLS"
546 function default_cleanup_noexit
550 # Destroying the pool will also destroy any
551 # filesystems it contains.
553 if is_global_zone; then
554 zfs unmount -a > /dev/null 2>&1
555 ALL_POOLS=$(get_all_pools)
556 # Here, we loop through the pools we're allowed to
557 # destroy, only destroying them if it's safe to do
559 while [ ! -z ${ALL_POOLS} ]
561 for pool in ${ALL_POOLS}
563 if safe_to_destroy_pool $pool ;
567 ALL_POOLS=$(get_all_pools)
574 for fs in $(zfs list -H -o name \
575 | grep "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
576 destroy_dataset "$fs" "-Rf"
579 # Need cleanup here to avoid garbage dir left.
580 for fs in $(zfs list -H -o name); do
581 [[ $fs == /$ZONE_POOL ]] && continue
582 [[ -d $fs ]] && log_must rm -rf $fs/*
586 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
589 for fs in $(zfs list -H -o name); do
590 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
591 log_must zfs set reservation=none $fs
592 log_must zfs set recordsize=128K $fs
593 log_must zfs set mountpoint=/$fs $fs
595 enc=$(get_prop encryption $fs)
596 if [[ $? -ne 0 ]] || [[ -z "$enc" ]] || \
597 [[ "$enc" == "off" ]]; then
598 log_must zfs set checksum=on $fs
600 log_must zfs set compression=off $fs
601 log_must zfs set atime=on $fs
602 log_must zfs set devices=off $fs
603 log_must zfs set exec=on $fs
604 log_must zfs set setuid=on $fs
605 log_must zfs set readonly=off $fs
606 log_must zfs set snapdir=hidden $fs
607 log_must zfs set aclmode=groupmask $fs
608 log_must zfs set aclinherit=secure $fs
613 [[ -d $TESTDIR ]] && \
614 log_must rm -rf $TESTDIR
617 if is_mpath_device $disk1; then
621 rm -f $TEST_BASE_DIR/{err,out}
626 # Common function used to cleanup storage pools, file systems
629 function default_container_cleanup
631 if ! is_global_zone; then
635 ismounted $TESTPOOL/$TESTCTR/$TESTFS1
637 log_must zfs unmount $TESTPOOL/$TESTCTR/$TESTFS1
639 destroy_dataset "$TESTPOOL/$TESTCTR/$TESTFS1" "-R"
640 destroy_dataset "$TESTPOOL/$TESTCTR" "-Rf"
642 [[ -e $TESTDIR1 ]] && \
643 log_must rm -rf $TESTDIR1 > /dev/null 2>&1
649 # Common function used to cleanup snapshot of file system or volume. Default to
650 # delete the file system's snapshot
654 function destroy_snapshot
656 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
658 if ! snapexists $snap; then
659 log_fail "'$snap' does not exist."
663 # For the sake of the value which come from 'get_prop' is not equal
664 # to the really mountpoint when the snapshot is unmounted. So, firstly
665 # check and make sure this snapshot's been mounted in current system.
668 if ismounted $snap; then
669 mtpt=$(get_prop mountpoint $snap)
671 log_fail "get_prop mountpoint $snap failed."
674 destroy_dataset "$snap"
675 [[ $mtpt != "" && -d $mtpt ]] && \
676 log_must rm -rf $mtpt
680 # Common function used to cleanup clone.
684 function destroy_clone
686 typeset clone=${1:-$TESTPOOL/$TESTCLONE}
688 if ! datasetexists $clone; then
689 log_fail "'$clone' does not existed."
692 # With the same reason in destroy_snapshot
694 if ismounted $clone; then
695 mtpt=$(get_prop mountpoint $clone)
697 log_fail "get_prop mountpoint $clone failed."
700 destroy_dataset "$clone"
701 [[ $mtpt != "" && -d $mtpt ]] && \
702 log_must rm -rf $mtpt
706 # Common function used to cleanup bookmark of file system or volume. Default
707 # to delete the file system's bookmark.
711 function destroy_bookmark
713 typeset bkmark=${1:-$TESTPOOL/$TESTFS#$TESTBKMARK}
715 if ! bkmarkexists $bkmark; then
716 log_fail "'$bkmarkp' does not existed."
719 destroy_dataset "$bkmark"
722 # Return 0 if a snapshot exists; $? otherwise
728 zfs list -H -t snapshot "$1" > /dev/null 2>&1
733 # Return 0 if a bookmark exists; $? otherwise
737 function bkmarkexists
739 zfs list -H -t bookmark "$1" > /dev/null 2>&1
744 # Set a property to a certain value on a dataset.
745 # Sets a property of the dataset to the value as passed in.
747 # $1 dataset who's property is being set
749 # $3 value to set property to
751 # 0 if the property could be set.
752 # non-zero otherwise.
755 function dataset_setprop
757 typeset fn=dataset_setprop
760 log_note "$fn: Insufficient parameters (need 3, had $#)"
764 output=$(zfs set $2=$3 $1 2>&1)
767 log_note "Setting property on $1 failed."
768 log_note "property $2=$3"
769 log_note "Return Code: $rv"
770 log_note "Output: $output"
777 # Assign suite defined dataset properties.
778 # This function is used to apply the suite's defined default set of
779 # properties to a dataset.
780 # @parameters: $1 dataset to use
781 # @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
783 # 0 if the dataset has been altered.
784 # 1 if no pool name was passed in.
785 # 2 if the dataset could not be found.
786 # 3 if the dataset could not have it's properties set.
788 function dataset_set_defaultproperties
792 [[ -z $dataset ]] && return 1
796 for confset in $(zfs list); do
797 if [[ $dataset = $confset ]]; then
802 [[ $found -eq 0 ]] && return 2
803 if [[ -n $COMPRESSION_PROP ]]; then
804 dataset_setprop $dataset compression $COMPRESSION_PROP || \
806 log_note "Compression set to '$COMPRESSION_PROP' on $dataset"
808 if [[ -n $CHECKSUM_PROP ]]; then
809 dataset_setprop $dataset checksum $CHECKSUM_PROP || \
811 log_note "Checksum set to '$CHECKSUM_PROP' on $dataset"
817 # Check a numeric assertion
818 # @parameter: $@ the assertion to check
819 # @output: big loud notice if assertion failed
824 (($@)) || log_fail "$@"
828 # Function to format partition size of a disk
829 # Given a disk cxtxdx reduces all partitions
832 function zero_partitions #<whole_disk_name>
838 DSK=$DEV_DSKDIR/$diskname
839 DSK=$(echo $DSK | sed -e "s|//|/|g")
840 log_must parted $DSK -s -- mklabel gpt
841 blockdev --rereadpt $DSK 2>/dev/null
844 for i in 0 1 3 4 5 6 7
846 log_must set_partition $i "" 0mb $diskname
854 # Given a slice, size and disk, this function
855 # formats the slice to the specified size.
856 # Size should be specified with units as per
857 # the `format` command requirements eg. 100mb 3gb
859 # NOTE: This entire interface is problematic for the Linux parted utilty
860 # which requires the end of the partition to be specified. It would be
861 # best to retire this interface and replace it with something more flexible.
862 # At the moment a best effort is made.
864 function set_partition #<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
866 typeset -i slicenum=$1
872 if [[ -z $size || -z $disk ]]; then
873 log_fail "The size or disk name is unspecified."
875 typeset size_mb=${size%%[mMgG]}
877 size_mb=${size_mb%%[mMgG][bB]}
878 if [[ ${size:1:1} == 'g' ]]; then
879 ((size_mb = size_mb * 1024))
882 # Create GPT partition table when setting slice 0 or
883 # when the device doesn't already contain a GPT label.
884 parted $DEV_DSKDIR/$disk -s -- print 1 >/dev/null
886 if [[ $slicenum -eq 0 || $ret_val -ne 0 ]]; then
887 parted $DEV_DSKDIR/$disk -s -- mklabel gpt
888 if [[ $? -ne 0 ]]; then
889 log_note "Failed to create GPT partition table on $disk"
894 # When no start is given align on the first cylinder.
895 if [[ -z "$start" ]]; then
899 # Determine the cylinder size for the device and using
900 # that calculate the end offset in cylinders.
901 typeset -i cly_size_kb=0
902 cly_size_kb=$(parted -m $DEV_DSKDIR/$disk -s -- \
903 unit cyl print | head -3 | tail -1 | \
904 awk -F '[:k.]' '{print $4}')
905 ((end = (size_mb * 1024 / cly_size_kb) + start))
907 parted $DEV_DSKDIR/$disk -s -- \
908 mkpart part$slicenum ${start}cyl ${end}cyl
909 if [[ $? -ne 0 ]]; then
910 log_note "Failed to create partition $slicenum on $disk"
914 blockdev --rereadpt $DEV_DSKDIR/$disk 2>/dev/null
917 if [[ -z $slicenum || -z $size || -z $disk ]]; then
918 log_fail "The slice, size or disk name is unspecified."
921 typeset format_file=/var/tmp/format_in.$$
923 echo "partition" >$format_file
924 echo "$slicenum" >> $format_file
925 echo "" >> $format_file
926 echo "" >> $format_file
927 echo "$start" >> $format_file
928 echo "$size" >> $format_file
929 echo "label" >> $format_file
930 echo "" >> $format_file
931 echo "q" >> $format_file
932 echo "q" >> $format_file
934 format -e -s -d $disk -f $format_file
939 if [[ $ret_val -ne 0 ]]; then
940 log_note "Unable to format $disk slice $slicenum to $size"
947 # Delete all partitions on all disks - this is specifically for the use of multipath
948 # devices which currently can only be used in the test suite as raw/un-partitioned
949 # devices (ie a zpool cannot be created on a whole mpath device that has partitions)
951 function delete_partitions
955 if [[ -z $DISK_ARRAY_NUM ]]; then
956 DISK_ARRAY_NUM=$(echo ${DISKS} | nawk '{print NF}')
958 if [[ -z $DISKSARRAY ]]; then
963 if (( $DISK_ARRAY_NUM == 1 )); then
964 while ((j < MAX_PARTITIONS)); do
965 parted $DEV_DSKDIR/$DISK -s rm $j \
967 if (( $? == 1 )); then
968 lsblk | egrep ${DISK}${SLICE_PREFIX}${j} > /dev/null
969 if (( $? == 1 )); then
970 log_note "Partitions for $DISK should be deleted"
972 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
976 lsblk | egrep ${DISK}${SLICE_PREFIX}${j} > /dev/null
977 if (( $? == 0 )); then
978 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
984 for disk in `echo $DISKSARRAY`; do
985 while ((j < MAX_PARTITIONS)); do
986 parted $DEV_DSKDIR/$disk -s rm $j > /dev/null 2>&1
987 if (( $? == 1 )); then
988 lsblk | egrep ${disk}${SLICE_PREFIX}${j} > /dev/null
989 if (( $? == 1 )); then
990 log_note "Partitions for $disk should be deleted"
992 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
996 lsblk | egrep ${disk}${SLICE_PREFIX}${j} > /dev/null
997 if (( $? == 0 )); then
998 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
1011 # Get the end cyl of the given slice
1013 function get_endslice #<disk> <slice>
1017 if [[ -z $disk || -z $slice ]] ; then
1018 log_fail "The disk name or slice number is unspecified."
1022 endcyl=$(parted -s $DEV_DSKDIR/$disk -- unit cyl print | \
1023 grep "part${slice}" | \
1024 awk '{print $3}' | \
1026 ((endcyl = (endcyl + 1)))
1028 disk=${disk#/dev/dsk/}
1029 disk=${disk#/dev/rdsk/}
1033 ratio=$(prtvtoc /dev/rdsk/${disk}s2 | \
1034 grep "sectors\/cylinder" | \
1037 if ((ratio == 0)); then
1041 typeset -i endcyl=$(prtvtoc -h /dev/rdsk/${disk}s2 |
1042 nawk -v token="$slice" '{if ($1==token) print $6}')
1044 ((endcyl = (endcyl + 1) / ratio))
1052 # Given a size,disk and total slice number, this function formats the
1053 # disk slices from 0 to the total slice number with the same specified
1056 function partition_disk #<slice_size> <whole_disk_name> <total_slices>
1059 typeset slice_size=$1
1060 typeset disk_name=$2
1061 typeset total_slices=$3
1064 zero_partitions $disk_name
1065 while ((i < $total_slices)); do
1072 log_must set_partition $i "$cyl" $slice_size $disk_name
1073 cyl=$(get_endslice $disk_name $i)
1079 # This function continues to write to a filenum number of files into dirnum
1080 # number of directories until either file_write returns an error or the
1081 # maximum number of files per directory have been written.
1084 # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
1086 # Return value: 0 on success
1090 # destdir: is the directory where everything is to be created under
1091 # dirnum: the maximum number of subdirectories to use, -1 no limit
1092 # filenum: the maximum number of files per subdirectory
1093 # bytes: number of bytes to write
1094 # num_writes: numer of types to write out bytes
1095 # data: the data that will be written
1098 # file_fs /testdir 20 25 1024 256 0
1100 # Note: bytes * num_writes equals the size of the testfile
1102 function fill_fs # destdir dirnum filenum bytes num_writes data
1104 typeset destdir=${1:-$TESTDIR}
1105 typeset -i dirnum=${2:-50}
1106 typeset -i filenum=${3:-50}
1107 typeset -i bytes=${4:-8192}
1108 typeset -i num_writes=${5:-10240}
1109 typeset data=${6:-0}
1111 typeset -i odirnum=1
1112 typeset -i idirnum=0
1116 mkdir -p $destdir/$idirnum
1117 while (($odirnum > 0)); do
1118 if ((dirnum >= 0 && idirnum >= dirnum)); then
1122 file_write -o create -f $destdir/$idirnum/$TESTFILE.$fn \
1123 -b $bytes -c $num_writes -d $data
1125 if (($retval != 0)); then
1129 if (($fn >= $filenum)); then
1131 ((idirnum = idirnum + 1))
1132 mkdir -p $destdir/$idirnum
1141 # Simple function to get the specified property. If unable to
1142 # get the property then exits.
1144 # Note property is in 'parsable' format (-p)
1146 function get_prop # property dataset
1152 prop_val=$(zfs get -pH -o value $prop $dataset 2>/dev/null)
1153 if [[ $? -ne 0 ]]; then
1154 log_note "Unable to get $prop property for dataset " \
1164 # Simple function to get the specified property of pool. If unable to
1165 # get the property then exits.
1167 # Note property is in 'parsable' format (-p)
1169 function get_pool_prop # property pool
1175 if poolexists $pool ; then
1176 prop_val=$(zpool get -pH $prop $pool 2>/dev/null | tail -1 | \
1178 if [[ $? -ne 0 ]]; then
1179 log_note "Unable to get $prop property for pool " \
1184 log_note "Pool $pool not exists."
1192 # Return 0 if a pool exists; $? otherwise
1200 if [[ -z $pool ]]; then
1201 log_note "No pool name given."
1205 zpool get name "$pool" > /dev/null 2>&1
1209 # Return 0 if all the specified datasets exist; $? otherwise
1212 function datasetexists
1214 if (($# == 0)); then
1215 log_note "No dataset name given."
1219 while (($# > 0)); do
1220 zfs get name $1 > /dev/null 2>&1 || \
1228 # return 0 if none of the specified datasets exists, otherwise return 1.
1231 function datasetnonexists
1233 if (($# == 0)); then
1234 log_note "No dataset name given."
1238 while (($# > 0)); do
1239 zfs list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \
1247 function is_shared_impl
1253 for mtpt in `share | awk '{print $1}'` ; do
1254 if [[ $mtpt == $fs ]] ; then
1261 for mtpt in `share | awk '{print $2}'` ; do
1262 if [[ $mtpt == $fs ]] ; then
1267 typeset stat=$(svcs -H -o STA nfs/server:default)
1268 if [[ $stat != "ON" ]]; then
1269 log_note "Current nfs/server status: $stat"
1276 # Given a mountpoint, or a dataset name, determine if it is shared via NFS.
1278 # Returns 0 if shared, 1 otherwise.
1285 if [[ $fs != "/"* ]] ; then
1286 if datasetnonexists "$fs" ; then
1289 mtpt=$(get_prop mountpoint "$fs")
1291 none|legacy|-) return 1
1299 is_shared_impl "$fs"
1303 # Given a dataset name determine if it is shared via SMB.
1305 # Returns 0 if shared, 1 otherwise.
1307 function is_shared_smb
1312 if datasetnonexists "$fs" ; then
1315 fs=$(echo $fs | sed 's@/@_@g')
1319 for mtpt in `net usershare list | awk '{print $1}'` ; do
1320 if [[ $mtpt == $fs ]] ; then
1326 log_unsupported "Currently unsupported by the test framework"
1332 # Given a mountpoint, determine if it is not shared via NFS.
1334 # Returns 0 if not shared, 1 otherwise.
1341 if (($? == 0)); then
1349 # Given a dataset determine if it is not shared via SMB.
1351 # Returns 0 if not shared, 1 otherwise.
1353 function not_shared_smb
1358 if (($? == 0)); then
1366 # Helper function to unshare a mountpoint.
1368 function unshare_fs #fs
1372 is_shared $fs || is_shared_smb $fs
1373 if (($? == 0)); then
1374 log_must zfs unshare $fs
1381 # Helper function to share a NFS mountpoint.
1383 function share_nfs #fs
1389 if (($? != 0)); then
1390 log_must share "*:$fs"
1394 if (($? != 0)); then
1395 log_must share -F nfs $fs
1403 # Helper function to unshare a NFS mountpoint.
1405 function unshare_nfs #fs
1411 if (($? == 0)); then
1412 log_must unshare -u "*:$fs"
1416 if (($? == 0)); then
1417 log_must unshare -F nfs $fs
1425 # Helper function to show NFS shares.
1427 function showshares_nfs
1439 # Helper function to show SMB shares.
1441 function showshares_smb
1453 # Check NFS server status and trigger it online.
1455 function setup_nfs_server
1457 # Cannot share directory in non-global zone.
1459 if ! is_global_zone; then
1460 log_note "Cannot trigger NFS server by sharing in LZ."
1466 # Re-synchronize /var/lib/nfs/etab with /etc/exports and
1467 # /etc/exports.d./* to provide a clean test environment.
1471 log_note "NFS server must be started prior to running ZTS."
1475 typeset nfs_fmri="svc:/network/nfs/server:default"
1476 if [[ $(svcs -Ho STA $nfs_fmri) != "ON" ]]; then
1478 # Only really sharing operation can enable NFS server
1479 # to online permanently.
1481 typeset dummy=/tmp/dummy
1483 if [[ -d $dummy ]]; then
1484 log_must rm -rf $dummy
1487 log_must mkdir $dummy
1488 log_must share $dummy
1491 # Waiting for fmri's status to be the final status.
1492 # Otherwise, in transition, an asterisk (*) is appended for
1493 # instances, unshare will reverse status to 'DIS' again.
1495 # Waiting for 1's at least.
1499 while [[ timeout -ne 0 && $(svcs -Ho STA $nfs_fmri) == *'*' ]]
1506 log_must unshare $dummy
1507 log_must rm -rf $dummy
1510 log_note "Current NFS status: '$(svcs -Ho STA,FMRI $nfs_fmri)'"
1514 # To verify whether calling process is in global zone
1516 # Return 0 if in global zone, 1 in non-global zone
1518 function is_global_zone
1523 typeset cur_zone=$(zonename 2>/dev/null)
1524 if [[ $cur_zone != "global" ]]; then
1532 # Verify whether test is permitted to run from
1533 # global zone, local zone, or both
1535 # $1 zone limit, could be "global", "local", or "both"(no limit)
1537 # Return 0 if permitted, otherwise exit with log_unsupported
1539 function verify_runnable # zone limit
1543 [[ -z $limit ]] && return 0
1545 if is_global_zone ; then
1549 local) log_unsupported "Test is unable to run from "\
1552 *) log_note "Warning: unknown limit $limit - " \
1560 global) log_unsupported "Test is unable to run from "\
1563 *) log_note "Warning: unknown limit $limit - " \
1574 # Return 0 if create successfully or the pool exists; $? otherwise
1575 # Note: In local zones, this function should return 0 silently.
1578 # $2-n - [keyword] devs_list
1580 function create_pool #pool devs_list
1582 typeset pool=${1%%/*}
1586 if [[ -z $pool ]]; then
1587 log_note "Missing pool name."
1591 if poolexists $pool ; then
1595 if is_global_zone ; then
1596 [[ -d /$pool ]] && rm -rf /$pool
1597 log_must zpool create -f $pool $@
1603 # Return 0 if destroy successfully or the pool exists; $? otherwise
1604 # Note: In local zones, this function should return 0 silently.
1607 # Destroy pool with the given parameters.
1609 function destroy_pool #pool
1611 typeset pool=${1%%/*}
1614 if [[ -z $pool ]]; then
1615 log_note "No pool name given."
1619 if is_global_zone ; then
1620 if poolexists "$pool" ; then
1621 mtpt=$(get_prop mountpoint "$pool")
1623 # At times, syseventd/udev activity can cause attempts
1624 # to destroy a pool to fail with EBUSY. We retry a few
1625 # times allowing failures before requiring the destroy
1627 log_must_busy zpool destroy -f $pool
1630 log_must rm -rf $mtpt
1632 log_note "Pool does not exist. ($pool)"
1640 # Return 0 if created successfully; $? otherwise
1643 # $2-n - dataset options
1645 function create_dataset #dataset dataset_options
1651 if [[ -z $dataset ]]; then
1652 log_note "Missing dataset name."
1656 if datasetexists $dataset ; then
1657 destroy_dataset $dataset
1660 log_must zfs create $@ $dataset
1665 # Return 0 if destroy successfully or the dataset exists; $? otherwise
1666 # Note: In local zones, this function should return 0 silently.
1669 # $2 - custom arguments for zfs destroy
1670 # Destroy dataset with the given parameters.
1672 function destroy_dataset #dataset #args
1676 typeset args=${2:-""}
1678 if [[ -z $dataset ]]; then
1679 log_note "No dataset name given."
1683 if is_global_zone ; then
1684 if datasetexists "$dataset" ; then
1685 mtpt=$(get_prop mountpoint "$dataset")
1686 log_must_busy zfs destroy $args $dataset
1689 log_must rm -rf $mtpt
1691 log_note "Dataset does not exist. ($dataset)"
1700 # Firstly, create a pool with 5 datasets. Then, create a single zone and
1701 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
1702 # and a zvol device to the zone.
1705 # $2 zone root directory prefix
1708 function zfs_zones_setup #zone_name zone_root zone_ip
1710 typeset zone_name=${1:-$(hostname)-z}
1711 typeset zone_root=${2:-"/zone_root"}
1712 typeset zone_ip=${3:-"10.1.1.10"}
1713 typeset prefix_ctr=$ZONE_CTR
1714 typeset pool_name=$ZONE_POOL
1718 # Create pool and 5 container within it
1720 [[ -d /$pool_name ]] && rm -rf /$pool_name
1721 log_must zpool create -f $pool_name $DISKS
1722 while ((i < cntctr)); do
1723 log_must zfs create $pool_name/$prefix_ctr$i
1728 log_must zfs create -V 1g $pool_name/zone_zvol
1732 # If current system support slog, add slog device for pool
1734 if verify_slog_support ; then
1735 typeset sdevs="$TEST_BASE_DIR/sdev1 $TEST_BASE_DIR/sdev2"
1736 log_must mkfile $MINVDEVSIZE $sdevs
1737 log_must zpool add $pool_name log mirror $sdevs
1740 # this isn't supported just yet.
1741 # Create a filesystem. In order to add this to
1742 # the zone, it must have it's mountpoint set to 'legacy'
1743 # log_must zfs create $pool_name/zfs_filesystem
1744 # log_must zfs set mountpoint=legacy $pool_name/zfs_filesystem
1746 [[ -d $zone_root ]] && \
1747 log_must rm -rf $zone_root/$zone_name
1748 [[ ! -d $zone_root ]] && \
1749 log_must mkdir -p -m 0700 $zone_root/$zone_name
1751 # Create zone configure file and configure the zone
1753 typeset zone_conf=/tmp/zone_conf.$$
1754 echo "create" > $zone_conf
1755 echo "set zonepath=$zone_root/$zone_name" >> $zone_conf
1756 echo "set autoboot=true" >> $zone_conf
1758 while ((i < cntctr)); do
1759 echo "add dataset" >> $zone_conf
1760 echo "set name=$pool_name/$prefix_ctr$i" >> \
1762 echo "end" >> $zone_conf
1766 # add our zvol to the zone
1767 echo "add device" >> $zone_conf
1768 echo "set match=/dev/zvol/dsk/$pool_name/zone_zvol" >> $zone_conf
1769 echo "end" >> $zone_conf
1771 # add a corresponding zvol rdsk to the zone
1772 echo "add device" >> $zone_conf
1773 echo "set match=$ZVOL_RDEVDIR/$pool_name/zone_zvol" >> $zone_conf
1774 echo "end" >> $zone_conf
1776 # once it's supported, we'll add our filesystem to the zone
1777 # echo "add fs" >> $zone_conf
1778 # echo "set type=zfs" >> $zone_conf
1779 # echo "set special=$pool_name/zfs_filesystem" >> $zone_conf
1780 # echo "set dir=/export/zfs_filesystem" >> $zone_conf
1781 # echo "end" >> $zone_conf
1783 echo "verify" >> $zone_conf
1784 echo "commit" >> $zone_conf
1785 log_must zonecfg -z $zone_name -f $zone_conf
1786 log_must rm -f $zone_conf
1789 zoneadm -z $zone_name install
1790 if (($? == 0)); then
1791 log_note "SUCCESS: zoneadm -z $zone_name install"
1793 log_fail "FAIL: zoneadm -z $zone_name install"
1796 # Install sysidcfg file
1798 typeset sysidcfg=$zone_root/$zone_name/root/etc/sysidcfg
1799 echo "system_locale=C" > $sysidcfg
1800 echo "terminal=dtterm" >> $sysidcfg
1801 echo "network_interface=primary {" >> $sysidcfg
1802 echo "hostname=$zone_name" >> $sysidcfg
1803 echo "}" >> $sysidcfg
1804 echo "name_service=NONE" >> $sysidcfg
1805 echo "root_password=mo791xfZ/SFiw" >> $sysidcfg
1806 echo "security_policy=NONE" >> $sysidcfg
1807 echo "timezone=US/Eastern" >> $sysidcfg
1810 log_must zoneadm -z $zone_name boot
1814 # Reexport TESTPOOL & TESTPOOL(1-4)
1816 function reexport_pool
1821 while ((i < cntctr)); do
1823 TESTPOOL=$ZONE_POOL/$ZONE_CTR$i
1824 if ! ismounted $TESTPOOL; then
1825 log_must zfs mount $TESTPOOL
1828 eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i
1829 if eval ! ismounted \$TESTPOOL$i; then
1830 log_must eval zfs mount \$TESTPOOL$i
1838 # Verify a given disk or pool state
1840 # Return 0 is pool/disk matches expected state, 1 otherwise
1842 function check_state # pool disk state{online,offline,degraded}
1845 typeset disk=${2#$DEV_DSKDIR/}
1848 [[ -z $pool ]] || [[ -z $state ]] \
1849 && log_fail "Arguments invalid or missing"
1851 if [[ -z $disk ]]; then
1852 #check pool state only
1853 zpool get -H -o value health $pool \
1854 | grep -i "$state" > /dev/null 2>&1
1856 zpool status -v $pool | grep "$disk" \
1857 | grep -i "$state" > /dev/null 2>&1
1864 # Get the mountpoint of snapshot
1865 # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1868 function snapshot_mountpoint
1870 typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1872 if [[ $dataset != *@* ]]; then
1873 log_fail "Error name of snapshot '$dataset'."
1876 typeset fs=${dataset%@*}
1877 typeset snap=${dataset#*@}
1879 if [[ -z $fs || -z $snap ]]; then
1880 log_fail "Error name of snapshot '$dataset'."
1883 echo $(get_prop mountpoint $fs)/.zfs/snapshot/$snap
1887 # Given a device and 'ashift' value verify it's correctly set on every label
1889 function verify_ashift # device ashift
1894 zdb -e -lll $device | awk -v ashift=$ashift '/ashift: / {
1910 # Given a pool and file system, this function will verify the file system
1911 # using the zdb internal tool. Note that the pool is exported and imported
1912 # to ensure it has consistent state.
1914 function verify_filesys # pool filesystem dir
1917 typeset filesys="$2"
1918 typeset zdbout="/tmp/zdbout.$$"
1923 typeset search_path=""
1925 log_note "Calling zdb to verify filesystem '$filesys'"
1926 zfs unmount -a > /dev/null 2>&1
1927 log_must zpool export $pool
1929 if [[ -n $dirs ]] ; then
1930 for dir in $dirs ; do
1931 search_path="$search_path -d $dir"
1935 log_must zpool import $search_path $pool
1937 zdb -cudi $filesys > $zdbout 2>&1
1938 if [[ $? != 0 ]]; then
1939 log_note "Output: zdb -cudi $filesys"
1941 log_fail "zdb detected errors with: '$filesys'"
1944 log_must zfs mount -a
1945 log_must rm -rf $zdbout
1949 # Given a pool issue a scrub and verify that no checksum errors are reported.
1951 function verify_pool
1953 typeset pool=${1:-$TESTPOOL}
1955 log_must zpool scrub $pool
1956 log_must wait_scrubbed $pool
1958 cksum=$(zpool status $pool | awk 'L{print $NF;L=0} /CKSUM$/{L=1}')
1959 if [[ $cksum != 0 ]]; then
1960 log_must zpool status -v
1961 log_fail "Unexpected CKSUM errors found on $pool ($cksum)"
1966 # Given a pool, and this function list all disks in the pool
1968 function get_disklist # pool
1972 disklist=$(zpool iostat -v $1 | nawk '(NR >4) {print $1}' | \
1973 grep -v "\-\-\-\-\-" | \
1974 egrep -v -e "^(mirror|raidz[1-3]|spare|log|cache|special|dedup)$")
1980 # Given a pool, and this function list all disks in the pool with their full
1981 # path (like "/dev/sda" instead of "sda").
1983 function get_disklist_fullpath # pool
1992 # This function kills a given list of processes after a time period. We use
1993 # this in the stress tests instead of STF_TIMEOUT so that we can have processes
1994 # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1995 # would be listed as FAIL, which we don't want : we're happy with stress tests
1996 # running for a certain amount of time, then finishing.
1998 # @param $1 the time in seconds after which we should terminate these processes
1999 # @param $2..$n the processes we wish to terminate.
2001 function stress_timeout
2003 typeset -i TIMEOUT=$1
2007 log_note "Waiting for child processes($cpids). " \
2008 "It could last dozens of minutes, please be patient ..."
2009 log_must sleep $TIMEOUT
2011 log_note "Killing child processes after ${TIMEOUT} stress timeout."
2013 for pid in $cpids; do
2014 ps -p $pid > /dev/null 2>&1
2015 if (($? == 0)); then
2016 log_must kill -USR1 $pid
2022 # Verify a given hotspare disk is inuse or avail
2024 # Return 0 is pool/disk matches expected state, 1 otherwise
2026 function check_hotspare_state # pool disk state{inuse,avail}
2029 typeset disk=${2#$DEV_DSKDIR/}
2032 cur_state=$(get_device_state $pool $disk "spares")
2034 if [[ $state != ${cur_state} ]]; then
2041 # Wait until a hotspare transitions to a given state or times out.
2043 # Return 0 when pool/disk matches expected state, 1 on timeout.
2045 function wait_hotspare_state # pool disk state timeout
2048 typeset disk=${2#*$DEV_DSKDIR/}
2050 typeset timeout=${4:-60}
2053 while [[ $i -lt $timeout ]]; do
2054 if check_hotspare_state $pool $disk $state; then
2066 # Verify a given slog disk is inuse or avail
2068 # Return 0 is pool/disk matches expected state, 1 otherwise
2070 function check_slog_state # pool disk state{online,offline,unavail}
2073 typeset disk=${2#$DEV_DSKDIR/}
2076 cur_state=$(get_device_state $pool $disk "logs")
2078 if [[ $state != ${cur_state} ]]; then
2085 # Verify a given vdev disk is inuse or avail
2087 # Return 0 is pool/disk matches expected state, 1 otherwise
2089 function check_vdev_state # pool disk state{online,offline,unavail}
2092 typeset disk=${2#*$DEV_DSKDIR/}
2095 cur_state=$(get_device_state $pool $disk)
2097 if [[ $state != ${cur_state} ]]; then
2104 # Wait until a vdev transitions to a given state or times out.
2106 # Return 0 when pool/disk matches expected state, 1 on timeout.
2108 function wait_vdev_state # pool disk state timeout
2111 typeset disk=${2#*$DEV_DSKDIR/}
2113 typeset timeout=${4:-60}
2116 while [[ $i -lt $timeout ]]; do
2117 if check_vdev_state $pool $disk $state; then
2129 # Check the output of 'zpool status -v <pool>',
2130 # and to see if the content of <token> contain the <keyword> specified.
2132 # Return 0 is contain, 1 otherwise
2134 function check_pool_status # pool token keyword <verbose>
2139 typeset verbose=${4:-false}
2141 scan=$(zpool status -v "$pool" 2>/dev/null | nawk -v token="$token:" '
2142 ($1==token) {print $0}')
2143 if [[ $verbose == true ]]; then
2146 echo $scan | grep -i "$keyword" > /dev/null 2>&1
2152 # These 6 following functions are instance of check_pool_status()
2153 # is_pool_resilvering - to check if the pool is resilver in progress
2154 # is_pool_resilvered - to check if the pool is resilver completed
2155 # is_pool_scrubbing - to check if the pool is scrub in progress
2156 # is_pool_scrubbed - to check if the pool is scrub completed
2157 # is_pool_scrub_stopped - to check if the pool is scrub stopped
2158 # is_pool_scrub_paused - to check if the pool has scrub paused
2159 # is_pool_removing - to check if the pool is removing a vdev
2160 # is_pool_removed - to check if the pool is remove completed
2162 function is_pool_resilvering #pool <verbose>
2164 check_pool_status "$1" "scan" "resilver in progress since " $2
2168 function is_pool_resilvered #pool <verbose>
2170 check_pool_status "$1" "scan" "resilvered " $2
2174 function is_pool_scrubbing #pool <verbose>
2176 check_pool_status "$1" "scan" "scrub in progress since " $2
2180 function is_pool_scrubbed #pool <verbose>
2182 check_pool_status "$1" "scan" "scrub repaired" $2
2186 function is_pool_scrub_stopped #pool <verbose>
2188 check_pool_status "$1" "scan" "scrub canceled" $2
2192 function is_pool_scrub_paused #pool <verbose>
2194 check_pool_status "$1" "scan" "scrub paused since " $2
2198 function is_pool_removing #pool
2200 check_pool_status "$1" "remove" "in progress since "
2204 function is_pool_removed #pool
2206 check_pool_status "$1" "remove" "completed on"
2210 function wait_for_degraded
2213 typeset timeout=${2:-30}
2217 [[ $(get_pool_prop health $pool) == "DEGRADED" ]] && break
2218 log_note "$pool is not yet degraded."
2220 if ((SECONDS - t0 > $timeout)); then
2221 log_note "$pool not degraded after $timeout seconds."
2230 # Use create_pool()/destroy_pool() to clean up the information in
2231 # in the given disk to avoid slice overlapping.
2233 function cleanup_devices #vdevs
2235 typeset pool="foopool$$"
2237 if poolexists $pool ; then
2241 create_pool $pool $@
2248 # A function to find and locate free disks on a system or from given
2249 # disks as the parameter. It works by locating disks that are in use
2250 # as swap devices and dump devices, and also disks listed in /etc/vfstab
2252 # $@ given disks to find which are free, default is all disks in
2255 # @return a string containing the list of available disks
2259 # Trust provided list, no attempt is made to locate unused devices.
2266 sfi=/tmp/swaplist.$$
2267 dmpi=/tmp/dumpdev.$$
2268 max_finddisksnum=${MAX_FINDDISKSNUM:-6}
2271 dumpadm > $dmpi 2>/dev/null
2273 # write an awk script that can process the output of format
2274 # to produce a list of disks we know about. Note that we have
2275 # to escape "$2" so that the shell doesn't interpret it while
2276 # we're creating the awk script.
2277 # -------------------
2278 cat > /tmp/find_disks.awk <<EOF
2287 if (searchdisks && \$2 !~ "^$"){
2293 /^AVAILABLE DISK SELECTIONS:/{
2297 #---------------------
2299 chmod 755 /tmp/find_disks.awk
2300 disks=${@:-$(echo "" | format -e 2>/dev/null | /tmp/find_disks.awk)}
2301 rm /tmp/find_disks.awk
2304 for disk in $disks; do
2306 grep "${disk}[sp]" /etc/mnttab >/dev/null
2307 (($? == 0)) && continue
2309 grep "${disk}[sp]" $sfi >/dev/null
2310 (($? == 0)) && continue
2311 # check for dump device
2312 grep "${disk}[sp]" $dmpi >/dev/null
2313 (($? == 0)) && continue
2314 # check to see if this disk hasn't been explicitly excluded
2315 # by a user-set environment variable
2316 echo "${ZFS_HOST_DEVICES_IGNORE}" | grep "${disk}" > /dev/null
2317 (($? == 0)) && continue
2318 unused_candidates="$unused_candidates $disk"
2323 # now just check to see if those disks do actually exist
2324 # by looking for a device pointing to the first slice in
2325 # each case. limit the number to max_finddisksnum
2327 for disk in $unused_candidates; do
2328 if [ -b $DEV_DSKDIR/${disk}s0 ]; then
2329 if [ $count -lt $max_finddisksnum ]; then
2330 unused="$unused $disk"
2331 # do not impose limit if $@ is provided
2332 [[ -z $@ ]] && ((count = count + 1))
2337 # finally, return our disk list
2342 # Add specified user to specified group
2346 # $3 base of the homedir (optional)
2348 function add_user #<group_name> <user_name> <basedir>
2352 typeset basedir=${3:-"/var/tmp"}
2354 if ((${#gname} == 0 || ${#uname} == 0)); then
2355 log_fail "group name or user name are not defined."
2358 log_must useradd -g $gname -d $basedir/$uname -m $uname
2359 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.profile
2360 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.bash_profile
2361 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.login
2363 # Add new users to the same group and the command line utils.
2364 # This allows them to be run out of the original users home
2365 # directory as long as it permissioned to be group readable.
2367 cmd_group=$(stat --format="%G" $(which zfs))
2368 log_must usermod -a -G $cmd_group $uname
2375 # Delete the specified user.
2378 # $2 base of the homedir (optional)
2380 function del_user #<logname> <basedir>
2383 typeset basedir=${2:-"/var/tmp"}
2385 if ((${#user} == 0)); then
2386 log_fail "login name is necessary."
2389 if id $user > /dev/null 2>&1; then
2390 log_must_retry "currently used" 5 userdel $user
2393 [[ -d $basedir/$user ]] && rm -fr $basedir/$user
2399 # Select valid gid and create specified group.
2403 function add_group #<group_name>
2407 if ((${#group} == 0)); then
2408 log_fail "group name is necessary."
2411 # Assign 100 as the base gid, a larger value is selected for
2412 # Linux because for many distributions 1000 and under are reserved.
2415 groupadd $group > /dev/null 2>&1
2425 groupadd -g $gid $group > /dev/null 2>&1
2429 # The gid is not unique
2438 # Delete the specified group.
2442 function del_group #<group_name>
2445 if ((${#grp} == 0)); then
2446 log_fail "group name is necessary."
2450 getent group $grp > /dev/null 2>&1
2453 # Group does not exist.
2455 # Name already exists as a group name
2456 0) log_must groupdel $grp ;;
2460 groupmod -n $grp $grp > /dev/null 2>&1
2463 # Group does not exist.
2465 # Name already exists as a group name
2466 9) log_must groupdel $grp ;;
2475 # This function will return true if it's safe to destroy the pool passed
2476 # as argument 1. It checks for pools based on zvols and files, and also
2477 # files contained in a pool that may have a different mountpoint.
2479 function safe_to_destroy_pool { # $1 the pool name
2482 typeset DONT_DESTROY=""
2484 # We check that by deleting the $1 pool, we're not
2485 # going to pull the rug out from other pools. Do this
2486 # by looking at all other pools, ensuring that they
2487 # aren't built from files or zvols contained in this pool.
2489 for pool in $(zpool list -H -o name)
2493 # this is a list of the top-level directories in each of the
2494 # files that make up the path to the files the pool is based on
2495 FILEPOOL=$(zpool status -v $pool | grep /$1/ | \
2498 # this is a list of the zvols that make up the pool
2499 ZVOLPOOL=$(zpool status -v $pool | grep "$ZVOL_DEVDIR/$1$" \
2502 # also want to determine if it's a file-based pool using an
2503 # alternate mountpoint...
2504 POOL_FILE_DIRS=$(zpool status -v $pool | \
2505 grep / | awk '{print $1}' | \
2506 awk -F/ '{print $2}' | grep -v "dev")
2508 for pooldir in $POOL_FILE_DIRS
2510 OUTPUT=$(zfs list -H -r -o mountpoint $1 | \
2511 grep "${pooldir}$" | awk '{print $1}')
2513 ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
2517 if [ ! -z "$ZVOLPOOL" ]
2520 log_note "Pool $pool is built from $ZVOLPOOL on $1"
2523 if [ ! -z "$FILEPOOL" ]
2526 log_note "Pool $pool is built from $FILEPOOL on $1"
2529 if [ ! -z "$ALTMOUNTPOOL" ]
2532 log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
2536 if [ -z "${DONT_DESTROY}" ]
2540 log_note "Warning: it is not safe to destroy $1!"
2546 # Get the available ZFS compression options
2547 # $1 option type zfs_set|zfs_compress
2549 function get_compress_opts
2551 typeset COMPRESS_OPTS
2552 typeset GZIP_OPTS="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2553 gzip-6 gzip-7 gzip-8 gzip-9"
2555 if [[ $1 == "zfs_compress" ]] ; then
2556 COMPRESS_OPTS="on lzjb"
2557 elif [[ $1 == "zfs_set" ]] ; then
2558 COMPRESS_OPTS="on off lzjb"
2560 typeset valid_opts="$COMPRESS_OPTS"
2561 zfs get 2>&1 | grep gzip >/dev/null 2>&1
2562 if [[ $? -eq 0 ]]; then
2563 valid_opts="$valid_opts $GZIP_OPTS"
2569 # Verify zfs operation with -p option work as expected
2570 # $1 operation, value could be create, clone or rename
2571 # $2 dataset type, value could be fs or vol
2573 # $4 new dataset name
2575 function verify_opt_p_ops
2580 typeset newdataset=$4
2582 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2583 log_fail "$datatype is not supported."
2586 # check parameters accordingly
2591 if [[ $datatype == "vol" ]]; then
2592 ops="create -V $VOLSIZE"
2596 if [[ -z $newdataset ]]; then
2597 log_fail "newdataset should not be empty" \
2600 log_must datasetexists $dataset
2601 log_must snapexists $dataset
2604 if [[ -z $newdataset ]]; then
2605 log_fail "newdataset should not be empty" \
2608 log_must datasetexists $dataset
2611 log_fail "$ops is not supported."
2615 # make sure the upper level filesystem does not exist
2616 destroy_dataset "${newdataset%/*}" "-rRf"
2618 # without -p option, operation will fail
2619 log_mustnot zfs $ops $dataset $newdataset
2620 log_mustnot datasetexists $newdataset ${newdataset%/*}
2622 # with -p option, operation should succeed
2623 log_must zfs $ops -p $dataset $newdataset
2626 if ! datasetexists $newdataset ; then
2627 log_fail "-p option does not work for $ops"
2630 # when $ops is create or clone, redo the operation still return zero
2631 if [[ $ops != "rename" ]]; then
2632 log_must zfs $ops -p $dataset $newdataset
2639 # Get configuration of pool
2649 if ! poolexists "$pool" ; then
2652 alt_root=$(zpool list -H $pool | awk '{print $NF}')
2653 if [[ $alt_root == "-" ]]; then
2654 value=$(zdb -C $pool | grep "$config:" | awk -F: \
2657 value=$(zdb -e $pool | grep "$config:" | awk -F: \
2660 if [[ -n $value ]] ; then
2670 # Privated function. Random select one of items from arguments.
2675 function _random_get
2682 ((ind = RANDOM % cnt + 1))
2684 typeset ret=$(echo "$str" | cut -f $ind -d ' ')
2689 # Random select one of item from arguments which include NONE string
2691 function random_get_with_non
2696 _random_get "$cnt" "$@"
2700 # Random select one of item from arguments which doesn't include NONE string
2704 _random_get "$#" "$@"
2708 # Detect if the current system support slog
2710 function verify_slog_support
2712 typeset dir=$TEST_BASE_DIR/disk.$$
2718 mkfile $MINVDEVSIZE $vdev $sdev
2721 if ! zpool create -n $pool $vdev log $sdev > /dev/null 2>&1; then
2730 # The function will generate a dataset name with specific length
2731 # $1, the length of the name
2732 # $2, the base string to construct the name
2734 function gen_dataset_name
2737 typeset basestr="$2"
2738 typeset -i baselen=${#basestr}
2742 if ((len % baselen == 0)); then
2743 ((iter = len / baselen))
2745 ((iter = len / baselen + 1))
2747 while ((iter > 0)); do
2748 l_name="${l_name}$basestr"
2757 # Get cksum tuple of dataset
2760 # sample zdb output:
2761 # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2762 # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2763 # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2764 # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2765 function datasetcksum
2769 cksum=$(zdb -vvv $1 | grep "^Dataset $1 \[" | grep "cksum" \
2770 | awk -F= '{print $7}')
2781 cksum=$(cksum $1 | awk '{print $1}')
2786 # Get the given disk/slice state from the specific field of the pool
2788 function get_device_state #pool disk field("", "spares","logs")
2791 typeset disk=${2#$DEV_DSKDIR/}
2792 typeset field=${3:-$pool}
2794 state=$(zpool status -v "$pool" 2>/dev/null | \
2795 nawk -v device=$disk -v pool=$pool -v field=$field \
2796 'BEGIN {startconfig=0; startfield=0; }
2797 /config:/ {startconfig=1}
2798 (startconfig==1) && ($1==field) {startfield=1; next;}
2799 (startfield==1) && ($1==device) {print $2; exit;}
2801 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2807 # print the given directory filesystem type
2815 if [[ -z $dir ]]; then
2816 log_fail "Usage: get_fstype <directory>"
2823 df -n $dir | awk '{print $3}'
2827 # Given a disk, label it to VTOC regardless what label was on the disk
2833 if [[ -z $disk ]]; then
2834 log_fail "The disk name is unspecified."
2836 typeset label_file=/var/tmp/labelvtoc.$$
2837 typeset arch=$(uname -p)
2840 log_note "Currently unsupported by the test framework"
2844 if [[ $arch == "i386" ]]; then
2845 echo "label" > $label_file
2846 echo "0" >> $label_file
2847 echo "" >> $label_file
2848 echo "q" >> $label_file
2849 echo "q" >> $label_file
2851 fdisk -B $disk >/dev/null 2>&1
2852 # wait a while for fdisk finishes
2854 elif [[ $arch == "sparc" ]]; then
2855 echo "label" > $label_file
2856 echo "0" >> $label_file
2857 echo "" >> $label_file
2858 echo "" >> $label_file
2859 echo "" >> $label_file
2860 echo "q" >> $label_file
2862 log_fail "unknown arch type"
2865 format -e -s -d $disk -f $label_file
2866 typeset -i ret_val=$?
2869 # wait the format to finish
2872 if ((ret_val != 0)); then
2873 log_fail "unable to label $disk as VTOC."
2880 # check if the system was installed as zfsroot or not
2881 # return: 0 ture, otherwise false
2885 df -n / | grep zfs > /dev/null 2>&1
2890 # get the root filesystem name if it's zfsroot system.
2892 # return: root filesystem name
2898 rootfs=$(awk '{if ($2 == "/" && $3 == "zfs") print $1}' \
2901 if [[ -z "$rootfs" ]]; then
2902 log_fail "Can not get rootfs"
2904 zfs list $rootfs > /dev/null 2>&1
2905 if (($? == 0)); then
2908 log_fail "This is not a zfsroot system."
2913 # get the rootfs's pool name
2917 function get_rootpool
2923 rootfs=$(awk '{if ($2 == "/" && $3 =="zfs") print $1}' \
2926 if [[ -z "$rootfs" ]]; then
2927 log_fail "Can not get rootpool"
2929 zfs list $rootfs > /dev/null 2>&1
2930 if (($? == 0)); then
2931 rootpool=`echo $rootfs | awk -F\/ '{print $1}'`
2934 log_fail "This is not a zfsroot system."
2939 # Get the package name
2941 function get_package_name
2943 typeset dirpath=${1:-$STC_NAME}
2945 echo "SUNWstc-${dirpath}" | /usr/bin/sed -e "s/\//-/g"
2949 # Get the word numbers from a string separated by white space
2951 function get_word_count
2957 # To verify if the require numbers of disks is given
2959 function verify_disk_count
2961 typeset -i min=${2:-1}
2963 typeset -i count=$(get_word_count "$1")
2965 if ((count < min)); then
2966 log_untested "A minimum of $min disks is required to run." \
2967 " You specified $count disk(s)"
2971 function ds_is_volume
2973 typeset type=$(get_prop type $1)
2974 [[ $type = "volume" ]] && return 0
2978 function ds_is_filesystem
2980 typeset type=$(get_prop type $1)
2981 [[ $type = "filesystem" ]] && return 0
2985 function ds_is_snapshot
2987 typeset type=$(get_prop type $1)
2988 [[ $type = "snapshot" ]] && return 0
2993 # Check if Trusted Extensions are installed and enabled
2995 function is_te_enabled
2997 svcs -H -o state labeld 2>/dev/null | grep "enabled"
2998 if (($? != 0)); then
3005 # Utility function to determine if a system has multiple cpus.
3011 (($(psrinfo | wc -l) > 1))
3017 function get_cpu_freq
3020 lscpu | awk '/CPU MHz/ { print $3 }'
3022 psrinfo -v 0 | awk '/processor operates at/ {print $6}'
3026 # Run the given command as the user provided.
3032 log_note "user:$user $@"
3033 eval su - \$user -c \"$@\" > $TEST_BASE_DIR/out 2>$TEST_BASE_DIR/err
3038 # Check if the pool contains the specified vdevs
3043 # Return 0 if the vdevs are contained in the pool, 1 if any of the specified
3044 # vdevs is not in the pool, and 2 if pool name is missing.
3046 function vdevs_in_pool
3051 if [[ -z $pool ]]; then
3052 log_note "Missing pool name."
3058 # We could use 'zpool list' to only get the vdevs of the pool but we
3059 # can't reference a mirror/raidz vdev using its ID (i.e mirror-0),
3060 # therefore we use the 'zpool status' output.
3061 typeset tmpfile=$(mktemp)
3062 zpool status -v "$pool" | grep -A 1000 "config:" >$tmpfile
3064 grep -w ${vdev##*/} $tmpfile >/dev/null 2>&1
3065 [[ $? -ne 0 ]] && return 1
3079 max=$(echo $((max > i ? max : i)))
3091 min=$(echo $((min < i ? min : i)))
3098 # Generate a random number between 1 and the argument.
3103 echo $(( ($RANDOM % $max) + 1 ))
3106 # Write data that can be compressed into a directory
3107 function write_compressible
3111 typeset nfiles=${3:-1}
3112 typeset bs=${4:-1024k}
3113 typeset fname=${5:-file}
3115 [[ -d $dir ]] || log_fail "No directory: $dir"
3117 # Under Linux fio is not currently used since its behavior can
3118 # differ significantly across versions. This includes missing
3119 # command line options and cases where the --buffer_compress_*
3120 # options fail to behave as expected.
3122 typeset file_bytes=$(to_bytes $megs)
3123 typeset bs_bytes=4096
3124 typeset blocks=$(($file_bytes / $bs_bytes))
3126 for (( i = 0; i < $nfiles; i++ )); do
3127 truncate -s $file_bytes $dir/$fname.$i
3129 # Write every third block to get 66% compression.
3130 for (( j = 0; j < $blocks; j += 3 )); do
3131 dd if=/dev/urandom of=$dir/$fname.$i \
3132 seek=$j bs=$bs_bytes count=1 \
3133 conv=notrunc >/dev/null 2>&1
3137 log_must eval "fio \
3142 --buffer_compress_percentage=66 \
3143 --buffer_compress_chunk=4096 \
3150 --filename_format='$fname.\$jobnum' >/dev/null"
3159 [[ -e $pathname ]] || log_fail "No such file or directory: $pathname"
3160 objnum=$(stat -c %i $pathname)
3165 # Sync data to the pool
3168 # $2 boolean to force uberblock (and config including zpool cache file) update
3170 function sync_pool #pool <force>
3172 typeset pool=${1:-$TESTPOOL}
3173 typeset force=${2:-false}
3175 if [[ $force == true ]]; then
3176 log_must zpool sync -f $pool
3178 log_must zpool sync $pool
3185 # Wait for zpool 'freeing' property drops to zero.
3189 function wait_freeing #pool
3191 typeset pool=${1:-$TESTPOOL}
3193 [[ "0" == "$(zpool list -Ho freeing $pool)" ]] && break
3199 # Wait for every device replace operation to complete
3203 function wait_replacing #pool
3205 typeset pool=${1:-$TESTPOOL}
3207 [[ "" == "$(zpool status $pool |
3208 awk '/replacing-[0-9]+/ {print $1}')" ]] && break
3214 # Wait for a pool to be scrubbed
3217 # $2 number of seconds to wait (optional)
3219 # Returns true when pool has been scrubbed, or false if there's a timeout or if
3220 # no scrub was done.
3222 function wait_scrubbed
3224 typeset pool=${1:-$TESTPOOL}
3226 is_pool_scrubbed $pool && break
3231 # Backup the zed.rc in our test directory so that we can edit it for our test.
3233 # Returns: Backup file name. You will need to pass this to zed_rc_restore().
3234 function zed_rc_backup
3236 zedrc_backup="$(mktemp)"
3237 cp $ZEDLET_DIR/zed.rc $zedrc_backup
3241 function zed_rc_restore
3243 mv $1 $ZEDLET_DIR/zed.rc
3247 # Setup custom environment for the ZED.
3249 # $@ Optional list of zedlets to run under zed.
3256 if [[ ! -d $ZEDLET_DIR ]]; then
3257 log_must mkdir $ZEDLET_DIR
3260 if [[ ! -e $VDEVID_CONF ]]; then
3261 log_must touch $VDEVID_CONF
3264 if [[ -e $VDEVID_CONF_ETC ]]; then
3265 log_fail "Must not have $VDEVID_CONF_ETC file present on system"
3269 # Create a symlink for /etc/zfs/vdev_id.conf file.
3270 log_must ln -s $VDEVID_CONF $VDEVID_CONF_ETC
3272 # Setup minimal ZED configuration. Individual test cases should
3273 # add additional ZEDLETs as needed for their specific test.
3274 log_must cp ${ZEDLET_ETC_DIR}/zed.rc $ZEDLET_DIR
3275 log_must cp ${ZEDLET_ETC_DIR}/zed-functions.sh $ZEDLET_DIR
3277 # Scripts must only be user writable.
3278 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3279 saved_umask=$(umask)
3281 for i in $EXTRA_ZEDLETS ; do
3282 log_must cp ${ZEDLET_LIBEXEC_DIR}/$i $ZEDLET_DIR
3284 log_must umask $saved_umask
3287 # Customize the zed.rc file to enable the full debug log.
3288 log_must sed -i '/\#ZED_DEBUG_LOG=.*/d' $ZEDLET_DIR/zed.rc
3289 echo "ZED_DEBUG_LOG=$ZED_DEBUG_LOG" >>$ZEDLET_DIR/zed.rc
3294 # Cleanup custom ZED environment.
3296 # $@ Optional list of zedlets to remove from our test zed.d directory.
3297 function zed_cleanup
3304 log_must rm -f ${ZEDLET_DIR}/zed.rc
3305 log_must rm -f ${ZEDLET_DIR}/zed-functions.sh
3306 log_must rm -f ${ZEDLET_DIR}/all-syslog.sh
3307 log_must rm -f ${ZEDLET_DIR}/all-debug.sh
3308 log_must rm -f ${ZEDLET_DIR}/state
3310 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3311 for i in $EXTRA_ZEDLETS ; do
3312 log_must rm -f ${ZEDLET_DIR}/$i
3315 log_must rm -f $ZED_LOG
3316 log_must rm -f $ZED_DEBUG_LOG
3317 log_must rm -f $VDEVID_CONF_ETC
3318 log_must rm -f $VDEVID_CONF
3323 # Check if ZED is currently running, if not start ZED.
3331 # ZEDLET_DIR=/var/tmp/zed
3332 if [[ ! -d $ZEDLET_DIR ]]; then
3333 log_must mkdir $ZEDLET_DIR
3336 # Verify the ZED is not already running.
3337 pgrep -x zed > /dev/null
3338 if (($? == 0)); then
3339 log_fail "ZED already running"
3342 log_note "Starting ZED"
3343 # run ZED in the background and redirect foreground logging
3344 # output to $ZED_LOG.
3345 log_must truncate -s 0 $ZED_DEBUG_LOG
3346 log_must eval "zed -vF -d $ZEDLET_DIR -p $ZEDLET_DIR/zed.pid -P $PATH" \
3347 "-s $ZEDLET_DIR/state 2>$ZED_LOG &"
3361 log_note "Stopping ZED"
3362 if [[ -f ${ZEDLET_DIR}/zed.pid ]]; then
3363 zedpid=$(<${ZEDLET_DIR}/zed.pid)
3365 while ps -p $zedpid > /dev/null; do
3368 rm -f ${ZEDLET_DIR}/zed.pid
3376 function zed_events_drain
3378 while [ $(zpool events -H | wc -l) -ne 0 ]; do
3380 zpool events -c >/dev/null
3384 # Set a variable in zed.rc to something, un-commenting it in the process.
3394 eval sed -i $cmd $ZEDLET_DIR/zed.rc
3397 echo "$var=$val" >> $ZEDLET_DIR/zed.rc
3402 # Check is provided device is being active used as a swap device.
3404 function is_swap_inuse
3408 if [[ -z $device ]] ; then
3409 log_note "No device specified."
3414 swapon -s | grep -w $(readlink -f $device) > /dev/null 2>&1
3416 swap -l | grep -w $device > /dev/null 2>&1
3423 # Setup a swap device using the provided device.
3430 log_must eval "mkswap $swapdev > /dev/null 2>&1"
3431 log_must swapon $swapdev
3433 log_must swap -a $swapdev
3440 # Cleanup a swap device on the provided device.
3442 function swap_cleanup
3446 if is_swap_inuse $swapdev; then
3448 log_must swapoff $swapdev
3450 log_must swap -d $swapdev
3458 # Set a global system tunable (64-bit value)
3463 function set_tunable64
3465 set_tunable_impl "$1" "$2" Z
3469 # Set a global system tunable (32-bit value)
3474 function set_tunable32
3476 set_tunable_impl "$1" "$2" W
3479 function set_tunable_impl
3481 typeset tunable="$1"
3483 typeset mdb_cmd="$3"
3484 typeset module="${4:-zfs}"
3486 [[ -z "$tunable" ]] && return 1
3487 [[ -z "$value" ]] && return 1
3488 [[ -z "$mdb_cmd" ]] && return 1
3492 typeset zfs_tunables="/sys/module/$module/parameters"
3493 [[ -w "$zfs_tunables/$tunable" ]] || return 1
3494 echo -n "$value" > "$zfs_tunables/$tunable"
3498 [[ "$module" -eq "zfs" ]] || return 1
3499 echo "${tunable}/${mdb_cmd}0t${value}" | mdb -kw
3506 # Get a global system tunable
3510 function get_tunable
3512 get_tunable_impl "$1"
3515 function get_tunable_impl
3517 typeset tunable="$1"
3518 typeset module="${2:-zfs}"
3520 [[ -z "$tunable" ]] && return 1
3524 typeset zfs_tunables="/sys/module/$module/parameters"
3525 [[ -f "$zfs_tunables/$tunable" ]] || return 1
3526 cat $zfs_tunables/$tunable
3530 [[ "$module" -eq "zfs" ]] || return 1
3538 # Prints the current time in seconds since UNIX Epoch.
3540 function current_epoch
3546 # Get decimal value of global uint32_t variable using mdb.
3548 function mdb_get_uint32
3553 value=$(mdb -k -e "$variable/X | ::eval .=U")
3554 if [[ $? -ne 0 ]]; then
3555 log_fail "Failed to get value of '$variable' from mdb."
3564 # Set global uint32_t variable to a decimal value using mdb.
3566 function mdb_set_uint32
3571 mdb -kw -e "$variable/W 0t$value" > /dev/null
3572 if [[ $? -ne 0 ]]; then
3573 echo "Failed to set '$variable' to '$value' in mdb."
3581 # Set global scalar integer variable to a hex value using mdb.
3582 # Note: Target should have CTF data loaded.
3584 function mdb_ctf_set_int
3589 mdb -kw -e "$variable/z $value" > /dev/null
3590 if [[ $? -ne 0 ]]; then
3591 echo "Failed to set '$variable' to '$value' in mdb."