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 whether a dataset is mounted
50 # $2 filesystem type; optional - defaulted to zfs
52 # Return 0 if dataset is mounted; 1 if unmounted; 2 on error
57 [[ -z $fstype ]] && fstype
=zfs
58 typeset out dir name ret
62 if [[ "$1" == "/"* ]] ; then
63 for out
in $
($ZFS mount |
$AWK '{print $2}'); do
64 [[ $1 == $out ]] && return 0
67 for out
in $
($ZFS mount |
$AWK '{print $1}'); do
68 [[ $1 == $out ]] && return 0
73 out
=$
($DF -F $fstype $1 2>/dev
/null
)
75 (($ret != 0)) && return $ret
83 [[ "$1" == "$dir" ||
"$1" == "$name" ]] && return 0
86 out
=$
($DF -t $fstype $1 2>/dev
/null
)
90 if [[ -L "$ZVOL_DEVDIR/$1" ]]; then
91 link
=$
(readlink
-f $ZVOL_DEVDIR/$1)
93 $MOUNT |
$GREP -q "^$link" && \
102 # Return 0 if a dataset is mounted; 1 otherwise
105 # $2 filesystem type; optional - defaulted to zfs
110 (($?
== 0)) && return 0
114 # Return 0 if a dataset is unmounted; 1 otherwise
117 # $2 filesystem type; optional - defaulted to zfs
122 (($?
== 1)) && return 0
132 $ECHO $1 |
$SED "s/,/ /g"
135 function default_setup
137 default_setup_noexit
"$@"
143 # Given a list of disks, setup storage pools and datasets.
145 function default_setup_noexit
150 log_note begin default_setup_noexit
152 if is_global_zone
; then
153 if poolexists
$TESTPOOL ; then
154 destroy_pool
$TESTPOOL
156 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
157 log_note creating pool
$TESTPOOL $disklist
158 log_must
$ZPOOL create
-f $TESTPOOL $disklist
163 $RM -rf $TESTDIR || log_unresolved Could not remove
$TESTDIR
164 $MKDIR -p $TESTDIR || log_unresolved Could not create
$TESTDIR
166 log_must
$ZFS create
$TESTPOOL/$TESTFS
167 log_must
$ZFS set mountpoint
=$TESTDIR $TESTPOOL/$TESTFS
169 if [[ -n $container ]]; then
170 $RM -rf $TESTDIR1 || \
171 log_unresolved Could not remove
$TESTDIR1
172 $MKDIR -p $TESTDIR1 || \
173 log_unresolved Could not create
$TESTDIR1
175 log_must
$ZFS create
$TESTPOOL/$TESTCTR
176 log_must
$ZFS set canmount
=off
$TESTPOOL/$TESTCTR
177 log_must
$ZFS create
$TESTPOOL/$TESTCTR/$TESTFS1
178 log_must
$ZFS set mountpoint
=$TESTDIR1 \
179 $TESTPOOL/$TESTCTR/$TESTFS1
182 if [[ -n $volume ]]; then
183 if is_global_zone
; then
184 log_must
$ZFS create
-V $VOLSIZE $TESTPOOL/$TESTVOL
187 log_must
$ZFS create
$TESTPOOL/$TESTVOL
193 # Given a list of disks, setup a storage pool, file system and
196 function default_container_setup
200 default_setup
"$disklist" "true"
204 # Given a list of disks, setup a storage pool,file system
207 function default_volume_setup
211 default_setup
"$disklist" "" "true"
215 # Given a list of disks, setup a storage pool,file system,
216 # a container and a volume.
218 function default_container_volume_setup
222 default_setup
"$disklist" "true" "true"
226 # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
229 # $1 Existing filesystem or volume name. Default, $TESTFS
230 # $2 snapshot name. Default, $TESTSNAP
232 function create_snapshot
234 typeset fs_vol
=${1:-$TESTFS}
235 typeset snap
=${2:-$TESTSNAP}
237 [[ -z $fs_vol ]] && log_fail
"Filesystem or volume's name is undefined."
238 [[ -z $snap ]] && log_fail
"Snapshot's name is undefined."
240 if snapexists
$fs_vol@
$snap; then
241 log_fail
"$fs_vol@$snap already exists."
243 datasetexists
$fs_vol || \
244 log_fail
"$fs_vol must exist."
246 log_must
$ZFS snapshot
$fs_vol@
$snap
250 # Create a clone from a snapshot, default clone name is $TESTCLONE.
252 # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
253 # $2 Clone name, $TESTPOOL/$TESTCLONE is default.
255 function create_clone
# snapshot clone
257 typeset snap
=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
258 typeset clone
=${2:-$TESTPOOL/$TESTCLONE}
261 log_fail
"Snapshot name is undefined."
263 log_fail
"Clone name is undefined."
265 log_must
$ZFS clone
$snap $clone
268 function default_mirror_setup
270 default_mirror_setup_noexit
$1 $2 $3
276 # Given a pair of disks, set up a storage pool and dataset for the mirror
277 # @parameters: $1 the primary side of the mirror
278 # $2 the secondary side of the mirror
279 # @uses: ZPOOL ZFS TESTPOOL TESTFS
280 function default_mirror_setup_noexit
282 readonly func
="default_mirror_setup_noexit"
286 [[ -z $primary ]] && \
287 log_fail
"$func: No parameters passed"
288 [[ -z $secondary ]] && \
289 log_fail
"$func: No secondary partition passed"
290 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
291 log_must
$ZPOOL create
-f $TESTPOOL mirror $@
292 log_must
$ZFS create
$TESTPOOL/$TESTFS
293 log_must
$ZFS set mountpoint
=$TESTDIR $TESTPOOL/$TESTFS
297 # create a number of mirrors.
298 # We create a number($1) of 2 way mirrors using the pairs of disks named
299 # on the command line. These mirrors are *not* mounted
300 # @parameters: $1 the number of mirrors to create
301 # $... the devices to use to create the mirrors on
302 # @uses: ZPOOL ZFS TESTPOOL
303 function setup_mirrors
305 typeset
-i nmirrors
=$1
308 while ((nmirrors
> 0)); do
309 log_must
test -n "$1" -a -n "$2"
310 [[ -d /$TESTPOOL$nmirrors ]] && $RM -rf /$TESTPOOL$nmirrors
311 log_must
$ZPOOL create
-f $TESTPOOL$nmirrors mirror
$1 $2
313 ((nmirrors
= nmirrors
- 1))
318 # create a number of raidz pools.
319 # We create a number($1) of 2 raidz pools using the pairs of disks named
320 # on the command line. These pools are *not* mounted
321 # @parameters: $1 the number of pools to create
322 # $... the devices to use to create the pools on
323 # @uses: ZPOOL ZFS TESTPOOL
324 function setup_raidzs
326 typeset
-i nraidzs
=$1
329 while ((nraidzs
> 0)); do
330 log_must
test -n "$1" -a -n "$2"
331 [[ -d /$TESTPOOL$nraidzs ]] && $RM -rf /$TESTPOOL$nraidzs
332 log_must
$ZPOOL create
-f $TESTPOOL$nraidzs raidz
$1 $2
334 ((nraidzs
= nraidzs
- 1))
339 # Destroy the configured testpool mirrors.
340 # the mirrors are of the form ${TESTPOOL}{number}
341 # @uses: ZPOOL ZFS TESTPOOL
342 function destroy_mirrors
344 default_cleanup_noexit
350 # Given a minimum of two disks, set up a storage pool and dataset for the raid-z
351 # $1 the list of disks
353 function default_raidz_setup
355 typeset disklist
="$*"
356 disks
=(${disklist[*]})
358 if [[ ${#disks[*]} -lt 2 ]]; then
359 log_fail
"A raid-z requires a minimum of two disks."
362 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
363 log_must
$ZPOOL create
-f $TESTPOOL raidz
$1 $2 $3
364 log_must
$ZFS create
$TESTPOOL/$TESTFS
365 log_must
$ZFS set mountpoint
=$TESTDIR $TESTPOOL/$TESTFS
371 # Common function used to cleanup storage pools and datasets.
373 # Invoked at the start of the test suite to ensure the system
374 # is in a known state, and also at the end of each set of
375 # sub-tests to ensure errors from one set of tests doesn't
376 # impact the execution of the next set.
378 function default_cleanup
380 default_cleanup_noexit
385 function default_cleanup_noexit
390 # Destroying the pool will also destroy any
391 # filesystems it contains.
393 if is_global_zone
; then
394 $ZFS unmount
-a > /dev
/null
2>&1
395 [[ -z "$KEEP" ]] && KEEP
="rpool"
396 exclude
=`eval $ECHO \"'(${KEEP})'\"`
397 ALL_POOLS
=$
($ZPOOL list
-H -o name \
398 |
$GREP -v "$NO_POOLS" |
$EGREP -v "$exclude")
399 # Here, we loop through the pools we're allowed to
400 # destroy, only destroying them if it's safe to do
402 while [ ! -z ${ALL_POOLS} ]
404 for pool
in ${ALL_POOLS}
406 if safe_to_destroy_pool
$pool ;
410 ALL_POOLS
=$
($ZPOOL list
-H -o name \
411 |
$GREP -v "$NO_POOLS" \
412 |
$EGREP -v "$exclude")
419 for fs
in $
($ZFS list
-H -o name \
420 |
$GREP "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
421 datasetexists
$fs && \
422 log_must
$ZFS destroy
-Rf $fs
425 # Need cleanup here to avoid garbage dir left.
426 for fs
in $
($ZFS list
-H -o name
); do
427 [[ $fs == /$ZONE_POOL ]] && continue
428 [[ -d $fs ]] && log_must
$RM -rf $fs/*
432 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
435 for fs
in $
($ZFS list
-H -o name
); do
436 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
437 log_must
$ZFS set reservation
=none
$fs
438 log_must
$ZFS set recordsize
=128K
$fs
439 log_must
$ZFS set mountpoint
=/$fs $fs
441 enc
=$
(get_prop encryption
$fs)
442 if [[ $?
-ne 0 ]] ||
[[ -z "$enc" ]] || \
443 [[ "$enc" == "off" ]]; then
444 log_must
$ZFS set checksum
=on
$fs
446 log_must
$ZFS set compression
=off
$fs
447 log_must
$ZFS set atime
=on
$fs
448 log_must
$ZFS set devices
=off
$fs
449 log_must
$ZFS set exec=on
$fs
450 log_must
$ZFS set setuid
=on
$fs
451 log_must
$ZFS set readonly=off
$fs
452 log_must
$ZFS set snapdir
=hidden
$fs
453 log_must
$ZFS set aclmode
=groupmask
$fs
454 log_must
$ZFS set aclinherit
=secure
$fs
459 [[ -d $TESTDIR ]] && \
460 log_must
$RM -rf $TESTDIR
463 if is_mpath_device
$disk1; then
470 # Common function used to cleanup storage pools, file systems
473 function default_container_cleanup
475 if ! is_global_zone
; then
479 ismounted
$TESTPOOL/$TESTCTR/$TESTFS1
481 log_must
$ZFS unmount
$TESTPOOL/$TESTCTR/$TESTFS1
483 datasetexists
$TESTPOOL/$TESTCTR/$TESTFS1 && \
484 log_must
$ZFS destroy
-R $TESTPOOL/$TESTCTR/$TESTFS1
486 datasetexists
$TESTPOOL/$TESTCTR && \
487 log_must
$ZFS destroy
-Rf $TESTPOOL/$TESTCTR
489 [[ -e $TESTDIR1 ]] && \
490 log_must
$RM -rf $TESTDIR1 > /dev
/null
2>&1
496 # Common function used to cleanup snapshot of file system or volume. Default to
497 # delete the file system's snapshot
501 function destroy_snapshot
503 typeset snap
=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
505 if ! snapexists
$snap; then
506 log_fail
"'$snap' does not existed."
510 # For the sake of the value which come from 'get_prop' is not equal
511 # to the really mountpoint when the snapshot is unmounted. So, firstly
512 # check and make sure this snapshot's been mounted in current system.
515 if ismounted
$snap; then
516 mtpt
=$
(get_prop mountpoint
$snap)
518 log_fail
"get_prop mountpoint $snap failed."
521 log_must
$ZFS destroy
$snap
522 [[ $mtpt != "" && -d $mtpt ]] && \
523 log_must
$RM -rf $mtpt
527 # Common function used to cleanup clone.
531 function destroy_clone
533 typeset clone
=${1:-$TESTPOOL/$TESTCLONE}
535 if ! datasetexists
$clone; then
536 log_fail
"'$clone' does not existed."
539 # With the same reason in destroy_snapshot
541 if ismounted
$clone; then
542 mtpt
=$
(get_prop mountpoint
$clone)
544 log_fail
"get_prop mountpoint $clone failed."
547 log_must
$ZFS destroy
$clone
548 [[ $mtpt != "" && -d $mtpt ]] && \
549 log_must
$RM -rf $mtpt
552 # Return 0 if a snapshot exists; $? otherwise
558 $ZFS list
-H -t snapshot
"$1" > /dev
/null
2>&1
563 # Set a property to a certain value on a dataset.
564 # Sets a property of the dataset to the value as passed in.
566 # $1 dataset who's property is being set
568 # $3 value to set property to
570 # 0 if the property could be set.
571 # non-zero otherwise.
574 function dataset_setprop
576 typeset fn
=dataset_setprop
579 log_note
"$fn: Insufficient parameters (need 3, had $#)"
583 output
=$
($ZFS set $2=$3 $1 2>&1)
586 log_note
"Setting property on $1 failed."
587 log_note
"property $2=$3"
588 log_note
"Return Code: $rv"
589 log_note
"Output: $output"
596 # Assign suite defined dataset properties.
597 # This function is used to apply the suite's defined default set of
598 # properties to a dataset.
599 # @parameters: $1 dataset to use
600 # @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
602 # 0 if the dataset has been altered.
603 # 1 if no pool name was passed in.
604 # 2 if the dataset could not be found.
605 # 3 if the dataset could not have it's properties set.
607 function dataset_set_defaultproperties
611 [[ -z $dataset ]] && return 1
615 for confset
in $
($ZFS list
); do
616 if [[ $dataset = $confset ]]; then
621 [[ $found -eq 0 ]] && return 2
622 if [[ -n $COMPRESSION_PROP ]]; then
623 dataset_setprop
$dataset compression
$COMPRESSION_PROP || \
625 log_note
"Compression set to '$COMPRESSION_PROP' on $dataset"
627 if [[ -n $CHECKSUM_PROP ]]; then
628 dataset_setprop
$dataset checksum
$CHECKSUM_PROP || \
630 log_note
"Checksum set to '$CHECKSUM_PROP' on $dataset"
636 # Check a numeric assertion
637 # @parameter: $@ the assertion to check
638 # @output: big loud notice if assertion failed
643 (($@
)) || log_fail
"$@"
647 # Function to format partition size of a disk
648 # Given a disk cxtxdx reduces all partitions
651 function zero_partitions
#<whole_disk_name>
657 log_must
$FORMAT $DEV_DSKDIR/$diskname -s -- mklabel gpt
659 for i
in 0 1 3 4 5 6 7
661 set_partition
$i "" 0mb
$diskname
667 # Given a slice, size and disk, this function
668 # formats the slice to the specified size.
669 # Size should be specified with units as per
670 # the `format` command requirements eg. 100mb 3gb
672 # NOTE: This entire interface is problematic for the Linux parted utilty
673 # which requires the end of the partition to be specified. It would be
674 # best to retire this interface and replace it with something more flexible.
675 # At the moment a best effort is made.
677 function set_partition
#<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
679 typeset
-i slicenum
=$1
683 [[ -z $slicenum ||
-z $size ||
-z $disk ]] && \
684 log_fail
"The slice, size or disk name is unspecified."
687 typeset size_mb
=${size%%[mMgG]}
689 size_mb
=${size_mb%%[mMgG][bB]}
690 if [[ ${size:1:1} == 'g' ]]; then
691 ((size_mb
= size_mb
* 1024))
694 # Create GPT partition table when setting slice 0 or
695 # when the device doesn't already contain a GPT label.
696 $FORMAT $DEV_DSKDIR/$disk -s -- print
1 >/dev
/null
698 if [[ $slicenum -eq 0 ||
$ret_val -ne 0 ]]; then
699 log_must
$FORMAT $DEV_DSKDIR/$disk -s -- mklabel gpt
702 # When no start is given align on the first cylinder.
703 if [[ -z "$start" ]]; then
707 # Determine the cylinder size for the device and using
708 # that calculate the end offset in cylinders.
709 typeset
-i cly_size_kb
=0
710 cly_size_kb
=$
($FORMAT -m $DEV_DSKDIR/$disk -s -- \
711 unit cyl print |
$HEAD -3 |
$TAIL -1 | \
712 $AWK -F '[:k.]' '{print $4}')
713 ((end
= (size_mb
* 1024 / cly_size_kb
) + start
))
715 log_must
$FORMAT $DEV_DSKDIR/$disk -s -- \
716 mkpart part
$slicenum ${start}cyl
${end}cyl
718 $BLOCKDEV --rereadpt $DEV_DSKDIR/$disk 2>/dev
/null
721 typeset format_file
=/var
/tmp
/format_in.$$
723 $ECHO "partition" >$format_file
724 $ECHO "$slicenum" >> $format_file
725 $ECHO "" >> $format_file
726 $ECHO "" >> $format_file
727 $ECHO "$start" >> $format_file
728 $ECHO "$size" >> $format_file
729 $ECHO "label" >> $format_file
730 $ECHO "" >> $format_file
731 $ECHO "q" >> $format_file
732 $ECHO "q" >> $format_file
734 $FORMAT -e -s -d $disk -f $format_file
738 [[ $ret_val -ne 0 ]] && \
739 log_fail
"Unable to format $disk slice $slicenum to $size"
744 # Delete all partitions on all disks - this is specifically for the use of multipath
745 # devices which currently can only be used in the test suite as raw/un-partitioned
746 # devices (ie a zpool cannot be created on a whole mpath device that has partitions)
748 function delete_partitions
752 if [[ -z $DISK_ARRAY_NUM ]]; then
753 DISK_ARRAY_NUM
=$
($ECHO ${DISKS} |
$NAWK '{print NF}')
755 if [[ -z $DISKSARRAY ]]; then
760 if (( $DISK_ARRAY_NUM == 1 )); then
761 while ((j
< MAX_PARTITIONS
)); do
762 $FORMAT $DEV_DSKDIR/$DISK -s rm $j > /dev
/null
2>&1
763 if (( $?
== 1 )); then
764 $LSBLK |
$EGREP ${DISK}${SLICE_PREFIX}${j} > /dev
/null
765 if (( $?
== 1 )); then
766 log_note
"Partitions for $DISK should be deleted"
768 log_fail
"Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
772 $LSBLK |
$EGREP ${DISK}${SLICE_PREFIX}${j} > /dev
/null
773 if (( $?
== 0 )); then
774 log_fail
"Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
780 for disk
in `$ECHO $DISKSARRAY`; do
781 while ((j
< MAX_PARTITIONS
)); do
782 $FORMAT $DEV_DSKDIR/$disk -s rm $j > /dev
/null
2>&1
783 if (( $?
== 1 )); then
784 $LSBLK |
$EGREP ${disk}${SLICE_PREFIX}${j} > /dev
/null
785 if (( $?
== 1 )); then
786 log_note
"Partitions for $disk should be deleted"
788 log_fail
"Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
792 $LSBLK |
$EGREP ${disk}${SLICE_PREFIX}${j} > /dev
/null
793 if (( $?
== 0 )); then
794 log_fail
"Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
807 # Get the end cyl of the given slice
809 function get_endslice
#<disk> <slice>
813 if [[ -z $disk ||
-z $slice ]] ; then
814 log_fail
"The disk name or slice number is unspecified."
818 endcyl
=$
($FORMAT -s $DEV_DSKDIR/$disk -- unit cyl print | \
819 $GREP "part${slice}" | \
820 $AWK '{print $3}' | \
822 ((endcyl
= (endcyl
+ 1)))
824 disk
=${disk#/dev/dsk/}
825 disk
=${disk#/dev/rdsk/}
829 ratio
=$
($PRTVTOC /dev
/rdsk
/${disk}s2 | \
830 $GREP "sectors\/cylinder" | \
833 if ((ratio
== 0)); then
837 typeset
-i endcyl
=$
($PRTVTOC -h /dev
/rdsk
/${disk}s2 |
838 $NAWK -v token
="$slice" '{if ($1==token) print $6}')
840 ((endcyl
= (endcyl
+ 1) / ratio
))
848 # Given a size,disk and total slice number, this function formats the
849 # disk slices from 0 to the total slice number with the same specified
852 function partition_disk
#<slice_size> <whole_disk_name> <total_slices>
855 typeset slice_size
=$1
857 typeset total_slices
=$3
860 zero_partitions
$disk_name
861 while ((i
< $total_slices)); do
868 set_partition
$i "$cyl" $slice_size $disk_name
869 cyl
=$
(get_endslice
$disk_name $i)
875 # This function continues to write to a filenum number of files into dirnum
876 # number of directories until either $FILE_WRITE returns an error or the
877 # maximum number of files per directory have been written.
880 # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
882 # Return value: 0 on success
886 # destdir: is the directory where everything is to be created under
887 # dirnum: the maximum number of subdirectories to use, -1 no limit
888 # filenum: the maximum number of files per subdirectory
889 # bytes: number of bytes to write
890 # num_writes: numer of types to write out bytes
891 # data: the data that will be writen
894 # file_fs /testdir 20 25 1024 256 0
896 # Note: bytes * num_writes equals the size of the testfile
898 function fill_fs
# destdir dirnum filenum bytes num_writes data
900 typeset destdir
=${1:-$TESTDIR}
901 typeset
-i dirnum
=${2:-50}
902 typeset
-i filenum
=${3:-50}
903 typeset
-i bytes
=${4:-8192}
904 typeset
-i num_writes
=${5:-10240}
905 typeset
-i data
=${6:-0}
912 log_must
$MKDIR -p $destdir/$idirnum
913 while (($odirnum > 0)); do
914 if ((dirnum
>= 0 && idirnum
>= dirnum
)); then
918 $FILE_WRITE -o create
-f $destdir/$idirnum/$TESTFILE.
$fn \
919 -b $bytes -c $num_writes -d $data
921 if (($retval != 0)); then
925 if (($fn >= $filenum)); then
927 ((idirnum
= idirnum
+ 1))
928 log_must
$MKDIR -p $destdir/$idirnum
937 # Simple function to get the specified property. If unable to
938 # get the property then exits.
940 # Note property is in 'parsable' format (-p)
942 function get_prop
# property dataset
948 prop_val
=$
($ZFS get
-pH -o value
$prop $dataset 2>/dev
/null
)
949 if [[ $?
-ne 0 ]]; then
950 log_note
"Unable to get $prop property for dataset " \
960 # Simple function to get the specified property of pool. If unable to
961 # get the property then exits.
963 function get_pool_prop
# property pool
969 if poolexists
$pool ; then
970 prop_val
=$
($ZPOOL get
$prop $pool 2>/dev
/null |
$TAIL -1 | \
972 if [[ $?
-ne 0 ]]; then
973 log_note
"Unable to get $prop property for pool " \
978 log_note
"Pool $pool not exists."
986 # Return 0 if a pool exists; $? otherwise
994 if [[ -z $pool ]]; then
995 log_note
"No pool name given."
999 $ZPOOL get name
"$pool" > /dev
/null
2>&1
1003 # Return 0 if all the specified datasets exist; $? otherwise
1006 function datasetexists
1008 if (($# == 0)); then
1009 log_note
"No dataset name given."
1013 while (($# > 0)); do
1014 $ZFS get name
$1 > /dev
/null
2>&1 || \
1022 # return 0 if none of the specified datasets exists, otherwise return 1.
1025 function datasetnonexists
1027 if (($# == 0)); then
1028 log_note
"No dataset name given."
1032 while (($# > 0)); do
1033 $ZFS list
-H -t filesystem
,snapshot
,volume
$1 > /dev
/null
2>&1 \
1042 # Given a mountpoint, or a dataset name, determine if it is shared.
1044 # Returns 0 if shared, 1 otherwise.
1052 log_unsupported
"Currently unsupported by the test framework"
1056 if [[ $fs != "/"* ]] ; then
1057 if datasetnonexists
"$fs" ; then
1060 mtpt
=$
(get_prop mountpoint
"$fs")
1062 none|legacy|
-) return 1
1070 for mtpt
in `$SHARE | $AWK '{print $2}'` ; do
1071 if [[ $mtpt == $fs ]] ; then
1076 typeset stat
=$
($SVCS -H -o STA nfs
/server
:default
)
1077 if [[ $stat != "ON" ]]; then
1078 log_note
"Current nfs/server status: $stat"
1085 # Given a mountpoint, determine if it is not shared.
1087 # Returns 0 if not shared, 1 otherwise.
1094 log_unsupported
"Currently unsupported by the test framework"
1099 if (($?
== 0)); then
1107 # Helper function to unshare a mountpoint.
1109 function unshare_fs
#fs
1114 log_unsupported
"Currently unsupported by the test framework"
1119 if (($?
== 0)); then
1120 log_must
$ZFS unshare
$fs
1127 # Check NFS server status and trigger it online.
1129 function setup_nfs_server
1131 # Cannot share directory in non-global zone.
1133 if ! is_global_zone
; then
1134 log_note
"Cannot trigger NFS server by sharing in LZ."
1139 log_unsupported
"Currently unsupported by the test framework"
1143 typeset nfs_fmri
="svc:/network/nfs/server:default"
1144 if [[ $
($SVCS -Ho STA
$nfs_fmri) != "ON" ]]; then
1146 # Only really sharing operation can enable NFS server
1147 # to online permanently.
1149 typeset dummy
=/tmp
/dummy
1151 if [[ -d $dummy ]]; then
1152 log_must
$RM -rf $dummy
1155 log_must
$MKDIR $dummy
1156 log_must
$SHARE $dummy
1159 # Waiting for fmri's status to be the final status.
1160 # Otherwise, in transition, an asterisk (*) is appended for
1161 # instances, unshare will reverse status to 'DIS' again.
1163 # Waiting for 1's at least.
1167 while [[ timeout
-ne 0 && $
($SVCS -Ho STA
$nfs_fmri) == *'*' ]]
1174 log_must
$UNSHARE $dummy
1175 log_must
$RM -rf $dummy
1178 log_note
"Current NFS status: '$($SVCS -Ho STA,FMRI $nfs_fmri)'"
1182 # To verify whether calling process is in global zone
1184 # Return 0 if in global zone, 1 in non-global zone
1186 function is_global_zone
1188 typeset cur_zone
=$
($ZONENAME 2>/dev
/null
)
1189 if [[ $cur_zone != "global" ]]; then
1196 # Verify whether test is permitted to run from
1197 # global zone, local zone, or both
1199 # $1 zone limit, could be "global", "local", or "both"(no limit)
1201 # Return 0 if permitted, otherwise exit with log_unsupported
1203 function verify_runnable
# zone limit
1207 [[ -z $limit ]] && return 0
1209 if is_global_zone
; then
1213 local) log_unsupported
"Test is unable to run from "\
1216 *) log_note
"Warning: unknown limit $limit - " \
1224 global
) log_unsupported
"Test is unable to run from "\
1227 *) log_note
"Warning: unknown limit $limit - " \
1238 # Return 0 if create successfully or the pool exists; $? otherwise
1239 # Note: In local zones, this function should return 0 silently.
1242 # $2-n - [keyword] devs_list
1244 function create_pool
#pool devs_list
1246 typeset pool
=${1%%/*}
1250 if [[ -z $pool ]]; then
1251 log_note
"Missing pool name."
1255 if poolexists
$pool ; then
1259 if is_global_zone
; then
1260 [[ -d /$pool ]] && $RM -rf /$pool
1261 log_must
$ZPOOL create
-f $pool $@
1267 # Return 0 if destroy successfully or the pool exists; $? otherwise
1268 # Note: In local zones, this function should return 0 silently.
1271 # Destroy pool with the given parameters.
1273 function destroy_pool
#pool
1275 typeset pool
=${1%%/*}
1278 if [[ -z $pool ]]; then
1279 log_note
"No pool name given."
1283 if is_global_zone
; then
1284 if poolexists
"$pool" ; then
1285 mtpt
=$
(get_prop mountpoint
"$pool")
1287 # At times, syseventd activity can cause attempts to
1288 # destroy a pool to fail with EBUSY. We retry a few
1289 # times allowing failures before requiring the destroy
1291 typeset
-i wait_time
=10 ret
=1 count
=0
1293 while [[ $ret -ne 0 ]]; do
1294 $must $ZPOOL destroy
-f $pool
1296 [[ $ret -eq 0 ]] && break
1297 log_note
"zpool destroy failed with $ret"
1298 [[ count
++ -ge 7 ]] && must
=log_must
1303 log_must
$RM -rf $mtpt
1305 log_note
"Pool does not exist. ($pool)"
1314 # Firstly, create a pool with 5 datasets. Then, create a single zone and
1315 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
1316 # and a zvol device to the zone.
1319 # $2 zone root directory prefix
1322 function zfs_zones_setup
#zone_name zone_root zone_ip
1324 typeset zone_name
=${1:-$(hostname)-z}
1325 typeset zone_root
=${2:-"/zone_root"}
1326 typeset zone_ip
=${3:-"10.1.1.10"}
1327 typeset prefix_ctr
=$ZONE_CTR
1328 typeset pool_name
=$ZONE_POOL
1332 # Create pool and 5 container within it
1334 [[ -d /$pool_name ]] && $RM -rf /$pool_name
1335 log_must
$ZPOOL create
-f $pool_name $DISKS
1336 while ((i
< cntctr
)); do
1337 log_must
$ZFS create
$pool_name/$prefix_ctr$i
1342 log_must
$ZFS create
-V 1g
$pool_name/zone_zvol
1346 # If current system support slog, add slog device for pool
1348 if verify_slog_support
; then
1349 typeset sdevs
="/var/tmp/sdev1 /var/tmp/sdev2"
1350 log_must
$MKFILE 100M
$sdevs
1351 log_must
$ZPOOL add
$pool_name log mirror
$sdevs
1354 # this isn't supported just yet.
1355 # Create a filesystem. In order to add this to
1356 # the zone, it must have it's mountpoint set to 'legacy'
1357 # log_must $ZFS create $pool_name/zfs_filesystem
1358 # log_must $ZFS set mountpoint=legacy $pool_name/zfs_filesystem
1360 [[ -d $zone_root ]] && \
1361 log_must
$RM -rf $zone_root/$zone_name
1362 [[ ! -d $zone_root ]] && \
1363 log_must
$MKDIR -p -m 0700 $zone_root/$zone_name
1365 # Create zone configure file and configure the zone
1367 typeset zone_conf
=/tmp
/zone_conf.$$
1368 $ECHO "create" > $zone_conf
1369 $ECHO "set zonepath=$zone_root/$zone_name" >> $zone_conf
1370 $ECHO "set autoboot=true" >> $zone_conf
1372 while ((i
< cntctr
)); do
1373 $ECHO "add dataset" >> $zone_conf
1374 $ECHO "set name=$pool_name/$prefix_ctr$i" >> \
1376 $ECHO "end" >> $zone_conf
1380 # add our zvol to the zone
1381 $ECHO "add device" >> $zone_conf
1382 $ECHO "set match=$ZVOL_DEVDIR/$pool_name/zone_zvol" >> $zone_conf
1383 $ECHO "end" >> $zone_conf
1385 # add a corresponding zvol rdsk to the zone
1386 $ECHO "add device" >> $zone_conf
1387 $ECHO "set match=$ZVOL_RDEVDIR/$pool_name/zone_zvol" >> $zone_conf
1388 $ECHO "end" >> $zone_conf
1390 # once it's supported, we'll add our filesystem to the zone
1391 # $ECHO "add fs" >> $zone_conf
1392 # $ECHO "set type=zfs" >> $zone_conf
1393 # $ECHO "set special=$pool_name/zfs_filesystem" >> $zone_conf
1394 # $ECHO "set dir=/export/zfs_filesystem" >> $zone_conf
1395 # $ECHO "end" >> $zone_conf
1397 $ECHO "verify" >> $zone_conf
1398 $ECHO "commit" >> $zone_conf
1399 log_must
$ZONECFG -z $zone_name -f $zone_conf
1400 log_must
$RM -f $zone_conf
1403 $ZONEADM -z $zone_name install
1404 if (($?
== 0)); then
1405 log_note
"SUCCESS: $ZONEADM -z $zone_name install"
1407 log_fail
"FAIL: $ZONEADM -z $zone_name install"
1410 # Install sysidcfg file
1412 typeset sysidcfg
=$zone_root/$zone_name/root
/etc
/sysidcfg
1413 $ECHO "system_locale=C" > $sysidcfg
1414 $ECHO "terminal=dtterm" >> $sysidcfg
1415 $ECHO "network_interface=primary {" >> $sysidcfg
1416 $ECHO "hostname=$zone_name" >> $sysidcfg
1417 $ECHO "}" >> $sysidcfg
1418 $ECHO "name_service=NONE" >> $sysidcfg
1419 $ECHO "root_password=mo791xfZ/SFiw" >> $sysidcfg
1420 $ECHO "security_policy=NONE" >> $sysidcfg
1421 $ECHO "timezone=US/Eastern" >> $sysidcfg
1424 log_must
$ZONEADM -z $zone_name boot
1428 # Reexport TESTPOOL & TESTPOOL(1-4)
1430 function reexport_pool
1435 while ((i
< cntctr
)); do
1437 TESTPOOL
=$ZONE_POOL/$ZONE_CTR$i
1438 if ! ismounted
$TESTPOOL; then
1439 log_must
$ZFS mount
$TESTPOOL
1442 eval TESTPOOL
$i=$ZONE_POOL/$ZONE_CTR$i
1443 if eval ! ismounted \
$TESTPOOL$i; then
1444 log_must
eval $ZFS mount \
$TESTPOOL$i
1452 # Verify a given disk is online or offline
1454 # Return 0 is pool/disk matches expected state, 1 otherwise
1456 function check_state
# pool disk state{online,offline}
1459 typeset disk
=${2#$DEV_DSKDIR/}
1462 $ZPOOL status
-v $pool |
grep "$disk" \
1463 |
grep -i "$state" > /dev
/null
2>&1
1469 # Get the mountpoint of snapshot
1470 # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1473 function snapshot_mountpoint
1475 typeset dataset
=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1477 if [[ $dataset != *@
* ]]; then
1478 log_fail
"Error name of snapshot '$dataset'."
1481 typeset fs
=${dataset%@*}
1482 typeset snap
=${dataset#*@}
1484 if [[ -z $fs ||
-z $snap ]]; then
1485 log_fail
"Error name of snapshot '$dataset'."
1488 $ECHO $
(get_prop mountpoint
$fs)/.zfs
/snapshot
/$snap
1492 # Given a pool and file system, this function will verify the file system
1493 # using the zdb internal tool. Note that the pool is exported and imported
1494 # to ensure it has consistent state.
1496 function verify_filesys
# pool filesystem dir
1499 typeset filesys
="$2"
1500 typeset zdbout
="/tmp/zdbout.$$"
1505 typeset search_path
=""
1507 log_note
"Calling $ZDB to verify filesystem '$filesys'"
1508 $ZFS unmount
-a > /dev
/null
2>&1
1509 log_must
$ZPOOL export $pool
1511 if [[ -n $dirs ]] ; then
1512 for dir
in $dirs ; do
1513 search_path
="$search_path -d $dir"
1517 log_must
$ZPOOL import
$search_path $pool
1519 $ZDB -cudi $filesys > $zdbout 2>&1
1520 if [[ $?
!= 0 ]]; then
1521 log_note
"Output: $ZDB -cudi $filesys"
1523 log_fail
"$ZDB detected errors with: '$filesys'"
1526 log_must
$ZFS mount
-a
1527 log_must
$RM -rf $zdbout
1531 # Given a pool, and this function list all disks in the pool
1533 function get_disklist
# pool
1537 disklist
=$
($ZPOOL iostat
-v $1 |
$NAWK '(NR >4) {print $1}' | \
1538 $GREP -v "\-\-\-\-\-" | \
1539 $EGREP -v -e "^(mirror|raidz1|raidz2|spare|log|cache)$")
1545 # Given a pool, and this function list all disks in the pool with their full
1546 # path (like "/dev/sda" instead of "sda").
1548 function get_disklist_fullpath
# pool
1557 # This function kills a given list of processes after a time period. We use
1558 # this in the stress tests instead of STF_TIMEOUT so that we can have processes
1559 # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1560 # would be listed as FAIL, which we don't want : we're happy with stress tests
1561 # running for a certain amount of time, then finishing.
1563 # @param $1 the time in seconds after which we should terminate these processes
1564 # @param $2..$n the processes we wish to terminate.
1566 function stress_timeout
1568 typeset
-i TIMEOUT
=$1
1572 log_note
"Waiting for child processes($cpids). " \
1573 "It could last dozens of minutes, please be patient ..."
1574 log_must
$SLEEP $TIMEOUT
1576 log_note
"Killing child processes after ${TIMEOUT} stress timeout."
1578 for pid
in $cpids; do
1579 $PS -p $pid > /dev
/null
2>&1
1580 if (($?
== 0)); then
1581 log_must
$KILL -USR1 $pid
1587 # Verify a given hotspare disk is inuse or avail
1589 # Return 0 is pool/disk matches expected state, 1 otherwise
1591 function check_hotspare_state
# pool disk state{inuse,avail}
1594 typeset disk
=${2#$DEV_DSKDIR/}
1597 cur_state
=$
(get_device_state
$pool $disk "spares")
1599 if [[ $state != ${cur_state} ]]; then
1606 # Verify a given slog disk is inuse or avail
1608 # Return 0 is pool/disk matches expected state, 1 otherwise
1610 function check_slog_state
# pool disk state{online,offline,unavail}
1613 typeset disk
=${2#$DEV_DSKDIR/}
1616 cur_state
=$
(get_device_state
$pool $disk "logs")
1618 if [[ $state != ${cur_state} ]]; then
1625 # Verify a given vdev disk is inuse or avail
1627 # Return 0 is pool/disk matches expected state, 1 otherwise
1629 function check_vdev_state
# pool disk state{online,offline,unavail}
1632 typeset disk
=${2#$/DEV_DSKDIR/}
1635 cur_state
=$
(get_device_state
$pool $disk)
1637 if [[ $state != ${cur_state} ]]; then
1644 # Check the output of 'zpool status -v <pool>',
1645 # and to see if the content of <token> contain the <keyword> specified.
1647 # Return 0 is contain, 1 otherwise
1649 function check_pool_status
# pool token keyword
1655 $ZPOOL status
-v "$pool" 2>/dev
/null |
$NAWK -v token
="$token:" '
1656 ($1==token) {print $0}' \
1657 |
$GREP -i "$keyword" > /dev
/null
2>&1
1663 # These 5 following functions are instance of check_pool_status()
1664 # is_pool_resilvering - to check if the pool is resilver in progress
1665 # is_pool_resilvered - to check if the pool is resilver completed
1666 # is_pool_scrubbing - to check if the pool is scrub in progress
1667 # is_pool_scrubbed - to check if the pool is scrub completed
1668 # is_pool_scrub_stopped - to check if the pool is scrub stopped
1670 function is_pool_resilvering
#pool
1672 check_pool_status
"$1" "scan" "resilver in progress since "
1676 function is_pool_resilvered
#pool
1678 check_pool_status
"$1" "scan" "resilvered "
1682 function is_pool_scrubbing
#pool
1684 check_pool_status
"$1" "scan" "scrub in progress since "
1688 function is_pool_scrubbed
#pool
1690 check_pool_status
"$1" "scan" "scrub repaired"
1694 function is_pool_scrub_stopped
#pool
1696 check_pool_status
"$1" "scan" "scrub canceled"
1701 # Use create_pool()/destroy_pool() to clean up the infomation in
1702 # in the given disk to avoid slice overlapping.
1704 function cleanup_devices
#vdevs
1706 typeset pool
="foopool$$"
1708 if poolexists
$pool ; then
1712 create_pool
$pool $@
1719 # Verify the rsh connectivity to each remote host in RHOSTS.
1721 # Return 0 if remote host is accessible; otherwise 1.
1722 # $1 remote host name
1725 function verify_rsh_connect
#rhost, username
1729 typeset rsh_cmd
="$RSH -n"
1732 $GETENT hosts
$rhost >/dev
/null
2>&1
1733 if (($?
!= 0)); then
1734 log_note
"$rhost cannot be found from" \
1735 "administrative database."
1739 $PING $rhost 3 >/dev
/null
2>&1
1740 if (($?
!= 0)); then
1741 log_note
"$rhost is not reachable."
1745 if ((${#username} != 0)); then
1746 rsh_cmd
="$rsh_cmd -l $username"
1747 cur_user
="given user \"$username\""
1749 cur_user
="current user \"`$LOGNAME`\""
1752 if ! $rsh_cmd $rhost $TRUE; then
1753 log_note
"$RSH to $rhost is not accessible" \
1762 # Verify the remote host connection via rsh after rebooting
1765 function verify_remote
1770 # The following loop waits for the remote system rebooting.
1771 # Each iteration will wait for 150 seconds. there are
1772 # total 5 iterations, so the total timeout value will
1773 # be 12.5 minutes for the system rebooting. This number
1774 # is an approxiate number.
1777 while ! verify_rsh_connect
$rhost; do
1779 ((count
= count
+ 1))
1780 if ((count
> 5)); then
1788 # Replacement function for /usr/bin/rsh. This function will include
1789 # the /usr/bin/rsh and meanwhile return the execution status of the
1792 # $1 usrname passing down to -l option of /usr/bin/rsh
1793 # $2 remote machine hostname
1794 # $3... command string
1808 err_file
=/tmp
/${rhost}.$$.err
1809 if ((${#ruser} == 0)); then
1812 rsh_str
="$RSH -n -l $ruser"
1815 $rsh_str $rhost /bin
/ksh
-c "'$cmd_str; \
1816 print -u 2 \"status=\$?\"'" \
1817 >/dev
/null
2>$err_file
1819 if (($ret != 0)); then
1821 $RM -f $std_file $err_file
1822 log_fail
"$RSH itself failed with exit code $ret..."
1825 ret
=$
($GREP -v 'print -u 2' $err_file |
$GREP 'status=' | \
1827 (($ret != 0)) && $CAT $err_file >&2
1829 $RM -f $err_file >/dev
/null
2>&1
1834 # Get the SUNWstc-fs-zfs package installation path in a remote host
1835 # $1 remote host name
1837 function get_remote_pkgpath
1842 pkgpath
=$
($RSH -n $rhost "$PKGINFO -l SUNWstc-fs-zfs | $GREP BASEDIR: |\
1849 # A function to find and locate free disks on a system or from given
1850 # disks as the parameter. It works by locating disks that are in use
1851 # as swap devices and dump devices, and also disks listed in /etc/vfstab
1853 # $@ given disks to find which are free, default is all disks in
1856 # @return a string containing the list of available disks
1860 # Trust provided list, no attempt is made to locate unused devices.
1867 sfi
=/tmp
/swaplist.$$
1868 dmpi
=/tmp
/dumpdev.$$
1869 max_finddisksnum
=${MAX_FINDDISKSNUM:-6}
1872 $DUMPADM > $dmpi 2>/dev
/null
1874 # write an awk script that can process the output of format
1875 # to produce a list of disks we know about. Note that we have
1876 # to escape "$2" so that the shell doesn't interpret it while
1877 # we're creating the awk script.
1878 # -------------------
1879 $CAT > /tmp
/find_disks.
awk <<EOF
1888 if (searchdisks && \$2 !~ "^$"){
1894 /^AVAILABLE DISK SELECTIONS:/{
1898 #---------------------
1900 $CHMOD 755 /tmp
/find_disks.
awk
1901 disks
=${@:-$($ECHO "" | $FORMAT -e 2>/dev/null | /tmp/find_disks.awk)}
1902 $RM /tmp
/find_disks.
awk
1905 for disk
in $disks; do
1907 $GREP "${disk}[sp]" /etc
/mnttab
>/dev
/null
1908 (($?
== 0)) && continue
1910 $GREP "${disk}[sp]" $sfi >/dev
/null
1911 (($?
== 0)) && continue
1912 # check for dump device
1913 $GREP "${disk}[sp]" $dmpi >/dev
/null
1914 (($?
== 0)) && continue
1915 # check to see if this disk hasn't been explicitly excluded
1916 # by a user-set environment variable
1917 $ECHO "${ZFS_HOST_DEVICES_IGNORE}" |
$GREP "${disk}" > /dev
/null
1918 (($?
== 0)) && continue
1919 unused_candidates
="$unused_candidates $disk"
1924 # now just check to see if those disks do actually exist
1925 # by looking for a device pointing to the first slice in
1926 # each case. limit the number to max_finddisksnum
1928 for disk
in $unused_candidates; do
1929 if [ -b $DEV_DSKDIR/${disk}s0
]; then
1930 if [ $count -lt $max_finddisksnum ]; then
1931 unused
="$unused $disk"
1932 # do not impose limit if $@ is provided
1933 [[ -z $@
]] && ((count
= count
+ 1))
1938 # finally, return our disk list
1943 # Add specified user to specified group
1947 # $3 base of the homedir (optional)
1949 function add_user
#<group_name> <user_name> <basedir>
1953 typeset basedir
=${3:-"/var/tmp"}
1955 if ((${#gname} == 0 ||
${#uname} == 0)); then
1956 log_fail
"group name or user name are not defined."
1959 log_must
$USERADD -g $gname -d $basedir/$uname -m $uname
1961 # Add new users to the same group and the command line utils.
1962 # This allows them to be run out of the original users home
1963 # directory as long as it permissioned to be group readable.
1965 cmd_group
=$
(stat
--format="%G" $ZFS)
1966 log_must
$USERMOD -a -G $cmd_group $uname
1973 # Delete the specified user.
1976 # $2 base of the homedir (optional)
1978 function del_user
#<logname> <basedir>
1981 typeset basedir
=${2:-"/var/tmp"}
1983 if ((${#user} == 0)); then
1984 log_fail
"login name is necessary."
1987 if $ID $user > /dev
/null
2>&1; then
1988 log_must
$USERDEL $user
1991 [[ -d $basedir/$user ]] && $RM -fr $basedir/$user
1997 # Select valid gid and create specified group.
2001 function add_group
#<group_name>
2005 if ((${#group} == 0)); then
2006 log_fail
"group name is necessary."
2009 # Assign 100 as the base gid, a larger value is selected for
2010 # Linux because for many distributions 1000 and under are reserved.
2013 $GROUPADD $group > /dev
/null
2>&1
2024 $GROUPADD -g $gid $group > /dev
/null
2>&1
2028 # The gid is not unique
2037 # Delete the specified group.
2041 function del_group
#<group_name>
2044 if ((${#grp} == 0)); then
2045 log_fail
"group name is necessary."
2049 $GETENT group
$grp > /dev
/null
2>&1
2052 # Group does not exist.
2054 # Name already exists as a group name
2055 0) log_must
$GROUPDEL $grp ;;
2059 $GROUPMOD -n $grp $grp > /dev
/null
2>&1
2062 # Group does not exist.
2064 # Name already exists as a group name
2065 9) log_must
$GROUPDEL $grp ;;
2074 # This function will return true if it's safe to destroy the pool passed
2075 # as argument 1. It checks for pools based on zvols and files, and also
2076 # files contained in a pool that may have a different mountpoint.
2078 function safe_to_destroy_pool
{ # $1 the pool name
2081 typeset DONT_DESTROY
=""
2083 # We check that by deleting the $1 pool, we're not
2084 # going to pull the rug out from other pools. Do this
2085 # by looking at all other pools, ensuring that they
2086 # aren't built from files or zvols contained in this pool.
2088 for pool
in $
($ZPOOL list
-H -o name
)
2092 # this is a list of the top-level directories in each of the
2093 # files that make up the path to the files the pool is based on
2094 FILEPOOL
=$
($ZPOOL status
-v $pool |
$GREP /$1/ | \
2097 # this is a list of the zvols that make up the pool
2098 ZVOLPOOL
=$
($ZPOOL status
-v $pool |
$GREP "$ZVOL_DEVDIR/$1$" \
2099 |
$AWK '{print $1}')
2101 # also want to determine if it's a file-based pool using an
2102 # alternate mountpoint...
2103 POOL_FILE_DIRS
=$
($ZPOOL status
-v $pool | \
2104 $GREP / |
$AWK '{print $1}' | \
2105 $AWK -F/ '{print $2}' |
$GREP -v "dev")
2107 for pooldir
in $POOL_FILE_DIRS
2109 OUTPUT
=$
($ZFS list
-H -r -o mountpoint
$1 | \
2110 $GREP "${pooldir}$" |
$AWK '{print $1}')
2112 ALTMOUNTPOOL
="${ALTMOUNTPOOL}${OUTPUT}"
2116 if [ ! -z "$ZVOLPOOL" ]
2119 log_note
"Pool $pool is built from $ZVOLPOOL on $1"
2122 if [ ! -z "$FILEPOOL" ]
2125 log_note
"Pool $pool is built from $FILEPOOL on $1"
2128 if [ ! -z "$ALTMOUNTPOOL" ]
2131 log_note
"Pool $pool is built from $ALTMOUNTPOOL on $1"
2135 if [ -z "${DONT_DESTROY}" ]
2139 log_note
"Warning: it is not safe to destroy $1!"
2145 # Get the available ZFS compression options
2146 # $1 option type zfs_set|zfs_compress
2148 function get_compress_opts
2150 typeset COMPRESS_OPTS
2151 typeset GZIP_OPTS
="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2152 gzip-6 gzip-7 gzip-8 gzip-9"
2154 if [[ $1 == "zfs_compress" ]] ; then
2155 COMPRESS_OPTS
="on lzjb"
2156 elif [[ $1 == "zfs_set" ]] ; then
2157 COMPRESS_OPTS
="on off lzjb"
2159 typeset valid_opts
="$COMPRESS_OPTS"
2160 $ZFS get
2>&1 |
$GREP gzip >/dev
/null
2>&1
2161 if [[ $?
-eq 0 ]]; then
2162 valid_opts
="$valid_opts $GZIP_OPTS"
2168 # Verify zfs operation with -p option work as expected
2169 # $1 operation, value could be create, clone or rename
2170 # $2 dataset type, value could be fs or vol
2172 # $4 new dataset name
2174 function verify_opt_p_ops
2179 typeset newdataset
=$4
2181 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2182 log_fail
"$datatype is not supported."
2185 # check parameters accordingly
2190 if [[ $datatype == "vol" ]]; then
2191 ops
="create -V $VOLSIZE"
2195 if [[ -z $newdataset ]]; then
2196 log_fail
"newdataset should not be empty" \
2199 log_must datasetexists
$dataset
2200 log_must snapexists
$dataset
2203 if [[ -z $newdataset ]]; then
2204 log_fail
"newdataset should not be empty" \
2207 log_must datasetexists
$dataset
2208 log_mustnot snapexists
$dataset
2211 log_fail
"$ops is not supported."
2215 # make sure the upper level filesystem does not exist
2216 if datasetexists
${newdataset%/*} ; then
2217 log_must
$ZFS destroy
-rRf ${newdataset%/*}
2220 # without -p option, operation will fail
2221 log_mustnot
$ZFS $ops $dataset $newdataset
2222 log_mustnot datasetexists
$newdataset ${newdataset%/*}
2224 # with -p option, operation should succeed
2225 log_must
$ZFS $ops -p $dataset $newdataset
2228 if ! datasetexists
$newdataset ; then
2229 log_fail
"-p option does not work for $ops"
2232 # when $ops is create or clone, redo the operation still return zero
2233 if [[ $ops != "rename" ]]; then
2234 log_must
$ZFS $ops -p $dataset $newdataset
2241 # Get configuration of pool
2251 if ! poolexists
"$pool" ; then
2254 alt_root
=$
($ZPOOL list
-H $pool |
$AWK '{print $NF}')
2255 if [[ $alt_root == "-" ]]; then
2256 value
=$
($ZDB -C $pool |
$GREP "$config:" |
$AWK -F: \
2259 value
=$
($ZDB -e $pool |
$GREP "$config:" |
$AWK -F: \
2262 if [[ -n $value ]] ; then
2272 # Privated function. Random select one of items from arguments.
2277 function _random_get
2284 ((ind
= RANDOM
% cnt
+ 1))
2286 typeset ret
=$
($ECHO "$str" |
$CUT -f $ind -d ' ')
2291 # Random select one of item from arguments which include NONE string
2293 function random_get_with_non
2298 _random_get
"$cnt" "$@"
2302 # Random select one of item from arguments which doesn't include NONE string
2306 _random_get
"$#" "$@"
2310 # Detect if the current system support slog
2312 function verify_slog_support
2314 typeset dir
=/tmp
/disk.$$
2320 $MKFILE 64M
$vdev $sdev
2323 if ! $ZPOOL create
-n $pool $vdev log
$sdev > /dev
/null
2>&1; then
2332 # The function will generate a dataset name with specific length
2333 # $1, the length of the name
2334 # $2, the base string to construct the name
2336 function gen_dataset_name
2339 typeset basestr
="$2"
2340 typeset
-i baselen
=${#basestr}
2344 if ((len
% baselen
== 0)); then
2345 ((iter
= len
/ baselen
))
2347 ((iter
= len
/ baselen
+ 1))
2349 while ((iter
> 0)); do
2350 l_name
="${l_name}$basestr"
2359 # Get cksum tuple of dataset
2362 # sample zdb output:
2363 # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2364 # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2365 # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2366 # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2367 function datasetcksum
2371 cksum=$
($ZDB -vvv $1 |
$GREP "^Dataset $1 \[" |
$GREP "cksum" \
2372 |
$AWK -F= '{print $7}')
2383 cksum=$
($CKSUM $1 |
$AWK '{print $1}')
2388 # Get the given disk/slice state from the specific field of the pool
2390 function get_device_state
#pool disk field("", "spares","logs")
2393 typeset disk
=${2#$DEV_DSKDIR/}
2394 typeset field
=${3:-$pool}
2396 state
=$
($ZPOOL status
-v "$pool" 2>/dev
/null | \
2397 $NAWK -v device
=$disk -v pool
=$pool -v field
=$field \
2398 'BEGIN {startconfig=0; startfield=0; }
2399 /config:/ {startconfig=1}
2400 (startconfig==1) && ($1==field) {startfield=1; next;}
2401 (startfield==1) && ($1==device) {print $2; exit;}
2403 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2409 # print the given directory filesystem type
2417 if [[ -z $dir ]]; then
2418 log_fail
"Usage: get_fstype <directory>"
2425 $DF -n $dir |
$AWK '{print $3}'
2429 # Given a disk, label it to VTOC regardless what label was on the disk
2435 if [[ -z $disk ]]; then
2436 log_fail
"The disk name is unspecified."
2438 typeset label_file
=/var
/tmp
/labelvtoc.$$
2439 typeset arch
=$
($UNAME -p)
2442 log_note
"Currently unsupported by the test framework"
2446 if [[ $arch == "i386" ]]; then
2447 $ECHO "label" > $label_file
2448 $ECHO "0" >> $label_file
2449 $ECHO "" >> $label_file
2450 $ECHO "q" >> $label_file
2451 $ECHO "q" >> $label_file
2453 $FDISK -B $disk >/dev
/null
2>&1
2454 # wait a while for fdisk finishes
2456 elif [[ $arch == "sparc" ]]; then
2457 $ECHO "label" > $label_file
2458 $ECHO "0" >> $label_file
2459 $ECHO "" >> $label_file
2460 $ECHO "" >> $label_file
2461 $ECHO "" >> $label_file
2462 $ECHO "q" >> $label_file
2464 log_fail
"unknown arch type"
2467 $FORMAT -e -s -d $disk -f $label_file
2468 typeset
-i ret_val
=$?
2471 # wait the format to finish
2474 if ((ret_val
!= 0)); then
2475 log_fail
"unable to label $disk as VTOC."
2482 # check if the system was installed as zfsroot or not
2483 # return: 0 ture, otherwise false
2487 $DF -n / |
$GREP zfs
> /dev
/null
2>&1
2492 # get the root filesystem name if it's zfsroot system.
2494 # return: root filesystem name
2498 rootfs
=$
($AWK '{if ($2 == "/" && $3 == "zfs") print $1}' \
2500 if [[ -z "$rootfs" ]]; then
2501 log_fail
"Can not get rootfs"
2503 $ZFS list
$rootfs > /dev
/null
2>&1
2504 if (($?
== 0)); then
2507 log_fail
"This is not a zfsroot system."
2512 # get the rootfs's pool name
2516 function get_rootpool
2520 rootfs
=$
($AWK '{if ($2 == "/" && $3 =="zfs") print $1}' \
2522 if [[ -z "$rootfs" ]]; then
2523 log_fail
"Can not get rootpool"
2525 $ZFS list
$rootfs > /dev
/null
2>&1
2526 if (($?
== 0)); then
2527 rootpool
=`$ECHO $rootfs | awk -F\/ '{print $1}'`
2530 log_fail
"This is not a zfsroot system."
2535 # Get the sub string from specified source string
2538 # $2 start position. Count from 1
2541 function get_substr
#src_str pos offset
2546 $NAWK -v pos
=$2 -v offset
=$3 '{print substr($0, pos, offset)}'
2550 # Check if the given device is physical device
2552 function is_physical_device
#device
2554 typeset device
=${1#$DEV_DSKDIR}
2555 device
=${device#$DEV_RDSKDIR}
2558 [[ -b "$DEV_DSKDIR/$device" ]] && \
2559 [[ -f /sys
/module
/loop
/parameters
/max_part
]]
2562 $ECHO $device |
$EGREP "^c[0-F]+([td][0-F]+)+$" > /dev
/null
2>&1
2568 # Check if the given device is a real device (ie SCSI device)
2570 function is_real_device
#disk
2573 [[ -z $disk ]] && log_fail
"No argument for disk given."
2576 $LSBLK $DEV_RDSKDIR/$disk -o TYPE |
$EGREP disk
> /dev
/null
2>&1
2582 # Check if the given device is a loop device
2584 function is_loop_device
#disk
2587 [[ -z $disk ]] && log_fail
"No argument for disk given."
2590 $LSBLK $DEV_RDSKDIR/$disk -o TYPE |
$EGREP loop
> /dev
/null
2>&1
2596 # Check if the given device is a multipath device and if there is a sybolic
2597 # link to a device mapper and to a disk
2598 # Currently no support for dm devices alone without multipath
2600 function is_mpath_device
#disk
2603 [[ -z $disk ]] && log_fail
"No argument for disk given."
2606 $LSBLK $DEV_MPATHDIR/$disk -o TYPE |
$EGREP mpath
> /dev
/null
2>&1
2607 if (($?
== 0)); then
2608 $READLINK $DEV_MPATHDIR/$disk > /dev
/null
2>&1
2616 # Set the slice prefix for disk partitioning depending
2617 # on whether the device is a real, multipath, or loop device.
2618 # Currently all disks have to be of the same type, so only
2619 # checks first disk to determine slice prefix.
2621 function set_slice_prefix
2627 while (( i
< $DISK_ARRAY_NUM )); do
2628 disk
="$($ECHO $DISKS | $NAWK '{print $(i + 1)}')"
2629 if ( is_mpath_device
$disk ) && [[ -z $
($ECHO $disk |
awk 'substr($1,18,1)\
2630 ~ /^[[:digit:]]+$/') ]] ||
( is_real_device
$disk ); then
2631 export SLICE_PREFIX
=""
2633 elif ( is_mpath_device
$disk || is_loop_device
$disk ); then
2634 export SLICE_PREFIX
="p"
2637 log_fail
"$disk not supported for partitioning."
2645 # Set the directory path of the listed devices in $DISK_ARRAY_NUM
2646 # Currently all disks have to be of the same type, so only
2647 # checks first disk to determine device directory
2648 # default = /dev (linux)
2649 # real disk = /dev (linux)
2650 # multipath device = /dev/mapper (linux)
2652 function set_device_dir
2658 while (( i
< $DISK_ARRAY_NUM )); do
2659 disk
="$($ECHO $DISKS | $NAWK '{print $(i + 1)}')"
2660 if is_mpath_device
$disk; then
2661 export DEV_DSKDIR
=$DEV_MPATHDIR
2664 export DEV_DSKDIR
=$DEV_RDSKDIR
2670 export DEV_DSKDIR
=$DEV_RDSKDIR
2675 # Get the directory path of given device
2677 function get_device_dir
#device
2681 if ! $
(is_physical_device
$device) ; then
2682 if [[ $device != "/" ]]; then
2685 if [[ -b "$DEV_DSKDIR/$device" ]]; then
2686 device
="$DEV_DSKDIR"
2695 # Get the package name
2697 function get_package_name
2699 typeset dirpath
=${1:-$STC_NAME}
2701 echo "SUNWstc-${dirpath}" |
/usr
/bin
/sed -e "s/\//-/g"
2705 # Get the word numbers from a string separated by white space
2707 function get_word_count
2713 # To verify if the require numbers of disks is given
2715 function verify_disk_count
2717 typeset
-i min
=${2:-1}
2719 typeset
-i count
=$
(get_word_count
"$1")
2721 if ((count
< min
)); then
2722 log_untested
"A minimum of $min disks is required to run." \
2723 " You specified $count disk(s)"
2727 function ds_is_volume
2729 typeset
type=$
(get_prop
type $1)
2730 [[ $type = "volume" ]] && return 0
2734 function ds_is_filesystem
2736 typeset
type=$
(get_prop
type $1)
2737 [[ $type = "filesystem" ]] && return 0
2741 function ds_is_snapshot
2743 typeset
type=$
(get_prop
type $1)
2744 [[ $type = "snapshot" ]] && return 0
2749 # Check if Trusted Extensions are installed and enabled
2751 function is_te_enabled
2753 $SVCS -H -o state labeld
2>/dev
/null |
$GREP "enabled"
2754 if (($?
!= 0)); then
2761 # Utility function to determine if a system has multiple cpus.
2767 (($
($PSRINFO |
$WC -l) > 1))
2773 function get_cpu_freq
2776 lscpu |
$AWK '/CPU MHz/ { print $3 }'
2778 $PSRINFO -v 0 |
$AWK '/processor operates at/ {print $6}'
2782 # Run the given command as the user provided.
2788 log_note
"user:$user $@"
2789 eval \
$SU \
$user -c \"$@
\" > /tmp
/out
2>/tmp
/err
2794 # Check if the pool contains the specified vdevs
2799 # Return 0 if the vdevs are contained in the pool, 1 if any of the specified
2800 # vdevs is not in the pool, and 2 if pool name is missing.
2802 function vdevs_in_pool
2807 if [[ -z $pool ]]; then
2808 log_note
"Missing pool name."
2814 typeset tmpfile
=$
($MKTEMP)
2815 $ZPOOL list
-Hv "$pool" >$tmpfile
2817 $GREP -w ${vdev##*/} $tmpfile >/dev
/null
2>&1
2818 [[ $?
-ne 0 ]] && return 1
2832 max
=$
(echo $
((max
> i ? max
: i
)))
2844 min
=$
(echo $
((min
< i ? min
: i
)))
2851 # Wait for newly created block devices to have their minors created.
2853 function block_device_wait
2862 # Synchronize all the data in pool
2866 function sync_pool
#pool
2868 typeset pool
=${1:-$TESTPOOL}
2872 # Flush all the pool data.
2874 $ZPOOL scrub
$pool >/dev
/null
2>&1
2876 (( $ret != 0 )) && \
2877 log_fail
"$ZPOOL scrub $pool failed."
2879 while ! is_pool_scrubbed
$pool; do
2880 if is_pool_resilvered
$pool ; then
2881 log_fail
"$pool should not be resilver completed."