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 "$@"
218 # Given a list of disks, setup storage pools and datasets.
220 function default_setup_noexit
225 log_note begin default_setup_noexit
227 if is_global_zone; then
228 if poolexists $TESTPOOL ; then
229 destroy_pool $TESTPOOL
231 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
232 log_must zpool create -f $TESTPOOL $disklist
237 rm -rf $TESTDIR || log_unresolved Could not remove $TESTDIR
238 mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR
240 log_must zfs create $TESTPOOL/$TESTFS
241 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
243 if [[ -n $container ]]; then
244 rm -rf $TESTDIR1 || \
245 log_unresolved Could not remove $TESTDIR1
246 mkdir -p $TESTDIR1 || \
247 log_unresolved Could not create $TESTDIR1
249 log_must zfs create $TESTPOOL/$TESTCTR
250 log_must zfs set canmount=off $TESTPOOL/$TESTCTR
251 log_must zfs create $TESTPOOL/$TESTCTR/$TESTFS1
252 log_must zfs set mountpoint=$TESTDIR1 \
253 $TESTPOOL/$TESTCTR/$TESTFS1
256 if [[ -n $volume ]]; then
257 if is_global_zone ; then
258 log_must zfs create -V $VOLSIZE $TESTPOOL/$TESTVOL
261 log_must zfs create $TESTPOOL/$TESTVOL
267 # Given a list of disks, setup a storage pool, file system and
270 function default_container_setup
274 default_setup "$disklist" "true"
278 # Given a list of disks, setup a storage pool,file system
281 function default_volume_setup
285 default_setup "$disklist" "" "true"
289 # Given a list of disks, setup a storage pool,file system,
290 # a container and a volume.
292 function default_container_volume_setup
296 default_setup "$disklist" "true" "true"
300 # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
303 # $1 Existing filesystem or volume name. Default, $TESTPOOL/$TESTFS
304 # $2 snapshot name. Default, $TESTSNAP
306 function create_snapshot
308 typeset fs_vol=${1:-$TESTPOOL/$TESTFS}
309 typeset snap=${2:-$TESTSNAP}
311 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
312 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
314 if snapexists $fs_vol@$snap; then
315 log_fail "$fs_vol@$snap already exists."
317 datasetexists $fs_vol || \
318 log_fail "$fs_vol must exist."
320 log_must zfs snapshot $fs_vol@$snap
324 # Create a clone from a snapshot, default clone name is $TESTCLONE.
326 # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
327 # $2 Clone name, $TESTPOOL/$TESTCLONE is default.
329 function create_clone # snapshot clone
331 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
332 typeset clone=${2:-$TESTPOOL/$TESTCLONE}
335 log_fail "Snapshot name is undefined."
337 log_fail "Clone name is undefined."
339 log_must zfs clone $snap $clone
343 # Create a bookmark of the given snapshot. Defaultly create a bookmark on
346 # $1 Existing filesystem or volume name. Default, $TESTFS
347 # $2 Existing snapshot name. Default, $TESTSNAP
348 # $3 bookmark name. Default, $TESTBKMARK
350 function create_bookmark
352 typeset fs_vol=${1:-$TESTFS}
353 typeset snap=${2:-$TESTSNAP}
354 typeset bkmark=${3:-$TESTBKMARK}
356 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
357 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
358 [[ -z $bkmark ]] && log_fail "Bookmark's name is undefined."
360 if bkmarkexists $fs_vol#$bkmark; then
361 log_fail "$fs_vol#$bkmark already exists."
363 datasetexists $fs_vol || \
364 log_fail "$fs_vol must exist."
365 snapexists $fs_vol@$snap || \
366 log_fail "$fs_vol@$snap must exist."
368 log_must zfs bookmark $fs_vol@$snap $fs_vol#$bkmark
372 # Create a temporary clone result of an interrupted resumable 'zfs receive'
373 # $1 Destination filesystem name. Must not exist, will be created as the result
374 # of this function along with its %recv temporary clone
375 # $2 Source filesystem name. Must not exist, will be created and destroyed
377 function create_recv_clone
380 typeset sendfs="${2:-$TESTPOOL/create_recv_clone}"
381 typeset snap="$sendfs@snap1"
382 typeset incr="$sendfs@snap2"
383 typeset mountpoint="$TESTDIR/create_recv_clone"
384 typeset sendfile="$TESTDIR/create_recv_clone.zsnap"
386 [[ -z $recvfs ]] && log_fail "Recv filesystem's name is undefined."
388 datasetexists $recvfs && log_fail "Recv filesystem must not exist."
389 datasetexists $sendfs && log_fail "Send filesystem must not exist."
391 log_must zfs create -o mountpoint="$mountpoint" $sendfs
392 log_must zfs snapshot $snap
393 log_must eval "zfs send $snap | zfs recv -u $recvfs"
394 log_must mkfile 1m "$mountpoint/data"
395 log_must zfs snapshot $incr
396 log_must eval "zfs send -i $snap $incr | dd bs=10K count=1 > $sendfile"
397 log_mustnot eval "zfs recv -su $recvfs < $sendfile"
398 destroy_dataset "$sendfs" "-r"
399 log_must rm -f "$sendfile"
401 if [[ $(get_prop 'inconsistent' "$recvfs/%recv") -ne 1 ]]; then
402 log_fail "Error creating temporary $recvfs/%recv clone"
406 function default_mirror_setup
408 default_mirror_setup_noexit $1 $2 $3
414 # Given a pair of disks, set up a storage pool and dataset for the mirror
415 # @parameters: $1 the primary side of the mirror
416 # $2 the secondary side of the mirror
417 # @uses: ZPOOL ZFS TESTPOOL TESTFS
418 function default_mirror_setup_noexit
420 readonly func="default_mirror_setup_noexit"
424 [[ -z $primary ]] && \
425 log_fail "$func: No parameters passed"
426 [[ -z $secondary ]] && \
427 log_fail "$func: No secondary partition passed"
428 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
429 log_must zpool create -f $TESTPOOL mirror $@
430 log_must zfs create $TESTPOOL/$TESTFS
431 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
435 # create a number of mirrors.
436 # We create a number($1) of 2 way mirrors using the pairs of disks named
437 # on the command line. These mirrors are *not* mounted
438 # @parameters: $1 the number of mirrors to create
439 # $... the devices to use to create the mirrors on
440 # @uses: ZPOOL ZFS TESTPOOL
441 function setup_mirrors
443 typeset -i nmirrors=$1
446 while ((nmirrors > 0)); do
447 log_must test -n "$1" -a -n "$2"
448 [[ -d /$TESTPOOL$nmirrors ]] && rm -rf /$TESTPOOL$nmirrors
449 log_must zpool create -f $TESTPOOL$nmirrors mirror $1 $2
451 ((nmirrors = nmirrors - 1))
456 # create a number of raidz pools.
457 # We create a number($1) of 2 raidz pools using the pairs of disks named
458 # on the command line. These pools are *not* mounted
459 # @parameters: $1 the number of pools to create
460 # $... the devices to use to create the pools on
461 # @uses: ZPOOL ZFS TESTPOOL
462 function setup_raidzs
464 typeset -i nraidzs=$1
467 while ((nraidzs > 0)); do
468 log_must test -n "$1" -a -n "$2"
469 [[ -d /$TESTPOOL$nraidzs ]] && rm -rf /$TESTPOOL$nraidzs
470 log_must zpool create -f $TESTPOOL$nraidzs raidz $1 $2
472 ((nraidzs = nraidzs - 1))
477 # Destroy the configured testpool mirrors.
478 # the mirrors are of the form ${TESTPOOL}{number}
479 # @uses: ZPOOL ZFS TESTPOOL
480 function destroy_mirrors
482 default_cleanup_noexit
488 # Given a minimum of two disks, set up a storage pool and dataset for the raid-z
489 # $1 the list of disks
491 function default_raidz_setup
493 typeset disklist="$*"
494 disks=(${disklist[*]})
496 if [[ ${#disks[*]} -lt 2 ]]; then
497 log_fail "A raid-z requires a minimum of two disks."
500 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
501 log_must zpool create -f $TESTPOOL raidz $disklist
502 log_must zfs create $TESTPOOL/$TESTFS
503 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
509 # Common function used to cleanup storage pools and datasets.
511 # Invoked at the start of the test suite to ensure the system
512 # is in a known state, and also at the end of each set of
513 # sub-tests to ensure errors from one set of tests doesn't
514 # impact the execution of the next set.
516 function default_cleanup
518 default_cleanup_noexit
524 # Utility function used to list all available pool names.
526 # NOTE: $KEEP is a variable containing pool names, separated by a newline
527 # character, that must be excluded from the returned list.
529 function get_all_pools
531 zpool list -H -o name | grep -Fvx "$KEEP" | grep -v "$NO_POOLS"
534 function default_cleanup_noexit
538 # Destroying the pool will also destroy any
539 # filesystems it contains.
541 if is_global_zone; then
542 zfs unmount -a > /dev/null 2>&1
543 ALL_POOLS=$(get_all_pools)
544 # Here, we loop through the pools we're allowed to
545 # destroy, only destroying them if it's safe to do
547 while [ ! -z ${ALL_POOLS} ]
549 for pool in ${ALL_POOLS}
551 if safe_to_destroy_pool $pool ;
555 ALL_POOLS=$(get_all_pools)
562 for fs in $(zfs list -H -o name \
563 | grep "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
564 destroy_dataset "$fs" "-Rf"
567 # Need cleanup here to avoid garbage dir left.
568 for fs in $(zfs list -H -o name); do
569 [[ $fs == /$ZONE_POOL ]] && continue
570 [[ -d $fs ]] && log_must rm -rf $fs/*
574 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
577 for fs in $(zfs list -H -o name); do
578 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
579 log_must zfs set reservation=none $fs
580 log_must zfs set recordsize=128K $fs
581 log_must zfs set mountpoint=/$fs $fs
583 enc=$(get_prop encryption $fs)
584 if [[ $? -ne 0 ]] || [[ -z "$enc" ]] || \
585 [[ "$enc" == "off" ]]; then
586 log_must zfs set checksum=on $fs
588 log_must zfs set compression=off $fs
589 log_must zfs set atime=on $fs
590 log_must zfs set devices=off $fs
591 log_must zfs set exec=on $fs
592 log_must zfs set setuid=on $fs
593 log_must zfs set readonly=off $fs
594 log_must zfs set snapdir=hidden $fs
595 log_must zfs set aclmode=groupmask $fs
596 log_must zfs set aclinherit=secure $fs
601 [[ -d $TESTDIR ]] && \
602 log_must rm -rf $TESTDIR
605 if is_mpath_device $disk1; then
609 rm -f $TEST_BASE_DIR/{err,out}
614 # Common function used to cleanup storage pools, file systems
617 function default_container_cleanup
619 if ! is_global_zone; then
623 ismounted $TESTPOOL/$TESTCTR/$TESTFS1
625 log_must zfs unmount $TESTPOOL/$TESTCTR/$TESTFS1
627 destroy_dataset "$TESTPOOL/$TESTCTR/$TESTFS1" "-R"
628 destroy_dataset "$TESTPOOL/$TESTCTR" "-Rf"
630 [[ -e $TESTDIR1 ]] && \
631 log_must rm -rf $TESTDIR1 > /dev/null 2>&1
637 # Common function used to cleanup snapshot of file system or volume. Default to
638 # delete the file system's snapshot
642 function destroy_snapshot
644 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
646 if ! snapexists $snap; then
647 log_fail "'$snap' does not exist."
651 # For the sake of the value which come from 'get_prop' is not equal
652 # to the really mountpoint when the snapshot is unmounted. So, firstly
653 # check and make sure this snapshot's been mounted in current system.
656 if ismounted $snap; then
657 mtpt=$(get_prop mountpoint $snap)
659 log_fail "get_prop mountpoint $snap failed."
662 destroy_dataset "$snap"
663 [[ $mtpt != "" && -d $mtpt ]] && \
664 log_must rm -rf $mtpt
668 # Common function used to cleanup clone.
672 function destroy_clone
674 typeset clone=${1:-$TESTPOOL/$TESTCLONE}
676 if ! datasetexists $clone; then
677 log_fail "'$clone' does not existed."
680 # With the same reason in destroy_snapshot
682 if ismounted $clone; then
683 mtpt=$(get_prop mountpoint $clone)
685 log_fail "get_prop mountpoint $clone failed."
688 destroy_dataset "$clone"
689 [[ $mtpt != "" && -d $mtpt ]] && \
690 log_must rm -rf $mtpt
694 # Common function used to cleanup bookmark of file system or volume. Default
695 # to delete the file system's bookmark.
699 function destroy_bookmark
701 typeset bkmark=${1:-$TESTPOOL/$TESTFS#$TESTBKMARK}
703 if ! bkmarkexists $bkmark; then
704 log_fail "'$bkmarkp' does not existed."
707 destroy_dataset "$bkmark"
710 # Return 0 if a snapshot exists; $? otherwise
716 zfs list -H -t snapshot "$1" > /dev/null 2>&1
721 # Return 0 if a bookmark exists; $? otherwise
725 function bkmarkexists
727 zfs list -H -t bookmark "$1" > /dev/null 2>&1
732 # Set a property to a certain value on a dataset.
733 # Sets a property of the dataset to the value as passed in.
735 # $1 dataset who's property is being set
737 # $3 value to set property to
739 # 0 if the property could be set.
740 # non-zero otherwise.
743 function dataset_setprop
745 typeset fn=dataset_setprop
748 log_note "$fn: Insufficient parameters (need 3, had $#)"
752 output=$(zfs set $2=$3 $1 2>&1)
755 log_note "Setting property on $1 failed."
756 log_note "property $2=$3"
757 log_note "Return Code: $rv"
758 log_note "Output: $output"
765 # Assign suite defined dataset properties.
766 # This function is used to apply the suite's defined default set of
767 # properties to a dataset.
768 # @parameters: $1 dataset to use
769 # @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
771 # 0 if the dataset has been altered.
772 # 1 if no pool name was passed in.
773 # 2 if the dataset could not be found.
774 # 3 if the dataset could not have it's properties set.
776 function dataset_set_defaultproperties
780 [[ -z $dataset ]] && return 1
784 for confset in $(zfs list); do
785 if [[ $dataset = $confset ]]; then
790 [[ $found -eq 0 ]] && return 2
791 if [[ -n $COMPRESSION_PROP ]]; then
792 dataset_setprop $dataset compression $COMPRESSION_PROP || \
794 log_note "Compression set to '$COMPRESSION_PROP' on $dataset"
796 if [[ -n $CHECKSUM_PROP ]]; then
797 dataset_setprop $dataset checksum $CHECKSUM_PROP || \
799 log_note "Checksum set to '$CHECKSUM_PROP' on $dataset"
805 # Check a numeric assertion
806 # @parameter: $@ the assertion to check
807 # @output: big loud notice if assertion failed
812 (($@)) || log_fail "$@"
816 # Function to format partition size of a disk
817 # Given a disk cxtxdx reduces all partitions
820 function zero_partitions #<whole_disk_name>
826 DSK=$DEV_DSKDIR/$diskname
827 DSK=$(echo $DSK | sed -e "s|//|/|g")
828 log_must parted $DSK -s -- mklabel gpt
829 blockdev --rereadpt $DSK 2>/dev/null
832 for i in 0 1 3 4 5 6 7
834 log_must set_partition $i "" 0mb $diskname
842 # Given a slice, size and disk, this function
843 # formats the slice to the specified size.
844 # Size should be specified with units as per
845 # the `format` command requirements eg. 100mb 3gb
847 # NOTE: This entire interface is problematic for the Linux parted utilty
848 # which requires the end of the partition to be specified. It would be
849 # best to retire this interface and replace it with something more flexible.
850 # At the moment a best effort is made.
852 function set_partition #<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
854 typeset -i slicenum=$1
860 if [[ -z $size || -z $disk ]]; then
861 log_fail "The size or disk name is unspecified."
863 typeset size_mb=${size%%[mMgG]}
865 size_mb=${size_mb%%[mMgG][bB]}
866 if [[ ${size:1:1} == 'g' ]]; then
867 ((size_mb = size_mb * 1024))
870 # Create GPT partition table when setting slice 0 or
871 # when the device doesn't already contain a GPT label.
872 parted $DEV_DSKDIR/$disk -s -- print 1 >/dev/null
874 if [[ $slicenum -eq 0 || $ret_val -ne 0 ]]; then
875 parted $DEV_DSKDIR/$disk -s -- mklabel gpt
876 if [[ $? -ne 0 ]]; then
877 log_note "Failed to create GPT partition table on $disk"
882 # When no start is given align on the first cylinder.
883 if [[ -z "$start" ]]; then
887 # Determine the cylinder size for the device and using
888 # that calculate the end offset in cylinders.
889 typeset -i cly_size_kb=0
890 cly_size_kb=$(parted -m $DEV_DSKDIR/$disk -s -- \
891 unit cyl print | head -3 | tail -1 | \
892 awk -F '[:k.]' '{print $4}')
893 ((end = (size_mb * 1024 / cly_size_kb) + start))
895 parted $DEV_DSKDIR/$disk -s -- \
896 mkpart part$slicenum ${start}cyl ${end}cyl
897 if [[ $? -ne 0 ]]; then
898 log_note "Failed to create partition $slicenum on $disk"
902 blockdev --rereadpt $DEV_DSKDIR/$disk 2>/dev/null
905 if [[ -z $slicenum || -z $size || -z $disk ]]; then
906 log_fail "The slice, size or disk name is unspecified."
909 typeset format_file=/var/tmp/format_in.$$
911 echo "partition" >$format_file
912 echo "$slicenum" >> $format_file
913 echo "" >> $format_file
914 echo "" >> $format_file
915 echo "$start" >> $format_file
916 echo "$size" >> $format_file
917 echo "label" >> $format_file
918 echo "" >> $format_file
919 echo "q" >> $format_file
920 echo "q" >> $format_file
922 format -e -s -d $disk -f $format_file
927 if [[ $ret_val -ne 0 ]]; then
928 log_note "Unable to format $disk slice $slicenum to $size"
935 # Delete all partitions on all disks - this is specifically for the use of multipath
936 # devices which currently can only be used in the test suite as raw/un-partitioned
937 # devices (ie a zpool cannot be created on a whole mpath device that has partitions)
939 function delete_partitions
943 if [[ -z $DISK_ARRAY_NUM ]]; then
944 DISK_ARRAY_NUM=$(echo ${DISKS} | nawk '{print NF}')
946 if [[ -z $DISKSARRAY ]]; then
951 if (( $DISK_ARRAY_NUM == 1 )); then
952 while ((j < MAX_PARTITIONS)); do
953 parted $DEV_DSKDIR/$DISK -s rm $j \
955 if (( $? == 1 )); then
956 lsblk | egrep ${DISK}${SLICE_PREFIX}${j} > /dev/null
957 if (( $? == 1 )); then
958 log_note "Partitions for $DISK should be deleted"
960 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
964 lsblk | egrep ${DISK}${SLICE_PREFIX}${j} > /dev/null
965 if (( $? == 0 )); then
966 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
972 for disk in `echo $DISKSARRAY`; do
973 while ((j < MAX_PARTITIONS)); do
974 parted $DEV_DSKDIR/$disk -s rm $j > /dev/null 2>&1
975 if (( $? == 1 )); then
976 lsblk | egrep ${disk}${SLICE_PREFIX}${j} > /dev/null
977 if (( $? == 1 )); then
978 log_note "Partitions for $disk should be deleted"
980 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
984 lsblk | egrep ${disk}${SLICE_PREFIX}${j} > /dev/null
985 if (( $? == 0 )); then
986 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
999 # Get the end cyl of the given slice
1001 function get_endslice #<disk> <slice>
1005 if [[ -z $disk || -z $slice ]] ; then
1006 log_fail "The disk name or slice number is unspecified."
1010 endcyl=$(parted -s $DEV_DSKDIR/$disk -- unit cyl print | \
1011 grep "part${slice}" | \
1012 awk '{print $3}' | \
1014 ((endcyl = (endcyl + 1)))
1016 disk=${disk#/dev/dsk/}
1017 disk=${disk#/dev/rdsk/}
1021 ratio=$(prtvtoc /dev/rdsk/${disk}s2 | \
1022 grep "sectors\/cylinder" | \
1025 if ((ratio == 0)); then
1029 typeset -i endcyl=$(prtvtoc -h /dev/rdsk/${disk}s2 |
1030 nawk -v token="$slice" '{if ($1==token) print $6}')
1032 ((endcyl = (endcyl + 1) / ratio))
1040 # Given a size,disk and total slice number, this function formats the
1041 # disk slices from 0 to the total slice number with the same specified
1044 function partition_disk #<slice_size> <whole_disk_name> <total_slices>
1047 typeset slice_size=$1
1048 typeset disk_name=$2
1049 typeset total_slices=$3
1052 zero_partitions $disk_name
1053 while ((i < $total_slices)); do
1060 log_must set_partition $i "$cyl" $slice_size $disk_name
1061 cyl=$(get_endslice $disk_name $i)
1067 # This function continues to write to a filenum number of files into dirnum
1068 # number of directories until either file_write returns an error or the
1069 # maximum number of files per directory have been written.
1072 # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
1074 # Return value: 0 on success
1078 # destdir: is the directory where everything is to be created under
1079 # dirnum: the maximum number of subdirectories to use, -1 no limit
1080 # filenum: the maximum number of files per subdirectory
1081 # bytes: number of bytes to write
1082 # num_writes: numer of types to write out bytes
1083 # data: the data that will be written
1086 # file_fs /testdir 20 25 1024 256 0
1088 # Note: bytes * num_writes equals the size of the testfile
1090 function fill_fs # destdir dirnum filenum bytes num_writes data
1092 typeset destdir=${1:-$TESTDIR}
1093 typeset -i dirnum=${2:-50}
1094 typeset -i filenum=${3:-50}
1095 typeset -i bytes=${4:-8192}
1096 typeset -i num_writes=${5:-10240}
1097 typeset data=${6:-0}
1099 typeset -i odirnum=1
1100 typeset -i idirnum=0
1104 mkdir -p $destdir/$idirnum
1105 while (($odirnum > 0)); do
1106 if ((dirnum >= 0 && idirnum >= dirnum)); then
1110 file_write -o create -f $destdir/$idirnum/$TESTFILE.$fn \
1111 -b $bytes -c $num_writes -d $data
1113 if (($retval != 0)); then
1117 if (($fn >= $filenum)); then
1119 ((idirnum = idirnum + 1))
1120 mkdir -p $destdir/$idirnum
1129 # Simple function to get the specified property. If unable to
1130 # get the property then exits.
1132 # Note property is in 'parsable' format (-p)
1134 function get_prop # property dataset
1140 prop_val=$(zfs get -pH -o value $prop $dataset 2>/dev/null)
1141 if [[ $? -ne 0 ]]; then
1142 log_note "Unable to get $prop property for dataset " \
1152 # Simple function to get the specified property of pool. If unable to
1153 # get the property then exits.
1155 # Note property is in 'parsable' format (-p)
1157 function get_pool_prop # property pool
1163 if poolexists $pool ; then
1164 prop_val=$(zpool get -pH $prop $pool 2>/dev/null | tail -1 | \
1166 if [[ $? -ne 0 ]]; then
1167 log_note "Unable to get $prop property for pool " \
1172 log_note "Pool $pool not exists."
1180 # Return 0 if a pool exists; $? otherwise
1188 if [[ -z $pool ]]; then
1189 log_note "No pool name given."
1193 zpool get name "$pool" > /dev/null 2>&1
1197 # Return 0 if all the specified datasets exist; $? otherwise
1200 function datasetexists
1202 if (($# == 0)); then
1203 log_note "No dataset name given."
1207 while (($# > 0)); do
1208 zfs get name $1 > /dev/null 2>&1 || \
1216 # return 0 if none of the specified datasets exists, otherwise return 1.
1219 function datasetnonexists
1221 if (($# == 0)); then
1222 log_note "No dataset name given."
1226 while (($# > 0)); do
1227 zfs list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \
1235 function is_shared_impl
1241 for mtpt in `share | awk '{print $1}'` ; do
1242 if [[ $mtpt == $fs ]] ; then
1249 for mtpt in `share | awk '{print $2}'` ; do
1250 if [[ $mtpt == $fs ]] ; then
1255 typeset stat=$(svcs -H -o STA nfs/server:default)
1256 if [[ $stat != "ON" ]]; then
1257 log_note "Current nfs/server status: $stat"
1264 # Given a mountpoint, or a dataset name, determine if it is shared via NFS.
1266 # Returns 0 if shared, 1 otherwise.
1273 if [[ $fs != "/"* ]] ; then
1274 if datasetnonexists "$fs" ; then
1277 mtpt=$(get_prop mountpoint "$fs")
1279 none|legacy|-) return 1
1287 is_shared_impl "$fs"
1291 # Given a dataset name determine if it is shared via SMB.
1293 # Returns 0 if shared, 1 otherwise.
1295 function is_shared_smb
1300 if datasetnonexists "$fs" ; then
1303 fs=$(echo $fs | sed 's@/@_@g')
1307 for mtpt in `net usershare list | awk '{print $1}'` ; do
1308 if [[ $mtpt == $fs ]] ; then
1314 log_unsupported "Currently unsupported by the test framework"
1320 # Given a mountpoint, determine if it is not shared via NFS.
1322 # Returns 0 if not shared, 1 otherwise.
1329 if (($? == 0)); then
1337 # Given a dataset determine if it is not shared via SMB.
1339 # Returns 0 if not shared, 1 otherwise.
1341 function not_shared_smb
1346 if (($? == 0)); then
1354 # Helper function to unshare a mountpoint.
1356 function unshare_fs #fs
1360 is_shared $fs || is_shared_smb $fs
1361 if (($? == 0)); then
1362 log_must zfs unshare $fs
1369 # Helper function to share a NFS mountpoint.
1371 function share_nfs #fs
1377 if (($? != 0)); then
1378 log_must share "*:$fs"
1382 if (($? != 0)); then
1383 log_must share -F nfs $fs
1391 # Helper function to unshare a NFS mountpoint.
1393 function unshare_nfs #fs
1399 if (($? == 0)); then
1400 log_must unshare -u "*:$fs"
1404 if (($? == 0)); then
1405 log_must unshare -F nfs $fs
1413 # Helper function to show NFS shares.
1415 function showshares_nfs
1427 # Helper function to show SMB shares.
1429 function showshares_smb
1441 # Check NFS server status and trigger it online.
1443 function setup_nfs_server
1445 # Cannot share directory in non-global zone.
1447 if ! is_global_zone; then
1448 log_note "Cannot trigger NFS server by sharing in LZ."
1454 # Re-synchronize /var/lib/nfs/etab with /etc/exports and
1455 # /etc/exports.d./* to provide a clean test environment.
1459 log_note "NFS server must be started prior to running ZTS."
1463 typeset nfs_fmri="svc:/network/nfs/server:default"
1464 if [[ $(svcs -Ho STA $nfs_fmri) != "ON" ]]; then
1466 # Only really sharing operation can enable NFS server
1467 # to online permanently.
1469 typeset dummy=/tmp/dummy
1471 if [[ -d $dummy ]]; then
1472 log_must rm -rf $dummy
1475 log_must mkdir $dummy
1476 log_must share $dummy
1479 # Waiting for fmri's status to be the final status.
1480 # Otherwise, in transition, an asterisk (*) is appended for
1481 # instances, unshare will reverse status to 'DIS' again.
1483 # Waiting for 1's at least.
1487 while [[ timeout -ne 0 && $(svcs -Ho STA $nfs_fmri) == *'*' ]]
1494 log_must unshare $dummy
1495 log_must rm -rf $dummy
1498 log_note "Current NFS status: '$(svcs -Ho STA,FMRI $nfs_fmri)'"
1502 # To verify whether calling process is in global zone
1504 # Return 0 if in global zone, 1 in non-global zone
1506 function is_global_zone
1511 typeset cur_zone=$(zonename 2>/dev/null)
1512 if [[ $cur_zone != "global" ]]; then
1520 # Verify whether test is permitted to run from
1521 # global zone, local zone, or both
1523 # $1 zone limit, could be "global", "local", or "both"(no limit)
1525 # Return 0 if permitted, otherwise exit with log_unsupported
1527 function verify_runnable # zone limit
1531 [[ -z $limit ]] && return 0
1533 if is_global_zone ; then
1537 local) log_unsupported "Test is unable to run from "\
1540 *) log_note "Warning: unknown limit $limit - " \
1548 global) log_unsupported "Test is unable to run from "\
1551 *) log_note "Warning: unknown limit $limit - " \
1562 # Return 0 if create successfully or the pool exists; $? otherwise
1563 # Note: In local zones, this function should return 0 silently.
1566 # $2-n - [keyword] devs_list
1568 function create_pool #pool devs_list
1570 typeset pool=${1%%/*}
1574 if [[ -z $pool ]]; then
1575 log_note "Missing pool name."
1579 if poolexists $pool ; then
1583 if is_global_zone ; then
1584 [[ -d /$pool ]] && rm -rf /$pool
1585 log_must zpool create -f $pool $@
1591 # Return 0 if destroy successfully or the pool exists; $? otherwise
1592 # Note: In local zones, this function should return 0 silently.
1595 # Destroy pool with the given parameters.
1597 function destroy_pool #pool
1599 typeset pool=${1%%/*}
1602 if [[ -z $pool ]]; then
1603 log_note "No pool name given."
1607 if is_global_zone ; then
1608 if poolexists "$pool" ; then
1609 mtpt=$(get_prop mountpoint "$pool")
1611 # At times, syseventd/udev activity can cause attempts
1612 # to destroy a pool to fail with EBUSY. We retry a few
1613 # times allowing failures before requiring the destroy
1615 log_must_busy zpool destroy -f $pool
1618 log_must rm -rf $mtpt
1620 log_note "Pool does not exist. ($pool)"
1628 # Return 0 if created successfully; $? otherwise
1631 # $2-n - dataset options
1633 function create_dataset #dataset dataset_options
1639 if [[ -z $dataset ]]; then
1640 log_note "Missing dataset name."
1644 if datasetexists $dataset ; then
1645 destroy_dataset $dataset
1648 log_must zfs create $@ $dataset
1653 # Return 0 if destroy successfully or the dataset exists; $? otherwise
1654 # Note: In local zones, this function should return 0 silently.
1657 # $2 - custom arguments for zfs destroy
1658 # Destroy dataset with the given parameters.
1660 function destroy_dataset #dataset #args
1664 typeset args=${2:-""}
1666 if [[ -z $dataset ]]; then
1667 log_note "No dataset name given."
1671 if is_global_zone ; then
1672 if datasetexists "$dataset" ; then
1673 mtpt=$(get_prop mountpoint "$dataset")
1674 log_must_busy zfs destroy $args $dataset
1677 log_must rm -rf $mtpt
1679 log_note "Dataset does not exist. ($dataset)"
1688 # Firstly, create a pool with 5 datasets. Then, create a single zone and
1689 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
1690 # and a zvol device to the zone.
1693 # $2 zone root directory prefix
1696 function zfs_zones_setup #zone_name zone_root zone_ip
1698 typeset zone_name=${1:-$(hostname)-z}
1699 typeset zone_root=${2:-"/zone_root"}
1700 typeset zone_ip=${3:-"10.1.1.10"}
1701 typeset prefix_ctr=$ZONE_CTR
1702 typeset pool_name=$ZONE_POOL
1706 # Create pool and 5 container within it
1708 [[ -d /$pool_name ]] && rm -rf /$pool_name
1709 log_must zpool create -f $pool_name $DISKS
1710 while ((i < cntctr)); do
1711 log_must zfs create $pool_name/$prefix_ctr$i
1716 log_must zfs create -V 1g $pool_name/zone_zvol
1720 # If current system support slog, add slog device for pool
1722 if verify_slog_support ; then
1723 typeset sdevs="$TEST_BASE_DIR/sdev1 $TEST_BASE_DIR/sdev2"
1724 log_must mkfile $MINVDEVSIZE $sdevs
1725 log_must zpool add $pool_name log mirror $sdevs
1728 # this isn't supported just yet.
1729 # Create a filesystem. In order to add this to
1730 # the zone, it must have it's mountpoint set to 'legacy'
1731 # log_must zfs create $pool_name/zfs_filesystem
1732 # log_must zfs set mountpoint=legacy $pool_name/zfs_filesystem
1734 [[ -d $zone_root ]] && \
1735 log_must rm -rf $zone_root/$zone_name
1736 [[ ! -d $zone_root ]] && \
1737 log_must mkdir -p -m 0700 $zone_root/$zone_name
1739 # Create zone configure file and configure the zone
1741 typeset zone_conf=/tmp/zone_conf.$$
1742 echo "create" > $zone_conf
1743 echo "set zonepath=$zone_root/$zone_name" >> $zone_conf
1744 echo "set autoboot=true" >> $zone_conf
1746 while ((i < cntctr)); do
1747 echo "add dataset" >> $zone_conf
1748 echo "set name=$pool_name/$prefix_ctr$i" >> \
1750 echo "end" >> $zone_conf
1754 # add our zvol to the zone
1755 echo "add device" >> $zone_conf
1756 echo "set match=/dev/zvol/dsk/$pool_name/zone_zvol" >> $zone_conf
1757 echo "end" >> $zone_conf
1759 # add a corresponding zvol rdsk to the zone
1760 echo "add device" >> $zone_conf
1761 echo "set match=$ZVOL_RDEVDIR/$pool_name/zone_zvol" >> $zone_conf
1762 echo "end" >> $zone_conf
1764 # once it's supported, we'll add our filesystem to the zone
1765 # echo "add fs" >> $zone_conf
1766 # echo "set type=zfs" >> $zone_conf
1767 # echo "set special=$pool_name/zfs_filesystem" >> $zone_conf
1768 # echo "set dir=/export/zfs_filesystem" >> $zone_conf
1769 # echo "end" >> $zone_conf
1771 echo "verify" >> $zone_conf
1772 echo "commit" >> $zone_conf
1773 log_must zonecfg -z $zone_name -f $zone_conf
1774 log_must rm -f $zone_conf
1777 zoneadm -z $zone_name install
1778 if (($? == 0)); then
1779 log_note "SUCCESS: zoneadm -z $zone_name install"
1781 log_fail "FAIL: zoneadm -z $zone_name install"
1784 # Install sysidcfg file
1786 typeset sysidcfg=$zone_root/$zone_name/root/etc/sysidcfg
1787 echo "system_locale=C" > $sysidcfg
1788 echo "terminal=dtterm" >> $sysidcfg
1789 echo "network_interface=primary {" >> $sysidcfg
1790 echo "hostname=$zone_name" >> $sysidcfg
1791 echo "}" >> $sysidcfg
1792 echo "name_service=NONE" >> $sysidcfg
1793 echo "root_password=mo791xfZ/SFiw" >> $sysidcfg
1794 echo "security_policy=NONE" >> $sysidcfg
1795 echo "timezone=US/Eastern" >> $sysidcfg
1798 log_must zoneadm -z $zone_name boot
1802 # Reexport TESTPOOL & TESTPOOL(1-4)
1804 function reexport_pool
1809 while ((i < cntctr)); do
1811 TESTPOOL=$ZONE_POOL/$ZONE_CTR$i
1812 if ! ismounted $TESTPOOL; then
1813 log_must zfs mount $TESTPOOL
1816 eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i
1817 if eval ! ismounted \$TESTPOOL$i; then
1818 log_must eval zfs mount \$TESTPOOL$i
1826 # Verify a given disk or pool state
1828 # Return 0 is pool/disk matches expected state, 1 otherwise
1830 function check_state # pool disk state{online,offline,degraded}
1833 typeset disk=${2#$DEV_DSKDIR/}
1836 [[ -z $pool ]] || [[ -z $state ]] \
1837 && log_fail "Arguments invalid or missing"
1839 if [[ -z $disk ]]; then
1840 #check pool state only
1841 zpool get -H -o value health $pool \
1842 | grep -i "$state" > /dev/null 2>&1
1844 zpool status -v $pool | grep "$disk" \
1845 | grep -i "$state" > /dev/null 2>&1
1852 # Get the mountpoint of snapshot
1853 # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1856 function snapshot_mountpoint
1858 typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1860 if [[ $dataset != *@* ]]; then
1861 log_fail "Error name of snapshot '$dataset'."
1864 typeset fs=${dataset%@*}
1865 typeset snap=${dataset#*@}
1867 if [[ -z $fs || -z $snap ]]; then
1868 log_fail "Error name of snapshot '$dataset'."
1871 echo $(get_prop mountpoint $fs)/.zfs/snapshot/$snap
1875 # Given a device and 'ashift' value verify it's correctly set on every label
1877 function verify_ashift # device ashift
1882 zdb -e -lll $device | awk -v ashift=$ashift '/ashift: / {
1898 # Given a pool and file system, this function will verify the file system
1899 # using the zdb internal tool. Note that the pool is exported and imported
1900 # to ensure it has consistent state.
1902 function verify_filesys # pool filesystem dir
1905 typeset filesys="$2"
1906 typeset zdbout="/tmp/zdbout.$$"
1911 typeset search_path=""
1913 log_note "Calling zdb to verify filesystem '$filesys'"
1914 zfs unmount -a > /dev/null 2>&1
1915 log_must zpool export $pool
1917 if [[ -n $dirs ]] ; then
1918 for dir in $dirs ; do
1919 search_path="$search_path -d $dir"
1923 log_must zpool import $search_path $pool
1925 zdb -cudi $filesys > $zdbout 2>&1
1926 if [[ $? != 0 ]]; then
1927 log_note "Output: zdb -cudi $filesys"
1929 log_fail "zdb detected errors with: '$filesys'"
1932 log_must zfs mount -a
1933 log_must rm -rf $zdbout
1937 # Given a pool issue a scrub and verify that no checksum errors are reported.
1939 function verify_pool
1941 typeset pool=${1:-$TESTPOOL}
1943 log_must zpool scrub $pool
1944 log_must wait_scrubbed $pool
1946 cksum=$(zpool status $pool | awk 'L{print $NF;L=0} /CKSUM$/{L=1}')
1947 if [[ $cksum != 0 ]]; then
1948 log_must zpool status -v
1949 log_fail "Unexpected CKSUM errors found on $pool ($cksum)"
1954 # Given a pool, and this function list all disks in the pool
1956 function get_disklist # pool
1960 disklist=$(zpool iostat -v $1 | nawk '(NR >4) {print $1}' | \
1961 grep -v "\-\-\-\-\-" | \
1962 egrep -v -e "^(mirror|raidz[1-3]|spare|log|cache|special|dedup)$")
1968 # Given a pool, and this function list all disks in the pool with their full
1969 # path (like "/dev/sda" instead of "sda").
1971 function get_disklist_fullpath # pool
1980 # This function kills a given list of processes after a time period. We use
1981 # this in the stress tests instead of STF_TIMEOUT so that we can have processes
1982 # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1983 # would be listed as FAIL, which we don't want : we're happy with stress tests
1984 # running for a certain amount of time, then finishing.
1986 # @param $1 the time in seconds after which we should terminate these processes
1987 # @param $2..$n the processes we wish to terminate.
1989 function stress_timeout
1991 typeset -i TIMEOUT=$1
1995 log_note "Waiting for child processes($cpids). " \
1996 "It could last dozens of minutes, please be patient ..."
1997 log_must sleep $TIMEOUT
1999 log_note "Killing child processes after ${TIMEOUT} stress timeout."
2001 for pid in $cpids; do
2002 ps -p $pid > /dev/null 2>&1
2003 if (($? == 0)); then
2004 log_must kill -USR1 $pid
2010 # Verify a given hotspare disk is inuse or avail
2012 # Return 0 is pool/disk matches expected state, 1 otherwise
2014 function check_hotspare_state # pool disk state{inuse,avail}
2017 typeset disk=${2#$DEV_DSKDIR/}
2020 cur_state=$(get_device_state $pool $disk "spares")
2022 if [[ $state != ${cur_state} ]]; then
2029 # Wait until a hotspare transitions to a given state or times out.
2031 # Return 0 when pool/disk matches expected state, 1 on timeout.
2033 function wait_hotspare_state # pool disk state timeout
2036 typeset disk=${2#*$DEV_DSKDIR/}
2038 typeset timeout=${4:-60}
2041 while [[ $i -lt $timeout ]]; do
2042 if check_hotspare_state $pool $disk $state; then
2054 # Verify a given slog disk is inuse or avail
2056 # Return 0 is pool/disk matches expected state, 1 otherwise
2058 function check_slog_state # pool disk state{online,offline,unavail}
2061 typeset disk=${2#$DEV_DSKDIR/}
2064 cur_state=$(get_device_state $pool $disk "logs")
2066 if [[ $state != ${cur_state} ]]; then
2073 # Verify a given vdev disk is inuse or avail
2075 # Return 0 is pool/disk matches expected state, 1 otherwise
2077 function check_vdev_state # pool disk state{online,offline,unavail}
2080 typeset disk=${2#*$DEV_DSKDIR/}
2083 cur_state=$(get_device_state $pool $disk)
2085 if [[ $state != ${cur_state} ]]; then
2092 # Wait until a vdev transitions to a given state or times out.
2094 # Return 0 when pool/disk matches expected state, 1 on timeout.
2096 function wait_vdev_state # pool disk state timeout
2099 typeset disk=${2#*$DEV_DSKDIR/}
2101 typeset timeout=${4:-60}
2104 while [[ $i -lt $timeout ]]; do
2105 if check_vdev_state $pool $disk $state; then
2117 # Check the output of 'zpool status -v <pool>',
2118 # and to see if the content of <token> contain the <keyword> specified.
2120 # Return 0 is contain, 1 otherwise
2122 function check_pool_status # pool token keyword <verbose>
2127 typeset verbose=${4:-false}
2129 scan=$(zpool status -v "$pool" 2>/dev/null | nawk -v token="$token:" '
2130 ($1==token) {print $0}')
2131 if [[ $verbose == true ]]; then
2134 echo $scan | grep -i "$keyword" > /dev/null 2>&1
2140 # These 6 following functions are instance of check_pool_status()
2141 # is_pool_resilvering - to check if the pool is resilver in progress
2142 # is_pool_resilvered - to check if the pool is resilver completed
2143 # is_pool_scrubbing - to check if the pool is scrub in progress
2144 # is_pool_scrubbed - to check if the pool is scrub completed
2145 # is_pool_scrub_stopped - to check if the pool is scrub stopped
2146 # is_pool_scrub_paused - to check if the pool has scrub paused
2147 # is_pool_removing - to check if the pool is removing a vdev
2148 # is_pool_removed - to check if the pool is remove completed
2150 function is_pool_resilvering #pool <verbose>
2152 check_pool_status "$1" "scan" "resilver in progress since " $2
2156 function is_pool_resilvered #pool <verbose>
2158 check_pool_status "$1" "scan" "resilvered " $2
2162 function is_pool_scrubbing #pool <verbose>
2164 check_pool_status "$1" "scan" "scrub in progress since " $2
2168 function is_pool_scrubbed #pool <verbose>
2170 check_pool_status "$1" "scan" "scrub repaired" $2
2174 function is_pool_scrub_stopped #pool <verbose>
2176 check_pool_status "$1" "scan" "scrub canceled" $2
2180 function is_pool_scrub_paused #pool <verbose>
2182 check_pool_status "$1" "scan" "scrub paused since " $2
2186 function is_pool_removing #pool
2188 check_pool_status "$1" "remove" "in progress since "
2192 function is_pool_removed #pool
2194 check_pool_status "$1" "remove" "completed on"
2198 function wait_for_degraded
2201 typeset timeout=${2:-30}
2205 [[ $(get_pool_prop health $pool) == "DEGRADED" ]] && break
2206 log_note "$pool is not yet degraded."
2208 if ((SECONDS - t0 > $timeout)); then
2209 log_note "$pool not degraded after $timeout seconds."
2218 # Use create_pool()/destroy_pool() to clean up the information in
2219 # in the given disk to avoid slice overlapping.
2221 function cleanup_devices #vdevs
2223 typeset pool="foopool$$"
2225 if poolexists $pool ; then
2229 create_pool $pool $@
2236 # A function to find and locate free disks on a system or from given
2237 # disks as the parameter. It works by locating disks that are in use
2238 # as swap devices and dump devices, and also disks listed in /etc/vfstab
2240 # $@ given disks to find which are free, default is all disks in
2243 # @return a string containing the list of available disks
2247 # Trust provided list, no attempt is made to locate unused devices.
2254 sfi=/tmp/swaplist.$$
2255 dmpi=/tmp/dumpdev.$$
2256 max_finddisksnum=${MAX_FINDDISKSNUM:-6}
2259 dumpadm > $dmpi 2>/dev/null
2261 # write an awk script that can process the output of format
2262 # to produce a list of disks we know about. Note that we have
2263 # to escape "$2" so that the shell doesn't interpret it while
2264 # we're creating the awk script.
2265 # -------------------
2266 cat > /tmp/find_disks.awk <<EOF
2275 if (searchdisks && \$2 !~ "^$"){
2281 /^AVAILABLE DISK SELECTIONS:/{
2285 #---------------------
2287 chmod 755 /tmp/find_disks.awk
2288 disks=${@:-$(echo "" | format -e 2>/dev/null | /tmp/find_disks.awk)}
2289 rm /tmp/find_disks.awk
2292 for disk in $disks; do
2294 grep "${disk}[sp]" /etc/mnttab >/dev/null
2295 (($? == 0)) && continue
2297 grep "${disk}[sp]" $sfi >/dev/null
2298 (($? == 0)) && continue
2299 # check for dump device
2300 grep "${disk}[sp]" $dmpi >/dev/null
2301 (($? == 0)) && continue
2302 # check to see if this disk hasn't been explicitly excluded
2303 # by a user-set environment variable
2304 echo "${ZFS_HOST_DEVICES_IGNORE}" | grep "${disk}" > /dev/null
2305 (($? == 0)) && continue
2306 unused_candidates="$unused_candidates $disk"
2311 # now just check to see if those disks do actually exist
2312 # by looking for a device pointing to the first slice in
2313 # each case. limit the number to max_finddisksnum
2315 for disk in $unused_candidates; do
2316 if [ -b $DEV_DSKDIR/${disk}s0 ]; then
2317 if [ $count -lt $max_finddisksnum ]; then
2318 unused="$unused $disk"
2319 # do not impose limit if $@ is provided
2320 [[ -z $@ ]] && ((count = count + 1))
2325 # finally, return our disk list
2330 # Add specified user to specified group
2334 # $3 base of the homedir (optional)
2336 function add_user #<group_name> <user_name> <basedir>
2340 typeset basedir=${3:-"/var/tmp"}
2342 if ((${#gname} == 0 || ${#uname} == 0)); then
2343 log_fail "group name or user name are not defined."
2346 log_must useradd -g $gname -d $basedir/$uname -m $uname
2347 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.profile
2348 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.bash_profile
2349 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.login
2351 # Add new users to the same group and the command line utils.
2352 # This allows them to be run out of the original users home
2353 # directory as long as it permissioned to be group readable.
2355 cmd_group=$(stat --format="%G" $(which zfs))
2356 log_must usermod -a -G $cmd_group $uname
2363 # Delete the specified user.
2366 # $2 base of the homedir (optional)
2368 function del_user #<logname> <basedir>
2371 typeset basedir=${2:-"/var/tmp"}
2373 if ((${#user} == 0)); then
2374 log_fail "login name is necessary."
2377 if id $user > /dev/null 2>&1; then
2378 log_must_retry "currently used" 5 userdel $user
2381 [[ -d $basedir/$user ]] && rm -fr $basedir/$user
2387 # Select valid gid and create specified group.
2391 function add_group #<group_name>
2395 if ((${#group} == 0)); then
2396 log_fail "group name is necessary."
2399 # Assign 100 as the base gid, a larger value is selected for
2400 # Linux because for many distributions 1000 and under are reserved.
2403 groupadd $group > /dev/null 2>&1
2413 groupadd -g $gid $group > /dev/null 2>&1
2417 # The gid is not unique
2426 # Delete the specified group.
2430 function del_group #<group_name>
2433 if ((${#grp} == 0)); then
2434 log_fail "group name is necessary."
2438 getent group $grp > /dev/null 2>&1
2441 # Group does not exist.
2443 # Name already exists as a group name
2444 0) log_must groupdel $grp ;;
2448 groupmod -n $grp $grp > /dev/null 2>&1
2451 # Group does not exist.
2453 # Name already exists as a group name
2454 9) log_must groupdel $grp ;;
2463 # This function will return true if it's safe to destroy the pool passed
2464 # as argument 1. It checks for pools based on zvols and files, and also
2465 # files contained in a pool that may have a different mountpoint.
2467 function safe_to_destroy_pool { # $1 the pool name
2470 typeset DONT_DESTROY=""
2472 # We check that by deleting the $1 pool, we're not
2473 # going to pull the rug out from other pools. Do this
2474 # by looking at all other pools, ensuring that they
2475 # aren't built from files or zvols contained in this pool.
2477 for pool in $(zpool list -H -o name)
2481 # this is a list of the top-level directories in each of the
2482 # files that make up the path to the files the pool is based on
2483 FILEPOOL=$(zpool status -v $pool | grep /$1/ | \
2486 # this is a list of the zvols that make up the pool
2487 ZVOLPOOL=$(zpool status -v $pool | grep "$ZVOL_DEVDIR/$1$" \
2490 # also want to determine if it's a file-based pool using an
2491 # alternate mountpoint...
2492 POOL_FILE_DIRS=$(zpool status -v $pool | \
2493 grep / | awk '{print $1}' | \
2494 awk -F/ '{print $2}' | grep -v "dev")
2496 for pooldir in $POOL_FILE_DIRS
2498 OUTPUT=$(zfs list -H -r -o mountpoint $1 | \
2499 grep "${pooldir}$" | awk '{print $1}')
2501 ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
2505 if [ ! -z "$ZVOLPOOL" ]
2508 log_note "Pool $pool is built from $ZVOLPOOL on $1"
2511 if [ ! -z "$FILEPOOL" ]
2514 log_note "Pool $pool is built from $FILEPOOL on $1"
2517 if [ ! -z "$ALTMOUNTPOOL" ]
2520 log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
2524 if [ -z "${DONT_DESTROY}" ]
2528 log_note "Warning: it is not safe to destroy $1!"
2534 # Get the available ZFS compression options
2535 # $1 option type zfs_set|zfs_compress
2537 function get_compress_opts
2539 typeset COMPRESS_OPTS
2540 typeset GZIP_OPTS="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2541 gzip-6 gzip-7 gzip-8 gzip-9"
2543 if [[ $1 == "zfs_compress" ]] ; then
2544 COMPRESS_OPTS="on lzjb"
2545 elif [[ $1 == "zfs_set" ]] ; then
2546 COMPRESS_OPTS="on off lzjb"
2548 typeset valid_opts="$COMPRESS_OPTS"
2549 zfs get 2>&1 | grep gzip >/dev/null 2>&1
2550 if [[ $? -eq 0 ]]; then
2551 valid_opts="$valid_opts $GZIP_OPTS"
2557 # Verify zfs operation with -p option work as expected
2558 # $1 operation, value could be create, clone or rename
2559 # $2 dataset type, value could be fs or vol
2561 # $4 new dataset name
2563 function verify_opt_p_ops
2568 typeset newdataset=$4
2570 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2571 log_fail "$datatype is not supported."
2574 # check parameters accordingly
2579 if [[ $datatype == "vol" ]]; then
2580 ops="create -V $VOLSIZE"
2584 if [[ -z $newdataset ]]; then
2585 log_fail "newdataset should not be empty" \
2588 log_must datasetexists $dataset
2589 log_must snapexists $dataset
2592 if [[ -z $newdataset ]]; then
2593 log_fail "newdataset should not be empty" \
2596 log_must datasetexists $dataset
2597 log_mustnot snapexists $dataset
2600 log_fail "$ops is not supported."
2604 # make sure the upper level filesystem does not exist
2605 destroy_dataset "${newdataset%/*}" "-rRf"
2607 # without -p option, operation will fail
2608 log_mustnot zfs $ops $dataset $newdataset
2609 log_mustnot datasetexists $newdataset ${newdataset%/*}
2611 # with -p option, operation should succeed
2612 log_must zfs $ops -p $dataset $newdataset
2615 if ! datasetexists $newdataset ; then
2616 log_fail "-p option does not work for $ops"
2619 # when $ops is create or clone, redo the operation still return zero
2620 if [[ $ops != "rename" ]]; then
2621 log_must zfs $ops -p $dataset $newdataset
2628 # Get configuration of pool
2638 if ! poolexists "$pool" ; then
2641 alt_root=$(zpool list -H $pool | awk '{print $NF}')
2642 if [[ $alt_root == "-" ]]; then
2643 value=$(zdb -C $pool | grep "$config:" | awk -F: \
2646 value=$(zdb -e $pool | grep "$config:" | awk -F: \
2649 if [[ -n $value ]] ; then
2659 # Privated function. Random select one of items from arguments.
2664 function _random_get
2671 ((ind = RANDOM % cnt + 1))
2673 typeset ret=$(echo "$str" | cut -f $ind -d ' ')
2678 # Random select one of item from arguments which include NONE string
2680 function random_get_with_non
2685 _random_get "$cnt" "$@"
2689 # Random select one of item from arguments which doesn't include NONE string
2693 _random_get "$#" "$@"
2697 # Detect if the current system support slog
2699 function verify_slog_support
2701 typeset dir=$TEST_BASE_DIR/disk.$$
2707 mkfile $MINVDEVSIZE $vdev $sdev
2710 if ! zpool create -n $pool $vdev log $sdev > /dev/null 2>&1; then
2719 # The function will generate a dataset name with specific length
2720 # $1, the length of the name
2721 # $2, the base string to construct the name
2723 function gen_dataset_name
2726 typeset basestr="$2"
2727 typeset -i baselen=${#basestr}
2731 if ((len % baselen == 0)); then
2732 ((iter = len / baselen))
2734 ((iter = len / baselen + 1))
2736 while ((iter > 0)); do
2737 l_name="${l_name}$basestr"
2746 # Get cksum tuple of dataset
2749 # sample zdb output:
2750 # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2751 # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2752 # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2753 # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2754 function datasetcksum
2758 cksum=$(zdb -vvv $1 | grep "^Dataset $1 \[" | grep "cksum" \
2759 | awk -F= '{print $7}')
2770 cksum=$(cksum $1 | awk '{print $1}')
2775 # Get the given disk/slice state from the specific field of the pool
2777 function get_device_state #pool disk field("", "spares","logs")
2780 typeset disk=${2#$DEV_DSKDIR/}
2781 typeset field=${3:-$pool}
2783 state=$(zpool status -v "$pool" 2>/dev/null | \
2784 nawk -v device=$disk -v pool=$pool -v field=$field \
2785 'BEGIN {startconfig=0; startfield=0; }
2786 /config:/ {startconfig=1}
2787 (startconfig==1) && ($1==field) {startfield=1; next;}
2788 (startfield==1) && ($1==device) {print $2; exit;}
2790 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2796 # print the given directory filesystem type
2804 if [[ -z $dir ]]; then
2805 log_fail "Usage: get_fstype <directory>"
2812 df -n $dir | awk '{print $3}'
2816 # Given a disk, label it to VTOC regardless what label was on the disk
2822 if [[ -z $disk ]]; then
2823 log_fail "The disk name is unspecified."
2825 typeset label_file=/var/tmp/labelvtoc.$$
2826 typeset arch=$(uname -p)
2829 log_note "Currently unsupported by the test framework"
2833 if [[ $arch == "i386" ]]; then
2834 echo "label" > $label_file
2835 echo "0" >> $label_file
2836 echo "" >> $label_file
2837 echo "q" >> $label_file
2838 echo "q" >> $label_file
2840 fdisk -B $disk >/dev/null 2>&1
2841 # wait a while for fdisk finishes
2843 elif [[ $arch == "sparc" ]]; then
2844 echo "label" > $label_file
2845 echo "0" >> $label_file
2846 echo "" >> $label_file
2847 echo "" >> $label_file
2848 echo "" >> $label_file
2849 echo "q" >> $label_file
2851 log_fail "unknown arch type"
2854 format -e -s -d $disk -f $label_file
2855 typeset -i ret_val=$?
2858 # wait the format to finish
2861 if ((ret_val != 0)); then
2862 log_fail "unable to label $disk as VTOC."
2869 # check if the system was installed as zfsroot or not
2870 # return: 0 ture, otherwise false
2874 df -n / | grep zfs > /dev/null 2>&1
2879 # get the root filesystem name if it's zfsroot system.
2881 # return: root filesystem name
2887 rootfs=$(awk '{if ($2 == "/" && $3 == "zfs") print $1}' \
2890 if [[ -z "$rootfs" ]]; then
2891 log_fail "Can not get rootfs"
2893 zfs list $rootfs > /dev/null 2>&1
2894 if (($? == 0)); then
2897 log_fail "This is not a zfsroot system."
2902 # get the rootfs's pool name
2906 function get_rootpool
2912 rootfs=$(awk '{if ($2 == "/" && $3 =="zfs") print $1}' \
2915 if [[ -z "$rootfs" ]]; then
2916 log_fail "Can not get rootpool"
2918 zfs list $rootfs > /dev/null 2>&1
2919 if (($? == 0)); then
2920 rootpool=`echo $rootfs | awk -F\/ '{print $1}'`
2923 log_fail "This is not a zfsroot system."
2928 # Get the package name
2930 function get_package_name
2932 typeset dirpath=${1:-$STC_NAME}
2934 echo "SUNWstc-${dirpath}" | /usr/bin/sed -e "s/\//-/g"
2938 # Get the word numbers from a string separated by white space
2940 function get_word_count
2946 # To verify if the require numbers of disks is given
2948 function verify_disk_count
2950 typeset -i min=${2:-1}
2952 typeset -i count=$(get_word_count "$1")
2954 if ((count < min)); then
2955 log_untested "A minimum of $min disks is required to run." \
2956 " You specified $count disk(s)"
2960 function ds_is_volume
2962 typeset type=$(get_prop type $1)
2963 [[ $type = "volume" ]] && return 0
2967 function ds_is_filesystem
2969 typeset type=$(get_prop type $1)
2970 [[ $type = "filesystem" ]] && return 0
2974 function ds_is_snapshot
2976 typeset type=$(get_prop type $1)
2977 [[ $type = "snapshot" ]] && return 0
2982 # Check if Trusted Extensions are installed and enabled
2984 function is_te_enabled
2986 svcs -H -o state labeld 2>/dev/null | grep "enabled"
2987 if (($? != 0)); then
2994 # Utility function to determine if a system has multiple cpus.
3000 (($(psrinfo | wc -l) > 1))
3006 function get_cpu_freq
3009 lscpu | awk '/CPU MHz/ { print $3 }'
3011 psrinfo -v 0 | awk '/processor operates at/ {print $6}'
3015 # Run the given command as the user provided.
3021 log_note "user:$user $@"
3022 eval su - \$user -c \"$@\" > $TEST_BASE_DIR/out 2>$TEST_BASE_DIR/err
3027 # Check if the pool contains the specified vdevs
3032 # Return 0 if the vdevs are contained in the pool, 1 if any of the specified
3033 # vdevs is not in the pool, and 2 if pool name is missing.
3035 function vdevs_in_pool
3040 if [[ -z $pool ]]; then
3041 log_note "Missing pool name."
3047 # We could use 'zpool list' to only get the vdevs of the pool but we
3048 # can't reference a mirror/raidz vdev using its ID (i.e mirror-0),
3049 # therefore we use the 'zpool status' output.
3050 typeset tmpfile=$(mktemp)
3051 zpool status -v "$pool" | grep -A 1000 "config:" >$tmpfile
3053 grep -w ${vdev##*/} $tmpfile >/dev/null 2>&1
3054 [[ $? -ne 0 ]] && return 1
3068 max=$(echo $((max > i ? max : i)))
3080 min=$(echo $((min < i ? min : i)))
3087 # Generate a random number between 1 and the argument.
3092 echo $(( ($RANDOM % $max) + 1 ))
3095 # Write data that can be compressed into a directory
3096 function write_compressible
3100 typeset nfiles=${3:-1}
3101 typeset bs=${4:-1024k}
3102 typeset fname=${5:-file}
3104 [[ -d $dir ]] || log_fail "No directory: $dir"
3106 # Under Linux fio is not currently used since its behavior can
3107 # differ significantly across versions. This includes missing
3108 # command line options and cases where the --buffer_compress_*
3109 # options fail to behave as expected.
3111 typeset file_bytes=$(to_bytes $megs)
3112 typeset bs_bytes=4096
3113 typeset blocks=$(($file_bytes / $bs_bytes))
3115 for (( i = 0; i < $nfiles; i++ )); do
3116 truncate -s $file_bytes $dir/$fname.$i
3118 # Write every third block to get 66% compression.
3119 for (( j = 0; j < $blocks; j += 3 )); do
3120 dd if=/dev/urandom of=$dir/$fname.$i \
3121 seek=$j bs=$bs_bytes count=1 \
3122 conv=notrunc >/dev/null 2>&1
3126 log_must eval "fio \
3131 --buffer_compress_percentage=66 \
3132 --buffer_compress_chunk=4096 \
3139 --filename_format='$fname.\$jobnum' >/dev/null"
3148 [[ -e $pathname ]] || log_fail "No such file or directory: $pathname"
3149 objnum=$(stat -c %i $pathname)
3154 # Sync data to the pool
3157 # $2 boolean to force uberblock (and config including zpool cache file) update
3159 function sync_pool #pool <force>
3161 typeset pool=${1:-$TESTPOOL}
3162 typeset force=${2:-false}
3164 if [[ $force == true ]]; then
3165 log_must zpool sync -f $pool
3167 log_must zpool sync $pool
3174 # Wait for zpool 'freeing' property drops to zero.
3178 function wait_freeing #pool
3180 typeset pool=${1:-$TESTPOOL}
3182 [[ "0" == "$(zpool list -Ho freeing $pool)" ]] && break
3188 # Wait for every device replace operation to complete
3192 function wait_replacing #pool
3194 typeset pool=${1:-$TESTPOOL}
3196 [[ "" == "$(zpool status $pool |
3197 awk '/replacing-[0-9]+/ {print $1}')" ]] && break
3203 # Wait for a pool to be scrubbed
3206 # $2 number of seconds to wait (optional)
3208 # Returns true when pool has been scrubbed, or false if there's a timeout or if
3209 # no scrub was done.
3211 function wait_scrubbed
3213 typeset pool=${1:-$TESTPOOL}
3215 is_pool_scrubbed $pool && break
3220 # Backup the zed.rc in our test directory so that we can edit it for our test.
3222 # Returns: Backup file name. You will need to pass this to zed_rc_restore().
3223 function zed_rc_backup
3225 zedrc_backup="$(mktemp)"
3226 cp $ZEDLET_DIR/zed.rc $zedrc_backup
3230 function zed_rc_restore
3232 mv $1 $ZEDLET_DIR/zed.rc
3236 # Setup custom environment for the ZED.
3238 # $@ Optional list of zedlets to run under zed.
3245 if [[ ! -d $ZEDLET_DIR ]]; then
3246 log_must mkdir $ZEDLET_DIR
3249 if [[ ! -e $VDEVID_CONF ]]; then
3250 log_must touch $VDEVID_CONF
3253 if [[ -e $VDEVID_CONF_ETC ]]; then
3254 log_fail "Must not have $VDEVID_CONF_ETC file present on system"
3258 # Create a symlink for /etc/zfs/vdev_id.conf file.
3259 log_must ln -s $VDEVID_CONF $VDEVID_CONF_ETC
3261 # Setup minimal ZED configuration. Individual test cases should
3262 # add additional ZEDLETs as needed for their specific test.
3263 log_must cp ${ZEDLET_ETC_DIR}/zed.rc $ZEDLET_DIR
3264 log_must cp ${ZEDLET_ETC_DIR}/zed-functions.sh $ZEDLET_DIR
3266 # Scripts must only be user writable.
3267 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3268 saved_umask=$(umask)
3270 for i in $EXTRA_ZEDLETS ; do
3271 log_must cp ${ZEDLET_LIBEXEC_DIR}/$i $ZEDLET_DIR
3273 log_must umask $saved_umask
3276 # Customize the zed.rc file to enable the full debug log.
3277 log_must sed -i '/\#ZED_DEBUG_LOG=.*/d' $ZEDLET_DIR/zed.rc
3278 echo "ZED_DEBUG_LOG=$ZED_DEBUG_LOG" >>$ZEDLET_DIR/zed.rc
3283 # Cleanup custom ZED environment.
3285 # $@ Optional list of zedlets to remove from our test zed.d directory.
3286 function zed_cleanup
3293 log_must rm -f ${ZEDLET_DIR}/zed.rc
3294 log_must rm -f ${ZEDLET_DIR}/zed-functions.sh
3295 log_must rm -f ${ZEDLET_DIR}/all-syslog.sh
3296 log_must rm -f ${ZEDLET_DIR}/all-debug.sh
3297 log_must rm -f ${ZEDLET_DIR}/state
3299 if [[ -n "$EXTRA_ZEDLETS" ]] ; then
3300 for i in $EXTRA_ZEDLETS ; do
3301 log_must rm -f ${ZEDLET_DIR}/$i
3304 log_must rm -f $ZED_LOG
3305 log_must rm -f $ZED_DEBUG_LOG
3306 log_must rm -f $VDEVID_CONF_ETC
3307 log_must rm -f $VDEVID_CONF
3312 # Check if ZED is currently running, if not start ZED.
3320 # ZEDLET_DIR=/var/tmp/zed
3321 if [[ ! -d $ZEDLET_DIR ]]; then
3322 log_must mkdir $ZEDLET_DIR
3325 # Verify the ZED is not already running.
3326 pgrep -x zed > /dev/null
3327 if (($? == 0)); then
3328 log_fail "ZED already running"
3331 log_note "Starting ZED"
3332 # run ZED in the background and redirect foreground logging
3333 # output to $ZED_LOG.
3334 log_must truncate -s 0 $ZED_DEBUG_LOG
3335 log_must eval "zed -vF -d $ZEDLET_DIR -p $ZEDLET_DIR/zed.pid -P $PATH" \
3336 "-s $ZEDLET_DIR/state 2>$ZED_LOG &"
3350 log_note "Stopping ZED"
3351 if [[ -f ${ZEDLET_DIR}/zed.pid ]]; then
3352 zedpid=$(<${ZEDLET_DIR}/zed.pid)
3354 while ps -p $zedpid > /dev/null; do
3357 rm -f ${ZEDLET_DIR}/zed.pid
3365 function zed_events_drain
3367 while [ $(zpool events -H | wc -l) -ne 0 ]; do
3369 zpool events -c >/dev/null
3373 # Set a variable in zed.rc to something, un-commenting it in the process.
3383 eval sed -i $cmd $ZEDLET_DIR/zed.rc
3386 echo "$var=$val" >> $ZEDLET_DIR/zed.rc
3391 # Check is provided device is being active used as a swap device.
3393 function is_swap_inuse
3397 if [[ -z $device ]] ; then
3398 log_note "No device specified."
3403 swapon -s | grep -w $(readlink -f $device) > /dev/null 2>&1
3405 swap -l | grep -w $device > /dev/null 2>&1
3412 # Setup a swap device using the provided device.
3419 log_must eval "mkswap $swapdev > /dev/null 2>&1"
3420 log_must swapon $swapdev
3422 log_must swap -a $swapdev
3429 # Cleanup a swap device on the provided device.
3431 function swap_cleanup
3435 if is_swap_inuse $swapdev; then
3437 log_must swapoff $swapdev
3439 log_must swap -d $swapdev
3447 # Set a global system tunable (64-bit value)
3452 function set_tunable64
3454 set_tunable_impl "$1" "$2" Z
3458 # Set a global system tunable (32-bit value)
3463 function set_tunable32
3465 set_tunable_impl "$1" "$2" W
3468 function set_tunable_impl
3470 typeset tunable="$1"
3472 typeset mdb_cmd="$3"
3473 typeset module="${4:-zfs}"
3475 [[ -z "$tunable" ]] && return 1
3476 [[ -z "$value" ]] && return 1
3477 [[ -z "$mdb_cmd" ]] && return 1
3481 typeset zfs_tunables="/sys/module/$module/parameters"
3482 [[ -w "$zfs_tunables/$tunable" ]] || return 1
3483 echo -n "$value" > "$zfs_tunables/$tunable"
3487 [[ "$module" -eq "zfs" ]] || return 1
3488 echo "${tunable}/${mdb_cmd}0t${value}" | mdb -kw
3495 # Get a global system tunable
3499 function get_tunable
3501 get_tunable_impl "$1"
3504 function get_tunable_impl
3506 typeset tunable="$1"
3507 typeset module="${2:-zfs}"
3509 [[ -z "$tunable" ]] && return 1
3513 typeset zfs_tunables="/sys/module/$module/parameters"
3514 [[ -f "$zfs_tunables/$tunable" ]] || return 1
3515 cat $zfs_tunables/$tunable
3519 [[ "$module" -eq "zfs" ]] || return 1
3527 # Prints the current time in seconds since UNIX Epoch.
3529 function current_epoch
3535 # Get decimal value of global uint32_t variable using mdb.
3537 function mdb_get_uint32
3542 value=$(mdb -k -e "$variable/X | ::eval .=U")
3543 if [[ $? -ne 0 ]]; then
3544 log_fail "Failed to get value of '$variable' from mdb."
3553 # Set global uint32_t variable to a decimal value using mdb.
3555 function mdb_set_uint32
3560 mdb -kw -e "$variable/W 0t$value" > /dev/null
3561 if [[ $? -ne 0 ]]; then
3562 echo "Failed to set '$variable' to '$value' in mdb."
3570 # Set global scalar integer variable to a hex value using mdb.
3571 # Note: Target should have CTF data loaded.
3573 function mdb_ctf_set_int
3578 mdb -kw -e "$variable/z $value" > /dev/null
3579 if [[ $? -ne 0 ]]; then
3580 echo "Failed to set '$variable' to '$value' in mdb."