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
295 # Create a bookmark of the given snapshot. Defaultly create a bookmark on
298 # $1 Existing filesystem or volume name. Default, $TESTFS
299 # $2 Existing snapshot name. Default, $TESTSNAP
300 # $3 bookmark name. Default, $TESTBKMARK
302 function create_bookmark
304 typeset fs_vol
=${1:-$TESTFS}
305 typeset snap
=${2:-$TESTSNAP}
306 typeset bkmark
=${3:-$TESTBKMARK}
308 [[ -z $fs_vol ]] && log_fail
"Filesystem or volume's name is undefined."
309 [[ -z $snap ]] && log_fail
"Snapshot's name is undefined."
310 [[ -z $bkmark ]] && log_fail
"Bookmark's name is undefined."
312 if bkmarkexists
$fs_vol#$bkmark; then
313 log_fail
"$fs_vol#$bkmark already exists."
315 datasetexists
$fs_vol || \
316 log_fail
"$fs_vol must exist."
317 snapexists
$fs_vol@
$snap || \
318 log_fail
"$fs_vol@$snap must exist."
320 log_must
$ZFS bookmark
$fs_vol@
$snap $fs_vol#$bkmark
323 function default_mirror_setup
325 default_mirror_setup_noexit
$1 $2 $3
331 # Given a pair of disks, set up a storage pool and dataset for the mirror
332 # @parameters: $1 the primary side of the mirror
333 # $2 the secondary side of the mirror
334 # @uses: ZPOOL ZFS TESTPOOL TESTFS
335 function default_mirror_setup_noexit
337 readonly func
="default_mirror_setup_noexit"
341 [[ -z $primary ]] && \
342 log_fail
"$func: No parameters passed"
343 [[ -z $secondary ]] && \
344 log_fail
"$func: No secondary partition passed"
345 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
346 log_must
$ZPOOL create
-f $TESTPOOL mirror $@
347 log_must
$ZFS create
$TESTPOOL/$TESTFS
348 log_must
$ZFS set mountpoint
=$TESTDIR $TESTPOOL/$TESTFS
352 # create a number of mirrors.
353 # We create a number($1) of 2 way mirrors using the pairs of disks named
354 # on the command line. These mirrors are *not* mounted
355 # @parameters: $1 the number of mirrors to create
356 # $... the devices to use to create the mirrors on
357 # @uses: ZPOOL ZFS TESTPOOL
358 function setup_mirrors
360 typeset
-i nmirrors
=$1
363 while ((nmirrors
> 0)); do
364 log_must
test -n "$1" -a -n "$2"
365 [[ -d /$TESTPOOL$nmirrors ]] && $RM -rf /$TESTPOOL$nmirrors
366 log_must
$ZPOOL create
-f $TESTPOOL$nmirrors mirror
$1 $2
368 ((nmirrors
= nmirrors
- 1))
373 # create a number of raidz pools.
374 # We create a number($1) of 2 raidz pools using the pairs of disks named
375 # on the command line. These pools are *not* mounted
376 # @parameters: $1 the number of pools to create
377 # $... the devices to use to create the pools on
378 # @uses: ZPOOL ZFS TESTPOOL
379 function setup_raidzs
381 typeset
-i nraidzs
=$1
384 while ((nraidzs
> 0)); do
385 log_must
test -n "$1" -a -n "$2"
386 [[ -d /$TESTPOOL$nraidzs ]] && $RM -rf /$TESTPOOL$nraidzs
387 log_must
$ZPOOL create
-f $TESTPOOL$nraidzs raidz
$1 $2
389 ((nraidzs
= nraidzs
- 1))
394 # Destroy the configured testpool mirrors.
395 # the mirrors are of the form ${TESTPOOL}{number}
396 # @uses: ZPOOL ZFS TESTPOOL
397 function destroy_mirrors
399 default_cleanup_noexit
405 # Given a minimum of two disks, set up a storage pool and dataset for the raid-z
406 # $1 the list of disks
408 function default_raidz_setup
410 typeset disklist
="$*"
411 disks
=(${disklist[*]})
413 if [[ ${#disks[*]} -lt 2 ]]; then
414 log_fail
"A raid-z requires a minimum of two disks."
417 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
418 log_must
$ZPOOL create
-f $TESTPOOL raidz
$1 $2 $3
419 log_must
$ZFS create
$TESTPOOL/$TESTFS
420 log_must
$ZFS set mountpoint
=$TESTDIR $TESTPOOL/$TESTFS
426 # Common function used to cleanup storage pools and datasets.
428 # Invoked at the start of the test suite to ensure the system
429 # is in a known state, and also at the end of each set of
430 # sub-tests to ensure errors from one set of tests doesn't
431 # impact the execution of the next set.
433 function default_cleanup
435 default_cleanup_noexit
440 function default_cleanup_noexit
445 # Destroying the pool will also destroy any
446 # filesystems it contains.
448 if is_global_zone
; then
449 $ZFS unmount
-a > /dev
/null
2>&1
450 [[ -z "$KEEP" ]] && KEEP
="rpool"
451 exclude
=`eval $ECHO \"'(${KEEP})'\"`
452 ALL_POOLS
=$
($ZPOOL list
-H -o name \
453 |
$GREP -v "$NO_POOLS" |
$EGREP -vw "$exclude")
454 # Here, we loop through the pools we're allowed to
455 # destroy, only destroying them if it's safe to do
457 while [ ! -z ${ALL_POOLS} ]
459 for pool
in ${ALL_POOLS}
461 if safe_to_destroy_pool
$pool ;
465 ALL_POOLS
=$
($ZPOOL list
-H -o name \
466 |
$GREP -v "$NO_POOLS" \
467 |
$EGREP -v "$exclude")
474 for fs
in $
($ZFS list
-H -o name \
475 |
$GREP "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
476 datasetexists
$fs && \
477 log_must
$ZFS destroy
-Rf $fs
480 # Need cleanup here to avoid garbage dir left.
481 for fs
in $
($ZFS list
-H -o name
); do
482 [[ $fs == /$ZONE_POOL ]] && continue
483 [[ -d $fs ]] && log_must
$RM -rf $fs/*
487 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
490 for fs
in $
($ZFS list
-H -o name
); do
491 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
492 log_must
$ZFS set reservation
=none
$fs
493 log_must
$ZFS set recordsize
=128K
$fs
494 log_must
$ZFS set mountpoint
=/$fs $fs
496 enc
=$
(get_prop encryption
$fs)
497 if [[ $?
-ne 0 ]] ||
[[ -z "$enc" ]] || \
498 [[ "$enc" == "off" ]]; then
499 log_must
$ZFS set checksum
=on
$fs
501 log_must
$ZFS set compression
=off
$fs
502 log_must
$ZFS set atime
=on
$fs
503 log_must
$ZFS set devices
=off
$fs
504 log_must
$ZFS set exec=on
$fs
505 log_must
$ZFS set setuid
=on
$fs
506 log_must
$ZFS set readonly=off
$fs
507 log_must
$ZFS set snapdir
=hidden
$fs
508 log_must
$ZFS set aclmode
=groupmask
$fs
509 log_must
$ZFS set aclinherit
=secure
$fs
514 [[ -d $TESTDIR ]] && \
515 log_must
$RM -rf $TESTDIR
518 if is_mpath_device
$disk1; then
525 # Common function used to cleanup storage pools, file systems
528 function default_container_cleanup
530 if ! is_global_zone
; then
534 ismounted
$TESTPOOL/$TESTCTR/$TESTFS1
536 log_must
$ZFS unmount
$TESTPOOL/$TESTCTR/$TESTFS1
538 datasetexists
$TESTPOOL/$TESTCTR/$TESTFS1 && \
539 log_must
$ZFS destroy
-R $TESTPOOL/$TESTCTR/$TESTFS1
541 datasetexists
$TESTPOOL/$TESTCTR && \
542 log_must
$ZFS destroy
-Rf $TESTPOOL/$TESTCTR
544 [[ -e $TESTDIR1 ]] && \
545 log_must
$RM -rf $TESTDIR1 > /dev
/null
2>&1
551 # Common function used to cleanup snapshot of file system or volume. Default to
552 # delete the file system's snapshot
556 function destroy_snapshot
558 typeset snap
=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
560 if ! snapexists
$snap; then
561 log_fail
"'$snap' does not existed."
565 # For the sake of the value which come from 'get_prop' is not equal
566 # to the really mountpoint when the snapshot is unmounted. So, firstly
567 # check and make sure this snapshot's been mounted in current system.
570 if ismounted
$snap; then
571 mtpt
=$
(get_prop mountpoint
$snap)
573 log_fail
"get_prop mountpoint $snap failed."
576 log_must
$ZFS destroy
$snap
577 [[ $mtpt != "" && -d $mtpt ]] && \
578 log_must
$RM -rf $mtpt
582 # Common function used to cleanup clone.
586 function destroy_clone
588 typeset clone
=${1:-$TESTPOOL/$TESTCLONE}
590 if ! datasetexists
$clone; then
591 log_fail
"'$clone' does not existed."
594 # With the same reason in destroy_snapshot
596 if ismounted
$clone; then
597 mtpt
=$
(get_prop mountpoint
$clone)
599 log_fail
"get_prop mountpoint $clone failed."
602 log_must
$ZFS destroy
$clone
603 [[ $mtpt != "" && -d $mtpt ]] && \
604 log_must
$RM -rf $mtpt
608 # Common function used to cleanup bookmark of file system or volume. Default
609 # to delete the file system's bookmark.
613 function destroy_bookmark
615 typeset bkmark
=${1:-$TESTPOOL/$TESTFS#$TESTBKMARK}
617 if ! bkmarkexists
$bkmark; then
618 log_fail
"'$bkmarkp' does not existed."
621 log_must
$ZFS destroy
$bkmark
624 # Return 0 if a snapshot exists; $? otherwise
630 $ZFS list
-H -t snapshot
"$1" > /dev
/null
2>&1
635 # Return 0 if a bookmark exists; $? otherwise
639 function bkmarkexists
641 $ZFS list
-H -t bookmark
"$1" > /dev
/null
2>&1
646 # Set a property to a certain value on a dataset.
647 # Sets a property of the dataset to the value as passed in.
649 # $1 dataset who's property is being set
651 # $3 value to set property to
653 # 0 if the property could be set.
654 # non-zero otherwise.
657 function dataset_setprop
659 typeset fn
=dataset_setprop
662 log_note
"$fn: Insufficient parameters (need 3, had $#)"
666 output
=$
($ZFS set $2=$3 $1 2>&1)
669 log_note
"Setting property on $1 failed."
670 log_note
"property $2=$3"
671 log_note
"Return Code: $rv"
672 log_note
"Output: $output"
679 # Assign suite defined dataset properties.
680 # This function is used to apply the suite's defined default set of
681 # properties to a dataset.
682 # @parameters: $1 dataset to use
683 # @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
685 # 0 if the dataset has been altered.
686 # 1 if no pool name was passed in.
687 # 2 if the dataset could not be found.
688 # 3 if the dataset could not have it's properties set.
690 function dataset_set_defaultproperties
694 [[ -z $dataset ]] && return 1
698 for confset
in $
($ZFS list
); do
699 if [[ $dataset = $confset ]]; then
704 [[ $found -eq 0 ]] && return 2
705 if [[ -n $COMPRESSION_PROP ]]; then
706 dataset_setprop
$dataset compression
$COMPRESSION_PROP || \
708 log_note
"Compression set to '$COMPRESSION_PROP' on $dataset"
710 if [[ -n $CHECKSUM_PROP ]]; then
711 dataset_setprop
$dataset checksum
$CHECKSUM_PROP || \
713 log_note
"Checksum set to '$CHECKSUM_PROP' on $dataset"
719 # Check a numeric assertion
720 # @parameter: $@ the assertion to check
721 # @output: big loud notice if assertion failed
726 (($@
)) || log_fail
"$@"
730 # Function to format partition size of a disk
731 # Given a disk cxtxdx reduces all partitions
734 function zero_partitions
#<whole_disk_name>
740 log_must
$FORMAT $DEV_DSKDIR/$diskname -s -- mklabel gpt
742 for i
in 0 1 3 4 5 6 7
744 set_partition
$i "" 0mb
$diskname
750 # Given a slice, size and disk, this function
751 # formats the slice to the specified size.
752 # Size should be specified with units as per
753 # the `format` command requirements eg. 100mb 3gb
755 # NOTE: This entire interface is problematic for the Linux parted utilty
756 # which requires the end of the partition to be specified. It would be
757 # best to retire this interface and replace it with something more flexible.
758 # At the moment a best effort is made.
760 function set_partition
#<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
762 typeset
-i slicenum
=$1
766 [[ -z $slicenum ||
-z $size ||
-z $disk ]] && \
767 log_fail
"The slice, size or disk name is unspecified."
770 typeset size_mb
=${size%%[mMgG]}
772 size_mb
=${size_mb%%[mMgG][bB]}
773 if [[ ${size:1:1} == 'g' ]]; then
774 ((size_mb
= size_mb
* 1024))
777 # Create GPT partition table when setting slice 0 or
778 # when the device doesn't already contain a GPT label.
779 $FORMAT $DEV_DSKDIR/$disk -s -- print
1 >/dev
/null
781 if [[ $slicenum -eq 0 ||
$ret_val -ne 0 ]]; then
782 log_must
$FORMAT $DEV_DSKDIR/$disk -s -- mklabel gpt
785 # When no start is given align on the first cylinder.
786 if [[ -z "$start" ]]; then
790 # Determine the cylinder size for the device and using
791 # that calculate the end offset in cylinders.
792 typeset
-i cly_size_kb
=0
793 cly_size_kb
=$
($FORMAT -m $DEV_DSKDIR/$disk -s -- \
794 unit cyl print |
$HEAD -3 |
$TAIL -1 | \
795 $AWK -F '[:k.]' '{print $4}')
796 ((end
= (size_mb
* 1024 / cly_size_kb
) + start
))
798 log_must
$FORMAT $DEV_DSKDIR/$disk -s -- \
799 mkpart part
$slicenum ${start}cyl
${end}cyl
801 $BLOCKDEV --rereadpt $DEV_DSKDIR/$disk 2>/dev
/null
804 typeset format_file
=/var
/tmp
/format_in.$$
806 $ECHO "partition" >$format_file
807 $ECHO "$slicenum" >> $format_file
808 $ECHO "" >> $format_file
809 $ECHO "" >> $format_file
810 $ECHO "$start" >> $format_file
811 $ECHO "$size" >> $format_file
812 $ECHO "label" >> $format_file
813 $ECHO "" >> $format_file
814 $ECHO "q" >> $format_file
815 $ECHO "q" >> $format_file
817 $FORMAT -e -s -d $disk -f $format_file
821 [[ $ret_val -ne 0 ]] && \
822 log_fail
"Unable to format $disk slice $slicenum to $size"
827 # Delete all partitions on all disks - this is specifically for the use of multipath
828 # devices which currently can only be used in the test suite as raw/un-partitioned
829 # devices (ie a zpool cannot be created on a whole mpath device that has partitions)
831 function delete_partitions
835 if [[ -z $DISK_ARRAY_NUM ]]; then
836 DISK_ARRAY_NUM
=$
($ECHO ${DISKS} |
$NAWK '{print NF}')
838 if [[ -z $DISKSARRAY ]]; then
843 if (( $DISK_ARRAY_NUM == 1 )); then
844 while ((j
< MAX_PARTITIONS
)); do
845 $FORMAT $DEV_DSKDIR/$DISK -s rm $j > /dev
/null
2>&1
846 if (( $?
== 1 )); then
847 $LSBLK |
$EGREP ${DISK}${SLICE_PREFIX}${j} > /dev
/null
848 if (( $?
== 1 )); then
849 log_note
"Partitions for $DISK should be deleted"
851 log_fail
"Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
855 $LSBLK |
$EGREP ${DISK}${SLICE_PREFIX}${j} > /dev
/null
856 if (( $?
== 0 )); then
857 log_fail
"Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
863 for disk
in `$ECHO $DISKSARRAY`; do
864 while ((j
< MAX_PARTITIONS
)); do
865 $FORMAT $DEV_DSKDIR/$disk -s rm $j > /dev
/null
2>&1
866 if (( $?
== 1 )); then
867 $LSBLK |
$EGREP ${disk}${SLICE_PREFIX}${j} > /dev
/null
868 if (( $?
== 1 )); then
869 log_note
"Partitions for $disk should be deleted"
871 log_fail
"Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
875 $LSBLK |
$EGREP ${disk}${SLICE_PREFIX}${j} > /dev
/null
876 if (( $?
== 0 )); then
877 log_fail
"Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
890 # Get the end cyl of the given slice
892 function get_endslice
#<disk> <slice>
896 if [[ -z $disk ||
-z $slice ]] ; then
897 log_fail
"The disk name or slice number is unspecified."
901 endcyl
=$
($FORMAT -s $DEV_DSKDIR/$disk -- unit cyl print | \
902 $GREP "part${slice}" | \
903 $AWK '{print $3}' | \
905 ((endcyl
= (endcyl
+ 1)))
907 disk
=${disk#/dev/dsk/}
908 disk
=${disk#/dev/rdsk/}
912 ratio
=$
($PRTVTOC /dev
/rdsk
/${disk}s2 | \
913 $GREP "sectors\/cylinder" | \
916 if ((ratio
== 0)); then
920 typeset
-i endcyl
=$
($PRTVTOC -h /dev
/rdsk
/${disk}s2 |
921 $NAWK -v token
="$slice" '{if ($1==token) print $6}')
923 ((endcyl
= (endcyl
+ 1) / ratio
))
931 # Given a size,disk and total slice number, this function formats the
932 # disk slices from 0 to the total slice number with the same specified
935 function partition_disk
#<slice_size> <whole_disk_name> <total_slices>
938 typeset slice_size
=$1
940 typeset total_slices
=$3
943 zero_partitions
$disk_name
944 while ((i
< $total_slices)); do
951 set_partition
$i "$cyl" $slice_size $disk_name
952 cyl
=$
(get_endslice
$disk_name $i)
958 # This function continues to write to a filenum number of files into dirnum
959 # number of directories until either $FILE_WRITE returns an error or the
960 # maximum number of files per directory have been written.
963 # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
965 # Return value: 0 on success
969 # destdir: is the directory where everything is to be created under
970 # dirnum: the maximum number of subdirectories to use, -1 no limit
971 # filenum: the maximum number of files per subdirectory
972 # bytes: number of bytes to write
973 # num_writes: numer of types to write out bytes
974 # data: the data that will be written
977 # file_fs /testdir 20 25 1024 256 0
979 # Note: bytes * num_writes equals the size of the testfile
981 function fill_fs
# destdir dirnum filenum bytes num_writes data
983 typeset destdir
=${1:-$TESTDIR}
984 typeset
-i dirnum
=${2:-50}
985 typeset
-i filenum
=${3:-50}
986 typeset
-i bytes
=${4:-8192}
987 typeset
-i num_writes
=${5:-10240}
988 typeset
-i data
=${6:-0}
995 log_must
$MKDIR -p $destdir/$idirnum
996 while (($odirnum > 0)); do
997 if ((dirnum
>= 0 && idirnum
>= dirnum
)); then
1001 $FILE_WRITE -o create
-f $destdir/$idirnum/$TESTFILE.
$fn \
1002 -b $bytes -c $num_writes -d $data
1004 if (($retval != 0)); then
1008 if (($fn >= $filenum)); then
1010 ((idirnum
= idirnum
+ 1))
1011 log_must
$MKDIR -p $destdir/$idirnum
1020 # Simple function to get the specified property. If unable to
1021 # get the property then exits.
1023 # Note property is in 'parsable' format (-p)
1025 function get_prop
# property dataset
1031 prop_val
=$
($ZFS get
-pH -o value
$prop $dataset 2>/dev
/null
)
1032 if [[ $?
-ne 0 ]]; then
1033 log_note
"Unable to get $prop property for dataset " \
1043 # Simple function to get the specified property of pool. If unable to
1044 # get the property then exits.
1046 function get_pool_prop
# property pool
1052 if poolexists
$pool ; then
1053 prop_val
=$
($ZPOOL get
$prop $pool 2>/dev
/null |
$TAIL -1 | \
1055 if [[ $?
-ne 0 ]]; then
1056 log_note
"Unable to get $prop property for pool " \
1061 log_note
"Pool $pool not exists."
1069 # Return 0 if a pool exists; $? otherwise
1077 if [[ -z $pool ]]; then
1078 log_note
"No pool name given."
1082 $ZPOOL get name
"$pool" > /dev
/null
2>&1
1086 # Return 0 if all the specified datasets exist; $? otherwise
1089 function datasetexists
1091 if (($# == 0)); then
1092 log_note
"No dataset name given."
1096 while (($# > 0)); do
1097 $ZFS get name
$1 > /dev
/null
2>&1 || \
1105 # return 0 if none of the specified datasets exists, otherwise return 1.
1108 function datasetnonexists
1110 if (($# == 0)); then
1111 log_note
"No dataset name given."
1115 while (($# > 0)); do
1116 $ZFS list
-H -t filesystem
,snapshot
,volume
$1 > /dev
/null
2>&1 \
1125 # Given a mountpoint, or a dataset name, determine if it is shared via NFS.
1127 # Returns 0 if shared, 1 otherwise.
1134 if [[ $fs != "/"* ]] ; then
1135 if datasetnonexists
"$fs" ; then
1138 mtpt
=$
(get_prop mountpoint
"$fs")
1140 none|legacy|
-) return 1
1149 for mtpt
in `$SHARE | $AWK '{print $1}'` ; do
1150 if [[ $mtpt == $fs ]] ; then
1157 for mtpt
in `$SHARE | $AWK '{print $2}'` ; do
1158 if [[ $mtpt == $fs ]] ; then
1163 typeset stat
=$
($SVCS -H -o STA nfs
/server
:default
)
1164 if [[ $stat != "ON" ]]; then
1165 log_note
"Current nfs/server status: $stat"
1172 # Given a dataset name determine if it is shared via SMB.
1174 # Returns 0 if shared, 1 otherwise.
1176 function is_shared_smb
1181 if datasetnonexists
"$fs" ; then
1184 fs
=$
(echo $fs |
sed 's@/@_@g')
1188 for mtpt
in `$NET usershare list | $AWK '{print $1}'` ; do
1189 if [[ $mtpt == $fs ]] ; then
1195 log_unsupported
"Currently unsupported by the test framework"
1201 # Given a mountpoint, determine if it is not shared via NFS.
1203 # Returns 0 if not shared, 1 otherwise.
1210 if (($?
== 0)); then
1218 # Given a dataset determine if it is not shared via SMB.
1220 # Returns 0 if not shared, 1 otherwise.
1222 function not_shared_smb
1227 if (($?
== 0)); then
1235 # Helper function to unshare a mountpoint.
1237 function unshare_fs
#fs
1241 is_shared
$fs || is_shared_smb
$fs
1242 if (($?
== 0)); then
1243 log_must
$ZFS unshare
$fs
1250 # Helper function to share a NFS mountpoint.
1252 function share_nfs
#fs
1258 if (($?
!= 0)); then
1259 log_must
$SHARE "*:$fs"
1263 if (($?
!= 0)); then
1264 log_must
$SHARE -F nfs
$fs
1272 # Helper function to unshare a NFS mountpoint.
1274 function unshare_nfs
#fs
1280 if (($?
== 0)); then
1281 log_must
$UNSHARE -u "*:$fs"
1285 if (($?
== 0)); then
1286 log_must
$UNSHARE -F nfs
$fs
1294 # Helper function to show NFS shares.
1296 function showshares_nfs
1308 # Helper function to show SMB shares.
1310 function showshares_smb
1322 # Check NFS server status and trigger it online.
1324 function setup_nfs_server
1326 # Cannot share directory in non-global zone.
1328 if ! is_global_zone
; then
1329 log_note
"Cannot trigger NFS server by sharing in LZ."
1334 log_note
"NFS server must started prior to running test framework."
1338 typeset nfs_fmri
="svc:/network/nfs/server:default"
1339 if [[ $
($SVCS -Ho STA
$nfs_fmri) != "ON" ]]; then
1341 # Only really sharing operation can enable NFS server
1342 # to online permanently.
1344 typeset dummy
=/tmp
/dummy
1346 if [[ -d $dummy ]]; then
1347 log_must
$RM -rf $dummy
1350 log_must
$MKDIR $dummy
1351 log_must
$SHARE $dummy
1354 # Waiting for fmri's status to be the final status.
1355 # Otherwise, in transition, an asterisk (*) is appended for
1356 # instances, unshare will reverse status to 'DIS' again.
1358 # Waiting for 1's at least.
1362 while [[ timeout
-ne 0 && $
($SVCS -Ho STA
$nfs_fmri) == *'*' ]]
1369 log_must
$UNSHARE $dummy
1370 log_must
$RM -rf $dummy
1373 log_note
"Current NFS status: '$($SVCS -Ho STA,FMRI $nfs_fmri)'"
1377 # To verify whether calling process is in global zone
1379 # Return 0 if in global zone, 1 in non-global zone
1381 function is_global_zone
1383 typeset cur_zone
=$
($ZONENAME 2>/dev
/null
)
1384 if [[ $cur_zone != "global" ]]; then
1391 # Verify whether test is permitted to run from
1392 # global zone, local zone, or both
1394 # $1 zone limit, could be "global", "local", or "both"(no limit)
1396 # Return 0 if permitted, otherwise exit with log_unsupported
1398 function verify_runnable
# zone limit
1402 [[ -z $limit ]] && return 0
1404 if is_global_zone
; then
1408 local) log_unsupported
"Test is unable to run from "\
1411 *) log_note
"Warning: unknown limit $limit - " \
1419 global
) log_unsupported
"Test is unable to run from "\
1422 *) log_note
"Warning: unknown limit $limit - " \
1433 # Return 0 if create successfully or the pool exists; $? otherwise
1434 # Note: In local zones, this function should return 0 silently.
1437 # $2-n - [keyword] devs_list
1439 function create_pool
#pool devs_list
1441 typeset pool
=${1%%/*}
1445 if [[ -z $pool ]]; then
1446 log_note
"Missing pool name."
1450 if poolexists
$pool ; then
1454 if is_global_zone
; then
1455 [[ -d /$pool ]] && $RM -rf /$pool
1456 log_must
$ZPOOL create
-f $pool $@
1462 # Return 0 if destroy successfully or the pool exists; $? otherwise
1463 # Note: In local zones, this function should return 0 silently.
1466 # Destroy pool with the given parameters.
1468 function destroy_pool
#pool
1470 typeset pool
=${1%%/*}
1473 if [[ -z $pool ]]; then
1474 log_note
"No pool name given."
1478 if is_global_zone
; then
1479 if poolexists
"$pool" ; then
1480 mtpt
=$
(get_prop mountpoint
"$pool")
1482 # At times, syseventd activity can cause attempts to
1483 # destroy a pool to fail with EBUSY. We retry a few
1484 # times allowing failures before requiring the destroy
1486 typeset
-i wait_time
=10 ret
=1 count
=0
1488 while [[ $ret -ne 0 ]]; do
1489 $must $ZPOOL destroy
-f $pool
1491 [[ $ret -eq 0 ]] && break
1492 log_note
"zpool destroy failed with $ret"
1493 [[ count
++ -ge 7 ]] && must
=log_must
1498 log_must
$RM -rf $mtpt
1500 log_note
"Pool does not exist. ($pool)"
1509 # Firstly, create a pool with 5 datasets. Then, create a single zone and
1510 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
1511 # and a zvol device to the zone.
1514 # $2 zone root directory prefix
1517 function zfs_zones_setup
#zone_name zone_root zone_ip
1519 typeset zone_name
=${1:-$(hostname)-z}
1520 typeset zone_root
=${2:-"/zone_root"}
1521 typeset zone_ip
=${3:-"10.1.1.10"}
1522 typeset prefix_ctr
=$ZONE_CTR
1523 typeset pool_name
=$ZONE_POOL
1527 # Create pool and 5 container within it
1529 [[ -d /$pool_name ]] && $RM -rf /$pool_name
1530 log_must
$ZPOOL create
-f $pool_name $DISKS
1531 while ((i
< cntctr
)); do
1532 log_must
$ZFS create
$pool_name/$prefix_ctr$i
1537 log_must
$ZFS create
-V 1g
$pool_name/zone_zvol
1541 # If current system support slog, add slog device for pool
1543 if verify_slog_support
; then
1544 typeset sdevs
="/var/tmp/sdev1 /var/tmp/sdev2"
1545 log_must
$MKFILE 100M
$sdevs
1546 log_must
$ZPOOL add
$pool_name log mirror
$sdevs
1549 # this isn't supported just yet.
1550 # Create a filesystem. In order to add this to
1551 # the zone, it must have it's mountpoint set to 'legacy'
1552 # log_must $ZFS create $pool_name/zfs_filesystem
1553 # log_must $ZFS set mountpoint=legacy $pool_name/zfs_filesystem
1555 [[ -d $zone_root ]] && \
1556 log_must
$RM -rf $zone_root/$zone_name
1557 [[ ! -d $zone_root ]] && \
1558 log_must
$MKDIR -p -m 0700 $zone_root/$zone_name
1560 # Create zone configure file and configure the zone
1562 typeset zone_conf
=/tmp
/zone_conf.$$
1563 $ECHO "create" > $zone_conf
1564 $ECHO "set zonepath=$zone_root/$zone_name" >> $zone_conf
1565 $ECHO "set autoboot=true" >> $zone_conf
1567 while ((i
< cntctr
)); do
1568 $ECHO "add dataset" >> $zone_conf
1569 $ECHO "set name=$pool_name/$prefix_ctr$i" >> \
1571 $ECHO "end" >> $zone_conf
1575 # add our zvol to the zone
1576 $ECHO "add device" >> $zone_conf
1577 $ECHO "set match=$ZVOL_DEVDIR/$pool_name/zone_zvol" >> $zone_conf
1578 $ECHO "end" >> $zone_conf
1580 # add a corresponding zvol rdsk to the zone
1581 $ECHO "add device" >> $zone_conf
1582 $ECHO "set match=$ZVOL_RDEVDIR/$pool_name/zone_zvol" >> $zone_conf
1583 $ECHO "end" >> $zone_conf
1585 # once it's supported, we'll add our filesystem to the zone
1586 # $ECHO "add fs" >> $zone_conf
1587 # $ECHO "set type=zfs" >> $zone_conf
1588 # $ECHO "set special=$pool_name/zfs_filesystem" >> $zone_conf
1589 # $ECHO "set dir=/export/zfs_filesystem" >> $zone_conf
1590 # $ECHO "end" >> $zone_conf
1592 $ECHO "verify" >> $zone_conf
1593 $ECHO "commit" >> $zone_conf
1594 log_must
$ZONECFG -z $zone_name -f $zone_conf
1595 log_must
$RM -f $zone_conf
1598 $ZONEADM -z $zone_name install
1599 if (($?
== 0)); then
1600 log_note
"SUCCESS: $ZONEADM -z $zone_name install"
1602 log_fail
"FAIL: $ZONEADM -z $zone_name install"
1605 # Install sysidcfg file
1607 typeset sysidcfg
=$zone_root/$zone_name/root
/etc
/sysidcfg
1608 $ECHO "system_locale=C" > $sysidcfg
1609 $ECHO "terminal=dtterm" >> $sysidcfg
1610 $ECHO "network_interface=primary {" >> $sysidcfg
1611 $ECHO "hostname=$zone_name" >> $sysidcfg
1612 $ECHO "}" >> $sysidcfg
1613 $ECHO "name_service=NONE" >> $sysidcfg
1614 $ECHO "root_password=mo791xfZ/SFiw" >> $sysidcfg
1615 $ECHO "security_policy=NONE" >> $sysidcfg
1616 $ECHO "timezone=US/Eastern" >> $sysidcfg
1619 log_must
$ZONEADM -z $zone_name boot
1623 # Reexport TESTPOOL & TESTPOOL(1-4)
1625 function reexport_pool
1630 while ((i
< cntctr
)); do
1632 TESTPOOL
=$ZONE_POOL/$ZONE_CTR$i
1633 if ! ismounted
$TESTPOOL; then
1634 log_must
$ZFS mount
$TESTPOOL
1637 eval TESTPOOL
$i=$ZONE_POOL/$ZONE_CTR$i
1638 if eval ! ismounted \
$TESTPOOL$i; then
1639 log_must
eval $ZFS mount \
$TESTPOOL$i
1647 # Verify a given disk is online or offline
1649 # Return 0 is pool/disk matches expected state, 1 otherwise
1651 function check_state
# pool disk state{online,offline}
1654 typeset disk
=${2#$DEV_DSKDIR/}
1657 $ZPOOL status
-v $pool |
grep "$disk" \
1658 |
grep -i "$state" > /dev
/null
2>&1
1664 # Get the mountpoint of snapshot
1665 # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1668 function snapshot_mountpoint
1670 typeset dataset
=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1672 if [[ $dataset != *@
* ]]; then
1673 log_fail
"Error name of snapshot '$dataset'."
1676 typeset fs
=${dataset%@*}
1677 typeset snap
=${dataset#*@}
1679 if [[ -z $fs ||
-z $snap ]]; then
1680 log_fail
"Error name of snapshot '$dataset'."
1683 $ECHO $
(get_prop mountpoint
$fs)/.zfs
/snapshot
/$snap
1687 # Given a pool and file system, this function will verify the file system
1688 # using the zdb internal tool. Note that the pool is exported and imported
1689 # to ensure it has consistent state.
1691 function verify_filesys
# pool filesystem dir
1694 typeset filesys
="$2"
1695 typeset zdbout
="/tmp/zdbout.$$"
1700 typeset search_path
=""
1702 log_note
"Calling $ZDB to verify filesystem '$filesys'"
1703 $ZFS unmount
-a > /dev
/null
2>&1
1704 log_must
$ZPOOL export $pool
1706 if [[ -n $dirs ]] ; then
1707 for dir
in $dirs ; do
1708 search_path
="$search_path -d $dir"
1712 log_must
$ZPOOL import
$search_path $pool
1714 $ZDB -cudi $filesys > $zdbout 2>&1
1715 if [[ $?
!= 0 ]]; then
1716 log_note
"Output: $ZDB -cudi $filesys"
1718 log_fail
"$ZDB detected errors with: '$filesys'"
1721 log_must
$ZFS mount
-a
1722 log_must
$RM -rf $zdbout
1726 # Given a pool, and this function list all disks in the pool
1728 function get_disklist
# pool
1732 disklist
=$
($ZPOOL iostat
-v $1 |
$NAWK '(NR >4) {print $1}' | \
1733 $GREP -v "\-\-\-\-\-" | \
1734 $EGREP -v -e "^(mirror|raidz1|raidz2|spare|log|cache)$")
1740 # Given a pool, and this function list all disks in the pool with their full
1741 # path (like "/dev/sda" instead of "sda").
1743 function get_disklist_fullpath
# pool
1752 # This function kills a given list of processes after a time period. We use
1753 # this in the stress tests instead of STF_TIMEOUT so that we can have processes
1754 # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1755 # would be listed as FAIL, which we don't want : we're happy with stress tests
1756 # running for a certain amount of time, then finishing.
1758 # @param $1 the time in seconds after which we should terminate these processes
1759 # @param $2..$n the processes we wish to terminate.
1761 function stress_timeout
1763 typeset
-i TIMEOUT
=$1
1767 log_note
"Waiting for child processes($cpids). " \
1768 "It could last dozens of minutes, please be patient ..."
1769 log_must
$SLEEP $TIMEOUT
1771 log_note
"Killing child processes after ${TIMEOUT} stress timeout."
1773 for pid
in $cpids; do
1774 $PS -p $pid > /dev
/null
2>&1
1775 if (($?
== 0)); then
1776 log_must
$KILL -USR1 $pid
1782 # Verify a given hotspare disk is inuse or avail
1784 # Return 0 is pool/disk matches expected state, 1 otherwise
1786 function check_hotspare_state
# pool disk state{inuse,avail}
1789 typeset disk
=${2#$DEV_DSKDIR/}
1792 cur_state
=$
(get_device_state
$pool $disk "spares")
1794 if [[ $state != ${cur_state} ]]; then
1801 # Verify a given slog disk is inuse or avail
1803 # Return 0 is pool/disk matches expected state, 1 otherwise
1805 function check_slog_state
# pool disk state{online,offline,unavail}
1808 typeset disk
=${2#$DEV_DSKDIR/}
1811 cur_state
=$
(get_device_state
$pool $disk "logs")
1813 if [[ $state != ${cur_state} ]]; then
1820 # Verify a given vdev disk is inuse or avail
1822 # Return 0 is pool/disk matches expected state, 1 otherwise
1824 function check_vdev_state
# pool disk state{online,offline,unavail}
1827 typeset disk
=${2#$/DEV_DSKDIR/}
1830 cur_state
=$
(get_device_state
$pool $disk)
1832 if [[ $state != ${cur_state} ]]; then
1839 # Check the output of 'zpool status -v <pool>',
1840 # and to see if the content of <token> contain the <keyword> specified.
1842 # Return 0 is contain, 1 otherwise
1844 function check_pool_status
# pool token keyword
1850 $ZPOOL status
-v "$pool" 2>/dev
/null |
$NAWK -v token
="$token:" '
1851 ($1==token) {print $0}' \
1852 |
$GREP -i "$keyword" > /dev
/null
2>&1
1858 # These 5 following functions are instance of check_pool_status()
1859 # is_pool_resilvering - to check if the pool is resilver in progress
1860 # is_pool_resilvered - to check if the pool is resilver completed
1861 # is_pool_scrubbing - to check if the pool is scrub in progress
1862 # is_pool_scrubbed - to check if the pool is scrub completed
1863 # is_pool_scrub_stopped - to check if the pool is scrub stopped
1865 function is_pool_resilvering
#pool
1867 check_pool_status
"$1" "scan" "resilver in progress since "
1871 function is_pool_resilvered
#pool
1873 check_pool_status
"$1" "scan" "resilvered "
1877 function is_pool_scrubbing
#pool
1879 check_pool_status
"$1" "scan" "scrub in progress since "
1883 function is_pool_scrubbed
#pool
1885 check_pool_status
"$1" "scan" "scrub repaired"
1889 function is_pool_scrub_stopped
#pool
1891 check_pool_status
"$1" "scan" "scrub canceled"
1896 # Use create_pool()/destroy_pool() to clean up the information in
1897 # in the given disk to avoid slice overlapping.
1899 function cleanup_devices
#vdevs
1901 typeset pool
="foopool$$"
1903 if poolexists
$pool ; then
1907 create_pool
$pool $@
1914 # Verify the rsh connectivity to each remote host in RHOSTS.
1916 # Return 0 if remote host is accessible; otherwise 1.
1917 # $1 remote host name
1920 function verify_rsh_connect
#rhost, username
1924 typeset rsh_cmd
="$RSH -n"
1927 $GETENT hosts
$rhost >/dev
/null
2>&1
1928 if (($?
!= 0)); then
1929 log_note
"$rhost cannot be found from" \
1930 "administrative database."
1934 $PING $rhost 3 >/dev
/null
2>&1
1935 if (($?
!= 0)); then
1936 log_note
"$rhost is not reachable."
1940 if ((${#username} != 0)); then
1941 rsh_cmd
="$rsh_cmd -l $username"
1942 cur_user
="given user \"$username\""
1944 cur_user
="current user \"`$LOGNAME`\""
1947 if ! $rsh_cmd $rhost $TRUE; then
1948 log_note
"$RSH to $rhost is not accessible" \
1957 # Verify the remote host connection via rsh after rebooting
1960 function verify_remote
1965 # The following loop waits for the remote system rebooting.
1966 # Each iteration will wait for 150 seconds. there are
1967 # total 5 iterations, so the total timeout value will
1968 # be 12.5 minutes for the system rebooting. This number
1969 # is an approxiate number.
1972 while ! verify_rsh_connect
$rhost; do
1974 ((count
= count
+ 1))
1975 if ((count
> 5)); then
1983 # Replacement function for /usr/bin/rsh. This function will include
1984 # the /usr/bin/rsh and meanwhile return the execution status of the
1987 # $1 usrname passing down to -l option of /usr/bin/rsh
1988 # $2 remote machine hostname
1989 # $3... command string
2003 err_file
=/tmp
/${rhost}.$$.err
2004 if ((${#ruser} == 0)); then
2007 rsh_str
="$RSH -n -l $ruser"
2010 $rsh_str $rhost /bin
/ksh
-c "'$cmd_str; \
2011 print -u 2 \"status=\$?\"'" \
2012 >/dev
/null
2>$err_file
2014 if (($ret != 0)); then
2016 $RM -f $std_file $err_file
2017 log_fail
"$RSH itself failed with exit code $ret..."
2020 ret
=$
($GREP -v 'print -u 2' $err_file |
$GREP 'status=' | \
2022 (($ret != 0)) && $CAT $err_file >&2
2024 $RM -f $err_file >/dev
/null
2>&1
2029 # Get the SUNWstc-fs-zfs package installation path in a remote host
2030 # $1 remote host name
2032 function get_remote_pkgpath
2037 pkgpath
=$
($RSH -n $rhost "$PKGINFO -l SUNWstc-fs-zfs | $GREP BASEDIR: |\
2044 # A function to find and locate free disks on a system or from given
2045 # disks as the parameter. It works by locating disks that are in use
2046 # as swap devices and dump devices, and also disks listed in /etc/vfstab
2048 # $@ given disks to find which are free, default is all disks in
2051 # @return a string containing the list of available disks
2055 # Trust provided list, no attempt is made to locate unused devices.
2062 sfi
=/tmp
/swaplist.$$
2063 dmpi
=/tmp
/dumpdev.$$
2064 max_finddisksnum
=${MAX_FINDDISKSNUM:-6}
2067 $DUMPADM > $dmpi 2>/dev
/null
2069 # write an awk script that can process the output of format
2070 # to produce a list of disks we know about. Note that we have
2071 # to escape "$2" so that the shell doesn't interpret it while
2072 # we're creating the awk script.
2073 # -------------------
2074 $CAT > /tmp
/find_disks.
awk <<EOF
2083 if (searchdisks && \$2 !~ "^$"){
2089 /^AVAILABLE DISK SELECTIONS:/{
2093 #---------------------
2095 $CHMOD 755 /tmp
/find_disks.
awk
2096 disks
=${@:-$($ECHO "" | $FORMAT -e 2>/dev/null | /tmp/find_disks.awk)}
2097 $RM /tmp
/find_disks.
awk
2100 for disk
in $disks; do
2102 $GREP "${disk}[sp]" /etc
/mnttab
>/dev
/null
2103 (($?
== 0)) && continue
2105 $GREP "${disk}[sp]" $sfi >/dev
/null
2106 (($?
== 0)) && continue
2107 # check for dump device
2108 $GREP "${disk}[sp]" $dmpi >/dev
/null
2109 (($?
== 0)) && continue
2110 # check to see if this disk hasn't been explicitly excluded
2111 # by a user-set environment variable
2112 $ECHO "${ZFS_HOST_DEVICES_IGNORE}" |
$GREP "${disk}" > /dev
/null
2113 (($?
== 0)) && continue
2114 unused_candidates
="$unused_candidates $disk"
2119 # now just check to see if those disks do actually exist
2120 # by looking for a device pointing to the first slice in
2121 # each case. limit the number to max_finddisksnum
2123 for disk
in $unused_candidates; do
2124 if [ -b $DEV_DSKDIR/${disk}s0
]; then
2125 if [ $count -lt $max_finddisksnum ]; then
2126 unused
="$unused $disk"
2127 # do not impose limit if $@ is provided
2128 [[ -z $@
]] && ((count
= count
+ 1))
2133 # finally, return our disk list
2138 # Add specified user to specified group
2142 # $3 base of the homedir (optional)
2144 function add_user
#<group_name> <user_name> <basedir>
2148 typeset basedir
=${3:-"/var/tmp"}
2150 if ((${#gname} == 0 ||
${#uname} == 0)); then
2151 log_fail
"group name or user name are not defined."
2154 log_must
$USERADD -g $gname -d $basedir/$uname -m $uname
2156 # Add new users to the same group and the command line utils.
2157 # This allows them to be run out of the original users home
2158 # directory as long as it permissioned to be group readable.
2160 cmd_group
=$
(stat
--format="%G" $ZFS)
2161 log_must
$USERMOD -a -G $cmd_group $uname
2168 # Delete the specified user.
2171 # $2 base of the homedir (optional)
2173 function del_user
#<logname> <basedir>
2176 typeset basedir
=${2:-"/var/tmp"}
2178 if ((${#user} == 0)); then
2179 log_fail
"login name is necessary."
2182 if $ID $user > /dev
/null
2>&1; then
2183 log_must
$USERDEL $user
2186 [[ -d $basedir/$user ]] && $RM -fr $basedir/$user
2192 # Select valid gid and create specified group.
2196 function add_group
#<group_name>
2200 if ((${#group} == 0)); then
2201 log_fail
"group name is necessary."
2204 # Assign 100 as the base gid, a larger value is selected for
2205 # Linux because for many distributions 1000 and under are reserved.
2208 $GROUPADD $group > /dev
/null
2>&1
2219 $GROUPADD -g $gid $group > /dev
/null
2>&1
2223 # The gid is not unique
2232 # Delete the specified group.
2236 function del_group
#<group_name>
2239 if ((${#grp} == 0)); then
2240 log_fail
"group name is necessary."
2244 $GETENT group
$grp > /dev
/null
2>&1
2247 # Group does not exist.
2249 # Name already exists as a group name
2250 0) log_must
$GROUPDEL $grp ;;
2254 $GROUPMOD -n $grp $grp > /dev
/null
2>&1
2257 # Group does not exist.
2259 # Name already exists as a group name
2260 9) log_must
$GROUPDEL $grp ;;
2269 # This function will return true if it's safe to destroy the pool passed
2270 # as argument 1. It checks for pools based on zvols and files, and also
2271 # files contained in a pool that may have a different mountpoint.
2273 function safe_to_destroy_pool
{ # $1 the pool name
2276 typeset DONT_DESTROY
=""
2278 # We check that by deleting the $1 pool, we're not
2279 # going to pull the rug out from other pools. Do this
2280 # by looking at all other pools, ensuring that they
2281 # aren't built from files or zvols contained in this pool.
2283 for pool
in $
($ZPOOL list
-H -o name
)
2287 # this is a list of the top-level directories in each of the
2288 # files that make up the path to the files the pool is based on
2289 FILEPOOL
=$
($ZPOOL status
-v $pool |
$GREP /$1/ | \
2292 # this is a list of the zvols that make up the pool
2293 ZVOLPOOL
=$
($ZPOOL status
-v $pool |
$GREP "$ZVOL_DEVDIR/$1$" \
2294 |
$AWK '{print $1}')
2296 # also want to determine if it's a file-based pool using an
2297 # alternate mountpoint...
2298 POOL_FILE_DIRS
=$
($ZPOOL status
-v $pool | \
2299 $GREP / |
$AWK '{print $1}' | \
2300 $AWK -F/ '{print $2}' |
$GREP -v "dev")
2302 for pooldir
in $POOL_FILE_DIRS
2304 OUTPUT
=$
($ZFS list
-H -r -o mountpoint
$1 | \
2305 $GREP "${pooldir}$" |
$AWK '{print $1}')
2307 ALTMOUNTPOOL
="${ALTMOUNTPOOL}${OUTPUT}"
2311 if [ ! -z "$ZVOLPOOL" ]
2314 log_note
"Pool $pool is built from $ZVOLPOOL on $1"
2317 if [ ! -z "$FILEPOOL" ]
2320 log_note
"Pool $pool is built from $FILEPOOL on $1"
2323 if [ ! -z "$ALTMOUNTPOOL" ]
2326 log_note
"Pool $pool is built from $ALTMOUNTPOOL on $1"
2330 if [ -z "${DONT_DESTROY}" ]
2334 log_note
"Warning: it is not safe to destroy $1!"
2340 # Get the available ZFS compression options
2341 # $1 option type zfs_set|zfs_compress
2343 function get_compress_opts
2345 typeset COMPRESS_OPTS
2346 typeset GZIP_OPTS
="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2347 gzip-6 gzip-7 gzip-8 gzip-9"
2349 if [[ $1 == "zfs_compress" ]] ; then
2350 COMPRESS_OPTS
="on lzjb"
2351 elif [[ $1 == "zfs_set" ]] ; then
2352 COMPRESS_OPTS
="on off lzjb"
2354 typeset valid_opts
="$COMPRESS_OPTS"
2355 $ZFS get
2>&1 |
$GREP gzip >/dev
/null
2>&1
2356 if [[ $?
-eq 0 ]]; then
2357 valid_opts
="$valid_opts $GZIP_OPTS"
2363 # Verify zfs operation with -p option work as expected
2364 # $1 operation, value could be create, clone or rename
2365 # $2 dataset type, value could be fs or vol
2367 # $4 new dataset name
2369 function verify_opt_p_ops
2374 typeset newdataset
=$4
2376 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2377 log_fail
"$datatype is not supported."
2380 # check parameters accordingly
2385 if [[ $datatype == "vol" ]]; then
2386 ops
="create -V $VOLSIZE"
2390 if [[ -z $newdataset ]]; then
2391 log_fail
"newdataset should not be empty" \
2394 log_must datasetexists
$dataset
2395 log_must snapexists
$dataset
2398 if [[ -z $newdataset ]]; then
2399 log_fail
"newdataset should not be empty" \
2402 log_must datasetexists
$dataset
2403 log_mustnot snapexists
$dataset
2406 log_fail
"$ops is not supported."
2410 # make sure the upper level filesystem does not exist
2411 if datasetexists
${newdataset%/*} ; then
2412 log_must
$ZFS destroy
-rRf ${newdataset%/*}
2415 # without -p option, operation will fail
2416 log_mustnot
$ZFS $ops $dataset $newdataset
2417 log_mustnot datasetexists
$newdataset ${newdataset%/*}
2419 # with -p option, operation should succeed
2420 log_must
$ZFS $ops -p $dataset $newdataset
2423 if ! datasetexists
$newdataset ; then
2424 log_fail
"-p option does not work for $ops"
2427 # when $ops is create or clone, redo the operation still return zero
2428 if [[ $ops != "rename" ]]; then
2429 log_must
$ZFS $ops -p $dataset $newdataset
2436 # Get configuration of pool
2446 if ! poolexists
"$pool" ; then
2449 alt_root
=$
($ZPOOL list
-H $pool |
$AWK '{print $NF}')
2450 if [[ $alt_root == "-" ]]; then
2451 value
=$
($ZDB -C $pool |
$GREP "$config:" |
$AWK -F: \
2454 value
=$
($ZDB -e $pool |
$GREP "$config:" |
$AWK -F: \
2457 if [[ -n $value ]] ; then
2467 # Privated function. Random select one of items from arguments.
2472 function _random_get
2479 ((ind
= RANDOM
% cnt
+ 1))
2481 typeset ret
=$
($ECHO "$str" |
$CUT -f $ind -d ' ')
2486 # Random select one of item from arguments which include NONE string
2488 function random_get_with_non
2493 _random_get
"$cnt" "$@"
2497 # Random select one of item from arguments which doesn't include NONE string
2501 _random_get
"$#" "$@"
2505 # Detect if the current system support slog
2507 function verify_slog_support
2509 typeset dir
=/tmp
/disk.$$
2515 $MKFILE 64M
$vdev $sdev
2518 if ! $ZPOOL create
-n $pool $vdev log
$sdev > /dev
/null
2>&1; then
2527 # The function will generate a dataset name with specific length
2528 # $1, the length of the name
2529 # $2, the base string to construct the name
2531 function gen_dataset_name
2534 typeset basestr
="$2"
2535 typeset
-i baselen
=${#basestr}
2539 if ((len
% baselen
== 0)); then
2540 ((iter
= len
/ baselen
))
2542 ((iter
= len
/ baselen
+ 1))
2544 while ((iter
> 0)); do
2545 l_name
="${l_name}$basestr"
2554 # Get cksum tuple of dataset
2557 # sample zdb output:
2558 # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2559 # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2560 # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2561 # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2562 function datasetcksum
2566 cksum=$
($ZDB -vvv $1 |
$GREP "^Dataset $1 \[" |
$GREP "cksum" \
2567 |
$AWK -F= '{print $7}')
2578 cksum=$
($CKSUM $1 |
$AWK '{print $1}')
2583 # Get the given disk/slice state from the specific field of the pool
2585 function get_device_state
#pool disk field("", "spares","logs")
2588 typeset disk
=${2#$DEV_DSKDIR/}
2589 typeset field
=${3:-$pool}
2591 state
=$
($ZPOOL status
-v "$pool" 2>/dev
/null | \
2592 $NAWK -v device
=$disk -v pool
=$pool -v field
=$field \
2593 'BEGIN {startconfig=0; startfield=0; }
2594 /config:/ {startconfig=1}
2595 (startconfig==1) && ($1==field) {startfield=1; next;}
2596 (startfield==1) && ($1==device) {print $2; exit;}
2598 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2604 # print the given directory filesystem type
2612 if [[ -z $dir ]]; then
2613 log_fail
"Usage: get_fstype <directory>"
2620 $DF -n $dir |
$AWK '{print $3}'
2624 # Given a disk, label it to VTOC regardless what label was on the disk
2630 if [[ -z $disk ]]; then
2631 log_fail
"The disk name is unspecified."
2633 typeset label_file
=/var
/tmp
/labelvtoc.$$
2634 typeset arch
=$
($UNAME -p)
2637 log_note
"Currently unsupported by the test framework"
2641 if [[ $arch == "i386" ]]; then
2642 $ECHO "label" > $label_file
2643 $ECHO "0" >> $label_file
2644 $ECHO "" >> $label_file
2645 $ECHO "q" >> $label_file
2646 $ECHO "q" >> $label_file
2648 $FDISK -B $disk >/dev
/null
2>&1
2649 # wait a while for fdisk finishes
2651 elif [[ $arch == "sparc" ]]; then
2652 $ECHO "label" > $label_file
2653 $ECHO "0" >> $label_file
2654 $ECHO "" >> $label_file
2655 $ECHO "" >> $label_file
2656 $ECHO "" >> $label_file
2657 $ECHO "q" >> $label_file
2659 log_fail
"unknown arch type"
2662 $FORMAT -e -s -d $disk -f $label_file
2663 typeset
-i ret_val
=$?
2666 # wait the format to finish
2669 if ((ret_val
!= 0)); then
2670 log_fail
"unable to label $disk as VTOC."
2677 # check if the system was installed as zfsroot or not
2678 # return: 0 ture, otherwise false
2682 $DF -n / |
$GREP zfs
> /dev
/null
2>&1
2687 # get the root filesystem name if it's zfsroot system.
2689 # return: root filesystem name
2693 rootfs
=$
($AWK '{if ($2 == "/" && $3 == "zfs") print $1}' \
2695 if [[ -z "$rootfs" ]]; then
2696 log_fail
"Can not get rootfs"
2698 $ZFS list
$rootfs > /dev
/null
2>&1
2699 if (($?
== 0)); then
2702 log_fail
"This is not a zfsroot system."
2707 # get the rootfs's pool name
2711 function get_rootpool
2715 rootfs
=$
($AWK '{if ($2 == "/" && $3 =="zfs") print $1}' \
2717 if [[ -z "$rootfs" ]]; then
2718 log_fail
"Can not get rootpool"
2720 $ZFS list
$rootfs > /dev
/null
2>&1
2721 if (($?
== 0)); then
2722 rootpool
=`$ECHO $rootfs | awk -F\/ '{print $1}'`
2725 log_fail
"This is not a zfsroot system."
2730 # Get the sub string from specified source string
2733 # $2 start position. Count from 1
2736 function get_substr
#src_str pos offset
2741 $NAWK -v pos
=$2 -v offset
=$3 '{print substr($0, pos, offset)}'
2745 # Check if the given device is physical device
2747 function is_physical_device
#device
2749 typeset device
=${1#$DEV_DSKDIR}
2750 device
=${device#$DEV_RDSKDIR}
2753 [[ -b "$DEV_DSKDIR/$device" ]] && \
2754 [[ -f /sys
/module
/loop
/parameters
/max_part
]]
2757 $ECHO $device |
$EGREP "^c[0-F]+([td][0-F]+)+$" > /dev
/null
2>&1
2763 # Check if the given device is a real device (ie SCSI device)
2765 function is_real_device
#disk
2768 [[ -z $disk ]] && log_fail
"No argument for disk given."
2771 $LSBLK $DEV_RDSKDIR/$disk -o TYPE |
$EGREP disk
> /dev
/null
2>&1
2777 # Check if the given device is a loop device
2779 function is_loop_device
#disk
2782 [[ -z $disk ]] && log_fail
"No argument for disk given."
2785 $LSBLK $DEV_RDSKDIR/$disk -o TYPE |
$EGREP loop
> /dev
/null
2>&1
2791 # Check if the given device is a multipath device and if there is a sybolic
2792 # link to a device mapper and to a disk
2793 # Currently no support for dm devices alone without multipath
2795 function is_mpath_device
#disk
2798 [[ -z $disk ]] && log_fail
"No argument for disk given."
2801 $LSBLK $DEV_MPATHDIR/$disk -o TYPE |
$EGREP mpath
> /dev
/null
2>&1
2802 if (($?
== 0)); then
2803 $READLINK $DEV_MPATHDIR/$disk > /dev
/null
2>&1
2811 # Set the slice prefix for disk partitioning depending
2812 # on whether the device is a real, multipath, or loop device.
2813 # Currently all disks have to be of the same type, so only
2814 # checks first disk to determine slice prefix.
2816 function set_slice_prefix
2822 while (( i
< $DISK_ARRAY_NUM )); do
2823 disk
="$($ECHO $DISKS | $NAWK '{print $(i + 1)}')"
2824 if ( is_mpath_device
$disk ) && [[ -z $
($ECHO $disk |
awk 'substr($1,18,1)\
2825 ~ /^[[:digit:]]+$/') ]] ||
( is_real_device
$disk ); then
2826 export SLICE_PREFIX
=""
2828 elif ( is_mpath_device
$disk || is_loop_device
$disk ); then
2829 export SLICE_PREFIX
="p"
2832 log_fail
"$disk not supported for partitioning."
2840 # Set the directory path of the listed devices in $DISK_ARRAY_NUM
2841 # Currently all disks have to be of the same type, so only
2842 # checks first disk to determine device directory
2843 # default = /dev (linux)
2844 # real disk = /dev (linux)
2845 # multipath device = /dev/mapper (linux)
2847 function set_device_dir
2853 while (( i
< $DISK_ARRAY_NUM )); do
2854 disk
="$($ECHO $DISKS | $NAWK '{print $(i + 1)}')"
2855 if is_mpath_device
$disk; then
2856 export DEV_DSKDIR
=$DEV_MPATHDIR
2859 export DEV_DSKDIR
=$DEV_RDSKDIR
2865 export DEV_DSKDIR
=$DEV_RDSKDIR
2870 # Get the directory path of given device
2872 function get_device_dir
#device
2876 if ! $
(is_physical_device
$device) ; then
2877 if [[ $device != "/" ]]; then
2880 if [[ -b "$DEV_DSKDIR/$device" ]]; then
2881 device
="$DEV_DSKDIR"
2890 # Get the package name
2892 function get_package_name
2894 typeset dirpath
=${1:-$STC_NAME}
2896 echo "SUNWstc-${dirpath}" |
/usr
/bin
/sed -e "s/\//-/g"
2900 # Get the word numbers from a string separated by white space
2902 function get_word_count
2908 # To verify if the require numbers of disks is given
2910 function verify_disk_count
2912 typeset
-i min
=${2:-1}
2914 typeset
-i count
=$
(get_word_count
"$1")
2916 if ((count
< min
)); then
2917 log_untested
"A minimum of $min disks is required to run." \
2918 " You specified $count disk(s)"
2922 function ds_is_volume
2924 typeset
type=$
(get_prop
type $1)
2925 [[ $type = "volume" ]] && return 0
2929 function ds_is_filesystem
2931 typeset
type=$
(get_prop
type $1)
2932 [[ $type = "filesystem" ]] && return 0
2936 function ds_is_snapshot
2938 typeset
type=$
(get_prop
type $1)
2939 [[ $type = "snapshot" ]] && return 0
2944 # Check if Trusted Extensions are installed and enabled
2946 function is_te_enabled
2948 $SVCS -H -o state labeld
2>/dev
/null |
$GREP "enabled"
2949 if (($?
!= 0)); then
2956 # Utility function to determine if a system has multiple cpus.
2962 (($
($PSRINFO |
$WC -l) > 1))
2968 function get_cpu_freq
2971 lscpu |
$AWK '/CPU MHz/ { print $3 }'
2973 $PSRINFO -v 0 |
$AWK '/processor operates at/ {print $6}'
2977 # Run the given command as the user provided.
2983 log_note
"user:$user $@"
2984 eval \
$SU \
$user -c \"$@
\" > /tmp
/out
2>/tmp
/err
2989 # Check if the pool contains the specified vdevs
2994 # Return 0 if the vdevs are contained in the pool, 1 if any of the specified
2995 # vdevs is not in the pool, and 2 if pool name is missing.
2997 function vdevs_in_pool
3002 if [[ -z $pool ]]; then
3003 log_note
"Missing pool name."
3009 typeset tmpfile
=$
($MKTEMP)
3010 $ZPOOL list
-Hv "$pool" >$tmpfile
3012 $GREP -w ${vdev##*/} $tmpfile >/dev
/null
2>&1
3013 [[ $?
-ne 0 ]] && return 1
3027 max
=$
(echo $
((max
> i ? max
: i
)))
3039 min
=$
(echo $
((min
< i ? min
: i
)))
3046 # Wait for newly created block devices to have their minors created.
3048 function block_device_wait
3057 # Synchronize all the data in pool
3061 function sync_pool
#pool
3063 typeset pool
=${1:-$TESTPOOL}
3067 # Flush all the pool data.
3069 $ZPOOL scrub
$pool >/dev
/null
2>&1
3071 (( $ret != 0 )) && \
3072 log_fail
"$ZPOOL scrub $pool failed."
3074 while ! is_pool_scrubbed
$pool; do
3075 if is_pool_resilvered
$pool ; then
3076 log_fail
"$pool should not be resilver completed."