5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
24 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
29 # Copyright (c) 2012, 2015 by Delphix. All rights reserved.
32 .
${STF_TOOLS}/include
/logapi.shlib
34 # Determine if this is a Linux test system
36 # Return 0 if platform Linux, 1 if otherwise
40 if [[ $
($UNAME -o) == "GNU/Linux" ]]; then
47 # Determine if this is a 32-bit system
49 # Return 0 if platform is 32-bit, 1 if otherwise
53 if [[ $
(getconf LONG_BIT
) == "32" ]]; then
60 # Determine if kmemleak is enabled
62 # Return 0 if kmemleak is enabled, 1 if otherwise
66 if is_linux
&& [[ -e /sys
/kernel
/debug
/kmemleak
]]; then
73 # Determine whether a dataset is mounted
76 # $2 filesystem type; optional - defaulted to zfs
78 # Return 0 if dataset is mounted; 1 if unmounted; 2 on error
83 [[ -z $fstype ]] && fstype
=zfs
84 typeset out dir name ret
88 if [[ "$1" == "/"* ]] ; then
89 for out
in $
($ZFS mount |
$AWK '{print $2}'); do
90 [[ $1 == $out ]] && return 0
93 for out
in $
($ZFS mount |
$AWK '{print $1}'); do
94 [[ $1 == $out ]] && return 0
99 out
=$
($DF -F $fstype $1 2>/dev
/null
)
101 (($ret != 0)) && return $ret
109 [[ "$1" == "$dir" ||
"$1" == "$name" ]] && return 0
112 out
=$
($DF -t $fstype $1 2>/dev
/null
)
116 if [[ -L "$ZVOL_DEVDIR/$1" ]]; then
117 link
=$
(readlink
-f $ZVOL_DEVDIR/$1)
118 [[ -n "$link" ]] && \
119 $MOUNT |
$GREP -q "^$link" && \
128 # Return 0 if a dataset is mounted; 1 otherwise
131 # $2 filesystem type; optional - defaulted to zfs
136 (($?
== 0)) && return 0
140 # Return 0 if a dataset is unmounted; 1 otherwise
143 # $2 filesystem type; optional - defaulted to zfs
148 (($?
== 1)) && return 0
158 $ECHO $1 |
$SED "s/,/ /g"
161 function default_setup
163 default_setup_noexit
"$@"
169 # Given a list of disks, setup storage pools and datasets.
171 function default_setup_noexit
176 log_note begin default_setup_noexit
178 if is_global_zone
; then
179 if poolexists
$TESTPOOL ; then
180 destroy_pool
$TESTPOOL
182 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
183 log_note creating pool
$TESTPOOL $disklist
184 log_must
$ZPOOL create
-f $TESTPOOL $disklist
189 $RM -rf $TESTDIR || log_unresolved Could not remove
$TESTDIR
190 $MKDIR -p $TESTDIR || log_unresolved Could not create
$TESTDIR
192 log_must
$ZFS create
$TESTPOOL/$TESTFS
193 log_must
$ZFS set mountpoint
=$TESTDIR $TESTPOOL/$TESTFS
195 if [[ -n $container ]]; then
196 $RM -rf $TESTDIR1 || \
197 log_unresolved Could not remove
$TESTDIR1
198 $MKDIR -p $TESTDIR1 || \
199 log_unresolved Could not create
$TESTDIR1
201 log_must
$ZFS create
$TESTPOOL/$TESTCTR
202 log_must
$ZFS set canmount
=off
$TESTPOOL/$TESTCTR
203 log_must
$ZFS create
$TESTPOOL/$TESTCTR/$TESTFS1
204 log_must
$ZFS set mountpoint
=$TESTDIR1 \
205 $TESTPOOL/$TESTCTR/$TESTFS1
208 if [[ -n $volume ]]; then
209 if is_global_zone
; then
210 log_must
$ZFS create
-V $VOLSIZE $TESTPOOL/$TESTVOL
213 log_must
$ZFS create
$TESTPOOL/$TESTVOL
219 # Given a list of disks, setup a storage pool, file system and
222 function default_container_setup
226 default_setup
"$disklist" "true"
230 # Given a list of disks, setup a storage pool,file system
233 function default_volume_setup
237 default_setup
"$disklist" "" "true"
241 # Given a list of disks, setup a storage pool,file system,
242 # a container and a volume.
244 function default_container_volume_setup
248 default_setup
"$disklist" "true" "true"
252 # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
255 # $1 Existing filesystem or volume name. Default, $TESTFS
256 # $2 snapshot name. Default, $TESTSNAP
258 function create_snapshot
260 typeset fs_vol
=${1:-$TESTFS}
261 typeset snap
=${2:-$TESTSNAP}
263 [[ -z $fs_vol ]] && log_fail
"Filesystem or volume's name is undefined."
264 [[ -z $snap ]] && log_fail
"Snapshot's name is undefined."
266 if snapexists
$fs_vol@
$snap; then
267 log_fail
"$fs_vol@$snap already exists."
269 datasetexists
$fs_vol || \
270 log_fail
"$fs_vol must exist."
272 log_must
$ZFS snapshot
$fs_vol@
$snap
276 # Create a clone from a snapshot, default clone name is $TESTCLONE.
278 # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
279 # $2 Clone name, $TESTPOOL/$TESTCLONE is default.
281 function create_clone
# snapshot clone
283 typeset snap
=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
284 typeset clone
=${2:-$TESTPOOL/$TESTCLONE}
287 log_fail
"Snapshot name is undefined."
289 log_fail
"Clone name is undefined."
291 log_must
$ZFS clone
$snap $clone
294 function default_mirror_setup
296 default_mirror_setup_noexit
$1 $2 $3
302 # Given a pair of disks, set up a storage pool and dataset for the mirror
303 # @parameters: $1 the primary side of the mirror
304 # $2 the secondary side of the mirror
305 # @uses: ZPOOL ZFS TESTPOOL TESTFS
306 function default_mirror_setup_noexit
308 readonly func
="default_mirror_setup_noexit"
312 [[ -z $primary ]] && \
313 log_fail
"$func: No parameters passed"
314 [[ -z $secondary ]] && \
315 log_fail
"$func: No secondary partition passed"
316 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
317 log_must
$ZPOOL create
-f $TESTPOOL mirror $@
318 log_must
$ZFS create
$TESTPOOL/$TESTFS
319 log_must
$ZFS set mountpoint
=$TESTDIR $TESTPOOL/$TESTFS
323 # create a number of mirrors.
324 # We create a number($1) of 2 way mirrors using the pairs of disks named
325 # on the command line. These mirrors are *not* mounted
326 # @parameters: $1 the number of mirrors to create
327 # $... the devices to use to create the mirrors on
328 # @uses: ZPOOL ZFS TESTPOOL
329 function setup_mirrors
331 typeset
-i nmirrors
=$1
334 while ((nmirrors
> 0)); do
335 log_must
test -n "$1" -a -n "$2"
336 [[ -d /$TESTPOOL$nmirrors ]] && $RM -rf /$TESTPOOL$nmirrors
337 log_must
$ZPOOL create
-f $TESTPOOL$nmirrors mirror
$1 $2
339 ((nmirrors
= nmirrors
- 1))
344 # create a number of raidz pools.
345 # We create a number($1) of 2 raidz pools using the pairs of disks named
346 # on the command line. These pools are *not* mounted
347 # @parameters: $1 the number of pools to create
348 # $... the devices to use to create the pools on
349 # @uses: ZPOOL ZFS TESTPOOL
350 function setup_raidzs
352 typeset
-i nraidzs
=$1
355 while ((nraidzs
> 0)); do
356 log_must
test -n "$1" -a -n "$2"
357 [[ -d /$TESTPOOL$nraidzs ]] && $RM -rf /$TESTPOOL$nraidzs
358 log_must
$ZPOOL create
-f $TESTPOOL$nraidzs raidz
$1 $2
360 ((nraidzs
= nraidzs
- 1))
365 # Destroy the configured testpool mirrors.
366 # the mirrors are of the form ${TESTPOOL}{number}
367 # @uses: ZPOOL ZFS TESTPOOL
368 function destroy_mirrors
370 default_cleanup_noexit
376 # Given a minimum of two disks, set up a storage pool and dataset for the raid-z
377 # $1 the list of disks
379 function default_raidz_setup
381 typeset disklist
="$*"
382 disks
=(${disklist[*]})
384 if [[ ${#disks[*]} -lt 2 ]]; then
385 log_fail
"A raid-z requires a minimum of two disks."
388 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
389 log_must
$ZPOOL create
-f $TESTPOOL raidz
$1 $2 $3
390 log_must
$ZFS create
$TESTPOOL/$TESTFS
391 log_must
$ZFS set mountpoint
=$TESTDIR $TESTPOOL/$TESTFS
397 # Common function used to cleanup storage pools and datasets.
399 # Invoked at the start of the test suite to ensure the system
400 # is in a known state, and also at the end of each set of
401 # sub-tests to ensure errors from one set of tests doesn't
402 # impact the execution of the next set.
404 function default_cleanup
406 default_cleanup_noexit
411 function default_cleanup_noexit
416 # Destroying the pool will also destroy any
417 # filesystems it contains.
419 if is_global_zone
; then
420 $ZFS unmount
-a > /dev
/null
2>&1
421 [[ -z "$KEEP" ]] && KEEP
="rpool"
422 exclude
=`eval $ECHO \"'(${KEEP})'\"`
423 ALL_POOLS
=$
($ZPOOL list
-H -o name \
424 |
$GREP -v "$NO_POOLS" |
$EGREP -v "$exclude")
425 # Here, we loop through the pools we're allowed to
426 # destroy, only destroying them if it's safe to do
428 while [ ! -z ${ALL_POOLS} ]
430 for pool
in ${ALL_POOLS}
432 if safe_to_destroy_pool
$pool ;
436 ALL_POOLS
=$
($ZPOOL list
-H -o name \
437 |
$GREP -v "$NO_POOLS" \
438 |
$EGREP -v "$exclude")
445 for fs
in $
($ZFS list
-H -o name \
446 |
$GREP "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
447 datasetexists
$fs && \
448 log_must
$ZFS destroy
-Rf $fs
451 # Need cleanup here to avoid garbage dir left.
452 for fs
in $
($ZFS list
-H -o name
); do
453 [[ $fs == /$ZONE_POOL ]] && continue
454 [[ -d $fs ]] && log_must
$RM -rf $fs/*
458 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
461 for fs
in $
($ZFS list
-H -o name
); do
462 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
463 log_must
$ZFS set reservation
=none
$fs
464 log_must
$ZFS set recordsize
=128K
$fs
465 log_must
$ZFS set mountpoint
=/$fs $fs
467 enc
=$
(get_prop encryption
$fs)
468 if [[ $?
-ne 0 ]] ||
[[ -z "$enc" ]] || \
469 [[ "$enc" == "off" ]]; then
470 log_must
$ZFS set checksum
=on
$fs
472 log_must
$ZFS set compression
=off
$fs
473 log_must
$ZFS set atime
=on
$fs
474 log_must
$ZFS set devices
=off
$fs
475 log_must
$ZFS set exec=on
$fs
476 log_must
$ZFS set setuid
=on
$fs
477 log_must
$ZFS set readonly=off
$fs
478 log_must
$ZFS set snapdir
=hidden
$fs
479 log_must
$ZFS set aclmode
=groupmask
$fs
480 log_must
$ZFS set aclinherit
=secure
$fs
485 [[ -d $TESTDIR ]] && \
486 log_must
$RM -rf $TESTDIR
489 if is_mpath_device
$disk1; then
496 # Common function used to cleanup storage pools, file systems
499 function default_container_cleanup
501 if ! is_global_zone
; then
505 ismounted
$TESTPOOL/$TESTCTR/$TESTFS1
507 log_must
$ZFS unmount
$TESTPOOL/$TESTCTR/$TESTFS1
509 datasetexists
$TESTPOOL/$TESTCTR/$TESTFS1 && \
510 log_must
$ZFS destroy
-R $TESTPOOL/$TESTCTR/$TESTFS1
512 datasetexists
$TESTPOOL/$TESTCTR && \
513 log_must
$ZFS destroy
-Rf $TESTPOOL/$TESTCTR
515 [[ -e $TESTDIR1 ]] && \
516 log_must
$RM -rf $TESTDIR1 > /dev
/null
2>&1
522 # Common function used to cleanup snapshot of file system or volume. Default to
523 # delete the file system's snapshot
527 function destroy_snapshot
529 typeset snap
=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
531 if ! snapexists
$snap; then
532 log_fail
"'$snap' does not existed."
536 # For the sake of the value which come from 'get_prop' is not equal
537 # to the really mountpoint when the snapshot is unmounted. So, firstly
538 # check and make sure this snapshot's been mounted in current system.
541 if ismounted
$snap; then
542 mtpt
=$
(get_prop mountpoint
$snap)
544 log_fail
"get_prop mountpoint $snap failed."
547 log_must
$ZFS destroy
$snap
548 [[ $mtpt != "" && -d $mtpt ]] && \
549 log_must
$RM -rf $mtpt
553 # Common function used to cleanup clone.
557 function destroy_clone
559 typeset clone
=${1:-$TESTPOOL/$TESTCLONE}
561 if ! datasetexists
$clone; then
562 log_fail
"'$clone' does not existed."
565 # With the same reason in destroy_snapshot
567 if ismounted
$clone; then
568 mtpt
=$
(get_prop mountpoint
$clone)
570 log_fail
"get_prop mountpoint $clone failed."
573 log_must
$ZFS destroy
$clone
574 [[ $mtpt != "" && -d $mtpt ]] && \
575 log_must
$RM -rf $mtpt
578 # Return 0 if a snapshot exists; $? otherwise
584 $ZFS list
-H -t snapshot
"$1" > /dev
/null
2>&1
589 # Set a property to a certain value on a dataset.
590 # Sets a property of the dataset to the value as passed in.
592 # $1 dataset who's property is being set
594 # $3 value to set property to
596 # 0 if the property could be set.
597 # non-zero otherwise.
600 function dataset_setprop
602 typeset fn
=dataset_setprop
605 log_note
"$fn: Insufficient parameters (need 3, had $#)"
609 output
=$
($ZFS set $2=$3 $1 2>&1)
612 log_note
"Setting property on $1 failed."
613 log_note
"property $2=$3"
614 log_note
"Return Code: $rv"
615 log_note
"Output: $output"
622 # Assign suite defined dataset properties.
623 # This function is used to apply the suite's defined default set of
624 # properties to a dataset.
625 # @parameters: $1 dataset to use
626 # @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
628 # 0 if the dataset has been altered.
629 # 1 if no pool name was passed in.
630 # 2 if the dataset could not be found.
631 # 3 if the dataset could not have it's properties set.
633 function dataset_set_defaultproperties
637 [[ -z $dataset ]] && return 1
641 for confset
in $
($ZFS list
); do
642 if [[ $dataset = $confset ]]; then
647 [[ $found -eq 0 ]] && return 2
648 if [[ -n $COMPRESSION_PROP ]]; then
649 dataset_setprop
$dataset compression
$COMPRESSION_PROP || \
651 log_note
"Compression set to '$COMPRESSION_PROP' on $dataset"
653 if [[ -n $CHECKSUM_PROP ]]; then
654 dataset_setprop
$dataset checksum
$CHECKSUM_PROP || \
656 log_note
"Checksum set to '$CHECKSUM_PROP' on $dataset"
662 # Check a numeric assertion
663 # @parameter: $@ the assertion to check
664 # @output: big loud notice if assertion failed
669 (($@
)) || log_fail
"$@"
673 # Function to format partition size of a disk
674 # Given a disk cxtxdx reduces all partitions
677 function zero_partitions
#<whole_disk_name>
683 log_must
$FORMAT $DEV_DSKDIR/$diskname -s -- mklabel gpt
685 for i
in 0 1 3 4 5 6 7
687 set_partition
$i "" 0mb
$diskname
693 # Given a slice, size and disk, this function
694 # formats the slice to the specified size.
695 # Size should be specified with units as per
696 # the `format` command requirements eg. 100mb 3gb
698 # NOTE: This entire interface is problematic for the Linux parted utilty
699 # which requires the end of the partition to be specified. It would be
700 # best to retire this interface and replace it with something more flexible.
701 # At the moment a best effort is made.
703 function set_partition
#<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
705 typeset
-i slicenum
=$1
709 [[ -z $slicenum ||
-z $size ||
-z $disk ]] && \
710 log_fail
"The slice, size or disk name is unspecified."
713 typeset size_mb
=${size%%[mMgG]}
715 size_mb
=${size_mb%%[mMgG][bB]}
716 if [[ ${size:1:1} == 'g' ]]; then
717 ((size_mb
= size_mb
* 1024))
720 # Create GPT partition table when setting slice 0 or
721 # when the device doesn't already contain a GPT label.
722 $FORMAT $DEV_DSKDIR/$disk -s -- print
1 >/dev
/null
724 if [[ $slicenum -eq 0 ||
$ret_val -ne 0 ]]; then
725 log_must
$FORMAT $DEV_DSKDIR/$disk -s -- mklabel gpt
728 # When no start is given align on the first cylinder.
729 if [[ -z "$start" ]]; then
733 # Determine the cylinder size for the device and using
734 # that calculate the end offset in cylinders.
735 typeset
-i cly_size_kb
=0
736 cly_size_kb
=$
($FORMAT -m $DEV_DSKDIR/$disk -s -- \
737 unit cyl print |
$HEAD -3 |
$TAIL -1 | \
738 $AWK -F '[:k.]' '{print $4}')
739 ((end
= (size_mb
* 1024 / cly_size_kb
) + start
))
741 log_must
$FORMAT $DEV_DSKDIR/$disk -s -- \
742 mkpart part
$slicenum ${start}cyl
${end}cyl
744 $BLOCKDEV --rereadpt $DEV_DSKDIR/$disk 2>/dev
/null
747 typeset format_file
=/var
/tmp
/format_in.$$
749 $ECHO "partition" >$format_file
750 $ECHO "$slicenum" >> $format_file
751 $ECHO "" >> $format_file
752 $ECHO "" >> $format_file
753 $ECHO "$start" >> $format_file
754 $ECHO "$size" >> $format_file
755 $ECHO "label" >> $format_file
756 $ECHO "" >> $format_file
757 $ECHO "q" >> $format_file
758 $ECHO "q" >> $format_file
760 $FORMAT -e -s -d $disk -f $format_file
764 [[ $ret_val -ne 0 ]] && \
765 log_fail
"Unable to format $disk slice $slicenum to $size"
770 # Delete all partitions on all disks - this is specifically for the use of multipath
771 # devices which currently can only be used in the test suite as raw/un-partitioned
772 # devices (ie a zpool cannot be created on a whole mpath device that has partitions)
774 function delete_partitions
778 if [[ -z $DISK_ARRAY_NUM ]]; then
779 DISK_ARRAY_NUM
=$
($ECHO ${DISKS} |
$NAWK '{print NF}')
781 if [[ -z $DISKSARRAY ]]; then
786 if (( $DISK_ARRAY_NUM == 1 )); then
787 while ((j
< MAX_PARTITIONS
)); do
788 $FORMAT $DEV_DSKDIR/$DISK -s rm $j > /dev
/null
2>&1
789 if (( $?
== 1 )); then
790 $LSBLK |
$EGREP ${DISK}${SLICE_PREFIX}${j} > /dev
/null
791 if (( $?
== 1 )); then
792 log_note
"Partitions for $DISK should be deleted"
794 log_fail
"Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
798 $LSBLK |
$EGREP ${DISK}${SLICE_PREFIX}${j} > /dev
/null
799 if (( $?
== 0 )); then
800 log_fail
"Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
806 for disk
in `$ECHO $DISKSARRAY`; do
807 while ((j
< MAX_PARTITIONS
)); do
808 $FORMAT $DEV_DSKDIR/$disk -s rm $j > /dev
/null
2>&1
809 if (( $?
== 1 )); then
810 $LSBLK |
$EGREP ${disk}${SLICE_PREFIX}${j} > /dev
/null
811 if (( $?
== 1 )); then
812 log_note
"Partitions for $disk should be deleted"
814 log_fail
"Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
818 $LSBLK |
$EGREP ${disk}${SLICE_PREFIX}${j} > /dev
/null
819 if (( $?
== 0 )); then
820 log_fail
"Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
833 # Get the end cyl of the given slice
835 function get_endslice
#<disk> <slice>
839 if [[ -z $disk ||
-z $slice ]] ; then
840 log_fail
"The disk name or slice number is unspecified."
844 endcyl
=$
($FORMAT -s $DEV_DSKDIR/$disk -- unit cyl print | \
845 $GREP "part${slice}" | \
846 $AWK '{print $3}' | \
848 ((endcyl
= (endcyl
+ 1)))
850 disk
=${disk#/dev/dsk/}
851 disk
=${disk#/dev/rdsk/}
855 ratio
=$
($PRTVTOC /dev
/rdsk
/${disk}s2 | \
856 $GREP "sectors\/cylinder" | \
859 if ((ratio
== 0)); then
863 typeset
-i endcyl
=$
($PRTVTOC -h /dev
/rdsk
/${disk}s2 |
864 $NAWK -v token
="$slice" '{if ($1==token) print $6}')
866 ((endcyl
= (endcyl
+ 1) / ratio
))
874 # Given a size,disk and total slice number, this function formats the
875 # disk slices from 0 to the total slice number with the same specified
878 function partition_disk
#<slice_size> <whole_disk_name> <total_slices>
881 typeset slice_size
=$1
883 typeset total_slices
=$3
886 zero_partitions
$disk_name
887 while ((i
< $total_slices)); do
894 set_partition
$i "$cyl" $slice_size $disk_name
895 cyl
=$
(get_endslice
$disk_name $i)
901 # This function continues to write to a filenum number of files into dirnum
902 # number of directories until either $FILE_WRITE returns an error or the
903 # maximum number of files per directory have been written.
906 # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
908 # Return value: 0 on success
912 # destdir: is the directory where everything is to be created under
913 # dirnum: the maximum number of subdirectories to use, -1 no limit
914 # filenum: the maximum number of files per subdirectory
915 # bytes: number of bytes to write
916 # num_writes: numer of types to write out bytes
917 # data: the data that will be written
920 # file_fs /testdir 20 25 1024 256 0
922 # Note: bytes * num_writes equals the size of the testfile
924 function fill_fs
# destdir dirnum filenum bytes num_writes data
926 typeset destdir
=${1:-$TESTDIR}
927 typeset
-i dirnum
=${2:-50}
928 typeset
-i filenum
=${3:-50}
929 typeset
-i bytes
=${4:-8192}
930 typeset
-i num_writes
=${5:-10240}
931 typeset
-i data
=${6:-0}
938 log_must
$MKDIR -p $destdir/$idirnum
939 while (($odirnum > 0)); do
940 if ((dirnum
>= 0 && idirnum
>= dirnum
)); then
944 $FILE_WRITE -o create
-f $destdir/$idirnum/$TESTFILE.
$fn \
945 -b $bytes -c $num_writes -d $data
947 if (($retval != 0)); then
951 if (($fn >= $filenum)); then
953 ((idirnum
= idirnum
+ 1))
954 log_must
$MKDIR -p $destdir/$idirnum
963 # Simple function to get the specified property. If unable to
964 # get the property then exits.
966 # Note property is in 'parsable' format (-p)
968 function get_prop
# property dataset
974 prop_val
=$
($ZFS get
-pH -o value
$prop $dataset 2>/dev
/null
)
975 if [[ $?
-ne 0 ]]; then
976 log_note
"Unable to get $prop property for dataset " \
986 # Simple function to get the specified property of pool. If unable to
987 # get the property then exits.
989 function get_pool_prop
# property pool
995 if poolexists
$pool ; then
996 prop_val
=$
($ZPOOL get
$prop $pool 2>/dev
/null |
$TAIL -1 | \
998 if [[ $?
-ne 0 ]]; then
999 log_note
"Unable to get $prop property for pool " \
1004 log_note
"Pool $pool not exists."
1012 # Return 0 if a pool exists; $? otherwise
1020 if [[ -z $pool ]]; then
1021 log_note
"No pool name given."
1025 $ZPOOL get name
"$pool" > /dev
/null
2>&1
1029 # Return 0 if all the specified datasets exist; $? otherwise
1032 function datasetexists
1034 if (($# == 0)); then
1035 log_note
"No dataset name given."
1039 while (($# > 0)); do
1040 $ZFS get name
$1 > /dev
/null
2>&1 || \
1048 # return 0 if none of the specified datasets exists, otherwise return 1.
1051 function datasetnonexists
1053 if (($# == 0)); then
1054 log_note
"No dataset name given."
1058 while (($# > 0)); do
1059 $ZFS list
-H -t filesystem
,snapshot
,volume
$1 > /dev
/null
2>&1 \
1068 # Given a mountpoint, or a dataset name, determine if it is shared via NFS.
1070 # Returns 0 if shared, 1 otherwise.
1077 if [[ $fs != "/"* ]] ; then
1078 if datasetnonexists
"$fs" ; then
1081 mtpt
=$
(get_prop mountpoint
"$fs")
1083 none|legacy|
-) return 1
1092 for mtpt
in `$SHARE | $AWK '{print $1}'` ; do
1093 if [[ $mtpt == $fs ]] ; then
1100 for mtpt
in `$SHARE | $AWK '{print $2}'` ; do
1101 if [[ $mtpt == $fs ]] ; then
1106 typeset stat
=$
($SVCS -H -o STA nfs
/server
:default
)
1107 if [[ $stat != "ON" ]]; then
1108 log_note
"Current nfs/server status: $stat"
1115 # Given a dataset name determine if it is shared via SMB.
1117 # Returns 0 if shared, 1 otherwise.
1119 function is_shared_smb
1124 if datasetnonexists
"$fs" ; then
1127 fs
=$
(echo $fs |
sed 's@/@_@g')
1131 for mtpt
in `$NET usershare list | $AWK '{print $1}'` ; do
1132 if [[ $mtpt == $fs ]] ; then
1138 log_unsupported
"Currently unsupported by the test framework"
1144 # Given a mountpoint, determine if it is not shared via NFS.
1146 # Returns 0 if not shared, 1 otherwise.
1153 if (($?
== 0)); then
1161 # Given a dataset determine if it is not shared via SMB.
1163 # Returns 0 if not shared, 1 otherwise.
1165 function not_shared_smb
1170 if (($?
== 0)); then
1178 # Helper function to unshare a mountpoint.
1180 function unshare_fs
#fs
1184 is_shared
$fs || is_shared_smb
$fs
1185 if (($?
== 0)); then
1186 log_must
$ZFS unshare
$fs
1193 # Helper function to share a NFS mountpoint.
1195 function share_nfs
#fs
1201 if (($?
!= 0)); then
1202 log_must
$SHARE "*:$fs"
1206 if (($?
!= 0)); then
1207 log_must
$SHARE -F nfs
$fs
1215 # Helper function to unshare a NFS mountpoint.
1217 function unshare_nfs
#fs
1223 if (($?
== 0)); then
1224 log_must
$UNSHARE -u "*:$fs"
1228 if (($?
== 0)); then
1229 log_must
$UNSHARE -F nfs
$fs
1237 # Helper function to show NFS shares.
1239 function showshares_nfs
1251 # Helper function to show SMB shares.
1253 function showshares_smb
1265 # Check NFS server status and trigger it online.
1267 function setup_nfs_server
1269 # Cannot share directory in non-global zone.
1271 if ! is_global_zone
; then
1272 log_note
"Cannot trigger NFS server by sharing in LZ."
1277 log_note
"NFS server must started prior to running test framework."
1281 typeset nfs_fmri
="svc:/network/nfs/server:default"
1282 if [[ $
($SVCS -Ho STA
$nfs_fmri) != "ON" ]]; then
1284 # Only really sharing operation can enable NFS server
1285 # to online permanently.
1287 typeset dummy
=/tmp
/dummy
1289 if [[ -d $dummy ]]; then
1290 log_must
$RM -rf $dummy
1293 log_must
$MKDIR $dummy
1294 log_must
$SHARE $dummy
1297 # Waiting for fmri's status to be the final status.
1298 # Otherwise, in transition, an asterisk (*) is appended for
1299 # instances, unshare will reverse status to 'DIS' again.
1301 # Waiting for 1's at least.
1305 while [[ timeout
-ne 0 && $
($SVCS -Ho STA
$nfs_fmri) == *'*' ]]
1312 log_must
$UNSHARE $dummy
1313 log_must
$RM -rf $dummy
1316 log_note
"Current NFS status: '$($SVCS -Ho STA,FMRI $nfs_fmri)'"
1320 # To verify whether calling process is in global zone
1322 # Return 0 if in global zone, 1 in non-global zone
1324 function is_global_zone
1326 typeset cur_zone
=$
($ZONENAME 2>/dev
/null
)
1327 if [[ $cur_zone != "global" ]]; then
1334 # Verify whether test is permitted to run from
1335 # global zone, local zone, or both
1337 # $1 zone limit, could be "global", "local", or "both"(no limit)
1339 # Return 0 if permitted, otherwise exit with log_unsupported
1341 function verify_runnable
# zone limit
1345 [[ -z $limit ]] && return 0
1347 if is_global_zone
; then
1351 local) log_unsupported
"Test is unable to run from "\
1354 *) log_note
"Warning: unknown limit $limit - " \
1362 global
) log_unsupported
"Test is unable to run from "\
1365 *) log_note
"Warning: unknown limit $limit - " \
1376 # Return 0 if create successfully or the pool exists; $? otherwise
1377 # Note: In local zones, this function should return 0 silently.
1380 # $2-n - [keyword] devs_list
1382 function create_pool
#pool devs_list
1384 typeset pool
=${1%%/*}
1388 if [[ -z $pool ]]; then
1389 log_note
"Missing pool name."
1393 if poolexists
$pool ; then
1397 if is_global_zone
; then
1398 [[ -d /$pool ]] && $RM -rf /$pool
1399 log_must
$ZPOOL create
-f $pool $@
1405 # Return 0 if destroy successfully or the pool exists; $? otherwise
1406 # Note: In local zones, this function should return 0 silently.
1409 # Destroy pool with the given parameters.
1411 function destroy_pool
#pool
1413 typeset pool
=${1%%/*}
1416 if [[ -z $pool ]]; then
1417 log_note
"No pool name given."
1421 if is_global_zone
; then
1422 if poolexists
"$pool" ; then
1423 mtpt
=$
(get_prop mountpoint
"$pool")
1425 # At times, syseventd activity can cause attempts to
1426 # destroy a pool to fail with EBUSY. We retry a few
1427 # times allowing failures before requiring the destroy
1429 typeset
-i wait_time
=10 ret
=1 count
=0
1431 while [[ $ret -ne 0 ]]; do
1432 $must $ZPOOL destroy
-f $pool
1434 [[ $ret -eq 0 ]] && break
1435 log_note
"zpool destroy failed with $ret"
1436 [[ count
++ -ge 7 ]] && must
=log_must
1441 log_must
$RM -rf $mtpt
1443 log_note
"Pool does not exist. ($pool)"
1452 # Firstly, create a pool with 5 datasets. Then, create a single zone and
1453 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
1454 # and a zvol device to the zone.
1457 # $2 zone root directory prefix
1460 function zfs_zones_setup
#zone_name zone_root zone_ip
1462 typeset zone_name
=${1:-$(hostname)-z}
1463 typeset zone_root
=${2:-"/zone_root"}
1464 typeset zone_ip
=${3:-"10.1.1.10"}
1465 typeset prefix_ctr
=$ZONE_CTR
1466 typeset pool_name
=$ZONE_POOL
1470 # Create pool and 5 container within it
1472 [[ -d /$pool_name ]] && $RM -rf /$pool_name
1473 log_must
$ZPOOL create
-f $pool_name $DISKS
1474 while ((i
< cntctr
)); do
1475 log_must
$ZFS create
$pool_name/$prefix_ctr$i
1480 log_must
$ZFS create
-V 1g
$pool_name/zone_zvol
1484 # If current system support slog, add slog device for pool
1486 if verify_slog_support
; then
1487 typeset sdevs
="/var/tmp/sdev1 /var/tmp/sdev2"
1488 log_must
$MKFILE 100M
$sdevs
1489 log_must
$ZPOOL add
$pool_name log mirror
$sdevs
1492 # this isn't supported just yet.
1493 # Create a filesystem. In order to add this to
1494 # the zone, it must have it's mountpoint set to 'legacy'
1495 # log_must $ZFS create $pool_name/zfs_filesystem
1496 # log_must $ZFS set mountpoint=legacy $pool_name/zfs_filesystem
1498 [[ -d $zone_root ]] && \
1499 log_must
$RM -rf $zone_root/$zone_name
1500 [[ ! -d $zone_root ]] && \
1501 log_must
$MKDIR -p -m 0700 $zone_root/$zone_name
1503 # Create zone configure file and configure the zone
1505 typeset zone_conf
=/tmp
/zone_conf.$$
1506 $ECHO "create" > $zone_conf
1507 $ECHO "set zonepath=$zone_root/$zone_name" >> $zone_conf
1508 $ECHO "set autoboot=true" >> $zone_conf
1510 while ((i
< cntctr
)); do
1511 $ECHO "add dataset" >> $zone_conf
1512 $ECHO "set name=$pool_name/$prefix_ctr$i" >> \
1514 $ECHO "end" >> $zone_conf
1518 # add our zvol to the zone
1519 $ECHO "add device" >> $zone_conf
1520 $ECHO "set match=$ZVOL_DEVDIR/$pool_name/zone_zvol" >> $zone_conf
1521 $ECHO "end" >> $zone_conf
1523 # add a corresponding zvol rdsk to the zone
1524 $ECHO "add device" >> $zone_conf
1525 $ECHO "set match=$ZVOL_RDEVDIR/$pool_name/zone_zvol" >> $zone_conf
1526 $ECHO "end" >> $zone_conf
1528 # once it's supported, we'll add our filesystem to the zone
1529 # $ECHO "add fs" >> $zone_conf
1530 # $ECHO "set type=zfs" >> $zone_conf
1531 # $ECHO "set special=$pool_name/zfs_filesystem" >> $zone_conf
1532 # $ECHO "set dir=/export/zfs_filesystem" >> $zone_conf
1533 # $ECHO "end" >> $zone_conf
1535 $ECHO "verify" >> $zone_conf
1536 $ECHO "commit" >> $zone_conf
1537 log_must
$ZONECFG -z $zone_name -f $zone_conf
1538 log_must
$RM -f $zone_conf
1541 $ZONEADM -z $zone_name install
1542 if (($?
== 0)); then
1543 log_note
"SUCCESS: $ZONEADM -z $zone_name install"
1545 log_fail
"FAIL: $ZONEADM -z $zone_name install"
1548 # Install sysidcfg file
1550 typeset sysidcfg
=$zone_root/$zone_name/root
/etc
/sysidcfg
1551 $ECHO "system_locale=C" > $sysidcfg
1552 $ECHO "terminal=dtterm" >> $sysidcfg
1553 $ECHO "network_interface=primary {" >> $sysidcfg
1554 $ECHO "hostname=$zone_name" >> $sysidcfg
1555 $ECHO "}" >> $sysidcfg
1556 $ECHO "name_service=NONE" >> $sysidcfg
1557 $ECHO "root_password=mo791xfZ/SFiw" >> $sysidcfg
1558 $ECHO "security_policy=NONE" >> $sysidcfg
1559 $ECHO "timezone=US/Eastern" >> $sysidcfg
1562 log_must
$ZONEADM -z $zone_name boot
1566 # Reexport TESTPOOL & TESTPOOL(1-4)
1568 function reexport_pool
1573 while ((i
< cntctr
)); do
1575 TESTPOOL
=$ZONE_POOL/$ZONE_CTR$i
1576 if ! ismounted
$TESTPOOL; then
1577 log_must
$ZFS mount
$TESTPOOL
1580 eval TESTPOOL
$i=$ZONE_POOL/$ZONE_CTR$i
1581 if eval ! ismounted \
$TESTPOOL$i; then
1582 log_must
eval $ZFS mount \
$TESTPOOL$i
1590 # Verify a given disk is online or offline
1592 # Return 0 is pool/disk matches expected state, 1 otherwise
1594 function check_state
# pool disk state{online,offline}
1597 typeset disk
=${2#$DEV_DSKDIR/}
1600 $ZPOOL status
-v $pool |
grep "$disk" \
1601 |
grep -i "$state" > /dev
/null
2>&1
1607 # Get the mountpoint of snapshot
1608 # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1611 function snapshot_mountpoint
1613 typeset dataset
=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1615 if [[ $dataset != *@
* ]]; then
1616 log_fail
"Error name of snapshot '$dataset'."
1619 typeset fs
=${dataset%@*}
1620 typeset snap
=${dataset#*@}
1622 if [[ -z $fs ||
-z $snap ]]; then
1623 log_fail
"Error name of snapshot '$dataset'."
1626 $ECHO $
(get_prop mountpoint
$fs)/.zfs
/snapshot
/$snap
1630 # Given a pool and file system, this function will verify the file system
1631 # using the zdb internal tool. Note that the pool is exported and imported
1632 # to ensure it has consistent state.
1634 function verify_filesys
# pool filesystem dir
1637 typeset filesys
="$2"
1638 typeset zdbout
="/tmp/zdbout.$$"
1643 typeset search_path
=""
1645 log_note
"Calling $ZDB to verify filesystem '$filesys'"
1646 $ZFS unmount
-a > /dev
/null
2>&1
1647 log_must
$ZPOOL export $pool
1649 if [[ -n $dirs ]] ; then
1650 for dir
in $dirs ; do
1651 search_path
="$search_path -d $dir"
1655 log_must
$ZPOOL import
$search_path $pool
1657 $ZDB -cudi $filesys > $zdbout 2>&1
1658 if [[ $?
!= 0 ]]; then
1659 log_note
"Output: $ZDB -cudi $filesys"
1661 log_fail
"$ZDB detected errors with: '$filesys'"
1664 log_must
$ZFS mount
-a
1665 log_must
$RM -rf $zdbout
1669 # Given a pool, and this function list all disks in the pool
1671 function get_disklist
# pool
1675 disklist
=$
($ZPOOL iostat
-v $1 |
$NAWK '(NR >4) {print $1}' | \
1676 $GREP -v "\-\-\-\-\-" | \
1677 $EGREP -v -e "^(mirror|raidz1|raidz2|spare|log|cache)$")
1683 # Given a pool, and this function list all disks in the pool with their full
1684 # path (like "/dev/sda" instead of "sda").
1686 function get_disklist_fullpath
# pool
1695 # This function kills a given list of processes after a time period. We use
1696 # this in the stress tests instead of STF_TIMEOUT so that we can have processes
1697 # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1698 # would be listed as FAIL, which we don't want : we're happy with stress tests
1699 # running for a certain amount of time, then finishing.
1701 # @param $1 the time in seconds after which we should terminate these processes
1702 # @param $2..$n the processes we wish to terminate.
1704 function stress_timeout
1706 typeset
-i TIMEOUT
=$1
1710 log_note
"Waiting for child processes($cpids). " \
1711 "It could last dozens of minutes, please be patient ..."
1712 log_must
$SLEEP $TIMEOUT
1714 log_note
"Killing child processes after ${TIMEOUT} stress timeout."
1716 for pid
in $cpids; do
1717 $PS -p $pid > /dev
/null
2>&1
1718 if (($?
== 0)); then
1719 log_must
$KILL -USR1 $pid
1725 # Verify a given hotspare disk is inuse or avail
1727 # Return 0 is pool/disk matches expected state, 1 otherwise
1729 function check_hotspare_state
# pool disk state{inuse,avail}
1732 typeset disk
=${2#$DEV_DSKDIR/}
1735 cur_state
=$
(get_device_state
$pool $disk "spares")
1737 if [[ $state != ${cur_state} ]]; then
1744 # Verify a given slog disk is inuse or avail
1746 # Return 0 is pool/disk matches expected state, 1 otherwise
1748 function check_slog_state
# pool disk state{online,offline,unavail}
1751 typeset disk
=${2#$DEV_DSKDIR/}
1754 cur_state
=$
(get_device_state
$pool $disk "logs")
1756 if [[ $state != ${cur_state} ]]; then
1763 # Verify a given vdev disk is inuse or avail
1765 # Return 0 is pool/disk matches expected state, 1 otherwise
1767 function check_vdev_state
# pool disk state{online,offline,unavail}
1770 typeset disk
=${2#$/DEV_DSKDIR/}
1773 cur_state
=$
(get_device_state
$pool $disk)
1775 if [[ $state != ${cur_state} ]]; then
1782 # Check the output of 'zpool status -v <pool>',
1783 # and to see if the content of <token> contain the <keyword> specified.
1785 # Return 0 is contain, 1 otherwise
1787 function check_pool_status
# pool token keyword
1793 $ZPOOL status
-v "$pool" 2>/dev
/null |
$NAWK -v token
="$token:" '
1794 ($1==token) {print $0}' \
1795 |
$GREP -i "$keyword" > /dev
/null
2>&1
1801 # These 5 following functions are instance of check_pool_status()
1802 # is_pool_resilvering - to check if the pool is resilver in progress
1803 # is_pool_resilvered - to check if the pool is resilver completed
1804 # is_pool_scrubbing - to check if the pool is scrub in progress
1805 # is_pool_scrubbed - to check if the pool is scrub completed
1806 # is_pool_scrub_stopped - to check if the pool is scrub stopped
1808 function is_pool_resilvering
#pool
1810 check_pool_status
"$1" "scan" "resilver in progress since "
1814 function is_pool_resilvered
#pool
1816 check_pool_status
"$1" "scan" "resilvered "
1820 function is_pool_scrubbing
#pool
1822 check_pool_status
"$1" "scan" "scrub in progress since "
1826 function is_pool_scrubbed
#pool
1828 check_pool_status
"$1" "scan" "scrub repaired"
1832 function is_pool_scrub_stopped
#pool
1834 check_pool_status
"$1" "scan" "scrub canceled"
1839 # Use create_pool()/destroy_pool() to clean up the information in
1840 # in the given disk to avoid slice overlapping.
1842 function cleanup_devices
#vdevs
1844 typeset pool
="foopool$$"
1846 if poolexists
$pool ; then
1850 create_pool
$pool $@
1857 # Verify the rsh connectivity to each remote host in RHOSTS.
1859 # Return 0 if remote host is accessible; otherwise 1.
1860 # $1 remote host name
1863 function verify_rsh_connect
#rhost, username
1867 typeset rsh_cmd
="$RSH -n"
1870 $GETENT hosts
$rhost >/dev
/null
2>&1
1871 if (($?
!= 0)); then
1872 log_note
"$rhost cannot be found from" \
1873 "administrative database."
1877 $PING $rhost 3 >/dev
/null
2>&1
1878 if (($?
!= 0)); then
1879 log_note
"$rhost is not reachable."
1883 if ((${#username} != 0)); then
1884 rsh_cmd
="$rsh_cmd -l $username"
1885 cur_user
="given user \"$username\""
1887 cur_user
="current user \"`$LOGNAME`\""
1890 if ! $rsh_cmd $rhost $TRUE; then
1891 log_note
"$RSH to $rhost is not accessible" \
1900 # Verify the remote host connection via rsh after rebooting
1903 function verify_remote
1908 # The following loop waits for the remote system rebooting.
1909 # Each iteration will wait for 150 seconds. there are
1910 # total 5 iterations, so the total timeout value will
1911 # be 12.5 minutes for the system rebooting. This number
1912 # is an approxiate number.
1915 while ! verify_rsh_connect
$rhost; do
1917 ((count
= count
+ 1))
1918 if ((count
> 5)); then
1926 # Replacement function for /usr/bin/rsh. This function will include
1927 # the /usr/bin/rsh and meanwhile return the execution status of the
1930 # $1 usrname passing down to -l option of /usr/bin/rsh
1931 # $2 remote machine hostname
1932 # $3... command string
1946 err_file
=/tmp
/${rhost}.$$.err
1947 if ((${#ruser} == 0)); then
1950 rsh_str
="$RSH -n -l $ruser"
1953 $rsh_str $rhost /bin
/ksh
-c "'$cmd_str; \
1954 print -u 2 \"status=\$?\"'" \
1955 >/dev
/null
2>$err_file
1957 if (($ret != 0)); then
1959 $RM -f $std_file $err_file
1960 log_fail
"$RSH itself failed with exit code $ret..."
1963 ret
=$
($GREP -v 'print -u 2' $err_file |
$GREP 'status=' | \
1965 (($ret != 0)) && $CAT $err_file >&2
1967 $RM -f $err_file >/dev
/null
2>&1
1972 # Get the SUNWstc-fs-zfs package installation path in a remote host
1973 # $1 remote host name
1975 function get_remote_pkgpath
1980 pkgpath
=$
($RSH -n $rhost "$PKGINFO -l SUNWstc-fs-zfs | $GREP BASEDIR: |\
1987 # A function to find and locate free disks on a system or from given
1988 # disks as the parameter. It works by locating disks that are in use
1989 # as swap devices and dump devices, and also disks listed in /etc/vfstab
1991 # $@ given disks to find which are free, default is all disks in
1994 # @return a string containing the list of available disks
1998 # Trust provided list, no attempt is made to locate unused devices.
2005 sfi
=/tmp
/swaplist.$$
2006 dmpi
=/tmp
/dumpdev.$$
2007 max_finddisksnum
=${MAX_FINDDISKSNUM:-6}
2010 $DUMPADM > $dmpi 2>/dev
/null
2012 # write an awk script that can process the output of format
2013 # to produce a list of disks we know about. Note that we have
2014 # to escape "$2" so that the shell doesn't interpret it while
2015 # we're creating the awk script.
2016 # -------------------
2017 $CAT > /tmp
/find_disks.
awk <<EOF
2026 if (searchdisks && \$2 !~ "^$"){
2032 /^AVAILABLE DISK SELECTIONS:/{
2036 #---------------------
2038 $CHMOD 755 /tmp
/find_disks.
awk
2039 disks
=${@:-$($ECHO "" | $FORMAT -e 2>/dev/null | /tmp/find_disks.awk)}
2040 $RM /tmp
/find_disks.
awk
2043 for disk
in $disks; do
2045 $GREP "${disk}[sp]" /etc
/mnttab
>/dev
/null
2046 (($?
== 0)) && continue
2048 $GREP "${disk}[sp]" $sfi >/dev
/null
2049 (($?
== 0)) && continue
2050 # check for dump device
2051 $GREP "${disk}[sp]" $dmpi >/dev
/null
2052 (($?
== 0)) && continue
2053 # check to see if this disk hasn't been explicitly excluded
2054 # by a user-set environment variable
2055 $ECHO "${ZFS_HOST_DEVICES_IGNORE}" |
$GREP "${disk}" > /dev
/null
2056 (($?
== 0)) && continue
2057 unused_candidates
="$unused_candidates $disk"
2062 # now just check to see if those disks do actually exist
2063 # by looking for a device pointing to the first slice in
2064 # each case. limit the number to max_finddisksnum
2066 for disk
in $unused_candidates; do
2067 if [ -b $DEV_DSKDIR/${disk}s0
]; then
2068 if [ $count -lt $max_finddisksnum ]; then
2069 unused
="$unused $disk"
2070 # do not impose limit if $@ is provided
2071 [[ -z $@
]] && ((count
= count
+ 1))
2076 # finally, return our disk list
2081 # Add specified user to specified group
2085 # $3 base of the homedir (optional)
2087 function add_user
#<group_name> <user_name> <basedir>
2091 typeset basedir
=${3:-"/var/tmp"}
2093 if ((${#gname} == 0 ||
${#uname} == 0)); then
2094 log_fail
"group name or user name are not defined."
2097 log_must
$USERADD -g $gname -d $basedir/$uname -m $uname
2099 # Add new users to the same group and the command line utils.
2100 # This allows them to be run out of the original users home
2101 # directory as long as it permissioned to be group readable.
2103 cmd_group
=$
(stat
--format="%G" $ZFS)
2104 log_must
$USERMOD -a -G $cmd_group $uname
2111 # Delete the specified user.
2114 # $2 base of the homedir (optional)
2116 function del_user
#<logname> <basedir>
2119 typeset basedir
=${2:-"/var/tmp"}
2121 if ((${#user} == 0)); then
2122 log_fail
"login name is necessary."
2125 if $ID $user > /dev
/null
2>&1; then
2126 log_must
$USERDEL $user
2129 [[ -d $basedir/$user ]] && $RM -fr $basedir/$user
2135 # Select valid gid and create specified group.
2139 function add_group
#<group_name>
2143 if ((${#group} == 0)); then
2144 log_fail
"group name is necessary."
2147 # Assign 100 as the base gid, a larger value is selected for
2148 # Linux because for many distributions 1000 and under are reserved.
2151 $GROUPADD $group > /dev
/null
2>&1
2162 $GROUPADD -g $gid $group > /dev
/null
2>&1
2166 # The gid is not unique
2175 # Delete the specified group.
2179 function del_group
#<group_name>
2182 if ((${#grp} == 0)); then
2183 log_fail
"group name is necessary."
2187 $GETENT group
$grp > /dev
/null
2>&1
2190 # Group does not exist.
2192 # Name already exists as a group name
2193 0) log_must
$GROUPDEL $grp ;;
2197 $GROUPMOD -n $grp $grp > /dev
/null
2>&1
2200 # Group does not exist.
2202 # Name already exists as a group name
2203 9) log_must
$GROUPDEL $grp ;;
2212 # This function will return true if it's safe to destroy the pool passed
2213 # as argument 1. It checks for pools based on zvols and files, and also
2214 # files contained in a pool that may have a different mountpoint.
2216 function safe_to_destroy_pool
{ # $1 the pool name
2219 typeset DONT_DESTROY
=""
2221 # We check that by deleting the $1 pool, we're not
2222 # going to pull the rug out from other pools. Do this
2223 # by looking at all other pools, ensuring that they
2224 # aren't built from files or zvols contained in this pool.
2226 for pool
in $
($ZPOOL list
-H -o name
)
2230 # this is a list of the top-level directories in each of the
2231 # files that make up the path to the files the pool is based on
2232 FILEPOOL
=$
($ZPOOL status
-v $pool |
$GREP /$1/ | \
2235 # this is a list of the zvols that make up the pool
2236 ZVOLPOOL
=$
($ZPOOL status
-v $pool |
$GREP "$ZVOL_DEVDIR/$1$" \
2237 |
$AWK '{print $1}')
2239 # also want to determine if it's a file-based pool using an
2240 # alternate mountpoint...
2241 POOL_FILE_DIRS
=$
($ZPOOL status
-v $pool | \
2242 $GREP / |
$AWK '{print $1}' | \
2243 $AWK -F/ '{print $2}' |
$GREP -v "dev")
2245 for pooldir
in $POOL_FILE_DIRS
2247 OUTPUT
=$
($ZFS list
-H -r -o mountpoint
$1 | \
2248 $GREP "${pooldir}$" |
$AWK '{print $1}')
2250 ALTMOUNTPOOL
="${ALTMOUNTPOOL}${OUTPUT}"
2254 if [ ! -z "$ZVOLPOOL" ]
2257 log_note
"Pool $pool is built from $ZVOLPOOL on $1"
2260 if [ ! -z "$FILEPOOL" ]
2263 log_note
"Pool $pool is built from $FILEPOOL on $1"
2266 if [ ! -z "$ALTMOUNTPOOL" ]
2269 log_note
"Pool $pool is built from $ALTMOUNTPOOL on $1"
2273 if [ -z "${DONT_DESTROY}" ]
2277 log_note
"Warning: it is not safe to destroy $1!"
2283 # Get the available ZFS compression options
2284 # $1 option type zfs_set|zfs_compress
2286 function get_compress_opts
2288 typeset COMPRESS_OPTS
2289 typeset GZIP_OPTS
="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2290 gzip-6 gzip-7 gzip-8 gzip-9"
2292 if [[ $1 == "zfs_compress" ]] ; then
2293 COMPRESS_OPTS
="on lzjb"
2294 elif [[ $1 == "zfs_set" ]] ; then
2295 COMPRESS_OPTS
="on off lzjb"
2297 typeset valid_opts
="$COMPRESS_OPTS"
2298 $ZFS get
2>&1 |
$GREP gzip >/dev
/null
2>&1
2299 if [[ $?
-eq 0 ]]; then
2300 valid_opts
="$valid_opts $GZIP_OPTS"
2306 # Verify zfs operation with -p option work as expected
2307 # $1 operation, value could be create, clone or rename
2308 # $2 dataset type, value could be fs or vol
2310 # $4 new dataset name
2312 function verify_opt_p_ops
2317 typeset newdataset
=$4
2319 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2320 log_fail
"$datatype is not supported."
2323 # check parameters accordingly
2328 if [[ $datatype == "vol" ]]; then
2329 ops
="create -V $VOLSIZE"
2333 if [[ -z $newdataset ]]; then
2334 log_fail
"newdataset should not be empty" \
2337 log_must datasetexists
$dataset
2338 log_must snapexists
$dataset
2341 if [[ -z $newdataset ]]; then
2342 log_fail
"newdataset should not be empty" \
2345 log_must datasetexists
$dataset
2346 log_mustnot snapexists
$dataset
2349 log_fail
"$ops is not supported."
2353 # make sure the upper level filesystem does not exist
2354 if datasetexists
${newdataset%/*} ; then
2355 log_must
$ZFS destroy
-rRf ${newdataset%/*}
2358 # without -p option, operation will fail
2359 log_mustnot
$ZFS $ops $dataset $newdataset
2360 log_mustnot datasetexists
$newdataset ${newdataset%/*}
2362 # with -p option, operation should succeed
2363 log_must
$ZFS $ops -p $dataset $newdataset
2366 if ! datasetexists
$newdataset ; then
2367 log_fail
"-p option does not work for $ops"
2370 # when $ops is create or clone, redo the operation still return zero
2371 if [[ $ops != "rename" ]]; then
2372 log_must
$ZFS $ops -p $dataset $newdataset
2379 # Get configuration of pool
2389 if ! poolexists
"$pool" ; then
2392 alt_root
=$
($ZPOOL list
-H $pool |
$AWK '{print $NF}')
2393 if [[ $alt_root == "-" ]]; then
2394 value
=$
($ZDB -C $pool |
$GREP "$config:" |
$AWK -F: \
2397 value
=$
($ZDB -e $pool |
$GREP "$config:" |
$AWK -F: \
2400 if [[ -n $value ]] ; then
2410 # Privated function. Random select one of items from arguments.
2415 function _random_get
2422 ((ind
= RANDOM
% cnt
+ 1))
2424 typeset ret
=$
($ECHO "$str" |
$CUT -f $ind -d ' ')
2429 # Random select one of item from arguments which include NONE string
2431 function random_get_with_non
2436 _random_get
"$cnt" "$@"
2440 # Random select one of item from arguments which doesn't include NONE string
2444 _random_get
"$#" "$@"
2448 # Detect if the current system support slog
2450 function verify_slog_support
2452 typeset dir
=/tmp
/disk.$$
2458 $MKFILE 64M
$vdev $sdev
2461 if ! $ZPOOL create
-n $pool $vdev log
$sdev > /dev
/null
2>&1; then
2470 # The function will generate a dataset name with specific length
2471 # $1, the length of the name
2472 # $2, the base string to construct the name
2474 function gen_dataset_name
2477 typeset basestr
="$2"
2478 typeset
-i baselen
=${#basestr}
2482 if ((len
% baselen
== 0)); then
2483 ((iter
= len
/ baselen
))
2485 ((iter
= len
/ baselen
+ 1))
2487 while ((iter
> 0)); do
2488 l_name
="${l_name}$basestr"
2497 # Get cksum tuple of dataset
2500 # sample zdb output:
2501 # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2502 # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2503 # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2504 # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2505 function datasetcksum
2509 cksum=$
($ZDB -vvv $1 |
$GREP "^Dataset $1 \[" |
$GREP "cksum" \
2510 |
$AWK -F= '{print $7}')
2521 cksum=$
($CKSUM $1 |
$AWK '{print $1}')
2526 # Get the given disk/slice state from the specific field of the pool
2528 function get_device_state
#pool disk field("", "spares","logs")
2531 typeset disk
=${2#$DEV_DSKDIR/}
2532 typeset field
=${3:-$pool}
2534 state
=$
($ZPOOL status
-v "$pool" 2>/dev
/null | \
2535 $NAWK -v device
=$disk -v pool
=$pool -v field
=$field \
2536 'BEGIN {startconfig=0; startfield=0; }
2537 /config:/ {startconfig=1}
2538 (startconfig==1) && ($1==field) {startfield=1; next;}
2539 (startfield==1) && ($1==device) {print $2; exit;}
2541 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2547 # print the given directory filesystem type
2555 if [[ -z $dir ]]; then
2556 log_fail
"Usage: get_fstype <directory>"
2563 $DF -n $dir |
$AWK '{print $3}'
2567 # Given a disk, label it to VTOC regardless what label was on the disk
2573 if [[ -z $disk ]]; then
2574 log_fail
"The disk name is unspecified."
2576 typeset label_file
=/var
/tmp
/labelvtoc.$$
2577 typeset arch
=$
($UNAME -p)
2580 log_note
"Currently unsupported by the test framework"
2584 if [[ $arch == "i386" ]]; then
2585 $ECHO "label" > $label_file
2586 $ECHO "0" >> $label_file
2587 $ECHO "" >> $label_file
2588 $ECHO "q" >> $label_file
2589 $ECHO "q" >> $label_file
2591 $FDISK -B $disk >/dev
/null
2>&1
2592 # wait a while for fdisk finishes
2594 elif [[ $arch == "sparc" ]]; then
2595 $ECHO "label" > $label_file
2596 $ECHO "0" >> $label_file
2597 $ECHO "" >> $label_file
2598 $ECHO "" >> $label_file
2599 $ECHO "" >> $label_file
2600 $ECHO "q" >> $label_file
2602 log_fail
"unknown arch type"
2605 $FORMAT -e -s -d $disk -f $label_file
2606 typeset
-i ret_val
=$?
2609 # wait the format to finish
2612 if ((ret_val
!= 0)); then
2613 log_fail
"unable to label $disk as VTOC."
2620 # check if the system was installed as zfsroot or not
2621 # return: 0 ture, otherwise false
2625 $DF -n / |
$GREP zfs
> /dev
/null
2>&1
2630 # get the root filesystem name if it's zfsroot system.
2632 # return: root filesystem name
2636 rootfs
=$
($AWK '{if ($2 == "/" && $3 == "zfs") print $1}' \
2638 if [[ -z "$rootfs" ]]; then
2639 log_fail
"Can not get rootfs"
2641 $ZFS list
$rootfs > /dev
/null
2>&1
2642 if (($?
== 0)); then
2645 log_fail
"This is not a zfsroot system."
2650 # get the rootfs's pool name
2654 function get_rootpool
2658 rootfs
=$
($AWK '{if ($2 == "/" && $3 =="zfs") print $1}' \
2660 if [[ -z "$rootfs" ]]; then
2661 log_fail
"Can not get rootpool"
2663 $ZFS list
$rootfs > /dev
/null
2>&1
2664 if (($?
== 0)); then
2665 rootpool
=`$ECHO $rootfs | awk -F\/ '{print $1}'`
2668 log_fail
"This is not a zfsroot system."
2673 # Get the sub string from specified source string
2676 # $2 start position. Count from 1
2679 function get_substr
#src_str pos offset
2684 $NAWK -v pos
=$2 -v offset
=$3 '{print substr($0, pos, offset)}'
2688 # Check if the given device is physical device
2690 function is_physical_device
#device
2692 typeset device
=${1#$DEV_DSKDIR}
2693 device
=${device#$DEV_RDSKDIR}
2696 [[ -b "$DEV_DSKDIR/$device" ]] && \
2697 [[ -f /sys
/module
/loop
/parameters
/max_part
]]
2700 $ECHO $device |
$EGREP "^c[0-F]+([td][0-F]+)+$" > /dev
/null
2>&1
2706 # Check if the given device is a real device (ie SCSI device)
2708 function is_real_device
#disk
2711 [[ -z $disk ]] && log_fail
"No argument for disk given."
2714 $LSBLK $DEV_RDSKDIR/$disk -o TYPE |
$EGREP disk
> /dev
/null
2>&1
2720 # Check if the given device is a loop device
2722 function is_loop_device
#disk
2725 [[ -z $disk ]] && log_fail
"No argument for disk given."
2728 $LSBLK $DEV_RDSKDIR/$disk -o TYPE |
$EGREP loop
> /dev
/null
2>&1
2734 # Check if the given device is a multipath device and if there is a sybolic
2735 # link to a device mapper and to a disk
2736 # Currently no support for dm devices alone without multipath
2738 function is_mpath_device
#disk
2741 [[ -z $disk ]] && log_fail
"No argument for disk given."
2744 $LSBLK $DEV_MPATHDIR/$disk -o TYPE |
$EGREP mpath
> /dev
/null
2>&1
2745 if (($?
== 0)); then
2746 $READLINK $DEV_MPATHDIR/$disk > /dev
/null
2>&1
2754 # Set the slice prefix for disk partitioning depending
2755 # on whether the device is a real, multipath, or loop device.
2756 # Currently all disks have to be of the same type, so only
2757 # checks first disk to determine slice prefix.
2759 function set_slice_prefix
2765 while (( i
< $DISK_ARRAY_NUM )); do
2766 disk
="$($ECHO $DISKS | $NAWK '{print $(i + 1)}')"
2767 if ( is_mpath_device
$disk ) && [[ -z $
($ECHO $disk |
awk 'substr($1,18,1)\
2768 ~ /^[[:digit:]]+$/') ]] ||
( is_real_device
$disk ); then
2769 export SLICE_PREFIX
=""
2771 elif ( is_mpath_device
$disk || is_loop_device
$disk ); then
2772 export SLICE_PREFIX
="p"
2775 log_fail
"$disk not supported for partitioning."
2783 # Set the directory path of the listed devices in $DISK_ARRAY_NUM
2784 # Currently all disks have to be of the same type, so only
2785 # checks first disk to determine device directory
2786 # default = /dev (linux)
2787 # real disk = /dev (linux)
2788 # multipath device = /dev/mapper (linux)
2790 function set_device_dir
2796 while (( i
< $DISK_ARRAY_NUM )); do
2797 disk
="$($ECHO $DISKS | $NAWK '{print $(i + 1)}')"
2798 if is_mpath_device
$disk; then
2799 export DEV_DSKDIR
=$DEV_MPATHDIR
2802 export DEV_DSKDIR
=$DEV_RDSKDIR
2808 export DEV_DSKDIR
=$DEV_RDSKDIR
2813 # Get the directory path of given device
2815 function get_device_dir
#device
2819 if ! $
(is_physical_device
$device) ; then
2820 if [[ $device != "/" ]]; then
2823 if [[ -b "$DEV_DSKDIR/$device" ]]; then
2824 device
="$DEV_DSKDIR"
2833 # Get the package name
2835 function get_package_name
2837 typeset dirpath
=${1:-$STC_NAME}
2839 echo "SUNWstc-${dirpath}" |
/usr
/bin
/sed -e "s/\//-/g"
2843 # Get the word numbers from a string separated by white space
2845 function get_word_count
2851 # To verify if the require numbers of disks is given
2853 function verify_disk_count
2855 typeset
-i min
=${2:-1}
2857 typeset
-i count
=$
(get_word_count
"$1")
2859 if ((count
< min
)); then
2860 log_untested
"A minimum of $min disks is required to run." \
2861 " You specified $count disk(s)"
2865 function ds_is_volume
2867 typeset
type=$
(get_prop
type $1)
2868 [[ $type = "volume" ]] && return 0
2872 function ds_is_filesystem
2874 typeset
type=$
(get_prop
type $1)
2875 [[ $type = "filesystem" ]] && return 0
2879 function ds_is_snapshot
2881 typeset
type=$
(get_prop
type $1)
2882 [[ $type = "snapshot" ]] && return 0
2887 # Check if Trusted Extensions are installed and enabled
2889 function is_te_enabled
2891 $SVCS -H -o state labeld
2>/dev
/null |
$GREP "enabled"
2892 if (($?
!= 0)); then
2899 # Utility function to determine if a system has multiple cpus.
2905 (($
($PSRINFO |
$WC -l) > 1))
2911 function get_cpu_freq
2914 lscpu |
$AWK '/CPU MHz/ { print $3 }'
2916 $PSRINFO -v 0 |
$AWK '/processor operates at/ {print $6}'
2920 # Run the given command as the user provided.
2926 log_note
"user:$user $@"
2927 eval \
$SU \
$user -c \"$@
\" > /tmp
/out
2>/tmp
/err
2932 # Check if the pool contains the specified vdevs
2937 # Return 0 if the vdevs are contained in the pool, 1 if any of the specified
2938 # vdevs is not in the pool, and 2 if pool name is missing.
2940 function vdevs_in_pool
2945 if [[ -z $pool ]]; then
2946 log_note
"Missing pool name."
2952 typeset tmpfile
=$
($MKTEMP)
2953 $ZPOOL list
-Hv "$pool" >$tmpfile
2955 $GREP -w ${vdev##*/} $tmpfile >/dev
/null
2>&1
2956 [[ $?
-ne 0 ]] && return 1
2970 max
=$
(echo $
((max
> i ? max
: i
)))
2982 min
=$
(echo $
((min
< i ? min
: i
)))
2989 # Wait for newly created block devices to have their minors created.
2991 function block_device_wait
3000 # Synchronize all the data in pool
3004 function sync_pool
#pool
3006 typeset pool
=${1:-$TESTPOOL}
3010 # Flush all the pool data.
3012 $ZPOOL scrub
$pool >/dev
/null
2>&1
3014 (( $ret != 0 )) && \
3015 log_fail
"$ZPOOL scrub $pool failed."
3017 while ! is_pool_scrubbed
$pool; do
3018 if is_pool_resilvered
$pool ; then
3019 log_fail
"$pool should not be resilver completed."